Repository navigation
Expand file tree
/
Copy pathkloak.c
More file actions
2617 lines (2346 loc) · 80.1 KB
/
Copy pathkloak.c
File metadata and controls
2617 lines (2346 loc) · 80.1 KB
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
/*
* Copyright (c) 2025 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
* See the file COPYING for copying conditions.
*/
/*
* NOTES FOR DEVELOPERS:
* - Use signed arithmetic wherever possible. Any form of integer
* over/underflow is dangerous here, thus kloak has -ftrapv enabled and thus
* signed arithmetic over/underflow will simply crash (and thus restart)
* kloak rather than resulting in memory corruption. Unsigned over/underflow
* however does NOT trap because it is well-defined in C. Thus avoid
* unsigned arithmetic wherever possible.
* - Use an assert to check that a value is within bounds before every cast.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <stdbool.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <math.h>
#include <sys/queue.h>
#include <getopt.h>
#include <assert.h>
#include <limits.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <ctype.h>
#include <linux/input.h>
#include <sys/inotify.h>
#include <wayland-client.h>
/*
* The following files are autogenerated by wayland-scanner from files under
* the protocol/ directory.
*/
#include "wlr-virtual-pointer.h"
#include "virtual-keyboard.h"
/*
* wayland-scanner generates code that is generates warnings with -Wcast-qual
* for xdg-output-protocol.h and wlr-layer-shell.h.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#include "xdg-output-protocol.h"
#include "wlr-layer-shell.h"
#pragma GCC diagnostic pop
#include <libinput.h>
#include <libevdev/libevdev.h>
#include <xkbcommon/xkbcommon.h>
#include "kloak.h"
/********************/
/* global variables */
/********************/
static double cursor_x = 0;
static double cursor_y = 0;
static double prev_cursor_x = 0;
static double prev_cursor_y = 0;
static struct disp_state state = { 0 };
static struct libinput *li = NULL;
static int inotify_fd = 0;
static struct pollfd *ev_fds = NULL;
static int64_t prev_release_time = 0;
static TAILQ_HEAD(tailhead_evq, input_packet) evq_head;
static int32_t max_delay = DEFAULT_MAX_DELAY_MS;
static int32_t startup_delay = DEFAULT_STARTUP_TIMEOUT_MS;
static uint32_t cursor_color = 0x00000000;
static bool should_draw_cursor = true;
static bool enable_natural_scrolling = false;
static uint32_t **esc_key_list = NULL;
static size_t *esc_key_sublist_len = NULL;
static bool *active_esc_key_list = NULL;
static size_t esc_key_list_len = 0;
static LIST_HEAD(listhead_ldi, li_device_info) ldi_head;
static const char *default_esc_key_str
= "KEY_RIGHTSHIFT,KEY_ESC";
static int64_t start_time = 0;
static int randfd = 0;
static bool did_wayland_init = false;
static struct key_name_value key_table[] = {
{"KEY_ESC", KEY_ESC},
{"KEY_1", KEY_1},
{"KEY_2", KEY_2},
{"KEY_3", KEY_3},
{"KEY_4", KEY_4},
{"KEY_5", KEY_5},
{"KEY_6", KEY_6},
{"KEY_7", KEY_7},
{"KEY_8", KEY_8},
{"KEY_9", KEY_9},
{"KEY_0", KEY_0},
{"KEY_MINUS", KEY_MINUS},
{"KEY_EQUAL", KEY_EQUAL},
{"KEY_BACKSPACE", KEY_BACKSPACE},
{"KEY_TAB", KEY_TAB},
{"KEY_Q", KEY_Q},
{"KEY_W", KEY_W},
{"KEY_E", KEY_E},
{"KEY_R", KEY_R},
{"KEY_T", KEY_T},
{"KEY_Y", KEY_Y},
{"KEY_U", KEY_U},
{"KEY_I", KEY_I},
{"KEY_O", KEY_O},
{"KEY_P", KEY_P},
{"KEY_LEFTBRACE", KEY_LEFTBRACE},
{"KEY_RIGHTBRACE", KEY_RIGHTBRACE},
{"KEY_ENTER", KEY_ENTER},
{"KEY_LEFTCTRL", KEY_LEFTCTRL},
{"KEY_A", KEY_A},
{"KEY_S", KEY_S},
{"KEY_D", KEY_D},
{"KEY_F", KEY_F},
{"KEY_G", KEY_G},
{"KEY_H", KEY_H},
{"KEY_J", KEY_J},
{"KEY_K", KEY_K},
{"KEY_L", KEY_L},
{"KEY_SEMICOLON", KEY_SEMICOLON},
{"KEY_APOSTROPHE", KEY_APOSTROPHE},
{"KEY_GRAVE", KEY_GRAVE},
{"KEY_LEFTSHIFT", KEY_LEFTSHIFT},
{"KEY_BACKSLASH", KEY_BACKSLASH},
{"KEY_Z", KEY_Z},
{"KEY_X", KEY_X},
{"KEY_C", KEY_C},
{"KEY_V", KEY_V},
{"KEY_B", KEY_B},
{"KEY_N", KEY_N},
{"KEY_M", KEY_M},
{"KEY_COMMA", KEY_COMMA},
{"KEY_DOT", KEY_DOT},
{"KEY_SLASH", KEY_SLASH},
{"KEY_RIGHTSHIFT", KEY_RIGHTSHIFT},
{"KEY_KPASTERISK", KEY_KPASTERISK},
{"KEY_LEFTALT", KEY_LEFTALT},
{"KEY_SPACE", KEY_SPACE},
{"KEY_CAPSLOCK", KEY_CAPSLOCK},
{"KEY_F1", KEY_F1},
{"KEY_F2", KEY_F2},
{"KEY_F3", KEY_F3},
{"KEY_F4", KEY_F4},
{"KEY_F5", KEY_F5},
{"KEY_F6", KEY_F6},
{"KEY_F7", KEY_F7},
{"KEY_F8", KEY_F8},
{"KEY_F9", KEY_F9},
{"KEY_F10", KEY_F10},
{"KEY_NUMLOCK", KEY_NUMLOCK},
{"KEY_SCROLLLOCK", KEY_SCROLLLOCK},
{"KEY_KP7", KEY_KP7},
{"KEY_KP8", KEY_KP8},
{"KEY_KP9", KEY_KP9},
{"KEY_KPMINUS", KEY_KPMINUS},
{"KEY_KP4", KEY_KP4},
{"KEY_KP5", KEY_KP5},
{"KEY_KP6", KEY_KP6},
{"KEY_KPPLUS", KEY_KPPLUS},
{"KEY_KP1", KEY_KP1},
{"KEY_KP2", KEY_KP2},
{"KEY_KP3", KEY_KP3},
{"KEY_KP0", KEY_KP0},
{"KEY_KPDOT", KEY_KPDOT},
{"KEY_ZENKAKUHANKAKU", KEY_ZENKAKUHANKAKU},
{"KEY_102ND", KEY_102ND},
{"KEY_F11", KEY_F11},
{"KEY_F12", KEY_F12},
{"KEY_RO", KEY_RO},
{"KEY_KATAKANA", KEY_KATAKANA},
{"KEY_HIRAGANA", KEY_HIRAGANA},
{"KEY_HENKAN", KEY_HENKAN},
{"KEY_KATAKANAHIRAGANA", KEY_KATAKANAHIRAGANA},
{"KEY_MUHENKAN", KEY_MUHENKAN},
{"KEY_KPJPCOMMA", KEY_KPJPCOMMA},
{"KEY_KPENTER", KEY_KPENTER},
{"KEY_RIGHTCTRL", KEY_RIGHTCTRL},
{"KEY_KPSLASH", KEY_KPSLASH},
{"KEY_SYSRQ", KEY_SYSRQ},
{"KEY_RIGHTALT", KEY_RIGHTALT},
{"KEY_LINEFEED", KEY_LINEFEED},
{"KEY_HOME", KEY_HOME},
{"KEY_UP", KEY_UP},
{"KEY_PAGEUP", KEY_PAGEUP},
{"KEY_LEFT", KEY_LEFT},
{"KEY_RIGHT", KEY_RIGHT},
{"KEY_END", KEY_END},
{"KEY_DOWN", KEY_DOWN},
{"KEY_PAGEDOWN", KEY_PAGEDOWN},
{"KEY_INSERT", KEY_INSERT},
{"KEY_DELETE", KEY_DELETE},
{"KEY_MACRO", KEY_MACRO},
{"KEY_MUTE", KEY_MUTE},
{"KEY_VOLUMEDOWN", KEY_VOLUMEDOWN},
{"KEY_VOLUMEUP", KEY_VOLUMEUP},
{"KEY_POWER", KEY_POWER},
{"KEY_POWER2", KEY_POWER2},
{"KEY_KPEQUAL", KEY_KPEQUAL},
{"KEY_KPPLUSMINUS", KEY_KPPLUSMINUS},
{"KEY_PAUSE", KEY_PAUSE},
{"KEY_SCALE", KEY_SCALE},
{"KEY_KPCOMMA", KEY_KPCOMMA},
{"KEY_HANGEUL", KEY_HANGEUL},
{"KEY_HANGUEL", KEY_HANGUEL},
{"KEY_HANJA", KEY_HANJA},
{"KEY_YEN", KEY_YEN},
{"KEY_LEFTMETA", KEY_LEFTMETA},
{"KEY_RIGHTMETA", KEY_RIGHTMETA},
{"KEY_COMPOSE", KEY_COMPOSE},
{"KEY_F13", KEY_F13},
{"KEY_F14", KEY_F14},
{"KEY_F15", KEY_F15},
{"KEY_F16", KEY_F16},
{"KEY_F17", KEY_F17},
{"KEY_F18", KEY_F18},
{"KEY_F19", KEY_F19},
{"KEY_F20", KEY_F20},
{"KEY_F21", KEY_F21},
{"KEY_F22", KEY_F22},
{"KEY_F23", KEY_F23},
{"KEY_F24", KEY_F24},
{"KEY_UNKNOWN", KEY_UNKNOWN},
{NULL, 0}
};
/*********************/
/* utility functions */
/*********************/
static void *safe_calloc(size_t nmemb, size_t size) {
void *out_ptr = calloc(nmemb, size);
if (out_ptr == NULL) {
fprintf(stderr,
"FATAL ERROR: Could not allocate memory: %s\n", strerror(errno));
exit(1);
}
return out_ptr;
}
static void *safe_reallocarray(void *ptr, size_t nmemb, size_t size) {
void *out_ptr = reallocarray(ptr, nmemb, size);
if (out_ptr == NULL) {
fprintf(stderr,
"FATAL ERROR: Could not allocate memory: %s\n", strerror(errno));
exit(1);
}
return out_ptr;
}
static char *safe_strdup(const char *s) {
char *out_ptr = strdup(s);
if (out_ptr == NULL) {
fprintf(stderr,
"FATAL ERROR: Could not allocate memory: %s\n", strerror(errno));
exit(1);
}
return out_ptr;
}
static int safe_open(const char *pathname, int flags) {
int out_int = open(pathname, flags);
if (out_int == -1) {
fprintf(stderr,
"FATAL ERROR: Could not open file '%s': %s\n", pathname,
strerror(errno));
exit(1);
}
return out_int;
}
static void safe_close(int fd) {
int rslt = close(fd);
if (rslt == -1) {
fprintf(stderr,
"FATAL ERROR: Could not close a file: %s\n", strerror(errno));
exit(1);
}
}
static DIR *safe_opendir(const char *name, bool allow_enoent) {
DIR *out_ptr = opendir(name);
int dir_fd = 0;
if (out_ptr == NULL) {
if (!allow_enoent || errno != ENOENT) {
fprintf(stderr,
"FATAL ERROR: Could not open directory '%s': %s\n", name,
strerror(errno));
exit(1);
}
}
dir_fd = dirfd(out_ptr);
if (dir_fd == -1) {
fprintf(stderr,
"FATAL ERROR: Could not get file descriptor for directory '%s': %s\n",
name, strerror(errno));
exit(1);
}
if (fcntl(dir_fd, F_SETFD, FD_CLOEXEC) == -1) {
fprintf(stderr,
"FATAL ERROR: Could not set FD_CLOEXEC on file descriptor for directory '%s': %s\n",
name, strerror(errno));
exit(1);
}
return out_ptr;
}
static void safe_closedir(DIR *dirp) {
int rslt = closedir(dirp);
if (rslt == -1) {
fprintf(stderr,
"FATAL ERROR: Could not close a directory: %s\n", strerror(errno));
exit(1);
}
}
/*static off_t safe_lseek(int fd, off_t offset, int whence) {
off_t rslt = lseek(fd, offset, whence);
if (rslt == -1) {
fprintf(stderr,
"FATAL ERROR: Could not seek within a file: %s\n", strerror(errno));
exit(1);
}
return rslt;
}*/
static void read_random(char *buf, ssize_t len) {
assert(len >= 0);
assert(randfd > 0);
assert(buf != NULL);
if (read(randfd, buf, (size_t)(len)) < len) {
fprintf(stderr,
"FATAL ERROR: Could not read %ld byte(s) from /dev/urandom!\n", len);
exit(1);
}
}
static void randname(char *buf, ssize_t len) {
char randchar = 0;
assert(len >= 0);
assert(buf != NULL);
for (ssize_t i = 0; i < len; i++) {
do {
read_random(&randchar, 1);
if (randchar == CHAR_MIN) {
randchar = 0;
} else {
randchar = (char)(abs(randchar));
}
} while (randchar >= (CHAR_MAX - (CHAR_MAX % ALPHABET_LEN)));
randchar %= (ALPHABET_LEN * 2);
if (randchar < ALPHABET_LEN) {
randchar += ASCII_UPPERCASE_START;
} else {
randchar += ASCII_LOWERCASE_START;
}
buf[i] = randchar;
}
}
static int create_shm_file(ssize_t size) {
int32_t retries = 100;
int fd = -1;
/* 18 = length of string '/kloak-XXXXXXXXXX' + NULL terminator */
char name[18];
int snprintf_len = 0;
int32_t ret = 0;
assert(should_draw_cursor);
assert(size >= 0);
/*
* In a perfect world, we'd do the sizeof check here at compile time, but
* unfortunately that isn't possible.
*/
if (sizeof(off_t) == 4) {
assert(size <= INT32_MAX);
} else if (sizeof(off_t) == 8) {
assert(size <= INT64_MAX);
} else {
fprintf(stderr,
"FATAL ERROR: off_t is not 32-bit or 64-bit, cannot continue\n");
exit(1);
}
do {
snprintf_len = snprintf(name, sizeof(name), "%s", "/kloak-XXXXXXXXXX");
assert(snprintf_len > 0 && (size_t)(snprintf_len) == sizeof(name) - 1);
/* 10 = length of 'XXXXXXXXXX', 11 = length + NULL terminator */
randname(name + sizeof(name) - 11, 10);
--retries;
fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
if (fd >= 0) {
shm_unlink(name);
break;
}
} while (retries > 0 && errno == EEXIST);
if (fd == -1) {
fprintf(stderr,
"FATAL ERROR: Could not create shared memory fd: %s\n",
strerror(errno));
exit(1);
}
do {
ret = ftruncate(fd, (off_t)(size));
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
close(fd);
fprintf(stderr,
"FATAL ERROR: Could not allocate shared memory block: %s\n",
strerror(errno));
exit(1);
}
return fd;
}
static int64_t current_time_ms(void) {
struct timespec spec = { 0 };
int64_t result = 0;
clock_gettime(CLOCK_MONOTONIC, &spec);
result = (spec.tv_sec * 1000) + (spec.tv_nsec / 1000000);
assert(result >= 0);
if (start_time == 0) {
start_time = result;
return 0;
}
return result - start_time;
}
static int64_t random_between(int64_t lower, int64_t upper) {
int64_t maxval = 0;
union rand_int64 randval;
assert(lower >= 0);
assert(upper >= 0);
/* default to max if the interval is not valid */
if (lower >= upper) {
return upper;
}
maxval = upper - lower + 1;
assert(maxval > 0);
do {
read_random(randval.raw, sizeof(int64_t));
if (randval.val == INT64_MIN) {
randval.val = 0;
} else {
randval.val = llabs(randval.val);
}
} while (randval.val >= (INT64_MAX - (INT64_MAX % maxval)));
randval.val %= maxval;
return lower + randval.val;
}
static bool check_point_in_area(int32_t x, int32_t y, int32_t rect_x,
int32_t rect_y, int32_t rect_width, int32_t rect_height) {
if (x < 0 || y < 0 || rect_x < 0 || rect_y < 0 || rect_width < 0
|| rect_height < 0) {
return false;
}
if (x >= rect_x && x < rect_x + rect_width
&& y >= rect_y && y < rect_y + rect_height) {
return true;
}
return false;
}
static bool check_screen_touch(struct output_geometry scr1,
struct output_geometry scr2) {
/*
* We check for both touching and overlapping screens. Screens are
* overlapping if any of one screen's corner points falls inside the area of
* the other screen. The criteria to establish touching screens is a bit
* tricky, but a shortcut we can take is to simply grow the size of one of
* the screens by one pixel in every direction (i.e., subtract one from both
* the X and Y position coordinates and then add two to the width and
* height). Then any form of screen touching will be seen as an overlap,
* including touching at the corners.
*/
if (scr1.x < 0 || scr1.y < 0 || scr1.width < 0 || scr1.height < 0
|| scr2.x < 0 || scr2.y < 0 || scr2.width < 0 || scr2.height < 0) {
return false;
}
if (scr1.x > 0) {
scr1.x -= 1;
scr1.width += 2;
} else {
scr1.width += 1;
}
if (scr1.y > 0) {
scr1.y -= 1;
scr1.height += 2;
} else {
scr1.height += 1;
}
if (check_point_in_area(scr1.x, scr1.y, scr2.x, scr2.y, scr2.x + scr2.width,
scr2.y + scr2.height)) {
return true;
}
if (check_point_in_area(scr1.x + scr1.width, scr1.y, scr2.x, scr2.y,
scr2.x + scr2.width, scr2.y + scr2.height)) {
return true;
}
if (check_point_in_area(scr1.x, scr1.y + scr1.height, scr2.x, scr2.y,
scr2.x + scr2.width, scr2.y + scr2.height)) {
return true;
}
if (check_point_in_area(scr1.x + scr1.width, scr1.y + scr1.height, scr2.x,
scr2.y, scr2.x + scr2.width, scr2.y + scr2.height)) {
return true;
}
return false;
}
static void recalc_global_space(struct disp_state *param_state) {
int32_t ul_corner_x = INT32_MAX;
int32_t ul_corner_y = INT32_MAX;
int32_t br_corner_x = 0;
int32_t br_corner_y = 0;
int32_t cur_geom_x = 0;
int32_t cur_geom_y = 0;
int32_t cur_geom_width = 0;
int32_t cur_geom_height = 0;
int32_t temp_br_x = 0;
int32_t temp_br_y = 0;
struct output_geometry *screen_list[MAX_SCREEN_COUNT];
ssize_t screen_list_len = 0;
struct output_geometry *conn_screen_list[MAX_SCREEN_COUNT];
ssize_t conn_screen_list_len = 0;
bool screen_in_conn_list = false;
struct output_geometry *conn_screen = NULL;
struct output_geometry *cur_screen = NULL;
for (ssize_t i = 0; i < MAX_SCREEN_COUNT; i++) {
if (!param_state->output_geometries[i]) {
continue;
}
cur_geom_x = param_state->output_geometries[i]->x;
cur_geom_y = param_state->output_geometries[i]->y;
cur_geom_width = param_state->output_geometries[i]->width;
cur_geom_height = param_state->output_geometries[i]->height;
if (cur_geom_x < 0 || cur_geom_y < 0
|| cur_geom_width < 0 || cur_geom_height < 0) {
continue;
}
screen_list[screen_list_len] = param_state->output_geometries[i];
screen_list_len++;
if (cur_geom_x < ul_corner_x) {
ul_corner_x = cur_geom_x;
}
if (cur_geom_y < ul_corner_y) {
ul_corner_y = cur_geom_y;
}
temp_br_x = cur_geom_x + cur_geom_width;
temp_br_y = cur_geom_y + cur_geom_height;
if (temp_br_x > br_corner_x) {
br_corner_x = temp_br_x;
}
if (temp_br_y > br_corner_y) {
br_corner_y = temp_br_y;
}
}
/* Silently fail if we haven't gotten a valid state yet */
if (screen_list_len <= 0) {
return;
}
if (ul_corner_x > br_corner_x) {
return;
}
if (ul_corner_y > br_corner_y) {
return;
}
conn_screen_list[0] = screen_list[0];
conn_screen_list_len = 1;
/*
* Check for gaps between the screens. We don't support running if gaps are
* present. To do this, we start with an arbitrary screen, then find all
* screens touching it, then find all screens touching those screens, and so
* on, until we can't find any more screens touching whatever "zone" we
* started in. If the number of connected screens is equal to the number of
* attached screens, then all screens are connected, otherwise there is a
* gap somewhere.
*/
for (ssize_t i = 0; i < conn_screen_list_len; i++) {
for (ssize_t j = 0; j < screen_list_len; j++) {
screen_in_conn_list = false;
for (ssize_t k = 0; k < conn_screen_list_len; k++) {
if (screen_list[j] == conn_screen_list[k]) {
screen_in_conn_list = true;
break;
}
}
if (screen_in_conn_list) {
continue;
}
conn_screen = conn_screen_list[i];
cur_screen = screen_list[j];
if (check_screen_touch(*conn_screen, *cur_screen)) {
/* Found a touching screen! */
conn_screen_list[conn_screen_list_len] = cur_screen;
conn_screen_list_len++;
}
}
}
if (conn_screen_list_len != screen_list_len) {
fprintf(stderr,
"FATAL ERROR: Multiple screens are attached and gaps are present between them. kloak cannot operate in this configuration.\n");
exit(1);
}
param_state->global_space_width = br_corner_x;
param_state->global_space_height = br_corner_y;
param_state->pointer_space_x = ul_corner_x;
param_state->pointer_space_y = ul_corner_y;
}
static struct screen_local_coord abs_coord_to_screen_local_coord(int32_t x,
int32_t y) {
struct screen_local_coord out_data = { 0 };
int32_t cur_geom_x = 0;
int32_t cur_geom_y = 0;
int32_t cur_geom_width = 0;
int32_t cur_geom_height = 0;
int32_t i = 0;
if (x < 0 || y < 0) {
return out_data;
}
for (i = 0; i < MAX_SCREEN_COUNT; i++) {
if (!state.output_geometries[i]) {
continue;
}
cur_geom_x = state.output_geometries[i]->x;
cur_geom_y = state.output_geometries[i]->y;
cur_geom_width = state.output_geometries[i]->width;
cur_geom_height = state.output_geometries[i]->height;
if (cur_geom_x < 0 || cur_geom_y < 0 || cur_geom_width < 0
|| cur_geom_height < 0 ) {
continue;
}
if (!(x >= cur_geom_x)) {
continue;
}
if (!(y >= cur_geom_y)) {
continue;
}
if (!(x < cur_geom_x + cur_geom_width)) {
continue;
}
if (!(y < cur_geom_y + cur_geom_height)) {
continue;
}
out_data.output_idx = i;
out_data.x = x - cur_geom_x;
out_data.y = y - cur_geom_y;
out_data.valid = true;
break;
}
/* There is a possibility that out_data will contain all zeros; if this
* happens, it means that no output covered the requested coordinates in the
* compositor's global space. That's a valid thing to tell a caller, so just
* return it anyway. */
return out_data;
}
static struct coord screen_local_coord_to_abs_coord(int32_t x, int32_t y,
int32_t output_idx) {
int32_t cur_geom_x = 0;
int32_t cur_geom_y = 0;
int32_t cur_geom_width = 0;
int32_t cur_geom_height = 0;
struct coord out_val = {
.x = -1,
.y = -1,
};
assert(output_idx >= 0);
if (x < 0 || y < 0) {
return out_val;
}
if (!state.output_geometries[output_idx]) {
return out_val;
}
cur_geom_x = state.output_geometries[output_idx]->x;
cur_geom_y = state.output_geometries[output_idx]->y;
cur_geom_width = state.output_geometries[output_idx]->width;
cur_geom_height = state.output_geometries[output_idx]->height;
if (cur_geom_x < 0 || cur_geom_y < 0 || cur_geom_width < 0
|| cur_geom_height < 0) {
return out_val;
}
out_val.x = cur_geom_x + x;
out_val.y = cur_geom_y + y;
return out_val;
}
struct coord traverse_line(struct coord start, struct coord end,
int32_t pos) {
struct coord out_val = { 0 };
double num = 0;
double denom = 0;
double slope = 0;
double steep = 0;
num = ((double) end.y) - ((double) start.y);
denom = ((double) start.x) - ((double) end.x);
if (pos == 0) return start;
if ((int64_t)(denom) == 0) {
/* vertical line */
out_val.x = start.x;
if (start.y < end.y) {
out_val.y = start.y + pos;
} else {
out_val.y = start.y - pos;
}
return out_val;
}
slope = num / denom;
steep = fabs(slope);
if (steep < 1) {
if (start.x < end.x) {
out_val.x = start.x + pos;
} else {
out_val.x = start.x - pos;
}
if (start.y < end.y) {
out_val.y = start.y + (int32_t)((double) pos * steep);
} else {
out_val.y = start.y - (int32_t)((double) pos * steep);
}
} else {
if (start.y < end.y) {
out_val.y = start.y + pos;
} else {
out_val.y = start.y - pos;
}
if (start.x < end.x) {
out_val.x = start.x + (int32_t)((double) pos * (1 / steep));
} else {
out_val.x = start.x - (int32_t)((double) pos * (1 / steep));
}
}
return out_val;
}
static void draw_block(uint32_t *pixbuf, int32_t offset, int32_t x, int32_t y,
int32_t layer_width, int32_t layer_height, int32_t rad, bool crosshair) {
int32_t start_x = 0;
int32_t start_y = 0;
int32_t end_x = 0;
int32_t end_y = 0;
int32_t work_x = 0;
int32_t work_y = 0;
assert(should_draw_cursor);
start_x = x - rad;
if (start_x < 0) start_x = 0;
start_y = y - rad;
if (start_y < 0) start_y = 0;
end_x = x + rad;
if (end_x >= layer_width) end_x = layer_width - 1;
end_y = y + rad;
if (end_y >= layer_height) end_y = layer_height - 1;
for (work_y = start_y; work_y <= end_y; work_y++) {
for (work_x = start_x; work_x <= end_x; work_x++) {
if (crosshair && work_x == x) {
pixbuf[offset + (work_y * layer_width + work_x)] = cursor_color;
} else if (crosshair && work_y == y) {
pixbuf[offset + (work_y * layer_width + work_x)] = cursor_color;
} else {
pixbuf[offset + (work_y * layer_width + work_x)] = 0x00000000;
}
}
}
}
static int32_t parse_uint31_arg(const char *arg_name, const char *val,
int base) {
char *val_endchar = NULL;
uint64_t val_int = 0;
val_int = strtoul(val, &val_endchar, base);
if (*val_endchar != '\0') {
goto parse_uint31_arg_error;
}
if (val_int > INT32_MAX) {
goto parse_uint31_arg_error;
}
return (int32_t)(val_int);
parse_uint31_arg_error:
fprintf(stderr,
"FATAL ERROR: Invalid value '%s' passed to parameter '%s'!\n", val, arg_name);
exit(1);
return -1;
}
static uint32_t parse_uint32_arg(const char *arg_name, const char *val,
int base) {
char *val_endchar = NULL;
uint64_t val_int = 0;
val_int = strtoul(val, &val_endchar, base);
if (*val_endchar != '\0') {
goto parse_uint31_arg_error;
}
if (val_int > UINT32_MAX) {
goto parse_uint31_arg_error;
}
return (uint32_t)(val_int);
parse_uint31_arg_error:
fprintf(stderr,
"FATAL ERROR: Invalid value '%s' passed to parameter '%s'!\n", val, arg_name);
exit(1);
return 0;
}
static int32_t sleep_ms(int64_t ms) {
struct timespec ts = { 0 };
int nanosleep_ret = 0;
assert(ms >= 0);
ts.tv_sec = (time_t)(ms / 1000);
ts.tv_nsec = (ms % 1000) * 1000000;
do {
nanosleep_ret = nanosleep(&ts, &ts);
} while (nanosleep_ret == -1 && errno == EINTR);
if (nanosleep_ret == -1) {
return -1;
}
return 0;
}
static char *sgenprintf(const char *str, ...) {
char *rslt = NULL;
int rslt_len = 0;
int rslt_writelen = 0;
va_list extra_args;
va_start(extra_args, str);
rslt_len = vsnprintf(NULL, 0, str, extra_args) + 1;
va_end(extra_args);
if (rslt_len == -1) {
fprintf(stderr,
"FATAL ERROR: String processing issue: %s\n", strerror(errno));
exit(1);
}
rslt = safe_calloc(1, (size_t)(rslt_len));
va_start(extra_args, str);
rslt_writelen = vsnprintf(rslt, (size_t)(rslt_len), str, extra_args);
va_end(extra_args);
assert(rslt_writelen == rslt_len - 1);
return rslt;
}
static uint32_t lookup_keycode(const char *name) {
struct key_name_value *p = NULL;
for (p = key_table; p->name != NULL; p++) {
if (strcmp(p->name, name) == 0) {
return p->value;
}
}
return 0;
}
static void attach_input_device(const char *dev_name) {
bool found_device = false;
struct libinput_device *new_device = NULL;
char *device_path = NULL;
struct li_device_info *ldi_node = NULL;
ldi_node = LIST_FIRST(&ldi_head);
while (ldi_node != NULL) {
if (strcmp(ldi_node->device_name, dev_name) == 0) {
found_device = true;
break;
}
ldi_node = LIST_NEXT(ldi_node, entries);
}
if (found_device) {
/*
* This may mean a device was quickly detached and then attached again,
* thus try removing and re-attaching it.
*/
detach_input_device(dev_name);
}
device_path = sgenprintf("/dev/input/%s", dev_name);
new_device = libinput_path_add_device(li, device_path);
free(device_path);
/* Set any "special" input device settings. */
if (enable_natural_scrolling == true
&& libinput_device_config_scroll_has_natural_scroll(new_device) != 0) {
libinput_device_config_scroll_set_natural_scroll_enabled(new_device, 1);
}
if (new_device == NULL) {
return;
}
ldi_node = safe_calloc(1, sizeof(struct li_device_info));
ldi_node->device = new_device;
ldi_node->device_name = safe_strdup(dev_name);
LIST_INSERT_HEAD(&ldi_head, ldi_node, entries);
}
static void detach_input_device(const char *dev_name) {
struct li_device_info *ldi_node = NULL;
bool found_device = false;
ldi_node = LIST_FIRST(&ldi_head);
while (ldi_node != NULL) {
if (strcmp(ldi_node->device_name, dev_name) == 0) {
found_device = true;
break;
}
ldi_node = LIST_NEXT(ldi_node, entries);
}
if (!found_device) {
return;
}
libinput_path_remove_device(ldi_node->device);
free(ldi_node->device_name);
LIST_REMOVE(ldi_node, entries);
free(ldi_node);
}
/********************/
/* wayland handling */
/********************/
static void registry_handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface,