forked from bakkeby/dusk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dusk.c
4104 lines (3611 loc) · 102 KB
/
dusk.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.
*
* The dusk dynamic window manager is designed like any other X client as well.
* It is driven through handling X events. In contrast to other X clients, a
* window manager selects for SubstructureRedirectMask on the root window, to
* receive events about window (dis-)appearance. Only one X connection at a
* time is allowed to select for this event mask.
*
* The event handlers of dusk are organized in an array which is accessed
* whenever a new event has been fetched. This allows event dispatching
* in O(1) time.
*
* Each child of the root window is called a client, except windows which have
* set the override_redirect flag. Clients are organized in a linked client
* list on each workspace. The focus history is remembered through a stack list
* on each workspace. A workspace client area is restricted on a per monitor
* basis.
*
* Keys and workspace rules are organized as arrays and defined in config.h.
*
* To understand everything else, start reading main().
*/
#include <errno.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
#include <X11/Xft/Xft.h>
#include "drw.h"
#include "util.h"
#include <assert.h>
#include <libgen.h>
#include <sys/stat.h>
#define SPAWN_CWD_DELIM " []{}()<>\"':"
/* macros */
#define Button6 6
#define Button7 7
#define Button8 8
#define Button9 9
#define BARRULES 50
#define NUM_STATUSES 10
#define STATUS_BUFFER 512
#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
#define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
#define INTERSECT(X,Y,W,H,Z) (MAX(0, MIN((X)+(W),(Z)->wx+(Z)->ww) - MAX((X),(Z)->wx)) \
* MAX(0, MIN((Y)+(H),(Z)->wy+(Z)->wh) - MAX((Y),(Z)->wy)))
#define INTERSECTC(X,Y,W,H,Z) (MAX(0, MIN((X)+(W),(Z)->x+(Z)->w) - MAX((X),(Z)->x)) \
* MAX(0, MIN((Y)+(H),(Z)->y+(Z)->h) - MAX((Y),(Z)->y)))
#define LENGTH(X) (sizeof X / sizeof X[0])
#define MOUSEMASK (BUTTONMASK|PointerMotionMask)
#define WIDTH(X) ((X)->w + 2 * (X)->bw)
#define HEIGHT(X) ((X)->h + 2 * (X)->bw)
#define WTYPE "_NET_WM_WINDOW_TYPE_"
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)))
#define TEXT2DW(X) (status2dtextlength((X)))
#define CLIENT (arg && arg->v ? (Client*)arg->v : selws->sel)
#define NAME(X) ((X) ? (X)->name : "NULL")
/* enums */
enum {
CurResizeBR,
CurResizeBL,
CurResizeTR,
CurResizeTL,
CurResizeHorzArrow,
CurResizeVertArrow,
CurIronCross,
CurNormal,
CurResize,
CurMove,
CurSwallow,
CurLast
}; /* cursor */
enum {
SchemeNorm,
SchemeSel,
SchemeTitleNorm,
SchemeTitleSel,
SchemeWsNorm,
SchemeWsVisible,
SchemeWsSel,
SchemeWsOcc,
SchemeScratchSel,
SchemeScratchNorm,
SchemeHidSel,
SchemeHidNorm,
SchemeUrg,
SchemeMarked,
SchemeFlexActTTB,
SchemeFlexActLTR,
SchemeFlexActMONO,
SchemeFlexActGRID,
SchemeFlexActGRIDC,
SchemeFlexActGRD1,
SchemeFlexActGRD2,
SchemeFlexActGRDM,
SchemeFlexActHGRD,
SchemeFlexActDWDL,
SchemeFlexActDWDLC,
SchemeFlexActSPRL,
SchemeFlexActSPRLC,
SchemeFlexActTTMI,
SchemeFlexActTTMIC,
SchemeFlexActFloat,
SchemeFlexInaTTB,
SchemeFlexInaLTR,
SchemeFlexInaMONO,
SchemeFlexInaGRID,
SchemeFlexInaGRIDC,
SchemeFlexInaGRD1,
SchemeFlexInaGRD2,
SchemeFlexInaGRDM,
SchemeFlexInaHGRD,
SchemeFlexInaDWDL,
SchemeFlexInaDWDLC,
SchemeFlexInaSPRL,
SchemeFlexInaSPRLC,
SchemeFlexInaTTMI,
SchemeFlexInaTTMIC,
SchemeFlexInaFloat,
SchemeFlexSelTTB,
SchemeFlexSelLTR,
SchemeFlexSelMONO,
SchemeFlexSelGRID,
SchemeFlexSelGRIDC,
SchemeFlexSelGRD1,
SchemeFlexSelGRD2,
SchemeFlexSelGRDM,
SchemeFlexSelHGRD,
SchemeFlexSelDWDL,
SchemeFlexSelDWDLC,
SchemeFlexSelSPRL,
SchemeFlexSelSPRLC,
SchemeFlexSelTTMI,
SchemeFlexSelTTMIC,
SchemeFlexSelFloat,
SchemeLast,
}; /* color schemes */
static const char *default_resource_prefixes[SchemeLast] = {
/* resource prefix */
[SchemeNorm] = "norm",
[SchemeSel] = "sel",
[SchemeTitleNorm] = "titlenorm",
[SchemeTitleSel] = "titlesel",
[SchemeWsNorm] = "wsnorm",
[SchemeWsVisible] = "wsvis",
[SchemeWsSel] = "wssel",
[SchemeWsOcc] = "wsocc",
[SchemeHidNorm] = "hidnorm",
[SchemeHidSel] = "hidsel",
[SchemeUrg] = "urg",
[SchemeMarked] = "marked",
[SchemeScratchNorm] = "scratchnorm",
[SchemeScratchSel] = "scratchsel",
[SchemeFlexActTTB] = "act.TTB",
[SchemeFlexActLTR] = "act.LTR",
[SchemeFlexActMONO] = "act.MONO",
[SchemeFlexActGRID] = "act.GRID",
[SchemeFlexActGRIDC] = "act.GRIDC",
[SchemeFlexActGRD1] = "act.GRD1",
[SchemeFlexActGRD2] = "act.GRD2",
[SchemeFlexActGRDM] = "act.GRDM",
[SchemeFlexActHGRD] = "act.HGRD",
[SchemeFlexActDWDL] = "act.DWDL",
[SchemeFlexActDWDLC] = "act.DWDLC",
[SchemeFlexActSPRL] = "act.SPRL",
[SchemeFlexActSPRLC] = "act.SPRLC",
[SchemeFlexActTTMI] = "act.TTMI",
[SchemeFlexActTTMIC] = "act.TTMIC",
[SchemeFlexActFloat] = "act.float",
[SchemeFlexInaTTB] = "norm.TTB",
[SchemeFlexInaLTR] = "norm.LTR",
[SchemeFlexInaMONO] = "norm.MONO",
[SchemeFlexInaGRID] = "norm.GRID",
[SchemeFlexInaGRIDC] = "norm.GRIDC",
[SchemeFlexInaGRD1] = "norm.GRD1",
[SchemeFlexInaGRD2] = "norm.GRD2",
[SchemeFlexInaGRDM] = "norm.GRDM",
[SchemeFlexInaHGRD] = "norm.HGRD",
[SchemeFlexInaDWDL] = "norm.DWDL",
[SchemeFlexInaDWDLC] = "norm.DWDLC",
[SchemeFlexInaSPRL] = "norm.SPRL",
[SchemeFlexInaSPRLC] = "norm.SPRLC",
[SchemeFlexInaTTMI] = "norm.TTMI",
[SchemeFlexInaTTMIC] = "norm.TTMIC",
[SchemeFlexInaFloat] = "norm.float",
[SchemeFlexSelTTB] = "sel.TTB",
[SchemeFlexSelLTR] = "sel.LTR",
[SchemeFlexSelMONO] = "sel.MONO",
[SchemeFlexSelGRID] = "sel.GRID",
[SchemeFlexSelGRIDC] = "sel.GRIDC",
[SchemeFlexSelGRD1] = "sel.GRD1",
[SchemeFlexSelGRD2] = "sel.GRD2",
[SchemeFlexSelGRDM] = "sel.GRDM",
[SchemeFlexSelHGRD] = "sel.HGRD",
[SchemeFlexSelDWDL] = "sel.DWDL",
[SchemeFlexSelDWDLC] = "sel.DWDLC",
[SchemeFlexSelSPRL] = "sel.SPRL",
[SchemeFlexSelSPRLC] = "sel.SPRLC",
[SchemeFlexSelTTMI] = "sel.TTMI",
[SchemeFlexSelTTMIC] = "sel.TTMIC",
[SchemeFlexSelFloat] = "sel.float",
};
enum {
ClkLtSymbol,
ClkStatusText,
ClkWinTitle,
ClkClientWin,
ClkRootWin,
ClkWorkspaceBar,
ClkLast
}; /* clicks */
enum {
LAYOUT, /* controls overall layout arrangement / split */
MASTER, /* indicates the tile arrangement for the master area */
STACK, /* indicates the tile arrangement for the stack area */
STACK2, /* indicates the tile arrangement for the secondary stack area */
LTAXIS_LAST,
}; /* named flextile constants */
typedef union {
long i;
unsigned long ui;
float f;
const void *v;
} Arg;
typedef struct Monitor Monitor;
typedef struct Bar Bar;
typedef struct {
unsigned int click;
unsigned int mask;
unsigned int button;
void (*func)(const Arg *arg);
const Arg arg;
} Button;
typedef struct Workspace Workspace;
typedef struct Client Client;
struct Client {
char name[256];
char altname[256];
char label[32];
char iconpath[256]; /* maximum file path length under linux is 4096 bytes */
float mina, maxa;
float cfact;
int x, y, w, h;
int sfx, sfy, sfw, sfh; /* stored float geometry, used on mode revert */
int oldx, oldy, oldw, oldh;
int basew, baseh, incw, inch, maxw, maxh, minw, minh;
int bw, oldbw;
int group;
int area; /* arrangement area (master, stack, secondary stack) */
int arr; /* tile arrangement (left to right, top to bottom, etc.) */
int scheme;
char scratchkey;
char swallowkey;
char swallowedby;
unsigned int idx;
double opacity;
pid_t pid;
Client *next;
Client *snext;
Client *swallowing;
Client *linked;
Workspace *ws;
Workspace *revertws; /* holds the original workspace info from when the client was opened */
Window win;
unsigned int icw, ich;
Picture icon;
uint64_t flags;
uint64_t prevflags;
};
typedef struct {
int type;
unsigned int mod;
#if USE_KEYCODES
KeyCode keycode;
#else
KeySym keysym;
#endif // USE_KEYCODES
void (*func)(const Arg *);
const Arg arg;
} Key;
typedef struct {
char *icon;
Arg arg;
int pos; /* indicates that the icon is to be added as a prefix (0) or suffix (1) */
} StackerIcon;
typedef struct {
int nmaster;
int nstack;
int layout;
int masteraxis; /* master stack area */
int stack1axis; /* primary stack area */
int stack2axis; /* secondary stack area, e.g. centered master */
void (*symbolfunc)(Workspace *, unsigned int);
} LayoutPreset;
typedef struct {
const char *symbol;
void (*arrange)(Workspace *);
LayoutPreset preset;
const char *name;
} Layout;
typedef struct Preview Preview;
struct Monitor {
int num; /* monitor index */
int mx, my, mw, mh; /* screen size */
int wx, wy, ww, wh; /* window area */
int gappih; /* horizontal gap between windows */
int gappiv; /* vertical gap between windows */
int gappoh; /* horizontal outer gaps */
int gappov; /* vertical outer gaps */
int showbar;
int orientation; /* screen orientation: 0 = Horizontal, 1 = Vertical */
uint64_t wsmask;
uint64_t prevwsmask;
unsigned int borderpx;
Monitor *next;
Workspace *selws;
Bar *bar;
Preview *preview;
};
typedef struct {
const char *class;
const char *role;
const char *instance;
const char *title;
const char *wintype;
const int transient;
const double opacity;
const uint64_t flags;
const char *floatpos;
const char scratchkey;
const char *workspace;
const char *label;
const char swallowedby;
const char swallowkey;
const char *iconpath;
const char *alttitle;
int resume;
} Rule;
struct Workspace {
int wx, wy, ww, wh; /* workspace area */
char ltsymbol[64];
char name[16];
float mfact;
float wfact;
int scheme[4];
int ltaxis[4];
int nstack;
int nmaster;
int enablegaps;
int visible;
int orientation;
int num;
int pinned; /* whether workspace is pinned to assigned monitor or not */
Client *clients;
Client *sel;
Client *stack;
Client *prevzoom;
Workspace *next;
Monitor *mon;
Pixmap preview;
const Layout *layout;
const Layout *prevlayout;
char *icondef; /* default icon */
char *iconvac; /* vacant icon (when workspace is selected, default is empty, and no clients) */
char *iconocc; /* when workspace has clients */
};
typedef struct {
char name[16];
int monitor;
int pinned;
int layout;
float mfact;
int nmaster;
int nstack;
int enablegaps;
int norm_scheme;
int vis_scheme;
int sel_scheme;
int occ_scheme;
char *icondef;
char *iconvac;
char *iconocc;
} WorkspaceRule;
/* function declarations */
static void applyrules(Client *c);
static int reapplyrules(Client *c);
static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
static void arrange(Workspace *ws);
static void arrangemon(Monitor *m);
static void arrangews(Workspace *ws);
static void attach(Client *c);
static void attachstack(Client *c);
static void buttonpress(XEvent *e);
static void checkotherwm(void);
static void cleanup(void);
static void cleanupmon(Monitor *mon);
static void clientfittomon(Client *c, Monitor *m, int *cx, int *cy, int *cw, int *ch);
static void clientfsrestore(Client *c);
static void clientsfsrestore(Client *clients);
static int clientscheme(Client *c, Client *sel);
static void clientmessage(XEvent *e);
static void clientmonresize(Client *c, Monitor *from, Monitor *to);
static void clientsmonresize(Client *clients, Monitor *from, Monitor *to);
static void clientrelposmon(Client *c, Monitor *o, Monitor *n, int *cx, int *cy, int *cw, int *ch);
static void clienttomon(const Arg *arg);
static void clientstomon(const Arg *arg);
static void configure(Client *c);
static Workspace *configurenotify(XEvent *e);
static void configurerequest(XEvent *e);
static Monitor *createmon(int num);
static Workspace *destroynotify(XEvent *e);
static void detach(Client *c);
static void detachstack(Client *c);
static Monitor *dirtomon(int dir);
static Workspace *dirtows(int dir);
static void entermon(Monitor *m, Client *next);
static void enternotify(XEvent *e);
static void expose(XEvent *e);
static void focus(Client *c);
static void focusin(XEvent *e);
static void focusmon(const Arg *arg);
static void focusstack(const Arg *arg);
static Atom getatomprop(Client *c, Atom prop, Atom req);
static Client *getpointerclient(void);
static int getrootptr(int *x, int *y);
static long getstate(Window w);
static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
static void grabbuttons(Client *c, int focused);
static void grabkeys(void);
static void hide(Client *c);
static void incnmaster(const Arg *arg);
static void incnstack(const Arg *arg);
static int isatomstate(XClientMessageEvent *cme, int atom);
static void keypress(XEvent *e);
static void keyrelease(XEvent *e);
static void killclient(const Arg *arg);
static void manage(Window w, XWindowAttributes *wa);
static void mappingnotify(XEvent *e);
static void maprequest(XEvent *e);
static void motionnotify(XEvent *e);
static unsigned long long now(void);
static void propertynotify(XEvent *e);
static void restart(const Arg *arg);
static void quit(const Arg *arg);
static void raiseclient(Client *c);
static void readclientstackingorder(void);
static Monitor *recttomon(int x, int y, int w, int h);
static Workspace *recttows(int x, int y, int w, int h);
static Client *recttoclient(int x, int y, int w, int h, int include_floating);
static void resize(Client *c, int x, int y, int w, int h, int interact);
static void resizeclient(Client *c, int x, int y, int w, int h);
static void resizeclientpad(Client *c, int x, int y, int w, int h, int xpad, int ypad);
static void restack(Workspace *ws);
static void run(void);
static void scan(void);
static int sendevent(Window w, Atom proto, int m, long d0, long d1, long d2, long d3, long d4);
static void setbackground(void);
static void setclientstate(Client *c, long state);
static void setfocus(Client *c);
static void setfullscreen(Client *c, int fullscreen, int setfakefullscreen);
static void setlayout(const Arg *arg);
static void setmfact(const Arg *arg);
static void setup(void);
static void seturgent(Client *c, int urg);
static void show(Client *c);
static void skipfocusevents(void);
static void spawn(const Arg *arg);
static pid_t spawncmd(const Arg *arg, int buttonclick, int orphan);
static void structurenotify(XEvent *e);
static unsigned int textw_clamp(const char *str, unsigned int n);
static void togglefloating(const Arg *arg);
static void unfocus(Client *c, int setfocus, Client *nextfocus);
static void unmanage(Client *c, int destroyed);
static Workspace *unmapnotify(XEvent *e);
static void updateclientlist(void);
static int updategeom(int width, int height);
static void updatenumlockmask(void);
static void updatesizehints(Client *c);
static void updatetitle(Client *c);
static void updatewmhints(Client *c);
static Client *wintoclient(Window w);
static Monitor *wintomon(Window w);
static int xerror(Display *dpy, XErrorEvent *ee);
static int xerrordummy(Display *dpy, XErrorEvent *ee);
static int xerrorstart(Display *dpy, XErrorEvent *ee);
static void zoom(const Arg *arg);
/* bar functions */
#include "lib/include.h"
/* variables */
static const char broken[] = "fubar";
static char rawstatustext[NUM_STATUSES][STATUS_BUFFER];
static const char* env_home;
static int env_homelen;
static int screen;
static int sw, sh; /* X display screen geometry width, height */
static int lrpad; /* sum of left and right padding for text */
static int force_warp = 0; /* force warp in some situations, e.g. killclient */
static int cursor_hidden = 0;
static int mouse_x = 0;
static int mouse_y = 0;
static int prev_ptr_x = 0;
static int prev_ptr_y = 0;
static int ignore_warp = 0; /* force skip warp in some situations, e.g. dragmfact, dragcfact */
static int num_workspaces = 0; /* the number of available workspaces */
static int combo = 0; /* used for combo keys */
static int monitorchanged = 0; /* used for combo logic */
static int grp_idx = 0; /* used for grouping windows together */
static int arrange_focus_on_monocle = 1; /* used in focus to arrange monocle layouts on focus */
/* Used by propertynotify to throttle repeating notifications */
static int pn_prev_state = 0;
static Window pn_prev_win = 0;
static Atom pn_prev_atom = None;
static unsigned int pn_prev_count = 0;
static int (*xerrorxlib)(Display *, XErrorEvent *);
static unsigned int numlockmask = 0;
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ButtonRelease] = keyrelease,
[ClientMessage] = clientmessage,
[ConfigureNotify] = structurenotify,
[ConfigureRequest] = configurerequest,
[DestroyNotify] = structurenotify,
[EnterNotify] = enternotify,
[Expose] = expose,
[FocusIn] = focusin,
#ifdef HAVE_LIBXI
[GenericEvent] = genericevent,
#endif
[KeyPress] = keypress,
[KeyRelease] = keypress,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[MotionNotify] = motionnotify,
[PropertyNotify] = propertynotify,
[ResizeRequest] = resizerequest,
[UnmapNotify] = structurenotify,
};
static Atom wmatom[WMLast], netatom[NetLast], allowed[NetWMActionLast], xatom[XLast], duskatom[DuskLast];
static int running = 1;
static Cur *cursor[CurLast];
static Clr **scheme;
static Display *dpy;
static Drw *drw;
static Monitor *mons, *selmon, *dummymon;
static Workspace *workspaces, *selws;
static Window root, wmcheckwin;
/* configuration, allows nested code to access above variables */
#include "config.h"
#include "lib/include.c"
/* function implementations */
void
applyrules(Client *c)
{
const Rule *r;
const char *class, *instance;
Atom game_id = None, da = None, *win_types = NULL;
char role[64] = {0};
int di;
unsigned long dl, nitems;
unsigned char *p = NULL;
unsigned int i, transient;
Workspace *ws = NULL;
XClassHint ch = { NULL, NULL };
if (XGetWindowProperty(dpy, c->win, netatom[NetWMWindowType], 0L, sizeof(Atom), False, XA_ATOM,
&da, &di, &nitems, &dl, &p) == Success && p) {
win_types = (Atom *) p;
}
/* rule matching */
XGetClassHint(dpy, c->win, &ch);
class = ch.res_class ? ch.res_class : broken;
instance = ch.res_name ? ch.res_name : broken;
gettextprop(c->win, wmatom[WMWindowRole], role, sizeof(role));
game_id = getatomprop(c, duskatom[SteamGameID], AnyPropertyType);
transient = ISTRANSIENT(c) ? 1 : 0;
/* Steam games may come through with custom class, instance and name making it hard to create
* generic rules for them. Overriding the class with "steam_app_" to make this easier. */
if (game_id && !strstr(class, "steam_app_"))
class = "steam_app_";
if (enabled(Debug))
fprintf(stderr, "applyrules: new client %s (%ld), class = '%s', instance = '%s', role = '%s', wintype = '%ld'\n", c->name, c->win, class, instance, role, nitems ? win_types[0] : 0);
for (i = 0; i < LENGTH(clientrules); i++) {
r = &clientrules[i];
if ((!r->title || strstr(c->name, r->title))
&& (!r->class || strstr(class, r->class))
&& (!r->role || strstr(role, r->role))
&& (!r->instance || strstr(instance, r->instance))
&& (!r->wintype || atomin(XInternAtom(dpy, r->wintype, False), win_types, nitems))
&& (r->transient == -1 || r->transient == transient))
{
c->flags |= Ruled | r->flags;
c->scratchkey = r->scratchkey;
c->swallowedby = r->swallowedby;
c->swallowkey = r->swallowkey;
if (r->opacity)
c->opacity = r->opacity;
if (r->workspace)
for (ws = workspaces; ws && strcmp(ws->name, r->workspace) != 0; ws = ws->next);
c->ws = ws && ws->mon != dummymon ? ws : selws;
if (r->floatpos)
setfloatpos(c, r->floatpos, 0, 1);
if (REVERTWORKSPACE(c) && !c->ws->visible)
c->revertws = c->ws->mon->selws;
if (r->label)
strlcpy(c->label, r->label, sizeof c->label);
else
saveclientclass(c);
if (r->iconpath)
load_icon_from_png_image(c, r->iconpath);
if (r->alttitle)
strlcpy(c->altname, r->alttitle, sizeof c->altname);
if (enabled(Debug) || DEBUGGING(c))
fprintf(stderr, "applyrules: client rule %d matched:\n class: %s\n role: %s\n instance: %s\n title: %s\n wintype: %s\n flags: %lu\n floatpos: %s\n workspace: %s\n label: %s\n",
i,
r->class ? r->class : "NULL",
r->role ? r->role : "NULL",
r->instance ? r->instance : "NULL",
r->title ? r->title : "NULL",
r->wintype ? r->wintype : "NULL",
r->flags,
r->floatpos ? r->floatpos : "NULL",
r->workspace,
r->label ? r->label : "NULL");
if (!r->resume)
break; /* only allow one rule match */
}
}
if (!RULED(c)) {
if (transient)
addflag(c, Centered);
saveclientclass(c);
}
if (ch.res_class)
XFree(ch.res_class);
if (ch.res_name)
XFree(ch.res_name);
if (p)
XFree(p);
}
/* This mimics most of what the manage function does when initially managing the window. It returns
* 1 if rules were applied, and 0 otherwise. A window getting rules applied this way will not be
* swallowed.
*/
int
reapplyrules(Client *c)
{
Client *t;
Window trans = None;
Workspace *client_ws, *rule_ws;
uint64_t flags;
if (RULED(c) && !REAPPLYRULES(c))
return 0;
flags = c->flags;
c->flags = 0;
client_ws = c->ws;
applyrules(c);
if (!RULED(c)) {
c->flags = flags;
return 0;
}
if (DISALLOWED(c)) {
killclient(&((Arg) { .v = c }));
return 1;
}
if (ISUNMANAGED(c)) {
XMapWindow(dpy, c->win);
if (LOWER(c))
XLowerWindow(dpy, c->win);
else if (RAISE(c))
XRaiseWindow(dpy, c->win);
unmanage(c, 0);
if (client_ws) {
arrange(client_ws);
focus(NULL);
drawbar(client_ws->mon);
}
return 1;
}
rule_ws = c->ws;
if (rule_ws != client_ws || ISSTICKY(c)) {
c->ws = client_ws;
detach(c);
detachstack(c);
if (ISSTICKY(c)) {
stickyws->mon = rule_ws->mon;
detachws(stickyws);
attachws(stickyws, rule_ws);
c->ws = stickyws;
stickyws->sel = c;
rule_ws = stickyws;
} else
c->ws = rule_ws;
attach(c);
attachstack(c);
if (client_ws->visible)
arrange(client_ws);
else if (client_ws->mon != rule_ws->mon)
drawbar(client_ws->mon);
}
if (NOBORDER(c)) {
c->bw = 0;
removeflag(c, NoBorder);
}
if (c->opacity)
opacity(c, c->opacity);
if (ISCENTERED(c)) {
/* Transient windows are centered within the geometry of the parent window */
if (ISTRANSIENT(c) && XGetTransientForHint(dpy, c->win, &trans) && (t = wintoclient(trans))) {
c->sfx = c->x = t->x + WIDTH(t) / 2 - WIDTH(c) / 2;
c->sfy = c->y = t->y + HEIGHT(t) / 2 - HEIGHT(c) / 2;
} else {
c->sfx = c->x = c->ws->mon->wx + (c->ws->mon->ww - WIDTH(c)) / 2;
c->sfy = c->y = c->ws->mon->wy + (c->ws->mon->wh - HEIGHT(c)) / 2;
}
}
if (rule_ws->visible && rule_ws != stickyws)
arrange(rule_ws);
else
drawbar(rule_ws->mon);
/* If the client indicates that it is in fullscreen, or if the FullScreen flag has been
* explictly set via client rules, then enable fullscreen now. */
if (getatomprop(c, netatom[NetWMState], XA_ATOM) == netatom[NetWMFullscreen] || ISFULLSCREEN(c)) {
setflag(c, FullScreen, 0);
setfullscreen(c, 1, 0);
} else if (ISFLOATING(c)) {
raiseclient(c);
XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
savefloats(c);
}
updateclientdesktop(c);
updatewmhints(c);
updatemotifhints(c);
setfloatinghint(c);
if (SEMISCRATCHPAD(c) && c->scratchkey)
initsemiscratchpad(c);
if (SWITCHWORKSPACE(c) && !c->ws->visible)
viewwsonmon(c->ws, c->ws->mon, 0);
if (ISVISIBLE(c))
show(c);
else
hide(c);
return 1;
}
int
applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
{
int baseismin;
Monitor *m = c->ws->mon;
/* set minimum possible */
*w = MAX(1, *w);
*h = MAX(1, *h);
if (interact) {
if (*x > sw)
*x = sw - WIDTH(c);
if (*y > sh)
*y = sh - HEIGHT(c);
if (*x + *w + 2 * c->bw < 0)
*x = 0;
if (*y + *h + 2 * c->bw < 0)
*y = 0;
} else {
if (*x >= m->wx + m->ww)
*x = m->wx + m->ww - WIDTH(c);
if (*y >= m->wy + m->wh)
*y = m->wy + m->wh - HEIGHT(c);
if (*x + *w + 2 * c->bw <= m->wx)
*x = m->wx;
if (*y + *h + 2 * c->bw <= m->wy)
*y = m->wy;
}
if (*h < bh)
*h = bh;
if (*w < bh)
*w = bh;
if (!IGNORESIZEHINTS(c) && (enabled(ResizeHints) || RESPECTSIZEHINTS(c) || FREEFLOW(c))) {
if (REFRESHSIZEHINTS(c))
updatesizehints(c);
/* see last two sentences in ICCCM 4.1.2.3 */
baseismin = c->basew == c->minw && c->baseh == c->minh;
if (!baseismin) { /* temporarily remove base dimensions */
*w -= c->basew;
*h -= c->baseh;
}
/* adjust for aspect ratio limits */
if (c->mina > 0 && c->maxa > 0) {
if (c->maxa < (float)*w / *h)
*w = *h * c->maxa + 0.5;
else if (c->mina < (float)*h / *w)
*h = *w * c->mina + 0.5;
}
if (baseismin) { /* increment calculation requires this */
*w -= c->basew;
*h -= c->baseh;
}
/* adjust for increment value */
if (c->incw)
*w -= *w % c->incw;
if (c->inch)
*h -= *h % c->inch;
/* restore base dimensions */
*w = MAX(*w + c->basew, c->minw);
*h = MAX(*h + c->baseh, c->minh);
if (c->maxw)
*w = MIN(*w, c->maxw);
if (c->maxh)
*h = MIN(*h, c->maxh);
}
if (noborder(c, *x, *y, *w, *h))
addflag(c, NoBorder);
return *x != c->x || *y != c->y || *w != c->w || *h != c->h || NOBORDER(c);
}
void
arrange(Workspace *ws)
{
if (ws && !ws->visible)
return;
if (ws) {
arrangews(ws);
restack(ws);
drawbar(ws->mon);
return;
}
for (ws = workspaces; ws; ws = ws->next)
arrangews(ws);
drawbars();
}
void
arrangemon(Monitor *m)
{
Workspace *ws;
for (ws = workspaces; ws; ws = ws->next)
if (ws->mon == m)
arrangews(ws);
}
void
arrangews(Workspace *ws)
{
if (!ws->visible || !ws->layout->arrange || ws == stickyws)
return;
ws->layout->arrange(ws);
}
void
attach(Client *c)
{
c->next = c->ws->clients;
c->ws->clients = c;
}
void
attachstack(Client *c)
{
c->snext = c->ws->stack;
c->ws->stack = c;
}
void
buttonpress(XEvent *e)
{
int click, i, allow_focus;
Arg arg = {0};
Client *c;
Monitor *m;
Workspace *ws;
XButtonPressedEvent *ev = &e->xbutton;
click = ClkRootWin;
allow_focus = (disabled(FocusOnClick) || (ev->button != Button4 && ev->button != Button5));
/* focus monitor if necessary */
if ((m = wintomon(ev->window)) && m != selmon && allow_focus) {
ws = m->selws;
if (ws) {
unfocus(ws->sel, 1, NULL);
selws = ws;
}
selmon = m;
focus(NULL);
}
c = wintoclient(ev->window);
#ifdef HAVE_LIBXI
if (!c && cursor_hidden && enabled(BanishMouseCursor)) {
c = recttoclient(mouse_x, mouse_y, 1, 1, 1);
show_cursor(NULL);
}
#endif
if (c) {
if (allow_focus) {
focus(c);
if (ISSTICKY(c)) {
restack(stickyws);
} else {
restack(selws);
}
}
XAllowEvents(dpy, ReplayPointer, CurrentTime);
click = ClkClientWin;
}
if (click == ClkRootWin) {
barpress(ev, m, &arg, &click);
}
for (i = 0; i < LENGTH(buttons); i++) {
if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
&& CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
buttons[i].func((click == ClkWorkspaceBar || click == ClkWinTitle) && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
}
}
#ifdef HAVE_LIBXI
last_button_press = now();