aboutsummaryrefslogtreecommitdiffstats
path: root/swaylock
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-04-02 22:48:13 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2018-04-04 18:47:48 -0400
commitb32bf595aeae7f8ac68354e45a80c0438374ec17 (patch)
treeb93185d2ac4b7081b9d0651eaaadeae21de48929 /swaylock
parentMove swaybg background rendering into common/ (diff)
downloadsway-b32bf595aeae7f8ac68354e45a80c0438374ec17.tar.gz
sway-b32bf595aeae7f8ac68354e45a80c0438374ec17.tar.zst
sway-b32bf595aeae7f8ac68354e45a80c0438374ec17.zip
Initial swaylock port
Diffstat (limited to 'swaylock')
-rw-r--r--swaylock/main.c952
-rw-r--r--swaylock/meson.build18
2 files changed, 265 insertions, 705 deletions
diff --git a/swaylock/main.c b/swaylock/main.c
index c2615951..8673694d 100644
--- a/swaylock/main.c
+++ b/swaylock/main.c
@@ -1,387 +1,235 @@
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 "background-image.h"
19#include "swaylock/swaylock.h" 17#include "pool-buffer.h"
20#include "ipc-client.h" 18#include "cairo.h"
21#include "log.h"
22#include "util.h" 19#include "util.h"
23 20#include "wlr-layer-shell-unstable-v1-client-protocol.h"
24struct registry *registry; 21
25struct render_data render_data; 22struct swaylock_args {
26struct lock_config *config; 23 uint32_t color;
27bool show_indicator = true; 24 enum background_mode mode;
28 25 bool show_indicator;
29void wl_dispatch_events() { 26};
30 wl_display_flush(registry->display); 27
31 if (wl_display_dispatch(registry->display) == -1) { 28struct swaylock_state {
32 sway_log(L_ERROR, "failed to run wl_display_dispatch"); 29 struct wl_display *display;
33 exit(1); 30 struct wl_compositor *compositor;
31 struct zwlr_layer_shell_v1 *layer_shell;
32 struct wl_shm *shm;
33 struct wl_list contexts;
34 struct swaylock_args args;
35 bool run_display;
36};
37
38struct swaylock_context {
39 cairo_surface_t *image;
40 struct swaylock_state *state;
41 struct wl_output *output;
42 struct wl_surface *surface;
43 struct zwlr_layer_surface_v1 *layer_surface;
44 struct pool_buffer buffers[2];
45 struct pool_buffer *current_buffer;
46 uint32_t width, height;
47 struct wl_list link;
48};
49
50static void daemonize() {
51 if (fork() == 0) {
52 int devnull = open("/dev/null", O_RDWR);
53 dup2(STDOUT_FILENO, devnull);
54 dup2(STDERR_FILENO, devnull);
55 chdir("/");
56 } else {
57 exit(0);
34 } 58 }
35} 59}
36 60
37void sigalarm_handler(int sig) { 61static void render_frame(struct swaylock_context *context) {
38 signal(SIGALRM, SIG_IGN); 62 struct swaylock_state *state = context->state;
39 // Hide typing indicator 63 context->current_buffer = get_next_buffer(state->shm,
40 render_data.auth_state = AUTH_STATE_IDLE; 64 context->buffers, context->width, context->height);
41 render(&render_data, config); 65 cairo_t *cairo = context->current_buffer->cairo;
42 wl_display_flush(registry->display); 66 if (state->args.mode == BACKGROUND_MODE_SOLID_COLOR) {
43 signal(SIGALRM, sigalarm_handler); 67 cairo_set_source_u32(cairo, state->args.color);
44} 68 cairo_paint(cairo);
45 69 } else {
46void sway_terminate(int exit_code) { 70 render_background_image(cairo, context->image,
47 int i; 71 state->args.mode, context->width, context->height);
48 for (i = 0; i < render_data.surfaces->length; ++i) {
49 struct window *window = render_data.surfaces->items[i];
50 window_teardown(window);
51 }
52 list_free(render_data.surfaces);
53 if (registry) {
54 registry_teardown(registry);
55 } 72 }
56 exit(exit_code); 73 wl_surface_attach(context->surface, context->current_buffer->buffer, 0, 0);
74 wl_surface_damage(context->surface, 0, 0, context->width, context->height);
75 wl_surface_commit(context->surface);
57} 76}
58 77
59char *password; 78static void layer_surface_configure(void *data,
60int password_size; 79 struct zwlr_layer_surface_v1 *surface,
61enum line_source line_source = LINE_SOURCE_DEFAULT; 80 uint32_t serial, uint32_t width, uint32_t height) {
62 81 struct swaylock_context *context = data;
63struct lock_config *init_config() { 82 context->width = width;
64 struct lock_config *config = calloc(1, sizeof(struct lock_config)); 83 context->height = height;
65 84 zwlr_layer_surface_v1_ack_configure(surface, serial);
66 config->font = strdup("sans-serif"); 85 render_frame(context);
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
81 config->colors.invalid.inner_ring = 0xFA0000BF;
82 config->colors.invalid.outer_ring = 0x7D3300FF;
83
84 config->radius = 50;
85 config->thickness = 10;
86
87 return config;
88} 86}
89 87
90void free_config(struct lock_config *config) { 88static void layer_surface_closed(void *data,
91 free(config->font); 89 struct zwlr_layer_surface_v1 *surface) {
92 free(config); 90 struct swaylock_context *context = data;
91 zwlr_layer_surface_v1_destroy(context->layer_surface);
92 wl_surface_destroy(context->surface);
93 context->state->run_display = false;
93} 94}
94 95
95int function_conversation(int num_msg, const struct pam_message **msg, 96struct zwlr_layer_surface_v1_listener layer_surface_listener = {
96 struct pam_response **resp, void *appdata_ptr) { 97 .configure = layer_surface_configure,
97 98 .closed = layer_surface_closed,
98 const char* msg_style_names[] = { 99};
99 NULL,
100 "PAM_PROMPT_ECHO_OFF",
101 "PAM_PROMPT_ECHO_ON",
102 "PAM_ERROR_MSG",
103 "PAM_TEXT_INFO",
104 };
105 100
106 /* PAM expects an array of responses, one for each message */ 101static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
107 struct pam_response *pam_reply = calloc(num_msg, sizeof(struct pam_response)); 102 uint32_t serial, struct wl_surface *surface,
108 *resp = pam_reply; 103 wl_fixed_t surface_x, wl_fixed_t surface_y) {
109 104 wl_pointer_set_cursor(wl_pointer, serial, NULL, 0, 0);
110 for(int i=0; i<num_msg; ++i) { 105}
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 106
121 case PAM_ERROR_MSG: 107static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
122 case PAM_TEXT_INFO: 108 uint32_t serial, struct wl_surface *surface) {
123 break; 109 // Who cares
124 } 110}
125 }
126 111
127 return PAM_SUCCESS; 112static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
113 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
114 // Who cares
128} 115}
129 116
130/** 117static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
131 * Note: PAM will free() 'password' during the process 118 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
132 */ 119 // Who cares
133bool verify_password() {
134 struct passwd *passwd = getpwuid(getuid());
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 }
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} 120}
151 121
152void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t code, uint32_t codepoint) { 122static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
153 int redraw_screen = 0; 123 uint32_t time, uint32_t axis, wl_fixed_t value) {
154 char *password_realloc; 124 // Who cares
155 int i; 125}
156 126
157 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { 127static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) {
158 switch (sym) { 128 // Who cares
159 case XKB_KEY_KP_Enter: 129}
160 case XKB_KEY_Return:
161 render_data.auth_state = AUTH_STATE_VALIDATING;
162 130
163 render(&render_data, config); 131static void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
164 // Make sure our render call will actually be displayed on the screen 132 uint32_t axis_source) {
165 wl_dispatch_events(); 133 // Who cares
134}
166 135
167 if (verify_password()) { 136static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
168 exit(0); 137 uint32_t time, uint32_t axis) {
169 } 138 // Who cares
139}
170 140
171 render_data.auth_state = AUTH_STATE_INVALID; 141static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
172 redraw_screen = 1; 142 uint32_t axis, int32_t discrete) {
143 // Who cares
144}
173 145
174 password_size = 1024; 146struct wl_pointer_listener pointer_listener = {
175 password = malloc(password_size); 147 .enter = wl_pointer_enter,
176 password[0] = '\0'; 148 .leave = wl_pointer_leave,
177 break; 149 .motion = wl_pointer_motion,
178 case XKB_KEY_BackSpace: 150 .button = wl_pointer_button,
179 i = strlen(password); 151 .axis = wl_pointer_axis,
180 if (i > 0) { 152 .frame = wl_pointer_frame,
181 password[i - 1] = '\0'; 153 .axis_source = wl_pointer_axis_source,
182 render_data.auth_state = AUTH_STATE_BACKSPACE; 154 .axis_stop = wl_pointer_axis_stop,
183 redraw_screen = 1; 155 .axis_discrete = wl_pointer_axis_discrete,
184 } 156};
185 break; 157
186 case XKB_KEY_Control_L: 158static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
187 case XKB_KEY_Control_R: 159 enum wl_seat_capability caps) {
188 case XKB_KEY_Shift_L: 160 if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
189 case XKB_KEY_Shift_R: 161 struct wl_pointer *pointer = wl_seat_get_pointer(wl_seat);
190 case XKB_KEY_Caps_Lock: 162 wl_pointer_add_listener(pointer, &pointer_listener, NULL);
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 } 163 }
246} 164}
247 165
248void render_color(struct window *window, uint32_t color) { 166static void seat_handle_name(void *data, struct wl_seat *wl_seat,
249 cairo_set_source_u32(window->cairo, color); 167 const char *name) {
250 cairo_paint(window->cairo); 168 // Who cares
251} 169}
252 170
253void render_image(struct window *window, cairo_surface_t *image, enum scaling_mode scaling_mode) { 171const struct wl_seat_listener seat_listener = {
254 double width = cairo_image_surface_get_width(image); 172 .capabilities = seat_handle_capabilities,
255 double height = cairo_image_surface_get_height(image); 173 .name = seat_handle_name,
256 int wwidth = window->width * window->scale; 174};
257 int wheight = window->height * window->scale; 175
258 176static void handle_global(void *data, struct wl_registry *registry,
259 switch (scaling_mode) { 177 uint32_t name, const char *interface, uint32_t version) {
260 case SCALING_MODE_STRETCH: 178 struct swaylock_state *state = data;
261 cairo_scale(window->cairo, 179 if (strcmp(interface, wl_compositor_interface.name) == 0) {
262 (double) wwidth / width, 180 state->compositor = wl_registry_bind(registry, name,
263 (double) wheight / height); 181 &wl_compositor_interface, 1);
264 cairo_set_source_surface(window->cairo, image, 0, 0); 182 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
265 break; 183 state->shm = wl_registry_bind(registry, name,
266 case SCALING_MODE_FILL: 184 &wl_shm_interface, 1);
267 { 185 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
268 double window_ratio = (double) wwidth / wheight; 186 struct wl_seat *seat = wl_registry_bind(
269 double bg_ratio = width / height; 187 registry, name, &wl_seat_interface, 1);
270 188 wl_seat_add_listener(seat, &seat_listener, NULL);
271 if (window_ratio > bg_ratio) { 189 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
272 double scale = (double) wwidth / width; 190 state->layer_shell = wl_registry_bind(
273 cairo_scale(window->cairo, scale, scale); 191 registry, name, &zwlr_layer_shell_v1_interface, 1);
274 cairo_set_source_surface(window->cairo, image, 192 } else if (strcmp(interface, wl_output_interface.name) == 0) {
275 0, 193 struct swaylock_context *context =
276 (double) wheight/2 / scale - height/2); 194 calloc(1, sizeof(struct swaylock_context));
277 } else { 195 context->state = state;
278 double scale = (double) wheight / height; 196 context->output = wl_registry_bind(registry, name,
279 cairo_scale(window->cairo, scale, scale); 197 &wl_output_interface, 1);
280 cairo_set_source_surface(window->cairo, image, 198 wl_list_insert(&state->contexts, &context->link);
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 } 199 }
200}
319 201
320 cairo_paint(window->cairo); 202static void handle_global_remove(void *data, struct wl_registry *registry,
203 uint32_t name) {
204 // who cares
321} 205}
322 206
323cairo_surface_t *load_image(char *image_path) { 207static const struct wl_registry_listener registry_listener = {
324 cairo_surface_t *image = NULL; 208 .global = handle_global,
209 .global_remove = handle_global_remove,
210};
325 211
326#ifdef WITH_GDK_PIXBUF 212static struct swaylock_state state;
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 213
341 return image; 214static void sigalarm_handler(int sig) {
215 signal(SIGALRM, SIG_IGN);
216 // TODO: Hide typing indicator
217 signal(SIGALRM, sigalarm_handler);
342} 218}
343 219
344int main(int argc, char **argv) { 220int 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); 221 signal(SIGALRM, sigalarm_handler);
358 222
359 static struct option long_options[] = { 223 static struct option long_options[] = {
360 {"help", no_argument, NULL, 'h'}, 224 {"help", no_argument, NULL, 'h'},
361 {"color", required_argument, NULL, 'c'}, 225 {"color", required_argument, NULL, 'c'},
362 {"image", required_argument, NULL, 'i'}, 226 {"image", required_argument, NULL, 'i'},
363 {"scaling", required_argument, NULL, 0}, 227 {"scaling", required_argument, NULL, 's'},
364 {"tiling", no_argument, NULL, 't'}, 228 {"tiling", no_argument, NULL, 't'},
365 {"version", no_argument, NULL, 'v'}, 229 {"version", no_argument, NULL, 'v'},
366 {"socket", required_argument, NULL, 'p'}, 230 {"socket", required_argument, NULL, 'p'},
367 {"no-unlock-indicator", no_argument, NULL, 'u'}, 231 {"no-unlock-indicator", no_argument, NULL, 'u'},
368 {"daemonize", no_argument, NULL, 'f'}, 232 {"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} 233 {0, 0, 0, 0}
386 }; 234 };
387 235
@@ -390,415 +238,109 @@ int main(int argc, char **argv) {
390 "\n" 238 "\n"
391 " -h, --help Show help message and quit.\n" 239 " -h, --help Show help message and quit.\n"
392 " -c, --color <rrggbb[aa]> Turn the screen into the given color instead of white.\n" 240 " -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" 241 " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n"
394 " -t, --tiling Same as --scaling=tile.\n" 242 " -t, --tiling Same as --scaling=tile.\n"
395 " -v, --version Show the version number and quit.\n" 243 " -v, --version Show the version number and quit.\n"
396 " -i, --image [<output>:]<path> Display the given image.\n" 244 " -i, --image [<output>:]<path> Display the given image.\n"
397 " -u, --no-unlock-indicator Disable the unlock indicator.\n" 245 " -u, --no-unlock-indicator Disable the unlock indicator.\n"
398 " -f, --daemonize Detach from the controlling terminal.\n" 246 " -f, --daemonize Detach from the controlling terminal.\n"
399 " --socket <socket> Use the specified socket.\n" 247 " --socket <socket> Use the specified socket.\n";
400 " For more information see `man swaylock`\n";
401
402 248
403 registry = registry_poll(); 249 struct swaylock_args args = {
250 .mode = BACKGROUND_MODE_SOLID_COLOR,
251 .color = 0xFFFFFFFF,
252 .show_indicator = true,
253 };
254 state.args = args;
255 wlr_log_init(L_DEBUG, NULL);
404 256
405 int c; 257 int c;
406 while (1) { 258 while (1) {
407 int option_index = 0; 259 int option_index = 0;
408 c = getopt_long(argc, argv, "hc:i:srtvuf", long_options, &option_index); 260 c = getopt_long(argc, argv, "hc:i:s:tvuf", long_options, &option_index);
409 if (c == -1) { 261 if (c == -1) {
410 break; 262 break;
411 } 263 }
412 switch (c) { 264 switch (c) {
413 case 'c': 265 case 'c': {
414 { 266 state.args.color = parse_color(optarg);
415 render_data.color = parse_color(optarg); 267 state.args.mode = BACKGROUND_MODE_SOLID_COLOR;
416 render_data.color_set = 1;
417 break; 268 break;
418 } 269 }
419 case 'i': 270 case 'i':
420 { 271 // TODO
421 char *image_path = strchr(optarg, ':'); 272 return 1;
422 if (image_path == NULL) { 273 case 's':
423 if (render_data.num_images == 0) { 274 state.args.mode = parse_background_mode(optarg);
424 // Provided image without output 275 if (state.args.mode == BACKGROUND_MODE_INVALID) {
425 render_data.image = load_image(optarg); 276 return 1;
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 } 277 }
444 break; 278 break;
445 }
446 case 't': 279 case 't':
447 scaling_mode_str = "tile"; 280 // TODO
448 break;
449 case 'p':
450 socket_path = optarg;
451 break; 281 break;
452 case 'v': 282 case 'v':
453 fprintf(stdout, "swaylock version " SWAY_VERSION "\n"); 283#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
454 exit(EXIT_SUCCESS); 284 fprintf(stdout, "swaylock version %s (%s, branch \"%s\")\n",
455 break; 285 SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
286#else
287 fprintf(stdout, "version unknown\n");
288#endif
289 return 0;
456 case 'u': 290 case 'u':
457 show_indicator = false; 291 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; 292 break;
468 } 293 case 'f':
469 case 'r': 294 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; 295 break;
517 default: 296 default:
518 fprintf(stderr, "%s", usage); 297 fprintf(stderr, "%s", usage);
519 exit(EXIT_FAILURE); 298 return 1;
520 } 299 }
521 } 300 }
522 301
523 render_data.scaling_mode = SCALING_MODE_STRETCH; 302 wl_list_init(&state.contexts);
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 }
547 }
548
549 if (!registry) {
550 sway_abort("Unable to connect to wayland compositor");
551 }
552
553 if (!registry->swaylock) {
554 sway_abort("swaylock requires the compositor to support the swaylock extension.");
555 }
556 303
557 if (registry->pointer) { 304 assert(state.display = wl_display_connect(NULL));
558 // We don't want swaylock to have a pointer
559 wl_pointer_destroy(registry->pointer);
560 registry->pointer = NULL;
561 }
562
563 for (i = 0; i < registry->outputs->length; ++i) {
564 struct output_state *output = registry->outputs->items[i];
565 struct window *window = window_setup(registry,
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 305
605 json_object_put(json_outputs); 306 struct wl_registry *registry = wl_display_get_registry(state.display);
606 close(socketfd); 307 wl_registry_add_listener(registry, &registry_listener, &state);
607 free(displays_paths); 308 wl_display_roundtrip(state.display);
608 } 309 assert(state.compositor && state.layer_shell && state.shm);
609 310
610 render(&render_data, config); 311 if (wl_list_empty(&state.contexts)) {
611 bool locked = false; 312 wlr_log(L_DEBUG, "Exiting - no outputs to show on.");
612 while (wl_display_dispatch(registry->display) != -1) { 313 return 0;
613 if (!locked) {
614 for (i = 0; i < registry->outputs->length; ++i) {
615 struct output_state *output = registry->outputs->items[i];
616 struct window *window = render_data.surfaces->items[i];
617 lock_set_lock_surface(registry->swaylock, output->output, window->surface);
618 }
619 locked = true;
620 }
621 } 314 }
622 315
623 // Free surfaces 316 struct swaylock_context *context;
624 if (render_data.num_images == -1) { 317 wl_list_for_each(context, &state.contexts, link) {
625 cairo_surface_destroy(render_data.image); 318 assert(context->surface =
626 } else if (render_data.num_images >= 1) { 319 wl_compositor_create_surface(state.compositor));
627 for (i = 0; i < registry->outputs->length; ++i) { 320
628 if (render_data.images[i] != NULL) { 321 context->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
629 cairo_surface_destroy(render_data.images[i]); 322 state.layer_shell, context->surface, context->output,
630 } 323 ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, "lockscreen");
631 } 324 assert(context->layer_surface);
632 free(render_data.images); 325
326 zwlr_layer_surface_v1_set_size(context->layer_surface, 0, 0);
327 zwlr_layer_surface_v1_set_anchor(context->layer_surface,
328 ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
329 ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
330 ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
331 ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
332 zwlr_layer_surface_v1_set_exclusive_zone(context->layer_surface, -1);
333 zwlr_layer_surface_v1_set_keyboard_interactivity(
334 context->layer_surface, true);
335 zwlr_layer_surface_v1_add_listener(context->layer_surface,
336 &layer_surface_listener, context);
337 wl_surface_commit(context->surface);
338 wl_display_roundtrip(state.display);
633 } 339 }
634 340
635 for (i = 0; i < render_data.surfaces->length; ++i) { 341 state.run_display = true;
636 struct window *window = render_data.surfaces->items[i]; 342 while (wl_display_dispatch(state.display) != -1 && state.run_display) {
637 window_teardown(window); 343 // This space intentionally left blank
638 } 344 }
639 list_free(render_data.surfaces);
640 registry_teardown(registry);
641
642 free_config(config);
643
644 return 0; 345 return 0;
645} 346}
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}
diff --git a/swaylock/meson.build b/swaylock/meson.build
new file mode 100644
index 00000000..5b886ded
--- /dev/null
+++ b/swaylock/meson.build
@@ -0,0 +1,18 @@
1executable(
2 'swaylock',
3 'main.c',
4 include_directories: [sway_inc],
5 dependencies: [
6 cairo,
7 client_protos,
8 gdk_pixbuf,
9 libpam,
10 math,
11 pango,
12 pangocairo,
13 wayland_client,
14 wlroots,
15 ],
16 link_with: [lib_sway_common, lib_sway_client],
17 install: true
18)