forked from djpohly/dwl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
dwl.c
3343 lines (2971 loc) · 103 KB
/
dwl.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
/*
* See LICENSE file for copyright and license details.
*/
#include <getopt.h>
#include <libinput.h>
#include <limits.h>
#include <linux/input-event-codes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#include <wayland-server-core.h>
#include <wlr/backend.h>
#include <wlr/backend/libinput.h>
#include <wlr/render/allocator.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_data_control_v1.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_export_dmabuf_v1.h>
#include <wlr/types/wlr_gamma_control_v1.h>
#include <wlr/types/wlr_idle.h>
#include <wlr/types/wlr_idle_inhibit_v1.h>
#include <wlr/types/wlr_idle_notify_v1.h>
#include <wlr/types/wlr_input_device.h>
#include <wlr/types/wlr_input_inhibitor.h>
#include <wlr/types/wlr_keyboard.h>
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_output_management_v1.h>
#include <wlr/types/wlr_pointer.h>
#include <wlr/types/wlr_presentation_time.h>
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/types/wlr_primary_selection_v1.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_screencopy_v1.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_server_decoration.h>
#include <wlr/types/wlr_session_lock_v1.h>
#include <wlr/types/wlr_single_pixel_buffer_v1.h>
#include <wlr/types/wlr_subcompositor.h>
#include <wlr/types/wlr_viewporter.h>
#include <wlr/types/wlr_virtual_keyboard_v1.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/types/wlr_xdg_activation_v1.h>
#include <wlr/types/wlr_xdg_decoration_v1.h>
#include <wlr/types/wlr_xdg_output_v1.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/log.h>
#include <xkbcommon/xkbcommon.h>
#include <libguile.h>
#include "dscm-unstable-v1-protocol.h"
#ifdef XWAYLAND
#include <wlr/xwayland.h>
#include <X11/Xlib.h>
#include <xcb/xcb_icccm.h>
#endif
#include "util.h"
/* macros */
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define CLEANMASK(mask) (mask & ~WLR_MODIFIER_CAPS)
#define VISIBLEON(C, M) ((M) && (C)->mon == (M) && ((C)->tags & (M)->tagset[(M)->seltags]))
#define LENGTH(X) (sizeof X / sizeof X[0])
#define ROUND(X) ((int)((X)+0.5))
#define END(A) ((A) + LENGTH(A))
#define LISTEN(E, L, H) wl_signal_add((E), ((L)->notify = (H), (L)))
#define IDLE_NOTIFY_ACTIVITY wlr_idle_notify_activity(idle, seat), wlr_idle_notifier_v1_notify_activity(idle_notifier, seat)
#define MAX_KEYCHORD_LAYERS 5
/* enums */
enum { CurNormal, CurPressed, CurMove, CurResize }; /* cursor */
enum { XDGShell, LayerShell, X11Managed, X11Unmanaged }; /* client types */
enum { LyrBg, LyrBottom, LyrTop, LyrOverlay, LyrTile, LyrFloat, LyrFS, LyrDragIcon, LyrBlock, NUM_LAYERS }; /* scene layers */
#ifdef XWAYLAND
enum { NetWMWindowTypeDialog, NetWMWindowTypeSplash, NetWMWindowTypeToolbar,
NetWMWindowTypeUtility, NetLast }; /* EWMH atoms */
#endif
typedef union {
int i;
unsigned int ui;
float f;
void *v;
} Arg;
typedef struct Monitor Monitor;
typedef struct {
/* Must keep these three elements in this order */
unsigned int type; /* XDGShell or X11* */
struct wlr_box geom; /* layout-relative, includes border */
Monitor *mon;
struct wlr_scene_tree *scene;
struct wlr_scene_rect *border[4]; /* top, bottom, left, right */
struct wlr_scene_tree *scene_surface;
struct wl_list link;
struct wl_list flink;
union {
struct wlr_xdg_surface *xdg;
struct wlr_xwayland_surface *xwayland;
} surface;
struct wl_listener commit;
struct wl_listener map;
struct wl_listener maximize;
struct wl_listener unmap;
struct wl_listener destroy;
struct wl_listener set_title;
struct wl_listener fullscreen;
struct wlr_box prev; /* layout-relative, includes border */
#ifdef XWAYLAND
struct wl_listener activate;
struct wl_listener configure;
struct wl_listener set_hints;
#endif
unsigned int bw;
unsigned int tags;
int isfloating, isurgent, isfullscreen;
uint32_t resize; /* configure serial of a pending resize */
double alpha;
double prevalpha;
int prevx;
int prevy;
int prevwidth;
int prevheight;
} Client;
typedef struct {
unsigned int mod;
unsigned int key;
} Key;
typedef struct {
Key keys[MAX_KEYCHORD_LAYERS];
unsigned int n;
unsigned int isbutton;
scm_t_bits *action;
struct wl_list link;
} Binding;
typedef struct {
struct wl_list link;
struct wlr_keyboard *wlr_keyboard;
struct wl_listener modifiers;
struct wl_listener key;
struct wl_listener destroy;
} Keyboard;
typedef struct {
struct wl_list link;
struct wlr_input_device *dev;
struct wl_listener destroy;
} Pointer;
typedef struct {
/* Must keep these three elements in this order */
unsigned int type; /* LayerShell */
struct wlr_box geom;
Monitor *mon;
struct wlr_scene_tree *scene;
struct wlr_scene_tree *popups;
struct wlr_scene_layer_surface_v1 *scene_layer;
struct wl_list link;
struct wlr_layer_surface_v1 *layer_surface;
int mapped;
struct wl_listener destroy;
struct wl_listener map;
struct wl_listener unmap;
struct wl_listener surface_commit;
} LayerSurface;
typedef struct {
char *id;
char *symbol;
scm_t_bits *arrange;
struct wl_list link;
} Layout;
typedef struct {
struct wl_list link;
struct wl_resource *resource;
} DscmClient;
typedef struct {
struct wl_list link;
struct wl_resource *resource;
struct Monitor *monitor;
} DscmMonitor;
struct Monitor {
struct wl_list link;
struct wlr_output *wlr_output;
struct wlr_scene_output *scene_output;
struct wlr_scene_rect *fullscreen_bg; /* See createmon() for info */
struct wl_listener frame;
struct wl_listener destroy;
struct wl_listener destroy_lock_surface;
struct wlr_session_lock_surface_v1 *lock_surface;
struct wlr_box m; /* monitor area, layout-relative */
struct wlr_box w; /* window area, layout-relative */
struct wl_list layers[4]; /* LayerSurface::link */
struct wl_list dscm;
const Layout *lt[2];
unsigned int seltags;
unsigned int sellt;
unsigned int tagset[2];
unsigned int prevtagset;
double mfact;
int gappih; /* horizontal gap between windows */
int gappiv; /* vertical gap between windows */
int gappoh; /* horizontal outer gaps */
int gappov; /* vertical outer gaps */
int nmaster;
};
typedef struct {
char *name;
float mfact;
int nmaster;
float scale;
const Layout *lt;
enum wl_output_transform rr;
struct wl_list link;
} MonitorRule;
typedef struct {
char *id;
char *title;
unsigned int tags;
int isfloating;
double alpha;
int monitor;
struct wl_list link;
} Rule;
typedef struct {
struct wlr_scene_tree *scene;
struct wlr_session_lock_v1 *lock;
struct wl_listener new_surface;
struct wl_listener unlock;
struct wl_listener destroy;
} SessionLock;
/* dscm protocol */
static void dscm_sendevents(void);
static void dscm_closemon(struct wl_client *client, struct wl_resource *resource);
static void dscm_destroymon(struct wl_resource *resource);
static void dscm_printstatusmon(Monitor *m, const DscmMonitor *mon);
static void dscm_printstatus(Monitor *m);
static void dscm_settags(struct wl_client *client, struct wl_resource *resource,
uint32_t t, uint32_t toggletagset);
static void dscm_setlayout(struct wl_client *client, struct wl_resource *resource,
uint32_t layout);
static void dscm_setclient(struct wl_client *client, struct wl_resource *resource,
uint32_t and, uint32_t xor);
static void dscm_release(struct wl_client *client, struct wl_resource *resource);
static void dscm_getmon(struct wl_client *client, struct wl_resource *resource,
uint32_t id, struct wl_resource *output);
static void dscm_eval(struct wl_client *client, struct wl_resource *resource,
const char *exp);
static void dscm_eval_callback(struct wl_resource *resource, char *result,
uint32_t status);
static void dscm_destroy(struct wl_resource *resource);
static void dscm_bind(struct wl_client *client, void *data, uint32_t version,
uint32_t id);
/* function declarations */
static void applybounds(Client *c, struct wlr_box *bbox);
static void applyexclusive(struct wlr_box *usable_area, uint32_t anchor,
int32_t exclusive, int32_t margin_top,
int32_t margin_right, int32_t margin_bottom,
int32_t margin_left);
static void applylibinputrules(struct wlr_input_device *dev);
static void applymonrules(Monitor *m);
static void applyrules(Client *c);
static void arrange(Monitor *m);
static void arrangelayer(Monitor *m, struct wl_list *list,
struct wlr_box *usable_area, int exclusive);
static void arrangelayers(Monitor *m);
static void axisnotify(struct wl_listener *listener, void *data);
static void buttonpress(struct wl_listener *listener, void *data);
static void changealpha(const Arg *arg);
static void chvt(const Arg *arg);
static void checkidleinhibitor(struct wlr_surface *exclude);
static void cleanup(void);
static void cleanupkeyboard(struct wl_listener *listener, void *data);
static void cleanupmon(struct wl_listener *listener, void *data);
static void closemon(Monitor *m);
static void commitlayersurfacenotify(struct wl_listener *listener, void *data);
static void commitnotify(struct wl_listener *listener, void *data);
static void createdecoration(struct wl_listener *listener, void *data);
static void createidleinhibitor(struct wl_listener *listener, void *data);
static void createkeyboard(struct wlr_keyboard *keyboard);
static void createlayersurface(struct wl_listener *listener, void *data);
static void createlocksurface(struct wl_listener *listener, void *data);
static void createmon(struct wl_listener *listener, void *data);
static void createnotify(struct wl_listener *listener, void *data);
static void createpointer(struct wlr_pointer *pointer);
static void cursorframe(struct wl_listener *listener, void *data);
static void cyclelayout(const Arg *arg);
static void destroydragicon(struct wl_listener *listener, void *data);
static void destroyidleinhibitor(struct wl_listener *listener, void *data);
static void destroylayersurfacenotify(struct wl_listener *listener, void *data);
static void destroylock(SessionLock *lock, int unlocked);
static void destroylocksurface(struct wl_listener *listener, void *data);
static void destroynotify(struct wl_listener *listener, void *data);
static void destroypointer(struct wl_listener *listener, void *data);
static void destroysessionlock(struct wl_listener *listener, void *data);
static void destroysessionmgr(struct wl_listener *listener, void *data);
static Monitor *dirtomon(enum wlr_direction dir);
static void dragicondestroy(struct wl_listener *listener, void *data);
static void focusclient(Client *c, int lift);
static void focusmon(const Arg *arg);
static void focusstack(const Arg *arg);
static Client *focustop(Monitor *m);
static void fullscreennotify(struct wl_listener *listener, void *data);
static void incnmaster(const Arg *arg);
static void inputdevice(struct wl_listener *listener, void *data);
static int keybinding(uint32_t mods, xkb_keysym_t keysym);
static void keypress(struct wl_listener *listener, void *data);
static void keypressmod(struct wl_listener *listener, void *data);
static void killclient(const Arg *arg);
static void locksession(struct wl_listener *listener, void *data);
static void maplayersurfacenotify(struct wl_listener *listener, void *data);
static void mapnotify(struct wl_listener *listener, void *data);
static void maximizenotify(struct wl_listener *listener, void *data);
static void monocle(Monitor *m);
static void motionabsolute(struct wl_listener *listener, void *data);
static void motionnotify(uint32_t time);
static void motionrelative(struct wl_listener *listener, void *data);
static void moveresize(const Arg *arg);
static void outputmgrapply(struct wl_listener *listener, void *data);
static void outputmgrapplyortest(struct wlr_output_configuration_v1 *config,
int test);
static void outputmgrtest(struct wl_listener *listener, void *data);
static void pointerfocus(Client *c, struct wlr_surface *surface,
double sx, double sy, uint32_t time);
static void printstatus(void);
static void quit(const Arg *arg);
static void quitsignal(int signo);
static void rendermon(struct wl_listener *listener, void *data);
static void requeststartdrag(struct wl_listener *listener, void *data);
static void resize(Client *c, struct wlr_box geo, int interact, int draw_borders);
static void run(char *startup_cmd);
static void setcursor(struct wl_listener *listener, void *data);
static void setfloating(Client *c, int floating);
static void setfullscreen(Client *c, int fullscreen);
static void setlayout(const Arg *arg);
static void setmfact(const Arg *arg);
static void setmon(Client *c, Monitor *m, unsigned int newtags);
static void setup();
static void sigchld(int unused);
static void setpsel(struct wl_listener *listener, void *data);
static void setsel(struct wl_listener *listener, void *data);
static void spawn(const Arg *arg);
static void startdrag(struct wl_listener *listener, void *data);
static void tag(const Arg *arg);
static void tagmon(const Arg *arg);
static void tile(Monitor *m);
static void togglefloating(const Arg *arg);
static void togglefullscreen(const Arg *arg);
static void toggletag(const Arg *arg);
static void toggleview(const Arg *arg);
static void unlocksession(struct wl_listener *listener, void *data);
static void unmaplayersurfacenotify(struct wl_listener *listener, void *data);
static void unmapnotify(struct wl_listener *listener, void *data);
static void updatemons(struct wl_listener *listener, void *data);
static void updatetitle(struct wl_listener *listener, void *data);
static void urgent(struct wl_listener *listener, void *data);
static void view(const Arg *arg);
static void viewprev(const Arg *arg);
static void virtualkeyboard(struct wl_listener *listener, void *data);
static void writepid(const char *runtimedir);
static Monitor *xytomon(double x, double y);
static struct wlr_scene_node *xytonode(double x, double y, struct wlr_surface **psurface,
Client **pc, LayerSurface **pl, double *nx, double *ny);
static void zoom(const Arg *arg);
static void setgaps(int oh, int ov, int ih, int iv);
static void incrgaps(const Arg *arg);
static void incrigaps(const Arg *arg);
static void incrogaps(const Arg *arg);
static void togglegaps(const Arg *arg);
static void defaultgaps(const Arg *arg);
/* variables */
static const char broken[] = "broken";
static const char *cursor_image = "left_ptr";
static pid_t child_pid = -1;
static int locked;
static void *exclusive_focus;
static char *config_file = NULL;
static struct wl_display *dpy;
static struct wlr_backend *backend;
static struct wlr_scene *scene;
static struct wlr_scene_tree *layers[NUM_LAYERS];
static struct wlr_renderer *drw;
static struct wlr_allocator *alloc;
static struct wlr_compositor *compositor;
static struct wlr_scene_rect *root;
static struct wlr_xdg_shell *xdg_shell;
static struct wlr_xdg_activation_v1 *activation;
static struct wlr_xdg_decoration_manager_v1 *xdg_decoration_mgr;
static struct wl_list clients; /* tiling order */
static struct wl_list fstack; /* focus order */
static struct wl_list pointers;
static struct wl_list dscm_clients;
static struct wlr_idle *idle;
static struct wlr_idle_notifier_v1 *idle_notifier;
static struct wlr_idle_inhibit_manager_v1 *idle_inhibit_mgr;
static struct wlr_input_inhibit_manager *input_inhibit_mgr;
static struct wlr_layer_shell_v1 *layer_shell;
static struct wlr_output_manager_v1 *output_mgr;
static struct wlr_presentation *presentation;
static struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard_mgr;
static struct wlr_cursor *cursor;
static struct wlr_xcursor_manager *cursor_mgr;
static struct wlr_session_lock_manager_v1 *session_lock_mgr;
static struct wlr_scene_rect *locked_bg;
static struct wlr_session_lock_v1 *cur_lock;
static struct wlr_seat *seat;
static struct wl_list keyboards;
static unsigned int cursor_mode;
static Client *grabc;
static int grabcx, grabcy; /* client-relative */
static struct wlr_output_layout *output_layout;
static struct wlr_box sgeom;
static struct wl_list mons;
static Monitor *selmon;
static int enablegaps = 1; /* enables gaps, used by togglegaps */
static unsigned int currentkey = 0; /* used to keep track of keychord sequences */
/* global event handlers */
static struct wl_listener cursor_axis = {.notify = axisnotify};
static struct wl_listener cursor_button = {.notify = buttonpress};
static struct wl_listener cursor_frame = {.notify = cursorframe};
static struct wl_listener cursor_motion = {.notify = motionrelative};
static struct wl_listener cursor_motion_absolute = {.notify = motionabsolute};
static struct wl_listener drag_icon_destroy = {.notify = destroydragicon};
static struct wl_listener idle_inhibitor_create = {.notify = createidleinhibitor};
static struct wl_listener idle_inhibitor_destroy = {.notify = destroyidleinhibitor};
static struct wl_listener layout_change = {.notify = updatemons};
static struct wl_listener new_input = {.notify = inputdevice};
static struct wl_listener new_virtual_keyboard = {.notify = virtualkeyboard};
static struct wl_listener new_output = {.notify = createmon};
static struct wl_listener new_xdg_surface = {.notify = createnotify};
static struct wl_listener new_xdg_decoration = {.notify = createdecoration};
static struct wl_listener new_layer_shell_surface = {.notify = createlayersurface};
static struct wl_listener output_mgr_apply = {.notify = outputmgrapply};
static struct wl_listener output_mgr_test = {.notify = outputmgrtest};
static struct wl_listener request_activate = {.notify = urgent};
static struct wl_listener request_cursor = {.notify = setcursor};
static struct wl_listener request_set_psel = {.notify = setpsel};
static struct wl_listener request_set_sel = {.notify = setsel};
static struct wl_listener request_start_drag = {.notify = requeststartdrag};
static struct wl_listener start_drag = {.notify = startdrag};
static struct wl_listener session_lock_create_lock = {.notify = locksession};
static struct wl_listener session_lock_mgr_destroy = {.notify = destroysessionmgr};
/* dscm event handlers */
static struct dscm_monitor_v1_interface dscm_monitor_implementation = {
.release = dscm_closemon,
.set_tags = dscm_settags,
.set_layout = dscm_setlayout,
.set_client_tags = dscm_setclient,
};
static struct dscm_v1_interface dscm_implementation = {
.release = dscm_release,
.get_monitor = dscm_getmon,
.eval = dscm_eval,
};
#ifdef XWAYLAND
static void activatex11(struct wl_listener *listener, void *data);
static void configurex11(struct wl_listener *listener, void *data);
static void createnotifyx11(struct wl_listener *listener, void *data);
static Atom getatom(xcb_connection_t *xc, const char *name);
static void sethints(struct wl_listener *listener, void *data);
static void sigchld(int unused);
static void xwaylandready(struct wl_listener *listener, void *data);
static struct wl_listener new_xwayland_surface = {.notify = createnotifyx11};
static struct wl_listener xwayland_ready = {.notify = xwaylandready};
static struct wlr_xwayland *xwayland;
static Atom netatom[NetLast];
#endif
/* attempt to encapsulate suck into one file */
#include "client.h"
/* include guile config and bindings */
#include "dscm/shared.h"
#include "dscm/ipc.h"
#include "dscm/keycodes.h"
#include "dscm/utils.h"
#include "dscm/hooks.h"
#include "dscm/config.h"
#include "dscm/bindings.h"
/* function implementations */
void
applybounds(Client *c, struct wlr_box *bbox)
{
if (!c->isfullscreen) {
struct wlr_box min = {0}, max = {0};
client_get_size_hints(c, &max, &min);
/* try to set size hints */
c->geom.width = MAX(min.width + (2 * (int)c->bw), c->geom.width);
c->geom.height = MAX(min.height + (2 * (int)c->bw), c->geom.height);
/* Some clients set them max size to INT_MAX, which does not violates
* the protocol but its innecesary, they can set them max size to zero. */
if (max.width > 0 && !(2 * c->bw > INT_MAX - max.width)) /* Checks for overflow */
c->geom.width = MIN(max.width + (2 * c->bw), c->geom.width);
if (max.height > 0 && !(2 * c->bw > INT_MAX - max.height)) /* Checks for overflow */
c->geom.height = MIN(max.height + (2 * c->bw), c->geom.height);
}
if (c->geom.x >= bbox->x + bbox->width)
c->geom.x = bbox->x + bbox->width - c->geom.width;
if (c->geom.y >= bbox->y + bbox->height)
c->geom.y = bbox->y + bbox->height - c->geom.height;
if (c->geom.x + c->geom.width + 2 * c->bw <= bbox->x)
c->geom.x = bbox->x;
if (c->geom.y + c->geom.height + 2 * c->bw <= bbox->y)
c->geom.y = bbox->y;
}
void
applymonrules(Monitor *m)
{
MonitorRule *r;
wl_list_for_each(r, &monrules, link) {
if (!r->name || strstr(m->wlr_output->name, r->name)) {
m->mfact = r->mfact;
m->nmaster = r->nmaster;
wlr_output_set_scale(m->wlr_output, r->scale);
wlr_xcursor_manager_load(cursor_mgr, r->scale);
m->lt[0] = m->lt[1] = r->lt;
wlr_output_set_transform(m->wlr_output, r->rr);
break;
}
}
wlr_output_commit(m->wlr_output);
}
void
applyrules(Client *c)
{
/* rule matching */
const char *appid, *title;
unsigned int j, newtags = 0;
Rule *r;
Monitor *mon = selmon, *m;
c->isfloating = client_is_float_type(c);
if (!(appid = client_get_appid(c)))
appid = broken;
if (!(title = client_get_title(c)))
title = broken;
wl_list_for_each(r, &rules, link) {
if ((!r->title || strstr(title, r->title))
&& (!r->id || strstr(appid, r->id))) {
c->isfloating = r->isfloating;
c->alpha = r->alpha;
newtags |= r->tags;
j = 0;
wl_list_for_each(m, &mons, link)
if (r->monitor == j++)
mon = m;
}
}
wlr_scene_node_reparent(&c->scene->node, layers[c->isfloating ? LyrFloat : LyrTile]);
setmon(c, mon, newtags);
}
void
arrange(Monitor *m)
{
Client *c;
wl_list_for_each(c, &clients, link)
if (c->mon == m)
wlr_scene_node_set_enabled(&c->scene->node, VISIBLEON(c, m));
wlr_scene_node_set_enabled(&m->fullscreen_bg->node,
(c = focustop(m)) && c->isfullscreen);
if (m && m->lt[m->sellt]->arrange)
dscm_safe_call(DSCM_CALL_ARRANGE, m->lt[m->sellt]->arrange, m);
motionnotify(0);
checkidleinhibitor(NULL);
}
void
arrangelayer(Monitor *m, struct wl_list *list, struct wlr_box *usable_area,
int exclusive)
{
LayerSurface *layersurface;
struct wlr_box full_area = m->m;
wl_list_for_each(layersurface, list, link) {
struct wlr_layer_surface_v1 *wlr_layer_surface = layersurface->layer_surface;
struct wlr_layer_surface_v1_state *state = &wlr_layer_surface->current;
if (exclusive != (state->exclusive_zone > 0))
continue;
wlr_scene_layer_surface_v1_configure(layersurface->scene_layer, &full_area, usable_area);
wlr_scene_node_set_position(&layersurface->popups->node,
layersurface->scene->node.x, layersurface->scene->node.y);
layersurface->geom.x = layersurface->scene->node.x;
layersurface->geom.y = layersurface->scene->node.y;
}
}
void
arrangelayers(Monitor *m)
{
int i;
struct wlr_box usable_area = m->m;
uint32_t layers_above_shell[] = {
ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY,
ZWLR_LAYER_SHELL_V1_LAYER_TOP,
};
LayerSurface *layersurface;
if (!m->wlr_output->enabled)
return;
/* Arrange exclusive surfaces from top->bottom */
for (i = 3; i >= 0; i--)
arrangelayer(m, &m->layers[i], &usable_area, 1);
if (memcmp(&usable_area, &m->w, sizeof(struct wlr_box))) {
m->w = usable_area;
arrange(m);
}
/* Arrange non-exlusive surfaces from top->bottom */
for (i = 3; i >= 0; i--)
arrangelayer(m, &m->layers[i], &usable_area, 0);
/* Find topmost keyboard interactive layer, if such a layer exists */
for (i = 0; i < LENGTH(layers_above_shell); i++) {
wl_list_for_each_reverse(layersurface,
&m->layers[layers_above_shell[i]], link) {
if (!locked && layersurface->layer_surface->current.keyboard_interactive
&& layersurface->mapped) {
/* Deactivate the focused client. */
focusclient(NULL, 0);
exclusive_focus = layersurface;
client_notify_enter(layersurface->layer_surface->surface, wlr_seat_get_keyboard(seat));
return;
}
}
}
}
void
axisnotify(struct wl_listener *listener, void *data)
{
/* This event is forwarded by the cursor when a pointer emits an axis event,
* for example when you move the scroll wheel. */
struct wlr_pointer_axis_event *event = data;
IDLE_NOTIFY_ACTIVITY;
/* TODO: allow usage of scroll whell for mousebindings, it can be implemented
* checking the event's orientation and the delta of the event */
/* Notify the client with pointer focus of the axis event. */
wlr_seat_pointer_notify_axis(seat,
event->time_msec, event->orientation, event->delta,
event->delta_discrete, event->source);
}
void
buttonpress(struct wl_listener *listener, void *data)
{
struct wlr_pointer_button_event *event = data;
struct wlr_keyboard *keyboard;
uint32_t mods;
Client *c;
Binding *b;
IDLE_NOTIFY_ACTIVITY;
switch (event->state) {
case WLR_BUTTON_PRESSED:
cursor_mode = CurPressed;
if (locked)
break;
/* Change focus if the button was _pressed_ over a client */
xytonode(cursor->x, cursor->y, NULL, &c, NULL, NULL, NULL);
if (c && (!client_is_unmanaged(c) || client_wants_focus(c)))
focusclient(c, 1);
keyboard = wlr_seat_get_keyboard(seat);
mods = keyboard ? wlr_keyboard_get_modifiers(keyboard) : 0;
wl_list_for_each(b, &buttons, link) {
/* Mouse bindings can only have one key. */
if (CLEANMASK(mods) == CLEANMASK(b->keys[0].mod) &&
event->button == b->keys[0].key && b->action) {
dscm_safe_call(DSCM_CALL_ACTION, b->action, NULL);
return;
}
}
break;
case WLR_BUTTON_RELEASED:
/* If you released any buttons, we exit interactive move/resize mode. */
if (!locked && cursor_mode != CurNormal && cursor_mode != CurPressed) {
cursor_mode = CurNormal;
/* Clear the pointer focus, this way if the cursor is over a surface
* we will send an enter event after which the client will provide us
* a cursor surface */
wlr_seat_pointer_clear_focus(seat);
motionnotify(0);
/* Drop the window off on its new monitor */
selmon = xytomon(cursor->x, cursor->y);
setmon(grabc, selmon, 0);
return;
} else {
cursor_mode = CurNormal;
}
break;
}
/* If the event wasn't handled by the compositor, notify the client with
* pointer focus that a button press has occurred */
wlr_seat_pointer_notify_button(seat,
event->time_msec, event->button, event->state);
}
void
changealpha(const Arg *arg)
{
Client *sel = focustop(selmon);
if (sel) {
sel->alpha += arg->f;
if (sel->alpha > 1.0)
sel->alpha = 1.0;
if (sel->alpha < 0.1)
sel->alpha = 0.1;
}
}
void
chvt(const Arg *arg)
{
wlr_session_change_vt(wlr_backend_get_session(backend), arg->ui);
}
void
checkidleinhibitor(struct wlr_surface *exclude)
{
int inhibited = 0;
struct wlr_idle_inhibitor_v1 *inhibitor;
wl_list_for_each(inhibitor, &idle_inhibit_mgr->inhibitors, link) {
struct wlr_surface *surface = wlr_surface_get_root_surface(inhibitor->surface);
struct wlr_scene_tree *tree = surface->data;
if (exclude != surface && (bypass_surface_visibility ||
(!tree || tree->node.enabled))) {
inhibited = 1;
break;
}
}
wlr_idle_set_enabled(idle, NULL, !inhibited);
wlr_idle_notifier_v1_set_inhibited(idle_notifier, inhibited);
}
void
cleanup(void)
{
#ifdef XWAYLAND
wlr_xwayland_destroy(xwayland);
#endif
wl_display_destroy_clients(dpy);
if (child_pid > 0) {
kill(child_pid, SIGTERM);
waitpid(child_pid, NULL, 0);
}
wlr_backend_destroy(backend);
wlr_renderer_destroy(drw);
wlr_allocator_destroy(alloc);
wlr_xcursor_manager_destroy(cursor_mgr);
wlr_cursor_destroy(cursor);
wlr_output_layout_destroy(output_layout);
wlr_seat_destroy(seat);
wl_display_destroy(dpy);
}
void
cleanupkeyboard(struct wl_listener *listener, void *data)
{
Keyboard *kb = wl_container_of(listener, kb, destroy);
wl_list_remove(&kb->link);
wl_list_remove(&kb->modifiers.link);
wl_list_remove(&kb->key.link);
wl_list_remove(&kb->destroy.link);
free(kb);
}
void
cleanupmon(struct wl_listener *listener, void *data)
{
DscmMonitor *mon, *montmp;
Monitor *m = wl_container_of(listener, m, destroy);
LayerSurface *l, *tmp;
int i;
for (i = 0; i <= ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY; i++)
wl_list_for_each_safe(l, tmp, &m->layers[i], link)
wlr_layer_surface_v1_destroy(l->layer_surface);
wl_list_remove(&m->destroy.link);
wl_list_remove(&m->frame.link);
wl_list_remove(&m->link);
m->wlr_output->data = NULL;
wlr_output_layout_remove(output_layout, m->wlr_output);
wlr_scene_output_destroy(m->scene_output);
wlr_scene_node_destroy(&m->fullscreen_bg->node);
wl_list_for_each_safe(mon, montmp, &m->dscm, link) {
wl_resource_set_user_data(mon->resource, NULL);
free(mon);
}
closemon(m);
free(m);
}
void
closemon(Monitor *m)
{
/* update selmon if needed and
* move closed monitor's clients to the focused one */
Client *c;
if (wl_list_empty(&mons)) {
selmon = NULL;
} else if (m == selmon) {
int nmons = wl_list_length(&mons), i = 0;
do /* don't switch to disabled mons */
selmon = wl_container_of(mons.next, selmon, link);
while (!selmon->wlr_output->enabled && i++ < nmons);
}
wl_list_for_each(c, &clients, link) {
if (c->isfloating && c->geom.x > m->m.width)
resize(c, (struct wlr_box){.x = c->geom.x - m->w.width, .y = c->geom.y,
.width = c->geom.width, .height = c->geom.height}, 0, 1);
if (c->mon == m)
setmon(c, selmon, c->tags);
}
focusclient(focustop(selmon), 1);
printstatus();
}
void
commitlayersurfacenotify(struct wl_listener *listener, void *data)
{
LayerSurface *layersurface = wl_container_of(listener, layersurface,
surface_commit);
struct wlr_layer_surface_v1 *wlr_layer_surface = layersurface->layer_surface;
struct wlr_output *wlr_output = wlr_layer_surface->output;
/* For some reason this layersurface have no monitor, this can be because
* its monitor has just been destroyed */
if (!wlr_output || !(layersurface->mon = wlr_output->data))
return;
if (layers[wlr_layer_surface->current.layer] != layersurface->scene->node.parent) {
wlr_scene_node_reparent(&layersurface->scene->node,
layers[wlr_layer_surface->current.layer]);
wlr_scene_node_reparent(&layersurface->popups->node,
layers[wlr_layer_surface->current.layer]);
wl_list_remove(&layersurface->link);
wl_list_insert(&layersurface->mon->layers[wlr_layer_surface->current.layer],
&layersurface->link);
}
if (wlr_layer_surface->current.layer < ZWLR_LAYER_SHELL_V1_LAYER_TOP)
wlr_scene_node_reparent(&layersurface->popups->node, layers[LyrTop]);
if (wlr_layer_surface->current.committed == 0
&& layersurface->mapped == wlr_layer_surface->mapped)
return;
layersurface->mapped = wlr_layer_surface->mapped;
arrangelayers(layersurface->mon);
}
void
commitnotify(struct wl_listener *listener, void *data)
{
Client *c = wl_container_of(listener, c, commit);
struct wlr_box box = {0};
client_get_geometry(c, &box);
if (c->mon && !wlr_box_empty(&box) && (box.width != c->geom.width - 2 * c->bw
|| box.height != c->geom.height - 2 * c->bw))
c->isfloating ? resize(c, c->geom, 1, c->bw) : arrange(c->mon);
/* mark a pending resize as completed */
if (c->resize && c->resize <= c->surface.xdg->current.configure_serial)
c->resize = 0;
}
void
createdecoration(struct wl_listener *listener, void *data)
{
struct wlr_xdg_toplevel_decoration_v1 *dec = data;
wlr_xdg_toplevel_decoration_v1_set_mode(dec, WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
}
void
createidleinhibitor(struct wl_listener *listener, void *data)
{
struct wlr_idle_inhibitor_v1 *idle_inhibitor = data;
wl_signal_add(&idle_inhibitor->events.destroy, &idle_inhibitor_destroy);
checkidleinhibitor(NULL);
}
void
createkeyboard(struct wlr_keyboard *keyboard)
{
struct xkb_context *context;
struct xkb_keymap *keymap;
Keyboard *kb = keyboard->data = ecalloc(1, sizeof(*kb));
kb->wlr_keyboard = keyboard;
/* Prepare an XKB keymap and assign it to the keyboard. */
context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
keymap = xkb_keymap_new_from_names(context, xkb_rules,
XKB_KEYMAP_COMPILE_NO_FLAGS);
wlr_keyboard_set_keymap(keyboard, keymap);
xkb_keymap_unref(keymap);
xkb_context_unref(context);
wlr_keyboard_set_repeat_info(keyboard, repeat_rate, repeat_delay);
/* Here we set up listeners for keyboard events. */
LISTEN(&keyboard->events.modifiers, &kb->modifiers, keypressmod);
LISTEN(&keyboard->events.key, &kb->key, keypress);
LISTEN(&keyboard->base.events.destroy, &kb->destroy, cleanupkeyboard);
wlr_seat_set_keyboard(seat, keyboard);
/* And add the keyboard to our list of keyboards */
wl_list_insert(&keyboards, &kb->link);
}
void
createlayersurface(struct wl_listener *listener, void *data)
{
struct wlr_layer_surface_v1 *wlr_layer_surface = data;
LayerSurface *layersurface;
struct wlr_layer_surface_v1_state old_state;
if (!wlr_layer_surface->output)
wlr_layer_surface->output = selmon ? selmon->wlr_output : NULL;
if (!wlr_layer_surface->output)
wlr_layer_surface_v1_destroy(wlr_layer_surface);
layersurface = ecalloc(1, sizeof(LayerSurface));
layersurface->type = LayerShell;
LISTEN(&wlr_layer_surface->surface->events.commit,
&layersurface->surface_commit, commitlayersurfacenotify);
LISTEN(&wlr_layer_surface->events.destroy, &layersurface->destroy,
destroylayersurfacenotify);
LISTEN(&wlr_layer_surface->events.map, &layersurface->map,
maplayersurfacenotify);
LISTEN(&wlr_layer_surface->events.unmap, &layersurface->unmap,
unmaplayersurfacenotify);
layersurface->layer_surface = wlr_layer_surface;
layersurface->mon = wlr_layer_surface->output->data;
wlr_layer_surface->data = layersurface;
layersurface->scene_layer = wlr_scene_layer_surface_v1_create(
layers[wlr_layer_surface->pending.layer], wlr_layer_surface);
layersurface->scene = layersurface->scene_layer->tree;
layersurface->popups = wlr_layer_surface->surface->data =
wlr_scene_tree_create(layers[wlr_layer_surface->pending.layer]);
layersurface->scene->node.data = layersurface;