pax_global_header00006660000000000000000000000064136120730430014511gustar00rootroot0000000000000052 comment=89f1ede511802c025a952c141a48340b8ad62ec6 swaylock-1.5/000077500000000000000000000000001361207304300132125ustar00rootroot00000000000000swaylock-1.5/.build.yml000066400000000000000000000006411361207304300151130ustar00rootroot00000000000000image: alpine/edge packages: - meson - cairo-dev - wayland-dev - wayland-protocols - libxkbcommon-dev - gdk-pixbuf-dev - linux-pam-dev - scdoc sources: - https://github.com/swaywm/swaylock tasks: - setup: | cd swaylock meson build - build: | cd swaylock ninja -C build - build-no-pam: | cd swaylock meson configure build -Dpam=disabled ninja -C build swaylock-1.5/.gitignore000066400000000000000000000000061361207304300151760ustar00rootroot00000000000000build swaylock-1.5/LICENSE000066400000000000000000000020451361207304300142200ustar00rootroot00000000000000Copyright (c) 2016-2019 Drew DeVault Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. swaylock-1.5/README.md000066400000000000000000000026011361207304300144700ustar00rootroot00000000000000# swaylock swaylock is a screen locking utility for Wayland compositors. It is compatible with any Wayland compositor which implements the following Wayland protocols: - wlr-layer-shell - wlr-input-inhibitor - xdg-output - xdg-shell See the man page, `swaylock(1)`, for instructions on using swaylock. ## Release Signatures Releases are signed with [B22DA89A](http://pgp.mit.edu/pks/lookup?op=vindex&search=0x52CB6609B22DA89A) and published [on GitHub](https://github.com/swaywm/swaylock/releases). swaylock releases are managed independently of sway releases. ## Installation ### From Packages Swaylock is available in many distributions. Try installing the "swaylock" package for yours. If you're interested in packaging swaylock for your distribution, stop by the IRC channel or shoot an email to sir@cmpwn.com for advice. ### Compiling from Source Install dependencies: * meson \* * wayland * wayland-protocols \* * libxkbcommon * cairo * gdk-pixbuf2 \*\* * pam (optional) * [scdoc](https://git.sr.ht/~sircmpwn/scdoc) (optional: man pages) \* * git \* _\*Compile-time dep_ _\*\*optional: required for background images other than PNG_ Run these commands: meson build ninja -C build sudo ninja -C build install On systems without PAM, you need to suid the swaylock binary: sudo chmod a+s /usr/local/bin/swaylock Swaylock will drop root permissions shortly after startup. swaylock-1.5/background-image.c000066400000000000000000000072501361207304300165610ustar00rootroot00000000000000#include #include "background-image.h" #include "cairo.h" #include "log.h" enum background_mode parse_background_mode(const char *mode) { if (strcmp(mode, "stretch") == 0) { return BACKGROUND_MODE_STRETCH; } else if (strcmp(mode, "fill") == 0) { return BACKGROUND_MODE_FILL; } else if (strcmp(mode, "fit") == 0) { return BACKGROUND_MODE_FIT; } else if (strcmp(mode, "center") == 0) { return BACKGROUND_MODE_CENTER; } else if (strcmp(mode, "tile") == 0) { return BACKGROUND_MODE_TILE; } else if (strcmp(mode, "solid_color") == 0) { return BACKGROUND_MODE_SOLID_COLOR; } swaylock_log(LOG_ERROR, "Unsupported background mode: %s", mode); return BACKGROUND_MODE_INVALID; } cairo_surface_t *load_background_image(const char *path) { cairo_surface_t *image; #if HAVE_GDK_PIXBUF GError *err = NULL; GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &err); if (!pixbuf) { swaylock_log(LOG_ERROR, "Failed to load background image (%s).", err->message); return NULL; } image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf); g_object_unref(pixbuf); #else image = cairo_image_surface_create_from_png(path); #endif // HAVE_GDK_PIXBUF if (!image) { swaylock_log(LOG_ERROR, "Failed to read background image."); return NULL; } if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS) { swaylock_log(LOG_ERROR, "Failed to read background image: %s." #if !HAVE_GDK_PIXBUF "\nSway was compiled without gdk_pixbuf support, so only" "\nPNG images can be loaded. This is the likely cause." #endif // !HAVE_GDK_PIXBUF , cairo_status_to_string(cairo_surface_status(image))); return NULL; } return image; } void render_background_image(cairo_t *cairo, cairo_surface_t *image, enum background_mode mode, int buffer_width, int buffer_height) { double width = cairo_image_surface_get_width(image); double height = cairo_image_surface_get_height(image); cairo_save(cairo); switch (mode) { case BACKGROUND_MODE_STRETCH: cairo_scale(cairo, (double)buffer_width / width, (double)buffer_height / height); cairo_set_source_surface(cairo, image, 0, 0); break; case BACKGROUND_MODE_FILL: { double window_ratio = (double)buffer_width / buffer_height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { double scale = (double)buffer_width / width; cairo_scale(cairo, scale, scale); cairo_set_source_surface(cairo, image, 0, (double)buffer_height / 2 / scale - height / 2); } else { double scale = (double)buffer_height / height; cairo_scale(cairo, scale, scale); cairo_set_source_surface(cairo, image, (double)buffer_width / 2 / scale - width / 2, 0); } break; } case BACKGROUND_MODE_FIT: { double window_ratio = (double)buffer_width / buffer_height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { double scale = (double)buffer_height / height; cairo_scale(cairo, scale, scale); cairo_set_source_surface(cairo, image, (double)buffer_width / 2 / scale - width / 2, 0); } else { double scale = (double)buffer_width / width; cairo_scale(cairo, scale, scale); cairo_set_source_surface(cairo, image, 0, (double)buffer_height / 2 / scale - height / 2); } break; } case BACKGROUND_MODE_CENTER: cairo_set_source_surface(cairo, image, (double)buffer_width / 2 - width / 2, (double)buffer_height / 2 - height / 2); break; case BACKGROUND_MODE_TILE: { cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); cairo_set_source(cairo, pattern); break; } case BACKGROUND_MODE_SOLID_COLOR: case BACKGROUND_MODE_INVALID: assert(0); break; } cairo_paint(cairo); cairo_restore(cairo); } swaylock-1.5/cairo.c000066400000000000000000000065751361207304300144700ustar00rootroot00000000000000#include #include #include "cairo.h" #if HAVE_GDK_PIXBUF #include #endif void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { cairo_set_source_rgba(cairo, (color >> (3*8) & 0xFF) / 255.0, (color >> (2*8) & 0xFF) / 255.0, (color >> (1*8) & 0xFF) / 255.0, (color >> (0*8) & 0xFF) / 255.0); } cairo_subpixel_order_t to_cairo_subpixel_order(enum wl_output_subpixel subpixel) { switch (subpixel) { case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB: return CAIRO_SUBPIXEL_ORDER_RGB; case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR: return CAIRO_SUBPIXEL_ORDER_BGR; case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB: return CAIRO_SUBPIXEL_ORDER_VRGB; case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR: return CAIRO_SUBPIXEL_ORDER_VBGR; default: return CAIRO_SUBPIXEL_ORDER_DEFAULT; } return CAIRO_SUBPIXEL_ORDER_DEFAULT; } #if HAVE_GDK_PIXBUF cairo_surface_t* gdk_cairo_image_surface_create_from_pixbuf(const GdkPixbuf *gdkbuf) { int chan = gdk_pixbuf_get_n_channels(gdkbuf); if (chan < 3) { return NULL; } const guint8* gdkpix = gdk_pixbuf_read_pixels(gdkbuf); if (!gdkpix) { return NULL; } gint w = gdk_pixbuf_get_width(gdkbuf); gint h = gdk_pixbuf_get_height(gdkbuf); int stride = gdk_pixbuf_get_rowstride(gdkbuf); cairo_format_t fmt = (chan == 3) ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32; cairo_surface_t * cs = cairo_image_surface_create (fmt, w, h); cairo_surface_flush (cs); if ( !cs || cairo_surface_status(cs) != CAIRO_STATUS_SUCCESS) { return NULL; } int cstride = cairo_image_surface_get_stride(cs); unsigned char * cpix = cairo_image_surface_get_data(cs); if (chan == 3) { int i; for (i = h; i; --i) { const guint8 *gp = gdkpix; unsigned char *cp = cpix; const guint8* end = gp + 3*w; while (gp < end) { #if G_BYTE_ORDER == G_LITTLE_ENDIAN cp[0] = gp[2]; cp[1] = gp[1]; cp[2] = gp[0]; #else cp[1] = gp[0]; cp[2] = gp[1]; cp[3] = gp[2]; #endif gp += 3; cp += 4; } gdkpix += stride; cpix += cstride; } } else { /* premul-color = alpha/255 * color/255 * 255 = (alpha*color)/255 * (z/255) = z/256 * 256/255 = z/256 (1 + 1/255) * = z/256 + (z/256)/255 = (z + z/255)/256 * # recurse once * = (z + (z + z/255)/256)/256 * = (z + z/256 + z/256/255) / 256 * # only use 16bit uint operations, loose some precision, * # result is floored. * -> (z + z>>8)>>8 * # add 0x80/255 = 0.5 to convert floor to round * => (z+0x80 + (z+0x80)>>8 ) >> 8 * ------ * tested as equal to lround(z/255.0) for uint z in [0..0xfe02] */ #define PREMUL_ALPHA(x,a,b,z) \ G_STMT_START { z = a * b + 0x80; x = (z + (z >> 8)) >> 8; } \ G_STMT_END int i; for (i = h; i; --i) { const guint8 *gp = gdkpix; unsigned char *cp = cpix; const guint8* end = gp + 4*w; guint z1, z2, z3; while (gp < end) { #if G_BYTE_ORDER == G_LITTLE_ENDIAN PREMUL_ALPHA(cp[0], gp[2], gp[3], z1); PREMUL_ALPHA(cp[1], gp[1], gp[3], z2); PREMUL_ALPHA(cp[2], gp[0], gp[3], z3); cp[3] = gp[3]; #else PREMUL_ALPHA(cp[1], gp[0], gp[3], z1); PREMUL_ALPHA(cp[2], gp[1], gp[3], z2); PREMUL_ALPHA(cp[3], gp[2], gp[3], z3); cp[0] = gp[3]; #endif gp += 4; cp += 4; } gdkpix += stride; cpix += cstride; } #undef PREMUL_ALPHA } cairo_surface_mark_dirty(cs); return cs; } #endif // HAVE_GDK_PIXBUF swaylock-1.5/comm.c000066400000000000000000000044741361207304300143220ustar00rootroot00000000000000#include #include #include #include #include "comm.h" #include "log.h" #include "swaylock.h" static int comm[2][2] = {{-1, -1}, {-1, -1}}; ssize_t read_comm_request(char **buf_ptr) { size_t size; ssize_t amt; amt = read(comm[0][0], &size, sizeof(size)); if (amt == 0) { return 0; } else if (amt < 0) { swaylock_log_errno(LOG_ERROR, "read pw request"); return -1; } swaylock_log(LOG_DEBUG, "received pw check request"); char *buf = malloc(size); if (!buf) { swaylock_log_errno(LOG_ERROR, "failed to malloc pw buffer"); return -1; } size_t offs = 0; do { amt = read(comm[0][0], &buf[offs], size - offs); if (amt <= 0) { swaylock_log_errno(LOG_ERROR, "failed to read pw"); return -1; } offs += (size_t)amt; } while (offs < size); *buf_ptr = buf; return size; } bool write_comm_reply(bool success) { if (write(comm[1][1], &success, sizeof(success)) != sizeof(success)) { swaylock_log_errno(LOG_ERROR, "failed to write pw check result"); return false; } return true; } bool spawn_comm_child(void) { if (pipe(comm[0]) != 0) { swaylock_log_errno(LOG_ERROR, "failed to create pipe"); return false; } if (pipe(comm[1]) != 0) { swaylock_log_errno(LOG_ERROR, "failed to create pipe"); return false; } pid_t child = fork(); if (child < 0) { swaylock_log_errno(LOG_ERROR, "failed to fork"); return false; } else if (child == 0) { close(comm[0][1]); close(comm[1][0]); run_pw_backend_child(); } close(comm[0][0]); close(comm[1][1]); return true; } bool write_comm_request(struct swaylock_password *pw) { bool result = false; size_t len = pw->len + 1; size_t offs = 0; if (write(comm[0][1], &len, sizeof(len)) < 0) { swaylock_log_errno(LOG_ERROR, "Failed to request pw check"); goto out; } do { ssize_t amt = write(comm[0][1], &pw->buffer[offs], len - offs); if (amt < 0) { swaylock_log_errno(LOG_ERROR, "Failed to write pw buffer"); goto out; } offs += amt; } while (offs < len); result = true; out: clear_password_buffer(pw); return result; } bool read_comm_reply(void) { bool result = false; if (read(comm[1][0], &result, sizeof(result)) != sizeof(result)) { swaylock_log_errno(LOG_ERROR, "Failed to read pw result"); result = false; } return result; } int get_comm_reply_fd(void) { return comm[1][0]; } swaylock-1.5/completions/000077500000000000000000000000001361207304300155465ustar00rootroot00000000000000swaylock-1.5/completions/bash/000077500000000000000000000000001361207304300164635ustar00rootroot00000000000000swaylock-1.5/completions/bash/swaylock000066400000000000000000000037751361207304300202560ustar00rootroot00000000000000# swaylock(1) completion _swaylock() { local cur prev _get_comp_words_by_ref -n : cur prev short=( -C -c -d -e -f -F -h -i -k -K -L -l -n -r -s -t -u -v ) long=( --bs-hl-color --caps-lock-bs-hl-color --caps-lock-key-hl-color --color --config --daemonize --debug --disable-caps-lock-text --font --font-size --help --hide-keyboard-layout --ignore-empty-password --image --indicator-caps-lock --indicator-idle-visible --indicator-radius --indicator-thickness --inside-caps-lock-color --inside-clear-color --inside-color --inside-ver-color --inside-wrong-color --key-hl-color --layout-bg-color --layout-border-color --layout-text-color --line-caps-lock-color --line-clear-color --line-color --line-uses-inside --line-uses-ring --line-ver-color --line-wrong-color --no-unlock-indicator --ring-caps-lock-color --ring-clear-color --ring-color --ring-ver-color --ring-wrong-color --scaling --separator-color --show-failed-attempts --show-keyboard-layout --text-caps-lock-color --text-clear-color --text-color --text-ver-color --text-wrong-color --tiling --version ) scaling=( 'stretch' 'fill' 'fit' 'center' 'tile' 'solid_color' ) case $prev in -c|--color) return ;; --scaling) COMPREPLY=($(compgen -W "${scaling[*]}" -- "$cur")) return ;; -i|--image) if grep -q : <<< "$cur"; then output="${cur%%:*}:" cur="${cur#*:}" else output= fi COMPREPLY=($(compgen -f -- "$cur")) return ;; esac if [[ $cur == --* ]]; then COMPREPLY=($(compgen -W "${long[*]}" -- "$cur")) else COMPREPLY=($(compgen -W "${short[*]}" -- "$cur")) COMPREPLY+=($(compgen -W "${long[*]}" -- "$cur")) fi } && complete -F _swaylock swaylock swaylock-1.5/completions/fish/000077500000000000000000000000001361207304300164775ustar00rootroot00000000000000swaylock-1.5/completions/fish/swaylock.fish000066400000000000000000000137211361207304300212120ustar00rootroot00000000000000# swaylock(1) completion complete -c swaylock -l bs-hl-color --description "Sets the color of backspace highlight segments." complete -c swaylock -l caps-lock-bs-hl-color --description "Sets the color of backspace highlight segments when Caps Lock is active." complete -c swaylock -l caps-lock-key-hl-color --description "Sets the color of the key press highlight segments when Caps Lock is active." complete -c swaylock -l color -s c --description "Turn the screen into the given color instead of white." complete -c swaylock -l config -s C --description "Path to the config file." complete -c swaylock -l daemonize -s f --description "Detach from the controlling terminal after locking." complete -c swaylock -l debug -s d --description "Enable debugging output." complete -c swaylock -l disable-caps-lock-text -s L --description "Disable the Caps Lock text." complete -c swaylock -l font --description "Sets the font of the text." complete -c swaylock -l font-size --description "Sets a fixed font size for the indicator text." complete -c swaylock -l help -s h --description "Show help message and quit." complete -c swaylock -l hide-keyboard-layout -s K --description "Hide the current xkb layout while typing." complete -c swaylock -l ignore-empty-password -s e --description "When an empty password is provided, do not validate it." complete -c swaylock -l image -s i --description "Display the given image, optionally only on the given output." complete -c swaylock -l indicator-caps-lock -s l --description "Show the current Caps Lock state also on the indicator." complete -c swaylock -l indicator-idle-visible --description "Sets the indicator to show even if idle." complete -c swaylock -l indicator-radius --description "Sets the indicator radius." complete -c swaylock -l indicator-thickness --description "Sets the indicator thickness." complete -c swaylock -l inside-caps-lock-color --description "Sets the color of the inside of the indicator when Caps Lock is active." complete -c swaylock -l inside-clear-color --description "Sets the color of the inside of the indicator when cleared." complete -c swaylock -l inside-color --description "Sets the color of the inside of the indicator." complete -c swaylock -l inside-ver-color --description "Sets the color of the inside of the indicator when verifying." complete -c swaylock -l inside-wrong-color --description "Sets the color of the inside of the indicator when invalid." complete -c swaylock -l key-hl-color --description "Sets the color of the key press highlight segments." complete -c swaylock -l layout-bg-color --description "Sets the background color of the box containing the layout text." complete -c swaylock -l layout-border-color --description "Sets the color of the border of the box containing the layout text." complete -c swaylock -l layout-text-color --description "Sets the color of the layout text." complete -c swaylock -l line-caps-lock-color --description "Sets the color of the line between the inside and ring when Caps Lock is active." complete -c swaylock -l line-clear-color --description "Sets the color of the line between the inside and ring when cleared." complete -c swaylock -l line-color --description "Sets the color of the line between the inside and ring." complete -c swaylock -l line-uses-inside -s n --description "Use the inside color for the line between the inside and ring." complete -c swaylock -l line-uses-ring -s r --description "Use the ring color for the line between the inside and ring." complete -c swaylock -l line-ver-color --description "Sets the color of the line between the inside and ring when verifying." complete -c swaylock -l line-wrong-color --description "Sets the color of the line between the inside and ring when invalid." complete -c swaylock -l no-unlock-indicator -s u --description "Disable the unlock indicator." complete -c swaylock -l ring-caps-lock-color --description "Sets the color of the ring of the indicator when Caps Lock is active." complete -c swaylock -l ring-clear-color --description "Sets the color of the ring of the indicator when cleared." complete -c swaylock -l ring-color --description "Sets the color of the ring of the indicator." complete -c swaylock -l ring-ver-color --description "Sets the color of the ring of the indicator when verifying." complete -c swaylock -l ring-wrong-color --description "Sets the color of the ring of the indicator when invalid." complete -c swaylock -l scaling -s s --description "Image scaling mode: stretch, fill, fit, center, tile, solid_color." complete -c swaylock -l separator-color --description "Sets the color of the lines that separate highlight segments." complete -c swaylock -l show-failed-attempts -s F --description "Show current count of failed authentication attempts." complete -c swaylock -l show-keyboard-layout -s k --description "Display the current xkb layout while typing." complete -c swaylock -l text-caps-lock-color --description "Sets the color of the text when Caps Lock is active." complete -c swaylock -l text-clear-color --description "Sets the color of the text when cleared." complete -c swaylock -l text-color --description "Sets the color of the text." complete -c swaylock -l text-ver-color --description "Sets the color of the text when verifying." complete -c swaylock -l text-wrong-color --description "Sets the color of the text when invalid." complete -c swaylock -l tiling -s t --description "Same as --scaling=tile." complete -c swaylock -l version -s v --description "Show the version number and quit." swaylock-1.5/completions/zsh/000077500000000000000000000000001361207304300163525ustar00rootroot00000000000000swaylock-1.5/completions/zsh/_swaylock000066400000000000000000000123111361207304300202660ustar00rootroot00000000000000#compdef swaylock # # Completion script for swaylock # _arguments -s \ '(--bs-hl-color)'--bs-hl-color'[Sets the color of backspace highlight segments]:color:' \ '(--caps-lock-bs-hl-color)'--caps-lock-bs-hl-color'[Sets the color of backspace highlight segments when Caps Lock is active]:color:' \ '(--caps-lock-key-hl-color)'--caps-lock-key-hl-color'[Sets the color of the key press highlight segments when Caps Lock is active]:color:' \ '(--color -c)'{--color,-c}'[Turn the screen into the given color instead of white]:color:' \ '(--config -C)'{--config,-C}'[Path to the config file]:filename:_files' \ '(--daemonize -f)'{--daemonize,-f}'[Detach from the controlling terminal after locking]' \ '(--debug -d)'{--debug,-d}'[Enable debugging output]' \ '(--disable-caps-lock-text -L)'{--disable-caps-lock-text,-L}'[Disable the Caps Lock text]' \ '(--font)'--font'[Sets the font of the text]:font:' \ '(--font-size)'--font-size'[Sets a fixed font size for the indicator text]' \ '(--help -h)'{--help,-h}'[Show help message and quit]' \ '(--hide-keyboard-layout -K)'{--hide-keyboard-layout,-K}'[Hide the current xkb layout while typing]' \ '(--ignore-empty-password -e)'{--ignore-empty-password,-e}'[When an empty password is provided, do not validate it]' \ '(--image -i)'{--image,-i}'[Display the given image, optionally only on the given output]:filename:_files' \ '(--indicator-caps-lock -l)'{--indicator-caps-lock,-l}'[Show the current Caps Lock state also on the indicator]' \ '(--indicator-idle-visible)'--indicator-idle-visible'[Sets the indicator to show even if idle]' \ '(--indicator-radius)'--indicator-radius'[Sets the indicator radius]:radius:' \ '(--indicator-thickness)'--indicator-thickness'[Sets the indicator thickness]:thickness:' \ '(--inside-caps-lock-color)'--inside-caps-lock-color'[Sets the color of the inside of the indicator when Caps Lock is active]:color:' \ '(--inside-clear-color)'--inside-clear-color'[Sets the color of the inside of the indicator when cleared]:color:' \ '(--inside-color)'--inside-color'[Sets the color of the inside of the indicator]:color:' \ '(--inside-ver-color)'--inside-ver-color'[Sets the color of the inside of the indicator when verifying]:color:' \ '(--inside-wrong-color)'--inside-wrong-color'[Sets the color of the inside of the indicator when invalid]:color:' \ '(--key-hl-color)'--key-hl-color'[Sets the color of the key press highlight segments]:color:' \ '(--layout-bg-color)'--layout-bg-color'[Sets the background color of the box containing the layout text]:color:' \ '(--layout-border-color)'--layout-border-color'[Sets the color of the border of the box containing the layout text]:color:' \ '(--layout-text-color)'--layout-text-color'[Sets the color of the layout text]:color:' \ '(--line-caps-lock-color)'--line-caps-lock-color'[Sets the color of the line between the inside and ring when Caps Lock is active]:color:' \ '(--line-clear-color)'--line-clear-color'[Sets the color of the line between the inside and ring when cleared]:color:' \ '(--line-color)'--line-color'[Sets the color of the line between the inside and ring]:color:' \ '(--line-uses-inside -n)'{--line-uses-inside,-n}'[Use the inside color for the line between the inside and ring]' \ '(--line-uses-ring -r)'{--line-uses-ring,-r}'[Use the ring color for the line between the inside and ring]' \ '(--line-ver-color)'--line-ver-color'[Sets the color of the line between the inside and ring when verifying]:color:' \ '(--line-wrong-color)'--line-wrong-color'[Sets the color of the line between the inside and ring when invalid]:color:' \ '(--no-unlock-indicator -u)'{--no-unlock-indicator,-u}'[Disable the unlock indicator]' \ '(--ring-caps-lock-color)'--ring-caps-lock-color'[Sets the color of the ring of the indicator when Caps Lock is active]:color:' \ '(--ring-clear-color)'--ring-clear-color'[Sets the color of the ring of the indicator when cleared]:color:' \ '(--ring-color)'--ring-color'[Sets the color of the ring of the indicator]:color:' \ '(--ring-ver-color)'--ring-ver-color'[Sets the color of the ring of the indicator when verifying]:color:' \ '(--ring-wrong-color)'--ring-wrong-color'[Sets the color of the ring of the indicator when invalid]:color:' \ '(--scaling -s)'{--scaling,-s}'[Image scaling mode: stretch, fill, fit, center, tile, solid_color]:mode:(stretch fill fit center tile solid_color)' \ '(--separator-color)'--separator-color'[Sets the color of the lines that separate highlight segments]:color:' \ '(--show-failed-attempts -F)'{--show-failed-attempts,-F}'[Show current count of failed authentication attempts]' \ '(--show-keyboard-layout -k)'{--show-keyboard-layout,-k}'[Display the current xkb layout while typing]' \ '(--text-caps-lock-color)'--text-caps-lock-color'[Sets the color of the text when Caps Lock is active]:color:' \ '(--text-clear-color)'--text-clear-color'[Sets the color of the text when cleared]:color:' \ '(--text-color)'--text-color'[Sets the color of the text]:color:' \ '(--text-ver-color)'--text-ver-color'[Sets the color of the text when verifying]:color:' \ '(--text-wrong-color)'--text-wrong-color'[Sets the color of the text when invalid]:color:' \ '(--tiling -t)'{--tiling,-t}'[Same as --scaling=tile]' \ '(--version -v)'{--version,-v}'[Show the version number and quit]' swaylock-1.5/include/000077500000000000000000000000001361207304300146355ustar00rootroot00000000000000swaylock-1.5/include/background-image.h000066400000000000000000000010501361207304300202010ustar00rootroot00000000000000#ifndef _SWAY_BACKGROUND_IMAGE_H #define _SWAY_BACKGROUND_IMAGE_H #include "cairo.h" enum background_mode { BACKGROUND_MODE_STRETCH, BACKGROUND_MODE_FILL, BACKGROUND_MODE_FIT, BACKGROUND_MODE_CENTER, BACKGROUND_MODE_TILE, BACKGROUND_MODE_SOLID_COLOR, BACKGROUND_MODE_INVALID, }; enum background_mode parse_background_mode(const char *mode); cairo_surface_t *load_background_image(const char *path); void render_background_image(cairo_t *cairo, cairo_surface_t *image, enum background_mode mode, int buffer_width, int buffer_height); #endif swaylock-1.5/include/cairo.h000066400000000000000000000011061361207304300161010ustar00rootroot00000000000000#ifndef _SWAY_CAIRO_H #define _SWAY_CAIRO_H #include "config.h" #include #include #include #if HAVE_GDK_PIXBUF #include #endif void cairo_set_source_u32(cairo_t *cairo, uint32_t color); cairo_subpixel_order_t to_cairo_subpixel_order(enum wl_output_subpixel subpixel); cairo_surface_t *cairo_image_surface_scale(cairo_surface_t *image, int width, int height); #if HAVE_GDK_PIXBUF cairo_surface_t* gdk_cairo_image_surface_create_from_pixbuf( const GdkPixbuf *gdkbuf); #endif // HAVE_GDK_PIXBUF #endif swaylock-1.5/include/comm.h000066400000000000000000000007521361207304300157450ustar00rootroot00000000000000#ifndef _SWAYLOCK_COMM_H #define _SWAYLOCK_COMM_H #include struct swaylock_password; bool spawn_comm_child(void); ssize_t read_comm_request(char **buf_ptr); bool write_comm_reply(bool success); // Requests the provided password to be checked. The password is always cleared // when the function returns. bool write_comm_request(struct swaylock_password *pw); bool read_comm_reply(void); // FD to poll for password authentication replies. int get_comm_reply_fd(void); #endif swaylock-1.5/include/log.h000066400000000000000000000015011361207304300155640ustar00rootroot00000000000000#ifndef _SWAYLOCK_LOG_H #define _SWAYLOCK_LOG_H #include #include #include enum log_importance { LOG_SILENT = 0, LOG_ERROR = 1, LOG_INFO = 2, LOG_DEBUG = 3, LOG_IMPORTANCE_LAST, }; void swaylock_log_init(enum log_importance verbosity); #ifdef __GNUC__ #define _ATTRIB_PRINTF(start, end) __attribute__((format(printf, start, end))) #else #define _ATTRIB_PRINTF(start, end) #endif void _swaylock_log(enum log_importance verbosity, const char *format, ...) _ATTRIB_PRINTF(2, 3); const char *_swaylock_strip_path(const char *filepath); #define swaylock_log(verb, fmt, ...) \ _swaylock_log(verb, "[%s:%d] " fmt, _swaylock_strip_path(__FILE__), \ __LINE__, ##__VA_ARGS__) #define swaylock_log_errno(verb, fmt, ...) \ swaylock_log(verb, fmt ": %s", ##__VA_ARGS__, strerror(errno)) #endif swaylock-1.5/include/loop.h000066400000000000000000000022771361207304300157670ustar00rootroot00000000000000#ifndef _SWAY_LOOP_H #define _SWAY_LOOP_H #include /** * This is an event loop system designed for sway clients, not sway itself. * * The loop consists of file descriptors and timers. Typically the Wayland * display's file descriptor will be one of the fds in the loop. */ struct loop; struct loop_timer; /** * Create an event loop. */ struct loop *loop_create(void); /** * Destroy the event loop (eg. on program termination). */ void loop_destroy(struct loop *loop); /** * Poll the event loop. This will block until one of the fds has data. */ void loop_poll(struct loop *loop); /** * Add a file descriptor to the loop. */ void loop_add_fd(struct loop *loop, int fd, short mask, void (*func)(int fd, short mask, void *data), void *data); /** * Add a timer to the loop. * * When the timer expires, the timer will be removed from the loop and freed. */ struct loop_timer *loop_add_timer(struct loop *loop, int ms, void (*callback)(void *data), void *data); /** * Remove a file descriptor from the loop. */ bool loop_remove_fd(struct loop *loop, int fd); /** * Remove a timer from the loop. */ bool loop_remove_timer(struct loop *loop, struct loop_timer *timer); #endif swaylock-1.5/include/meson.build000066400000000000000000000000761361207304300170020ustar00rootroot00000000000000configure_file(output: 'config.h', configuration: conf_data) swaylock-1.5/include/pool-buffer.h000066400000000000000000000007471361207304300172360ustar00rootroot00000000000000#ifndef _SWAY_BUFFERS_H #define _SWAY_BUFFERS_H #include #include #include #include struct pool_buffer { struct wl_buffer *buffer; cairo_surface_t *surface; cairo_t *cairo; uint32_t width, height; void *data; size_t size; bool busy; }; struct pool_buffer *get_next_buffer(struct wl_shm *shm, struct pool_buffer pool[static 2], uint32_t width, uint32_t height); void destroy_buffer(struct pool_buffer *buffer); #endif swaylock-1.5/include/seat.h000066400000000000000000000006241361207304300157440ustar00rootroot00000000000000#ifndef _SWAYLOCK_SEAT_H #define _SWAYLOCK_SEAT_H #include struct swaylock_xkb { bool caps_lock; bool control; struct xkb_state *state; struct xkb_context *context; struct xkb_keymap *keymap; }; struct swaylock_seat { struct swaylock_state *state; struct wl_pointer *pointer; struct wl_keyboard *keyboard; }; extern const struct wl_seat_listener seat_listener; #endif swaylock-1.5/include/swaylock.h000066400000000000000000000071421361207304300166460ustar00rootroot00000000000000#ifndef _SWAYLOCK_H #define _SWAYLOCK_H #include #include #include #include "background-image.h" #include "cairo.h" #include "pool-buffer.h" #include "seat.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" enum auth_state { AUTH_STATE_IDLE, AUTH_STATE_CLEAR, AUTH_STATE_INPUT, AUTH_STATE_INPUT_NOP, AUTH_STATE_BACKSPACE, AUTH_STATE_VALIDATING, AUTH_STATE_INVALID, }; struct swaylock_colorset { uint32_t input; uint32_t cleared; uint32_t caps_lock; uint32_t verifying; uint32_t wrong; }; struct swaylock_colors { uint32_t background; uint32_t bs_highlight; uint32_t key_highlight; uint32_t caps_lock_bs_highlight; uint32_t caps_lock_key_highlight; uint32_t separator; uint32_t layout_background; uint32_t layout_border; uint32_t layout_text; struct swaylock_colorset inside; struct swaylock_colorset line; struct swaylock_colorset ring; struct swaylock_colorset text; }; struct swaylock_args { struct swaylock_colors colors; enum background_mode mode; char *font; uint32_t font_size; uint32_t radius; uint32_t thickness; uint32_t indicator_x_position; uint32_t indicator_y_position; bool override_indicator_x_position; bool override_indicator_y_position; bool ignore_empty; bool show_indicator; bool show_caps_lock_text; bool show_caps_lock_indicator; bool show_keyboard_layout; bool hide_keyboard_layout; bool show_failed_attempts; bool daemonize; bool indicator_idle_visible; }; struct swaylock_password { size_t len; char buffer[1024]; }; struct swaylock_state { struct loop *eventloop; struct loop_timer *clear_indicator_timer; // clears the indicator struct loop_timer *clear_password_timer; // clears the password buffer struct wl_display *display; struct wl_compositor *compositor; struct wl_subcompositor *subcompositor; struct zwlr_layer_shell_v1 *layer_shell; struct zwlr_input_inhibit_manager_v1 *input_inhibit_manager; struct wl_shm *shm; struct wl_list surfaces; struct wl_list images; struct swaylock_args args; struct swaylock_password password; struct swaylock_xkb xkb; enum auth_state auth_state; int failed_attempts; bool run_display; struct zxdg_output_manager_v1 *zxdg_output_manager; }; struct swaylock_surface { cairo_surface_t *image; struct swaylock_state *state; struct wl_output *output; uint32_t output_global_name; struct zxdg_output_v1 *xdg_output; struct wl_surface *surface; struct wl_surface *child; // surface made into subsurface struct wl_subsurface *subsurface; struct zwlr_layer_surface_v1 *layer_surface; struct pool_buffer buffers[2]; struct pool_buffer indicator_buffers[2]; struct pool_buffer *current_buffer; bool frame_pending, dirty; uint32_t width, height; uint32_t indicator_width, indicator_height; int32_t scale; enum wl_output_subpixel subpixel; char *output_name; struct wl_list link; }; // There is exactly one swaylock_image for each -i argument struct swaylock_image { char *path; char *output_name; cairo_surface_t *cairo_surface; struct wl_list link; }; void swaylock_handle_key(struct swaylock_state *state, xkb_keysym_t keysym, uint32_t codepoint); void render_frame_background(struct swaylock_surface *surface); void render_frame(struct swaylock_surface *surface); void render_frames(struct swaylock_state *state); void damage_surface(struct swaylock_surface *surface); void damage_state(struct swaylock_state *state); void clear_password_buffer(struct swaylock_password *pw); void schedule_indicator_clear(struct swaylock_state *state); void initialize_pw_backend(int argc, char **argv); void run_pw_backend_child(void); void clear_buffer(char *buf, size_t size); #endif swaylock-1.5/include/unicode.h000066400000000000000000000012501361207304300164320ustar00rootroot00000000000000#ifndef _SWAY_UNICODE_H #define _SWAY_UNICODE_H #include #include // Technically UTF-8 supports up to 6 byte codepoints, but Unicode itself // doesn't really bother with more than 4. #define UTF8_MAX_SIZE 4 #define UTF8_INVALID 0x80 /** * Grabs the next UTF-8 character and advances the string pointer */ uint32_t utf8_decode(const char **str); /** * Encodes a character as UTF-8 and returns the length of that character. */ size_t utf8_encode(char *str, uint32_t ch); /** * Returns the size of the next UTF-8 character */ int utf8_size(const char *str); /** * Returns the size of a UTF-8 character */ size_t utf8_chsize(uint32_t ch); #endif swaylock-1.5/log.c000066400000000000000000000026321361207304300141420ustar00rootroot00000000000000#define _POSIX_C_SOURCE 199506L #include #include #include #include #include #include #include #include "log.h" static enum log_importance log_importance = LOG_ERROR; static const char *verbosity_colors[] = { [LOG_SILENT] = "", [LOG_ERROR ] = "\x1B[1;31m", [LOG_INFO ] = "\x1B[1;34m", [LOG_DEBUG ] = "\x1B[1;30m", }; void swaylock_log_init(enum log_importance verbosity) { if (verbosity < LOG_IMPORTANCE_LAST) { log_importance = verbosity; } } void _swaylock_log(enum log_importance verbosity, const char *fmt, ...) { if (verbosity > log_importance) { return; } va_list args; va_start(args, fmt); // prefix the time to the log message struct tm result; time_t t = time(NULL); struct tm *tm_info = localtime_r(&t, &result); char buffer[26]; // generate time prefix strftime(buffer, sizeof(buffer), "%F %T - ", tm_info); fprintf(stderr, "%s", buffer); unsigned c = (verbosity < LOG_IMPORTANCE_LAST) ? verbosity : LOG_IMPORTANCE_LAST - 1; if (isatty(STDERR_FILENO)) { fprintf(stderr, "%s", verbosity_colors[c]); } vfprintf(stderr, fmt, args); if (isatty(STDERR_FILENO)) { fprintf(stderr, "\x1B[0m"); } fprintf(stderr, "\n"); va_end(args); } const char *_swaylock_strip_path(const char *filepath) { if (*filepath == '.') { while (*filepath == '.' || *filepath == '/') { ++filepath; } } return filepath; } swaylock-1.5/loop.c000066400000000000000000000114511361207304300143310ustar00rootroot00000000000000#define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include "log.h" #include "loop.h" struct loop_fd_event { void (*callback)(int fd, short mask, void *data); void *data; struct wl_list link; // struct loop_fd_event::link }; struct loop_timer { void (*callback)(void *data); void *data; struct timespec expiry; struct wl_list link; // struct loop_timer::link }; struct loop { struct pollfd *fds; int fd_length; int fd_capacity; struct wl_list fd_events; // struct loop_fd_event::link struct wl_list timers; // struct loop_timer::link }; struct loop *loop_create(void) { struct loop *loop = calloc(1, sizeof(struct loop)); if (!loop) { swaylock_log(LOG_ERROR, "Unable to allocate memory for loop"); return NULL; } loop->fd_capacity = 10; loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity); wl_list_init(&loop->fd_events); wl_list_init(&loop->timers); return loop; } void loop_destroy(struct loop *loop) { struct loop_fd_event *event = NULL, *tmp_event = NULL; wl_list_for_each_safe(event, tmp_event, &loop->fd_events, link) { wl_list_remove(&event->link); free(event); } struct loop_timer *timer = NULL, *tmp_timer = NULL; wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) { wl_list_remove(&timer->link); free(timer); } free(loop->fds); free(loop); } void loop_poll(struct loop *loop) { // Calculate next timer in ms int ms = INT_MAX; if (!wl_list_empty(&loop->timers)) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); struct loop_timer *timer = NULL; wl_list_for_each(timer, &loop->timers, link) { int timer_ms = (timer->expiry.tv_sec - now.tv_sec) * 1000; timer_ms += (timer->expiry.tv_nsec - now.tv_nsec) / 1000000; if (timer_ms < ms) { ms = timer_ms; } } } if (ms < 0) { ms = 0; } poll(loop->fds, loop->fd_length, ms); // Dispatch fds size_t fd_index = 0; struct loop_fd_event *event = NULL; wl_list_for_each(event, &loop->fd_events, link) { struct pollfd pfd = loop->fds[fd_index]; // Always send these events unsigned events = pfd.events | POLLHUP | POLLERR; if (pfd.revents & events) { event->callback(pfd.fd, pfd.revents, event->data); } ++fd_index; } // Dispatch timers if (!wl_list_empty(&loop->timers)) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); struct loop_timer *timer = NULL, *tmp_timer = NULL; wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) { bool expired = timer->expiry.tv_sec < now.tv_sec || (timer->expiry.tv_sec == now.tv_sec && timer->expiry.tv_nsec < now.tv_nsec); if (expired) { timer->callback(timer->data); loop_remove_timer(loop, timer); } } } } void loop_add_fd(struct loop *loop, int fd, short mask, void (*callback)(int fd, short mask, void *data), void *data) { struct loop_fd_event *event = calloc(1, sizeof(struct loop_fd_event)); if (!event) { swaylock_log(LOG_ERROR, "Unable to allocate memory for event"); return; } event->callback = callback; event->data = data; wl_list_insert(loop->fd_events.prev, &event->link); struct pollfd pfd = {fd, mask, 0}; if (loop->fd_length == loop->fd_capacity) { loop->fd_capacity += 10; loop->fds = realloc(loop->fds, sizeof(struct pollfd) * loop->fd_capacity); } loop->fds[loop->fd_length++] = pfd; } struct loop_timer *loop_add_timer(struct loop *loop, int ms, void (*callback)(void *data), void *data) { struct loop_timer *timer = calloc(1, sizeof(struct loop_timer)); if (!timer) { swaylock_log(LOG_ERROR, "Unable to allocate memory for timer"); return NULL; } timer->callback = callback; timer->data = data; clock_gettime(CLOCK_MONOTONIC, &timer->expiry); timer->expiry.tv_sec += ms / 1000; long int nsec = (ms % 1000) * 1000000; if (timer->expiry.tv_nsec + nsec >= 1000000000) { timer->expiry.tv_sec++; nsec -= 1000000000; } timer->expiry.tv_nsec += nsec; wl_list_insert(&loop->timers, &timer->link); return timer; } bool loop_remove_fd(struct loop *loop, int fd) { size_t fd_index = 0; struct loop_fd_event *event = NULL, *tmp_event = NULL; wl_list_for_each_safe(event, tmp_event, &loop->fd_events, link) { if (loop->fds[fd_index].fd == fd) { wl_list_remove(&event->link); free(event); loop->fd_length--; memmove(&loop->fds[fd_index], &loop->fds[fd_index + 1], sizeof(struct pollfd) * (loop->fd_length - fd_index)); return true; } ++fd_index; } return false; } bool loop_remove_timer(struct loop *loop, struct loop_timer *remove) { struct loop_timer *timer = NULL, *tmp_timer = NULL; wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) { if (timer == remove) { wl_list_remove(&timer->link); free(timer); return true; } } return false; } swaylock-1.5/main.c000066400000000000000000001073461361207304300143150ustar00rootroot00000000000000#define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "background-image.h" #include "cairo.h" #include "comm.h" #include "log.h" #include "loop.h" #include "pool-buffer.h" #include "seat.h" #include "swaylock.h" #include "wlr-input-inhibitor-unstable-v1-client-protocol.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "xdg-output-unstable-v1-client-protocol.h" static uint32_t parse_color(const char *color) { if (color[0] == '#') { ++color; } int len = strlen(color); if (len != 6 && len != 8) { swaylock_log(LOG_DEBUG, "Invalid color %s, defaulting to 0xFFFFFFFF", color); return 0xFFFFFFFF; } uint32_t res = (uint32_t)strtoul(color, NULL, 16); if (strlen(color) == 6) { res = (res << 8) | 0xFF; } return res; } int lenient_strcmp(char *a, char *b) { if (a == b) { return 0; } else if (!a) { return -1; } else if (!b) { return 1; } else { return strcmp(a, b); } } static void daemonize(void) { int fds[2]; if (pipe(fds) != 0) { swaylock_log(LOG_ERROR, "Failed to pipe"); exit(1); } if (fork() == 0) { setsid(); close(fds[0]); int devnull = open("/dev/null", O_RDWR); dup2(STDOUT_FILENO, devnull); dup2(STDERR_FILENO, devnull); close(devnull); uint8_t success = 0; if (chdir("/") != 0) { write(fds[1], &success, 1); exit(1); } success = 1; if (write(fds[1], &success, 1) != 1) { exit(1); } close(fds[1]); } else { close(fds[1]); uint8_t success; if (read(fds[0], &success, 1) != 1 || !success) { swaylock_log(LOG_ERROR, "Failed to daemonize"); exit(1); } close(fds[0]); exit(0); } } static void destroy_surface(struct swaylock_surface *surface) { wl_list_remove(&surface->link); if (surface->layer_surface != NULL) { zwlr_layer_surface_v1_destroy(surface->layer_surface); } if (surface->surface != NULL) { wl_surface_destroy(surface->surface); } destroy_buffer(&surface->buffers[0]); destroy_buffer(&surface->buffers[1]); wl_output_destroy(surface->output); free(surface); } static const struct zwlr_layer_surface_v1_listener layer_surface_listener; static cairo_surface_t *select_image(struct swaylock_state *state, struct swaylock_surface *surface); static bool surface_is_opaque(struct swaylock_surface *surface) { if (surface->image) { return cairo_surface_get_content(surface->image) == CAIRO_CONTENT_COLOR; } return (surface->state->args.colors.background & 0xff) == 0xff; } static void create_layer_surface(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; surface->image = select_image(state, surface); surface->surface = wl_compositor_create_surface(state->compositor); assert(surface->surface); surface->child = wl_compositor_create_surface(state->compositor); assert(surface->child); surface->subsurface = wl_subcompositor_get_subsurface(state->subcompositor, surface->child, surface->surface); assert(surface->subsurface); wl_subsurface_set_sync(surface->subsurface); surface->layer_surface = zwlr_layer_shell_v1_get_layer_surface( state->layer_shell, surface->surface, surface->output, ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, "lockscreen"); assert(surface->layer_surface); zwlr_layer_surface_v1_set_size(surface->layer_surface, 0, 0); zwlr_layer_surface_v1_set_anchor(surface->layer_surface, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT); zwlr_layer_surface_v1_set_exclusive_zone(surface->layer_surface, -1); zwlr_layer_surface_v1_set_keyboard_interactivity( surface->layer_surface, true); zwlr_layer_surface_v1_add_listener(surface->layer_surface, &layer_surface_listener, surface); if (surface_is_opaque(surface) && surface->state->args.mode != BACKGROUND_MODE_CENTER && surface->state->args.mode != BACKGROUND_MODE_FIT) { struct wl_region *region = wl_compositor_create_region(surface->state->compositor); wl_region_add(region, 0, 0, INT32_MAX, INT32_MAX); wl_surface_set_opaque_region(surface->surface, region); wl_region_destroy(region); } wl_surface_commit(surface->surface); } static void layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *layer_surface, uint32_t serial, uint32_t width, uint32_t height) { struct swaylock_surface *surface = data; surface->width = width; surface->height = height; surface->indicator_width = 1; surface->indicator_height = 1; zwlr_layer_surface_v1_ack_configure(layer_surface, serial); render_frame_background(surface); render_frame(surface); } static void layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *layer_surface) { struct swaylock_surface *surface = data; destroy_surface(surface); } static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { .configure = layer_surface_configure, .closed = layer_surface_closed, }; static const struct wl_callback_listener surface_frame_listener; static void surface_frame_handle_done(void *data, struct wl_callback *callback, uint32_t time) { struct swaylock_surface *surface = data; wl_callback_destroy(callback); surface->frame_pending = false; if (surface->dirty) { // Schedule a frame in case the surface is damaged again struct wl_callback *callback = wl_surface_frame(surface->surface); wl_callback_add_listener(callback, &surface_frame_listener, surface); surface->frame_pending = true; render_frame(surface); surface->dirty = false; } } static const struct wl_callback_listener surface_frame_listener = { .done = surface_frame_handle_done, }; void damage_surface(struct swaylock_surface *surface) { surface->dirty = true; if (surface->frame_pending) { return; } struct wl_callback *callback = wl_surface_frame(surface->surface); wl_callback_add_listener(callback, &surface_frame_listener, surface); surface->frame_pending = true; wl_surface_commit(surface->surface); } void damage_state(struct swaylock_state *state) { struct swaylock_surface *surface; wl_list_for_each(surface, &state->surfaces, link) { damage_surface(surface); } } static void handle_wl_output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel, const char *make, const char *model, int32_t transform) { struct swaylock_surface *surface = data; surface->subpixel = subpixel; if (surface->state->run_display) { damage_surface(surface); } } static void handle_wl_output_mode(void *data, struct wl_output *output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) { // Who cares } static void handle_wl_output_done(void *data, struct wl_output *output) { // Who cares } static void handle_wl_output_scale(void *data, struct wl_output *output, int32_t factor) { struct swaylock_surface *surface = data; surface->scale = factor; if (surface->state->run_display) { damage_surface(surface); } } struct wl_output_listener _wl_output_listener = { .geometry = handle_wl_output_geometry, .mode = handle_wl_output_mode, .done = handle_wl_output_done, .scale = handle_wl_output_scale, }; static void handle_xdg_output_logical_size(void *data, struct zxdg_output_v1 *output, int width, int height) { // Who cares } static void handle_xdg_output_logical_position(void *data, struct zxdg_output_v1 *output, int x, int y) { // Who cares } static void handle_xdg_output_name(void *data, struct zxdg_output_v1 *output, const char *name) { swaylock_log(LOG_DEBUG, "output name is %s", name); struct swaylock_surface *surface = data; surface->xdg_output = output; surface->output_name = strdup(name); } static void handle_xdg_output_description(void *data, struct zxdg_output_v1 *output, const char *description) { // Who cares } static void handle_xdg_output_done(void *data, struct zxdg_output_v1 *output) { // Who cares } struct zxdg_output_v1_listener _xdg_output_listener = { .logical_position = handle_xdg_output_logical_position, .logical_size = handle_xdg_output_logical_size, .done = handle_xdg_output_done, .name = handle_xdg_output_name, .description = handle_xdg_output_description, }; static void handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { struct swaylock_state *state = data; if (strcmp(interface, wl_compositor_interface.name) == 0) { state->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 3); } else if (strcmp(interface, wl_subcompositor_interface.name) == 0) { state->subcompositor = wl_registry_bind(registry, name, &wl_subcompositor_interface, 1); } else if (strcmp(interface, wl_shm_interface.name) == 0) { state->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); } else if (strcmp(interface, wl_seat_interface.name) == 0) { struct wl_seat *seat = wl_registry_bind( registry, name, &wl_seat_interface, 3); struct swaylock_seat *swaylock_seat = calloc(1, sizeof(struct swaylock_seat)); swaylock_seat->state = state; wl_seat_add_listener(seat, &seat_listener, swaylock_seat); } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { state->layer_shell = wl_registry_bind( registry, name, &zwlr_layer_shell_v1_interface, 1); } else if (strcmp(interface, zwlr_input_inhibit_manager_v1_interface.name) == 0) { state->input_inhibit_manager = wl_registry_bind( registry, name, &zwlr_input_inhibit_manager_v1_interface, 1); } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) { state->zxdg_output_manager = wl_registry_bind( registry, name, &zxdg_output_manager_v1_interface, 2); } else if (strcmp(interface, wl_output_interface.name) == 0) { struct swaylock_surface *surface = calloc(1, sizeof(struct swaylock_surface)); surface->state = state; surface->output = wl_registry_bind(registry, name, &wl_output_interface, 3); surface->output_global_name = name; wl_output_add_listener(surface->output, &_wl_output_listener, surface); wl_list_insert(&state->surfaces, &surface->link); if (state->run_display) { create_layer_surface(surface); wl_display_roundtrip(state->display); } } } static void handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { struct swaylock_state *state = data; struct swaylock_surface *surface; wl_list_for_each(surface, &state->surfaces, link) { if (surface->output_global_name == name) { destroy_surface(surface); break; } } } static const struct wl_registry_listener registry_listener = { .global = handle_global, .global_remove = handle_global_remove, }; static cairo_surface_t *select_image(struct swaylock_state *state, struct swaylock_surface *surface) { struct swaylock_image *image; cairo_surface_t *default_image = NULL; wl_list_for_each(image, &state->images, link) { if (lenient_strcmp(image->output_name, surface->output_name) == 0) { return image->cairo_surface; } else if (!image->output_name) { default_image = image->cairo_surface; } } return default_image; } static char *join_args(char **argv, int argc) { assert(argc > 0); int len = 0, i; for (i = 0; i < argc; ++i) { len += strlen(argv[i]) + 1; } char *res = malloc(len); len = 0; for (i = 0; i < argc; ++i) { strcpy(res + len, argv[i]); len += strlen(argv[i]); res[len++] = ' '; } res[len - 1] = '\0'; return res; } static void load_image(char *arg, struct swaylock_state *state) { // [[]:] struct swaylock_image *image = calloc(1, sizeof(struct swaylock_image)); char *separator = strchr(arg, ':'); if (separator) { *separator = '\0'; image->output_name = separator == arg ? NULL : strdup(arg); image->path = strdup(separator + 1); } else { image->output_name = NULL; image->path = strdup(arg); } struct swaylock_image *iter_image, *temp; wl_list_for_each_safe(iter_image, temp, &state->images, link) { if (lenient_strcmp(iter_image->output_name, image->output_name) == 0) { if (image->output_name) { swaylock_log(LOG_DEBUG, "Replacing image defined for output %s with %s", image->output_name, image->path); } else { swaylock_log(LOG_DEBUG, "Replacing default image with %s", image->path); } wl_list_remove(&iter_image->link); free(iter_image->cairo_surface); free(iter_image->output_name); free(iter_image->path); free(iter_image); break; } } // The shell will not expand ~ to the value of $HOME when an output name is // given. Also, any image paths given in the config file need to have shell // expansions performed wordexp_t p; while (strstr(image->path, " ")) { image->path = realloc(image->path, strlen(image->path) + 2); char *ptr = strstr(image->path, " ") + 1; memmove(ptr + 1, ptr, strlen(ptr) + 1); *ptr = '\\'; } if (wordexp(image->path, &p, 0) == 0) { free(image->path); image->path = join_args(p.we_wordv, p.we_wordc); wordfree(&p); } // Load the actual image image->cairo_surface = load_background_image(image->path); if (!image->cairo_surface) { free(image); return; } wl_list_insert(&state->images, &image->link); swaylock_log(LOG_DEBUG, "Loaded image %s for output %s", image->path, image->output_name ? image->output_name : "*"); } static void set_default_colors(struct swaylock_colors *colors) { colors->background = 0xFFFFFFFF; colors->bs_highlight = 0xDB3300FF; colors->key_highlight = 0x33DB00FF; colors->caps_lock_bs_highlight = 0xDB3300FF; colors->caps_lock_key_highlight = 0x33DB00FF; colors->separator = 0x000000FF; colors->layout_background = 0x000000C0; colors->layout_border = 0x00000000; colors->layout_text = 0xFFFFFFFF; colors->inside = (struct swaylock_colorset){ .input = 0x000000C0, .cleared = 0xE5A445C0, .caps_lock = 0x000000C0, .verifying = 0x0072FFC0, .wrong = 0xFA0000C0, }; colors->line = (struct swaylock_colorset){ .input = 0x000000FF, .cleared = 0x000000FF, .caps_lock = 0x000000FF, .verifying = 0x000000FF, .wrong = 0x000000FF, }; colors->ring = (struct swaylock_colorset){ .input = 0x337D00FF, .cleared = 0xE5A445FF, .caps_lock = 0xE5A445FF, .verifying = 0x3300FFFF, .wrong = 0x7D3300FF, }; colors->text = (struct swaylock_colorset){ .input = 0xE5A445FF, .cleared = 0x000000FF, .caps_lock = 0xE5A445FF, .verifying = 0x000000FF, .wrong = 0x000000FF, }; } enum line_mode { LM_LINE, LM_INSIDE, LM_RING, }; static int parse_options(int argc, char **argv, struct swaylock_state *state, enum line_mode *line_mode, char **config_path) { enum long_option_codes { LO_BS_HL_COLOR = 256, LO_CAPS_LOCK_BS_HL_COLOR, LO_CAPS_LOCK_KEY_HL_COLOR, LO_FONT, LO_FONT_SIZE, LO_IND_IDLE_VISIBLE, LO_IND_RADIUS, LO_IND_X_POSITION, LO_IND_Y_POSITION, LO_IND_THICKNESS, LO_INSIDE_COLOR, LO_INSIDE_CLEAR_COLOR, LO_INSIDE_CAPS_LOCK_COLOR, LO_INSIDE_VER_COLOR, LO_INSIDE_WRONG_COLOR, LO_KEY_HL_COLOR, LO_LAYOUT_TXT_COLOR, LO_LAYOUT_BG_COLOR, LO_LAYOUT_BORDER_COLOR, LO_LINE_COLOR, LO_LINE_CLEAR_COLOR, LO_LINE_CAPS_LOCK_COLOR, LO_LINE_VER_COLOR, LO_LINE_WRONG_COLOR, LO_RING_COLOR, LO_RING_CLEAR_COLOR, LO_RING_CAPS_LOCK_COLOR, LO_RING_VER_COLOR, LO_RING_WRONG_COLOR, LO_SEP_COLOR, LO_TEXT_COLOR, LO_TEXT_CLEAR_COLOR, LO_TEXT_CAPS_LOCK_COLOR, LO_TEXT_VER_COLOR, LO_TEXT_WRONG_COLOR, }; static struct option long_options[] = { {"config", required_argument, NULL, 'C'}, {"color", required_argument, NULL, 'c'}, {"debug", no_argument, NULL, 'd'}, {"ignore-empty-password", no_argument, NULL, 'e'}, {"daemonize", no_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'}, {"image", required_argument, NULL, 'i'}, {"disable-caps-lock-text", no_argument, NULL, 'L'}, {"indicator-caps-lock", no_argument, NULL, 'l'}, {"line-uses-inside", no_argument, NULL, 'n'}, {"line-uses-ring", no_argument, NULL, 'r'}, {"scaling", required_argument, NULL, 's'}, {"tiling", no_argument, NULL, 't'}, {"no-unlock-indicator", no_argument, NULL, 'u'}, {"show-keyboard-layout", no_argument, NULL, 'k'}, {"hide-keyboard-layout", no_argument, NULL, 'K'}, {"show-failed-attempts", no_argument, NULL, 'F'}, {"version", no_argument, NULL, 'v'}, {"bs-hl-color", required_argument, NULL, LO_BS_HL_COLOR}, {"caps-lock-bs-hl-color", required_argument, NULL, LO_CAPS_LOCK_BS_HL_COLOR}, {"caps-lock-key-hl-color", required_argument, NULL, LO_CAPS_LOCK_KEY_HL_COLOR}, {"font", required_argument, NULL, LO_FONT}, {"font-size", required_argument, NULL, LO_FONT_SIZE}, {"indicator-idle-visible", no_argument, NULL, LO_IND_IDLE_VISIBLE}, {"indicator-radius", required_argument, NULL, LO_IND_RADIUS}, {"indicator-thickness", required_argument, NULL, LO_IND_THICKNESS}, {"indicator-x-position", required_argument, NULL, LO_IND_X_POSITION}, {"indicator-y-position", required_argument, NULL, LO_IND_Y_POSITION}, {"inside-color", required_argument, NULL, LO_INSIDE_COLOR}, {"inside-clear-color", required_argument, NULL, LO_INSIDE_CLEAR_COLOR}, {"inside-caps-lock-color", required_argument, NULL, LO_INSIDE_CAPS_LOCK_COLOR}, {"inside-ver-color", required_argument, NULL, LO_INSIDE_VER_COLOR}, {"inside-wrong-color", required_argument, NULL, LO_INSIDE_WRONG_COLOR}, {"key-hl-color", required_argument, NULL, LO_KEY_HL_COLOR}, {"layout-bg-color", required_argument, NULL, LO_LAYOUT_BG_COLOR}, {"layout-border-color", required_argument, NULL, LO_LAYOUT_BORDER_COLOR}, {"layout-text-color", required_argument, NULL, LO_LAYOUT_TXT_COLOR}, {"line-color", required_argument, NULL, LO_LINE_COLOR}, {"line-clear-color", required_argument, NULL, LO_LINE_CLEAR_COLOR}, {"line-caps-lock-color", required_argument, NULL, LO_LINE_CAPS_LOCK_COLOR}, {"line-ver-color", required_argument, NULL, LO_LINE_VER_COLOR}, {"line-wrong-color", required_argument, NULL, LO_LINE_WRONG_COLOR}, {"ring-color", required_argument, NULL, LO_RING_COLOR}, {"ring-clear-color", required_argument, NULL, LO_RING_CLEAR_COLOR}, {"ring-caps-lock-color", required_argument, NULL, LO_RING_CAPS_LOCK_COLOR}, {"ring-ver-color", required_argument, NULL, LO_RING_VER_COLOR}, {"ring-wrong-color", required_argument, NULL, LO_RING_WRONG_COLOR}, {"separator-color", required_argument, NULL, LO_SEP_COLOR}, {"text-color", required_argument, NULL, LO_TEXT_COLOR}, {"text-clear-color", required_argument, NULL, LO_TEXT_CLEAR_COLOR}, {"text-caps-lock-color", required_argument, NULL, LO_TEXT_CAPS_LOCK_COLOR}, {"text-ver-color", required_argument, NULL, LO_TEXT_VER_COLOR}, {"text-wrong-color", required_argument, NULL, LO_TEXT_WRONG_COLOR}, {0, 0, 0, 0} }; const char usage[] = "Usage: swaylock [options...]\n" "\n" " -C, --config " "Path to the config file.\n" " -c, --color " "Turn the screen into the given color instead of white.\n" " -d, --debug " "Enable debugging output.\n" " -e, --ignore-empty-password " "When an empty password is provided, do not validate it.\n" " -F, --show-failed-attempts " "Show current count of failed authentication attempts.\n" " -f, --daemonize " "Detach from the controlling terminal after locking.\n" " -h, --help " "Show help message and quit.\n" " -i, --image [[]:] " "Display the given image, optionally only on the given output.\n" " -k, --show-keyboard-layout " "Display the current xkb layout while typing.\n" " -K, --hide-keyboard-layout " "Hide the current xkb layout while typing.\n" " -L, --disable-caps-lock-text " "Disable the Caps Lock text.\n" " -l, --indicator-caps-lock " "Show the current Caps Lock state also on the indicator.\n" " -s, --scaling " "Image scaling mode: stretch, fill, fit, center, tile, solid_color.\n" " -t, --tiling " "Same as --scaling=tile.\n" " -u, --no-unlock-indicator " "Disable the unlock indicator.\n" " -v, --version " "Show the version number and quit.\n" " --bs-hl-color " "Sets the color of backspace highlight segments.\n" " --caps-lock-bs-hl-color " "Sets the color of backspace highlight segments when Caps Lock " "is active.\n" " --caps-lock-key-hl-color " "Sets the color of the key press highlight segments when " "Caps Lock is active.\n" " --font " "Sets the font of the text.\n" " --font-size " "Sets a fixed font size for the indicator text.\n" " --indicator-idle-visible " "Sets the indicator to show even if idle.\n" " --indicator-radius " "Sets the indicator radius.\n" " --indicator-thickness " "Sets the indicator thickness.\n" " --indicator-x-position " "Sets the horizontal position of the indicator.\n" " --indicator-y-position " "Sets the vertical position of the indicator.\n" " --inside-color " "Sets the color of the inside of the indicator.\n" " --inside-clear-color " "Sets the color of the inside of the indicator when cleared.\n" " --inside-caps-lock-color " "Sets the color of the inside of the indicator when Caps Lock " "is active.\n" " --inside-ver-color " "Sets the color of the inside of the indicator when verifying.\n" " --inside-wrong-color " "Sets the color of the inside of the indicator when invalid.\n" " --key-hl-color " "Sets the color of the key press highlight segments.\n" " --layout-bg-color " "Sets the background color of the box containing the layout text.\n" " --layout-border-color " "Sets the color of the border of the box containing the layout text.\n" " --layout-text-color " "Sets the color of the layout text.\n" " --line-color " "Sets the color of the line between the inside and ring.\n" " --line-clear-color " "Sets the color of the line between the inside and ring when " "cleared.\n" " --line-caps-lock-color " "Sets the color of the line between the inside and ring when " "Caps Lock is active.\n" " --line-ver-color " "Sets the color of the line between the inside and ring when " "verifying.\n" " --line-wrong-color " "Sets the color of the line between the inside and ring when " "invalid.\n" " -n, --line-uses-inside " "Use the inside color for the line between the inside and ring.\n" " -r, --line-uses-ring " "Use the ring color for the line between the inside and ring.\n" " --ring-color " "Sets the color of the ring of the indicator.\n" " --ring-clear-color " "Sets the color of the ring of the indicator when cleared.\n" " --ring-caps-lock-color " "Sets the color of the ring of the indicator when Caps Lock " "is active.\n" " --ring-ver-color " "Sets the color of the ring of the indicator when verifying.\n" " --ring-wrong-color " "Sets the color of the ring of the indicator when invalid.\n" " --separator-color " "Sets the color of the lines that separate highlight segments.\n" " --text-color " "Sets the color of the text.\n" " --text-clear-color " "Sets the color of the text when cleared.\n" " --text-caps-lock-color " "Sets the color of the text when Caps Lock is active.\n" " --text-ver-color " "Sets the color of the text when verifying.\n" " --text-wrong-color " "Sets the color of the text when invalid.\n" "\n" "All options are of the form .\n"; int c; optind = 1; while (1) { int opt_idx = 0; c = getopt_long(argc, argv, "c:deFfhi:kKLlnrs:tuvC:", long_options, &opt_idx); if (c == -1) { break; } switch (c) { case 'C': if (config_path) { *config_path = strdup(optarg); } break; case 'c': if (state) { state->args.colors.background = parse_color(optarg); } break; case 'd': swaylock_log_init(LOG_DEBUG); break; case 'e': if (state) { state->args.ignore_empty = true; } break; case 'F': if (state) { state->args.show_failed_attempts = true; } break; case 'f': if (state) { state->args.daemonize = true; } break; case 'i': if (state) { load_image(optarg, state); } break; case 'k': if (state) { state->args.show_keyboard_layout = true; } break; case 'K': if (state) { state->args.hide_keyboard_layout = true; } break; case 'L': if (state) { state->args.show_caps_lock_text = false; } break; case 'l': if (state) { state->args.show_caps_lock_indicator = true; } break; case 'n': if (line_mode) { *line_mode = LM_INSIDE; } break; case 'r': if (line_mode) { *line_mode = LM_RING; } break; case 's': if (state) { state->args.mode = parse_background_mode(optarg); if (state->args.mode == BACKGROUND_MODE_INVALID) { return 1; } } break; case 't': if (state) { state->args.mode = BACKGROUND_MODE_TILE; } break; case 'u': if (state) { state->args.show_indicator = false; } break; case 'v': fprintf(stdout, "swaylock version " SWAYLOCK_VERSION "\n"); exit(EXIT_SUCCESS); break; case LO_BS_HL_COLOR: if (state) { state->args.colors.bs_highlight = parse_color(optarg); } break; case LO_CAPS_LOCK_BS_HL_COLOR: if (state) { state->args.colors.caps_lock_bs_highlight = parse_color(optarg); } break; case LO_CAPS_LOCK_KEY_HL_COLOR: if (state) { state->args.colors.caps_lock_key_highlight = parse_color(optarg); } break; case LO_FONT: if (state) { free(state->args.font); state->args.font = strdup(optarg); } break; case LO_FONT_SIZE: if (state) { state->args.font_size = atoi(optarg); } break; case LO_IND_IDLE_VISIBLE: if (state) { state->args.indicator_idle_visible = true; } break; case LO_IND_RADIUS: if (state) { state->args.radius = strtol(optarg, NULL, 0); } break; case LO_IND_THICKNESS: if (state) { state->args.thickness = strtol(optarg, NULL, 0); } break; case LO_IND_X_POSITION: if (state) { state->args.override_indicator_x_position = true; state->args.indicator_x_position = atoi(optarg); } break; case LO_IND_Y_POSITION: if (state) { state->args.override_indicator_y_position = true; state->args.indicator_y_position = atoi(optarg); } break; case LO_INSIDE_COLOR: if (state) { state->args.colors.inside.input = parse_color(optarg); } break; case LO_INSIDE_CLEAR_COLOR: if (state) { state->args.colors.inside.cleared = parse_color(optarg); } break; case LO_INSIDE_CAPS_LOCK_COLOR: if (state) { state->args.colors.inside.caps_lock = parse_color(optarg); } break; case LO_INSIDE_VER_COLOR: if (state) { state->args.colors.inside.verifying = parse_color(optarg); } break; case LO_INSIDE_WRONG_COLOR: if (state) { state->args.colors.inside.wrong = parse_color(optarg); } break; case LO_KEY_HL_COLOR: if (state) { state->args.colors.key_highlight = parse_color(optarg); } break; case LO_LAYOUT_BG_COLOR: if (state) { state->args.colors.layout_background = parse_color(optarg); } break; case LO_LAYOUT_BORDER_COLOR: if (state) { state->args.colors.layout_border = parse_color(optarg); } break; case LO_LAYOUT_TXT_COLOR: if (state) { state->args.colors.layout_text = parse_color(optarg); } break; case LO_LINE_COLOR: if (state) { state->args.colors.line.input = parse_color(optarg); } break; case LO_LINE_CLEAR_COLOR: if (state) { state->args.colors.line.cleared = parse_color(optarg); } break; case LO_LINE_CAPS_LOCK_COLOR: if (state) { state->args.colors.line.caps_lock = parse_color(optarg); } break; case LO_LINE_VER_COLOR: if (state) { state->args.colors.line.verifying = parse_color(optarg); } break; case LO_LINE_WRONG_COLOR: if (state) { state->args.colors.line.wrong = parse_color(optarg); } break; case LO_RING_COLOR: if (state) { state->args.colors.ring.input = parse_color(optarg); } break; case LO_RING_CLEAR_COLOR: if (state) { state->args.colors.ring.cleared = parse_color(optarg); } break; case LO_RING_CAPS_LOCK_COLOR: if (state) { state->args.colors.ring.caps_lock = parse_color(optarg); } break; case LO_RING_VER_COLOR: if (state) { state->args.colors.ring.verifying = parse_color(optarg); } break; case LO_RING_WRONG_COLOR: if (state) { state->args.colors.ring.wrong = parse_color(optarg); } break; case LO_SEP_COLOR: if (state) { state->args.colors.separator = parse_color(optarg); } break; case LO_TEXT_COLOR: if (state) { state->args.colors.text.input = parse_color(optarg); } break; case LO_TEXT_CLEAR_COLOR: if (state) { state->args.colors.text.cleared = parse_color(optarg); } break; case LO_TEXT_CAPS_LOCK_COLOR: if (state) { state->args.colors.text.caps_lock = parse_color(optarg); } break; case LO_TEXT_VER_COLOR: if (state) { state->args.colors.text.verifying = parse_color(optarg); } break; case LO_TEXT_WRONG_COLOR: if (state) { state->args.colors.text.wrong = parse_color(optarg); } break; default: fprintf(stderr, "%s", usage); return 1; } } return 0; } static bool file_exists(const char *path) { return path && access(path, R_OK) != -1; } static char *get_config_path(void) { static const char *config_paths[] = { "$HOME/.swaylock/config", "$XDG_CONFIG_HOME/swaylock/config", SYSCONFDIR "/swaylock/config", }; char *config_home = getenv("XDG_CONFIG_HOME"); if (!config_home || config_home[0] == '\0') { config_paths[1] = "$HOME/.config/swaylock/config"; } wordexp_t p; char *path; for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { if (wordexp(config_paths[i], &p, 0) == 0) { path = strdup(p.we_wordv[0]); wordfree(&p); if (file_exists(path)) { return path; } free(path); } } return NULL; } static int load_config(char *path, struct swaylock_state *state, enum line_mode *line_mode) { FILE *config = fopen(path, "r"); if (!config) { swaylock_log(LOG_ERROR, "Failed to read config. Running without it."); return 0; } char *line = NULL; size_t line_size = 0; ssize_t nread; int line_number = 0; int result = 0; while ((nread = getline(&line, &line_size, config)) != -1) { line_number++; if (line[nread - 1] == '\n') { line[--nread] = '\0'; } if (!*line || line[0] == '#') { continue; } swaylock_log(LOG_DEBUG, "Config Line #%d: %s", line_number, line); char *flag = malloc(nread + 3); if (flag == NULL) { free(line); free(config); swaylock_log(LOG_ERROR, "Failed to allocate memory"); return 0; } sprintf(flag, "--%s", line); char *argv[] = {"swaylock", flag}; result = parse_options(2, argv, state, line_mode, NULL); free(flag); if (result != 0) { break; } } free(line); fclose(config); return 0; } static struct swaylock_state state; static void display_in(int fd, short mask, void *data) { if (wl_display_dispatch(state.display) == -1) { state.run_display = false; } } static void comm_in(int fd, short mask, void *data) { if (read_comm_reply()) { // Authentication succeeded state.run_display = false; } else { state.auth_state = AUTH_STATE_INVALID; schedule_indicator_clear(&state); ++state.failed_attempts; damage_state(&state); } } int main(int argc, char **argv) { swaylock_log_init(LOG_ERROR); initialize_pw_backend(argc, argv); srand(time(NULL)); enum line_mode line_mode = LM_LINE; state.failed_attempts = 0; state.args = (struct swaylock_args){ .mode = BACKGROUND_MODE_FILL, .font = strdup("sans-serif"), .font_size = 0, .radius = 50, .thickness = 10, .indicator_x_position = 0, .indicator_y_position = 0, .override_indicator_x_position = false, .override_indicator_y_position = false, .ignore_empty = false, .show_indicator = true, .show_caps_lock_indicator = false, .show_caps_lock_text = true, .show_keyboard_layout = false, .hide_keyboard_layout = false, .show_failed_attempts = false, .indicator_idle_visible = false }; wl_list_init(&state.images); set_default_colors(&state.args.colors); char *config_path = NULL; int result = parse_options(argc, argv, NULL, NULL, &config_path); if (result != 0) { free(config_path); return result; } if (!config_path) { config_path = get_config_path(); } if (config_path) { swaylock_log(LOG_DEBUG, "Found config at %s", config_path); int config_status = load_config(config_path, &state, &line_mode); free(config_path); if (config_status != 0) { free(state.args.font); return config_status; } } if (argc > 1) { swaylock_log(LOG_DEBUG, "Parsing CLI Args"); int result = parse_options(argc, argv, &state, &line_mode, NULL); if (result != 0) { free(state.args.font); return result; } } if (line_mode == LM_INSIDE) { state.args.colors.line = state.args.colors.inside; } else if (line_mode == LM_RING) { state.args.colors.line = state.args.colors.ring; } #ifdef __linux__ // Most non-linux platforms require root to mlock() if (mlock(state.password.buffer, sizeof(state.password.buffer)) != 0) { swaylock_log(LOG_ERROR, "Unable to mlock() password memory."); return EXIT_FAILURE; } #endif wl_list_init(&state.surfaces); state.xkb.context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); state.display = wl_display_connect(NULL); if (!state.display) { free(state.args.font); swaylock_log(LOG_ERROR, "Unable to connect to the compositor. " "If your compositor is running, check or set the " "WAYLAND_DISPLAY environment variable."); return EXIT_FAILURE; } struct wl_registry *registry = wl_display_get_registry(state.display); wl_registry_add_listener(registry, ®istry_listener, &state); wl_display_roundtrip(state.display); assert(state.compositor && state.layer_shell && state.shm); if (!state.input_inhibit_manager) { free(state.args.font); swaylock_log(LOG_ERROR, "Compositor does not support the input " "inhibitor protocol, refusing to run insecurely"); return 1; } zwlr_input_inhibit_manager_v1_get_inhibitor(state.input_inhibit_manager); if (wl_display_roundtrip(state.display) == -1) { free(state.args.font); swaylock_log(LOG_ERROR, "Exiting - failed to inhibit input:" " is another lockscreen already running?"); return 2; } if (state.zxdg_output_manager) { struct swaylock_surface *surface; wl_list_for_each(surface, &state.surfaces, link) { surface->xdg_output = zxdg_output_manager_v1_get_xdg_output( state.zxdg_output_manager, surface->output); zxdg_output_v1_add_listener( surface->xdg_output, &_xdg_output_listener, surface); } wl_display_roundtrip(state.display); } else { swaylock_log(LOG_INFO, "Compositor does not support zxdg output " "manager, images assigned to named outputs will not work"); } struct swaylock_surface *surface; wl_list_for_each(surface, &state.surfaces, link) { create_layer_surface(surface); } if (state.args.daemonize) { wl_display_roundtrip(state.display); daemonize(); } state.eventloop = loop_create(); loop_add_fd(state.eventloop, wl_display_get_fd(state.display), POLLIN, display_in, NULL); loop_add_fd(state.eventloop, get_comm_reply_fd(), POLLIN, comm_in, NULL); state.run_display = true; while (state.run_display) { errno = 0; if (wl_display_flush(state.display) == -1 && errno != EAGAIN) { break; } loop_poll(state.eventloop); } free(state.args.font); return 0; } swaylock-1.5/meson.build000066400000000000000000000123461361207304300153620ustar00rootroot00000000000000project( 'swaylock', 'c', version: '1.4', license: 'MIT', meson_version: '>=0.48.0', default_options: [ 'c_std=c11', 'warning_level=2', 'werror=true', ], ) add_project_arguments( [ '-Wno-unused-parameter', '-Wno-unused-result', '-Wundef', '-Wvla', ], language: 'c', ) cc = meson.get_compiler('c') sysconfdir = get_option('sysconfdir') prefix = get_option('prefix') is_freebsd = host_machine.system().startswith('freebsd') add_project_arguments( '-DSYSCONFDIR="/@0@"'.format(join_paths(prefix, sysconfdir)), language : 'c') if is_freebsd add_project_arguments('-D_C11_SOURCE', language: 'c') endif wayland_client = dependency('wayland-client') wayland_protos = dependency('wayland-protocols', version: '>=1.14') xkbcommon = dependency('xkbcommon') cairo = dependency('cairo') gdk_pixbuf = dependency('gdk-pixbuf-2.0', required: get_option('gdk-pixbuf')) bash_comp = dependency('bash-completion', required: false) fish_comp = dependency('fish', required: false) libpam = cc.find_library('pam', required: get_option('pam')) crypt = cc.find_library('crypt', required: not libpam.found()) math = cc.find_library('m') git = find_program('git', required: false) scdoc = find_program('scdoc', required: get_option('man-pages')) wayland_scanner = find_program('wayland-scanner') version = '"@0@"'.format(meson.project_version()) if git.found() git_commit_hash = run_command([git.path(), 'describe', '--always', '--tags']) git_branch = run_command([git.path(), 'rev-parse', '--abbrev-ref', 'HEAD']) if git_commit_hash.returncode() == 0 and git_branch.returncode() == 0 version = '"@0@ (" __DATE__ ", branch \'@1@\')"'.format(git_commit_hash.stdout().strip(), git_branch.stdout().strip()) endif endif add_project_arguments('-DSWAYLOCK_VERSION=@0@'.format(version), language: 'c') wl_protocol_dir = wayland_protos.get_pkgconfig_variable('pkgdatadir') if wayland_client.version().version_compare('>=1.14.91') code_type = 'private-code' else code_type = 'code' endif wayland_scanner_code = generator( wayland_scanner, output: '@BASENAME@-protocol.c', arguments: [code_type, '@INPUT@', '@OUTPUT@'], ) wayland_scanner_client = generator( wayland_scanner, output: '@BASENAME@-client-protocol.h', arguments: ['client-header', '@INPUT@', '@OUTPUT@'], ) client_protos_src = [] client_protos_headers = [] client_protocols = [ [wl_protocol_dir, 'stable/xdg-shell/xdg-shell.xml'], [wl_protocol_dir, 'unstable/xdg-output/xdg-output-unstable-v1.xml'], ['wlr-layer-shell-unstable-v1.xml'], ['wlr-input-inhibitor-unstable-v1.xml'], ] foreach p : client_protocols xml = join_paths(p) client_protos_src += wayland_scanner_code.process(xml) client_protos_headers += wayland_scanner_client.process(xml) endforeach lib_client_protos = static_library( 'client_protos', client_protos_src + client_protos_headers, dependencies: [wayland_client] ) # for the include directory client_protos = declare_dependency( link_with: lib_client_protos, sources: client_protos_headers, ) conf_data = configuration_data() conf_data.set10('HAVE_GDK_PIXBUF', gdk_pixbuf.found()) subdir('include') dependencies = [ cairo, client_protos, gdk_pixbuf, math, xkbcommon, wayland_client, ] sources = [ 'background-image.c', 'cairo.c', 'comm.c', 'log.c', 'loop.c', 'main.c', 'password.c', 'pool-buffer.c', 'render.c', 'seat.c', 'unicode.c', ] if libpam.found() sources += ['pam.c'] dependencies += [libpam] else warning('The swaylock binary must be setuid when compiled without libpam') warning('You must do this manually post-install: chmod a+s /path/to/swaylock') sources += ['shadow.c'] dependencies += [crypt] endif swaylock_inc = include_directories('include') executable('swaylock', sources, include_directories: [swaylock_inc], dependencies: dependencies, install: true ) install_data( 'pam/swaylock', install_dir: sysconfdir + '/pam.d/' ) if scdoc.found() sh = find_program('sh') mandir = get_option('mandir') man_files = [ 'swaylock.1.scd', ] foreach filename : man_files topic = filename.split('.')[-3].split('/')[-1] section = filename.split('.')[-2] output = '@0@.@1@'.format(topic, section) custom_target( output, input: filename, output: output, command: [ sh, '-c', '@0@ < @INPUT@ > @1@'.format(scdoc.path(), output) ], install: true, install_dir: '@0@/man@1@'.format(mandir, section) ) endforeach endif datadir = get_option('datadir') if get_option('zsh-completions') zsh_files = files( 'completions/zsh/_swaylock', ) zsh_install_dir = datadir + '/zsh/site-functions' install_data(zsh_files, install_dir: zsh_install_dir) endif if get_option('bash-completions') bash_files = files( 'completions/bash/swaylock', ) if bash_comp.found() bash_install_dir = bash_comp.get_pkgconfig_variable('completionsdir') else bash_install_dir = datadir + '/bash-completion/completions' endif install_data(bash_files, install_dir: bash_install_dir) endif if get_option('fish-completions') fish_files = files( 'completions/fish/swaylock.fish', ) if fish_comp.found() fish_install_dir = fish_comp.get_pkgconfig_variable('completionsdir') else fish_install_dir = datadir + '/fish/vendor_completions.d' endif install_data(fish_files, install_dir: fish_install_dir) endif swaylock-1.5/meson_options.txt000066400000000000000000000011341361207304300166460ustar00rootroot00000000000000option('pam', type: 'feature', value: 'auto', description: 'Use PAM instead of shadow') option('gdk-pixbuf', type: 'feature', value: 'auto', description: 'Enable support for more image formats') option('man-pages', type: 'feature', value: 'auto', description: 'Generate and install man pages') option('zsh-completions', type: 'boolean', value: true, description: 'Install zsh shell completions') option('bash-completions', type: 'boolean', value: true, description: 'Install bash shell completions') option('fish-completions', type: 'boolean', value: true, description: 'Install fish shell completions') swaylock-1.5/pam.c000066400000000000000000000061121361207304300141330ustar00rootroot00000000000000#define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include "comm.h" #include "log.h" #include "swaylock.h" static char *pw_buf = NULL; void initialize_pw_backend(int argc, char **argv) { if (getuid() != geteuid() || getgid() != getegid()) { swaylock_log(LOG_ERROR, "swaylock is setuid, but was compiled with the PAM" " backend. Run 'chmod a-s %s' to fix. Aborting.", argv[0]); exit(EXIT_FAILURE); } if (!spawn_comm_child()) { exit(EXIT_FAILURE); } } static int handle_conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *data) { /* PAM expects an array of responses, one for each message */ struct pam_response *pam_reply = calloc(num_msg, sizeof(struct pam_response)); if (pam_reply == NULL) { swaylock_log(LOG_ERROR, "Allocation failed"); return PAM_ABORT; } *resp = pam_reply; for (int i = 0; i < num_msg; ++i) { switch (msg[i]->msg_style) { case PAM_PROMPT_ECHO_OFF: case PAM_PROMPT_ECHO_ON: pam_reply[i].resp = strdup(pw_buf); // PAM clears and frees this if (pam_reply[i].resp == NULL) { swaylock_log(LOG_ERROR, "Allocation failed"); return PAM_ABORT; } break; case PAM_ERROR_MSG: case PAM_TEXT_INFO: break; } } return PAM_SUCCESS; } static const char *get_pam_auth_error(int pam_status) { switch (pam_status) { case PAM_AUTH_ERR: return "invalid credentials"; case PAM_CRED_INSUFFICIENT: return "swaylock cannot authenticate users; check /etc/pam.d/swaylock " "has been installed properly"; case PAM_AUTHINFO_UNAVAIL: return "authentication information unavailable"; case PAM_MAXTRIES: return "maximum number of authentication tries exceeded"; default:; static char msg[64]; snprintf(msg, sizeof(msg), "unknown error (%d)", pam_status); return msg; } } void run_pw_backend_child(void) { struct passwd *passwd = getpwuid(getuid()); char *username = passwd->pw_name; const struct pam_conv conv = { .conv = handle_conversation, .appdata_ptr = NULL, }; pam_handle_t *auth_handle = NULL; if (pam_start("swaylock", username, &conv, &auth_handle) != PAM_SUCCESS) { swaylock_log(LOG_ERROR, "pam_start failed"); exit(EXIT_FAILURE); } /* This code does not run as root */ swaylock_log(LOG_DEBUG, "Prepared to authorize user %s", username); int pam_status = PAM_SUCCESS; while (1) { ssize_t size = read_comm_request(&pw_buf); if (size < 0) { exit(EXIT_FAILURE); } else if (size == 0) { break; } int pam_status = pam_authenticate(auth_handle, 0); bool success = pam_status == PAM_SUCCESS; if (!success) { swaylock_log(LOG_ERROR, "pam_authenticate failed: %s", get_pam_auth_error(pam_status)); } if (!write_comm_reply(success)) { clear_buffer(pw_buf, size); exit(EXIT_FAILURE); } clear_buffer(pw_buf, size); free(pw_buf); pw_buf = NULL; } if (pam_end(auth_handle, pam_status) != PAM_SUCCESS) { swaylock_log(LOG_ERROR, "pam_end failed"); exit(EXIT_FAILURE); } exit((pam_status == PAM_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE); } swaylock-1.5/pam/000077500000000000000000000000001361207304300137675ustar00rootroot00000000000000swaylock-1.5/pam/swaylock000066400000000000000000000002411361207304300155430ustar00rootroot00000000000000# # PAM configuration file for the swaylock screen locker. By default, it includes # the 'login' configuration file (see /etc/pam.d/login) # auth include login swaylock-1.5/password.c000066400000000000000000000101641361207304300152220ustar00rootroot00000000000000#include #include #include #include #include #include #include #include "comm.h" #include "log.h" #include "loop.h" #include "seat.h" #include "swaylock.h" #include "unicode.h" void clear_buffer(char *buf, size_t size) { // Use volatile keyword so so compiler can't optimize this out. volatile char *buffer = buf; volatile char zero = '\0'; for (size_t i = 0; i < size; ++i) { buffer[i] = zero; } } void clear_password_buffer(struct swaylock_password *pw) { clear_buffer(pw->buffer, sizeof(pw->buffer)); pw->len = 0; } static bool backspace(struct swaylock_password *pw) { if (pw->len != 0) { pw->buffer[--pw->len] = 0; return true; } return false; } static void append_ch(struct swaylock_password *pw, uint32_t codepoint) { size_t utf8_size = utf8_chsize(codepoint); if (pw->len + utf8_size + 1 >= sizeof(pw->buffer)) { // TODO: Display error return; } utf8_encode(&pw->buffer[pw->len], codepoint); pw->buffer[pw->len + utf8_size] = 0; pw->len += utf8_size; } static void clear_indicator(void *data) { struct swaylock_state *state = data; state->clear_indicator_timer = NULL; state->auth_state = AUTH_STATE_IDLE; damage_state(state); } void schedule_indicator_clear(struct swaylock_state *state) { if (state->clear_indicator_timer) { loop_remove_timer(state->eventloop, state->clear_indicator_timer); } state->clear_indicator_timer = loop_add_timer( state->eventloop, 3000, clear_indicator, state); } static void clear_password(void *data) { struct swaylock_state *state = data; state->clear_password_timer = NULL; state->auth_state = AUTH_STATE_CLEAR; clear_password_buffer(&state->password); damage_state(state); schedule_indicator_clear(state); } static void schedule_password_clear(struct swaylock_state *state) { if (state->clear_password_timer) { loop_remove_timer(state->eventloop, state->clear_password_timer); } state->clear_password_timer = loop_add_timer( state->eventloop, 10000, clear_password, state); } static void submit_password(struct swaylock_state *state) { if (state->args.ignore_empty && state->password.len == 0) { return; } state->auth_state = AUTH_STATE_VALIDATING; if (!write_comm_request(&state->password)) { state->auth_state = AUTH_STATE_INVALID; schedule_indicator_clear(state); } damage_state(state); } void swaylock_handle_key(struct swaylock_state *state, xkb_keysym_t keysym, uint32_t codepoint) { // Ignore input events if validating if (state->auth_state == AUTH_STATE_VALIDATING) { return; } switch (keysym) { case XKB_KEY_KP_Enter: /* fallthrough */ case XKB_KEY_Return: submit_password(state); break; case XKB_KEY_Delete: case XKB_KEY_BackSpace: if (backspace(&state->password)) { state->auth_state = AUTH_STATE_BACKSPACE; } else { state->auth_state = AUTH_STATE_CLEAR; } damage_state(state); schedule_indicator_clear(state); schedule_password_clear(state); break; case XKB_KEY_Escape: clear_password_buffer(&state->password); state->auth_state = AUTH_STATE_CLEAR; damage_state(state); schedule_indicator_clear(state); break; case XKB_KEY_Caps_Lock: case XKB_KEY_Shift_L: case XKB_KEY_Shift_R: case XKB_KEY_Control_L: case XKB_KEY_Control_R: case XKB_KEY_Meta_L: case XKB_KEY_Meta_R: case XKB_KEY_Alt_L: case XKB_KEY_Alt_R: case XKB_KEY_Super_L: case XKB_KEY_Super_R: state->auth_state = AUTH_STATE_INPUT_NOP; damage_state(state); schedule_indicator_clear(state); schedule_password_clear(state); break; case XKB_KEY_m: /* fallthrough */ case XKB_KEY_d: case XKB_KEY_j: if (state->xkb.control) { submit_password(state); break; } // fallthrough case XKB_KEY_c: /* fallthrough */ case XKB_KEY_u: if (state->xkb.control) { clear_password_buffer(&state->password); state->auth_state = AUTH_STATE_CLEAR; damage_state(state); schedule_indicator_clear(state); break; } // fallthrough default: if (codepoint) { append_ch(&state->password, codepoint); state->auth_state = AUTH_STATE_INPUT; damage_state(state); schedule_indicator_clear(state); schedule_password_clear(state); } break; } } swaylock-1.5/pool-buffer.c000066400000000000000000000060641361207304300156040ustar00rootroot00000000000000#define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include "pool-buffer.h" static bool set_cloexec(int fd) { long flags = fcntl(fd, F_GETFD); if (flags == -1) { return false; } if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { return false; } return true; } static int create_pool_file(size_t size, char **name) { static const char template[] = "sway-client-XXXXXX"; const char *path = getenv("XDG_RUNTIME_DIR"); if (path == NULL) { fprintf(stderr, "XDG_RUNTIME_DIR is not set\n"); return -1; } size_t name_size = strlen(template) + 1 + strlen(path) + 1; *name = malloc(name_size); if (*name == NULL) { fprintf(stderr, "allocation failed\n"); return -1; } snprintf(*name, name_size, "%s/%s", path, template); int fd = mkstemp(*name); if (fd < 0) { return -1; } if (!set_cloexec(fd)) { close(fd); return -1; } if (ftruncate(fd, size) < 0) { close(fd); return -1; } return fd; } static void buffer_release(void *data, struct wl_buffer *wl_buffer) { struct pool_buffer *buffer = data; buffer->busy = false; } static const struct wl_buffer_listener buffer_listener = { .release = buffer_release }; static struct pool_buffer *create_buffer(struct wl_shm *shm, struct pool_buffer *buf, int32_t width, int32_t height, uint32_t format) { uint32_t stride = width * 4; size_t size = stride * height; char *name; int fd = create_pool_file(size, &name); assert(fd != -1); void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size); buf->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format); wl_shm_pool_destroy(pool); close(fd); unlink(name); free(name); fd = -1; buf->size = size; buf->width = width; buf->height = height; buf->data = data; buf->surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride); buf->cairo = cairo_create(buf->surface); wl_buffer_add_listener(buf->buffer, &buffer_listener, buf); return buf; } void destroy_buffer(struct pool_buffer *buffer) { if (buffer->buffer) { wl_buffer_destroy(buffer->buffer); } if (buffer->cairo) { cairo_destroy(buffer->cairo); } if (buffer->surface) { cairo_surface_destroy(buffer->surface); } if (buffer->data) { munmap(buffer->data, buffer->size); } memset(buffer, 0, sizeof(struct pool_buffer)); } struct pool_buffer *get_next_buffer(struct wl_shm *shm, struct pool_buffer pool[static 2], uint32_t width, uint32_t height) { struct pool_buffer *buffer = NULL; for (size_t i = 0; i < 2; ++i) { if (pool[i].busy) { continue; } buffer = &pool[i]; } if (!buffer) { return NULL; } if (buffer->width != width || buffer->height != height) { destroy_buffer(buffer); } if (!buffer->buffer) { if (!create_buffer(shm, buffer, width, height, WL_SHM_FORMAT_ARGB8888)) { return NULL; } } buffer->busy = true; return buffer; } swaylock-1.5/render.c000066400000000000000000000257071361207304300146500ustar00rootroot00000000000000#include #include #include #include "cairo.h" #include "background-image.h" #include "swaylock.h" #define M_PI 3.14159265358979323846 const float TYPE_INDICATOR_RANGE = M_PI / 3.0f; const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f; static void set_color_for_state(cairo_t *cairo, struct swaylock_state *state, struct swaylock_colorset *colorset) { if (state->auth_state == AUTH_STATE_VALIDATING) { cairo_set_source_u32(cairo, colorset->verifying); } else if (state->auth_state == AUTH_STATE_INVALID) { cairo_set_source_u32(cairo, colorset->wrong); } else if (state->auth_state == AUTH_STATE_CLEAR) { cairo_set_source_u32(cairo, colorset->cleared); } else { if (state->xkb.caps_lock && state->args.show_caps_lock_indicator) { cairo_set_source_u32(cairo, colorset->caps_lock); } else if (state->xkb.caps_lock && !state->args.show_caps_lock_indicator && state->args.show_caps_lock_text) { uint32_t inputtextcolor = state->args.colors.text.input; state->args.colors.text.input = state->args.colors.text.caps_lock; cairo_set_source_u32(cairo, colorset->input); state->args.colors.text.input = inputtextcolor; } else { cairo_set_source_u32(cairo, colorset->input); } } } void render_frame_background(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; int buffer_width = surface->width * surface->scale; int buffer_height = surface->height * surface->scale; if (buffer_width == 0 || buffer_height == 0) { return; // not yet configured } surface->current_buffer = get_next_buffer(state->shm, surface->buffers, buffer_width, buffer_height); if (surface->current_buffer == NULL) { return; } cairo_t *cairo = surface->current_buffer->cairo; cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST); cairo_save(cairo); cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); cairo_set_source_u32(cairo, state->args.colors.background); cairo_paint(cairo); if (surface->image && state->args.mode != BACKGROUND_MODE_SOLID_COLOR) { cairo_set_operator(cairo, CAIRO_OPERATOR_OVER); render_background_image(cairo, surface->image, state->args.mode, buffer_width, buffer_height); } cairo_restore(cairo); cairo_identity_matrix(cairo); wl_surface_set_buffer_scale(surface->surface, surface->scale); wl_surface_attach(surface->surface, surface->current_buffer->buffer, 0, 0); wl_surface_damage(surface->surface, 0, 0, surface->width, surface->height); wl_surface_commit(surface->surface); } void render_frame(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; int arc_radius = state->args.radius * surface->scale; int arc_thickness = state->args.thickness * surface->scale; int buffer_diameter = (arc_radius + arc_thickness) * 2; int buffer_width = surface->indicator_width; int buffer_height = surface->indicator_height; int new_width = buffer_diameter; int new_height = buffer_diameter; int subsurf_xpos; int subsurf_ypos; // Center the indicator unless overridden by the user if (state->args.override_indicator_x_position) { subsurf_xpos = state->args.indicator_x_position - buffer_width / (2 * surface->scale) + 2 / surface->scale; } else { subsurf_xpos = surface->width / 2 - buffer_width / (2 * surface->scale) + 2 / surface->scale; } if (state->args.override_indicator_y_position) { subsurf_ypos = state->args.indicator_y_position - (state->args.radius + state->args.thickness); } else { subsurf_ypos = surface->height / 2 - (state->args.radius + state->args.thickness); } wl_subsurface_set_position(surface->subsurface, subsurf_xpos, subsurf_ypos); surface->current_buffer = get_next_buffer(state->shm, surface->indicator_buffers, buffer_width, buffer_height); if (surface->current_buffer == NULL) { return; } // Hide subsurface until we want it visible wl_surface_attach(surface->child, NULL, 0, 0); wl_surface_commit(surface->child); cairo_t *cairo = surface->current_buffer->cairo; cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST); cairo_font_options_t *fo = cairo_font_options_create(); cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL); cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL); cairo_font_options_set_subpixel_order(fo, to_cairo_subpixel_order(surface->subpixel)); cairo_set_font_options(cairo, fo); cairo_font_options_destroy(fo); cairo_identity_matrix(cairo); // Clear cairo_save(cairo); cairo_set_source_rgba(cairo, 0, 0, 0, 0); cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); cairo_paint(cairo); cairo_restore(cairo); float type_indicator_border_thickness = TYPE_INDICATOR_BORDER_THICKNESS * surface->scale; if (state->args.show_indicator && (state->auth_state != AUTH_STATE_IDLE || state->args.indicator_idle_visible)) { // Draw circle cairo_set_line_width(cairo, arc_thickness); cairo_arc(cairo, buffer_width / 2, buffer_diameter / 2, arc_radius, 0, 2 * M_PI); set_color_for_state(cairo, state, &state->args.colors.inside); cairo_fill_preserve(cairo); set_color_for_state(cairo, state, &state->args.colors.ring); cairo_stroke(cairo); // Draw a message char *text = NULL; const char *layout_text = NULL; char attempts[4]; // like i3lock: count no more than 999 set_color_for_state(cairo, state, &state->args.colors.text); cairo_select_font_face(cairo, state->args.font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); if (state->args.font_size > 0) { cairo_set_font_size(cairo, state->args.font_size); } else { cairo_set_font_size(cairo, arc_radius / 3.0f); } switch (state->auth_state) { case AUTH_STATE_VALIDATING: text = "verifying"; break; case AUTH_STATE_INVALID: text = "wrong"; break; case AUTH_STATE_CLEAR: text = "cleared"; break; case AUTH_STATE_INPUT: case AUTH_STATE_INPUT_NOP: case AUTH_STATE_BACKSPACE: // Caps Lock has higher priority if (state->xkb.caps_lock && state->args.show_caps_lock_text) { text = "Caps Lock"; } else if (state->args.show_failed_attempts && state->failed_attempts > 0) { if (state->failed_attempts > 999) { text = "999+"; } else { snprintf(attempts, sizeof(attempts), "%d", state->failed_attempts); text = attempts; } } xkb_layout_index_t num_layout = xkb_keymap_num_layouts(state->xkb.keymap); if (!state->args.hide_keyboard_layout && (state->args.show_keyboard_layout || num_layout > 1)) { xkb_layout_index_t curr_layout = 0; // advance to the first active layout (if any) while (curr_layout < num_layout && xkb_state_layout_index_is_active(state->xkb.state, curr_layout, XKB_STATE_LAYOUT_EFFECTIVE) != 1) { ++curr_layout; } // will handle invalid index if none are active layout_text = xkb_keymap_layout_get_name(state->xkb.keymap, curr_layout); } break; default: break; } if (text) { cairo_text_extents_t extents; cairo_font_extents_t fe; double x, y; cairo_text_extents(cairo, text, &extents); cairo_font_extents(cairo, &fe); x = (buffer_width / 2) - (extents.width / 2 + extents.x_bearing); y = (buffer_diameter / 2) + (fe.height / 2 - fe.descent); cairo_move_to(cairo, x, y); cairo_show_text(cairo, text); cairo_close_path(cairo); cairo_new_sub_path(cairo); if (new_width < extents.width) { new_width = extents.width; } } // Typing indicator: Highlight random part on keypress if (state->auth_state == AUTH_STATE_INPUT || state->auth_state == AUTH_STATE_BACKSPACE) { static double highlight_start = 0; highlight_start += (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5; cairo_arc(cairo, buffer_width / 2, buffer_diameter / 2, arc_radius, highlight_start, highlight_start + TYPE_INDICATOR_RANGE); if (state->auth_state == AUTH_STATE_INPUT) { if (state->xkb.caps_lock && state->args.show_caps_lock_indicator) { cairo_set_source_u32(cairo, state->args.colors.caps_lock_key_highlight); } else { cairo_set_source_u32(cairo, state->args.colors.key_highlight); } } else { if (state->xkb.caps_lock && state->args.show_caps_lock_indicator) { cairo_set_source_u32(cairo, state->args.colors.caps_lock_bs_highlight); } else { cairo_set_source_u32(cairo, state->args.colors.bs_highlight); } } cairo_stroke(cairo); // Draw borders cairo_set_source_u32(cairo, state->args.colors.separator); cairo_arc(cairo, buffer_width / 2, buffer_diameter / 2, arc_radius, highlight_start, highlight_start + type_indicator_border_thickness); cairo_stroke(cairo); cairo_arc(cairo, buffer_width / 2, buffer_diameter / 2, arc_radius, highlight_start + TYPE_INDICATOR_RANGE, highlight_start + TYPE_INDICATOR_RANGE + type_indicator_border_thickness); cairo_stroke(cairo); } // Draw inner + outer border of the circle set_color_for_state(cairo, state, &state->args.colors.line); cairo_set_line_width(cairo, 2.0 * surface->scale); cairo_arc(cairo, buffer_width / 2, buffer_diameter / 2, arc_radius - arc_thickness / 2, 0, 2 * M_PI); cairo_stroke(cairo); cairo_arc(cairo, buffer_width / 2, buffer_diameter / 2, arc_radius + arc_thickness / 2, 0, 2 * M_PI); cairo_stroke(cairo); // display layout text seperately if (layout_text) { cairo_text_extents_t extents; cairo_font_extents_t fe; double x, y; double box_padding = 4.0 * surface->scale; cairo_text_extents(cairo, layout_text, &extents); cairo_font_extents(cairo, &fe); // upper left coordinates for box x = (buffer_width / 2) - (extents.width / 2) - box_padding; y = buffer_diameter; // background box cairo_rectangle(cairo, x, y, extents.width + 2.0 * box_padding, fe.height + 2.0 * box_padding); cairo_set_source_u32(cairo, state->args.colors.layout_background); cairo_fill_preserve(cairo); // border cairo_set_source_u32(cairo, state->args.colors.layout_border); cairo_stroke(cairo); // take font extents and padding into account cairo_move_to(cairo, x - extents.x_bearing + box_padding, y + (fe.height - fe.descent) + box_padding); cairo_set_source_u32(cairo, state->args.colors.layout_text); cairo_show_text(cairo, layout_text); cairo_new_sub_path(cairo); new_height += fe.height + 2 * box_padding; if (new_width < extents.width + 2 * box_padding) { new_width = extents.width + 2 * box_padding; } } if (buffer_width != new_width || buffer_height != new_height) { destroy_buffer(surface->current_buffer); surface->indicator_width = new_width; surface->indicator_height = new_height; render_frame(surface); } } wl_surface_set_buffer_scale(surface->child, surface->scale); wl_surface_attach(surface->child, surface->current_buffer->buffer, 0, 0); wl_surface_damage(surface->child, 0, 0, surface->current_buffer->width, surface->current_buffer->height); wl_surface_commit(surface->child); wl_surface_commit(surface->surface); } void render_frames(struct swaylock_state *state) { struct swaylock_surface *surface; wl_list_for_each(surface, &state->surfaces, link) { render_frame(surface); } } swaylock-1.5/seat.c000066400000000000000000000130631361207304300143150ustar00rootroot00000000000000#include #include #include #include #include #include "log.h" #include "swaylock.h" #include "seat.h" static void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size) { struct swaylock_state *state = data; if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { close(fd); swaylock_log(LOG_ERROR, "Unknown keymap format %d, aborting", format); exit(1); } char *map_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); if (map_shm == MAP_FAILED) { close(fd); swaylock_log(LOG_ERROR, "Unable to initialize keymap shm, aborting"); exit(1); } struct xkb_keymap *keymap = xkb_keymap_new_from_string( state->xkb.context, map_shm, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); munmap(map_shm, size); close(fd); assert(keymap); struct xkb_state *xkb_state = xkb_state_new(keymap); assert(xkb_state); xkb_keymap_unref(state->xkb.keymap); xkb_state_unref(state->xkb.state); state->xkb.keymap = keymap; state->xkb.state = xkb_state; } static void keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys) { // Who cares } static void keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface) { // Who cares } static void keyboard_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t _key_state) { struct swaylock_state *state = data; enum wl_keyboard_key_state key_state = _key_state; xkb_keysym_t sym = xkb_state_key_get_one_sym(state->xkb.state, key + 8); uint32_t keycode = key_state == WL_KEYBOARD_KEY_STATE_PRESSED ? key + 8 : 0; uint32_t codepoint = xkb_state_key_get_utf32(state->xkb.state, keycode); if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED) { swaylock_handle_key(state, sym, codepoint); } } static void keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) { struct swaylock_state *state = data; int layout_same = xkb_state_layout_index_is_active(state->xkb.state, group, XKB_STATE_LAYOUT_EFFECTIVE); if (!layout_same) { damage_state(state); } xkb_state_update_mask(state->xkb.state, mods_depressed, mods_latched, mods_locked, 0, 0, group); int caps_lock = xkb_state_mod_name_is_active(state->xkb.state, XKB_MOD_NAME_CAPS, XKB_STATE_MODS_LOCKED); if (caps_lock != state->xkb.caps_lock) { state->xkb.caps_lock = caps_lock; damage_state(state); } state->xkb.control = xkb_state_mod_name_is_active(state->xkb.state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); } static void keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, int32_t rate, int32_t delay) { // TODO } static const struct wl_keyboard_listener keyboard_listener = { .keymap = keyboard_keymap, .enter = keyboard_enter, .leave = keyboard_leave, .key = keyboard_key, .modifiers = keyboard_modifiers, .repeat_info = keyboard_repeat_info, }; static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y) { wl_pointer_set_cursor(wl_pointer, serial, NULL, 0, 0); } static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface) { // Who cares } static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { // Who cares } static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { // Who cares } static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) { // Who cares } static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) { // Who cares } static void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source) { // Who cares } static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) { // Who cares } static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete) { // Who cares } static const struct wl_pointer_listener pointer_listener = { .enter = wl_pointer_enter, .leave = wl_pointer_leave, .motion = wl_pointer_motion, .button = wl_pointer_button, .axis = wl_pointer_axis, .frame = wl_pointer_frame, .axis_source = wl_pointer_axis_source, .axis_stop = wl_pointer_axis_stop, .axis_discrete = wl_pointer_axis_discrete, }; static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, enum wl_seat_capability caps) { struct swaylock_seat *seat = data; if (seat->pointer) { wl_pointer_release(seat->pointer); seat->pointer = NULL; } if (seat->keyboard) { wl_keyboard_release(seat->keyboard); seat->keyboard = NULL; } if ((caps & WL_SEAT_CAPABILITY_POINTER)) { seat->pointer = wl_seat_get_pointer(wl_seat); wl_pointer_add_listener(seat->pointer, &pointer_listener, NULL); } if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) { seat->keyboard = wl_seat_get_keyboard(wl_seat); wl_keyboard_add_listener(seat->keyboard, &keyboard_listener, seat->state); } } static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) { // Who cares } const struct wl_seat_listener seat_listener = { .capabilities = seat_handle_capabilities, .name = seat_handle_name, }; swaylock-1.5/shadow.c000066400000000000000000000045551361207304300146540ustar00rootroot00000000000000#define _XOPEN_SOURCE // for crypt #include #include #include #include #include #include #ifdef __GLIBC__ // GNU, you damn slimy bastard #include #endif #include "comm.h" #include "log.h" #include "swaylock.h" void initialize_pw_backend(int argc, char **argv) { if (geteuid() != 0) { swaylock_log(LOG_ERROR, "swaylock needs to be setuid to read /etc/shadow"); exit(EXIT_FAILURE); } if (!spawn_comm_child()) { exit(EXIT_FAILURE); } if (setgid(getgid()) != 0) { swaylock_log_errno(LOG_ERROR, "Unable to drop root"); exit(EXIT_FAILURE); } if (setuid(getuid()) != 0) { swaylock_log_errno(LOG_ERROR, "Unable to drop root"); exit(EXIT_FAILURE); } if (setuid(0) != -1) { swaylock_log_errno(LOG_ERROR, "Unable to drop root (we shouldn't be " "able to restore it after setuid)"); exit(EXIT_FAILURE); } } void run_pw_backend_child(void) { /* This code runs as root */ struct passwd *pwent = getpwuid(getuid()); if (!pwent) { swaylock_log_errno(LOG_ERROR, "failed to getpwuid"); exit(EXIT_FAILURE); } char *encpw = pwent->pw_passwd; if (strcmp(encpw, "x") == 0) { struct spwd *swent = getspnam(pwent->pw_name); if (!swent) { swaylock_log_errno(LOG_ERROR, "failed to getspnam"); exit(EXIT_FAILURE); } encpw = swent->sp_pwdp; } /* We don't need any additional logging here because the parent process will * also fail here and will handle logging for us. */ if (setgid(getgid()) != 0) { exit(EXIT_FAILURE); } if (setuid(getuid()) != 0) { exit(EXIT_FAILURE); } if (setuid(0) != -1) { exit(EXIT_FAILURE); } /* This code does not run as root */ swaylock_log(LOG_DEBUG, "Prepared to authorize user %s", pwent->pw_name); while (1) { char *buf; ssize_t size = read_comm_request(&buf); if (size < 0) { exit(EXIT_FAILURE); } else if (size == 0) { break; } char *c = crypt(buf, encpw); if (c == NULL) { swaylock_log_errno(LOG_ERROR, "crypt failed"); clear_buffer(buf, size); exit(EXIT_FAILURE); } bool success = strcmp(c, encpw) == 0; if (!write_comm_reply(success)) { clear_buffer(buf, size); exit(EXIT_FAILURE); } // We don't want to keep it in memory longer than necessary, // so clear *before* sleeping. clear_buffer(buf, size); free(buf); sleep(2); } clear_buffer(encpw, strlen(encpw)); exit(EXIT_SUCCESS); }swaylock-1.5/swaylock.1.scd000066400000000000000000000127101361207304300157010ustar00rootroot00000000000000swaylock(1) # NAME swaylock - Screen locker for Wayland # SYNOPSIS _swaylock_ [options...] Locks your Wayland session. # OPTIONS *-C, --config* The config file to use. By default, the following paths are checked: _$HOME/.swaylock/config_, _$XDG\_CONFIG\_HOME/swaylock/config_, and _SYSCONFDIR/swaylock/config_. All flags aside from this one are valid options in the configuration file using the format _long-option=value_. For options such as _ignore-empty-password_, just supply the _long-option_. All leading dashes should be omitted and the equals sign is required for flags that take an argument. *-d, --debug* Enable debugging output. *-e, --ignore-empty-password* When an empty password is provided, do not validate it. *-F, --show-failed-attempts* Show current count of failed authentication attempts. *-f, --daemonize* Detach from the controlling terminal after locking. Note: this is the default behavior of i3lock. *-h, --help* Show help message and quit. *-v, --version* Show the version number and quit. # APPEARANCE *-u, --no-unlock-indicator* Disable the unlock indicator. *-i, --image* [[]:] Display the given image, optionally only on the given output. Use -c to set a background color. If the path potentially contains a ':', prefix it with another ':' to prevent interpreting part of it as . *-k, --show-keyboard-layout* Display the current xkb layout while typing. *-K, --hide-keyboard-layout* Force hiding the current xkb layout while typing, even if more than one layout is configured or the show-keyboard-layout option is set. *-L, --disable-caps-lock-text* Disable the Caps Lock text. *-l, --indicator-caps-lock* Show the current Caps Lock state also on the indicator. *-s, --scaling* Image scaling mode: _stretch_, _fill_, _fit_, _center_, _tile_, _solid\_color_. Use _solid\_color_ to display only the background color, even if a background image is specified. *-t, --tiling* Same as --scaling=tile. *-c, --color* Turn the screen into the given color instead of white. If -i is used, this sets the background of the image to the given color. Defaults to white (FFFFFF). *--bs-hl-color* Sets the color of backspace highlight segments. *--caps-lock-bs-hl-color* Sets the color of backspace highlight segments when Caps Lock is active. *--caps-lock-key-hl-color* Sets the color of the key press highlight segments when Caps Lock is active. *--font* Sets the font of the text. *--font-size* Sets a fixed font size for the indicator text. *--indicator-idle-visible* Sets the indicator to show even if idle. *--indicator-radius* Sets the indicator radius. The default value is 50. *--indicator-thickness* Sets the indicator thickness. The default value is 10. *--inside-color* Sets the color of the inside of the indicator. *--inside-clear-color* Sets the color of the inside of the indicator when cleared. *--inside-caps-lock-color* Sets the color of the inside of the indicator when Caps Lock is active. *--inside-ver-color* Sets the color of the inside of the indicator when verifying. *--inside-wrong-color* Sets the color of the inside of the indicator when invalid. *--key-hl-color* Sets the color of the key press highlight segments. *--layout-bg-color* Sets the background color of the box containing the layout text. *--layout-border-color* Sets the color of the border of the box containing the layout text. *--layout-text-color* Sets the color of the layout text. *--line-color* Sets the color of the line between the inside and ring. *--line-clear-color* Sets the color of the line between the inside and ring when cleared. *--line-caps-lock-color* Sets the color of the line between the inside and ring when Caps Lock is active. *--line-ver-color* Sets the color of the line between the inside and ring when verifying. *--line-wrong-color* Sets the color of the line between the inside and ring when invalid. *-n, --line-uses-inside* Use the inside color for the line between the inside and ring. *-r, --line-uses-ring* Use the ring color for the line between the inside and ring. *--ring-color* Sets the color of the ring of the indicator when typing or idle. *--ring-clear-color* Sets the color of the ring of the indicator when cleared. *--ring-caps-lock-color* Sets the color of the ring of the indicator when Caps Lock is active. *--ring-ver-color* Sets the color of the ring of the indicator when verifying. *--ring-wrong-color* Sets the color of the ring of the indicator when invalid. *--separator-color* Sets the color of the lines that separate highlight segments. *--text-color* Sets the color of the text. *--text-clear-color* Sets the color of the text when cleared. *--text-caps-lock-color* Sets the color of the text when Caps Lock is active. *--text-ver-color* Sets the color of the text when verifying. *--text-wrong-color* Sets the color of the text when invalid. # AUTHORS Maintained by Drew DeVault , who is assisted by other open source contributors. For more information about swaylock development, see https://github.com/swaywm/swaylock. swaylock-1.5/unicode.c000066400000000000000000000020431361207304300150030ustar00rootroot00000000000000#include #include #include "unicode.h" size_t utf8_chsize(uint32_t ch) { if (ch < 0x80) { return 1; } else if (ch < 0x800) { return 2; } else if (ch < 0x10000) { return 3; } return 4; } size_t utf8_encode(char *str, uint32_t ch) { size_t len = 0; uint8_t first; if (ch < 0x80) { first = 0; len = 1; } else if (ch < 0x800) { first = 0xc0; len = 2; } else if (ch < 0x10000) { first = 0xe0; len = 3; } else { first = 0xf0; len = 4; } for (size_t i = len - 1; i > 0; --i) { str[i] = (ch & 0x3f) | 0x80; ch >>= 6; } str[0] = ch | first; return len; } static const struct { uint8_t mask; uint8_t result; int octets; } sizes[] = { { 0x80, 0x00, 1 }, { 0xE0, 0xC0, 2 }, { 0xF0, 0xE0, 3 }, { 0xF8, 0xF0, 4 }, { 0xFC, 0xF8, 5 }, { 0xFE, 0xF8, 6 }, { 0x80, 0x80, -1 }, }; int utf8_size(const char *s) { uint8_t c = (uint8_t)*s; for (size_t i = 0; i < sizeof(sizes) / sizeof(*sizes); ++i) { if ((c & sizes[i].mask) == sizes[i].result) { return sizes[i].octets; } } return -1; } swaylock-1.5/wlr-input-inhibitor-unstable-v1.xml000066400000000000000000000061241361207304300220240ustar00rootroot00000000000000 Copyright © 2018 Drew DeVault Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of the copyright holders not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. The copyright holders make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. Clients can use this interface to prevent input events from being sent to any surfaces but its own, which is useful for example in lock screen software. It is assumed that access to this interface will be locked down to whitelisted clients by the compositor. Activates the input inhibitor. As long as the inhibitor is active, the compositor will not send input events to other clients. While this resource exists, input to clients other than the owner of the inhibitor resource will not receive input events. The client that owns this resource will receive all input events normally. The compositor will also disable all of its own input processing (such as keyboard shortcuts) while the inhibitor is active. The compositor may continue to send input events to selected clients, such as an on-screen keyboard (via the input-method protocol). Destroy the inhibitor and allow other clients to receive input. swaylock-1.5/wlr-layer-shell-unstable-v1.xml000066400000000000000000000320731361207304300211230ustar00rootroot00000000000000 Copyright © 2017 Drew DeVault Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of the copyright holders not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. The copyright holders make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. Clients can use this interface to assign the surface_layer role to wl_surfaces. Such surfaces are assigned to a "layer" of the output and rendered with a defined z-depth respective to each other. They may also be anchored to the edges and corners of a screen and specify input handling semantics. This interface should be suitable for the implementation of many desktop shell components, and a broad number of other applications that interact with the desktop. Create a layer surface for an existing surface. This assigns the role of layer_surface, or raises a protocol error if another role is already assigned. Creating a layer surface from a wl_surface which has a buffer attached or committed is a client error, and any attempts by a client to attach or manipulate a buffer prior to the first layer_surface.configure call must also be treated as errors. You may pass NULL for output to allow the compositor to decide which output to use. Generally this will be the one that the user most recently interacted with. Clients can specify a namespace that defines the purpose of the layer surface. These values indicate which layers a surface can be rendered in. They are ordered by z depth, bottom-most first. Traditional shell surfaces will typically be rendered between the bottom and top layers. Fullscreen shell surfaces are typically rendered at the top layer. Multiple surfaces can share a single layer, and ordering within a single layer is undefined. An interface that may be implemented by a wl_surface, for surfaces that are designed to be rendered as a layer of a stacked desktop-like environment. Layer surface state (size, anchor, exclusive zone, margin, interactivity) is double-buffered, and will be applied at the time wl_surface.commit of the corresponding wl_surface is called. Sets the size of the surface in surface-local coordinates. The compositor will display the surface centered with respect to its anchors. If you pass 0 for either value, the compositor will assign it and inform you of the assignment in the configure event. You must set your anchor to opposite edges in the dimensions you omit; not doing so is a protocol error. Both values are 0 by default. Size is double-buffered, see wl_surface.commit. Requests that the compositor anchor the surface to the specified edges and corners. If two orthoginal edges are specified (e.g. 'top' and 'left'), then the anchor point will be the intersection of the edges (e.g. the top left corner of the output); otherwise the anchor point will be centered on that edge, or in the center if none is specified. Anchor is double-buffered, see wl_surface.commit. Requests that the compositor avoids occluding an area of the surface with other surfaces. The compositor's use of this information is implementation-dependent - do not assume that this region will not actually be occluded. A positive value is only meaningful if the surface is anchored to an edge, rather than a corner. The zone is the number of surface-local coordinates from the edge that are considered exclusive. Surfaces that do not wish to have an exclusive zone may instead specify how they should interact with surfaces that do. If set to zero, the surface indicates that it would like to be moved to avoid occluding surfaces with a positive excluzive zone. If set to -1, the surface indicates that it would not like to be moved to accomodate for other surfaces, and the compositor should extend it all the way to the edges it is anchored to. For example, a panel might set its exclusive zone to 10, so that maximized shell surfaces are not shown on top of it. A notification might set its exclusive zone to 0, so that it is moved to avoid occluding the panel, but shell surfaces are shown underneath it. A wallpaper or lock screen might set their exclusive zone to -1, so that they stretch below or over the panel. The default value is 0. Exclusive zone is double-buffered, see wl_surface.commit. Requests that the surface be placed some distance away from the anchor point on the output, in surface-local coordinates. Setting this value for edges you are not anchored to has no effect. The exclusive zone includes the margin. Margin is double-buffered, see wl_surface.commit. Set to 1 to request that the seat send keyboard events to this layer surface. For layers below the shell surface layer, the seat will use normal focus semantics. For layers above the shell surface layers, the seat will always give exclusive keyboard focus to the top-most layer which has keyboard interactivity set to true. Layer surfaces receive pointer, touch, and tablet events normally. If you do not want to receive them, set the input region on your surface to an empty region. Events is double-buffered, see wl_surface.commit. This assigns an xdg_popup's parent to this layer_surface. This popup should have been created via xdg_surface::get_popup with the parent set to NULL, and this request must be invoked before committing the popup's initial state. See the documentation of xdg_popup for more details about what an xdg_popup is and how it is used. When a configure event is received, if a client commits the surface in response to the configure event, then the client must make an ack_configure request sometime before the commit request, passing along the serial of the configure event. If the client receives multiple configure events before it can respond to one, it only has to ack the last configure event. A client is not required to commit immediately after sending an ack_configure request - it may even ack_configure several times before its next surface commit. A client may send multiple ack_configure requests before committing, but only the last request sent before a commit indicates which configure event the client really is responding to. This request destroys the layer surface. The configure event asks the client to resize its surface. Clients should arrange their surface for the new states, and then send an ack_configure request with the serial sent in this configure event at some point before committing the new surface. The client is free to dismiss all but the last configure event it received. The width and height arguments specify the size of the window in surface-local coordinates. The size is a hint, in the sense that the client is free to ignore it if it doesn't resize, pick a smaller size (to satisfy aspect ratio or resize in steps of NxM pixels). If the client picks a smaller size and is anchored to two opposite anchors (e.g. 'top' and 'bottom'), the surface will be centered on this axis. If the width or height arguments are zero, it means the client should decide its own window dimension. The closed event is sent by the compositor when the surface will no longer be shown. The output may have been destroyed or the user may have asked for it to be removed. Further changes to the surface will be ignored. The client should destroy the resource after receiving this event, and create a new surface if they so choose.