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