adds rule for floating webcam window
[dwm.git] / dwm.c
1 /* See LICENSE file for copyright and license details.
2 *
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
8 *
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
11 * in O(1) time.
12 *
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
17 * client.
18 *
19 * Keys and tagging rules are organized as arrays and defined in config.h.
20 *
21 * To understand everything else, start reading main().
22 */
23 #include <errno.h>
24 #include <locale.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
39 #ifdef XINERAMA
40 #include <X11/extensions/Xinerama.h>
41 #endif /* XINERAMA */
42 #include <X11/Xft/Xft.h>
43
44 #include "drw.h"
45 #include "util.h"
46
47 /* macros */
48 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
50 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
51 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
52 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
53 #define LENGTH(X) (sizeof X / sizeof X[0])
54 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
55 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
56 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
57 #define TAGMASK ((1 << LENGTH(tags)) - 1)
58 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
59
60 #define SYSTEM_TRAY_REQUEST_DOCK 0
61
62 /* XEMBED messages */
63 #define XEMBED_EMBEDDED_NOTIFY 0
64 #define XEMBED_WINDOW_ACTIVATE 1
65 #define XEMBED_FOCUS_IN 4
66 #define XEMBED_MODALITY_ON 10
67
68 #define XEMBED_MAPPED (1 << 0)
69 #define XEMBED_WINDOW_ACTIVATE 1
70 #define XEMBED_WINDOW_DEACTIVATE 2
71
72 #define VERSION_MAJOR 0
73 #define VERSION_MINOR 0
74 #define XEMBED_EMBEDDED_VERSION (VERSION_MAJOR << 16) | VERSION_MINOR
75
76 /* enums */
77 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
78 enum { SchemeNorm, SchemeSel }; /* color schemes */
79 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
80 NetSystemTray, NetSystemTrayOP, NetSystemTrayOrientation, NetSystemTrayOrientationHorz,
81 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
82 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
83 enum { Manager, Xembed, XembedInfo, XLast }; /* Xembed atoms */
84 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
85 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
86 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
87
88 typedef union {
89 int i;
90 unsigned int ui;
91 float f;
92 const void *v;
93 } Arg;
94
95 typedef struct {
96 unsigned int click;
97 unsigned int mask;
98 unsigned int button;
99 void (*func)(const Arg *arg);
100 const Arg arg;
101 } Button;
102
103 typedef struct Monitor Monitor;
104 typedef struct Client Client;
105 struct Client {
106 char name[256];
107 float mina, maxa;
108 int x, y, w, h;
109 int sfx, sfy, sfw, sfh; /* stored float geometry, used on mode revert */
110 int oldx, oldy, oldw, oldh;
111 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
112 int bw, oldbw;
113 unsigned int tags;
114 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
115 Client *next;
116 Client *snext;
117 Monitor *mon;
118 Window win;
119 };
120
121 typedef struct {
122 unsigned int mod;
123 KeySym keysym;
124 void (*func)(const Arg *);
125 const Arg arg;
126 } Key;
127
128 typedef struct {
129 const char *symbol;
130 void (*arrange)(Monitor *);
131 } Layout;
132
133 typedef struct Pertag Pertag;
134 struct Monitor {
135 char ltsymbol[16];
136 float mfact;
137 int nmaster;
138 int num;
139 int by; /* bar geometry */
140 int mx, my, mw, mh; /* screen size */
141 int wx, wy, ww, wh; /* window area */
142 unsigned int seltags;
143 unsigned int sellt;
144 unsigned int tagset[2];
145 int showbar;
146 int topbar;
147 Client *clients;
148 Client *sel;
149 Client *stack;
150 Monitor *next;
151 Window barwin;
152 const Layout *lt[2];
153 Pertag *pertag;
154 };
155
156 typedef struct {
157 const char *class;
158 const char *instance;
159 const char *title;
160 unsigned int tags;
161 int isfloating;
162 int monitor;
163 } Rule;
164
165 typedef struct Systray Systray;
166 struct Systray {
167 Window win;
168 Client *icons;
169 };
170
171 /* function declarations */
172 static void applyrules(Client *c);
173 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
174 static void arrange(Monitor *m);
175 static void arrangemon(Monitor *m);
176 static void attach(Client *c);
177 static void attachstack(Client *c);
178 static void buttonpress(XEvent *e);
179 static void checkotherwm(void);
180 static void cleanup(void);
181 static void cleanupmon(Monitor *mon);
182 static void clientmessage(XEvent *e);
183 static void configure(Client *c);
184 static void configurenotify(XEvent *e);
185 static void configurerequest(XEvent *e);
186 static Monitor *createmon(void);
187 static void destroynotify(XEvent *e);
188 static void detach(Client *c);
189 static void detachstack(Client *c);
190 static Monitor *dirtomon(int dir);
191 static void drawbar(Monitor *m);
192 static void drawbars(void);
193 static void enternotify(XEvent *e);
194 static void expose(XEvent *e);
195 static void focus(Client *c);
196 static void focusin(XEvent *e);
197 static void focusmon(const Arg *arg);
198 static void focusstack(const Arg *arg);
199 static Atom getatomprop(Client *c, Atom prop);
200 static int getrootptr(int *x, int *y);
201 static long getstate(Window w);
202 static unsigned int getsystraywidth();
203 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
204 static void grabbuttons(Client *c, int focused);
205 static void grabkeys(void);
206 static void incnmaster(const Arg *arg);
207 static void keypress(XEvent *e);
208 static void killclient(const Arg *arg);
209 static void manage(Window w, XWindowAttributes *wa);
210 static void mappingnotify(XEvent *e);
211 static void maprequest(XEvent *e);
212 static void monocle(Monitor *m);
213 static void motionnotify(XEvent *e);
214 static void movemouse(const Arg *arg);
215 static Client *nexttiled(Client *c);
216 static void pop(Client *);
217 static void propertynotify(XEvent *e);
218 static void quit(const Arg *arg);
219 static Monitor *recttomon(int x, int y, int w, int h);
220 static void removesystrayicon(Client *i);
221 static void resize(Client *c, int x, int y, int w, int h, int interact);
222 static void resizebarwin(Monitor *m);
223 static void resizeclient(Client *c, int x, int y, int w, int h);
224 static void resizemouse(const Arg *arg);
225 static void resizerequest(XEvent *e);
226 static void restack(Monitor *m);
227 static void run(void);
228 static void scan(void);
229 static int sendevent(Window w, Atom proto, int m, long d0, long d1, long d2, long d3, long d4);
230 static void sendmon(Client *c, Monitor *m);
231 static void setclientstate(Client *c, long state);
232 static void setfocus(Client *c);
233 static void setfullscreen(Client *c, int fullscreen);
234 static void setlayout(const Arg *arg);
235 static void setmfact(const Arg *arg);
236 static void setup(void);
237 static void seturgent(Client *c, int urg);
238 static void showhide(Client *c);
239 static void sigchld(int unused);
240 static void spawn(const Arg *arg);
241 static Monitor *systraytomon(Monitor *m);
242 static void tag(const Arg *arg);
243 static void tagmon(const Arg *arg);
244 static void tile(Monitor *);
245 static void togglebar(const Arg *arg);
246 static void togglefloating(const Arg *arg);
247 static void toggletag(const Arg *arg);
248 static void toggleview(const Arg *arg);
249 static void unfocus(Client *c, int setfocus);
250 static void unmanage(Client *c, int destroyed);
251 static void unmapnotify(XEvent *e);
252 static void updatebarpos(Monitor *m);
253 static void updatebars(void);
254 static void updateclientlist(void);
255 static int updategeom(void);
256 static void updatenumlockmask(void);
257 static void updatesizehints(Client *c);
258 static void updatestatus(void);
259 static void updatesystray(void);
260 static void updatesystrayicongeom(Client *i, int w, int h);
261 static void updatesystrayiconstate(Client *i, XPropertyEvent *ev);
262 static void updatetitle(Client *c);
263 static void updatewindowtype(Client *c);
264 static void updatewmhints(Client *c);
265 static void view(const Arg *arg);
266 static Client *wintoclient(Window w);
267 static Monitor *wintomon(Window w);
268 static Client *wintosystrayicon(Window w);
269 static int xerror(Display *dpy, XErrorEvent *ee);
270 static int xerrordummy(Display *dpy, XErrorEvent *ee);
271 static int xerrorstart(Display *dpy, XErrorEvent *ee);
272 static void zoom(const Arg *arg);
273
274 /* variables */
275 static Systray *systray = NULL;
276 static const char broken[] = "broken";
277 static char stext[256];
278 static int screen;
279 static int sw, sh; /* X display screen geometry width, height */
280 static int bh, blw = 0; /* bar geometry */
281 static int lrpad; /* sum of left and right padding for text */
282 static int (*xerrorxlib)(Display *, XErrorEvent *);
283 static unsigned int numlockmask = 0;
284 static void (*handler[LASTEvent]) (XEvent *) = {
285 [ButtonPress] = buttonpress,
286 [ClientMessage] = clientmessage,
287 [ConfigureRequest] = configurerequest,
288 [ConfigureNotify] = configurenotify,
289 [DestroyNotify] = destroynotify,
290 [EnterNotify] = enternotify,
291 [Expose] = expose,
292 [FocusIn] = focusin,
293 [KeyPress] = keypress,
294 [MappingNotify] = mappingnotify,
295 [MapRequest] = maprequest,
296 [MotionNotify] = motionnotify,
297 [PropertyNotify] = propertynotify,
298 [ResizeRequest] = resizerequest,
299 [UnmapNotify] = unmapnotify
300 };
301 static Atom wmatom[WMLast], netatom[NetLast], xatom[XLast];
302 static int running = 1;
303 static Cur *cursor[CurLast];
304 static Clr **scheme;
305 static Display *dpy;
306 static Drw *drw;
307 static Monitor *mons, *selmon;
308 static Window root, wmcheckwin;
309
310 /* configuration, allows nested code to access above variables */
311 #include "config.h"
312
313 struct Pertag {
314 unsigned int curtag, prevtag; /* current and previous tag */
315 int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
316 float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
317 unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
318 const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */
319 int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
320 };
321
322 /* compile-time check if all tags fit into an unsigned int bit array. */
323 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
324
325 /* function implementations */
326 void
327 applyrules(Client *c)
328 {
329 const char *class, *instance;
330 unsigned int i;
331 const Rule *r;
332 Monitor *m;
333 XClassHint ch = { NULL, NULL };
334
335 /* rule matching */
336 c->isfloating = 0;
337 c->tags = 0;
338 XGetClassHint(dpy, c->win, &ch);
339 class = ch.res_class ? ch.res_class : broken;
340 instance = ch.res_name ? ch.res_name : broken;
341
342 for (i = 0; i < LENGTH(rules); i++) {
343 r = &rules[i];
344 if ((!r->title || strstr(c->name, r->title))
345 && (!r->class || strstr(class, r->class))
346 && (!r->instance || strstr(instance, r->instance)))
347 {
348 c->isfloating = r->isfloating;
349 c->tags |= r->tags;
350 for (m = mons; m && m->num != r->monitor; m = m->next);
351 if (m)
352 c->mon = m;
353 }
354 }
355 if (ch.res_class)
356 XFree(ch.res_class);
357 if (ch.res_name)
358 XFree(ch.res_name);
359 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
360 }
361
362 int
363 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
364 {
365 int baseismin;
366 Monitor *m = c->mon;
367
368 /* set minimum possible */
369 *w = MAX(1, *w);
370 *h = MAX(1, *h);
371 if (interact) {
372 if (*x > sw)
373 *x = sw - WIDTH(c);
374 if (*y > sh)
375 *y = sh - HEIGHT(c);
376 if (*x + *w + 2 * c->bw < 0)
377 *x = 0;
378 if (*y + *h + 2 * c->bw < 0)
379 *y = 0;
380 } else {
381 if (*x >= m->wx + m->ww)
382 *x = m->wx + m->ww - WIDTH(c);
383 if (*y >= m->wy + m->wh)
384 *y = m->wy + m->wh - HEIGHT(c);
385 if (*x + *w + 2 * c->bw <= m->wx)
386 *x = m->wx;
387 if (*y + *h + 2 * c->bw <= m->wy)
388 *y = m->wy;
389 }
390 if (*h < bh)
391 *h = bh;
392 if (*w < bh)
393 *w = bh;
394 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
395 /* see last two sentences in ICCCM 4.1.2.3 */
396 baseismin = c->basew == c->minw && c->baseh == c->minh;
397 if (!baseismin) { /* temporarily remove base dimensions */
398 *w -= c->basew;
399 *h -= c->baseh;
400 }
401 /* adjust for aspect limits */
402 if (c->mina > 0 && c->maxa > 0) {
403 if (c->maxa < (float)*w / *h)
404 *w = *h * c->maxa + 0.5;
405 else if (c->mina < (float)*h / *w)
406 *h = *w * c->mina + 0.5;
407 }
408 if (baseismin) { /* increment calculation requires this */
409 *w -= c->basew;
410 *h -= c->baseh;
411 }
412 /* adjust for increment value */
413 if (c->incw)
414 *w -= *w % c->incw;
415 if (c->inch)
416 *h -= *h % c->inch;
417 /* restore base dimensions */
418 *w = MAX(*w + c->basew, c->minw);
419 *h = MAX(*h + c->baseh, c->minh);
420 if (c->maxw)
421 *w = MIN(*w, c->maxw);
422 if (c->maxh)
423 *h = MIN(*h, c->maxh);
424 }
425 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
426 }
427
428 void
429 arrange(Monitor *m)
430 {
431 if (m)
432 showhide(m->stack);
433 else for (m = mons; m; m = m->next)
434 showhide(m->stack);
435 if (m) {
436 arrangemon(m);
437 restack(m);
438 } else for (m = mons; m; m = m->next)
439 arrangemon(m);
440 }
441
442 void
443 arrangemon(Monitor *m)
444 {
445 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
446 if (m->lt[m->sellt]->arrange)
447 m->lt[m->sellt]->arrange(m);
448 }
449
450 void
451 attach(Client *c)
452 {
453 c->next = c->mon->clients;
454 c->mon->clients = c;
455 }
456
457 void
458 attachstack(Client *c)
459 {
460 c->snext = c->mon->stack;
461 c->mon->stack = c;
462 }
463
464 void
465 buttonpress(XEvent *e)
466 {
467 unsigned int i, x, click;
468 Arg arg = {0};
469 Client *c;
470 Monitor *m;
471 XButtonPressedEvent *ev = &e->xbutton;
472
473 click = ClkRootWin;
474 /* focus monitor if necessary */
475 if ((m = wintomon(ev->window)) && m != selmon) {
476 unfocus(selmon->sel, 1);
477 selmon = m;
478 focus(NULL);
479 }
480 if (ev->window == selmon->barwin) {
481 i = x = 0;
482 do
483 x += TEXTW(tags[i]);
484 while (ev->x >= x && ++i < LENGTH(tags));
485 if (i < LENGTH(tags)) {
486 click = ClkTagBar;
487 arg.ui = 1 << i;
488 } else if (ev->x < x + blw)
489 click = ClkLtSymbol;
490 else if (ev->x > selmon->ww - TEXTW(stext))
491 click = ClkStatusText;
492 else
493 click = ClkWinTitle;
494 } else if ((c = wintoclient(ev->window))) {
495 focus(c);
496 restack(selmon);
497 XAllowEvents(dpy, ReplayPointer, CurrentTime);
498 click = ClkClientWin;
499 }
500 for (i = 0; i < LENGTH(buttons); i++)
501 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
502 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
503 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
504 }
505
506 void
507 checkotherwm(void)
508 {
509 xerrorxlib = XSetErrorHandler(xerrorstart);
510 /* this causes an error if some other window manager is running */
511 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
512 XSync(dpy, False);
513 XSetErrorHandler(xerror);
514 XSync(dpy, False);
515 }
516
517 void
518 cleanup(void)
519 {
520 Arg a = {.ui = ~0};
521 Layout foo = { "", NULL };
522 Monitor *m;
523 size_t i;
524
525 view(&a);
526 selmon->lt[selmon->sellt] = &foo;
527 for (m = mons; m; m = m->next)
528 while (m->stack)
529 unmanage(m->stack, 0);
530 XUngrabKey(dpy, AnyKey, AnyModifier, root);
531 while (mons)
532 cleanupmon(mons);
533 if (showsystray) {
534 XUnmapWindow(dpy, systray->win);
535 XDestroyWindow(dpy, systray->win);
536 free(systray);
537 }
538 for (i = 0; i < CurLast; i++)
539 drw_cur_free(drw, cursor[i]);
540 for (i = 0; i < LENGTH(colors); i++)
541 free(scheme[i]);
542 XDestroyWindow(dpy, wmcheckwin);
543 drw_free(drw);
544 XSync(dpy, False);
545 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
546 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
547 }
548
549 void
550 cleanupmon(Monitor *mon)
551 {
552 Monitor *m;
553
554 if (mon == mons)
555 mons = mons->next;
556 else {
557 for (m = mons; m && m->next != mon; m = m->next);
558 m->next = mon->next;
559 }
560 XUnmapWindow(dpy, mon->barwin);
561 XDestroyWindow(dpy, mon->barwin);
562 free(mon);
563 }
564
565 void
566 clientmessage(XEvent *e)
567 {
568 XWindowAttributes wa;
569 XSetWindowAttributes swa;
570 XClientMessageEvent *cme = &e->xclient;
571 Client *c = wintoclient(cme->window);
572
573 if (showsystray && cme->window == systray->win && cme->message_type == netatom[NetSystemTrayOP]) {
574 /* add systray icons */
575 if (cme->data.l[1] == SYSTEM_TRAY_REQUEST_DOCK) {
576 if (!(c = (Client *)calloc(1, sizeof(Client))))
577 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
578 if (!(c->win = cme->data.l[2])) {
579 free(c);
580 return;
581 }
582 c->mon = selmon;
583 c->next = systray->icons;
584 systray->icons = c;
585 XGetWindowAttributes(dpy, c->win, &wa);
586 c->x = c->oldx = c->y = c->oldy = 0;
587 c->w = c->oldw = wa.width;
588 c->h = c->oldh = wa.height;
589 c->oldbw = wa.border_width;
590 c->bw = 0;
591 c->isfloating = True;
592 /* reuse tags field as mapped status */
593 c->tags = 1;
594 updatesizehints(c);
595 updatesystrayicongeom(c, wa.width, wa.height);
596 XAddToSaveSet(dpy, c->win);
597 XSelectInput(dpy, c->win, StructureNotifyMask | PropertyChangeMask | ResizeRedirectMask);
598 XReparentWindow(dpy, c->win, systray->win, 0, 0);
599 /* use parents background color */
600 swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
601 XChangeWindowAttributes(dpy, c->win, CWBackPixel, &swa);
602 sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_EMBEDDED_NOTIFY, 0 , systray->win, XEMBED_EMBEDDED_VERSION);
603 /* FIXME not sure if I have to send these events, too */
604 sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_FOCUS_IN, 0 , systray->win, XEMBED_EMBEDDED_VERSION);
605 sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_WINDOW_ACTIVATE, 0 , systray->win, XEMBED_EMBEDDED_VERSION);
606 sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_MODALITY_ON, 0 , systray->win, XEMBED_EMBEDDED_VERSION);
607 XSync(dpy, False);
608 resizebarwin(selmon);
609 updatesystray();
610 setclientstate(c, NormalState);
611 }
612 return;
613 }
614 if (!c)
615 return;
616 if (cme->message_type == netatom[NetWMState]) {
617 if (cme->data.l[1] == netatom[NetWMFullscreen]
618 || cme->data.l[2] == netatom[NetWMFullscreen])
619 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
620 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
621 } else if (cme->message_type == netatom[NetActiveWindow]) {
622 if (c != selmon->sel && !c->isurgent)
623 seturgent(c, 1);
624 }
625 }
626
627 void
628 configure(Client *c)
629 {
630 XConfigureEvent ce;
631
632 ce.type = ConfigureNotify;
633 ce.display = dpy;
634 ce.event = c->win;
635 ce.window = c->win;
636 ce.x = c->x;
637 ce.y = c->y;
638 ce.width = c->w;
639 ce.height = c->h;
640 ce.border_width = c->bw;
641 ce.above = None;
642 ce.override_redirect = False;
643 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
644 }
645
646 void
647 configurenotify(XEvent *e)
648 {
649 Monitor *m;
650 Client *c;
651 XConfigureEvent *ev = &e->xconfigure;
652 int dirty;
653
654 /* TODO: updategeom handling sucks, needs to be simplified */
655 if (ev->window == root) {
656 dirty = (sw != ev->width || sh != ev->height);
657 sw = ev->width;
658 sh = ev->height;
659 if (updategeom() || dirty) {
660 drw_resize(drw, sw, bh);
661 updatebars();
662 for (m = mons; m; m = m->next) {
663 for (c = m->clients; c; c = c->next)
664 if (c->isfullscreen)
665 resizeclient(c, m->mx, m->my, m->mw, m->mh);
666 resizebarwin(m);
667 }
668 focus(NULL);
669 arrange(NULL);
670 }
671 }
672 }
673
674 void
675 configurerequest(XEvent *e)
676 {
677 Client *c;
678 Monitor *m;
679 XConfigureRequestEvent *ev = &e->xconfigurerequest;
680 XWindowChanges wc;
681
682 if ((c = wintoclient(ev->window))) {
683 if (ev->value_mask & CWBorderWidth)
684 c->bw = ev->border_width;
685 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
686 m = c->mon;
687 if (ev->value_mask & CWX) {
688 c->oldx = c->x;
689 c->x = m->mx + ev->x;
690 }
691 if (ev->value_mask & CWY) {
692 c->oldy = c->y;
693 c->y = m->my + ev->y;
694 }
695 if (ev->value_mask & CWWidth) {
696 c->oldw = c->w;
697 c->w = ev->width;
698 }
699 if (ev->value_mask & CWHeight) {
700 c->oldh = c->h;
701 c->h = ev->height;
702 }
703 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
704 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
705 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
706 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
707 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
708 configure(c);
709 if (ISVISIBLE(c))
710 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
711 } else
712 configure(c);
713 } else {
714 wc.x = ev->x;
715 wc.y = ev->y;
716 wc.width = ev->width;
717 wc.height = ev->height;
718 wc.border_width = ev->border_width;
719 wc.sibling = ev->above;
720 wc.stack_mode = ev->detail;
721 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
722 }
723 XSync(dpy, False);
724 }
725
726 Monitor *
727 createmon(void)
728 {
729 Monitor *m;
730 unsigned int i;
731
732 m = ecalloc(1, sizeof(Monitor));
733 m->tagset[0] = m->tagset[1] = 1;
734 m->mfact = mfact;
735 m->nmaster = nmaster;
736 m->showbar = showbar;
737 m->topbar = topbar;
738 m->lt[0] = &layouts[0];
739 m->lt[1] = &layouts[1 % LENGTH(layouts)];
740 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
741 m->pertag = ecalloc(1, sizeof(Pertag));
742 m->pertag->curtag = m->pertag->prevtag = 1;
743
744 for (i = 0; i <= LENGTH(tags); i++) {
745 m->pertag->nmasters[i] = m->nmaster;
746 m->pertag->mfacts[i] = m->mfact;
747
748 m->pertag->ltidxs[i][0] = m->lt[0];
749 m->pertag->ltidxs[i][1] = m->lt[1];
750 m->pertag->sellts[i] = m->sellt;
751
752 m->pertag->showbars[i] = m->showbar;
753 }
754
755 return m;
756 }
757
758 void
759 destroynotify(XEvent *e)
760 {
761 Client *c;
762 XDestroyWindowEvent *ev = &e->xdestroywindow;
763
764 if ((c = wintoclient(ev->window)))
765 unmanage(c, 1);
766 else if ((c = wintosystrayicon(ev->window))) {
767 removesystrayicon(c);
768 resizebarwin(selmon);
769 updatesystray();
770 }
771 }
772
773 void
774 detach(Client *c)
775 {
776 Client **tc;
777
778 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
779 *tc = c->next;
780 }
781
782 void
783 detachstack(Client *c)
784 {
785 Client **tc, *t;
786
787 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
788 *tc = c->snext;
789
790 if (c == c->mon->sel) {
791 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
792 c->mon->sel = t;
793 }
794 }
795
796 Monitor *
797 dirtomon(int dir)
798 {
799 Monitor *m = NULL;
800
801 if (dir > 0) {
802 if (!(m = selmon->next))
803 m = mons;
804 } else if (selmon == mons)
805 for (m = mons; m->next; m = m->next);
806 else
807 for (m = mons; m->next != selmon; m = m->next);
808 return m;
809 }
810
811 void
812 drawbar(Monitor *m)
813 {
814 int x, w, sw = 0, stw = 0;
815 int boxs = drw->fonts->h / 9;
816 int boxw = drw->fonts->h / 6 + 2;
817 unsigned int i, occ = 0, urg = 0;
818 Client *c;
819
820 if(showsystray && m == systraytomon(m))
821 stw = getsystraywidth();
822
823 /* draw status first so it can be overdrawn by tags later */
824 if (m == selmon || 1) { /* status is only drawn on selected monitor */
825 drw_setscheme(drw, scheme[SchemeNorm]);
826 sw = TEXTW(stext) - lrpad / 2 + 2; /* 2px right padding */
827 drw_text(drw, m->ww - sw - stw, 0, sw, bh, lrpad / 2 - 2, stext, 0);
828 }
829
830 resizebarwin(m);
831 for (c = m->clients; c; c = c->next) {
832 occ |= c->tags;
833 if (c->isurgent)
834 urg |= c->tags;
835 }
836 x = 0;
837 for (i = 0; i < LENGTH(tags); i++) {
838 w = TEXTW(tags[i]);
839 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
840 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
841 if (occ & 1 << i)
842 drw_rect(drw, x + boxs, boxs, boxw, boxw,
843 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
844 urg & 1 << i);
845 x += w;
846 }
847 w = blw = TEXTW(m->ltsymbol);
848 drw_setscheme(drw, scheme[SchemeNorm]);
849 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
850
851 if ((w = m->ww - sw - stw - x) > bh) {
852 if (m->sel) {
853 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
854 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
855 if (m->sel->isfloating)
856 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
857 } else {
858 drw_setscheme(drw, scheme[SchemeNorm]);
859 drw_rect(drw, x, 0, w, bh, 1, 1);
860 }
861 }
862 drw_map(drw, m->barwin, 0, 0, m->ww - stw, bh);
863 }
864
865 void
866 drawbars(void)
867 {
868 Monitor *m;
869
870 for (m = mons; m; m = m->next)
871 drawbar(m);
872 }
873
874 void
875 enternotify(XEvent *e)
876 {
877 Client *c;
878 Monitor *m;
879 XCrossingEvent *ev = &e->xcrossing;
880
881 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
882 return;
883 c = wintoclient(ev->window);
884 m = c ? c->mon : wintomon(ev->window);
885 if (m != selmon) {
886 unfocus(selmon->sel, 1);
887 selmon = m;
888 } else if (!c || c == selmon->sel)
889 return;
890 focus(c);
891 }
892
893 void
894 expose(XEvent *e)
895 {
896 Monitor *m;
897 XExposeEvent *ev = &e->xexpose;
898
899 if (ev->count == 0 && (m = wintomon(ev->window))) {
900 drawbar(m);
901 if (m == selmon)
902 updatesystray();
903 }
904 }
905
906 void
907 focus(Client *c)
908 {
909 if (!c || !ISVISIBLE(c))
910 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
911 if (selmon->sel && selmon->sel != c)
912 unfocus(selmon->sel, 0);
913 if (c) {
914 if (c->mon != selmon)
915 selmon = c->mon;
916 if (c->isurgent)
917 seturgent(c, 0);
918 detachstack(c);
919 attachstack(c);
920 grabbuttons(c, 1);
921 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
922 setfocus(c);
923 } else {
924 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
925 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
926 }
927 selmon->sel = c;
928 drawbars();
929 }
930
931 /* there are some broken focus acquiring clients needing extra handling */
932 void
933 focusin(XEvent *e)
934 {
935 XFocusChangeEvent *ev = &e->xfocus;
936
937 if (selmon->sel && ev->window != selmon->sel->win)
938 setfocus(selmon->sel);
939 }
940
941 void
942 focusmon(const Arg *arg)
943 {
944 Monitor *m;
945
946 if (!mons->next)
947 return;
948 if ((m = dirtomon(arg->i)) == selmon)
949 return;
950 unfocus(selmon->sel, 0);
951 selmon = m;
952 focus(NULL);
953 }
954
955 void
956 focusstack(const Arg *arg)
957 {
958 Client *c = NULL, *i;
959
960 if (!selmon->sel)
961 return;
962 if (arg->i > 0) {
963 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
964 if (!c)
965 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
966 } else {
967 for (i = selmon->clients; i != selmon->sel; i = i->next)
968 if (ISVISIBLE(i))
969 c = i;
970 if (!c)
971 for (; i; i = i->next)
972 if (ISVISIBLE(i))
973 c = i;
974 }
975 if (c) {
976 focus(c);
977 restack(selmon);
978 }
979 }
980
981 Atom
982 getatomprop(Client *c, Atom prop)
983 {
984 int di;
985 unsigned long dl;
986 unsigned char *p = NULL;
987 Atom da, atom = None;
988 /* FIXME getatomprop should return the number of items and a pointer to
989 * the stored data instead of this workaround */
990 Atom req = XA_ATOM;
991 if (prop == xatom[XembedInfo])
992 req = xatom[XembedInfo];
993
994 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, req,
995 &da, &di, &dl, &dl, &p) == Success && p) {
996 atom = *(Atom *)p;
997 if (da == xatom[XembedInfo] && dl == 2)
998 atom = ((Atom *)p)[1];
999 XFree(p);
1000 }
1001 return atom;
1002 }
1003
1004 int
1005 getrootptr(int *x, int *y)
1006 {
1007 int di;
1008 unsigned int dui;
1009 Window dummy;
1010
1011 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
1012 }
1013
1014 long
1015 getstate(Window w)
1016 {
1017 int format;
1018 long result = -1;
1019 unsigned char *p = NULL;
1020 unsigned long n, extra;
1021 Atom real;
1022
1023 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
1024 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
1025 return -1;
1026 if (n != 0)
1027 result = *p;
1028 XFree(p);
1029 return result;
1030 }
1031
1032 unsigned int
1033 getsystraywidth()
1034 {
1035 unsigned int w = 0;
1036 Client *i;
1037 if(showsystray)
1038 for(i = systray->icons; i; w += i->w + systrayspacing, i = i->next) ;
1039 return w ? w + systrayspacing : 1;
1040 }
1041
1042 int
1043 gettextprop(Window w, Atom atom, char *text, unsigned int size)
1044 {
1045 char **list = NULL;
1046 int n;
1047 XTextProperty name;
1048
1049 if (!text || size == 0)
1050 return 0;
1051 text[0] = '\0';
1052 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
1053 return 0;
1054 if (name.encoding == XA_STRING)
1055 strncpy(text, (char *)name.value, size - 1);
1056 else {
1057 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1058 strncpy(text, *list, size - 1);
1059 XFreeStringList(list);
1060 }
1061 }
1062 text[size - 1] = '\0';
1063 XFree(name.value);
1064 return 1;
1065 }
1066
1067 void
1068 grabbuttons(Client *c, int focused)
1069 {
1070 updatenumlockmask();
1071 {
1072 unsigned int i, j;
1073 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1074 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1075 if (!focused)
1076 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1077 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
1078 for (i = 0; i < LENGTH(buttons); i++)
1079 if (buttons[i].click == ClkClientWin)
1080 for (j = 0; j < LENGTH(modifiers); j++)
1081 XGrabButton(dpy, buttons[i].button,
1082 buttons[i].mask | modifiers[j],
1083 c->win, False, BUTTONMASK,
1084 GrabModeAsync, GrabModeSync, None, None);
1085 }
1086 }
1087
1088 void
1089 grabkeys(void)
1090 {
1091 updatenumlockmask();
1092 {
1093 unsigned int i, j;
1094 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1095 KeyCode code;
1096
1097 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1098 for (i = 0; i < LENGTH(keys); i++)
1099 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1100 for (j = 0; j < LENGTH(modifiers); j++)
1101 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1102 True, GrabModeAsync, GrabModeAsync);
1103 }
1104 }
1105
1106 void
1107 incnmaster(const Arg *arg)
1108 {
1109 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0);
1110 arrange(selmon);
1111 }
1112
1113 #ifdef XINERAMA
1114 static int
1115 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1116 {
1117 while (n--)
1118 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1119 && unique[n].width == info->width && unique[n].height == info->height)
1120 return 0;
1121 return 1;
1122 }
1123 #endif /* XINERAMA */
1124
1125 void
1126 keypress(XEvent *e)
1127 {
1128 unsigned int i;
1129 KeySym keysym;
1130 XKeyEvent *ev;
1131
1132 ev = &e->xkey;
1133 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1134 for (i = 0; i < LENGTH(keys); i++)
1135 if (keysym == keys[i].keysym
1136 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1137 && keys[i].func)
1138 keys[i].func(&(keys[i].arg));
1139 }
1140
1141 void
1142 killclient(const Arg *arg)
1143 {
1144 if (!selmon->sel)
1145 return;
1146 if (!sendevent(selmon->sel->win, wmatom[WMDelete], NoEventMask, wmatom[WMDelete], CurrentTime, 0 , 0, 0)) {
1147 XGrabServer(dpy);
1148 XSetErrorHandler(xerrordummy);
1149 XSetCloseDownMode(dpy, DestroyAll);
1150 XKillClient(dpy, selmon->sel->win);
1151 XSync(dpy, False);
1152 XSetErrorHandler(xerror);
1153 XUngrabServer(dpy);
1154 }
1155 }
1156
1157 void
1158 manage(Window w, XWindowAttributes *wa)
1159 {
1160 Client *c, *t = NULL;
1161 Window trans = None;
1162 XWindowChanges wc;
1163
1164 c = ecalloc(1, sizeof(Client));
1165 c->win = w;
1166 /* geometry */
1167 c->x = c->oldx = wa->x;
1168 c->y = c->oldy = wa->y;
1169 c->w = c->oldw = wa->width;
1170 c->h = c->oldh = wa->height;
1171 c->oldbw = wa->border_width;
1172
1173 updatetitle(c);
1174 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1175 c->mon = t->mon;
1176 c->tags = t->tags;
1177 } else {
1178 c->mon = selmon;
1179 applyrules(c);
1180 }
1181
1182 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1183 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1184 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1185 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1186 c->x = MAX(c->x, c->mon->mx);
1187 /* only fix client y-offset, if the client center might cover the bar */
1188 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1189 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1190 c->bw = borderpx;
1191
1192 wc.border_width = c->bw;
1193 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1194 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1195 configure(c); /* propagates border_width, if size doesn't change */
1196 updatewindowtype(c);
1197 updatesizehints(c);
1198 updatewmhints(c);
1199 c->sfx = c->x;
1200 c->sfy = c->y;
1201 c->sfw = c->w;
1202 c->sfh = c->h;
1203 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1204 grabbuttons(c, 0);
1205 if (!c->isfloating)
1206 c->isfloating = c->oldstate = trans != None || c->isfixed;
1207 if (c->isfloating)
1208 XRaiseWindow(dpy, c->win);
1209 attach(c);
1210 attachstack(c);
1211 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1212 (unsigned char *) &(c->win), 1);
1213 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1214 setclientstate(c, NormalState);
1215 if (c->mon == selmon)
1216 unfocus(selmon->sel, 0);
1217 c->mon->sel = c;
1218 arrange(c->mon);
1219 XMapWindow(dpy, c->win);
1220 focus(NULL);
1221 }
1222
1223 void
1224 mappingnotify(XEvent *e)
1225 {
1226 XMappingEvent *ev = &e->xmapping;
1227
1228 XRefreshKeyboardMapping(ev);
1229 if (ev->request == MappingKeyboard)
1230 grabkeys();
1231 }
1232
1233 void
1234 maprequest(XEvent *e)
1235 {
1236 static XWindowAttributes wa;
1237 XMapRequestEvent *ev = &e->xmaprequest;
1238 Client *i;
1239 if ((i = wintosystrayicon(ev->window))) {
1240 sendevent(i->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_WINDOW_ACTIVATE, 0, systray->win, XEMBED_EMBEDDED_VERSION);
1241 resizebarwin(selmon);
1242 updatesystray();
1243 }
1244
1245 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1246 return;
1247 if (wa.override_redirect)
1248 return;
1249 if (!wintoclient(ev->window))
1250 manage(ev->window, &wa);
1251 }
1252
1253 void
1254 monocle(Monitor *m)
1255 {
1256 unsigned int n = 0;
1257 Client *c;
1258
1259 for (c = m->clients; c; c = c->next)
1260 if (ISVISIBLE(c))
1261 n++;
1262 if (n > 0) /* override layout symbol */
1263 snprintf(m->ltsymbol, sizeof m->ltsymbol, "%s/%d", layouts[2].symbol, n);
1264 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1265 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1266 }
1267
1268 void
1269 motionnotify(XEvent *e)
1270 {
1271 static Monitor *mon = NULL;
1272 Monitor *m;
1273 XMotionEvent *ev = &e->xmotion;
1274
1275 if (ev->window != root)
1276 return;
1277 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1278 unfocus(selmon->sel, 1);
1279 selmon = m;
1280 focus(NULL);
1281 }
1282 mon = m;
1283 }
1284
1285 void
1286 movemouse(const Arg *arg)
1287 {
1288 int x, y, ocx, ocy, nx, ny;
1289 Client *c;
1290 Monitor *m;
1291 XEvent ev;
1292 Time lasttime = 0;
1293
1294 if (!(c = selmon->sel))
1295 return;
1296 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1297 return;
1298 restack(selmon);
1299 ocx = c->x;
1300 ocy = c->y;
1301 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1302 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1303 return;
1304 if (!getrootptr(&x, &y))
1305 return;
1306 do {
1307 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1308 switch(ev.type) {
1309 case ConfigureRequest:
1310 case Expose:
1311 case MapRequest:
1312 handler[ev.type](&ev);
1313 break;
1314 case MotionNotify:
1315 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1316 continue;
1317 lasttime = ev.xmotion.time;
1318
1319 nx = ocx + (ev.xmotion.x - x);
1320 ny = ocy + (ev.xmotion.y - y);
1321 if (abs(selmon->wx - nx) < snap)
1322 nx = selmon->wx;
1323 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1324 nx = selmon->wx + selmon->ww - WIDTH(c);
1325 if (abs(selmon->wy - ny) < snap)
1326 ny = selmon->wy;
1327 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1328 ny = selmon->wy + selmon->wh - HEIGHT(c);
1329 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1330 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1331 togglefloating(NULL);
1332 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1333 resize(c, nx, ny, c->w, c->h, 1);
1334 break;
1335 }
1336 } while (ev.type != ButtonRelease);
1337 XUngrabPointer(dpy, CurrentTime);
1338 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1339 sendmon(c, m);
1340 selmon = m;
1341 focus(NULL);
1342 }
1343 }
1344
1345 Client *
1346 nexttiled(Client *c)
1347 {
1348 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1349 return c;
1350 }
1351
1352 void
1353 pop(Client *c)
1354 {
1355 detach(c);
1356 attach(c);
1357 focus(c);
1358 arrange(c->mon);
1359 }
1360
1361 void
1362 propertynotify(XEvent *e)
1363 {
1364 Client *c;
1365 Window trans;
1366 XPropertyEvent *ev = &e->xproperty;
1367
1368 if ((c = wintosystrayicon(ev->window))) {
1369 if (ev->atom == XA_WM_NORMAL_HINTS) {
1370 updatesizehints(c);
1371 updatesystrayicongeom(c, c->w, c->h);
1372 }
1373 else
1374 updatesystrayiconstate(c, ev);
1375 resizebarwin(selmon);
1376 updatesystray();
1377 }
1378 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1379 updatestatus();
1380 else if (ev->state == PropertyDelete)
1381 return; /* ignore */
1382 else if ((c = wintoclient(ev->window))) {
1383 switch(ev->atom) {
1384 default: break;
1385 case XA_WM_TRANSIENT_FOR:
1386 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1387 (c->isfloating = (wintoclient(trans)) != NULL))
1388 arrange(c->mon);
1389 break;
1390 case XA_WM_NORMAL_HINTS:
1391 updatesizehints(c);
1392 break;
1393 case XA_WM_HINTS:
1394 updatewmhints(c);
1395 drawbars();
1396 break;
1397 }
1398 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1399 updatetitle(c);
1400 if (c == c->mon->sel)
1401 drawbar(c->mon);
1402 }
1403 if (ev->atom == netatom[NetWMWindowType])
1404 updatewindowtype(c);
1405 }
1406 }
1407
1408 void
1409 quit(const Arg *arg)
1410 {
1411 running = 0;
1412 }
1413
1414 Monitor *
1415 recttomon(int x, int y, int w, int h)
1416 {
1417 Monitor *m, *r = selmon;
1418 int a, area = 0;
1419
1420 for (m = mons; m; m = m->next)
1421 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1422 area = a;
1423 r = m;
1424 }
1425 return r;
1426 }
1427
1428 void
1429 removesystrayicon(Client *i)
1430 {
1431 Client **ii;
1432
1433 if (!showsystray || !i)
1434 return;
1435 for (ii = &systray->icons; *ii && *ii != i; ii = &(*ii)->next);
1436 if (ii)
1437 *ii = i->next;
1438 free(i);
1439 }
1440
1441
1442 void
1443 resize(Client *c, int x, int y, int w, int h, int interact)
1444 {
1445 if (applysizehints(c, &x, &y, &w, &h, interact))
1446 resizeclient(c, x, y, w, h);
1447 }
1448
1449 void
1450 resizebarwin(Monitor *m) {
1451 unsigned int w = m->ww;
1452 if (showsystray && m == systraytomon(m))
1453 w -= getsystraywidth();
1454 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, w, bh);
1455 }
1456
1457 void
1458 resizeclient(Client *c, int x, int y, int w, int h)
1459 {
1460 XWindowChanges wc;
1461
1462 c->oldx = c->x; c->x = wc.x = x;
1463 c->oldy = c->y; c->y = wc.y = y;
1464 c->oldw = c->w; c->w = wc.width = w;
1465 c->oldh = c->h; c->h = wc.height = h;
1466 wc.border_width = c->bw;
1467 if (((nexttiled(c->mon->clients) == c && !nexttiled(c->next))
1468 || &monocle == c->mon->lt[c->mon->sellt]->arrange)
1469 && !c->isfullscreen) {
1470 c->w = wc.width += c->bw * 2;
1471 c->h = wc.height += c->bw * 2;
1472 wc.border_width = 0;
1473 }
1474 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1475 configure(c);
1476 XSync(dpy, False);
1477 }
1478
1479 void
1480 resizemouse(const Arg *arg)
1481 {
1482 int ocx, ocy, nw, nh;
1483 Client *c;
1484 Monitor *m;
1485 XEvent ev;
1486 Time lasttime = 0;
1487
1488 if (!(c = selmon->sel))
1489 return;
1490 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1491 return;
1492 restack(selmon);
1493 ocx = c->x;
1494 ocy = c->y;
1495 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1496 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1497 return;
1498 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1499 do {
1500 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1501 switch(ev.type) {
1502 case ConfigureRequest:
1503 case Expose:
1504 case MapRequest:
1505 handler[ev.type](&ev);
1506 break;
1507 case MotionNotify:
1508 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1509 continue;
1510 lasttime = ev.xmotion.time;
1511
1512 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1513 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1514 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1515 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1516 {
1517 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1518 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1519 togglefloating(NULL);
1520 }
1521 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1522 resize(c, c->x, c->y, nw, nh, 1);
1523 break;
1524 }
1525 } while (ev.type != ButtonRelease);
1526 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1527 XUngrabPointer(dpy, CurrentTime);
1528 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1529 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1530 sendmon(c, m);
1531 selmon = m;
1532 focus(NULL);
1533 }
1534 }
1535
1536 void
1537 resizerequest(XEvent *e)
1538 {
1539 XResizeRequestEvent *ev = &e->xresizerequest;
1540 Client *i;
1541
1542 if ((i = wintosystrayicon(ev->window))) {
1543 updatesystrayicongeom(i, ev->width, ev->height);
1544 resizebarwin(selmon);
1545 updatesystray();
1546 }
1547 }
1548
1549 void
1550 restack(Monitor *m)
1551 {
1552 Client *c;
1553 XEvent ev;
1554 XWindowChanges wc;
1555
1556 drawbar(m);
1557 if (!m->sel)
1558 return;
1559 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1560 XRaiseWindow(dpy, m->sel->win);
1561 if (m->lt[m->sellt]->arrange) {
1562 wc.stack_mode = Below;
1563 wc.sibling = m->barwin;
1564 for (c = m->stack; c; c = c->snext)
1565 if (!c->isfloating && ISVISIBLE(c)) {
1566 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1567 wc.sibling = c->win;
1568 }
1569 }
1570 XSync(dpy, False);
1571 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1572 }
1573
1574 void
1575 run(void)
1576 {
1577 XEvent ev;
1578 /* main event loop */
1579 XSync(dpy, False);
1580 while (running && !XNextEvent(dpy, &ev))
1581 if (handler[ev.type])
1582 handler[ev.type](&ev); /* call handler */
1583 }
1584
1585 void
1586 scan(void)
1587 {
1588 unsigned int i, num;
1589 Window d1, d2, *wins = NULL;
1590 XWindowAttributes wa;
1591
1592 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1593 for (i = 0; i < num; i++) {
1594 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1595 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1596 continue;
1597 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1598 manage(wins[i], &wa);
1599 }
1600 for (i = 0; i < num; i++) { /* now the transients */
1601 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1602 continue;
1603 if (XGetTransientForHint(dpy, wins[i], &d1)
1604 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1605 manage(wins[i], &wa);
1606 }
1607 if (wins)
1608 XFree(wins);
1609 }
1610 }
1611
1612 void
1613 sendmon(Client *c, Monitor *m)
1614 {
1615 if (c->mon == m)
1616 return;
1617 unfocus(c, 1);
1618 detach(c);
1619 detachstack(c);
1620 c->mon = m;
1621 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1622 attach(c);
1623 attachstack(c);
1624 focus(NULL);
1625 arrange(NULL);
1626 }
1627
1628 void
1629 setclientstate(Client *c, long state)
1630 {
1631 long data[] = { state, None };
1632
1633 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1634 PropModeReplace, (unsigned char *)data, 2);
1635 }
1636
1637 int
1638 sendevent(Window w, Atom proto, int mask, long d0, long d1, long d2, long d3, long d4)
1639 {
1640 int n;
1641 Atom *protocols, mt;
1642 int exists = 0;
1643 XEvent ev;
1644
1645 if (proto == wmatom[WMTakeFocus] || proto == wmatom[WMDelete]) {
1646 mt = wmatom[WMProtocols];
1647 if (XGetWMProtocols(dpy, w, &protocols, &n)) {
1648 while (!exists && n--)
1649 exists = protocols[n] == proto;
1650 XFree(protocols);
1651 }
1652 }
1653 else {
1654 exists = True;
1655 mt = proto;
1656 }
1657 if (exists) {
1658 ev.type = ClientMessage;
1659 ev.xclient.window = w;
1660 ev.xclient.message_type = mt;
1661 ev.xclient.format = 32;
1662 ev.xclient.data.l[0] = d0;
1663 ev.xclient.data.l[1] = d1;
1664 ev.xclient.data.l[2] = d2;
1665 ev.xclient.data.l[3] = d3;
1666 ev.xclient.data.l[4] = d4;
1667 XSendEvent(dpy, w, False, mask, &ev);
1668 }
1669 return exists;
1670 }
1671
1672 void
1673 setfocus(Client *c)
1674 {
1675 if (!c->neverfocus) {
1676 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1677 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1678 XA_WINDOW, 32, PropModeReplace,
1679 (unsigned char *) &(c->win), 1);
1680 }
1681 sendevent(c->win, wmatom[WMTakeFocus], NoEventMask, wmatom[WMTakeFocus], CurrentTime, 0, 0, 0);
1682 }
1683
1684 void
1685 setfullscreen(Client *c, int fullscreen)
1686 {
1687 if (fullscreen && !c->isfullscreen) {
1688 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1689 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1690 c->isfullscreen = 1;
1691 c->oldstate = c->isfloating;
1692 c->oldbw = c->bw;
1693 c->bw = 0;
1694 c->isfloating = 1;
1695 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1696 XRaiseWindow(dpy, c->win);
1697 } else if (!fullscreen && c->isfullscreen){
1698 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1699 PropModeReplace, (unsigned char*)0, 0);
1700 c->isfullscreen = 0;
1701 c->isfloating = c->oldstate;
1702 c->bw = c->oldbw;
1703 c->x = c->oldx;
1704 c->y = c->oldy;
1705 c->w = c->oldw;
1706 c->h = c->oldh;
1707 resizeclient(c, c->x, c->y, c->w, c->h);
1708 arrange(c->mon);
1709 }
1710 }
1711
1712 void
1713 setlayout(const Arg *arg)
1714 {
1715 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1716 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1;
1717 if (arg && arg->v)
1718 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v;
1719 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1720 if (selmon->sel)
1721 arrange(selmon);
1722 else
1723 drawbar(selmon);
1724 }
1725
1726 /* arg > 1.0 will set mfact absolutely */
1727 void
1728 setmfact(const Arg *arg)
1729 {
1730 float f;
1731
1732 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1733 return;
1734 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1735 if (f < 0.1 || f > 0.9)
1736 return;
1737 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
1738 arrange(selmon);
1739 }
1740
1741 void
1742 setup(void)
1743 {
1744 int i;
1745 XSetWindowAttributes wa;
1746 Atom utf8string;
1747
1748 /* clean up any zombies immediately */
1749 sigchld(0);
1750
1751 /* init screen */
1752 screen = DefaultScreen(dpy);
1753 sw = DisplayWidth(dpy, screen);
1754 sh = DisplayHeight(dpy, screen);
1755 root = RootWindow(dpy, screen);
1756 drw = drw_create(dpy, screen, root, sw, sh);
1757 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1758 die("no fonts could be loaded.");
1759 lrpad = drw->fonts->h;
1760 bh = drw->fonts->h + 2;
1761 updategeom();
1762 /* init atoms */
1763 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1764 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1765 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1766 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1767 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1768 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1769 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1770 netatom[NetSystemTray] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False);
1771 netatom[NetSystemTrayOP] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_OPCODE", False);
1772 netatom[NetSystemTrayOrientation] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION", False);
1773 netatom[NetSystemTrayOrientationHorz] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION_HORZ", False);
1774 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1775 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1776 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1777 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1778 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1779 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1780 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1781 xatom[Manager] = XInternAtom(dpy, "MANAGER", False);
1782 xatom[Xembed] = XInternAtom(dpy, "_XEMBED", False);
1783 xatom[XembedInfo] = XInternAtom(dpy, "_XEMBED_INFO", False);
1784 /* init cursors */
1785 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1786 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1787 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1788 /* init appearance */
1789 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1790 for (i = 0; i < LENGTH(colors); i++)
1791 scheme[i] = drw_scm_create(drw, colors[i], 3);
1792 /* init system tray */
1793 updatesystray();
1794 /* init bars */
1795 updatebars();
1796 updatestatus();
1797 /* supporting window for NetWMCheck */
1798 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1799 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1800 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1801 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1802 PropModeReplace, (unsigned char *) "dwm", 3);
1803 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1804 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1805 /* EWMH support per view */
1806 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1807 PropModeReplace, (unsigned char *) netatom, NetLast);
1808 XDeleteProperty(dpy, root, netatom[NetClientList]);
1809 /* select events */
1810 wa.cursor = cursor[CurNormal]->cursor;
1811 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1812 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1813 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1814 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1815 XSelectInput(dpy, root, wa.event_mask);
1816 grabkeys();
1817 focus(NULL);
1818 }
1819
1820
1821 void
1822 seturgent(Client *c, int urg)
1823 {
1824 XWMHints *wmh;
1825
1826 c->isurgent = urg;
1827 if (!(wmh = XGetWMHints(dpy, c->win)))
1828 return;
1829 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1830 XSetWMHints(dpy, c->win, wmh);
1831 XFree(wmh);
1832 }
1833
1834 void
1835 showhide(Client *c)
1836 {
1837 if (!c)
1838 return;
1839 if (ISVISIBLE(c)) {
1840 /* show clients top down */
1841 XMoveWindow(dpy, c->win, c->x, c->y);
1842 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1843 resize(c, c->x, c->y, c->w, c->h, 0);
1844 showhide(c->snext);
1845 } else {
1846 /* hide clients bottom up */
1847 showhide(c->snext);
1848 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1849 }
1850 }
1851
1852 void
1853 sigchld(int unused)
1854 {
1855 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1856 die("can't install SIGCHLD handler:");
1857 while (0 < waitpid(-1, NULL, WNOHANG));
1858 }
1859
1860 void
1861 spawn(const Arg *arg)
1862 {
1863 if (arg->v == dmenucmd)
1864 dmenumon[0] = '0' + selmon->num;
1865 if (fork() == 0) {
1866 if (dpy)
1867 close(ConnectionNumber(dpy));
1868 setsid();
1869 execvp(((char **)arg->v)[0], (char **)arg->v);
1870 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1871 perror(" failed");
1872 exit(EXIT_SUCCESS);
1873 }
1874 }
1875
1876 void
1877 tag(const Arg *arg)
1878 {
1879 if (selmon->sel && arg->ui & TAGMASK) {
1880 selmon->sel->tags = arg->ui & TAGMASK;
1881 focus(NULL);
1882 arrange(selmon);
1883 }
1884 }
1885
1886 void
1887 tagmon(const Arg *arg)
1888 {
1889 if (!selmon->sel || !mons->next)
1890 return;
1891 sendmon(selmon->sel, dirtomon(arg->i));
1892 }
1893
1894 void
1895 tile(Monitor *m) {
1896 unsigned int i, n, h, r, g = 0, mw, my, ty;
1897 Client *c;
1898
1899 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1900 if (n == 0)
1901 return;
1902
1903 if(n > m->nmaster)
1904 mw = m->nmaster ? (m->ww - (g = gappx)) * m->mfact : 0;
1905 else
1906 mw = m->ww;
1907 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1908 if(i < m->nmaster) {
1909 r = MIN(n, m->nmaster) - i;
1910 h = (m->wh - my - gappx * (r - 1)) / r;
1911 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1912 my += HEIGHT(c) + gappx;
1913 }
1914 else {
1915 r = n - i;
1916 h = (m->wh - ty - gappx * (r - 1)) / r;
1917 resize(c, m->wx + mw + g, m->wy + ty, m->ww - mw - g - (2*c->bw), h - (2*c->bw), False);
1918 ty += HEIGHT(c) + gappx;
1919 }
1920 }
1921
1922 void
1923 togglebar(const Arg *arg)
1924 {
1925 selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar;
1926 updatebarpos(selmon);
1927 resizebarwin(selmon);
1928 if (showsystray) {
1929 XWindowChanges wc;
1930 if (!selmon->showbar)
1931 wc.y = -bh;
1932 else if (selmon->showbar) {
1933 wc.y = 0;
1934 if (!selmon->topbar)
1935 wc.y = selmon->mh - bh;
1936 }
1937 XConfigureWindow(dpy, systray->win, CWY, &wc);
1938 }
1939 arrange(selmon);
1940 }
1941
1942 void
1943 togglefloating(const Arg *arg)
1944 {
1945 if (!selmon->sel)
1946 return;
1947 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1948 return;
1949 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1950 if (selmon->sel->isfloating)
1951 /* restore last known float dimensions */
1952 resize(selmon->sel, selmon->sel->sfx, selmon->sel->sfy,
1953 selmon->sel->sfw, selmon->sel->sfh, False);
1954 else {
1955 /* save last known float dimensions */
1956 selmon->sel->sfx = selmon->sel->x;
1957 selmon->sel->sfy = selmon->sel->y;
1958 selmon->sel->sfw = selmon->sel->w;
1959 selmon->sel->sfh = selmon->sel->h;
1960 }
1961 arrange(selmon);
1962 }
1963
1964 void
1965 toggletag(const Arg *arg)
1966 {
1967 unsigned int newtags;
1968
1969 if (!selmon->sel)
1970 return;
1971 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1972 if (newtags) {
1973 selmon->sel->tags = newtags;
1974 focus(NULL);
1975 arrange(selmon);
1976 }
1977 }
1978
1979 void
1980 toggleview(const Arg *arg)
1981 {
1982 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1983 int i;
1984
1985 if (newtagset) {
1986 selmon->tagset[selmon->seltags] = newtagset;
1987
1988 if (newtagset == ~0) {
1989 selmon->pertag->prevtag = selmon->pertag->curtag;
1990 selmon->pertag->curtag = 0;
1991 }
1992
1993 /* test if the user did not select the same tag */
1994 if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
1995 selmon->pertag->prevtag = selmon->pertag->curtag;
1996 for (i = 0; !(newtagset & 1 << i); i++) ;
1997 selmon->pertag->curtag = i + 1;
1998 }
1999
2000 /* apply settings for this view */
2001 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
2002 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
2003 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
2004 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
2005 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
2006
2007 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
2008 togglebar(NULL);
2009
2010 focus(NULL);
2011 arrange(selmon);
2012 }
2013 }
2014
2015 void
2016 unfocus(Client *c, int setfocus)
2017 {
2018 if (!c)
2019 return;
2020 grabbuttons(c, 0);
2021 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
2022 if (setfocus) {
2023 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
2024 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
2025 }
2026 }
2027
2028 void
2029 unmanage(Client *c, int destroyed)
2030 {
2031 Monitor *m = c->mon;
2032 XWindowChanges wc;
2033
2034 detach(c);
2035 detachstack(c);
2036 if (!destroyed) {
2037 wc.border_width = c->oldbw;
2038 XGrabServer(dpy); /* avoid race conditions */
2039 XSetErrorHandler(xerrordummy);
2040 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
2041 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
2042 setclientstate(c, WithdrawnState);
2043 XSync(dpy, False);
2044 XSetErrorHandler(xerror);
2045 XUngrabServer(dpy);
2046 }
2047 free(c);
2048 focus(NULL);
2049 updateclientlist();
2050 arrange(m);
2051 }
2052
2053 void
2054 unmapnotify(XEvent *e)
2055 {
2056 Client *c;
2057 XUnmapEvent *ev = &e->xunmap;
2058
2059 if ((c = wintoclient(ev->window))) {
2060 if (ev->send_event)
2061 setclientstate(c, WithdrawnState);
2062 else
2063 unmanage(c, 0);
2064 }
2065 else if ((c = wintosystrayicon(ev->window))) {
2066 /* KLUDGE! sometimes icons occasionally unmap their windows, but do
2067 * _not_ destroy them. We map those windows back */
2068 XMapRaised(dpy, c->win);
2069 updatesystray();
2070 }
2071 }
2072
2073 void
2074 updatebars(void)
2075 {
2076 unsigned int w;
2077 Monitor *m;
2078 XSetWindowAttributes wa = {
2079 .override_redirect = True,
2080 .background_pixmap = ParentRelative,
2081 .event_mask = ButtonPressMask|ExposureMask
2082 };
2083 XClassHint ch = {"dwm", "dwm"};
2084 for (m = mons; m; m = m->next) {
2085 if (m->barwin)
2086 continue;
2087 w = m->ww;
2088 if (showsystray && m == systraytomon(m))
2089 w -= getsystraywidth();
2090 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, w, bh, 0, DefaultDepth(dpy, screen),
2091 CopyFromParent, DefaultVisual(dpy, screen),
2092 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
2093 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2094 if (showsystray && m == systraytomon(m))
2095 XMapRaised(dpy, systray->win);
2096 XMapRaised(dpy, m->barwin);
2097 XSetClassHint(dpy, m->barwin, &ch);
2098 }
2099 }
2100
2101 void
2102 updatebarpos(Monitor *m)
2103 {
2104 m->wy = m->my;
2105 m->wh = m->mh;
2106 if (m->showbar) {
2107 m->wh -= bh;
2108 m->by = m->topbar ? m->wy : m->wy + m->wh;
2109 m->wy = m->topbar ? m->wy + bh : m->wy;
2110 } else
2111 m->by = -bh;
2112 }
2113
2114 void
2115 updateclientlist()
2116 {
2117 Client *c;
2118 Monitor *m;
2119
2120 XDeleteProperty(dpy, root, netatom[NetClientList]);
2121 for (m = mons; m; m = m->next)
2122 for (c = m->clients; c; c = c->next)
2123 XChangeProperty(dpy, root, netatom[NetClientList],
2124 XA_WINDOW, 32, PropModeAppend,
2125 (unsigned char *) &(c->win), 1);
2126 }
2127
2128 int
2129 updategeom(void)
2130 {
2131 int dirty = 0;
2132
2133 #ifdef XINERAMA
2134 if (XineramaIsActive(dpy)) {
2135 int i, j, n, nn;
2136 Client *c;
2137 Monitor *m;
2138 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2139 XineramaScreenInfo *unique = NULL;
2140
2141 for (n = 0, m = mons; m; m = m->next, n++);
2142 /* only consider unique geometries as separate screens */
2143 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
2144 for (i = 0, j = 0; i < nn; i++)
2145 if (isuniquegeom(unique, j, &info[i]))
2146 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2147 XFree(info);
2148 nn = j;
2149 if (n <= nn) { /* new monitors available */
2150 for (i = 0; i < (nn - n); i++) {
2151 for (m = mons; m && m->next; m = m->next);
2152 if (m)
2153 m->next = createmon();
2154 else
2155 mons = createmon();
2156 }
2157 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2158 if (i >= n
2159 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2160 || unique[i].width != m->mw || unique[i].height != m->mh)
2161 {
2162 dirty = 1;
2163 m->num = i;
2164 m->mx = m->wx = unique[i].x_org;
2165 m->my = m->wy = unique[i].y_org;
2166 m->mw = m->ww = unique[i].width;
2167 m->mh = m->wh = unique[i].height;
2168 updatebarpos(m);
2169 }
2170 } else { /* less monitors available nn < n */
2171 for (i = nn; i < n; i++) {
2172 for (m = mons; m && m->next; m = m->next);
2173 while ((c = m->clients)) {
2174 dirty = 1;
2175 m->clients = c->next;
2176 detachstack(c);
2177 c->mon = mons;
2178 attach(c);
2179 attachstack(c);
2180 }
2181 if (m == selmon)
2182 selmon = mons;
2183 cleanupmon(m);
2184 }
2185 }
2186 free(unique);
2187 } else
2188 #endif /* XINERAMA */
2189 { /* default monitor setup */
2190 if (!mons)
2191 mons = createmon();
2192 if (mons->mw != sw || mons->mh != sh) {
2193 dirty = 1;
2194 mons->mw = mons->ww = sw;
2195 mons->mh = mons->wh = sh;
2196 updatebarpos(mons);
2197 }
2198 }
2199 if (dirty) {
2200 selmon = mons;
2201 selmon = wintomon(root);
2202 }
2203 return dirty;
2204 }
2205
2206 void
2207 updatenumlockmask(void)
2208 {
2209 unsigned int i, j;
2210 XModifierKeymap *modmap;
2211
2212 numlockmask = 0;
2213 modmap = XGetModifierMapping(dpy);
2214 for (i = 0; i < 8; i++)
2215 for (j = 0; j < modmap->max_keypermod; j++)
2216 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2217 == XKeysymToKeycode(dpy, XK_Num_Lock))
2218 numlockmask = (1 << i);
2219 XFreeModifiermap(modmap);
2220 }
2221
2222 void
2223 updatesizehints(Client *c)
2224 {
2225 long msize;
2226 XSizeHints size;
2227
2228 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2229 /* size is uninitialized, ensure that size.flags aren't used */
2230 size.flags = PSize;
2231 if (size.flags & PBaseSize) {
2232 c->basew = size.base_width;
2233 c->baseh = size.base_height;
2234 } else if (size.flags & PMinSize) {
2235 c->basew = size.min_width;
2236 c->baseh = size.min_height;
2237 } else
2238 c->basew = c->baseh = 0;
2239 if (size.flags & PResizeInc) {
2240 c->incw = size.width_inc;
2241 c->inch = size.height_inc;
2242 } else
2243 c->incw = c->inch = 0;
2244 if (size.flags & PMaxSize) {
2245 c->maxw = size.max_width;
2246 c->maxh = size.max_height;
2247 } else
2248 c->maxw = c->maxh = 0;
2249 if (size.flags & PMinSize) {
2250 c->minw = size.min_width;
2251 c->minh = size.min_height;
2252 } else if (size.flags & PBaseSize) {
2253 c->minw = size.base_width;
2254 c->minh = size.base_height;
2255 } else
2256 c->minw = c->minh = 0;
2257 if (size.flags & PAspect) {
2258 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2259 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2260 } else
2261 c->maxa = c->mina = 0.0;
2262 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2263 }
2264
2265 void
2266 updatestatus(void)
2267 {
2268 Monitor* m;
2269 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2270 strcpy(stext, "dwm-"VERSION);
2271 for(m = mons; m; m = m->next)
2272 drawbar(m);
2273 }
2274
2275 void
2276 updatesystrayicongeom(Client *i, int w, int h)
2277 {
2278 if (i) {
2279 i->h = bh;
2280 if (w == h)
2281 i->w = bh;
2282 else if (h == bh)
2283 i->w = w;
2284 else
2285 i->w = (int) ((float)bh * ((float)w / (float)h));
2286 applysizehints(i, &(i->x), &(i->y), &(i->w), &(i->h), False);
2287 /* force icons into the systray dimenons if they don't want to */
2288 if (i->h > bh) {
2289 if (i->w == i->h)
2290 i->w = bh;
2291 else
2292 i->w = (int) ((float)bh * ((float)i->w / (float)i->h));
2293 i->h = bh;
2294 }
2295 }
2296 }
2297
2298 void
2299 updatesystrayiconstate(Client *i, XPropertyEvent *ev)
2300 {
2301 long flags;
2302 int code = 0;
2303
2304 if (!showsystray || !i || ev->atom != xatom[XembedInfo] ||
2305 !(flags = getatomprop(i, xatom[XembedInfo])))
2306 return;
2307
2308 if (flags & XEMBED_MAPPED && !i->tags) {
2309 i->tags = 1;
2310 code = XEMBED_WINDOW_ACTIVATE;
2311 XMapRaised(dpy, i->win);
2312 setclientstate(i, NormalState);
2313 }
2314 else if (!(flags & XEMBED_MAPPED) && i->tags) {
2315 i->tags = 0;
2316 code = XEMBED_WINDOW_DEACTIVATE;
2317 XUnmapWindow(dpy, i->win);
2318 setclientstate(i, WithdrawnState);
2319 }
2320 else
2321 return;
2322 sendevent(i->win, xatom[Xembed], StructureNotifyMask, CurrentTime, code, 0,
2323 systray->win, XEMBED_EMBEDDED_VERSION);
2324 }
2325
2326 void
2327 updatesystray(void)
2328 {
2329 XSetWindowAttributes wa;
2330 XWindowChanges wc;
2331 Client *i;
2332 Monitor *m = systraytomon(NULL);
2333 unsigned int x = m->mx + m->mw;
2334 unsigned int w = 1;
2335
2336 if (!showsystray)
2337 return;
2338 if (!systray) {
2339 /* init systray */
2340 if (!(systray = (Systray *)calloc(1, sizeof(Systray))))
2341 die("fatal: could not malloc() %u bytes\n", sizeof(Systray));
2342 systray->win = XCreateSimpleWindow(dpy, root, x, m->by, w, bh, 0, 0, scheme[SchemeSel][ColBg].pixel);
2343 wa.event_mask = ButtonPressMask | ExposureMask;
2344 wa.override_redirect = True;
2345 wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
2346 XSelectInput(dpy, systray->win, SubstructureNotifyMask);
2347 XChangeProperty(dpy, systray->win, netatom[NetSystemTrayOrientation], XA_CARDINAL, 32,
2348 PropModeReplace, (unsigned char *)&netatom[NetSystemTrayOrientationHorz], 1);
2349 XChangeWindowAttributes(dpy, systray->win, CWEventMask|CWOverrideRedirect|CWBackPixel, &wa);
2350 XMapRaised(dpy, systray->win);
2351 XSetSelectionOwner(dpy, netatom[NetSystemTray], systray->win, CurrentTime);
2352 if (XGetSelectionOwner(dpy, netatom[NetSystemTray]) == systray->win) {
2353 sendevent(root, xatom[Manager], StructureNotifyMask, CurrentTime, netatom[NetSystemTray], systray->win, 0, 0);
2354 XSync(dpy, False);
2355 }
2356 else {
2357 fprintf(stderr, "dwm: unable to obtain system tray.\n");
2358 free(systray);
2359 systray = NULL;
2360 return;
2361 }
2362 }
2363 for (w = 0, i = systray->icons; i; i = i->next) {
2364 /* make sure the background color stays the same */
2365 wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
2366 XChangeWindowAttributes(dpy, i->win, CWBackPixel, &wa);
2367 XMapRaised(dpy, i->win);
2368 w += systrayspacing;
2369 i->x = w;
2370 XMoveResizeWindow(dpy, i->win, i->x, 0, i->w, i->h);
2371 w += i->w;
2372 if (i->mon != m)
2373 i->mon = m;
2374 }
2375 w = w ? w + systrayspacing : 1;
2376 x -= w;
2377 XMoveResizeWindow(dpy, systray->win, x, m->by, w, bh);
2378 wc.x = x; wc.y = m->by; wc.width = w; wc.height = bh;
2379 wc.stack_mode = Above; wc.sibling = m->barwin;
2380 XConfigureWindow(dpy, systray->win, CWX|CWY|CWWidth|CWHeight|CWSibling|CWStackMode, &wc);
2381 XMapWindow(dpy, systray->win);
2382 XMapSubwindows(dpy, systray->win);
2383 /* redraw background */
2384 XSetForeground(dpy, drw->gc, scheme[SchemeNorm][ColBg].pixel);
2385 XFillRectangle(dpy, systray->win, drw->gc, 0, 0, w, bh);
2386 XSync(dpy, False);
2387 }
2388
2389 void
2390 updatetitle(Client *c)
2391 {
2392 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2393 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2394 if (c->name[0] == '\0') /* hack to mark broken clients */
2395 strcpy(c->name, broken);
2396 }
2397
2398 void
2399 updatewindowtype(Client *c)
2400 {
2401 Atom state = getatomprop(c, netatom[NetWMState]);
2402 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2403
2404 if (state == netatom[NetWMFullscreen])
2405 setfullscreen(c, 1);
2406 if (wtype == netatom[NetWMWindowTypeDialog])
2407 c->isfloating = 1;
2408 }
2409
2410 void
2411 updatewmhints(Client *c)
2412 {
2413 XWMHints *wmh;
2414
2415 if ((wmh = XGetWMHints(dpy, c->win))) {
2416 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2417 wmh->flags &= ~XUrgencyHint;
2418 XSetWMHints(dpy, c->win, wmh);
2419 } else
2420 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2421 if (wmh->flags & InputHint)
2422 c->neverfocus = !wmh->input;
2423 else
2424 c->neverfocus = 0;
2425 XFree(wmh);
2426 }
2427 }
2428
2429 void
2430 view(const Arg *arg)
2431 {
2432 int i;
2433 unsigned int tmptag;
2434
2435 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2436 return;
2437 selmon->seltags ^= 1; /* toggle sel tagset */
2438 if (arg->ui & TAGMASK) {
2439 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2440 selmon->pertag->prevtag = selmon->pertag->curtag;
2441
2442 if (arg->ui == ~0)
2443 selmon->pertag->curtag = 0;
2444 else {
2445 for (i = 0; !(arg->ui & 1 << i); i++) ;
2446 selmon->pertag->curtag = i + 1;
2447 }
2448 } else {
2449 tmptag = selmon->pertag->prevtag;
2450 selmon->pertag->prevtag = selmon->pertag->curtag;
2451 selmon->pertag->curtag = tmptag;
2452 }
2453
2454 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
2455 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
2456 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
2457 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
2458 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
2459
2460 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
2461 togglebar(NULL);
2462
2463 focus(NULL);
2464 arrange(selmon);
2465 }
2466
2467 Client *
2468 wintoclient(Window w)
2469 {
2470 Client *c;
2471 Monitor *m;
2472
2473 for (m = mons; m; m = m->next)
2474 for (c = m->clients; c; c = c->next)
2475 if (c->win == w)
2476 return c;
2477 return NULL;
2478 }
2479
2480 Client *
2481 wintosystrayicon(Window w) {
2482 Client *i = NULL;
2483
2484 if (!showsystray || !w)
2485 return i;
2486 for (i = systray->icons; i && i->win != w; i = i->next) ;
2487 return i;
2488 }
2489
2490 Monitor *
2491 wintomon(Window w)
2492 {
2493 int x, y;
2494 Client *c;
2495 Monitor *m;
2496
2497 if (w == root && getrootptr(&x, &y))
2498 return recttomon(x, y, 1, 1);
2499 for (m = mons; m; m = m->next)
2500 if (w == m->barwin)
2501 return m;
2502 if ((c = wintoclient(w)))
2503 return c->mon;
2504 return selmon;
2505 }
2506
2507 /* There's no way to check accesses to destroyed windows, thus those cases are
2508 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2509 * default error handler, which may call exit. */
2510 int
2511 xerror(Display *dpy, XErrorEvent *ee)
2512 {
2513 if (ee->error_code == BadWindow
2514 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2515 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2516 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2517 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2518 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2519 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2520 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2521 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2522 return 0;
2523 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2524 ee->request_code, ee->error_code);
2525 return xerrorxlib(dpy, ee); /* may call exit */
2526 }
2527
2528 int
2529 xerrordummy(Display *dpy, XErrorEvent *ee)
2530 {
2531 return 0;
2532 }
2533
2534 /* Startup Error handler to check if another window manager
2535 * is already running. */
2536 int
2537 xerrorstart(Display *dpy, XErrorEvent *ee)
2538 {
2539 die("dwm: another window manager is already running");
2540 return -1;
2541 }
2542
2543 Monitor *
2544 systraytomon(Monitor *m) {
2545 Monitor *t;
2546 int i, n;
2547 if(!systraypinning) {
2548 if(!m)
2549 return selmon;
2550 return m == selmon ? m : NULL;
2551 }
2552 for(n = 1, t = mons; t && t->next; n++, t = t->next) ;
2553 for(i = 1, t = mons; t && t->next && i < systraypinning; i++, t = t->next) ;
2554 if(systraypinningfailfirst && n < systraypinning)
2555 return mons;
2556 return t;
2557 }
2558
2559 void
2560 zoom(const Arg *arg)
2561 {
2562 Client *c = selmon->sel;
2563
2564 if (!selmon->lt[selmon->sellt]->arrange
2565 || (selmon->sel && selmon->sel->isfloating))
2566 return;
2567 if (c == nexttiled(selmon->clients))
2568 if (!c || !(c = nexttiled(c->next)))
2569 return;
2570 pop(c);
2571 }
2572
2573 int
2574 main(int argc, char *argv[])
2575 {
2576 if (argc == 2 && !strcmp("-v", argv[1]))
2577 die("dwm-"VERSION);
2578 else if (argc != 1)
2579 die("usage: dwm [-v]");
2580 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2581 fputs("warning: no locale support\n", stderr);
2582 if (!(dpy = XOpenDisplay(NULL)))
2583 die("dwm: cannot open display");
2584 checkotherwm();
2585 setup();
2586 #ifdef __OpenBSD__
2587 if (pledge("stdio rpath proc exec", NULL) == -1)
2588 die("pledge");
2589 #endif /* __OpenBSD__ */
2590 scan();
2591 run();
2592 cleanup();
2593 XCloseDisplay(dpy);
2594 return EXIT_SUCCESS;
2595 }