-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.c
More file actions
1139 lines (988 loc) · 35.7 KB
/
main.c
File metadata and controls
1139 lines (988 loc) · 35.7 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
#include <getopt.h>
#include <linux/input-event-codes.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#include "buffer.h"
#include "output-layout.h"
#include "wooz.h"
#include "viewporter-protocol.h"
#include "wlr-screencopy-unstable-v1-protocol.h"
#include "xdg-output-unstable-v1-protocol.h"
#include "xdg-shell-protocol.h"
// Key codes from linux/input-event-codes.h
#define KEY_ESC 1
#define KEY_0 11
#define KEY_MINUS 12
#define KEY_EQUAL 13
#define KEY_Q 16
#define KEY_X 45
#define KEY_KPMINUS 74
#define KEY_KPPLUS 78
#define KEY_KP0 82
#define KEY_UP 103
#define KEY_LEFT 105
#define KEY_RIGHT 106
#define KEY_DOWN 108
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
#define MAX_SCROLL 16
#define DOUBLE_CLICK_TIME_MS 400
#define KEYBOARD_PAN_STEP 50.0
#define KEYBOARD_ZOOM_STEP 10.0
#define KEY_REPEAT_DELAY_MS 500
#define KEY_REPEAT_RATE_MS 50
static void restore_view(struct wooz_window *win) {
win->view_source = win->initial_view_source;
}
static void apply_zoom(struct wooz_window *win, double zoom_change,
double center_x, double center_y) {
double ratio = win->output->ratio;
// Calculate the zoom change in pixels
double scroll = zoom_change;
if (win->view_source.width - scroll * ratio < MAX_SCROLL ||
win->view_source.height - scroll < MAX_SCROLL)
return;
// Calculate center point as ratio of viewport
double dx = center_x / (double)win->output->logical_geometry.width;
double dy = center_y / (double)win->output->logical_geometry.height;
win->view_source.x += round(scroll * ratio * dx);
win->view_source.width -= round(scroll * ratio);
win->view_source.y += round(scroll * dy);
win->view_source.height -= round(scroll);
}
static void render_window(struct wooz_window *win) {
win->view_source.width =
max(min(win->view_source.width, win->output->buffer->width),
MAX_SCROLL * win->output->ratio);
win->view_source.height = max(
min(win->view_source.height, win->output->buffer->height), MAX_SCROLL);
win->view_source.x = max(min(win->view_source.x, win->output->buffer->width -
win->view_source.width),
0);
win->view_source.y = max(min(win->view_source.y, win->output->buffer->height -
win->view_source.height),
0);
wp_viewport_set_source(win->viewport,
wl_fixed_from_double(win->view_source.x),
wl_fixed_from_double(win->view_source.y),
wl_fixed_from_double(win->view_source.width),
wl_fixed_from_double(win->view_source.height));
wl_surface_commit(win->surface);
}
static void handle_key_action(struct wooz_state *state, uint32_t key) {
struct wooz_window *win = state->focused;
if (win == NULL) {
return;
}
switch (key) {
case KEY_EQUAL: // For keyboards where + is shift+=
case KEY_KPPLUS:
// Zoom in at center
apply_zoom(win, KEYBOARD_ZOOM_STEP,
win->output->logical_geometry.width / 2.0,
win->output->logical_geometry.height / 2.0);
render_window(win);
break;
case KEY_MINUS:
case KEY_KPMINUS:
// Zoom out at center
apply_zoom(win, -KEYBOARD_ZOOM_STEP,
win->output->logical_geometry.width / 2.0,
win->output->logical_geometry.height / 2.0);
render_window(win);
break;
case KEY_LEFT:
win->view_source.x -= KEYBOARD_PAN_STEP;
render_window(win);
break;
case KEY_RIGHT:
win->view_source.x += KEYBOARD_PAN_STEP;
render_window(win);
break;
case KEY_UP:
win->view_source.y -= KEYBOARD_PAN_STEP;
render_window(win);
break;
case KEY_DOWN:
win->view_source.y += KEYBOARD_PAN_STEP;
render_window(win);
break;
}
}
static void stop_key_repeat(struct wooz_state *state) {
if (state->repeat_timer_fd >= 0 && state->pressed_key != 0) {
struct itimerspec its = {0};
timerfd_settime(state->repeat_timer_fd, 0, &its, NULL);
state->pressed_key = 0;
}
}
static void start_key_repeat(struct wooz_state *state, uint32_t key) {
state->pressed_key = key;
if (state->repeat_timer_fd < 0) {
state->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
if (state->repeat_timer_fd < 0) {
return;
}
}
struct itimerspec its;
its.it_value.tv_sec = KEY_REPEAT_DELAY_MS / 1000;
its.it_value.tv_nsec = (KEY_REPEAT_DELAY_MS % 1000) * 1000000;
its.it_interval.tv_sec = KEY_REPEAT_RATE_MS / 1000;
its.it_interval.tv_nsec = (KEY_REPEAT_RATE_MS % 1000) * 1000000;
timerfd_settime(state->repeat_timer_fd, 0, &its, NULL);
}
static bool is_repeatable_key(uint32_t key) {
return key == KEY_EQUAL || key == KEY_KPPLUS || key == KEY_MINUS ||
key == KEY_KPMINUS || key == KEY_LEFT || key == KEY_RIGHT ||
key == KEY_UP || key == KEY_DOWN;
}
static void screencopy_frame_handle_buffer(
void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t format,
uint32_t width, uint32_t height, uint32_t stride) {
struct wooz_output *output = data;
output->buffer =
create_buffer(output->state->shm, format, width, height, stride);
if (output->buffer == NULL) {
fprintf(stderr, "failed to create buffer\n");
exit(EXIT_FAILURE);
}
// Handle rotated screens.
if (output->transform & WL_OUTPUT_TRANSFORM_90) {
int32_t tmp = output->buffer->width;
output->buffer->width = output->buffer->height;
output->buffer->height = tmp;
}
zwlr_screencopy_frame_v1_copy(frame, output->buffer->wl_buffer);
}
static void screencopy_frame_handle_flags(
void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t flags) {
struct wooz_output *output = data;
output->screencopy_frame_flags = flags;
}
static void screencopy_frame_handle_ready(
void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t tv_sec_hi,
uint32_t tv_sec_lo, uint32_t tv_nsec) {
struct wooz_output *output = data;
++output->state->n_done;
}
static void
screencopy_frame_handle_failed(void *data,
struct zwlr_screencopy_frame_v1 *frame) {
struct wooz_output *output = data;
fprintf(stderr, "failed to copy output %s\n", output->name);
exit(EXIT_FAILURE);
}
static const struct zwlr_screencopy_frame_v1_listener
screencopy_frame_listener = {
.buffer = screencopy_frame_handle_buffer,
.flags = screencopy_frame_handle_flags,
.ready = screencopy_frame_handle_ready,
.failed = screencopy_frame_handle_failed,
};
static void xdg_output_handle_logical_position(
void *data, struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y) {
struct wooz_output *output = data;
output->logical_geometry.x = x;
output->logical_geometry.y = y;
}
static void xdg_output_handle_logical_size(void *data,
struct zxdg_output_v1 *xdg_output,
int32_t width, int32_t height) {
struct wooz_output *output = data;
output->logical_geometry.width = width;
output->logical_geometry.height = height;
}
static void xdg_output_handle_done(void *data,
struct zxdg_output_v1 *xdg_output) {
struct wooz_output *output = data;
// Guess the output scale from the logical size
int32_t width = output->geometry.width;
output->logical_scale = (double)width / output->logical_geometry.width;
output->ratio = (double)output->logical_geometry.width /
(double)output->logical_geometry.height;
}
static void xdg_output_handle_name(void *data,
struct zxdg_output_v1 *xdg_output,
const char *name) {
struct wooz_output *output = data;
output->name = strdup(name);
}
static void xdg_output_handle_description(void *data,
struct zxdg_output_v1 *xdg_output,
const char *name) {
// No-op
}
static const struct zxdg_output_v1_listener xdg_output_listener = {
.logical_position = xdg_output_handle_logical_position,
.logical_size = xdg_output_handle_logical_size,
.done = xdg_output_handle_done,
.name = xdg_output_handle_name,
.description = xdg_output_handle_description,
};
static void output_handle_geometry(void *data, struct wl_output *wl_output,
int32_t x, int32_t y, int32_t physical_width,
int32_t physical_height, int32_t subpixel,
const char *make, const char *model,
int32_t transform) {
struct wooz_output *output = data;
output->geometry.x = x;
output->geometry.y = y;
output->transform = transform;
}
static void output_handle_mode(void *data, struct wl_output *wl_output,
uint32_t flags, int32_t width, int32_t height,
int32_t refresh) {
struct wooz_output *output = data;
if ((flags & WL_OUTPUT_MODE_CURRENT) != 0) {
output->geometry.width = output->transform ? height : width;
output->geometry.height = output->transform ? width : height;
}
}
static void output_handle_done(void *data, struct wl_output *wl_output) {
// No-op
}
static void output_handle_scale(void *data, struct wl_output *wl_output,
int32_t factor) {
struct wooz_output *output = data;
output->scale = factor;
}
static const struct wl_output_listener output_listener = {
.geometry = output_handle_geometry,
.mode = output_handle_mode,
.done = output_handle_done,
.scale = output_handle_scale,
};
static void xdg_wm_base_ping(void *data, struct xdg_wm_base *shell,
uint32_t serial) {
xdg_wm_base_pong(shell, serial);
}
static const struct xdg_wm_base_listener xdg_wm_base_listener = {
.ping = &xdg_wm_base_ping,
};
static void xdg_surface_configure(void *data, struct xdg_surface *xdg_surface,
uint32_t serial) {
struct wooz_window *win = data;
wl_surface_set_buffer_transform(win->surface, win->output->transform);
win->is_configured = true;
win->is_maximized = win->configure.is_maximized;
win->is_fullscreen = win->configure.is_fullscreen;
win->is_resizing = win->configure.is_resizing;
win->is_tiled_top = win->configure.is_tiled_top;
win->is_tiled_bottom = win->configure.is_tiled_bottom;
win->is_tiled_left = win->configure.is_tiled_left;
win->is_tiled_right = win->configure.is_tiled_right;
win->is_tiled = win->is_tiled_top || win->is_tiled_bottom ||
win->is_tiled_left || win->is_tiled_right;
xdg_surface_ack_configure(win->xdg_surface, serial);
wl_surface_attach(win->surface, win->output->buffer->wl_buffer, 0, 0);
if (win->viewport != NULL && win->configure.width != 0 &&
win->configure.height != 0) {
wp_viewport_set_destination(win->viewport, win->configure.width,
win->configure.height);
}
// Apply initial zoom on first configure
if (!win->initial_zoom_applied && win->state->config.initial_zoom > 0.0) {
double center_x = win->output->logical_geometry.width / 2.0;
double center_y = win->output->logical_geometry.height / 2.0;
double zoom_pixels =
win->output->geometry.height * win->state->config.initial_zoom;
apply_zoom(win, zoom_pixels, center_x, center_y);
render_window(win);
win->initial_zoom_applied = true;
return; // render_window already calls wl_surface_commit
}
wl_surface_commit(win->surface);
}
static const struct xdg_surface_listener xdg_surface_listener = {
.configure = &xdg_surface_configure,
};
static void xdg_toplevel_configure(void *data,
struct xdg_toplevel *xdg_toplevel,
int32_t width, int32_t height,
struct wl_array *states) {
bool is_activated = false;
bool is_fullscreen = false;
bool is_maximized = false;
bool is_resizing = false;
bool is_tiled_top = false;
bool is_tiled_bottom = false;
bool is_tiled_left = false;
bool is_tiled_right = false;
bool is_suspended = false;
enum xdg_toplevel_state *state;
wl_array_for_each(state, states) {
switch (*state) {
case XDG_TOPLEVEL_STATE_MAXIMIZED:
is_maximized = true;
break;
case XDG_TOPLEVEL_STATE_FULLSCREEN:
is_fullscreen = true;
break;
case XDG_TOPLEVEL_STATE_RESIZING:
is_resizing = true;
break;
case XDG_TOPLEVEL_STATE_ACTIVATED:
is_activated = true;
break;
case XDG_TOPLEVEL_STATE_TILED_LEFT:
is_tiled_left = true;
break;
case XDG_TOPLEVEL_STATE_TILED_RIGHT:
is_tiled_right = true;
break;
case XDG_TOPLEVEL_STATE_TILED_TOP:
is_tiled_top = true;
break;
case XDG_TOPLEVEL_STATE_TILED_BOTTOM:
is_tiled_bottom = true;
break;
case XDG_TOPLEVEL_STATE_SUSPENDED:
is_suspended = true;
break;
default:
break;
}
}
(void)is_suspended;
/*
* Changes done here are ignored until the configure event has
* been ack:ed in xdg_surface_configure().
*
* So, just store the config data and apply it later, in
* xdg_surface_configure() after we've ack:ed the event.
*/
struct wooz_window *win = data;
win->configure.is_activated = is_activated;
win->configure.is_fullscreen = is_fullscreen;
win->configure.is_maximized = is_maximized;
win->configure.is_resizing = is_resizing;
win->configure.is_tiled_top = is_tiled_top;
win->configure.is_tiled_bottom = is_tiled_bottom;
win->configure.is_tiled_left = is_tiled_left;
win->configure.is_tiled_right = is_tiled_right;
win->configure.width = width;
win->configure.height = height;
}
static void xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel) {
struct wooz_window *win = data;
win->state->n_done = 0;
}
static void xdg_toplevel_configure_bounds(void *data,
struct xdg_toplevel *xdg_toplevel,
int32_t width, int32_t height) {
/* TODO: ensure we don't pick a bigger size */
}
static void xdg_toplevel_wm_capabilities(void *data,
struct xdg_toplevel *xdg_toplevel,
struct wl_array *caps) {
struct wooz_window *win = data;
win->wm_capabilities.maximize = false;
win->wm_capabilities.minimize = false;
win->wm_capabilities.window_menu = false;
win->wm_capabilities.fullscreen = false;
enum xdg_toplevel_wm_capabilities *cap;
wl_array_for_each(cap, caps) {
switch (*cap) {
case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE:
win->wm_capabilities.maximize = true;
break;
case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE:
win->wm_capabilities.minimize = true;
break;
case XDG_TOPLEVEL_WM_CAPABILITIES_WINDOW_MENU:
win->wm_capabilities.window_menu = true;
break;
case XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN:
win->wm_capabilities.fullscreen = true;
break;
}
}
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = &xdg_toplevel_configure,
.close = &xdg_toplevel_close,
.configure_bounds = &xdg_toplevel_configure_bounds,
.wm_capabilities = xdg_toplevel_wm_capabilities,
};
static void pointer_handle_enter(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t sx, wl_fixed_t sy) {
struct wooz_state *state = data;
struct wooz_window *window;
wl_list_for_each(window, &state->windows, link) {
if (window->surface == surface) {
window->is_focused = true;
state->focused = window;
window->pointer_x = wl_fixed_to_double(sx);
window->pointer_y = wl_fixed_to_double(sy);
} else {
window->is_focused = false;
}
}
}
static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface) {
struct wooz_state *state = data;
struct wooz_window *window;
wl_list_for_each(window, &state->windows, link) {
if (window->surface == surface) {
window->is_focused = false;
break;
}
}
}
static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {
struct wooz_state *state = data;
struct wooz_window *win = state->focused;
double x = wl_fixed_to_double(sx);
double y = wl_fixed_to_double(sy);
if (win->pointer_pressed) {
double scale = win->view_source.width / win->output->logical_geometry.width;
double dx = (x - win->pointer_x) * scale;
double dy = (y - win->pointer_y) * scale;
win->view_source.x -= dx;
win->view_source.y -= dy;
render_window(win);
} else if (state->config.mouse_track) {
// Mouse tracking: center viewport on mouse position
double viewport_center_x = win->pointer_x;
double viewport_center_y = win->pointer_y;
double new_center_x = x;
double new_center_y = y;
double dx = (new_center_x - viewport_center_x);
double dy = (new_center_y - viewport_center_y);
win->view_source.x += dx;
win->view_source.y += dy;
render_window(win);
}
win->pointer_x = x;
win->pointer_y = y;
}
static void pointer_handle_button(void *data, struct wl_pointer *pointer,
uint32_t serial, uint32_t time,
uint32_t button, uint32_t button_state) {
struct wooz_state *state = data;
struct wooz_window *win = state->focused;
if (button == BTN_LEFT) {
if (button_state == WL_POINTER_BUTTON_STATE_PRESSED) {
// Check for double-click
if (win->last_click_button == BTN_LEFT &&
(time - win->last_click_time) < DOUBLE_CLICK_TIME_MS) {
// Double-click detected - restore view
restore_view(win);
render_window(win);
win->last_click_time = 0;
} else {
win->last_click_time = time;
win->last_click_button = button;
}
win->pointer_pressed = true;
} else {
win->pointer_pressed = false;
}
} else if (button == BTN_RIGHT &&
button_state == WL_POINTER_BUTTON_STATE_RELEASED) {
win->state->n_done = 0;
}
}
static void pointer_handle_axis(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis,
wl_fixed_t value) {
struct wooz_state *state = data;
struct wooz_window *win = state->focused;
double scale = win->view_source.width / win->output->geometry.width;
// x10 for faster zoom.
double scroll = wl_fixed_to_double(value) * scale * 10;
// Invert scroll if configured
if (state->config.invert_scroll) {
scroll = -scroll;
}
if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
apply_zoom(win, scroll, win->pointer_x, win->pointer_y);
render_window(win);
}
}
static const struct wl_pointer_listener pointer_listener = {
.enter = pointer_handle_enter,
.leave = pointer_handle_leave,
.motion = pointer_handle_motion,
.button = pointer_handle_button,
.axis = pointer_handle_axis,
};
static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
uint32_t format, int32_t fd, uint32_t size) {
close(fd);
}
static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface,
struct wl_array *keys) {
// No-op
}
static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface) {
// No-op
}
static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t time, uint32_t key,
uint32_t key_state) {
struct wooz_state *state = data;
struct wooz_window *win = state->focused;
if (win == NULL) {
return;
}
if (key_state == WL_KEYBOARD_KEY_STATE_RELEASED) {
// Stop key repeat when any key is released
if (state->pressed_key == key) {
stop_key_repeat(state);
}
return;
}
// Check for custom close key
if (state->config.close_key != 0 && key == state->config.close_key) {
state->n_done = 0;
return;
}
switch (key) {
case KEY_ESC:
// Default close behavior (if no custom close key)
if (state->config.close_key == 0) {
state->n_done = 0;
}
break;
case KEY_0:
case KEY_KP0:
// Restore/unzoom
restore_view(win);
render_window(win);
break;
case KEY_EQUAL: // For keyboards where + is shift+=
case KEY_KPPLUS:
case KEY_MINUS:
case KEY_KPMINUS:
case KEY_LEFT:
case KEY_RIGHT:
case KEY_UP:
case KEY_DOWN:
// Handle the key action immediately
handle_key_action(state, key);
// Start key repeat for these keys
if (is_repeatable_key(key)) {
start_key_repeat(state, key);
}
break;
}
}
static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked, uint32_t group) {
// No-op
}
static void keyboard_handle_repeat_info(void *data,
struct wl_keyboard *keyboard,
int32_t rate, int32_t delay) {
// No-op
}
static const struct wl_keyboard_listener keyboard_listener = {
.keymap = keyboard_handle_keymap,
.enter = keyboard_handle_enter,
.leave = keyboard_handle_leave,
.key = keyboard_handle_key,
.modifiers = keyboard_handle_modifiers,
.repeat_info = keyboard_handle_repeat_info,
};
static void seat_handle_capabilities(void *data, struct wl_seat *seat,
uint32_t capabilities) {
struct wooz_state *state = data;
if ((capabilities & WL_SEAT_CAPABILITY_POINTER) != 0) {
state->pointer = wl_seat_get_pointer(seat);
wl_pointer_add_listener(state->pointer, &pointer_listener, state);
} else {
if (state->pointer != NULL) {
wl_pointer_destroy(state->pointer);
state->pointer = NULL;
}
}
if ((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) != 0) {
state->keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(state->keyboard, &keyboard_listener, state);
} else {
if (state->keyboard != NULL) {
wl_keyboard_destroy(state->keyboard);
state->keyboard = NULL;
}
}
}
static const struct wl_seat_listener seat_listener = {
.capabilities = seat_handle_capabilities,
};
static void handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface,
uint32_t version) {
struct wooz_state *state = data;
if (strcmp(interface, wl_compositor_interface.name) == 0) {
state->compositor =
wl_registry_bind(registry, name, &wl_compositor_interface, 5);
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
state->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
} else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
uint32_t bind_version = (version > 2) ? 2 : version;
state->xdg_output_manager = wl_registry_bind(
registry, name, &zxdg_output_manager_v1_interface, bind_version);
} else if (strcmp(interface, wl_output_interface.name) == 0) {
struct wooz_output *output = calloc(1, sizeof(struct wooz_output));
output->state = state;
output->scale = 1;
output->wl_output =
wl_registry_bind(registry, name, &wl_output_interface, 3);
wl_output_add_listener(output->wl_output, &output_listener, output);
wl_list_insert(&state->outputs, &output->link);
} 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);
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
state->shell = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
xdg_wm_base_add_listener(state->shell, &xdg_wm_base_listener, state);
} else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
state->viewporter =
wl_registry_bind(registry, name, &wp_viewporter_interface, 1);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
state->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
wl_seat_add_listener(state->seat, &seat_listener, state);
}
}
static void handle_global_remove(void *data, struct wl_registry *registry,
uint32_t name) {
// who cares
}
static const struct wl_registry_listener registry_listener = {
.global = handle_global,
.global_remove = handle_global_remove,
};
static const char usage[] =
"Usage: wooz [options...]\n"
"\n"
"Options:\n"
" -h, --help Show help message and quit\n"
" --map-close KEY Set key to close (e.g., 'Esc', 'q')\n"
" --mouse-track Enable mouse tracking (follow mouse without "
"clicking)\n"
" --output NAME Run on specific output (e.g., 'DP-1')\n"
" --zoom-in PERCENT Set initial zoom percentage (e.g., '10%', "
"'50%')\n"
" --invert-scroll Invert scroll direction (scroll up zooms in)\n"
"\n"
"Controls:\n"
" Mouse scroll Zoom in/out at mouse position\n"
" Left click + drag Pan the view\n"
" Right click Exit\n"
" Double click Restore/unzoom\n"
" +/- Zoom in/out at center\n"
" Arrow keys Pan the view\n"
" 0 Restore/unzoom\n"
" Esc Exit (default)\n";
static bool should_include_output(struct wooz_output *output,
const char *filter) {
// If no filter is set, include all outputs
if (filter == NULL) {
return true;
}
// If output doesn't have a name yet, we can't filter it
if (output->name == NULL) {
return false;
}
// Check if the output name matches the filter
return strcmp(output->name, filter) == 0;
}
static uint32_t parse_key_name(const char *name) {
if (strcmp(name, "Esc") == 0 || strcmp(name, "Escape") == 0) {
return KEY_ESC;
} else if (strcmp(name, "q") == 0 || strcmp(name, "Q") == 0) {
return KEY_Q;
} else if (strcmp(name, "x") == 0 || strcmp(name, "X") == 0) {
return KEY_X;
}
return 0; // Invalid key
}
int main(int argc, char *argv[]) {
struct wooz_config config = {0};
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"map-close", required_argument, 0, 'c'},
{"mouse-track", no_argument, 0, 'm'},
{"output", required_argument, 0, 'o'},
{"zoom-in", required_argument, 0, 'z'},
{"invert-scroll", no_argument, 0, 'i'},
{0, 0, 0, 0}};
int opt;
int option_index = 0;
while ((opt = getopt_long(argc, argv, "h", long_options, &option_index)) !=
-1) {
switch (opt) {
case 'h':
printf("%s", usage);
return EXIT_SUCCESS;
case 'c': {
config.close_key = parse_key_name(optarg);
if (config.close_key == 0) {
fprintf(stderr, "Invalid key name: %s (supported: Esc, q, x)\n",
optarg);
return EXIT_FAILURE;
}
break;
}
case 'm':
config.mouse_track = true;
break;
case 'o':
config.output_filter = strdup(optarg);
break;
case 'z': {
char *endptr;
double zoom = strtod(optarg, &endptr);
if (*endptr == '%') {
config.initial_zoom = zoom / 100.0;
} else {
config.initial_zoom = zoom;
}
if (config.initial_zoom < 0.0 || config.initial_zoom >= 1.0) {
fprintf(stderr, "Invalid zoom percentage: %s (must be 0-99%%)\n",
optarg);
return EXIT_FAILURE;
}
break;
}
case 'i':
config.invert_scroll = true;
break;
default:
fprintf(stderr, "%s", usage);
return EXIT_FAILURE;
}
}
struct wooz_state state = {0};
state.config = config;
state.repeat_timer_fd = -1;
wl_list_init(&state.outputs);
wl_list_init(&state.windows);
state.display = wl_display_connect(NULL);
if (state.display == NULL) {
fprintf(stderr, "failed to create display\n");
return EXIT_FAILURE;
}
state.registry = wl_display_get_registry(state.display);
wl_registry_add_listener(state.registry, ®istry_listener, &state);
if (wl_display_roundtrip(state.display) < 0) {
fprintf(stderr, "wl_display_roundtrip() failed\n");
return EXIT_FAILURE;
}
if (state.compositor == NULL) {
fprintf(stderr, "wl_compositor is missing\n");
return EXIT_FAILURE;
}
if (state.shell == NULL) {
fprintf(stderr, "no XDG shell interface\n");
return EXIT_FAILURE;
}
if (state.shm == NULL) {
fprintf(stderr, "compositor doesn't support wl_shm\n");
return EXIT_FAILURE;
}
if (state.screencopy_manager == NULL) {
fprintf(stderr, "compositor doesn't support wlr-screencopy-unstable-v1\n");
return EXIT_FAILURE;
}
if (state.viewporter == NULL) {
fprintf(stderr, "compositor doesn't support viewporter\n");
return EXIT_FAILURE;
}
if (state.seat == NULL) {
fprintf(stderr, "compositor doesn't support seat\n");
return EXIT_FAILURE;
}
if (wl_list_empty(&state.outputs)) {
fprintf(stderr, "no wl_output\n");
return EXIT_FAILURE;
}
if (state.xdg_output_manager != NULL) {
struct wooz_output *output;
wl_list_for_each(output, &state.outputs, link) {
output->xdg_output = zxdg_output_manager_v1_get_xdg_output(
state.xdg_output_manager, output->wl_output);
zxdg_output_v1_add_listener(output->xdg_output, &xdg_output_listener,
output);
}
if (wl_display_roundtrip(state.display) < 0) {
fprintf(stderr, "wl_display_roundtrip() failed\n");
return EXIT_FAILURE;
}
} else {
fprintf(stderr, "warning: zxdg_output_manager_v1 isn't available, "
"guessing the output layout\n");
struct wooz_output *output;
wl_list_for_each(output, &state.outputs, link) {
guess_output_logical_geometry(output);
}
}
size_t n_pending = 0;
struct wooz_output *output;
wl_list_for_each(output, &state.outputs, link) {
// Skip outputs that don't match the filter
if (!should_include_output(output, state.config.output_filter)) {
continue;
}
output->screencopy_frame = zwlr_screencopy_manager_v1_capture_output(
state.screencopy_manager, false, output->wl_output);
zwlr_screencopy_frame_v1_add_listener(output->screencopy_frame,
&screencopy_frame_listener, output);
++n_pending;
}
if (n_pending == 0) {
if (state.config.output_filter != NULL) {
fprintf(stderr, "no output found matching '%s'\n",
state.config.output_filter);
} else {
fprintf(stderr, "no outputs found\n");
}
return EXIT_FAILURE;
}
bool done = false;
while (!done && wl_display_dispatch(state.display) != -1) {
done = (state.n_done == n_pending);
}
if (!done) {
fprintf(stderr, "failed to screenshoot all outputs\n");
return EXIT_FAILURE;
}
wl_list_for_each(output, &state.outputs, link) {
// Skip outputs that don't match the filter
if (!should_include_output(output, state.config.output_filter)) {
continue;