1 /* See LICENSE file for copyright and license details.
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.
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
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
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
31 #include <sys/types.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
42 #include <X11/Xft/Xft.h>
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_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
61 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
62 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
63 enum { NetSupported, NetWMName, NetWMState,
64 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
65 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
66 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
67 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
68 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
81 void (*func)(const Arg *arg);
85 typedef struct Monitor Monitor;
86 typedef struct Client Client;
91 int oldx, oldy, oldw, oldh;
92 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
95 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
105 void (*func)(const Arg *);
111 void (*arrange)(Monitor *);
119 int by; /* bar geometry */
120 int mx, my, mw, mh; /* screen size */
121 int wx, wy, ww, wh; /* window area */
122 unsigned int seltags;
124 unsigned int tagset[2];
137 const char *instance;
144 /* function declarations */
145 static void applyrules(Client *c);
146 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
147 static void arrange(Monitor *m);
148 static void arrangemon(Monitor *m);
149 static void attach(Client *c);
150 static void attachstack(Client *c);
151 static void buttonpress(XEvent *e);
152 static void checkotherwm(void);
153 static void cleanup(void);
154 static void cleanupmon(Monitor *mon);
155 static void clearurgent(Client *c);
156 static void clientmessage(XEvent *e);
157 static void configure(Client *c);
158 static void configurenotify(XEvent *e);
159 static void configurerequest(XEvent *e);
160 static Monitor *createmon(void);
161 static void destroynotify(XEvent *e);
162 static void detach(Client *c);
163 static void detachstack(Client *c);
164 static Monitor *dirtomon(int dir);
165 static void drawbar(Monitor *m);
166 static void drawbars(void);
167 static void enternotify(XEvent *e);
168 static void expose(XEvent *e);
169 static void focus(Client *c);
170 static void focusin(XEvent *e);
171 static void focusmon(const Arg *arg);
172 static void focusstack(const Arg *arg);
173 static int getrootptr(int *x, int *y);
174 static long getstate(Window w);
175 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
176 static void grabbuttons(Client *c, int focused);
177 static void grabkeys(void);
178 static void incnmaster(const Arg *arg);
179 static void keypress(XEvent *e);
180 static void killclient(const Arg *arg);
181 static void manage(Window w, XWindowAttributes *wa);
182 static void mappingnotify(XEvent *e);
183 static void maprequest(XEvent *e);
184 static void monocle(Monitor *m);
185 static void motionnotify(XEvent *e);
186 static void movemouse(const Arg *arg);
187 static Client *nexttiled(Client *c);
188 static void pop(Client *);
189 static void propertynotify(XEvent *e);
190 static void quit(const Arg *arg);
191 static Monitor *recttomon(int x, int y, int w, int h);
192 static void resize(Client *c, int x, int y, int w, int h, int interact);
193 static void resizeclient(Client *c, int x, int y, int w, int h);
194 static void resizemouse(const Arg *arg);
195 static void restack(Monitor *m);
196 static void run(void);
197 static void scan(void);
198 static int sendevent(Client *c, Atom proto);
199 static void sendmon(Client *c, Monitor *m);
200 static void setclientstate(Client *c, long state);
201 static void setfocus(Client *c);
202 static void setfullscreen(Client *c, int fullscreen);
203 static void setlayout(const Arg *arg);
204 static void setmfact(const Arg *arg);
205 static void setup(void);
206 static void showhide(Client *c);
207 static void sigchld(int unused);
208 static void spawn(const Arg *arg);
209 static void tag(const Arg *arg);
210 static void tagmon(const Arg *arg);
211 static void tile(Monitor *);
212 static void togglebar(const Arg *arg);
213 static void togglefloating(const Arg *arg);
214 static void toggletag(const Arg *arg);
215 static void toggleview(const Arg *arg);
216 static void unfocus(Client *c, int setfocus);
217 static void unmanage(Client *c, int destroyed);
218 static void unmapnotify(XEvent *e);
219 static int updategeom(void);
220 static void updatebarpos(Monitor *m);
221 static void updatebars(void);
222 static void updateclientlist(void);
223 static void updatenumlockmask(void);
224 static void updatesizehints(Client *c);
225 static void updatestatus(void);
226 static void updatewindowtype(Client *c);
227 static void updatetitle(Client *c);
228 static void updatewmhints(Client *c);
229 static void view(const Arg *arg);
230 static Client *wintoclient(Window w);
231 static Monitor *wintomon(Window w);
232 static int xerror(Display *dpy, XErrorEvent *ee);
233 static int xerrordummy(Display *dpy, XErrorEvent *ee);
234 static int xerrorstart(Display *dpy, XErrorEvent *ee);
235 static void zoom(const Arg *arg);
238 static const char broken[] = "broken";
239 static char stext[256];
241 static int sw, sh; /* X display screen geometry width, height */
242 static int bh, blw = 0; /* bar geometry */
243 static int (*xerrorxlib)(Display *, XErrorEvent *);
244 static unsigned int numlockmask = 0;
245 static void (*handler[LASTEvent]) (XEvent *) = {
246 [ButtonPress] = buttonpress,
247 [ClientMessage] = clientmessage,
248 [ConfigureRequest] = configurerequest,
249 [ConfigureNotify] = configurenotify,
250 [DestroyNotify] = destroynotify,
251 [EnterNotify] = enternotify,
254 [KeyPress] = keypress,
255 [MappingNotify] = mappingnotify,
256 [MapRequest] = maprequest,
257 [MotionNotify] = motionnotify,
258 [PropertyNotify] = propertynotify,
259 [UnmapNotify] = unmapnotify
261 static Atom wmatom[WMLast], netatom[NetLast];
262 static int running = 1;
263 static Cur *cursor[CurLast];
264 static ClrScheme scheme[SchemeLast];
267 static Monitor *mons, *selmon;
270 /* configuration, allows nested code to access above variables */
273 /* compile-time check if all tags fit into an unsigned int bit array. */
274 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
276 /* function implementations */
278 applyrules(Client *c)
280 const char *class, *instance;
284 XClassHint ch = { NULL, NULL };
289 XGetClassHint(dpy, c->win, &ch);
290 class = ch.res_class ? ch.res_class : broken;
291 instance = ch.res_name ? ch.res_name : broken;
293 for (i = 0; i < LENGTH(rules); i++) {
295 if ((!r->title || strstr(c->name, r->title))
296 && (!r->class || strstr(class, r->class))
297 && (!r->instance || strstr(instance, r->instance)))
299 c->isfloating = r->isfloating;
301 for (m = mons; m && m->num != r->monitor; m = m->next);
310 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
314 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
319 /* set minimum possible */
327 if (*x + *w + 2 * c->bw < 0)
329 if (*y + *h + 2 * c->bw < 0)
332 if (*x >= m->wx + m->ww)
333 *x = m->wx + m->ww - WIDTH(c);
334 if (*y >= m->wy + m->wh)
335 *y = m->wy + m->wh - HEIGHT(c);
336 if (*x + *w + 2 * c->bw <= m->wx)
338 if (*y + *h + 2 * c->bw <= m->wy)
345 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
346 /* see last two sentences in ICCCM 4.1.2.3 */
347 baseismin = c->basew == c->minw && c->baseh == c->minh;
348 if (!baseismin) { /* temporarily remove base dimensions */
352 /* adjust for aspect limits */
353 if (c->mina > 0 && c->maxa > 0) {
354 if (c->maxa < (float)*w / *h)
355 *w = *h * c->maxa + 0.5;
356 else if (c->mina < (float)*h / *w)
357 *h = *w * c->mina + 0.5;
359 if (baseismin) { /* increment calculation requires this */
363 /* adjust for increment value */
368 /* restore base dimensions */
369 *w = MAX(*w + c->basew, c->minw);
370 *h = MAX(*h + c->baseh, c->minh);
372 *w = MIN(*w, c->maxw);
374 *h = MIN(*h, c->maxh);
376 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
384 else for (m = mons; m; m = m->next)
389 } else for (m = mons; m; m = m->next)
394 arrangemon(Monitor *m)
396 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
397 if (m->lt[m->sellt]->arrange)
398 m->lt[m->sellt]->arrange(m);
404 c->next = c->mon->clients;
409 attachstack(Client *c)
411 c->snext = c->mon->stack;
416 buttonpress(XEvent *e)
418 unsigned int i, x, click;
422 XButtonPressedEvent *ev = &e->xbutton;
425 /* focus monitor if necessary */
426 if ((m = wintomon(ev->window)) && m != selmon) {
427 unfocus(selmon->sel, 1);
431 if (ev->window == selmon->barwin) {
435 while (ev->x >= x && ++i < LENGTH(tags));
436 if (i < LENGTH(tags)) {
439 } else if (ev->x < x + blw)
441 else if (ev->x > selmon->ww - TEXTW(stext))
442 click = ClkStatusText;
445 } else if ((c = wintoclient(ev->window))) {
447 click = ClkClientWin;
449 for (i = 0; i < LENGTH(buttons); i++)
450 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
451 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
452 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
458 xerrorxlib = XSetErrorHandler(xerrorstart);
459 /* this causes an error if some other window manager is running */
460 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
462 XSetErrorHandler(xerror);
470 Layout foo = { "", NULL };
475 selmon->lt[selmon->sellt] = &foo;
476 for (m = mons; m; m = m->next)
478 unmanage(m->stack, 0);
479 XUngrabKey(dpy, AnyKey, AnyModifier, root);
482 for (i = 0; i < CurLast; i++)
483 drw_cur_free(drw, cursor[i]);
484 for (i = 0; i < SchemeLast; i++) {
485 drw_clr_free(scheme[i].border);
486 drw_clr_free(scheme[i].bg);
487 drw_clr_free(scheme[i].fg);
491 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
492 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
496 cleanupmon(Monitor *mon)
503 for (m = mons; m && m->next != mon; m = m->next);
506 XUnmapWindow(dpy, mon->barwin);
507 XDestroyWindow(dpy, mon->barwin);
512 clearurgent(Client *c)
517 if (!(wmh = XGetWMHints(dpy, c->win)))
519 wmh->flags &= ~XUrgencyHint;
520 XSetWMHints(dpy, c->win, wmh);
525 clientmessage(XEvent *e)
527 XClientMessageEvent *cme = &e->xclient;
528 Client *c = wintoclient(cme->window);
532 if (cme->message_type == netatom[NetWMState]) {
533 if (cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
534 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
535 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
536 } else if (cme->message_type == netatom[NetActiveWindow]) {
538 c->mon->seltags ^= 1;
539 c->mon->tagset[c->mon->seltags] = c->tags;
550 ce.type = ConfigureNotify;
558 ce.border_width = c->bw;
560 ce.override_redirect = False;
561 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
565 configurenotify(XEvent *e)
569 XConfigureEvent *ev = &e->xconfigure;
572 /* TODO: updategeom handling sucks, needs to be simplified */
573 if (ev->window == root) {
574 dirty = (sw != ev->width || sh != ev->height);
577 if (updategeom() || dirty) {
578 drw_resize(drw, sw, bh);
580 for (m = mons; m; m = m->next) {
581 for (c = m->clients; c; c = c->next)
583 resizeclient(c, m->mx, m->my, m->mw, m->mh);
584 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
593 configurerequest(XEvent *e)
597 XConfigureRequestEvent *ev = &e->xconfigurerequest;
600 if ((c = wintoclient(ev->window))) {
601 if (ev->value_mask & CWBorderWidth)
602 c->bw = ev->border_width;
603 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
605 if (ev->value_mask & CWX) {
607 c->x = m->mx + ev->x;
609 if (ev->value_mask & CWY) {
611 c->y = m->my + ev->y;
613 if (ev->value_mask & CWWidth) {
617 if (ev->value_mask & CWHeight) {
621 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
622 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
623 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
624 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
625 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
628 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
634 wc.width = ev->width;
635 wc.height = ev->height;
636 wc.border_width = ev->border_width;
637 wc.sibling = ev->above;
638 wc.stack_mode = ev->detail;
639 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
649 m = ecalloc(1, sizeof(Monitor));
650 m->tagset[0] = m->tagset[1] = 1;
652 m->nmaster = nmaster;
653 m->showbar = showbar;
655 m->lt[0] = &layouts[0];
656 m->lt[1] = &layouts[1 % LENGTH(layouts)];
657 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
662 destroynotify(XEvent *e)
665 XDestroyWindowEvent *ev = &e->xdestroywindow;
667 if ((c = wintoclient(ev->window)))
676 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
681 detachstack(Client *c)
685 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
688 if (c == c->mon->sel) {
689 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
700 if (!(m = selmon->next))
702 } else if (selmon == mons)
703 for (m = mons; m->next; m = m->next);
705 for (m = mons; m->next != selmon; m = m->next);
713 unsigned int i, occ = 0, urg = 0;
716 dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4;
718 for (c = m->clients; c; c = c->next) {
724 for (i = 0; i < LENGTH(tags); i++) {
726 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
727 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
728 drw_rect(drw, x + 1, 1, dx, dx, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
729 occ & 1 << i, urg & 1 << i);
732 w = blw = TEXTW(m->ltsymbol);
733 drw_setscheme(drw, &scheme[SchemeNorm]);
734 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
737 if (m == selmon) { /* status is only drawn on selected monitor */
744 drw_text(drw, x, 0, w, bh, stext, 0);
747 if ((w = x - xx) > bh) {
750 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
751 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
752 drw_rect(drw, x + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0);
754 drw_setscheme(drw, &scheme[SchemeNorm]);
755 drw_rect(drw, x, 0, w, bh, 1, 0, 1);
758 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
766 for (m = mons; m; m = m->next)
771 enternotify(XEvent *e)
775 XCrossingEvent *ev = &e->xcrossing;
777 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
779 c = wintoclient(ev->window);
780 m = c ? c->mon : wintomon(ev->window);
782 unfocus(selmon->sel, 1);
784 } else if (!c || c == selmon->sel)
793 XExposeEvent *ev = &e->xexpose;
795 if (ev->count == 0 && (m = wintomon(ev->window)))
802 if (!c || !ISVISIBLE(c))
803 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
804 /* was if (selmon->sel) */
805 if (selmon->sel && selmon->sel != c)
806 unfocus(selmon->sel, 0);
808 if (c->mon != selmon)
815 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix);
818 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
819 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
825 /* there are some broken focus acquiring clients */
829 XFocusChangeEvent *ev = &e->xfocus;
831 if (selmon->sel && ev->window != selmon->sel->win)
832 setfocus(selmon->sel);
836 focusmon(const Arg *arg)
842 if ((m = dirtomon(arg->i)) == selmon)
844 unfocus(selmon->sel, 0); /* s/1/0/ fixes input focus issues
845 in gedit and anjuta */
851 focusstack(const Arg *arg)
853 Client *c = NULL, *i;
858 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
860 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
862 for (i = selmon->clients; i != selmon->sel; i = i->next)
866 for (; i; i = i->next)
877 getatomprop(Client *c, Atom prop)
881 unsigned char *p = NULL;
882 Atom da, atom = None;
884 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
885 &da, &di, &dl, &dl, &p) == Success && p) {
893 getrootptr(int *x, int *y)
899 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
907 unsigned char *p = NULL;
908 unsigned long n, extra;
911 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
912 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
921 gettextprop(Window w, Atom atom, char *text, unsigned int size)
927 if (!text || size == 0)
930 XGetTextProperty(dpy, w, &name, atom);
933 if (name.encoding == XA_STRING)
934 strncpy(text, (char *)name.value, size - 1);
936 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
937 strncpy(text, *list, size - 1);
938 XFreeStringList(list);
941 text[size - 1] = '\0';
947 grabbuttons(Client *c, int focused)
952 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
953 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
955 for (i = 0; i < LENGTH(buttons); i++)
956 if (buttons[i].click == ClkClientWin)
957 for (j = 0; j < LENGTH(modifiers); j++)
958 XGrabButton(dpy, buttons[i].button,
959 buttons[i].mask | modifiers[j],
960 c->win, False, BUTTONMASK,
961 GrabModeAsync, GrabModeSync, None, None);
963 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
964 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
974 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
977 XUngrabKey(dpy, AnyKey, AnyModifier, root);
978 for (i = 0; i < LENGTH(keys); i++)
979 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
980 for (j = 0; j < LENGTH(modifiers); j++)
981 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
982 True, GrabModeAsync, GrabModeAsync);
987 incnmaster(const Arg *arg)
989 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
995 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
998 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
999 && unique[n].width == info->width && unique[n].height == info->height)
1003 #endif /* XINERAMA */
1013 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1014 for (i = 0; i < LENGTH(keys); i++)
1015 if (keysym == keys[i].keysym
1016 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1018 keys[i].func(&(keys[i].arg));
1022 killclient(const Arg *arg)
1026 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1028 XSetErrorHandler(xerrordummy);
1029 XSetCloseDownMode(dpy, DestroyAll);
1030 XKillClient(dpy, selmon->sel->win);
1032 XSetErrorHandler(xerror);
1038 manage(Window w, XWindowAttributes *wa)
1040 Client *c, *t = NULL;
1041 Window trans = None;
1044 c = ecalloc(1, sizeof(Client));
1047 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1055 c->x = c->oldx = wa->x;
1056 c->y = c->oldy = wa->y;
1057 c->w = c->oldw = wa->width;
1058 c->h = c->oldh = wa->height;
1059 c->oldbw = wa->border_width;
1061 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1062 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1063 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1064 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1065 c->x = MAX(c->x, c->mon->mx);
1066 /* only fix client y-offset, if the client center might cover the bar */
1067 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1068 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1071 wc.border_width = c->bw;
1072 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1073 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
1074 configure(c); /* propagates border_width, if size doesn't change */
1075 updatewindowtype(c);
1078 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1081 c->isfloating = c->oldstate = trans != None || c->isfixed;
1083 XRaiseWindow(dpy, c->win);
1086 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1087 (unsigned char *) &(c->win), 1);
1088 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1089 setclientstate(c, NormalState);
1090 if (c->mon == selmon)
1091 unfocus(selmon->sel, 0);
1094 XMapWindow(dpy, c->win);
1099 mappingnotify(XEvent *e)
1101 XMappingEvent *ev = &e->xmapping;
1103 XRefreshKeyboardMapping(ev);
1104 if (ev->request == MappingKeyboard)
1109 maprequest(XEvent *e)
1111 static XWindowAttributes wa;
1112 XMapRequestEvent *ev = &e->xmaprequest;
1114 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1116 if (wa.override_redirect)
1118 if (!wintoclient(ev->window))
1119 manage(ev->window, &wa);
1128 for (c = m->clients; c; c = c->next)
1131 if (n > 0) /* override layout symbol */
1132 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1133 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1134 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1138 motionnotify(XEvent *e)
1140 static Monitor *mon = NULL;
1142 XMotionEvent *ev = &e->xmotion;
1144 if (ev->window != root)
1146 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1147 unfocus(selmon->sel, 1);
1155 movemouse(const Arg *arg)
1157 int x, y, ocx, ocy, nx, ny;
1163 if (!(c = selmon->sel))
1165 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1170 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1171 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1173 if (!getrootptr(&x, &y))
1176 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1178 case ConfigureRequest:
1181 handler[ev.type](&ev);
1184 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1186 lasttime = ev.xmotion.time;
1188 nx = ocx + (ev.xmotion.x - x);
1189 ny = ocy + (ev.xmotion.y - y);
1190 if (nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1191 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1192 if (abs(selmon->wx - nx) < snap)
1194 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1195 nx = selmon->wx + selmon->ww - WIDTH(c);
1196 if (abs(selmon->wy - ny) < snap)
1198 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1199 ny = selmon->wy + selmon->wh - HEIGHT(c);
1200 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1201 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1202 togglefloating(NULL);
1204 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1205 resize(c, nx, ny, c->w, c->h, 1);
1208 } while (ev.type != ButtonRelease);
1209 XUngrabPointer(dpy, CurrentTime);
1210 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1218 nexttiled(Client *c)
1220 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1234 propertynotify(XEvent *e)
1238 XPropertyEvent *ev = &e->xproperty;
1240 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1242 else if (ev->state == PropertyDelete)
1243 return; /* ignore */
1244 else if ((c = wintoclient(ev->window))) {
1247 case XA_WM_TRANSIENT_FOR:
1248 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1249 (c->isfloating = (wintoclient(trans)) != NULL))
1252 case XA_WM_NORMAL_HINTS:
1260 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1262 if (c == c->mon->sel)
1265 if (ev->atom == netatom[NetWMWindowType])
1266 updatewindowtype(c);
1271 quit(const Arg *arg)
1277 recttomon(int x, int y, int w, int h)
1279 Monitor *m, *r = selmon;
1282 for (m = mons; m; m = m->next)
1283 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1291 resize(Client *c, int x, int y, int w, int h, int interact)
1293 if (applysizehints(c, &x, &y, &w, &h, interact))
1294 resizeclient(c, x, y, w, h);
1298 resizeclient(Client *c, int x, int y, int w, int h)
1302 c->oldx = c->x; c->x = wc.x = x;
1303 c->oldy = c->y; c->y = wc.y = y;
1304 c->oldw = c->w; c->w = wc.width = w;
1305 c->oldh = c->h; c->h = wc.height = h;
1306 wc.border_width = c->bw;
1307 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1313 resizemouse(const Arg *arg)
1315 int ocx, ocy, nw, nh;
1321 if (!(c = selmon->sel))
1323 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1328 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1329 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1331 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1333 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1335 case ConfigureRequest:
1338 handler[ev.type](&ev);
1341 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1343 lasttime = ev.xmotion.time;
1345 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1346 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1347 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1348 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1350 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1351 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1352 togglefloating(NULL);
1354 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1355 resize(c, c->x, c->y, nw, nh, 1);
1358 } while (ev.type != ButtonRelease);
1359 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1360 XUngrabPointer(dpy, CurrentTime);
1361 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1362 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1379 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1380 XRaiseWindow(dpy, m->sel->win);
1381 if (m->lt[m->sellt]->arrange) {
1382 wc.stack_mode = Below;
1383 wc.sibling = m->barwin;
1384 for (c = m->stack; c; c = c->snext)
1385 if (!c->isfloating && ISVISIBLE(c)) {
1386 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1387 wc.sibling = c->win;
1391 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1398 /* main event loop */
1400 while (running && !XNextEvent(dpy, &ev))
1401 if (handler[ev.type])
1402 handler[ev.type](&ev); /* call handler */
1408 unsigned int i, num;
1409 Window d1, d2, *wins = NULL;
1410 XWindowAttributes wa;
1412 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1413 for (i = 0; i < num; i++) {
1414 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1415 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1417 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1418 manage(wins[i], &wa);
1420 for (i = 0; i < num; i++) { /* now the transients */
1421 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1423 if (XGetTransientForHint(dpy, wins[i], &d1)
1424 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1425 manage(wins[i], &wa);
1433 sendmon(Client *c, Monitor *m)
1441 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1449 setclientstate(Client *c, long state)
1451 long data[] = { state, None };
1453 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1454 PropModeReplace, (unsigned char *)data, 2);
1458 sendevent(Client *c, Atom proto)
1465 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1466 while (!exists && n--)
1467 exists = protocols[n] == proto;
1471 ev.type = ClientMessage;
1472 ev.xclient.window = c->win;
1473 ev.xclient.message_type = wmatom[WMProtocols];
1474 ev.xclient.format = 32;
1475 ev.xclient.data.l[0] = proto;
1476 ev.xclient.data.l[1] = CurrentTime;
1477 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1485 if (!c->neverfocus) {
1486 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1487 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1488 XA_WINDOW, 32, PropModeReplace,
1489 (unsigned char *) &(c->win), 1);
1491 sendevent(c, wmatom[WMTakeFocus]);
1495 setfullscreen(Client *c, int fullscreen)
1497 if (fullscreen && !c->isfullscreen) {
1498 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1499 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1500 c->isfullscreen = 1;
1501 c->oldstate = c->isfloating;
1505 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1506 XRaiseWindow(dpy, c->win);
1507 } else if (!fullscreen && c->isfullscreen){
1508 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1509 PropModeReplace, (unsigned char*)0, 0);
1510 c->isfullscreen = 0;
1511 c->isfloating = c->oldstate;
1517 resizeclient(c, c->x, c->y, c->w, c->h);
1523 setlayout(const Arg *arg)
1525 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1528 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1529 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1536 /* arg > 1.0 will set mfact absolutly */
1538 setmfact(const Arg *arg)
1542 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1544 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1545 if (f < 0.1 || f > 0.9)
1554 XSetWindowAttributes wa;
1556 /* clean up any zombies immediately */
1560 screen = DefaultScreen(dpy);
1561 sw = DisplayWidth(dpy, screen);
1562 sh = DisplayHeight(dpy, screen);
1563 root = RootWindow(dpy, screen);
1564 drw = drw_create(dpy, screen, root, sw, sh);
1565 drw_load_fonts(drw, fonts, LENGTH(fonts));
1566 if (!drw->fontcount)
1567 die("no fonts could be loaded.\n");
1568 bh = drw->fonts[0]->h + 2;
1571 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1572 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1573 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1574 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1575 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1576 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1577 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1578 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1579 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1580 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1581 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1582 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1584 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1585 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1586 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1587 /* init appearance */
1588 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1589 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1590 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1591 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1592 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1593 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1597 /* EWMH support per view */
1598 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1599 PropModeReplace, (unsigned char *) netatom, NetLast);
1600 XDeleteProperty(dpy, root, netatom[NetClientList]);
1601 /* select for events */
1602 wa.cursor = cursor[CurNormal]->cursor;
1603 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1604 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1605 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1606 XSelectInput(dpy, root, wa.event_mask);
1617 /* show clients top down */
1618 XMoveWindow(dpy, c->win, c->x, c->y);
1619 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1620 resize(c, c->x, c->y, c->w, c->h, 0);
1623 /* hide clients bottom up */
1625 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1632 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1633 die("can't install SIGCHLD handler:");
1634 while (0 < waitpid(-1, NULL, WNOHANG));
1638 spawn(const Arg *arg)
1640 if (arg->v == dmenucmd)
1641 dmenumon[0] = '0' + selmon->num;
1644 close(ConnectionNumber(dpy));
1646 execvp(((char **)arg->v)[0], (char **)arg->v);
1647 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1656 if (selmon->sel && arg->ui & TAGMASK) {
1657 selmon->sel->tags = arg->ui & TAGMASK;
1664 tagmon(const Arg *arg)
1666 if (!selmon->sel || !mons->next)
1668 sendmon(selmon->sel, dirtomon(arg->i));
1674 unsigned int i, n, h, mw, my, ty;
1677 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1682 mw = m->nmaster ? m->ww * m->mfact : 0;
1685 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1686 if (i < m->nmaster) {
1687 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1688 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1691 h = (m->wh - ty) / (n - i);
1692 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1698 togglebar(const Arg *arg)
1700 selmon->showbar = !selmon->showbar;
1701 updatebarpos(selmon);
1702 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1707 togglefloating(const Arg *arg)
1711 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1713 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1714 if (selmon->sel->isfloating)
1715 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1716 selmon->sel->w, selmon->sel->h, 0);
1721 toggletag(const Arg *arg)
1723 unsigned int newtags;
1727 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1729 selmon->sel->tags = newtags;
1736 toggleview(const Arg *arg)
1738 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1741 selmon->tagset[selmon->seltags] = newtagset;
1748 unfocus(Client *c, int setfocus)
1753 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
1755 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1756 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1761 unmanage(Client *c, int destroyed)
1763 Monitor *m = c->mon;
1766 /* The server grab construct avoids race conditions. */
1770 wc.border_width = c->oldbw;
1772 XSetErrorHandler(xerrordummy);
1773 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1774 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1775 setclientstate(c, WithdrawnState);
1777 XSetErrorHandler(xerror);
1787 unmapnotify(XEvent *e)
1790 XUnmapEvent *ev = &e->xunmap;
1792 if ((c = wintoclient(ev->window))) {
1794 setclientstate(c, WithdrawnState);
1804 XSetWindowAttributes wa = {
1805 .override_redirect = True,
1806 .background_pixmap = ParentRelative,
1807 .event_mask = ButtonPressMask|ExposureMask
1809 for (m = mons; m; m = m->next) {
1812 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1813 CopyFromParent, DefaultVisual(dpy, screen),
1814 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1815 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1816 XMapRaised(dpy, m->barwin);
1821 updatebarpos(Monitor *m)
1827 m->by = m->topbar ? m->wy : m->wy + m->wh;
1828 m->wy = m->topbar ? m->wy + bh : m->wy;
1839 XDeleteProperty(dpy, root, netatom[NetClientList]);
1840 for (m = mons; m; m = m->next)
1841 for (c = m->clients; c; c = c->next)
1842 XChangeProperty(dpy, root, netatom[NetClientList],
1843 XA_WINDOW, 32, PropModeAppend,
1844 (unsigned char *) &(c->win), 1);
1853 if (XineramaIsActive(dpy)) {
1857 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1858 XineramaScreenInfo *unique = NULL;
1860 for (n = 0, m = mons; m; m = m->next, n++);
1861 /* only consider unique geometries as separate screens */
1862 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1863 for (i = 0, j = 0; i < nn; i++)
1864 if (isuniquegeom(unique, j, &info[i]))
1865 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1869 for (i = 0; i < (nn - n); i++) { /* new monitors available */
1870 for (m = mons; m && m->next; m = m->next);
1872 m->next = createmon();
1876 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1878 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1879 || unique[i].width != m->mw || unique[i].height != m->mh))
1883 m->mx = m->wx = unique[i].x_org;
1884 m->my = m->wy = unique[i].y_org;
1885 m->mw = m->ww = unique[i].width;
1886 m->mh = m->wh = unique[i].height;
1890 /* less monitors available nn < n */
1891 for (i = nn; i < n; i++) {
1892 for (m = mons; m && m->next; m = m->next);
1893 while (m->clients) {
1896 m->clients = c->next;
1909 #endif /* XINERAMA */
1910 /* default monitor setup */
1914 if (mons->mw != sw || mons->mh != sh) {
1916 mons->mw = mons->ww = sw;
1917 mons->mh = mons->wh = sh;
1923 selmon = wintomon(root);
1929 updatenumlockmask(void)
1932 XModifierKeymap *modmap;
1935 modmap = XGetModifierMapping(dpy);
1936 for (i = 0; i < 8; i++)
1937 for (j = 0; j < modmap->max_keypermod; j++)
1938 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1939 == XKeysymToKeycode(dpy, XK_Num_Lock))
1940 numlockmask = (1 << i);
1941 XFreeModifiermap(modmap);
1945 updatesizehints(Client *c)
1950 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
1951 /* size is uninitialized, ensure that size.flags aren't used */
1953 if (size.flags & PBaseSize) {
1954 c->basew = size.base_width;
1955 c->baseh = size.base_height;
1956 } else if (size.flags & PMinSize) {
1957 c->basew = size.min_width;
1958 c->baseh = size.min_height;
1960 c->basew = c->baseh = 0;
1961 if (size.flags & PResizeInc) {
1962 c->incw = size.width_inc;
1963 c->inch = size.height_inc;
1965 c->incw = c->inch = 0;
1966 if (size.flags & PMaxSize) {
1967 c->maxw = size.max_width;
1968 c->maxh = size.max_height;
1970 c->maxw = c->maxh = 0;
1971 if (size.flags & PMinSize) {
1972 c->minw = size.min_width;
1973 c->minh = size.min_height;
1974 } else if (size.flags & PBaseSize) {
1975 c->minw = size.base_width;
1976 c->minh = size.base_height;
1978 c->minw = c->minh = 0;
1979 if (size.flags & PAspect) {
1980 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1981 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1983 c->maxa = c->mina = 0.0;
1984 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1985 && c->maxw == c->minw && c->maxh == c->minh);
1989 updatetitle(Client *c)
1991 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1992 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1993 if (c->name[0] == '\0') /* hack to mark broken clients */
1994 strcpy(c->name, broken);
2000 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2001 strcpy(stext, "dwm-"VERSION);
2006 updatewindowtype(Client *c)
2008 Atom state = getatomprop(c, netatom[NetWMState]);
2009 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2011 if (state == netatom[NetWMFullscreen])
2012 setfullscreen(c, 1);
2013 if (wtype == netatom[NetWMWindowTypeDialog])
2018 updatewmhints(Client *c)
2022 if ((wmh = XGetWMHints(dpy, c->win))) {
2023 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2024 wmh->flags &= ~XUrgencyHint;
2025 XSetWMHints(dpy, c->win, wmh);
2027 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2028 if (wmh->flags & InputHint)
2029 c->neverfocus = !wmh->input;
2037 view(const Arg *arg)
2039 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2041 selmon->seltags ^= 1; /* toggle sel tagset */
2042 if (arg->ui & TAGMASK)
2043 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2049 wintoclient(Window w)
2054 for (m = mons; m; m = m->next)
2055 for (c = m->clients; c; c = c->next)
2068 if (w == root && getrootptr(&x, &y))
2069 return recttomon(x, y, 1, 1);
2070 for (m = mons; m; m = m->next)
2073 if ((c = wintoclient(w)))
2078 /* There's no way to check accesses to destroyed windows, thus those cases are
2079 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2080 * default error handler, which may call exit. */
2082 xerror(Display *dpy, XErrorEvent *ee)
2084 if (ee->error_code == BadWindow
2085 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2086 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2087 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2088 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2089 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2090 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2091 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2092 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2094 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2095 ee->request_code, ee->error_code);
2096 return xerrorxlib(dpy, ee); /* may call exit */
2100 xerrordummy(Display *dpy, XErrorEvent *ee)
2105 /* Startup Error handler to check if another window manager
2106 * is already running. */
2108 xerrorstart(Display *dpy, XErrorEvent *ee)
2110 die("dwm: another window manager is already running\n");
2115 zoom(const Arg *arg)
2117 Client *c = selmon->sel;
2119 if (!selmon->lt[selmon->sellt]->arrange
2120 || (selmon->sel && selmon->sel->isfloating))
2122 if (c == nexttiled(selmon->clients))
2123 if (!c || !(c = nexttiled(c->next)))
2129 main(int argc, char *argv[])
2131 if (argc == 2 && !strcmp("-v", argv[1]))
2132 die("dwm-"VERSION "\n");
2134 die("usage: dwm [-v]\n");
2135 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2136 fputs("warning: no locale support\n", stderr);
2137 if (!(dpy = XOpenDisplay(NULL)))
2138 die("dwm: cannot open display\n");
2145 return EXIT_SUCCESS;