-
Notifications
You must be signed in to change notification settings - Fork 1
/
demwm.c
3751 lines (3361 loc) · 99.1 KB
/
demwm.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.
*
* 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 monitor, the focus history is remembered through a stack list
* on each monitor. Each client contains a bit array to indicate the tags of a
* client.
*
* Keys, Buttons and Rules are organized as arrays and defined in config.h.
*
* Most of dwm/demwm codebase is around handling X events, thus being a window
* manager. We access the X server by the good old Xlib library (demwm uses the
* xcb lib to get the pid, swallow patch). Whenever we need to handle an event,
* we call handler, array that contains the calls and function to handle them
* (how to handle them).
*
* Most of that X stuff you won't need to touch, unless you plan on
* refactoring, go to golf, logic hunting and all for the precious (most likely
* micro) optimizations. The functions that take the struct type Arg as an
* argument is the 'user functions', you can take a look how they do things.
*
* Every window that is spawned goes through by manage(), so it is another good
* start.
*
* To understand everything else, start reading main(), commit messages,
* www.x.org/docs/X11/xlib.pdf and the comments I left, happy hacking.
*/
#include <errno.h>
#include <locale.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/XF86keysym.h>
#include <X11/XKBlib.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
#include <X11/Xft/Xft.h>
#include <X11/Xlib-xcb.h>
#include <xcb/res.h>
#ifdef __OpenBSD__
#include <sys/sysctl.h>
#include <kvm.h>
#endif /* __OpenBSD__ */
#ifdef TAG_PREVIEW
#include <Imlib2.h>
#endif /* TAG_PREVIEW */
#ifdef ICONS
#include <limits.h>
#include <stdint.h>
#endif /* ICONS */
#include "drw.h"
#include "util.h"
/* macros */
#define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
#define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
* MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
#define ISVISIBLE(C) ((C->tags & C->mon->seltags) || C->f & Sticky)
#define LENGTH(X) (sizeof(X) / sizeof(X[0]))
#define WIDTH(X) ((X)->w + 2 * (X)->bw)
#define HEIGHT(X) ((X)->h + 2 * (X)->bw)
#define NUMTAGS (LENGTH(tags) + LENGTH(scratchpads) + 1)
//#define TAGMASK ((1 << LENGTH(tags)) - 1)
#define SCRATCHPAD_MASK (1 << (NUMTAGS - 1)) /* dynamic scratchpads */
#define TAGMASK ((1 << NUMTAGS) - 1)
#define SPTAG(i) ((1 << LENGTH(tags)) << (i))
#define SPTAGMASK (((1 << LENGTH(scratchpads)) - 1) << LENGTH(tags))
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
#define TTEXTW(X) (drw_fontset_getwidth(drw, (X)))
#define RULE(...) { .monitor = -1, __VA_ARGS__ },
#define SETVAL(X, flag, val) X->f = ((val) ? X->f | flag : X->f & ~flag)
#define LOG(...) do { fprintf(stderr, "demwm: " __VA_ARGS__); fputc('\n', stderr); } while (0)
#ifdef DEBUG
#define debug(...) do { fprintf(stderr, "demwm(debug): %s:\n", __func__); fprintf(stderr, "\t" __VA_ARGS__); } while (0)
#else
#define debug(...)
#endif /* DEBUG */
/* enums */
#ifdef SYSTRAY
enum { VERSION_MAJOR = 0, VERSION_MINOR = 0, XEMBED_MAPPED = (1 << 0),
XEMBED_EMBEDDED_VERSION = (VERSION_MAJOR << 16) | VERSION_MINOR, }; /* Xembed messages */
enum { Manager, Xembed, XembedInfo, XLast }; /* Xembed atoms */
#endif /* SYSTRAY */
enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
enum { SchemeNorm, SchemeSel, SchemeUrgent, SchemeLt, SchemeTitle,
SchemeStatus, SchemeDelim, SchemeSys, SchemeIndUrg, SchemeIndOff,
SchemeIndOn, BorderNorm, BorderSel, BorderFloat, BorderUrg, SchemeLast }; /* color schemes */
enum { Sp1, Sp2, Sp3, Sp4, Sp5, Sp6, Sp7, Sp8, Sp9, Sp10 }; /* scratchpads */
enum { NetWMName, NetClientList, NetWMState, NetWMFullscreen, NetActiveWindow,
NetWMWindowTypeDesktop, NetWMWindowType, NetWMStateAbove, NetWMPid,
#ifdef ICONS
NetWMIcon,
#endif /* ICONS */
#ifdef SYSTRAY
NetSystemTray, NetSystemTrayOP, NetSystemTrayOrientation,
NetSystemTrayVisual, NetWMWindowTypeDock, NetSystemTrayOrientationHorz,
#endif /* SYSTRAY */
NetLast }; /* EWMH atoms */
enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
enum { EMMons, EMFlags, EMTags, EMPosx, EMPosy, EMLast }; /* Explo atoms */
enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
enum { AlwOnTop = 1 << 0, /* AlwaysOnTop */
Float = 1 << 1,
Fixed = 1 << 2, /* same height and width */
FS = 1 << 3, /* FullScreen */
FSLock = 1 << 4, /* FullScreen bit lock (prev on oldstate) */
FakeFS = 1 << 5, /* FakeFullScreen */
HintsValid = 1 << 6, /* reduces calls to updatesizehints() */
NeverFocus = 1 << 7, /* XWMhints InputHint */
NoSwallow = 1 << 8, /* don't 'swallow' this child */
Sticky = 1 << 9, /* window shown in all tags */
Terminal = 1 << 10, /* 'swallow' child processes */
UnCursor = 1 << 11,
Urg = 1 << 12, /* urgent */
WasFloat = 1 << 13, /* oldstate: used in fullscreen operations */
LastFlag = 1 << 14, /* placeholder for the last flag */
}; /* client flags/state */
enum { HideVacant = 1 << 0, ShowBar = 1 << 1, TopBar = 1 << 2 }; /* mon flags */
enum { Alt = Mod1Mask,
AltGr = Mod3Mask,
Button6 = 6,
Button7 = 7,
Ctrl = ControlMask,
Shift = ShiftMask,
ShiftGr = Mod5Mask,
Super = Mod4Mask,
}; /* modifiers */
enum { BUTTONMASK = (ButtonPressMask|ButtonReleaseMask),
MOUSEMASK = (ButtonPressMask|ButtonReleaseMask|PointerMotionMask),
WINMASK = (CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask),
}; /* masks */
enum { INTa, UNIa, FLTa, NOOa }; /* function table args */
enum { IPCSIZE = 64, Solid = 0xffU }; /* magick numbers */
typedef union {
int i;
unsigned int ui;
float f;
const void *v;
} Arg;
typedef struct {
const unsigned int scheme;
const char *command;
const unsigned int interval;
const unsigned int signal;
} Block;
typedef struct {
const unsigned int click;
const unsigned int mask;
const unsigned int button;
void (*func)(const Arg *arg);
const Arg arg;
} Button;
typedef struct Client Client;
typedef struct Monitor Monitor;
typedef struct Pertag Pertag;
struct Client {
char name[256];
float mina, maxa;
float cfact;
int sfx, sfy, sfw, sfh; /* old float geometry */
int x, y, w, h;
int oldx, oldy, oldw, oldh;
int basew, baseh, incw, inch, maxw, maxh, minw, minh;
int bw, oldbw;
unsigned int tags;
unsigned int f; /* flags */
int fakefullscreen;
pid_t pid;
#ifdef ICONS
unsigned int icw, ich;
Picture icon;
#endif /* ICONS */
Client *next;
Client *snext;
Client *swallowing;
Monitor *mon;
Window win;
};
typedef struct {
const unsigned int mod;
const KeySym keysym;
void (*func)(const Arg *);
const Arg arg;
} Key;
typedef struct {
const char *symbol;
void (*arrange)(Monitor *);
const int gaps;
} Layout;
struct Monitor {
char ltsymbol[16];
float mfact;
int num;
int by; /* bar geometry */
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 */
unsigned int nmaster;
unsigned int f; /* flags */
unsigned int seltags, oldtags;
Client *clients;
Client *sel;
Client *stack;
Monitor *next;
Window barwin;
#ifdef TAG_PREVIEW
Window tagwin;
int previewshow;
Pixmap *tagmap;
#endif /* TAG_PREVIEW */
const Layout *lt;
Pertag *pertag;
};
typedef struct {
const char *class;
const char *instance;
const char *title;
const unsigned int tags;
const int monitor;
const unsigned int flags;
} Rule;
#ifdef SYSTRAY
typedef struct {
Window win;
Client *icons;
} Systray;
#endif /* SYSTRAY */
/* function declarations */
static void applyrules(Client *c);
static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
static void arrange(Monitor *m);
static void arrangemon(Monitor *m);
static void attachdefault(Client *c);
static void attachabove(Client *c);
static void attachstack(Client *c);
static void attachcycle(const Arg *arg);
static void attachwhere(const Arg *arg);
static void buttonpress(XEvent *e);
static void checkotherwm(void);
static void cleanup(void);
static void cleanupmon(Monitor *mon);
static void clientmessage(XEvent *e);
static void configure(Client *c);
static void configurenotify(XEvent *e);
static void configurerequest(XEvent *e);
static Monitor *createmon(void);
static void destroynotify(XEvent *e);
static void detach(Client *c);
static void detachstack(Client *c);
static Monitor *dirtomon(int dir);
static void drawbar(Monitor *m);
static void drawbars(void);
static int drawstatus(void);
static void enternotify(XEvent *e);
static void expose(XEvent *e);
static Client *findbefore(Client *c);
static void focus(Client *c);
static void focusin(XEvent *e);
static int getrootptr(int *x, int *y);
static long getstate(Window w);
#ifdef ICONS
static uint32_t prealpha(uint32_t p);
static void geticonprop(Client *c);
static void freeicon(Client *c);
static void updateicon(Client *c);
#endif /* ICONS */
static void getcmd(int i, char *button);
static void getcmds(int time);
static void getsigcmds(unsigned int signal);
static int gcd(int a, int b);
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 keypress(XEvent *e);
static void keyrelease(XEvent *e);
static void losefullscreen(Client *next);
static void readxresources(void);
static void manage(Window w, XWindowAttributes *wa);
static void mappingnotify(XEvent *e);
static void maprequest(XEvent *e);
static void motionnotify(XEvent *e);
static Client *nexttiled(Client *c);
static void pop(Client *c);
static Client *prevtiled(Client *c);
static void propertynotify(XEvent *e);
static Monitor *recttomon(int x, int y, int w, int h);
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 restack(Monitor *m);
static void run(void);
static void scan(void);
static void resizebarwin(Monitor *m);
#ifdef SYSTRAY
static Atom getatomprop(Client *c, Atom prop);
static unsigned long getatom(Window w, Atom prop, long size, Atom req, unsigned char **p);
static void removesystrayicon(Client *i);
static void resizerequest(XEvent *e);
static Monitor *systraytomon(Monitor *m);
static void updatesystray(void);
static void sendsystrayev(Window w, long code);
static void updatesystrayicongeom(Client *i, int w, int h);
static void updatesystrayiconstate(Client *i);
static Client *wintosystrayicon(Window w);
#endif /* SYSTRAY */
static int sendevent(Client *c, Atom proto);
static void sendmon(Client *c, Monitor *m);
static void setclientstate(Client *c, long state);
static void saveclientprop(Client *c);
static void setclientprop(Client *c);
static void setfocus(Client *c);
static void setfullscreen(Client *c, int fullscreen);
static void setup(void);
static void setsignal(int sig, void (*sahandler)(int sig));
static void seturgent(Client *c, int urg);
static void shift(unsigned int *tag, int i);
static void showhide(Client *c);
#ifdef TAG_PREVIEW
static void showtagpreview(unsigned int i);
static void getpreview(void);
#endif /* TAG_PREVIEW */
static void sigalrm(int unused);
static void unfocus(Client *c, int setfocus);
static void unmanage(Client *c, int destroyed);
static void unmapnotify(XEvent *e);
static void updatebarpos(Monitor *m);
static void updatebars(void);
static void updateclientlist(void);
static int updategeom(void);
static void updatenumlockmask(void);
static void updatesizehints(Client *c);
static void updatestatus(void);
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 scratchpad_show_client(Client *c);
static int scratchpad_last_showed_is_killed (void);
static void scratchpad_show_first(void);
/* vanitygaps */
static void getgaps(Monitor *m, int *oh, int *ov, int *ih, int *iv, unsigned int *nc);
static void getfacts(Monitor *m, int msize, int ssize, float *mf, float *sf, int *mr, int *sr);
static void setgaps(int oh, int ov, int ih, int iv);
static pid_t getparentprocess(pid_t p);
static int isdescprocess(pid_t p, pid_t c);
static Client *swallowingclient(Window w);
static Client *termforwin(const Client *c);
static void swallow(Client *p, Client *c);
static void unswallow(Client *c);
static pid_t winpid(Window w);
/* user functions */
static void combotag(const Arg *arg);
static void comboview(const Arg *arg);
static void cyclelayout(const Arg *arg);
static void focusmaster(const Arg *arg);
static void focusmon(const Arg *arg);
static void focusstack(const Arg *arg);
static void incnmaster(const Arg *arg);
static void killclient(const Arg *arg);
static void movemouse(const Arg *arg);
static void pushstack(const Arg *arg);
static void quit(const Arg *arg);
static void restart(const Arg *arg);
static void resizemouse(const Arg *arg);
static void resizemousescroll(const Arg *arg);
static void sendstatusbar(const Arg *arg);
static void setlayout(const Arg *arg);
static void setcfact(const Arg *arg);
static void setmfact(const Arg *arg);
static void shiftpreview(const Arg *arg);
static void shifttag(const Arg *arg);
static void shifttagclients(const Arg *arg);
static void shiftview(const Arg *arg);
static void shiftviewclients(const Arg *arg);
static void shiftboth(const Arg *arg);
static void shiftswaptags(const Arg *arg);
static void spawn(const Arg *arg);
static void tag(const Arg *arg);
static void tagmon(const Arg *arg);
static void togglebar(const Arg *arg);
static void toggletagbar(const Arg *arg);
static void togglefakefullscreen(const Arg *arg);
static void togglefloating(const Arg *arg);
static void togglefullscreen(const Arg *arg);
static void togglescratch(const Arg *arg);
static void toggletag(const Arg *arg);
static void toggleview(const Arg *arg);
static void togglesticky(const Arg *arg);
static void previewtag(const Arg *arg);
static void updateblock(const Arg *arg);
static void updateallblocks(const Arg *arg);
static void view(const Arg *arg);
static void xrdb(const Arg *arg);
static void xinitvisual(int screen);
static void zoom(const Arg *arg);
static void zoomswap(const Arg *arg);
static void scratchpad_hide(const Arg *arg);
static void scratchpad_remove(const Arg *arg);
static void scratchpad_show(const Arg *arg);
static void togglesmartgaps(const Arg *arg);
static void togglegaps(const Arg *arg);
static void defaultgaps(const Arg *arg);
static void incrgaps(const Arg *arg); /* inner + outter gaps */
static void incrigaps(const Arg *arg); /* inner gaps (horiz + vert) */
static void incrivgaps(const Arg *arg); /* vertical inner gaps */
static void incrihgaps(const Arg *arg); /* horizontal inner gaps */
static void incrogaps(const Arg *arg); /* outter gaps (horiz + vert) */
static void incrohgaps(const Arg *arg); /* vertcal outter gaps */
static void incrovgaps(const Arg *arg); /* horizontal outter gaps */
static void swapfocus(const Arg *arg);
/* Customs */
static void togglealwaysontop(const Arg *arg);
static void movefloathorz(const Arg *arg);
static void movefloatvert(const Arg *arg);
static void movfh_setmfact(const Arg *arg);
static void movfv_pushstack(const Arg *arg);
static void swaptags(const Arg *arg);
static void toggletopbar(const Arg *arg);
//static void toggleborder(const Arg *arg);
static void togglevacant(const Arg *arg);
static void togglestatus(const Arg *arg);
static const struct { const unsigned int type;
void (*func)(const Arg *arg); const char *name; } parsetable[] = {
{ UNIa, updateblock, "updateblock" },
{ NOOa, updateallblocks, "updateallblocks" },
{ INTa, cyclelayout, "cyclelayout" },
{ INTa, focusmon, "focusmon" },
{ INTa, focusstack, "focusstack" },
{ INTa, incnmaster, "incnmaster" },
{ INTa, incrgaps, "incrgaps" },
{ INTa, incrogaps, "incrogaps" },
{ INTa, incrohgaps, "incrohgaps" },
{ INTa, incrovgaps, "incrovgaps" },
{ INTa, incrigaps, "incrigaps" },
{ INTa, incrihgaps, "incrihgaps" },
{ INTa, incrivgaps, "incrivgaps" },
{ NOOa, defaultgaps, "defaultgaps" },
{ INTa, setlayout, "setlayout" },
{ INTa, movefloathorz, "movefloathorz" },
{ INTa, movefloatvert, "movefloatvert" },
{ INTa, pushstack, "pushstack" },
{ NOOa, scratchpad_hide, "scratchpad_hide" },
{ NOOa, scratchpad_remove, "scratchpad_remove" },
{ NOOa, scratchpad_show, "scratchpad_show" },
{ FLTa, setmfact, "setmfact" },
{ INTa, shiftboth, "shiftboth" },
{ INTa, shifttag, "shifttag" },
{ INTa, shifttagclients, "shifttagclients" },
{ INTa, shiftview, "shiftview" },
{ INTa, shiftviewclients, "shiftviewclients" },
{ UNIa, swaptags, "swaptags" },
{ UNIa, tag, "tag" },
{ INTa, tagmon, "tagmon" },
{ NOOa, togglealwaysontop, "togglealwaysontop" },
{ NOOa, togglebar, "togglebar" },
{ NOOa, toggletagbar, "toggletagbar" },
{ NOOa, togglefloating, "togglefloating" },
{ NOOa, togglefullscreen, "togglefullscreen" },
{ NOOa, togglefakefullscreen, "togglefakefullscreen" },
{ NOOa, togglegaps, "togglegaps" },
{ NOOa, togglesmartgaps, "togglesmartgaps" },
{ NOOa, togglevacant, "togglevacant" },
{ NOOa, togglestatus, "togglestatus" },
{ NOOa, toggletopbar, "toggletopbar" },
{ UNIa, toggletag, "toggletag" },
{ UNIa, toggleview, "toggleview" },
{ NOOa, togglesticky, "togglesticky" },
{ UNIa, view, "view" },
{ NOOa, xrdb, "xrdb" },
{ NOOa, zoom, "zoom" },
{ NOOa, zoomswap, "zoomswap" },
{ NOOa, killclient, "killclient" },
{ NOOa, restart, "restart" },
{ NOOa, quit, "quit" },
};
static void (*handler[LASTEvent])(XEvent *) = {
[ButtonPress] = buttonpress,
[ClientMessage] = clientmessage,
[ConfigureRequest] = configurerequest,
[ConfigureNotify] = configurenotify,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[Expose] = expose,
[FocusIn] = focusin,
[KeyRelease] = keyrelease,
[KeyPress] = keypress,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[MotionNotify] = motionnotify,
[PropertyNotify] = propertynotify,
#ifdef SYSTRAY
[ResizeRequest] = resizerequest,
#endif /* SYSTRAY */
[UnmapNotify] = unmapnotify
};
/* variables */
#ifdef SYSTRAY
static Atom xatom[XLast];
static Systray *systray = NULL;
static unsigned int sysw = 1; /* systray width */
#endif /* SYSTRAY */
static Atom wmatom[WMLast], netatom[NetLast];
static Cur *cursor[CurLast];
static Client *scratchpad_last_showed = NULL;
static Client *prevclient = NULL;
static Display *dpy;
static Drw *drw;
static Monitor *mons, *selmon;
static Window root, wmcheckwin;
static const char broken[] = "broken";
static unsigned int stsw = 0; /* status width */
static unsigned int blocknum; /* blocks idx in mouse click */
static int combo = 0; /* combo flag */
static int sw, sh; /* X display screen geometry width, height */
static int bh; /* bar height */
static int lrpad; /* sum of left and right padding for text */
static int (*xerrorxlib)(Display *, XErrorEvent *); /* x11 error func */
static unsigned int numlockmask = 0;
static unsigned int sleepinterval = 0, maxinterval = 0;
static int running = 0; /* -1 restart, 0 quit, 1 running */
static void (*attach)(Client *);
/* various layouts to use on the config */
#include "layouts.c"
/* configuration, allows nested code to access above variables */
#include "config.h"
static Clr *scheme[LENGTH(colors)] = {0};
static char blockoutput[LENGTH(blocks)][CMDLENGTH + 1] = {0}; /* +1 for '\0' */
static int pipes[LENGTH(blocks)][2] = {0};
static unsigned int execlock = 0; /* ensure only one child process exists per block at an instance */
struct Pertag {
unsigned int curtag, prevtag; /* current and previous tag */
unsigned int showbars; /* display bar for the current tag */
unsigned int enablegaps; /* display gaps for the current tag */
int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
const Layout *ltidxs[LENGTH(tags) + 1]; /* matrix of tags and layouts indexes */
Client *prevzooms[LENGTH(tags) + 1]; /* store zoom information */
unsigned int gaps[LENGTH(tags) + 1]; /* gaps per tag */
};
#include "cmds.c"
/* compile-time check if all tags fit into an unsigned int bit array. */
struct NumTags { char limitexceeded[NUMTAGS > 31 ? -1 : 1]; };
/* function implementations */
void
applyrules(Client *c)
{
const char *class, *instance;
const Rule *r;
Monitor *m;
XClassHint ch = { NULL, NULL };
/* rule matching */
c->tags = 0;
XGetClassHint(dpy, c->win, &ch);
class = ch.res_class ? ch.res_class : broken;
instance = ch.res_name ? ch.res_name : broken;
for (r = &rules[0]; r <= &rules[LENGTH(rules) - 1]; r++) {
if ((!r->title || strstr(c->name, r->title))
&& (!r->class || strstr(class, r->class))
&& (!r->instance || strstr(instance, r->instance)))
{
if (r->flags & FakeFS)
c->fakefullscreen = 1;
c->f |= (r->flags & ~FakeFS);
c->tags |= r->tags;
for (m = mons; m && m->num != r->monitor; m = m->next);
if (m)
c->mon = m;
}
}
c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : (c->mon->seltags & ~SPTAGMASK);
debug("client title: '%s'\n\tX resource class: '%s'\n\tX resource name: '%s'\n\tflags: '%u'\n\tmonitor: '%d'\n\ttags: '%u'\n", c->name, ch.res_class, ch.res_name, c->f, c->mon->num, c->tags);
if (ch.res_class)
XFree(ch.res_class);
if (ch.res_name)
XFree(ch.res_name);
}
int
applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
{
int baseismin;
Monitor *m = c->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 (resizehints || ((c->f & Float) && floathints) || !c->mon->lt->arrange) {
if (!(c->f & HintsValid))
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 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);
}
return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
}
void
arrange(Monitor *m)
{
if (m)
showhide(m->stack);
else for (m = mons; m; m = m->next)
showhide(m->stack);
if (m) {
arrangemon(m);
restack(m);
} else for (m = mons; m; m = m->next)
arrangemon(m);
}
void
arrangemon(Monitor *m)
{
strncpy(m->ltsymbol, m->lt->symbol, sizeof m->ltsymbol);
if (m->lt->arrange)
m->lt->arrange(m);
}
//void
//attach(Client *c)
//{
// c->next = c->mon->clients;
// c->mon->clients = c;
//}
void
attachdefault(Client *c)
{
c->next = c->mon->clients;
c->mon->clients = c;
}
void
attachabove(Client *c)
{
Client *at;
if (!(c->mon->sel == NULL || c->mon->sel == c->mon->clients || (c->f & Float))) {
for (at = c->mon->clients; at->next != c->mon->sel; at = at->next);
c->next = at->next;
at->next = c;
return;
}
attachdefault(c);
}
void
attachstack(Client *c)
{
c->snext = c->mon->stack;
c->mon->stack = c;
}
void
swallow(Client *p, Client *c)
{
XWindowChanges wc;
Window tmp;
if ((c->f & (Terminal | NoSwallow))
|| ((!swallowfloat && c->f & Float) && !(c->f & FS))
|| (!swallowffs && c->f & FS))
return;
detach(c);
detachstack(c);
setclientstate(c, WithdrawnState);
XUnmapWindow(dpy, p->win);
p->swallowing = c;
c->mon = p->mon;
tmp = p->win;
p->win = c->win;
c->win = tmp;
#ifdef ICONS
updateicon(p);
#endif /* ICONS */
updatetitle(p);
wc.border_width = p->bw;
XConfigureWindow(dpy, p->win, CWBorderWidth, &wc);
XMoveResizeWindow(dpy, p->win, p->x, p->y, p->w, p->h);
XSetWindowBorder(dpy, p->win, scheme[p->f & Float ? BorderFloat : BorderSel][ColFg].pixel);
configure(p);
updateclientlist();
}
void
unswallow(Client *c)
{
XWindowChanges wc;
c->win = c->swallowing->win;
free(c->swallowing);
c->swallowing = NULL;
/* unfullscreen the client */
setfullscreen(c, 0);
#ifdef ICONS
freeicon(c);
#endif /* ICONS */
updatetitle(c);
XMapWindow(dpy, c->win);
wc.border_width = c->bw;
XConfigureWindow(dpy, c->win, CWBorderWidth, &wc);
XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
XSetWindowBorder(dpy, c->win, scheme[c->f & Float ? BorderFloat : BorderSel][ColFg].pixel);
configure(c);
setclientstate(c, NormalState);
focus(NULL);
arrange(c->mon);
}
void
buttonpress(XEvent *e)
{
unsigned int i, x, click, len, occ = 0;
Arg arg = {0};
Client *c;
Monitor *m;
XButtonPressedEvent *ev = &e->xbutton;
click = ClkRootWin;
/* focus monitor if necessary */
if ((m = wintomon(ev->window)) && m != selmon) {
unfocus(selmon->sel, 1);
selmon = m;
focus(NULL);
}
if (ev->window == selmon->barwin) {
i = x = 0;
for (c = m->clients; c; c = c->next)
occ |= c->tags == 255 ? 0 : c->tags;
do {
/* do not reserve space for vacant tags */
if (!(occ & 1 << i || m->seltags & 1 << i) && m->f & HideVacant)
continue;
x += TEXTW(m->f & HideVacant ? tagsalt[i] : tags[i]);
} while (ev->x >= x && ++i < LENGTH(tags));
if (i < LENGTH(tags)) {
click = ClkTagBar;
arg.ui = 1 << i;
#ifdef TAG_PREVIEW
/* hide preview if we click the bar */
if (selmon->previewshow) {
selmon->previewshow = 0;
XUnmapWindow(dpy, selmon->tagwin);
}
#endif /* TAG_PREVIEW */
} else if (ev->x < x + TEXTW(selmon->ltsymbol))
click = ClkLtSymbol;
else if (ev->x > (x = selmon->ww - stsw
#ifdef SYSTRAY
- sysw
#endif /* SYSTRAY */
)) {
click = ClkStatusText;
#if INVERSED
for (i = LENGTH(blocks) - 1; i >= 0; i--)
#else
for (i = 0; i < LENGTH(blocks); i++)
#endif /* INVERSED */
{
if (*blockoutput[i] == '\0') /* empty string, ignore */
continue;
len = TTEXTW(blockoutput[i]) + TTEXTW(delimiter);
x += len;
if (ev->x <= x && ev->x >= x - len) { /* if the mouse is between the block area */
blocknum = i; /* store what block the mouse is clicking */
break;
}
}
} else
click = ClkWinTitle;
} else if ((c = wintoclient(ev->window))) {
/* scrolling doesn't trigger focus */
if (ev->button != Button4 && ev->button != Button5)
focus(c);
restack(selmon);
XAllowEvents(dpy, ReplayPointer, CurrentTime);
click = ClkClientWin;
}
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 == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
}
void
checkotherwm(void)
{
xerrorxlib = XSetErrorHandler(xerrorstart);
/* this causes an error if some other window manager is running */
XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
XSync(dpy, False);
XSetErrorHandler(xerror);
XSync(dpy, False);
}
void
cleanup(void)
{
Monitor *m;
size_t i;
for (i = 0; i < LENGTH(blocks); i++) { /* close the pipes */
close(pipes[i][0]);
close(pipes[i][1]);
}
for (m = mons; m; m = m->next)
while (m->stack) {
if (running == -1)
saveclientprop(m->stack);
unmanage(m->stack, 0);
}
XUngrabKey(dpy, AnyKey, AnyModifier, root);
while (mons)
cleanupmon(mons);
#ifdef SYSTRAY
if (systray) {
while (systray->icons)
removesystrayicon(systray->icons);
if (systray->win) {
XUnmapWindow(dpy, systray->win);
XDestroyWindow(dpy, systray->win);
}
free(systray);
}
#endif /* SYSTRAY */
for (i = 0; i < CurLast; i++)
drw_cur_free(drw, cursor[i]);
for (i = 0; i < LENGTH(colors); i++)
free(scheme[i]);
XDestroyWindow(dpy, wmcheckwin);
drw_free(drw);
XSync(dpy, False);
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
XCloseDisplay(dpy);
}
void
cleanupmon(Monitor *mon)
{
Monitor *m;
if (mon == mons)
mons = mons->next;
else {
for (m = mons; m && m->next != mon; m = m->next);
m->next = mon->next;
}
#ifdef TAG_PREVIEW
for (size_t i = 0; i < LENGTH(tags); i++)
if (mon->tagmap[i])
XFreePixmap(dpy, mon->tagmap[i]);
free(mon->tagmap);
XUnmapWindow(dpy, mon->tagwin);
XDestroyWindow(dpy, mon->tagwin);
#endif /* TAG_PREVIEW */
XUnmapWindow(dpy, mon->barwin);
XDestroyWindow(dpy, mon->barwin);
free(mon->pertag);
free(mon);
}
void
clientmessage(XEvent *e)
{
XClientMessageEvent *cme = &e->xclient;
Client *c = wintoclient(cme->window);
#ifdef SYSTRAY
XWindowAttributes wa;
XSetWindowAttributes swa;
if (systray
&& cme->window == systray->win
&& cme->message_type == netatom[NetSystemTrayOP]
&& cme->data.l[1] == 0 /* SYSTEM_TRAY_REQUEST_DOCK */
&& cme->data.l[2]) {
/* do our little manage() like for the systray window */
c = ecalloc(1, sizeof(Client));
c->win = cme->data.l[2];
c->mon = selmon;
c->next = systray->icons;
systray->icons = c;
XGetWindowAttributes(dpy, c->win, &wa);
c->x = c->sfx = c->oldx = c->y = c->sfy = c->oldy = 0;