-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabbed.c
1376 lines (1193 loc) · 29 KB
/
tabbed.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* See LICENSE file for copyright and license details.
*/
#include <sys/wait.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/Xft/Xft.h>
#include "arg.h"
/* XEMBED messages */
#define XEMBED_EMBEDDED_NOTIFY 0
#define XEMBED_WINDOW_ACTIVATE 1
#define XEMBED_WINDOW_DEACTIVATE 2
#define XEMBED_REQUEST_FOCUS 3
#define XEMBED_FOCUS_IN 4
#define XEMBED_FOCUS_OUT 5
#define XEMBED_FOCUS_NEXT 6
#define XEMBED_FOCUS_PREV 7
/* 8-9 were used for XEMBED_GRAB_KEY/XEMBED_UNGRAB_KEY */
#define XEMBED_MODALITY_ON 10
#define XEMBED_MODALITY_OFF 11
#define XEMBED_REGISTER_ACCELERATOR 12
#define XEMBED_UNREGISTER_ACCELERATOR 13
#define XEMBED_ACTIVATE_ACCELERATOR 14
/* Details for XEMBED_FOCUS_IN: */
#define XEMBED_FOCUS_CURRENT 0
#define XEMBED_FOCUS_FIRST 1
#define XEMBED_FOCUS_LAST 2
/* Macros */
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define LENGTH(x) (sizeof((x)) / sizeof(*(x)))
#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
#define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height)
enum { ColFG, ColBG, ColLast }; /* color */
enum { WMProtocols, WMDelete, WMName, WMState, WMFullscreen,
XEmbed, WMSelectTab, WMLast }; /* default atoms */
typedef union {
int i;
const void *v;
} Arg;
typedef struct {
unsigned int mod;
KeySym keysym;
void (*func)(const Arg *);
const Arg arg;
} Key;
typedef struct {
int x, y, w, h;
XftColor norm[ColLast];
XftColor sel[ColLast];
XftColor urg[ColLast];
Drawable drawable;
GC gc;
struct {
int ascent;
int descent;
int height;
XftFont *xfont;
} font;
} DC; /* draw context */
typedef struct {
char name[256];
Window win;
int tabx;
Bool urgent;
Bool closed;
} Client;
/* function declarations */
static void buttonpress(const XEvent *e);
static void cleanup(void);
static void clientmessage(const XEvent *e);
static void configurenotify(const XEvent *e);
static void configurerequest(const XEvent *e);
static void createnotify(const XEvent *e);
static void destroynotify(const XEvent *e);
static void die(const char *errstr, ...);
static void drawbar(void);
static void drawtext(const char *text, XftColor col[ColLast]);
static void *ecalloc(size_t n, size_t size);
static void *erealloc(void *o, size_t size);
static void expose(const XEvent *e);
static void focus(int c);
static void focusin(const XEvent *e);
static void focusonce(const Arg *arg);
static void focusurgent(const Arg *arg);
static void fullscreen(const Arg *arg);
static char *getatom(int a);
static int getclient(Window w);
static XftColor getcolor(const char *colstr);
static int getfirsttab(void);
static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
static void initfont(const char *fontstr);
static Bool isprotodel(int c);
static void keypress(const XEvent *e);
static void killclient(const Arg *arg);
static void manage(Window win);
static void maprequest(const XEvent *e);
static void move(const Arg *arg);
static void movetab(const Arg *arg);
static void propertynotify(const XEvent *e);
static void resize(int c, int w, int h);
static void rotate(const Arg *arg);
static void run(void);
static void sendxembed(int c, long msg, long detail, long d1, long d2);
static void setcmd(int argc, char *argv[], int);
static void setup(void);
static void sigchld(int unused);
static void spawn(const Arg *arg);
static int textnw(const char *text, unsigned int len);
static void toggle(const Arg *arg);
static void unmanage(int c);
static void unmapnotify(const XEvent *e);
static void updatenumlockmask(void);
static void updatetitle(int c);
static int xerror(Display *dpy, XErrorEvent *ee);
static void xsettitle(Window w, const char *str);
/* variables */
static int screen;
static void (*handler[LASTEvent]) (const XEvent *) = {
[ButtonPress] = buttonpress,
[ClientMessage] = clientmessage,
[ConfigureNotify] = configurenotify,
[ConfigureRequest] = configurerequest,
[CreateNotify] = createnotify,
[UnmapNotify] = unmapnotify,
[DestroyNotify] = destroynotify,
[Expose] = expose,
[FocusIn] = focusin,
[KeyPress] = keypress,
[MapRequest] = maprequest,
[PropertyNotify] = propertynotify,
};
static int bh, obh, wx, wy, ww, wh;
static unsigned int numlockmask;
static Bool running = True, nextfocus, doinitspawn = True,
fillagain = False, closelastclient = False,
killclientsfirst = False;
static Display *dpy;
static DC dc;
static Atom wmatom[WMLast];
static Window root, win;
static Client **clients;
static int nclients, sel = -1, lastsel = -1;
static int (*xerrorxlib)(Display *, XErrorEvent *);
static int cmd_append_pos;
static char winid[64];
static char **cmd;
static char *wmname = "tabbed";
static const char *geometry;
char *argv0;
/* configuration, allows nested code to access above variables */
#include "config.h"
void
buttonpress(const XEvent *e)
{
const XButtonPressedEvent *ev = &e->xbutton;
int i, fc;
Arg arg;
if (ev->y < 0 || ev->y > bh)
return;
if (((fc = getfirsttab()) > 0 && ev->x < TEXTW(before)) || ev->x < 0)
return;
for (i = fc; i < nclients; i++) {
if (clients[i]->tabx > ev->x) {
switch (ev->button) {
case Button1:
focus(i);
break;
case Button2:
focus(i);
killclient(NULL);
break;
case Button4: /* FALLTHROUGH */
case Button5:
arg.i = ev->button == Button4 ? -1 : 1;
rotate(&arg);
break;
}
break;
}
}
}
void
cleanup(void)
{
int i;
for (i = 0; i < nclients; i++) {
focus(i);
killclient(NULL);
XReparentWindow(dpy, clients[i]->win, root, 0, 0);
unmanage(i);
}
free(clients);
clients = NULL;
XFreePixmap(dpy, dc.drawable);
XFreeGC(dpy, dc.gc);
XDestroyWindow(dpy, win);
XSync(dpy, False);
free(cmd);
}
void
clientmessage(const XEvent *e)
{
const XClientMessageEvent *ev = &e->xclient;
if (ev->message_type == wmatom[WMProtocols] &&
ev->data.l[0] == wmatom[WMDelete]) {
if (nclients > 1 && killclientsfirst) {
killclient(0);
return;
}
running = False;
}
}
void
configurenotify(const XEvent *e)
{
const XConfigureEvent *ev = &e->xconfigure;
if (ev->window == win && (ev->width != ww || ev->height != wh)) {
ww = ev->width;
wh = ev->height;
XFreePixmap(dpy, dc.drawable);
dc.drawable = XCreatePixmap(dpy, root, ww, wh,
DefaultDepth(dpy, screen));
if (!obh && (wh <= bh)) {
obh = bh;
bh = 0;
} else if (!bh && (wh > obh)) {
bh = obh;
obh = 0;
}
if (sel > -1)
resize(sel, ww, wh - bh);
XSync(dpy, False);
}
}
void
configurerequest(const XEvent *e)
{
const XConfigureRequestEvent *ev = &e->xconfigurerequest;
XWindowChanges wc;
int c;
if ((c = getclient(ev->window)) > -1) {
wc.x = 0;
wc.y = bh;
wc.width = ww;
wc.height = wh - bh;
wc.border_width = 0;
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
XConfigureWindow(dpy, clients[c]->win, ev->value_mask, &wc);
}
}
void
createnotify(const XEvent *e)
{
const XCreateWindowEvent *ev = &e->xcreatewindow;
if (ev->window != win && getclient(ev->window) < 0)
manage(ev->window);
}
void
destroynotify(const XEvent *e)
{
const XDestroyWindowEvent *ev = &e->xdestroywindow;
int c;
if ((c = getclient(ev->window)) > -1)
unmanage(c);
}
void
die(const char *errstr, ...)
{
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
void
drawbar(void)
{
XftColor *col;
int c, cc, fc, width;
char *name = NULL;
if (nclients == 0) {
dc.x = 0;
dc.w = ww;
XFetchName(dpy, win, &name);
drawtext(name ? name : "", dc.norm);
XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0);
XSync(dpy, False);
return;
}
width = ww;
cc = ww / tabwidth;
if (nclients > cc)
cc = (ww - TEXTW(before) - TEXTW(after)) / tabwidth;
if ((fc = getfirsttab()) + cc < nclients) {
dc.w = TEXTW(after);
dc.x = width - dc.w;
drawtext(after, dc.sel);
width -= dc.w;
}
dc.x = 0;
if (fc > 0) {
dc.w = TEXTW(before);
drawtext(before, dc.sel);
dc.x += dc.w;
width -= dc.w;
}
cc = MIN(cc, nclients);
for (c = fc; c < fc + cc; c++) {
dc.w = width / cc;
if (c == sel) {
col = dc.sel;
dc.w += width % cc;
} else {
col = clients[c]->urgent ? dc.urg : dc.norm;
}
drawtext(clients[c]->name, col);
dc.x += dc.w;
clients[c]->tabx = dc.x;
}
XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0);
XSync(dpy, False);
}
void
drawtext(const char *text, XftColor col[ColLast])
{
int i, j, x, y, h, len, olen;
char buf[256];
XftDraw *d;
XRectangle r = { dc.x, dc.y, dc.w, dc.h };
XSetForeground(dpy, dc.gc, col[ColBG].pixel);
XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
if (!text)
return;
olen = strlen(text);
h = dc.font.ascent + dc.font.descent;
y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
x = dc.x + (h / 2);
/* shorten text if necessary */
for (len = MIN(olen, sizeof(buf));
len && textnw(text, len) > dc.w - h; len--);
if (!len)
return;
memcpy(buf, text, len);
if (len < olen) {
for (i = len, j = strlen(titletrim); j && i;
buf[--i] = titletrim[--j])
;
}
d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen));
XftDrawStringUtf8(d, &col[ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len);
XftDrawDestroy(d);
}
void *
ecalloc(size_t n, size_t size)
{
void *p;
if (!(p = calloc(n, size)))
die("%s: cannot calloc\n", argv0);
return p;
}
void *
erealloc(void *o, size_t size)
{
void *p;
if (!(p = realloc(o, size)))
die("%s: cannot realloc\n", argv0);
return p;
}
void
expose(const XEvent *e)
{
const XExposeEvent *ev = &e->xexpose;
if (ev->count == 0 && win == ev->window)
drawbar();
}
void
focus(int c)
{
char buf[BUFSIZ] = "tabbed-"VERSION" ::";
size_t i, n;
XWMHints* wmh;
/* If c, sel and clients are -1, raise tabbed-win itself */
if (nclients == 0) {
cmd[cmd_append_pos] = NULL;
for(i = 0, n = strlen(buf); cmd[i] && n < sizeof(buf); i++)
n += snprintf(&buf[n], sizeof(buf) - n, " %s", cmd[i]);
xsettitle(win, buf);
XRaiseWindow(dpy, win);
return;
}
if (c < 0 || c >= nclients)
return;
resize(c, ww, wh - bh);
XRaiseWindow(dpy, clients[c]->win);
XSetInputFocus(dpy, clients[c]->win, RevertToParent, CurrentTime);
sendxembed(c, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0);
sendxembed(c, XEMBED_WINDOW_ACTIVATE, 0, 0, 0);
xsettitle(win, clients[c]->name);
if (sel != c) {
lastsel = sel;
sel = c;
}
if (clients[c]->urgent && (wmh = XGetWMHints(dpy, clients[c]->win))) {
wmh->flags &= ~XUrgencyHint;
XSetWMHints(dpy, clients[c]->win, wmh);
clients[c]->urgent = False;
XFree(wmh);
}
drawbar();
XSync(dpy, False);
}
void
focusin(const XEvent *e)
{
const XFocusChangeEvent *ev = &e->xfocus;
int dummy;
Window focused;
if (ev->mode != NotifyUngrab) {
XGetInputFocus(dpy, &focused, &dummy);
if (focused == win)
focus(sel);
}
}
void
focusonce(const Arg *arg)
{
nextfocus = True;
}
void
focusurgent(const Arg *arg)
{
int c;
if (sel < 0)
return;
for (c = (sel + 1) % nclients; c != sel; c = (c + 1) % nclients) {
if (clients[c]->urgent) {
focus(c);
return;
}
}
}
void
fullscreen(const Arg *arg)
{
XEvent e;
e.type = ClientMessage;
e.xclient.window = win;
e.xclient.message_type = wmatom[WMState];
e.xclient.format = 32;
e.xclient.data.l[0] = 2;
e.xclient.data.l[1] = wmatom[WMFullscreen];
e.xclient.data.l[2] = 0;
XSendEvent(dpy, root, False, SubstructureNotifyMask, &e);
}
char *
getatom(int a)
{
static char buf[BUFSIZ];
Atom adummy;
int idummy;
unsigned long ldummy;
unsigned char *p = NULL;
XGetWindowProperty(dpy, win, wmatom[a], 0L, BUFSIZ, False, XA_STRING,
&adummy, &idummy, &ldummy, &ldummy, &p);
if (p)
strncpy(buf, (char *)p, LENGTH(buf)-1);
else
buf[0] = '\0';
XFree(p);
return buf;
}
int
getclient(Window w)
{
int i;
for (i = 0; i < nclients; i++) {
if (clients[i]->win == w)
return i;
}
return -1;
}
XftColor
getcolor(const char *colstr)
{
XftColor color;
if (!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &color))
die("%s: cannot allocate color '%s'\n", argv0, colstr);
return color;
}
int
getfirsttab(void)
{
int cc, ret;
if (sel < 0)
return 0;
cc = ww / tabwidth;
if (nclients > cc)
cc = (ww - TEXTW(before) - TEXTW(after)) / tabwidth;
ret = sel - cc / 2 + (cc + 1) % 2;
return ret < 0 ? 0 :
ret + cc > nclients ? MAX(0, nclients - cc) :
ret;
}
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;
}
void
initfont(const char *fontstr)
{
if (!(dc.font.xfont = XftFontOpenName(dpy, screen, fontstr))
&& !(dc.font.xfont = XftFontOpenName(dpy, screen, "fixed")))
die("error, cannot load font: '%s'\n", fontstr);
dc.font.ascent = dc.font.xfont->ascent;
dc.font.descent = dc.font.xfont->descent;
dc.font.height = dc.font.ascent + dc.font.descent;
}
Bool
isprotodel(int c)
{
int i, n;
Atom *protocols;
Bool ret = False;
if (XGetWMProtocols(dpy, clients[c]->win, &protocols, &n)) {
for (i = 0; !ret && i < n; i++) {
if (protocols[i] == wmatom[WMDelete])
ret = True;
}
XFree(protocols);
}
return ret;
}
void
keypress(const XEvent *e)
{
const XKeyEvent *ev = &e->xkey;
unsigned int i;
KeySym keysym;
keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
for (i = 0; i < LENGTH(keys); i++) {
if (keysym == keys[i].keysym &&
CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) &&
keys[i].func)
keys[i].func(&(keys[i].arg));
}
}
void
killclient(const Arg *arg)
{
XEvent ev;
if (sel < 0)
return;
if (isprotodel(sel) && !clients[sel]->closed) {
ev.type = ClientMessage;
ev.xclient.window = clients[sel]->win;
ev.xclient.message_type = wmatom[WMProtocols];
ev.xclient.format = 32;
ev.xclient.data.l[0] = wmatom[WMDelete];
ev.xclient.data.l[1] = CurrentTime;
XSendEvent(dpy, clients[sel]->win, False, NoEventMask, &ev);
clients[sel]->closed = True;
} else {
XKillClient(dpy, clients[sel]->win);
}
}
void
manage(Window w)
{
updatenumlockmask();
{
int i, j, nextpos;
unsigned int modifiers[] = { 0, LockMask, numlockmask,
numlockmask | LockMask };
KeyCode code;
Client *c;
XEvent e;
XWithdrawWindow(dpy, w, 0);
XReparentWindow(dpy, w, win, 0, bh);
XSelectInput(dpy, w, PropertyChangeMask |
StructureNotifyMask | EnterWindowMask);
XSync(dpy, False);
for (i = 0; i < LENGTH(keys); i++) {
if ((code = XKeysymToKeycode(dpy, keys[i].keysym))) {
for (j = 0; j < LENGTH(modifiers); j++) {
XGrabKey(dpy, code, keys[i].mod |
modifiers[j], w, True,
GrabModeAsync, GrabModeAsync);
}
}
}
c = ecalloc(1, sizeof *c);
c->win = w;
nclients++;
clients = erealloc(clients, sizeof(Client *) * nclients);
if(npisrelative) {
nextpos = sel + newposition;
} else {
if (newposition < 0)
nextpos = nclients - newposition;
else
nextpos = newposition;
}
if (nextpos >= nclients)
nextpos = nclients - 1;
if (nextpos < 0)
nextpos = 0;
if (nclients > 1 && nextpos < nclients - 1)
memmove(&clients[nextpos + 1], &clients[nextpos],
sizeof(Client *) * (nclients - nextpos - 1));
clients[nextpos] = c;
updatetitle(nextpos);
XLowerWindow(dpy, w);
XMapWindow(dpy, w);
e.xclient.window = w;
e.xclient.type = ClientMessage;
e.xclient.message_type = wmatom[XEmbed];
e.xclient.format = 32;
e.xclient.data.l[0] = CurrentTime;
e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY;
e.xclient.data.l[2] = 0;
e.xclient.data.l[3] = win;
e.xclient.data.l[4] = 0;
XSendEvent(dpy, root, False, NoEventMask, &e);
XSync(dpy, False);
/* Adjust sel before focus does set it to lastsel. */
if (sel >= nextpos)
sel++;
focus(nextfocus ? nextpos :
sel < 0 ? 0 :
sel);
nextfocus = foreground;
}
}
void
maprequest(const XEvent *e)
{
const XMapRequestEvent *ev = &e->xmaprequest;
if (getclient(ev->window) < 0)
manage(ev->window);
}
void
move(const Arg *arg)
{
if (arg->i >= 0 && arg->i < nclients)
focus(arg->i);
}
void
movetab(const Arg *arg)
{
int c;
Client *new;
if (sel < 0)
return;
c = (sel + arg->i) % nclients;
if (c < 0)
c += nclients;
if (c == sel)
return;
new = clients[sel];
if (sel < c)
memmove(&clients[sel], &clients[sel+1],
sizeof(Client *) * (c - sel));
else
memmove(&clients[c+1], &clients[c],
sizeof(Client *) * (sel - c));
clients[c] = new;
sel = c;
drawbar();
}
void
propertynotify(const XEvent *e)
{
const XPropertyEvent *ev = &e->xproperty;
XWMHints *wmh;
int c;
char* selection = NULL;
Arg arg;
if (ev->state == PropertyNewValue && ev->atom == wmatom[WMSelectTab]) {
selection = getatom(WMSelectTab);
if (!strncmp(selection, "0x", 2)) {
arg.i = getclient(strtoul(selection, NULL, 0));
move(&arg);
} else {
cmd[cmd_append_pos] = selection;
arg.v = cmd;
spawn(&arg);
}
} else if (ev->state == PropertyNewValue && ev->atom == XA_WM_HINTS &&
(c = getclient(ev->window)) > -1 &&
(wmh = XGetWMHints(dpy, clients[c]->win))) {
if (wmh->flags & XUrgencyHint) {
XFree(wmh);
wmh = XGetWMHints(dpy, win);
if (c != sel) {
if (urgentswitch && wmh &&
!(wmh->flags & XUrgencyHint)) {
/* only switch, if tabbed was focused
* since last urgency hint if WMHints
* could not be received,
* default to no switch */
focus(c);
} else {
/* if no switch should be performed,
* mark tab as urgent */
clients[c]->urgent = True;
drawbar();
}
}
if (wmh && !(wmh->flags & XUrgencyHint)) {
/* update tabbed urgency hint
* if not set already */
wmh->flags |= XUrgencyHint;
XSetWMHints(dpy, win, wmh);
}
}
XFree(wmh);
} else if (ev->state != PropertyDelete && ev->atom == XA_WM_NAME &&
(c = getclient(ev->window)) > -1) {
updatetitle(c);
}
}
void
resize(int c, int w, int h)
{
XConfigureEvent ce;
XWindowChanges wc;
ce.x = 0;
ce.y = wc.y = bh;
ce.width = wc.width = w;
ce.height = wc.height = h;
ce.type = ConfigureNotify;
ce.display = dpy;
ce.event = clients[c]->win;
ce.window = clients[c]->win;
ce.above = None;
ce.override_redirect = False;
ce.border_width = 0;
XConfigureWindow(dpy, clients[c]->win, CWY | CWWidth | CWHeight, &wc);
XSendEvent(dpy, clients[c]->win, False, StructureNotifyMask,
(XEvent *)&ce);
}
void
rotate(const Arg *arg)
{
int nsel = -1;
if (sel < 0)
return;
if (arg->i == 0) {
if (lastsel > -1)
focus(lastsel);
} else if (sel > -1) {
/* Rotating in an arg->i step around the clients. */
nsel = sel + arg->i;
while (nsel >= nclients)
nsel -= nclients;
while (nsel < 0)
nsel += nclients;
focus(nsel);
}
}
void
run(void)
{
XEvent ev;
/* main event loop */
XSync(dpy, False);
drawbar();
if (doinitspawn == True)
spawn(NULL);
while (running) {
XNextEvent(dpy, &ev);
if (handler[ev.type])
(handler[ev.type])(&ev); /* call handler */
}
}
void
sendxembed(int c, long msg, long detail, long d1, long d2)
{
XEvent e = { 0 };
e.xclient.window = clients[c]->win;
e.xclient.type = ClientMessage;
e.xclient.message_type = wmatom[XEmbed];
e.xclient.format = 32;
e.xclient.data.l[0] = CurrentTime;
e.xclient.data.l[1] = msg;
e.xclient.data.l[2] = detail;
e.xclient.data.l[3] = d1;
e.xclient.data.l[4] = d2;
XSendEvent(dpy, clients[c]->win, False, NoEventMask, &e);
}
void
setcmd(int argc, char *argv[], int replace)
{
int i;
cmd = ecalloc(argc + 3, sizeof(*cmd));
if (argc == 0)
return;
for (i = 0; i < argc; i++)
cmd[i] = argv[i];
cmd[replace > 0 ? replace : argc] = winid;
cmd_append_pos = argc + !replace;
cmd[cmd_append_pos] = cmd[cmd_append_pos + 1] = NULL;
}
void
setup(void)
{
int bitm, tx, ty, tw, th, dh, dw, isfixed;
XWMHints *wmh;
XClassHint class_hint;
XSizeHints *size_hint;
/* clean up any zombies immediately */
sigchld(0);
/* init screen */
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
initfont(font);
bh = dc.h = dc.font.height + 2;
/* init atoms */
wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
wmatom[WMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN",
False);
wmatom[WMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
wmatom[WMSelectTab] = XInternAtom(dpy, "_TABBED_SELECT_TAB", False);
wmatom[WMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
wmatom[XEmbed] = XInternAtom(dpy, "_XEMBED", False);
/* init appearance */
wx = 0;