forked from swaywm/swaylock
-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.c
1898 lines (1754 loc) · 56.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <wayland-client.h>
#include <wordexp.h>
#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 "wlr-screencopy-unstable-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"
// returns a positive integer in milliseconds
static uint32_t parse_seconds(const char *seconds) {
char *endptr;
errno = 0;
float val = strtof(seconds, &endptr);
if (errno != 0) {
swaylock_log(LOG_DEBUG, "Invalid number for seconds %s, defaulting to 0", seconds);
return 0;
}
if (endptr == seconds) {
swaylock_log(LOG_DEBUG, "No digits were found in %s, defaulting to 0", seconds);
return 0;
}
if (val < 0) {
swaylock_log(LOG_DEBUG, "Negative seconds not allowed for %s, defaulting to 0", seconds);
return 0;
}
return (uint32_t)floor(val * 1000);
}
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;
}
static const char *parse_screen_pos(const char *str, struct swaylock_effect_screen_pos *pos) {
char *eptr;
float res = strtof(str, &eptr);
if (eptr == str)
return NULL;
pos->pos = res;
if (eptr[0] == '%') {
pos->is_percent = true;
return eptr + 1;
} else {
pos->is_percent = false;
return eptr;
}
}
static const char *parse_screen_pos_pair(const char *str, char delim,
struct swaylock_effect_screen_pos *pos1,
struct swaylock_effect_screen_pos *pos2) {
struct swaylock_effect_screen_pos tpos1, tpos2;
str = parse_screen_pos(str, &tpos1);
if (str == NULL || str[0] != delim)
return NULL;
str = parse_screen_pos(str + 1, &tpos2);
if (str == NULL)
return NULL;
pos1->pos = tpos1.pos;
pos1->is_percent = tpos1.is_percent;
pos2->pos = tpos2.pos;
pos2->is_percent = tpos2.is_percent;
return str;
}
static const char *parse_constant(const char *str1, const char *str2) {
size_t len = strlen(str2);
if (strncmp(str1, str2, len) == 0) {
return str1 + len;
} else {
return NULL;
}
}
static int parse_gravity_from_xy(float x, float y) {
if (x >= 0 && y >= 0)
return EFFECT_COMPOSE_GRAV_NW;
else if (x >= 0 && y < 0)
return EFFECT_COMPOSE_GRAV_SW;
else if (x < 0 && y >= 0)
return EFFECT_COMPOSE_GRAV_NE;
else
return EFFECT_COMPOSE_GRAV_SE;
}
static void parse_effect_compose(const char *str, struct swaylock_effect *effect) {
effect->e.compose.x = effect->e.compose.y = (struct swaylock_effect_screen_pos) { 50, 1 }; // 50%
effect->e.compose.w = effect->e.compose.h = (struct swaylock_effect_screen_pos) { -1, 0 }; // -1
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_CENTER;
effect->e.compose.imgpath = NULL;
// Parse position if they exist
const char *s = parse_screen_pos_pair(str, ',', &effect->e.compose.x, &effect->e.compose.y);
if (s == NULL) {
s = str;
} else {
// If we're given an x/y position, determine gravity automatically
// from whether x and y is positive or not
effect->e.compose.gravity = parse_gravity_from_xy(
effect->e.compose.x.pos, effect->e.compose.y.pos);
s += 1;
str = s;
}
// Parse dimensions if they exist
s = parse_screen_pos_pair(str, 'x', &effect->e.compose.w, &effect->e.compose.h);
if (s == NULL) {
s = str;
} else {
s += 1;
str = s;
}
// Parse gravity if it exists
if ((s = parse_constant(str, "center;")) != NULL)
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_CENTER;
else if ((s = parse_constant(str, "northwest;")) != NULL)
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_NW;
else if ((s = parse_constant(str, "northeast;")) != NULL)
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_NE;
else if ((s = parse_constant(str, "southwest;")) != NULL)
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_SW;
else if ((s = parse_constant(str, "southeast;")) != NULL)
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_SE;
else if ((s = parse_constant(str, "north;")) != NULL)
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_N;
else if ((s = parse_constant(str, "south;")) != NULL)
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_S;
else if ((s = parse_constant(str, "east;")) != NULL)
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_E;
else if ((s = parse_constant(str, "west;")) != NULL)
effect->e.compose.gravity = EFFECT_COMPOSE_GRAV_W;
if (s == NULL) {
s = str;
} else {
str = s;
}
// The rest is the file name
effect->e.compose.imgpath = strdup(str);
}
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 int daemonize_start() {
swaylock_trace();
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);
}
return 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 daemonize_done(void *fdptr) {
swaylock_trace();
int *fd = (int *)fdptr;
if (*fd < 0) {
return;
}
uint8_t success = 1;
if (write(*fd, &success, 1) != 1) {
swaylock_log(LOG_ERROR, "Failed to tell parent process that daemonization is done");
exit(1);
}
close(*fd);
*fd = -1;
}
static void destroy_surface(struct swaylock_surface *surface) {
swaylock_log(LOG_DEBUG, "Destroy surface for output %s", surface->output_name);
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]);
destroy_buffer(&surface->indicator_buffers[0]);
destroy_buffer(&surface->indicator_buffers[1]);
fade_destroy(&surface->fade);
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 (!fade_is_complete(&surface->fade)) {
return false;
}
if (surface->image) {
return cairo_surface_get_content(surface->image) == CAIRO_CONTENT_COLOR;
}
return (surface->state->args.colors.background & 0xff) == 0xff;
}
struct zxdg_output_v1_listener _xdg_output_listener;
static void create_layer_surface(struct swaylock_surface *surface) {
struct swaylock_state *state = surface->state;
if (state->args.fade_in) {
surface->fade.target_time = state->args.fade_in;
}
surface->image = select_image(state, surface);
static bool has_printed_zxdg_error = false;
if (state->zxdg_output_manager) {
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);
surface->events_pending += 1;
} else if (!has_printed_zxdg_error) {
swaylock_log(LOG_INFO, "Compositor does not support zxdg output "
"manager, images assigned to named outputs will not work");
has_printed_zxdg_error = true;
}
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);
surface->events_pending += 1;
wl_surface_commit(surface->surface);
}
static void initially_render_surface(struct swaylock_surface *surface) {
swaylock_log(LOG_DEBUG, "Surface for output %s ready", surface->output_name);
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);
}
render_frame_background(surface);
render_background_fade_prepare(surface, surface->current_buffer);
render_frame(surface);
}
static void layer_surface_configure(void *data,
struct zwlr_layer_surface_v1 *layer_surface,
uint32_t serial, uint32_t width, uint32_t height) {
swaylock_trace();
struct swaylock_surface *surface = data;
surface->width = width;
surface->height = height;
surface->indicator_width = 0;
surface->indicator_height = 0;
zwlr_layer_surface_v1_ack_configure(layer_surface, serial);
if (!surface->configured && --surface->events_pending == 0) {
initially_render_surface(surface);
}
surface->configured = true;
}
static void layer_surface_closed(void *data,
struct zwlr_layer_surface_v1 *layer_surface) {
swaylock_trace();
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;
surface->dirty = false;
if (!fade_is_complete(&surface->fade)) {
render_background_fade(surface, time);
surface->dirty = true;
}
render_frame(surface);
}
}
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) {
swaylock_trace();
struct swaylock_surface *surface = data;
surface->subpixel = subpixel;
surface->transform = transform;
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) {
swaylock_trace();
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 struct wl_buffer *create_shm_buffer(struct wl_shm *shm, enum wl_shm_format fmt,
int width, int height, int stride, void **data_out) {
int size = stride * height;
const char shm_name[] = "/swaylock-shm";
int fd = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd < 0) {
fprintf(stderr, "shm_open failed\n");
return NULL;
}
shm_unlink(shm_name);
int ret;
while ((ret = ftruncate(fd, size)) == EINTR) {
// No-op
}
if (ret < 0) {
close(fd);
fprintf(stderr, "ftruncate failed\n");
return NULL;
}
void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
return NULL;
}
struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
close(fd);
struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, width, height,
stride, fmt);
wl_shm_pool_destroy(pool);
*data_out = data;
return buffer;
}
static cairo_surface_t *apply_effects(cairo_surface_t *image, struct swaylock_state *state, int scale) {
if (state->args.effects_count == 0) {
return image;
}
if (state->args.time_effects) {
return swaylock_effects_run_timed(
image, scale,
state->args.effects, state->args.effects_count);
} else {
return swaylock_effects_run(
image, scale,
state->args.effects, state->args.effects_count);
}
}
static void handle_screencopy_frame_buffer(void *data,
struct zwlr_screencopy_frame_v1 *frame, uint32_t format, uint32_t width,
uint32_t height, uint32_t stride) {
swaylock_trace();
struct swaylock_surface *surface = data;
struct swaylock_image *image = calloc(1, sizeof(struct swaylock_image));
image->path = NULL;
image->output_name = surface->output_name;
void *bufdata;
struct wl_buffer *buf = create_shm_buffer(surface->state->shm, format, width, height, stride, &bufdata);
if (buf == NULL) {
free(image);
return;
}
surface->screencopy.format = format;
surface->screencopy.width = width;
surface->screencopy.height = height;
surface->screencopy.stride = stride;
surface->screencopy.image = image;
surface->screencopy.data = bufdata;
zwlr_screencopy_frame_v1_copy(frame, buf);
}
static void handle_screencopy_frame_flags(void *data,
struct zwlr_screencopy_frame_v1 *frame, uint32_t flags) {
swaylock_trace();
struct swaylock_surface *surface = data;
// The transform affecting a screenshot consists of three parts:
// Whether it's flipped vertically, whether it's flipped horizontally,
// and the four rotation options (0, 90, 180, 270).
// Any of the combinations of vertical flips, horizontal flips and rotation,
// can be expressed in terms of only horizontal flips and rotation
// (which is what the enum wl_output_transform encodes).
// Therefore, instead of inverting the Y axis or keeping around the
// "was it vertically flipped?" bit, we just map our state space onto the
// state space encoded by wl_output_transform and let load_background_from_buffer
// handle the rest.
if (flags & ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT) {
switch (surface->transform) {
case WL_OUTPUT_TRANSFORM_NORMAL:
surface->screencopy.transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
break;
case WL_OUTPUT_TRANSFORM_90:
surface->screencopy.transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
break;
case WL_OUTPUT_TRANSFORM_180:
surface->screencopy.transform = WL_OUTPUT_TRANSFORM_FLIPPED;
break;
case WL_OUTPUT_TRANSFORM_270:
surface->screencopy.transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED:
surface->screencopy.transform = WL_OUTPUT_TRANSFORM_180;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
surface->screencopy.transform = WL_OUTPUT_TRANSFORM_90;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
surface->screencopy.transform = WL_OUTPUT_TRANSFORM_NORMAL;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
surface->screencopy.transform = WL_OUTPUT_TRANSFORM_270;
break;
}
} else {
surface->screencopy.transform = surface->transform;
}
}
static void handle_screencopy_frame_ready(void *data,
struct zwlr_screencopy_frame_v1 *frame, uint32_t tv_sec_hi,
uint32_t tv_sec_lo, uint32_t tv_nsec) {
swaylock_trace();
struct swaylock_surface *surface = data;
struct swaylock_state *state = surface->state;
cairo_surface_t *image = load_background_from_buffer(
surface->screencopy.data,
surface->screencopy.format,
surface->screencopy.width,
surface->screencopy.height,
surface->screencopy.stride,
surface->screencopy.transform);
if (image == NULL) {
swaylock_log(LOG_ERROR, "Failed to create image from screenshot");
} else {
surface->screencopy.image->cairo_surface =
apply_effects(image, state, surface->scale);
surface->image = surface->screencopy.image->cairo_surface;
}
swaylock_log(LOG_DEBUG, "Loaded screenshot for output %s", surface->output_name);
wl_list_insert(&state->images, &surface->screencopy.image->link);
if (--surface->events_pending == 0) {
initially_render_surface(surface);
}
}
static void handle_screencopy_frame_failed(void *data,
struct zwlr_screencopy_frame_v1 *frame) {
swaylock_trace();
struct swaylock_surface *surface = data;
swaylock_log(LOG_ERROR, "Screencopy failed");
if (--surface->events_pending == 0) {
initially_render_surface(surface);
}
}
static const struct zwlr_screencopy_frame_v1_listener screencopy_frame_listener = {
.buffer = handle_screencopy_frame_buffer,
.flags = handle_screencopy_frame_flags,
.ready = handle_screencopy_frame_ready,
.failed = handle_screencopy_frame_failed,
};
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_trace();
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) {
swaylock_trace();
struct swaylock_surface *surface = data;
struct swaylock_state *state = surface->state;
cairo_surface_t *new_image = select_image(surface->state, surface);
if (new_image == surface->image && state->args.screenshots) {
static bool has_printed_screencopy_error = false;
if (state->screencopy_manager) {
surface->screencopy_frame = zwlr_screencopy_manager_v1_capture_output(
state->screencopy_manager, false, surface->output);
zwlr_screencopy_frame_v1_add_listener(surface->screencopy_frame,
&screencopy_frame_listener, surface);
surface->events_pending += 1;
} else if (!has_printed_screencopy_error) {
swaylock_log(LOG_INFO, "Compositor does not support screencopy manager, "
"screenshots will not work");
has_printed_screencopy_error = true;
}
} else if (new_image != NULL) {
if (state->args.screenshots) {
swaylock_log(LOG_DEBUG,
"Using existing image instead of taking a screenshot for output %s.",
surface->output_name);
}
surface->image = new_image;
}
if (--surface->events_pending == 0) {
initially_render_surface(surface);
}
}
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, 4);
} 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, 4);
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);
}
} else if (strcmp(interface, zwlr_screencopy_manager_v1_interface.name) == 0) {
state->screencopy_manager = wl_registry_bind(registry, name,
&zwlr_screencopy_manager_v1_interface, 1);
}
}
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) {
// [[<output>]:]<path>
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_TRACE,
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,
LO_EFFECT_BLUR,
LO_EFFECT_PIXELATE,
LO_EFFECT_SCALE,
LO_EFFECT_GREYSCALE,
LO_EFFECT_VIGNETTE,
LO_EFFECT_COMPOSE,
LO_EFFECT_CUSTOM,
LO_TIME_EFFECTS,
LO_INDICATOR,
LO_CLOCK,
LO_TIMESTR,
LO_DATESTR,
LO_FADE_IN,
LO_SUBMIT_ON_TOUCH,
LO_GRACE,
LO_GRACE_NO_MOUSE,
LO_GRACE_NO_TOUCH,
};
static struct option long_options[] = {
{"config", required_argument, NULL, 'C'},
{"color", required_argument, NULL, 'c'},
{"debug", no_argument, NULL, 'd'},
{"trace", no_argument, NULL, LO_TRACE},
{"ignore-empty-password", no_argument, NULL, 'e'},
{"daemonize", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"image", required_argument, NULL, 'i'},
{"screenshots", no_argument, NULL, 'S'},
{"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},