-
Notifications
You must be signed in to change notification settings - Fork 2
/
dwm.c
4103 lines (3738 loc) · 112 KB
/
dwm.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.
*
* 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 dwm 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 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 and tagging rules are organized as arrays and defined in config.h.
*
* To understand everything else, start reading main().
*/
#include <errno.h>
#include <fcntl.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 <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#include <xcb/res.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"
/* macros */
#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
#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 ISVISIBLEONTAG(C, T) ((C->tags & T))
#define ISVISIBLE(C) ISVISIBLEONTAG(C, C->mon->tagset[C->mon->seltags])
#define LENGTH(X) (sizeof X / sizeof X[0])
#define MOUSEMASK (BUTTONMASK|PointerMotionMask)
#define MWM_HINTS_FLAGS_FIELD 0
#define MWM_HINTS_DECORATIONS_FIELD 2
#define MWM_HINTS_DECORATIONS (1 << 1)
#define MWM_DECOR_ALL (1 << 0)
#define MWM_DECOR_BORDER (1 << 1)
#define MWM_DECOR_TITLE (1 << 3)
#define NUMTAGS 9
#define RULE(...) { .monitor = -1, __VA_ARGS__ },
#define WTYPE "_NET_WM_WINDOW_TYPE_"
#define WIDTH(X) ((X)->w + 2 * (X)->bw)
#define HEIGHT(X) ((X)->h + 2 * (X)->bw)
#define TAGMASK ((1 << LENGTH(tags)) - 1)
#define TAGSLENGTH (LENGTH(tags))
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
#define TTEXTW(X) (drw_fontset_getwidth(drw, (X)))
#define STATUSLENGTH 256
#define DWMBLOCKSLOCKFILE "/var/local/dwmblocks/dwmblocks.pid"
#define DELIMITERENDCHAR 10
#define LSPAD (lrpad / 2) /* padding on left side of status text */
#define RSPAD (lrpad / 2) /* padding on right side of status text */
/* enums */
enum { CurResizeBR, CurResizeBL, CurResizeTR, CurResizeTL,
CurNormal, CurHand, CurResize, CurMove, CurLast }; /* cursor */
enum { SchemeNorm, SchemeSel, SchemeDarker,
SchemeRed, SchemeGreen, SchemeBlue,
SchemeCyan, SchemeMagenta, SchemeYellow,
SchemeBlack, SchemeWhite,
SchemeBrRed, SchemeBrGreen, SchemeBrBlue,
SchemeBrCyan, SchemeBrMagenta, SchemeBrYellow,
SchemeBrBlack, SchemeBrWhite, SchemeFloat, SchemeInactive, SchemeBar,
SchemeTag, SchemeTag1, SchemeTag2, SchemeTag3,
SchemeTag4, SchemeTag5, SchemeTag6, SchemeTag7,
SchemeTag8, SchemeTag9, SchemeLayout,
SchemeTitle, SchemeTitleFloat,
SchemeTitle1, SchemeTitle2, SchemeTitle3,
SchemeTitle4, SchemeTitle5, SchemeTitle6,
SchemeTitle7, SchemeTitle8, SchemeTitle9 }; /* color schemes */
enum { NetSupported, NetWMName, NetWMState, NetWMStateAbove, NetWMCheck,
NetWMFullscreen, NetActiveWindow, NetWMWindowType,
NetWMWindowTypeDialog, NetClientList, NetDesktopNames,
NetDesktopViewport, NetNumberOfDesktops, NetCurrentDesktop,
NetClientListStacking, NetLast }; /* EWMH atoms */
enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMWindowRole, WMLast }; /* default atoms */
enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
typedef union {
int i;
unsigned int ui;
float f;
const void *v;
} Arg;
typedef struct {
unsigned int click;
unsigned int mask;
unsigned int button;
void (*func)(const Arg *arg);
const Arg arg;
} Button;
typedef struct Monitor Monitor;
typedef struct Client Client;
struct Client {
char name[256];
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;
unsigned int tags;
int isfixed, isfloating, isfloatpos, isurgent, isfullscreen, isterminal;
int alwaysontop, neverfocus, oldstate, noswallow;
int ignoresizehints;
pid_t pid;
Client *next;
Client *snext;
Client *swallowing;
Monitor *mon;
Window win;
};
typedef struct {
unsigned int mod;
KeySym keysym;
void (*func)(const Arg *);
const Arg arg;
} Key;
typedef struct {
const char *symbol;
void (*arrange)(Monitor *);
} Layout;
typedef struct Pertag Pertag;
struct Monitor {
char ltsymbol[16];
float mfact;
int nmaster;
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 */
int pertaggap;
int edgegap;
int bargap;
int centertitle;
int colorfultitle;
int colorfultag;
int showindicator;
int showtitle;
int showvacanttags;
int showbar;
int topbar;
int statushandcursor;
int focusedontop;
unsigned int seltags;
unsigned int sellt;
unsigned int tagset[2];
unsigned int alttag;
Client *clients;
Client *sel;
Client *stack;
Monitor *next;
Pertag *pertag;
Window barwin;
const Layout *lt[2];
};
struct Pertag {
unsigned int curtag, prevtag; /* current and previous tag */
int nmasters[NUMTAGS + 1]; /* number of windows in master area */
float mfacts[NUMTAGS + 1]; /* mfacts per tag */
int showbars[NUMTAGS + 1]; /* display bar for the current tag */
Client *prevzooms[NUMTAGS + 1]; /* store zoom information */
int enablegaps[NUMTAGS + 1]; /* toggle gap for the current tag */
unsigned int gaps[NUMTAGS + 1]; /* gap value per tag */
unsigned int sellts[NUMTAGS + 1]; /* selected layouts */
const Layout *ltidxs[NUMTAGS + 1][2]; /* matrix of tags and layouts indexes */
};
typedef struct {
const char *class;
const char *role;
const char *instance;
const char *title;
const char *wintype;
unsigned int tags;
int isfloating;
int alwaysontop;
int isterminal;
int noswallow;
int matchonce;
const char *floatpos;
int monitor;
} Rule;
/* function declarations */
static void alwaysontop(const Arg *arg);
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 attach(Client *c);
static void attachbelow(Client *c);
static void attachstack(Client *c);
static void bstack(Monitor *m);
static void buttonpress(XEvent *e);
static void centeredfloatingmaster(Monitor *m);
static void centeredmaster(Monitor *m);
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 cyclelayout(const Arg *arg);
static void defaultgaps(const Arg *arg);
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 void dwindle(Monitor *m);
static void enternotify(XEvent *e);
static void expose(XEvent *e);
static Client *findbefore(Client *c);
static void floatpos(const Arg *arg);
static void focus(Client *c);
static void focusin(XEvent *e);
static void focusmaster(const Arg *arg);
static void focusmon(const Arg *arg);
static void focusstack(const Arg *arg);
static void gaplessgrid(Monitor *m);
static Atom getatomprop(Client *c, Atom prop);
static void getfacts(Monitor *m, int msize, int ssize,
float *mf, float *sf, int *mr, int *sr);
static void getfloatpos(int pos, char pCh, int size, char sCh,
int min_p, int max_s, int cp, int cs, int cbw,
int defgrid, int *out_p, int *out_s);
static void getgaps(Monitor *m, int *oh, int *ov,
int *ih, int *iv, unsigned int *nc);
static pid_t getparentprocess(pid_t p);
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 incnmaster(const Arg *arg);
static void incrgaps(const Arg *arg);
static void incrigaps(const Arg *arg);
static void incrihgaps(const Arg *arg);
static void incrivgaps(const Arg *arg);
static void incrogaps(const Arg *arg);
static void incrohgaps(const Arg *arg);
static void incrovgaps(const Arg *arg);
static void inplacerotate(const Arg *arg);
static void insertclient(Client *item, Client *insertItem, int after);
static int isdescprocess(pid_t p, pid_t c);
static void keypress(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 monocle(Monitor *m);
static void motionnotify(XEvent *e);
static void movemouse(const Arg *arg);
static Client *nexttagged(Client *c);
static Client *nexttiled(Client *c);
static void pop(Client *);
static void propertynotify(XEvent *e);
static void quit(const Arg *arg);
static Monitor *recttomon(int x, int y, int w, int h);
static void resetfact(const Arg *arg);
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 resizemouse(const Arg *arg);
static void restack(Monitor *m);
static void run(void);
static void scan(void);
static int sendevent(Client *c, Atom proto);
static void sendmon(Client *c, Monitor *m);
static void setcfact(const Arg *arg);
static void setfloatpos(Client *c, const char *floatpos);
static void setgaps(int oh, int ov, int ih, int iv);
static void setclientstate(Client *c, long state);
static void setcurrentdesktop(void);
static void setdesktopnames(void);
static void setfocus(Client *c);
static void setfullscreen(Client *c, int fullscreen);
static void setlayout(const Arg *arg);
static void setmfact(const Arg *arg);
static void setnumdesktops(void);
static void setup(void);
static void setviewport(void);
static void seturgent(Client *c, int urg);
static void shiftclient(const Arg *arg);
static void shiftview(const Arg *arg);
static void showhide(Client *c);
static void sigchld(int unused);
static void sigdwmblocks(const Arg *arg);
static void spawn(const Arg *arg);
static void swallow(Client *p, Client *c);
static Client *swallowingclient(Window w);
static void tag(const Arg *arg);
static void tagmon(const Arg *arg);
static void tagview(const Arg *arg);
static Client *termforwin(const Client *c);
static void tile(Monitor *);
static void togglealttag(const Arg *arg);
static void togglebar(const Arg *arg);
static void togglebargap(const Arg *arg);
static void togglefloating(const Arg *arg);
static void togglegaps(const Arg *arg);
static void toggleindicator(const Arg *arg);
static void toggletag(const Arg *arg);
static void toggletagcolor(const Arg *arg);
static void toggletaggaps(const Arg *arg);
static void toggletitle(const Arg *arg);
static void toggletitlecolor(const Arg *arg);
static void toggletitlepos(const Arg *arg);
static void togglevacanttag(const Arg *arg);
static void toggleview(const Arg *arg);
static void unfocus(Client *c, int setfocus);
static void unmanage(Client *c, int destroyed);
static void unmapnotify(XEvent *e);
static void unswallow(Client *c);
static void updatebarpos(Monitor *m);
static void updatebars(void);
static void updateclientlist(void);
static void updatecurrentdesktop(void);
static void updatedwmblockssig(int x);
static int updategeom(void);
static void updatemotifhints(Client *c);
static void updatenumlockmask(void);
static void updatesizehints(Client *c);
static void updatestatus(void);
static void updatetitle(Client *c);
static void updatewmhints(Client *c);
static void view(const Arg *arg);
static pid_t winpid(Window w);
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);
/* variables */
static const char broken[] = "broken";
static char stextc[STATUSLENGTH];
static char stexts[STATUSLENGTH];
static int screen;
static int sw, sh; /* X display screen geometry width, height */
static int bh, blw, ble; /* bar geometry */
static int wstext; /* width of status text */
static int lrpad; /* sum of left and right padding for text */
static int vp; /* vertical padding for bar */
static int sp; /* side padding for bar */
static int (*xerrorxlib)(Display *, XErrorEvent *);
static unsigned int dwmblockssig;
static unsigned int numlockmask = 0;
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ClientMessage] = clientmessage,
[ConfigureRequest] = configurerequest,
[ConfigureNotify] = configurenotify,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[Expose] = expose,
[FocusIn] = focusin,
[KeyPress] = keypress,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[MotionNotify] = motionnotify,
[PropertyNotify] = propertynotify,
[UnmapNotify] = unmapnotify
};
static Atom wmatom[WMLast], netatom[NetLast], motifatom;
static int running = 1;
static Cur *cursor[CurLast];
static Clr **scheme;
static Display *dpy;
static Drw *drw;
static Monitor *mons, *selmon;
static Window root, wmcheckwin;
static xcb_connection_t *xcon;
/* configuration, allows nested code to access above variables */
#include "config.h"
/* compile-time check if all tags fit into an unsigned int bit array. */
struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
/* function implementations */
void
alwaysontop(const Arg *arg)
{
selmon->focusedontop = !selmon->focusedontop;
arrangemon(selmon);
}
void
applyrules(Client *c)
{
const char *class, *instance;
Atom wintype;
char role[64];
unsigned int i;
const Rule *r;
Monitor *m;
XClassHint ch = { NULL, NULL };
/* rule matching */
c->isfloating = 0;
c->alwaysontop = 0;
c->isfloatpos = 0;
c->noswallow = 0;
c->tags = 0;
XGetClassHint(dpy, c->win, &ch);
class = ch.res_class ? ch.res_class : broken;
instance = ch.res_name ? ch.res_name : broken;
wintype = getatomprop(c, netatom[NetWMWindowType]);
gettextprop(c->win, wmatom[WMWindowRole], role, sizeof(role));
for (i = 0; i < LENGTH(rules); i++) {
r = &rules[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 || wintype == XInternAtom(dpy, r->wintype, 0)))
{
c->isterminal = r->isterminal;
c->noswallow = r->noswallow;
c->isfloating = r->isfloating;
c->alwaysontop = r->alwaysontop;
c->tags |= r->tags;
for (m = mons; m && m->num != r->monitor; m = m->next);
if (m)
c->mon = m;
if (r->floatpos) {
c->isfloating = 1;
setfloatpos(c, r->floatpos);
c->isfloatpos = 1;
}
if (r->matchonce)
break;
}
}
if (ch.res_class)
XFree(ch.res_class);
if (ch.res_name)
XFree(ch.res_name);
c->tags = c->tags & TAGMASK
? c->tags & TAGMASK
: c->mon->tagset[c->mon->seltags];
}
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 (!c->ignoresizehints
&& (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange))
{ /* 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[m->sellt]->symbol, sizeof m->ltsymbol);
if (m->lt[m->sellt]->arrange)
m->lt[m->sellt]->arrange(m);
}
void
attach(Client *c)
{
c->next = c->mon->clients;
c->mon->clients = c;
}
void
attachbelow(Client *c)
{
/* If there is nothing on the monitor or */
/* the selected client is floating, attach as normal */
if (c->mon->sel == NULL || c->mon->sel->isfloating) {
Client *at = nexttagged(c);
if (!at) {
attach(c);
return;
}
c->next = at->next;
at->next = c;
return;
}
/* Set the new client's next property to */
/* the same as the currently selected clients next */
c->next = c->mon->sel->next;
/* Set the currently selected clients next property to the new client */
c->mon->sel->next = c;
}
void
attachstack(Client *c)
{
c->snext = c->mon->stack;
c->mon->stack = c;
}
/*
* Bottomstack layout + gaps
* https://dwm.suckless.org/patches/bottomstack/
*/
static void
bstack(Monitor *m)
{
unsigned int i, n;
int oh, ov, ih, iv;
int mx = 0, my = 0, mh = 0, mw = 0;
int sx = 0, sy = 0, sh = 0, sw = 0;
float mfacts, sfacts;
int mrest, srest;
Client *c;
getgaps(m, &oh, &ov, &ih, &iv, &n);
if (n == 0)
return;
sx = mx = m->wx + ov;
sy = my = m->wy + oh;
sh = mh = m->wh - 2*oh;
mw = m->ww - 2*ov - iv * (MIN(n, m->nmaster) - 1);
sw = m->ww - 2*ov - iv * (n - m->nmaster - 1);
if (m->nmaster && n > m->nmaster) {
sh = (mh - ih) * (1 - m->mfact);
mh = mh - ih - sh;
sx = mx;
sy = my + mh + ih;
}
getfacts(m, mw, sw, &mfacts, &sfacts, &mrest, &srest);
for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
if (i < m->nmaster) {
resize( c, mx, my,
mw * (c->cfact / mfacts)
+ (i < mrest ? 1 : 0) - (2*c->bw),
mh - (2*c->bw), 0);
mx += WIDTH(c) + iv;
} else {
resize( c, sx, sy,
sw * (c->cfact / sfacts)
+ ((i - m->nmaster) < srest ? 1 : 0)
- (2*c->bw), sh - (2*c->bw), 0);
sx += WIDTH(c) + iv;
}
}
}
void
buttonpress(XEvent *e)
{
int i, x;
unsigned int click = 0, occ = 0;
Arg arg = {0};
Client *c;
Monitor *m;
XButtonPressedEvent *ev = &e->xbutton;
/* focus monitor if necessary */
if ((m = wintomon(ev->window)) && m != selmon) {
unfocus(selmon->sel, 1);
selmon = m;
focus(NULL);
}
if (ev->window == selmon->barwin) {
if (ev->x < ble - blw) {
for (c = m->clients;
!selmon->showvacanttags && c;
c = c->next)
occ |= c->tags == 255 ? 0 : c->tags;
i = x = 0;
do {
/* do not reserve space for vacant tags */
if (!selmon->showvacanttags) {
if (!(occ & 1 << i
|| m->tagset[m->seltags] & 1 << i))
continue;
}
x += TEXTW(tags[i]);
} while (ev->x >= x && ++i < LENGTH(tags));
if (i < LENGTH(tags)) {
click = ClkTagBar;
arg.ui = 1 << i;
}
} else if (ev->x < ble) {
click = ClkLtSymbol;
} else if (ev->x < selmon->ww - wstext) {
click = ClkWinTitle;
} else if ((x = selmon->ww - (2 * sp) - RSPAD - ev->x) > 0
&& (x -= wstext - LSPAD - RSPAD) <= 0) {
updatedwmblockssig(x);
click = ClkStatusText;
} else {
return;
}
} else if ((c = wintoclient(ev->window))) {
focus(c);
restack(selmon);
XAllowEvents(dpy, ReplayPointer, CurrentTime);
click = ClkClientWin;
} else {
click = ClkRootWin;
}
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);
}
/*
* Centred master layout + gaps
* https://dwm.suckless.org/patches/centeredmaster/
*/
void
centeredfloatingmaster(Monitor *m)
{
unsigned int i, n;
float mfacts, sfacts;
float mivf = 1.0; // master inner vertical gap factor
int oh, ov, ih, iv, mrest, srest;
int mx = 0, my = 0, mh = 0, mw = 0;
int sx = 0, sy = 0, sh = 0, sw = 0;
Client *c;
getgaps(m, &oh, &ov, &ih, &iv, &n);
if (n == 0)
return;
sx = mx = m->wx + ov;
sy = my = m->wy + oh;
sh = mh = m->wh - 2*oh;
mw = m->ww - 2*ov - iv*(n - 1);
sw = m->ww - 2*ov - iv*(n - m->nmaster - 1);
if (m->nmaster && n > m->nmaster) {
mivf = 0.8;
/* go mfact box in the center if more than nmaster clients */
if (m->ww > m->wh) {
mw = m->ww * m->mfact
- iv*mivf*(MIN(n, m->nmaster) - 1);
mh = m->wh * 0.9;
} else {
mw = m->ww * 0.9 - iv*mivf*(MIN(n, m->nmaster) - 1);
mh = m->wh * m->mfact;
}
mx = m->wx + (m->ww - mw) / 2;
my = m->wy + (m->wh - mh - 2*oh) / 2;
sx = m->wx + ov;
sy = m->wy + oh;
sh = m->wh - 2*oh;
}
getfacts(m, mw, sw, &mfacts, &sfacts, &mrest, &srest);
for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
if (i < m->nmaster) {
/* nmaster clients are stacked horizontally, in the center of the screen */
resize( c, mx, my,
mw * (c->cfact / mfacts)
+ (i < mrest ? 1 : 0)
- (2*c->bw), mh - (2*c->bw), 0);
mx += WIDTH(c) + iv*mivf;
} else {
/* stack clients are stacked horizontally */
resize( c, sx, sy,
sw * (c->cfact / sfacts)
+ ((i - m->nmaster) < srest ? 1 : 0)
- (2*c->bw), sh - (2*c->bw), 0);
sx += WIDTH(c) + iv;
}
}
void
centeredmaster(Monitor *m)
{
unsigned int i, n;
int oh, ov, ih, iv;
int mx = 0, my = 0, mh = 0, mw = 0;
int lx = 0, ly = 0, lw = 0, lh = 0;
int rx = 0, ry = 0, rw = 0, rh = 0;
float mfacts = 0, lfacts = 0, rfacts = 0;
int mtotal = 0, ltotal = 0, rtotal = 0;
int mrest = 0, lrest = 0, rrest = 0;
Client *c;
getgaps(m, &oh, &ov, &ih, &iv, &n);
if (n == 0)
return;
/* initialize areas */
mx = m->wx + ov;
my = m->wy + oh;
mh = m->wh - 2*oh - ih * ((!m->nmaster ? n : MIN(n, m->nmaster)) - 1);
mw = m->ww - 2*ov;
lh = m->wh - 2*oh - ih * (((n - m->nmaster) / 2) - 1);
rh = m->wh - 2*oh - ih
* (((n - m->nmaster) / 2) - ((n - m->nmaster) % 2 ? 0 : 1));
if (m->nmaster && n > m->nmaster) {
/* go mfact box in the center if more than nmaster clients */
if (n - m->nmaster > 1) {
/* ||<-S->|<---M--->|<-S->|| */
mw = (m->ww - 2*ov - 2*iv) * m->mfact;
lw = (m->ww - mw - 2*ov - 2*iv) / 2;
rw = (m->ww - mw - 2*ov - 2*iv) - lw;
mx += lw + iv;
} else {
/* ||<---M--->|<-S->|| */
mw = (mw - iv) * m->mfact;
lw = 0;
rw = m->ww - mw - iv - 2*ov;
}
lx = m->wx + ov;
ly = m->wy + oh;
rx = mx + mw + iv;
ry = m->wy + oh;
}
/* calculate facts */
for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
if (!m->nmaster || n < m->nmaster)
mfacts += c->cfact;
else if ((n - m->nmaster) % 2)
lfacts += c->cfact; // total factor of left hand stack area
else
rfacts += c->cfact; // total factor of right hand stack area
}
for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++)
if (!m->nmaster || n < m->nmaster)
mtotal += mh * (c->cfact / mfacts);
else if ((n - m->nmaster) % 2)
ltotal += lh * (c->cfact / lfacts);
else
rtotal += rh * (c->cfact / rfacts);
mrest = mh - mtotal;
lrest = lh - ltotal;
rrest = rh - rtotal;
for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
if (!m->nmaster || i < m->nmaster) {
/* nmaster clients are stacked vertically, in the center of the screen */
resize( c, mx, my, mw - (2*c->bw),
mh * (c->cfact / mfacts)
+ (i < mrest ? 1 : 0)
- (2*c->bw), 0);
my += HEIGHT(c) + ih;
} else {
/* stack clients are stacked vertically */
if ((i - m->nmaster) % 2 ) {
resize( c, lx, ly, lw - (2*c->bw),
lh * (c->cfact / lfacts)
+ ((i - 2*m->nmaster) < 2*lrest ? 1 : 0)
- (2*c->bw), 0);
ly += HEIGHT(c) + ih;
} else {
resize( c, rx, ry, rw - (2*c->bw),
rh * (c->cfact / rfacts)
+ ((i - 2*m->nmaster) < 2*rrest ? 1 : 0)
- (2*c->bw), 0);
ry += HEIGHT(c) + ih;
}
}
}
}
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)
{
Arg a = {.ui = ~0};
Layout foo = { "", NULL };
Monitor *m;
size_t i;
view(&a);
selmon->lt[selmon->sellt] = &foo;
for (m = mons; m; m = m->next)
while (m->stack)
unmanage(m->stack, 0);
XUngrabKey(dpy, AnyKey, AnyModifier, root);
while (mons)
cleanupmon(mons);
for (i = 0; i < CurLast; i++)
drw_cur_free(drw, cursor[i]);
for (i = 0; i < LENGTH(colors); i++)
free(scheme[i]);
free(scheme);
XDestroyWindow(dpy, wmcheckwin);
drw_free(drw);
XSync(dpy, 0);
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
}
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;
}
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);
unsigned int i;
if (!c)
return;
if (cme->message_type == netatom[NetWMState]) {
if (cme->data.l[1] == netatom[NetWMFullscreen]
|| cme->data.l[2] == netatom[NetWMFullscreen])
setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
|| (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */
&& !c->isfullscreen)));
else if (cme->data.l[1] == netatom[NetWMStateAbove]
|| cme->data.l[2] == netatom[NetWMStateAbove])
c->alwaysontop = (cme->data.l[0] || cme->data.l[1]);
} else if (cme->message_type == netatom[NetActiveWindow]) {
for (i = 0; i < LENGTH(tags) && !((1 << i) & c->tags); i++);
if (i < LENGTH(tags)) {
const Arg a = {.ui = 1 << i};
unfocus(selmon->sel, 0);
selmon = c->mon;
view(&a);
focus(c);
restack(selmon);
}
}
}
void
configure(Client *c)
{
XConfigureEvent ce;
ce.type = ConfigureNotify;
ce.display = dpy;
ce.event = c->win;
ce.window = c->win;
ce.x = c->x;
ce.y = c->y;
ce.width = c->w;
ce.height = c->h;
ce.border_width = c->bw;
ce.above = None;
ce.override_redirect = 0;
XSendEvent(dpy, c->win, 0, StructureNotifyMask, (XEvent *)&ce);
}
void
configurenotify(XEvent *e)
{
Monitor *m;