aboutsummaryrefslogtreecommitdiffstats
path: root/swaylock/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaylock/main.c')
-rw-r--r--swaylock/main.c918
1 files changed, 203 insertions, 715 deletions
diff --git a/swaylock/main.c b/swaylock/main.c
index c2615951..4c6b44c6 100644
--- a/swaylock/main.c
+++ b/swaylock/main.c
@@ -1,387 +1,166 @@
1#define _XOPEN_SOURCE 500 1#define _XOPEN_SOURCE 700
2#include "wayland-swaylock-client-protocol.h" 2#define _POSIX_C_SOURCE 200112L
3#include <xkbcommon/xkbcommon.h> 3#include <assert.h>
4#include <xkbcommon/xkbcommon-names.h> 4#include <ctype.h>
5#include <security/pam_appl.h> 5#include <fcntl.h>
6#include <json-c/json.h> 6#include <getopt.h>
7#include <stdbool.h>
7#include <stdio.h> 8#include <stdio.h>
8#include <stdlib.h> 9#include <stdlib.h>
9#include <string.h> 10#include <string.h>
10#include <sys/types.h> 11#include <sys/stat.h>
11#include <pwd.h> 12#include <time.h>
12#include <getopt.h>
13#include <signal.h>
14#include <stdbool.h>
15#include <unistd.h> 13#include <unistd.h>
16#include "client/window.h" 14#include <wayland-client.h>
17#include "client/registry.h" 15#include <wlr/util/log.h>
18#include "client/cairo.h" 16#include "swaylock/seat.h"
19#include "swaylock/swaylock.h" 17#include "swaylock/swaylock.h"
20#include "ipc-client.h" 18#include "background-image.h"
21#include "log.h" 19#include "pool-buffer.h"
20#include "cairo.h"
22#include "util.h" 21#include "util.h"
22#include "wlr-input-inhibitor-unstable-v1-client-protocol.h"
23#include "wlr-layer-shell-unstable-v1-client-protocol.h"
23 24
24struct registry *registry; 25static void daemonize() {
25struct render_data render_data; 26 int fds[2];
26struct lock_config *config; 27 if (pipe(fds) != 0) {
27bool show_indicator = true; 28 wlr_log(L_ERROR, "Failed to pipe");
28
29void wl_dispatch_events() {
30 wl_display_flush(registry->display);
31 if (wl_display_dispatch(registry->display) == -1) {
32 sway_log(L_ERROR, "failed to run wl_display_dispatch");
33 exit(1); 29 exit(1);
34 } 30 }
31 if (fork() == 0) {
32 close(fds[0]);
33 int devnull = open("/dev/null", O_RDWR);
34 dup2(STDOUT_FILENO, devnull);
35 dup2(STDERR_FILENO, devnull);
36 uint8_t success = 0;
37 if (chdir("/") != 0) {
38 write(fds[1], &success, 1);
39 exit(1);
40 }
41 success = 1;
42 if (write(fds[1], &success, 1) != 1) {
43 exit(1);
44 }
45 close(fds[1]);
46 } else {
47 close(fds[1]);
48 uint8_t success;
49 if (read(fds[0], &success, 1) != 1 || !success) {
50 wlr_log(L_ERROR, "Failed to daemonize");
51 exit(1);
52 }
53 close(fds[0]);
54 exit(0);
55 }
35} 56}
36 57
37void sigalarm_handler(int sig) { 58static void layer_surface_configure(void *data,
38 signal(SIGALRM, SIG_IGN); 59 struct zwlr_layer_surface_v1 *layer_surface,
39 // Hide typing indicator 60 uint32_t serial, uint32_t width, uint32_t height) {
40 render_data.auth_state = AUTH_STATE_IDLE; 61 struct swaylock_surface *surface = data;
41 render(&render_data, config); 62 surface->width = width;
42 wl_display_flush(registry->display); 63 surface->height = height;
43 signal(SIGALRM, sigalarm_handler); 64 zwlr_layer_surface_v1_ack_configure(layer_surface, serial);
65 render_frame(surface);
44} 66}
45 67
46void sway_terminate(int exit_code) { 68static void layer_surface_closed(void *data,
47 int i; 69 struct zwlr_layer_surface_v1 *layer_surface) {
48 for (i = 0; i < render_data.surfaces->length; ++i) { 70 struct swaylock_surface *surface = data;
49 struct window *window = render_data.surfaces->items[i]; 71 zwlr_layer_surface_v1_destroy(surface->layer_surface);
50 window_teardown(window); 72 wl_surface_destroy(surface->surface);
51 } 73 surface->state->run_display = false;
52 list_free(render_data.surfaces);
53 if (registry) {
54 registry_teardown(registry);
55 }
56 exit(exit_code);
57} 74}
58 75
59char *password; 76static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
60int password_size; 77 .configure = layer_surface_configure,
61enum line_source line_source = LINE_SOURCE_DEFAULT; 78 .closed = layer_surface_closed,
62 79};
63struct lock_config *init_config() {
64 struct lock_config *config = calloc(1, sizeof(struct lock_config));
65
66 config->font = strdup("sans-serif");
67 config->colors.text = 0x000000FF;
68
69 config->colors.line = 0x000000FF;
70 config->colors.separator = 0x000000FF;
71
72 config->colors.input_cursor = 0x33DB00FF;
73 config->colors.backspace_cursor = 0xDB3300FF;
74
75 config->colors.normal.inner_ring = 0x000000BF;
76 config->colors.normal.outer_ring = 0x337D00FF;
77
78 config->colors.validating.inner_ring = 0x0072FFBF;
79 config->colors.validating.outer_ring = 0x3300FAFF;
80 80
81 config->colors.invalid.inner_ring = 0xFA0000BF; 81static void output_geometry(void *data, struct wl_output *output, int32_t x,
82 config->colors.invalid.outer_ring = 0x7D3300FF; 82 int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel,
83 83 const char *make, const char *model, int32_t transform) {
84 config->radius = 50; 84 // Who cares
85 config->thickness = 10;
86
87 return config;
88} 85}
89 86
90void free_config(struct lock_config *config) { 87static void output_mode(void *data, struct wl_output *output, uint32_t flags,
91 free(config->font); 88 int32_t width, int32_t height, int32_t refresh) {
92 free(config); 89 // Who cares
93} 90}
94 91
95int function_conversation(int num_msg, const struct pam_message **msg, 92static void output_done(void *data, struct wl_output *output) {
96 struct pam_response **resp, void *appdata_ptr) { 93 // Who cares
97
98 const char* msg_style_names[] = {
99 NULL,
100 "PAM_PROMPT_ECHO_OFF",
101 "PAM_PROMPT_ECHO_ON",
102 "PAM_ERROR_MSG",
103 "PAM_TEXT_INFO",
104 };
105
106 /* PAM expects an array of responses, one for each message */
107 struct pam_response *pam_reply = calloc(num_msg, sizeof(struct pam_response));
108 *resp = pam_reply;
109
110 for(int i=0; i<num_msg; ++i) {
111 sway_log(L_DEBUG, "msg[%d]: (%s) %s", i,
112 msg_style_names[msg[i]->msg_style],
113 msg[i]->msg);
114
115 switch (msg[i]->msg_style) {
116 case PAM_PROMPT_ECHO_OFF:
117 case PAM_PROMPT_ECHO_ON:
118 pam_reply[i].resp = password;
119 break;
120
121 case PAM_ERROR_MSG:
122 case PAM_TEXT_INFO:
123 break;
124 }
125 }
126
127 return PAM_SUCCESS;
128} 94}
129 95
130/** 96static void output_scale(void *data, struct wl_output *output, int32_t factor) {
131 * Note: PAM will free() 'password' during the process 97 struct swaylock_surface *surface = data;
132 */ 98 surface->scale = factor;
133bool verify_password() { 99 if (surface->state->run_display) {
134 struct passwd *passwd = getpwuid(getuid()); 100 render_frames(surface->state);
135 char *username = passwd->pw_name;
136
137 const struct pam_conv local_conversation = { function_conversation, NULL };
138 pam_handle_t *local_auth_handle = NULL;
139 int pam_err;
140 if ((pam_err = pam_start("swaylock", username, &local_conversation, &local_auth_handle)) != PAM_SUCCESS) {
141 sway_abort("PAM returned %d\n", pam_err);
142 } 101 }
143 if ((pam_err = pam_authenticate(local_auth_handle, 0)) != PAM_SUCCESS) {
144 return false;
145 }
146 if ((pam_err = pam_end(local_auth_handle, pam_err)) != PAM_SUCCESS) {
147 return false;
148 }
149 return true;
150} 102}
151 103
152void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t code, uint32_t codepoint) { 104struct wl_output_listener output_listener = {
153 int redraw_screen = 0; 105 .geometry = output_geometry,
154 char *password_realloc; 106 .mode = output_mode,
155 int i; 107 .done = output_done,
156 108 .scale = output_scale,
157 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { 109};
158 switch (sym) { 110
159 case XKB_KEY_KP_Enter: 111static void handle_global(void *data, struct wl_registry *registry,
160 case XKB_KEY_Return: 112 uint32_t name, const char *interface, uint32_t version) {
161 render_data.auth_state = AUTH_STATE_VALIDATING; 113 struct swaylock_state *state = data;
162 114 if (strcmp(interface, wl_compositor_interface.name) == 0) {
163 render(&render_data, config); 115 state->compositor = wl_registry_bind(registry, name,
164 // Make sure our render call will actually be displayed on the screen 116 &wl_compositor_interface, 3);
165 wl_dispatch_events(); 117 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
166 118 state->shm = wl_registry_bind(registry, name,
167 if (verify_password()) { 119 &wl_shm_interface, 1);
168 exit(0); 120 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
169 } 121 struct wl_seat *seat = wl_registry_bind(
170 122 registry, name, &wl_seat_interface, 1);
171 render_data.auth_state = AUTH_STATE_INVALID; 123 wl_seat_add_listener(seat, &seat_listener, state);
172 redraw_screen = 1; 124 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
173 125 state->layer_shell = wl_registry_bind(
174 password_size = 1024; 126 registry, name, &zwlr_layer_shell_v1_interface, 1);
175 password = malloc(password_size); 127 } else if (strcmp(interface, zwlr_input_inhibit_manager_v1_interface.name) == 0) {
176 password[0] = '\0'; 128 state->input_inhibit_manager = wl_registry_bind(
177 break; 129 registry, name, &zwlr_input_inhibit_manager_v1_interface, 1);
178 case XKB_KEY_BackSpace: 130 } else if (strcmp(interface, wl_output_interface.name) == 0) {
179 i = strlen(password); 131 struct swaylock_surface *surface =
180 if (i > 0) { 132 calloc(1, sizeof(struct swaylock_surface));
181 password[i - 1] = '\0'; 133 surface->state = state;
182 render_data.auth_state = AUTH_STATE_BACKSPACE; 134 surface->output = wl_registry_bind(registry, name,
183 redraw_screen = 1; 135 &wl_output_interface, 3);
184 } 136 wl_output_add_listener(surface->output, &output_listener, surface);
185 break; 137 wl_list_insert(&state->surfaces, &surface->link);
186 case XKB_KEY_Control_L:
187 case XKB_KEY_Control_R:
188 case XKB_KEY_Shift_L:
189 case XKB_KEY_Shift_R:
190 case XKB_KEY_Caps_Lock:
191 case XKB_KEY_Shift_Lock:
192 case XKB_KEY_Meta_L:
193 case XKB_KEY_Meta_R:
194 case XKB_KEY_Alt_L:
195 case XKB_KEY_Alt_R:
196 case XKB_KEY_Super_L:
197 case XKB_KEY_Super_R:
198 case XKB_KEY_Hyper_L:
199 case XKB_KEY_Hyper_R:
200 break; // don't draw screen on modifier keys
201 case XKB_KEY_Escape:
202 case XKB_KEY_u:
203 case XKB_KEY_U:
204 // clear password buffer on ctrl-u (or escape for i3lock compatibility)
205 if (sym == XKB_KEY_Escape || xkb_state_mod_name_is_active(registry->input->xkb.state,
206 XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE) > 0) {
207 render_data.auth_state = AUTH_STATE_BACKSPACE;
208 redraw_screen = 1;
209
210 password_size = 1024;
211 free(password);
212 password = malloc(password_size);
213 password[0] = '\0';
214 break;
215 }
216 /* fallthrough */
217 default:
218 render_data.auth_state = AUTH_STATE_INPUT;
219 redraw_screen = 1;
220 i = strlen(password);
221 if (i + 1 == password_size) {
222 password_size += 1024;
223 password_realloc = realloc(password, password_size);
224 // reset password if realloc fails.
225 if (password_realloc == NULL) {
226 password_size = 1024;
227 free(password);
228 password = malloc(password_size);
229 password[0] = '\0';
230 break;
231 } else {
232 password = password_realloc;
233 }
234 }
235 password[i] = (char)codepoint;
236 password[i + 1] = '\0';
237 break;
238 }
239 if (redraw_screen) {
240 render(&render_data, config);
241 wl_dispatch_events();
242 // Hide the indicator after a couple of seconds
243 alarm(5);
244 }
245 } 138 }
246} 139}
247 140
248void render_color(struct window *window, uint32_t color) { 141static void handle_global_remove(void *data, struct wl_registry *registry,
249 cairo_set_source_u32(window->cairo, color); 142 uint32_t name) {
250 cairo_paint(window->cairo); 143 // who cares
251}
252
253void render_image(struct window *window, cairo_surface_t *image, enum scaling_mode scaling_mode) {
254 double width = cairo_image_surface_get_width(image);
255 double height = cairo_image_surface_get_height(image);
256 int wwidth = window->width * window->scale;
257 int wheight = window->height * window->scale;
258
259 switch (scaling_mode) {
260 case SCALING_MODE_STRETCH:
261 cairo_scale(window->cairo,
262 (double) wwidth / width,
263 (double) wheight / height);
264 cairo_set_source_surface(window->cairo, image, 0, 0);
265 break;
266 case SCALING_MODE_FILL:
267 {
268 double window_ratio = (double) wwidth / wheight;
269 double bg_ratio = width / height;
270
271 if (window_ratio > bg_ratio) {
272 double scale = (double) wwidth / width;
273 cairo_scale(window->cairo, scale, scale);
274 cairo_set_source_surface(window->cairo, image,
275 0,
276 (double) wheight/2 / scale - height/2);
277 } else {
278 double scale = (double) wheight / height;
279 cairo_scale(window->cairo, scale, scale);
280 cairo_set_source_surface(window->cairo, image,
281 (double) wwidth/2 / scale - width/2,
282 0);
283 }
284 break;
285 }
286 case SCALING_MODE_FIT:
287 {
288 double window_ratio = (double) wwidth / wheight;
289 double bg_ratio = width / height;
290
291 if (window_ratio > bg_ratio) {
292 double scale = (double) wheight / height;
293 cairo_scale(window->cairo, scale, scale);
294 cairo_set_source_surface(window->cairo, image,
295 (double) wwidth/2 / scale - width/2,
296 0);
297 } else {
298 double scale = (double) wwidth / width;
299 cairo_scale(window->cairo, scale, scale);
300 cairo_set_source_surface(window->cairo, image,
301 0,
302 (double) wheight/2 / scale - height/2);
303 }
304 break;
305 }
306 case SCALING_MODE_CENTER:
307 cairo_set_source_surface(window->cairo, image,
308 (double) wwidth/2 - width/2,
309 (double) wheight/2 - height/2);
310 break;
311 case SCALING_MODE_TILE:
312 {
313 cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image);
314 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
315 cairo_set_source(window->cairo, pattern);
316 break;
317 }
318 }
319
320 cairo_paint(window->cairo);
321} 144}
322 145
323cairo_surface_t *load_image(char *image_path) { 146static const struct wl_registry_listener registry_listener = {
324 cairo_surface_t *image = NULL; 147 .global = handle_global,
325 148 .global_remove = handle_global_remove,
326#ifdef WITH_GDK_PIXBUF 149};
327 GError *err = NULL;
328 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err);
329 if (!pixbuf) {
330 sway_abort("Failed to load background image: %s", err->message);
331 }
332 image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf);
333 g_object_unref(pixbuf);
334#else
335 image = cairo_image_surface_create_from_png(image_path);
336#endif //WITH_GDK_PIXBUF
337 if (!image) {
338 sway_abort("Failed to read background image.");
339 }
340 150
341 return image; 151static struct swaylock_state state;
342}
343 152
344int main(int argc, char **argv) { 153int main(int argc, char **argv) {
345 const char *scaling_mode_str = "fit", *socket_path = NULL;
346 int i;
347 void *images = NULL;
348 config = init_config();
349
350 render_data.num_images = 0;
351 render_data.color_set = 0;
352 render_data.color = 0xFFFFFFFF;
353 render_data.auth_state = AUTH_STATE_IDLE;
354
355 init_log(L_INFO);
356 // Install SIGALARM handler (for hiding the typing indicator)
357 signal(SIGALRM, sigalarm_handler);
358
359 static struct option long_options[] = { 154 static struct option long_options[] = {
360 {"help", no_argument, NULL, 'h'}, 155 {"help", no_argument, NULL, 'h'},
361 {"color", required_argument, NULL, 'c'}, 156 {"color", required_argument, NULL, 'c'},
362 {"image", required_argument, NULL, 'i'}, 157 {"image", required_argument, NULL, 'i'},
363 {"scaling", required_argument, NULL, 0}, 158 {"scaling", required_argument, NULL, 's'},
364 {"tiling", no_argument, NULL, 't'}, 159 {"tiling", no_argument, NULL, 't'},
365 {"version", no_argument, NULL, 'v'}, 160 {"version", no_argument, NULL, 'v'},
366 {"socket", required_argument, NULL, 'p'}, 161 {"socket", required_argument, NULL, 'p'},
367 {"no-unlock-indicator", no_argument, NULL, 'u'}, 162 {"no-unlock-indicator", no_argument, NULL, 'u'},
368 {"daemonize", no_argument, NULL, 'f'}, 163 {"daemonize", no_argument, NULL, 'f'},
369 {"font", required_argument, NULL, 0},
370 {"line-uses-ring", no_argument, NULL, 'r'},
371 {"line-uses-inside", no_argument, NULL, 's'},
372 {"textcolor", required_argument, NULL, 0},
373 {"insidevercolor", required_argument, NULL, 0},
374 {"insidewrongcolor", required_argument, NULL, 0},
375 {"insidecolor", required_argument, NULL, 0},
376 {"ringvercolor", required_argument, NULL, 0},
377 {"ringwrongcolor", required_argument, NULL, 0},
378 {"ringcolor", required_argument, NULL, 0},
379 {"linecolor", required_argument, NULL, 0},
380 {"separatorcolor", required_argument, NULL, 0},
381 {"keyhlcolor", required_argument, NULL, 0},
382 {"bshlcolor", required_argument, NULL, 0},
383 {"indicator-radius", required_argument, NULL, 0},
384 {"indicator-thickness", required_argument, NULL, 0},
385 {0, 0, 0, 0} 164 {0, 0, 0, 0}
386 }; 165 };
387 166
@@ -390,415 +169,124 @@ int main(int argc, char **argv) {
390 "\n" 169 "\n"
391 " -h, --help Show help message and quit.\n" 170 " -h, --help Show help message and quit.\n"
392 " -c, --color <rrggbb[aa]> Turn the screen into the given color instead of white.\n" 171 " -c, --color <rrggbb[aa]> Turn the screen into the given color instead of white.\n"
393 " --scaling Scaling mode: stretch, fill, fit, center, tile.\n" 172 " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n"
394 " -t, --tiling Same as --scaling=tile.\n" 173 " -t, --tiling Same as --scaling=tile.\n"
395 " -v, --version Show the version number and quit.\n" 174 " -v, --version Show the version number and quit.\n"
396 " -i, --image [<output>:]<path> Display the given image.\n" 175 " -i, --image [<output>:]<path> Display the given image.\n"
397 " -u, --no-unlock-indicator Disable the unlock indicator.\n" 176 " -u, --no-unlock-indicator Disable the unlock indicator.\n"
398 " -f, --daemonize Detach from the controlling terminal.\n" 177 " -f, --daemonize Detach from the controlling terminal.\n"
399 " --socket <socket> Use the specified socket.\n" 178 " --socket <socket> Use the specified socket.\n";
400 " For more information see `man swaylock`\n";
401 179
402 180 struct swaylock_args args = {
403 registry = registry_poll(); 181 .mode = BACKGROUND_MODE_SOLID_COLOR,
182 .color = 0xFFFFFFFF,
183 .show_indicator = true,
184 };
185 cairo_surface_t *background_image = NULL;
186 state.args = args;
187 wlr_log_init(L_DEBUG, NULL);
404 188
405 int c; 189 int c;
406 while (1) { 190 while (1) {
407 int option_index = 0; 191 int option_index = 0;
408 c = getopt_long(argc, argv, "hc:i:srtvuf", long_options, &option_index); 192 c = getopt_long(argc, argv, "hc:i:s:tvuf", long_options, &option_index);
409 if (c == -1) { 193 if (c == -1) {
410 break; 194 break;
411 } 195 }
412 switch (c) { 196 switch (c) {
413 case 'c': 197 case 'c': {
414 { 198 state.args.color = parse_color(optarg);
415 render_data.color = parse_color(optarg); 199 state.args.mode = BACKGROUND_MODE_SOLID_COLOR;
416 render_data.color_set = 1;
417 break; 200 break;
418 } 201 }
419 case 'i': 202 case 'i':
420 { 203 // TODO: Multiple background images (bleh)
421 char *image_path = strchr(optarg, ':'); 204 background_image = load_background_image(optarg);
422 if (image_path == NULL) { 205 if (!background_image) {
423 if (render_data.num_images == 0) { 206 return 1;
424 // Provided image without output
425 render_data.image = load_image(optarg);
426 render_data.num_images = -1;
427 } else {
428 sway_log(L_ERROR, "output must be defined for all --images or no --images");
429 exit(EXIT_FAILURE);
430 }
431 } else {
432 // Provided image for all outputs
433 if (render_data.num_images == 0) {
434 images = calloc(registry->outputs->length, sizeof(char*) * 2);
435 } else if (render_data.num_images == -1) {
436 sway_log(L_ERROR, "output must be defined for all --images or no --images");
437 exit(EXIT_FAILURE);
438 }
439
440 image_path[0] = '\0';
441 ((char**) images)[render_data.num_images * 2] = optarg;
442 ((char**) images)[render_data.num_images++ * 2 + 1] = ++image_path;
443 } 207 }
208 state.args.mode = BACKGROUND_MODE_FILL;
444 break; 209 break;
445 } 210 case 's':
446 case 't': 211 state.args.mode = parse_background_mode(optarg);
447 scaling_mode_str = "tile"; 212 if (state.args.mode == BACKGROUND_MODE_INVALID) {
213 return 1;
214 }
448 break; 215 break;
449 case 'p': 216 case 't':
450 socket_path = optarg; 217 state.args.mode = BACKGROUND_MODE_TILE;
451 break; 218 break;
452 case 'v': 219 case 'v':
453 fprintf(stdout, "swaylock version " SWAY_VERSION "\n"); 220#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
454 exit(EXIT_SUCCESS); 221 fprintf(stdout, "swaylock version %s (%s, branch \"%s\")\n",
455 break; 222 SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
223#else
224 fprintf(stdout, "version unknown\n");
225#endif
226 return 0;
456 case 'u': 227 case 'u':
457 show_indicator = false; 228 state.args.show_indicator = false;
458 break;
459 case 'f': {
460 pid_t t = fork();
461 if (t == -1) {
462 sway_log(L_ERROR, "daemon call failed");
463 exit(EXIT_FAILURE);
464 } else if (t > 0) {
465 exit(0);
466 }
467 break; 229 break;
468 } 230 case 'f':
469 case 'r': 231 daemonize();
470 if (line_source != LINE_SOURCE_DEFAULT) {
471 sway_log(L_ERROR, "line source options conflict");
472 exit(EXIT_FAILURE);
473 }
474 line_source = LINE_SOURCE_RING;
475 break;
476 case 's':
477 if (line_source != LINE_SOURCE_DEFAULT) {
478 sway_log(L_ERROR, "line source options conflict");
479 exit(EXIT_FAILURE);
480 }
481 line_source = LINE_SOURCE_INSIDE;
482 break;
483 case 0:
484 if (strcmp(long_options[option_index].name, "font") == 0) {
485 free(config->font);
486 config->font = strdup(optarg);
487 } else if (strcmp(long_options[option_index].name, "scaling") == 0) {
488 scaling_mode_str = optarg;
489 } else if (strcmp(long_options[option_index].name, "textcolor") == 0) {
490 config->colors.text = parse_color(optarg);
491 } else if (strcmp(long_options[option_index].name, "insidevercolor") == 0) {
492 config->colors.validating.inner_ring = parse_color(optarg);
493 } else if (strcmp(long_options[option_index].name, "insidewrongcolor") == 0) {
494 config->colors.invalid.inner_ring = parse_color(optarg);
495 } else if (strcmp(long_options[option_index].name, "insidecolor") == 0) {
496 config->colors.normal.inner_ring = parse_color(optarg);
497 } else if (strcmp(long_options[option_index].name, "ringvercolor") == 0) {
498 config->colors.validating.outer_ring = parse_color(optarg);
499 } else if (strcmp(long_options[option_index].name, "ringwrongcolor") == 0) {
500 config->colors.invalid.outer_ring = parse_color(optarg);
501 } else if (strcmp(long_options[option_index].name, "ringcolor") == 0) {
502 config->colors.normal.outer_ring = parse_color(optarg);
503 } else if (strcmp(long_options[option_index].name, "linecolor") == 0) {
504 config->colors.line = parse_color(optarg);
505 } else if (strcmp(long_options[option_index].name, "separatorcolor") == 0) {
506 config->colors.separator = parse_color(optarg);
507 } else if (strcmp(long_options[option_index].name, "keyhlcolor") == 0) {
508 config->colors.input_cursor = parse_color(optarg);
509 } else if (strcmp(long_options[option_index].name, "bshlcolor") == 0) {
510 config->colors.backspace_cursor = parse_color(optarg);
511 } else if (strcmp(long_options[option_index].name, "indicator-radius") == 0) {
512 config->radius = atoi(optarg);
513 } else if (strcmp(long_options[option_index].name, "indicator-thickness") == 0) {
514 config->thickness = atoi(optarg);
515 }
516 break; 232 break;
517 default: 233 default:
518 fprintf(stderr, "%s", usage); 234 fprintf(stderr, "%s", usage);
519 exit(EXIT_FAILURE); 235 return 1;
520 }
521 }
522
523 render_data.scaling_mode = SCALING_MODE_STRETCH;
524 if (strcmp(scaling_mode_str, "stretch") == 0) {
525 render_data.scaling_mode = SCALING_MODE_STRETCH;
526 } else if (strcmp(scaling_mode_str, "fill") == 0) {
527 render_data.scaling_mode = SCALING_MODE_FILL;
528 } else if (strcmp(scaling_mode_str, "fit") == 0) {
529 render_data.scaling_mode = SCALING_MODE_FIT;
530 } else if (strcmp(scaling_mode_str, "center") == 0) {
531 render_data.scaling_mode = SCALING_MODE_CENTER;
532 } else if (strcmp(scaling_mode_str, "tile") == 0) {
533 render_data.scaling_mode = SCALING_MODE_TILE;
534 } else {
535 sway_abort("Unsupported scaling mode: %s", scaling_mode_str);
536 }
537
538 password_size = 1024;
539 password = malloc(password_size);
540 password[0] = '\0';
541 render_data.surfaces = create_list();
542 if (!socket_path) {
543 socket_path = get_socketpath();
544 if (!socket_path) {
545 sway_abort("Unable to retrieve socket path");
546 } 236 }
547 } 237 }
548 238
549 if (!registry) { 239 wl_list_init(&state.surfaces);
550 sway_abort("Unable to connect to wayland compositor"); 240 state.xkb.context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
551 } 241 state.display = wl_display_connect(NULL);
552 242 assert(state.display);
553 if (!registry->swaylock) { 243
554 sway_abort("swaylock requires the compositor to support the swaylock extension."); 244 struct wl_registry *registry = wl_display_get_registry(state.display);
555 } 245 wl_registry_add_listener(registry, &registry_listener, &state);
556 246 wl_display_roundtrip(state.display);
557 if (registry->pointer) { 247 assert(state.compositor && state.layer_shell && state.shm);
558 // We don't want swaylock to have a pointer 248 if (!state.input_inhibit_manager) {
559 wl_pointer_destroy(registry->pointer); 249 wlr_log(L_ERROR, "Compositor does not support the input inhibitor "
560 registry->pointer = NULL; 250 "protocol, refusing to run insecurely");
561 } 251 }
562 252
563 for (i = 0; i < registry->outputs->length; ++i) { 253 if (wl_list_empty(&state.surfaces)) {
564 struct output_state *output = registry->outputs->items[i]; 254 wlr_log(L_DEBUG, "Exiting - no outputs to show on.");
565 struct window *window = window_setup(registry, 255 return 0;
566 output->width, output->height, output->scale, true);
567 if (!window) {
568 sway_abort("Failed to create surfaces.");
569 }
570 list_add(render_data.surfaces, window);
571 }
572
573 registry->input->notify = notify_key;
574
575 // Different background for the output
576 if (render_data.num_images >= 1) {
577 char **displays_paths = images;
578 render_data.images = calloc(registry->outputs->length, sizeof(cairo_surface_t*));
579
580 int socketfd = ipc_open_socket(socket_path);
581 uint32_t len = 0;
582 char *outputs = ipc_single_command(socketfd, IPC_GET_OUTPUTS, "", &len);
583 struct json_object *json_outputs = json_tokener_parse(outputs);
584
585 for (i = 0; i < registry->outputs->length; ++i) {
586 if (displays_paths[i * 2] != NULL) {
587 for (int j = 0;; ++j) {
588 if (j >= json_object_array_length(json_outputs)) {
589 sway_log(L_ERROR, "%s is not an extant output", displays_paths[i * 2]);
590 exit(EXIT_FAILURE);
591 }
592
593 struct json_object *dsp_name, *at_j = json_object_array_get_idx(json_outputs, j);
594 if (!json_object_object_get_ex(at_j, "name", &dsp_name)) {
595 sway_abort("output doesn't have a name field");
596 }
597 if (!strcmp(displays_paths[i * 2], json_object_get_string(dsp_name))) {
598 render_data.images[j] = load_image(displays_paths[i * 2 + 1]);
599 break;
600 }
601 }
602 }
603 }
604
605 json_object_put(json_outputs);
606 close(socketfd);
607 free(displays_paths);
608 } 256 }
609 257
610 render(&render_data, config); 258 struct swaylock_surface *surface;
611 bool locked = false; 259 wl_list_for_each(surface, &state.surfaces, link) {
612 while (wl_display_dispatch(registry->display) != -1) { 260 surface->image = background_image;
613 if (!locked) { 261
614 for (i = 0; i < registry->outputs->length; ++i) { 262 surface->surface = wl_compositor_create_surface(state.compositor);
615 struct output_state *output = registry->outputs->items[i]; 263 assert(surface->surface);
616 struct window *window = render_data.surfaces->items[i]; 264
617 lock_set_lock_surface(registry->swaylock, output->output, window->surface); 265 surface->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
618 } 266 state.layer_shell, surface->surface, surface->output,
619 locked = true; 267 ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, "lockscreen");
620 } 268 assert(surface->layer_surface);
269
270 zwlr_layer_surface_v1_set_size(surface->layer_surface, 0, 0);
271 zwlr_layer_surface_v1_set_anchor(surface->layer_surface,
272 ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
273 ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
274 ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
275 ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
276 zwlr_layer_surface_v1_set_exclusive_zone(surface->layer_surface, -1);
277 zwlr_layer_surface_v1_set_keyboard_interactivity(
278 surface->layer_surface, true);
279 zwlr_layer_surface_v1_add_listener(surface->layer_surface,
280 &layer_surface_listener, surface);
281 wl_surface_commit(surface->surface);
282 wl_display_roundtrip(state.display);
621 } 283 }
622 284
623 // Free surfaces 285 zwlr_input_inhibit_manager_v1_get_inhibitor(state.input_inhibit_manager);
624 if (render_data.num_images == -1) {
625 cairo_surface_destroy(render_data.image);
626 } else if (render_data.num_images >= 1) {
627 for (i = 0; i < registry->outputs->length; ++i) {
628 if (render_data.images[i] != NULL) {
629 cairo_surface_destroy(render_data.images[i]);
630 }
631 }
632 free(render_data.images);
633 }
634 286
635 for (i = 0; i < render_data.surfaces->length; ++i) { 287 state.run_display = true;
636 struct window *window = render_data.surfaces->items[i]; 288 while (wl_display_dispatch(state.display) != -1 && state.run_display) {
637 window_teardown(window); 289 // This space intentionally left blank
638 } 290 }
639 list_free(render_data.surfaces);
640 registry_teardown(registry);
641
642 free_config(config);
643
644 return 0; 291 return 0;
645} 292}
646
647void render(struct render_data *render_data, struct lock_config *config) {
648 int i;
649 for (i = 0; i < render_data->surfaces->length; ++i) {
650 sway_log(L_DEBUG, "Render surface %d of %d", i, render_data->surfaces->length);
651 struct window *window = render_data->surfaces->items[i];
652 if (!window_prerender(window) || !window->cairo) {
653 continue;
654 }
655 int wwidth = window->width * window->scale;
656 int wheight = window->height * window->scale;
657
658 cairo_save(window->cairo);
659 cairo_set_operator(window->cairo, CAIRO_OPERATOR_CLEAR);
660 cairo_paint(window->cairo);
661 cairo_restore(window->cairo);
662
663 // Reset the transformation matrix
664 cairo_identity_matrix(window->cairo);
665
666 if (render_data->num_images == 0 || render_data->color_set) {
667 render_color(window, render_data->color);
668 }
669
670 if (render_data->num_images == -1) {
671 // One background for all
672 render_image(window, render_data->image, render_data->scaling_mode);
673 } else if (render_data->num_images >= 1) {
674 // Different backgrounds
675 if (render_data->images[i] != NULL) {
676 render_image(window, render_data->images[i], render_data->scaling_mode);
677 }
678 }
679
680 // Reset the transformation matrix again
681 cairo_identity_matrix(window->cairo);
682
683 // Draw specific values (copied from i3)
684 const float TYPE_INDICATOR_RANGE = M_PI / 3.0f;
685 const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f;
686
687 // Add visual indicator
688 if (show_indicator && render_data->auth_state != AUTH_STATE_IDLE) {
689 // Draw circle
690 cairo_set_line_width(window->cairo, config->thickness);
691 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius, 0, 2 * M_PI);
692 switch (render_data->auth_state) {
693 case AUTH_STATE_INPUT:
694 case AUTH_STATE_BACKSPACE: {
695 cairo_set_source_u32(window->cairo, config->colors.normal.inner_ring);
696 cairo_fill_preserve(window->cairo);
697 cairo_set_source_u32(window->cairo, config->colors.normal.outer_ring);
698 cairo_stroke(window->cairo);
699 } break;
700 case AUTH_STATE_VALIDATING: {
701 cairo_set_source_u32(window->cairo, config->colors.validating.inner_ring);
702 cairo_fill_preserve(window->cairo);
703 cairo_set_source_u32(window->cairo, config->colors.validating.outer_ring);
704 cairo_stroke(window->cairo);
705 } break;
706 case AUTH_STATE_INVALID: {
707 cairo_set_source_u32(window->cairo, config->colors.invalid.inner_ring);
708 cairo_fill_preserve(window->cairo);
709 cairo_set_source_u32(window->cairo, config->colors.invalid.outer_ring);
710 cairo_stroke(window->cairo);
711 } break;
712 default: break;
713 }
714
715 // Draw a message
716 char *text = NULL;
717 cairo_set_source_u32(window->cairo, config->colors.text);
718 cairo_select_font_face(window->cairo, config->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
719 cairo_set_font_size(window->cairo, config->radius/3.0f);
720 switch (render_data->auth_state) {
721 case AUTH_STATE_VALIDATING:
722 text = "verifying";
723 break;
724 case AUTH_STATE_INVALID:
725 text = "wrong";
726 break;
727 default: break;
728 }
729
730 if (text) {
731 cairo_text_extents_t extents;
732 double x, y;
733
734 cairo_text_extents(window->cairo, text, &extents);
735 x = wwidth/2 - ((extents.width/2) + extents.x_bearing);
736 y = wheight/2 - ((extents.height/2) + extents.y_bearing);
737
738 cairo_move_to(window->cairo, x, y);
739 cairo_show_text(window->cairo, text);
740 cairo_close_path(window->cairo);
741 cairo_new_sub_path(window->cairo);
742 }
743
744 // Typing indicator: Highlight random part on keypress
745 if (render_data->auth_state == AUTH_STATE_INPUT || render_data->auth_state == AUTH_STATE_BACKSPACE) {
746 static double highlight_start = 0;
747 highlight_start += (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5;
748 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius, highlight_start, highlight_start + TYPE_INDICATOR_RANGE);
749 if (render_data->auth_state == AUTH_STATE_INPUT) {
750 cairo_set_source_u32(window->cairo, config->colors.input_cursor);
751 } else {
752 cairo_set_source_u32(window->cairo, config->colors.backspace_cursor);
753 }
754 cairo_stroke(window->cairo);
755
756 // Draw borders
757 cairo_set_source_u32(window->cairo, config->colors.separator);
758 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius, highlight_start, highlight_start + TYPE_INDICATOR_BORDER_THICKNESS);
759 cairo_stroke(window->cairo);
760
761 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius, highlight_start + TYPE_INDICATOR_RANGE, (highlight_start + TYPE_INDICATOR_RANGE) + TYPE_INDICATOR_BORDER_THICKNESS);
762 cairo_stroke(window->cairo);
763 }
764
765 switch(line_source) {
766 case LINE_SOURCE_RING:
767 switch(render_data->auth_state) {
768 case AUTH_STATE_VALIDATING:
769 cairo_set_source_u32(window->cairo, config->colors.validating.outer_ring);
770 break;
771 case AUTH_STATE_INVALID:
772 cairo_set_source_u32(window->cairo, config->colors.invalid.outer_ring);
773 break;
774 default:
775 cairo_set_source_u32(window->cairo, config->colors.normal.outer_ring);
776 }
777 break;
778 case LINE_SOURCE_INSIDE:
779 switch(render_data->auth_state) {
780 case AUTH_STATE_VALIDATING:
781 cairo_set_source_u32(window->cairo, config->colors.validating.inner_ring);
782 break;
783 case AUTH_STATE_INVALID:
784 cairo_set_source_u32(window->cairo, config->colors.invalid.inner_ring);
785 break;
786 default:
787 cairo_set_source_u32(window->cairo, config->colors.normal.inner_ring);
788 break;
789 }
790 break;
791 default:
792 cairo_set_source_u32(window->cairo, config->colors.line);
793 break;
794 }
795 // Draw inner + outer border of the circle
796 cairo_set_line_width(window->cairo, 2.0);
797 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius - config->thickness/2, 0, 2*M_PI);
798 cairo_stroke(window->cairo);
799 cairo_arc(window->cairo, wwidth/2, wheight/2, config->radius + config->thickness/2, 0, 2*M_PI);
800 cairo_stroke(window->cairo);
801 }
802 window_render(window);
803 }
804}