-
Notifications
You must be signed in to change notification settings - Fork 8
/
echinus.c
2616 lines (2374 loc) · 61 KB
/
echinus.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.
*
* echinus 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 echinus 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 global
* doubly-linked client list, the focus history is remembered through a global
* stack list. Each client contains an array of Bools of the same size as the
* global tags 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 <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <regex.h>
#include <signal.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/XF86keysym.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/Xft/Xft.h>
#ifdef XRANDR
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/randr.h>
#endif
#include "echinus.h"
/* macros */
#define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
#define MOUSEMASK (BUTTONMASK | PointerMotionMask)
#define CLIENTMASK (PropertyChangeMask | StructureNotifyMask | FocusChangeMask)
#define CLIENTNOPROPAGATEMASK (BUTTONMASK | ButtonMotionMask)
#define FRAMEMASK (MOUSEMASK | SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask)
/* enums */
enum { StrutsOn, StrutsOff, StrutsHide }; /* struts position */
enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
enum { Clk2Focus, SloppyFloat, AllSloppy, SloppyRaise }; /* focus model */
/* function declarations */
void applyatoms(Client * c);
void applyrules(Client * c);
void arrange(Monitor * m);
void attach(Client * c, Bool attachaside);
void attachstack(Client * c);
void ban(Client * c);
void buttonpress(XEvent * e);
void bstack(Monitor * m);
void checkotherwm(void);
void cleanup(void);
void compileregs(void);
void configure(Client * c);
void configurenotify(XEvent * e);
void configurerequest(XEvent * e);
void destroynotify(XEvent * e);
void detach(Client * c);
void detachstack(Client * c);
void *emallocz(unsigned int size);
void enternotify(XEvent * e);
void eprint(const char *errstr, ...);
void expose(XEvent * e);
void iconify(const char *arg);
void incnmaster(const char *arg);
void focus(Client * c);
void focusnext(const char *arg);
void focusprev(const char *arg);
Client *getclient(Window w, Client * list, int part);
const char *getresource(const char *resource, const char *defval);
long getstate(Window w);
Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
void getpointer(int *x, int *y);
Monitor *getmonitor(int x, int y);
Monitor *curmonitor();
Monitor *clientmonitor(Client * c);
int idxoftag(const char *tag);
Bool isvisible(Client * c, Monitor * m);
void initmonitors(XEvent * e);
void keypress(XEvent * e);
void killclient(const char *arg);
void leavenotify(XEvent * e);
void focusin(XEvent * e);
void manage(Window w, XWindowAttributes * wa);
void mappingnotify(XEvent * e);
void monocle(Monitor * m);
void maprequest(XEvent * e);
void mousemove(Client * c);
void mouseresize(Client * c);
void moveresizekb(const char *arg);
Client *nexttiled(Client * c, Monitor * m);
Client *prevtiled(Client * c, Monitor * m);
void place(Client *c);
void propertynotify(XEvent * e);
void reparentnotify(XEvent * e);
void quit(const char *arg);
void restart(const char *arg);
void resize(Client * c, int x, int y, int w, int h, Bool sizehints);
void restack(Monitor * m);
void run(void);
void save(Client * c);
void scan(void);
void setclientstate(Client * c, long state);
void setlayout(const char *arg);
void setmwfact(const char *arg);
void setup(char *);
void spawn(const char *arg);
void tag(const char *arg);
void tile(Monitor * m);
void togglestruts(const char *arg);
void togglefloating(const char *arg);
void togglemax(const char *arg);
void togglefill(const char *arg);
void toggletag(const char *arg);
void toggleview(const char *arg);
void togglemonitor(const char *arg);
void focusview(const char *arg);
void unban(Client * c);
void unmanage(Client * c);
void updategeom(Monitor * m);
void updatestruts(Monitor * m);
void unmapnotify(XEvent * e);
void updatesizehints(Client * c);
void updateframe(Client * c);
void updatetitle(Client * c);
void view(const char *arg);
void viewprevtag(const char *arg); /* views previous selected tags */
void viewlefttag(const char *arg);
void viewrighttag(const char *arg);
int xerror(Display * dpy, XErrorEvent * ee);
int xerrordummy(Display * dsply, XErrorEvent * ee);
int xerrorstart(Display * dsply, XErrorEvent * ee);
int (*xerrorxlib) (Display *, XErrorEvent *);
void zoom(const char *arg);
/* variables */
char **cargv;
Display *dpy;
int screen;
Window root;
XrmDatabase xrdb;
Bool otherwm;
Bool running = True;
Bool selscreen = True;
Monitor *monitors;
Client *clients;
Client *sel;
Client *stack;
Cursor cursor[CurLast];
Style style;
Button button[LastBtn];
View *views;
Key **keys;
Rule **rules;
char **tags;
unsigned int ntags;
unsigned int nkeys;
unsigned int nrules;
unsigned int modkey;
unsigned int numlockmask;
/* configuration, allows nested code to access above variables */
#include "config.h"
struct {
Bool attachaside;
Bool dectiled;
Bool hidebastards;
int focus;
int gap;
int snap;
char command[255];
} options;
Layout layouts[] = {
/* function symbol features */
{ NULL, 'i', OVERLAP },
{ tile, 't', MWFACT | NMASTER | ZOOM },
{ bstack, 'b', MWFACT | ZOOM },
{ monocle, 'm', 0 },
{ NULL, 'f', OVERLAP },
{ NULL, '\0', 0 },
};
void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ButtonRelease] = buttonpress,
[ConfigureRequest] = configurerequest,
[ConfigureNotify] = configurenotify,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[LeaveNotify] = leavenotify,
[FocusIn] = focusin,
[Expose] = expose,
[KeyPress] = keypress,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[PropertyNotify] = propertynotify,
[ReparentNotify] = reparentnotify,
[UnmapNotify] = unmapnotify,
[ClientMessage] = clientmessage,
#ifdef XRANDR
[RRScreenChangeNotify] = initmonitors,
#endif
};
/* function implementations */
void
applyatoms(Client * c) {
unsigned int *t;
unsigned long n;
int i;
/* restore tag number from atom */
t = (unsigned int*)getatom(c->win, atom[WindowDesk], &n);
if (n != 0) {
if (*t >= ntags)
return;
for (i = 0; i < ntags; i++)
c->tags[i] = (i == *t) ? 1 : 0;
}
}
void
applyrules(Client * c) {
static char buf[512];
unsigned int i, j;
regmatch_t tmp;
Bool matched = False;
XClassHint ch = { 0 };
/* rule matching */
XGetClassHint(dpy, c->win, &ch);
snprintf(buf, sizeof(buf), "%s:%s:%s",
ch.res_class ? ch.res_class : "", ch.res_name ? ch.res_name : "", c->name);
buf[LENGTH(buf)-1] = 0;
for (i = 0; i < nrules; i++)
if (rules[i]->propregex && !regexec(rules[i]->propregex, buf, 1, &tmp, 0)) {
c->isfloating = rules[i]->isfloating;
c->title = rules[i]->hastitle;
for (j = 0; rules[i]->tagregex && j < ntags; j++) {
if (!regexec(rules[i]->tagregex, tags[j], 1, &tmp, 0)) {
matched = True;
c->tags[j] = True;
}
}
}
if (ch.res_class)
XFree(ch.res_class);
if (ch.res_name)
XFree(ch.res_name);
if (!matched)
memcpy(c->tags, curseltags, ntags * sizeof(curseltags[0]));
}
void
arrangefloats(Monitor * m) {
Client *c;
Monitor *om;
int dx, dy;
for (c = stack; c; c = c->snext) {
if (isvisible(c, m) && !c->isbastard &&
(c->isfloating || MFEATURES(m, OVERLAP))
&& !c->ismax && !c->isicon) {
DPRINTF("%d %d\n", c->rx, c->ry);
if (!(om = getmonitor(c->rx + c->rw/2,
c->ry + c->rh/2)))
continue;
dx = om->sx + om->sw - c->rx;
dy = om->sy + om->sh - c->ry;
if (dx > m->sw)
dx = m->sw;
if (dy > m->sh)
dy = m->sh;
resize(c, m->sx + m->sw - dx, m->sy + m->sh - dy, c->rw, c->rh, True);
save(c);
}
}
}
void
arrangemon(Monitor * m) {
Client *c;
if (views[m->curtag].layout->arrange)
views[m->curtag].layout->arrange(m);
arrangefloats(m);
restack(m);
for (c = stack; c; c = c->snext) {
if ((clientmonitor(c) == m) && ((!c->isbastard && !c->isicon) ||
(c->isbastard && views[m->curtag].barpos == StrutsOn))) {
unban(c);
}
}
for (c = stack; c; c = c->snext) {
if ((clientmonitor(c) == NULL) || (!c->isbastard && c->isicon) ||
(c->isbastard && views[m->curtag].barpos == StrutsHide)) {
ban(c);
}
}
}
void
arrange(Monitor * m) {
Monitor *i;
if (!m) {
for (i = monitors; i; i = i->next)
arrangemon(i);
} else
arrangemon(m);
}
void
attach(Client * c, Bool attachaside) {
if (attachaside) {
if (clients) {
Client * lastClient = clients;
while (lastClient->next)
lastClient = lastClient->next;
c->prev = lastClient;
lastClient->next = c;
}
else
clients = c;
}
else {
if (clients)
clients->prev = c;
c->next = clients;
clients = c;
}
}
void
attachstack(Client * c) {
c->snext = stack;
stack = c;
}
void
ban(Client * c) {
if (c->isbanned)
return;
c->ignoreunmap++;
setclientstate(c, IconicState);
XSelectInput(dpy, c->win, CLIENTMASK & ~(StructureNotifyMask | EnterWindowMask));
XSelectInput(dpy, c->frame, NoEventMask);
XUnmapWindow(dpy, c->frame);
XUnmapWindow(dpy, c->win);
XSelectInput(dpy, c->win, CLIENTMASK);
XSelectInput(dpy, c->frame, FRAMEMASK);
c->isbanned = True;
}
void
buttonpress(XEvent * e) {
Client *c;
int i;
XButtonPressedEvent *ev = &e->xbutton;
if (!curmonitor())
return;
if (ev->window == root) {
if (ev->type != ButtonRelease)
return;
switch (ev->button) {
case Button3:
spawn(options.command);
break;
case Button4:
viewlefttag(NULL);
break;
case Button5:
viewrighttag(NULL);
break;
}
return;
}
if ((c = getclient(ev->window, clients, ClientTitle))) {
DPRINTF("TITLE %s: 0x%x\n", c->name, (int) ev->window);
focus(c);
for (i = 0; i < LastBtn; i++) {
if (button[i].action == NULL)
continue;
if ((ev->x > button[i].x)
&& ((int)ev->x < (int)(button[i].x + style.titleheight))
&& (button[i].x != -1) && (int)ev->y < style.titleheight) {
if (ev->type == ButtonPress) {
DPRINTF("BUTTON %d PRESSED\n", i);
button[i].pressed = 1;
} else {
DPRINTF("BUTTON %d RELEASED\n", i);
button[i].pressed = 0;
button[i].action(NULL);
}
drawclient(c);
return;
}
}
for (i = 0; i < LastBtn; i++)
button[i].pressed = 0;
drawclient(c);
if (ev->type == ButtonRelease)
return;
restack(curmonitor());
if (ev->button == Button1)
mousemove(c);
else if (ev->button == Button3)
mouseresize(c);
} else if ((c = getclient(ev->window, clients, ClientWindow))) {
DPRINTF("WINDOW %s: 0x%x\n", c->name, (int) ev->window);
focus(c);
restack(curmonitor());
if (CLEANMASK(ev->state) != modkey) {
XAllowEvents(dpy, ReplayPointer, CurrentTime);
return;
}
if (ev->button == Button1) {
if (!FEATURES(curlayout, OVERLAP) && !c->isfloating)
togglefloating(NULL);
if (c->ismax)
togglemax(NULL);
mousemove(c);
} else if (ev->button == Button2) {
if (!FEATURES(curlayout, OVERLAP) && c->isfloating)
togglefloating(NULL);
else
zoom(NULL);
} else if (ev->button == Button3) {
if (!FEATURES(curlayout, OVERLAP) && !c->isfloating)
togglefloating(NULL);
if (c->ismax)
togglemax(NULL);
mouseresize(c);
}
} else if ((c = getclient(ev->window, clients, ClientFrame))) {
DPRINTF("FRAME %s: 0x%x\n", c->name, (int) ev->window);
/* Not supposed to happen */
}
}
void
checkotherwm(void) {
otherwm = False;
XSetErrorHandler(xerrorstart);
/* this causes an error if some other window manager is running */
XSelectInput(dpy, root, SubstructureRedirectMask);
XSync(dpy, False);
if (otherwm)
eprint("echinus: another window manager is already running\n");
XSync(dpy, False);
XSetErrorHandler(NULL);
xerrorxlib = XSetErrorHandler(xerror);
XSync(dpy, False);
}
void
cleanup(void) {
while (stack) {
unban(stack);
unmanage(stack);
}
free(tags);
free(keys);
initmonitors(NULL);
/* free resource database */
XrmDestroyDatabase(xrdb);
deinitstyle();
XUngrabKey(dpy, AnyKey, AnyModifier, root);
XFreeCursor(dpy, cursor[CurNormal]);
XFreeCursor(dpy, cursor[CurResize]);
XFreeCursor(dpy, cursor[CurMove]);
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
XSync(dpy, False);
}
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 - c->th;
ce.border_width = 0;
ce.above = None;
ce.override_redirect = False;
XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *) & ce);
}
void
configurenotify(XEvent * e) {
XConfigureEvent *ev = &e->xconfigure;
Monitor *m;
Client *c;
if (ev->window == root) {
#ifdef XRANDR
if (XRRUpdateConfiguration((XEvent *) ev)) {
#endif
initmonitors(e);
for (c = clients; c; c = c->next) {
if (c->isbastard) {
m = getmonitor(c->x + c->w/2, c->y);
c->tags = m->seltags;
updatestruts(m);
}
}
for (m = monitors; m; m = m->next)
updategeom(m);
arrange(NULL);
#ifdef XRANDR
}
#endif
}
}
void
configurerequest(XEvent * e) {
Client *c;
XConfigureRequestEvent *ev = &e->xconfigurerequest;
XWindowChanges wc;
Monitor *cm;
int x, y, w, h;
if ((c = getclient(ev->window, clients, ClientWindow))) {
cm = clientmonitor(c);
if (ev->value_mask & CWBorderWidth)
c->border = ev->border_width;
if (c->isfixed || c->isfloating || MFEATURES(clientmonitor(c), OVERLAP)) {
if (ev->value_mask & CWX)
x = ev->x;
if (ev->value_mask & CWY)
y = ev->y;
if (ev->value_mask & CWWidth)
w = ev->width;
if (ev->value_mask & CWHeight)
h = ev->height + c->th;
cm = getmonitor(x, y);
if (!(ev->value_mask & (CWX | CWY)) /* resize request */
&& (ev->value_mask & (CWWidth | CWHeight))) {
DPRINTF("RESIZE %s (%d,%d)->(%d,%d)\n", c->name, c->w, c->h, w, h);
resize(c, c->x, c->y, w, h, True);
save(c);
} else if ((ev->value_mask & (CWX | CWY)) /* move request */
&& !(ev->value_mask & (CWWidth | CWHeight))) {
DPRINTF("MOVE %s (%d,%d)->(%d,%d)\n", c->name, c->x, c->y, x, y);
resize(c, x, y, c->w, c->h, True);
save(c);
} else if ((ev->value_mask & (CWX | CWY)) /* move and resize request */
&& (ev->value_mask & (CWWidth | CWHeight))) {
DPRINTF("MOVE&RESIZE(MOVE) %s (%d,%d)->(%d,%d)\n", c->name, c->x, c->y, ev->x, ev->y);
DPRINTF("MOVE&RESIZE(RESIZE) %s (%d,%d)->(%d,%d)\n", c->name, c->w, c->h, ev->width, ev->height);
resize(c, x, y, w, h, True);
save(c);
} else if ((ev->value_mask & CWStackMode)) {
DPRINTF("RESTACK %s ignoring\n", c->name);
configure(c);
}
} else {
configure(c);
}
} else {
wc.x = ev->x;
wc.y = ev->y;
wc.width = ev->width;
wc.height = ev->height;
wc.border_width = ev->border_width;
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
}
XSync(dpy, False);
}
void
destroynotify(XEvent * e) {
Client *c;
XDestroyWindowEvent *ev = &e->xdestroywindow;
if (!(c = getclient(ev->window, clients, ClientWindow)))
return;
unmanage(c);
updateatom[ClientList] (NULL);
}
void
detach(Client * c) {
if (c->prev)
c->prev->next = c->next;
if (c->next)
c->next->prev = c->prev;
if (c == clients)
clients = c->next;
c->next = c->prev = NULL;
}
void
detachstack(Client * c) {
Client **tc;
for (tc = &stack; *tc && *tc != c; tc = &(*tc)->snext);
*tc = c->snext;
}
void *
emallocz(unsigned int size) {
void *res = calloc(1, size);
if (!res)
eprint("fatal: could not malloc() %u bytes\n", size);
return res;
}
void
enternotify(XEvent * e) {
XCrossingEvent *ev = &e->xcrossing;
Client *c;
if (ev->mode != NotifyNormal || ev->detail == NotifyInferior)
return;
if (!curmonitor())
return;
if ((c = getclient(ev->window, clients, ClientFrame))) {
if (c->isbastard)
return;
/* focus when switching monitors */
if (!isvisible(sel, curmonitor()))
focus(c);
switch (options.focus) {
case Clk2Focus:
break;
case SloppyFloat:
if (FEATURES(curlayout, OVERLAP) || c->isfloating)
focus(c);
break;
case AllSloppy:
focus(c);
break;
case SloppyRaise:
focus(c);
restack(curmonitor());
break;
}
} else if (ev->window == root) {
selscreen = True;
focus(NULL);
}
}
void
eprint(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
void
focusin(XEvent * e) {
XFocusChangeEvent *ev = &e->xfocus;
Client *c;
if (sel && ((c = getclient(ev->window, clients, ClientWindow)) != sel))
XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
else if (!c)
fprintf(stderr, "Caught FOCUSIN for unknown window 0x%x\n", ev->window);
}
void
expose(XEvent * e) {
XExposeEvent *ev = &e->xexpose;
XEvent tmp;
Client *c;
while (XCheckWindowEvent(dpy, ev->window, ExposureMask, &tmp));
if ((c = getclient(ev->window, clients, ClientTitle)))
drawclient(c);
}
void
givefocus(Client * c) {
XEvent ce;
if (checkatom(c->win, atom[WMProto], atom[WMTakeFocus])) {
ce.xclient.type = ClientMessage;
ce.xclient.message_type = atom[WMProto];
ce.xclient.display = dpy;
ce.xclient.window = c->win;
ce.xclient.format = 32;
ce.xclient.data.l[0] = atom[WMTakeFocus];
ce.xclient.data.l[1] = CurrentTime; /* incorrect */
ce.xclient.data.l[2] = 0l;
ce.xclient.data.l[3] = 0l;
ce.xclient.data.l[4] = 0l;
XSendEvent(dpy, c->win, False, NoEventMask, &ce);
}
}
void
focus(Client * c) {
Client *o;
o = sel;
if ((!c && selscreen) || (c && (c->isbastard || !isvisible(c, curmonitor()))))
for (c = stack;
c && (c->isbastard || c->isicon || !isvisible(c, curmonitor())); c = c->snext);
if (sel && sel != c) {
XSetWindowBorder(dpy, sel->frame, style.color.norm[ColBorder]);
}
if (c) {
detachstack(c);
attachstack(c);
/* unban(c); */
}
sel = c;
if (!selscreen)
return;
if (c) {
setclientstate(c, NormalState);
if (c->isfocusable) {
XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
givefocus(c);
}
XSetWindowBorder(dpy, sel->frame, style.color.sel[ColBorder]);
drawclient(c);
} else {
XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
}
if (o)
drawclient(o);
updateatom[ActiveWindow] (sel);
updateatom[ClientList] (NULL);
updateatom[CurDesk] (NULL);
}
void
focusicon(const char *arg) {
Client *c;
for (c = clients; c && (!c->isicon || !isvisible(c, curmonitor())); c = c->next);
if (!c)
return;
c->isicon = False;
focus(c);
arrange(curmonitor());
}
void
focusnext(const char *arg) {
Client *c;
if (!sel)
return;
for (c = sel->next;
c && (c->isbastard || c->isicon || !isvisible(c, curmonitor())); c = c->next);
if (!c)
for (c = clients;
c && (c->isbastard || c->isicon
|| !isvisible(c, curmonitor())); c = c->next);
if (c) {
focus(c);
restack(curmonitor());
}
}
void
focusprev(const char *arg) {
Client *c;
if (!sel)
return;
for (c = sel->prev;
c && (c->isbastard || c->isicon || !isvisible(c, curmonitor())); c = c->prev);
if (!c) {
for (c = clients; c && c->next; c = c->next);
for (;
c && (c->isbastard || c->isicon
|| !isvisible(c, curmonitor())); c = c->prev);
}
if (c) {
focus(c);
restack(curmonitor());
}
}
void
iconify(const char *arg) {
Client *c;
if (!sel)
return;
c = sel;
focusnext(NULL);
ban(c);
c->isicon = True;
arrange(curmonitor());
}
void
incnmaster(const char *arg) {
unsigned int i;
if (!FEATURES(curlayout, NMASTER))
return;
if (!arg) {
views[curmontag].nmaster = DEFNMASTER;
} else {
i = atoi(arg);
if ((views[curmontag].nmaster + i) < 1
|| curwah / (views[curmontag].nmaster + i) <= 2 * style.border)
return;
views[curmontag].nmaster += i;
}
if (sel)
arrange(curmonitor());
}
Client *
getclient(Window w, Client * list, int part) {
Client *c;
#define ClientPart(_c, _part) (((_part) == ClientWindow) ? (_c)->win : \
((_part) == ClientTitle) ? (_c)->title : \
((_part) == ClientFrame) ? (_c)->frame : 0)
for (c = list; c && (ClientPart(c, part)) != w; c = c->next);
return c;
}
long
getstate(Window w) {
long ret = -1;
long *p = NULL;
unsigned long n;
p = (long*)getatom(w, atom[WMState], &n);
if (n != 0)
ret = *p;
XFree(p);
return ret;
}
const char *
getresource(const char *resource, const char *defval) {
static char name[256], class[256], *type;
XrmValue value;
snprintf(name, sizeof(name), "%s.%s", RESNAME, resource);
snprintf(class, sizeof(class), "%s.%s", RESCLASS, resource);
XrmGetResource(xrdb, name, class, &type, &value);
if (value.addr)
return value.addr;
return defval;
}
Bool
gettextprop(Window w, Atom atom, char *text, unsigned int size) {
char **list = NULL;
int n;
XTextProperty name;
if (!text || size == 0)
return False;
text[0] = '\0';
XGetTextProperty(dpy, w, &name, atom);
if (!name.nitems)
return False;
if (name.encoding == XA_STRING) {
strncpy(text, (char *) name.value, size - 1);
} else {
if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
&& n > 0 && *list) {
strncpy(text, *list, size - 1);
XFreeStringList(list);
}
}
text[size - 1] = '\0';
XFree(name.value);
return True;
}
int
idxoftag(const char *tag) {
unsigned int i;
for (i = 0; (i < ntags) && strcmp(tag, tags[i]); i++);
return (i < ntags) ? i : 0;
}
Bool
isvisible(Client * c, Monitor * m) {
unsigned int i;
if (!c)
return False;
if (!m) {
for (m = monitors; m; m = m->next) {
for (i = 0; i < ntags; i++)
if (c->tags[i] && m->seltags[i])
return True;
}
} else {
for (i = 0; i < ntags; i++)
if (c->tags[i] && m->seltags[i])
return True;
}
return False;
}
void
grabkeys(void) {
unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
unsigned int i, j;
KeyCode code;
XUngrabKey(dpy, AnyKey, AnyModifier, root);
for (i = 0; i < nkeys; i++) {
if ((code = XKeysymToKeycode(dpy, keys[i]->keysym))) {
for (j = 0; j < LENGTH(modifiers); j++)
XGrabKey(dpy, code, keys[i]->mod | modifiers[j], root,
True, GrabModeAsync, GrabModeAsync);
}
}
}
void
keypress(XEvent * e) {
unsigned int i;
KeySym keysym;
XKeyEvent *ev;
if (!curmonitor())
return;
ev = &e->xkey;
keysym = XKeycodeToKeysym(dpy, (KeyCode) ev->keycode, 0);
for (i = 0; i < nkeys; i++)
if (keysym == keys[i]->keysym
&& CLEANMASK(keys[i]->mod) == CLEANMASK(ev->state)) {
if (keys[i]->func)
keys[i]->func(keys[i]->arg);
XUngrabKeyboard(dpy, CurrentTime);
}
}
void
killclient(const char *arg) {
XEvent ev;
if (!sel)
return;
if (checkatom(sel->win, atom[WMProto], atom[WMDelete])) {
ev.type = ClientMessage;
ev.xclient.window = sel->win;
ev.xclient.message_type = atom[WMProto];
ev.xclient.format = 32;
ev.xclient.data.l[0] = atom[WMDelete];
ev.xclient.data.l[1] = CurrentTime;
XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
} else {
XKillClient(dpy, sel->win);
}
}
void
leavenotify(XEvent * e) {