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>
47 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
49 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
51 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
52 #define LENGTH(X) (sizeof X / sizeof X[0])
53 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
54 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
56 #define TAGMASK ((1 << LENGTH(tags)) - 1)
57 #define TEXTW(X) (drw_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
60 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
61 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
62 enum { NetSupported, NetWMName, NetWMState,
63 NetWMFullscreen, NetWMWindowOpacity, NetActiveWindow, NetWMWindowType,
64 NetWMWindowTypeDialog, NetClientList, NetSupportingWMCheck, NetLast }; /* EWMH atoms */
65 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
66 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
67 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
80 void (*func)(const Arg *arg);
84 typedef struct Monitor Monitor;
85 typedef struct Client Client;
90 int oldx, oldy, oldw, oldh;
91 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
95 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, screen_hog;
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;
145 /* function declarations */
146 static void applyrules(Client *c);
147 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
148 static void arrange(Monitor *m);
149 static void arrangemon(Monitor *m);
150 static void attach_as_master(Client *c);
151 static void attach(Client *c);
152 static void attachstack(Client *c);
153 static void buttonpress(XEvent *e);
154 static void checkotherwm(void);
155 static void cleanup(void);
156 static void cleanupmon(Monitor *mon);
157 static void clearurgent(Client *c);
158 static void clientmessage(XEvent *e);
159 static void configure(Client *c);
160 static void configurenotify(XEvent *e);
161 static void configurerequest(XEvent *e);
162 static Monitor *createmon(void);
163 static void destroynotify(XEvent *e);
164 static void detach(Client *c);
165 static void detachstack(Client *c);
166 static Monitor *dirtomon(int dir);
167 static void drawbar(Monitor *m);
168 static void drawbars(void);
169 static void enternotify(XEvent *e);
170 static void expose(XEvent *e);
171 static void focus(Client *c);
172 static void focusin(XEvent *e);
173 static void focusmon(const Arg *arg);
174 static void focusstack(const Arg *arg);
175 static Bool getrootptr(int *x, int *y);
176 static long getstate(Window w);
177 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
178 static void grabbuttons(Client *c, Bool focused);
179 static void grabkeys(void);
180 static void incnmaster(const Arg *arg);
181 static void keypress(XEvent *e);
182 static void killclient(const Arg *arg);
183 static void manage(Window w, XWindowAttributes *wa);
184 static void mappingnotify(XEvent *e);
185 static void maprequest(XEvent *e);
186 static void monocle(Monitor *m);
187 static void motionnotify(XEvent *e);
188 static void movemouse(const Arg *arg);
189 static Client *nexttiled(Client *c);
190 static Client *snexttiled(Client *c);
191 static Client *nextvisible(Client *c);
192 static void pop(Client *);
193 static void propertynotify(XEvent *e);
194 static void quit(const Arg *arg);
195 static Monitor *recttomon(int x, int y, int w, int h);
196 static void resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base);
197 static void resizeclient(Client *c, int x, int y, int w, int h, Client *base);
198 static void resizemouse(const Arg *arg);
199 static void restack(Monitor *m);
200 static void run(void);
201 static void scan(void);
202 static Bool sendevent(Client *c, Atom proto);
203 static void sendmon(Client *c, Monitor *m);
204 static void setclientstate(Client *c, long state);
205 static void setfocus(Client *c);
206 static void setfullscreen(Client *c, Bool fullscreen);
207 static void setlayout(const Arg *arg);
208 static void setmfact(const Arg *arg);
209 static void setup(void);
210 static void showhide(Client *c);
211 static void sigchld(int unused);
212 static void spawn(const Arg *arg);
213 static void tag(const Arg *arg);
214 static void tagmon(const Arg *arg);
215 static void jason_layout(Monitor *);
216 static void tile(Monitor *);
217 static void togglebar(const Arg *arg);
218 static void togglefloating(const Arg *arg);
219 static void toggletag(const Arg *arg);
220 static void toggleview(const Arg *arg);
221 static void unfocus(Client *c, Bool setfocus);
222 static void unmanage(Client *c, Bool destroyed);
223 static void unmapnotify(XEvent *e);
224 static Bool updategeom(void);
225 static void updatebarpos(Monitor *m);
226 static void updatebars(void);
227 static void updateclientlist(void);
228 static void updatenumlockmask(void);
229 static void updatesizehints(Client *c);
230 static void updatestatus(void);
231 static void updatewindowtype(Client *c);
232 static void updatetitle(Client *c);
233 static void updatewmhints(Client *c);
234 static void view(const Arg *arg);
235 static Client *wintoclient(Window w);
236 static Monitor *wintomon(Window w);
237 static int xerror(Display *dpy, XErrorEvent *ee);
238 static int xerrordummy(Display *dpy, XErrorEvent *ee);
239 static int xerrorstart(Display *dpy, XErrorEvent *ee);
240 static void zoom(const Arg *arg);
243 static const char broken[] = "broken";
244 static char stext[256];
246 static int sw, sh; /* X display screen geometry width, height */
247 static int bh, blw = 0; /* bar geometry */
248 static int (*xerrorxlib)(Display *, XErrorEvent *);
249 static unsigned int numlockmask = 0;
250 static void (*handler[LASTEvent]) (XEvent *) = {
251 [ButtonPress] = buttonpress,
252 [ClientMessage] = clientmessage,
253 [ConfigureRequest] = configurerequest,
254 [ConfigureNotify] = configurenotify,
255 [DestroyNotify] = destroynotify,
256 [EnterNotify] = enternotify,
259 [KeyPress] = keypress,
260 [MappingNotify] = mappingnotify,
261 [MapRequest] = maprequest,
262 [MotionNotify] = motionnotify,
263 [PropertyNotify] = propertynotify,
264 [UnmapNotify] = unmapnotify
266 static Atom wmatom[WMLast], netatom[NetLast];
267 static Bool running = True;
268 static Cur *cursor[CurLast];
269 static ClrScheme scheme[SchemeLast];
273 static Monitor *mons, *selmon;
276 // unfocused windows get transparent (feature)
277 static const unsigned long opacities[] = { 0, 0xbfffffff, 0x00000000 }; // first unused
278 static void window_set_opaque(Client *c);
279 static void window_set_translucent(Client *c);
280 static void window_set_invisible(Client *c);
281 static void window_set_opacity(Client *c, int opacity_index);
282 static void update_window_opacities(Monitor *m);
284 window_set_opacity(Client *c, int opacity_index) {
285 if (c->opacity == opacity_index) {
288 c->opacity = opacity_index;
289 if (opacity_index == 0) {
290 XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
292 XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)(&opacities[opacity_index]), 1);
296 window_set_opaque(Client *c) {
297 window_set_opacity(c, 0);
300 window_set_translucent(Client *c) {
301 window_set_opacity(c, 1);
304 window_set_invisible(Client *c) {
305 window_set_opacity(c, 2);
308 update_window_opacities(Monitor *m) {
309 Client *master, *slave, *c;
310 Bool selection_floating = False;
311 slave = master = nexttiled(m->clients);
312 if (master) slave = nexttiled(master->next);
313 if (m->sel && m->sel != master) {
314 if (nexttiled(m->sel) == m->sel) // if selection is tiled
317 selection_floating = True;
319 for (c = m->clients; c; c = c->next) {
321 if (c->isfloating || c == m->sel || (selection_floating && (c == master || c == slave))) {
322 window_set_opaque(c);
323 } else if (c == master || c == slave) {
324 window_set_translucent(c);
326 window_set_opaque(c);
333 /* configuration, allows nested code to access above variables */
336 /* compile-time check if all tags fit into an unsigned int bit array. */
337 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
339 /* function implementations */
341 applyrules(Client *c) {
342 const char *class, *instance;
346 XClassHint ch = { NULL, NULL };
349 c->isfloating = c->tags = c->screen_hog = 0;
350 XGetClassHint(dpy, c->win, &ch);
351 class = ch.res_class ? ch.res_class : broken;
352 instance = ch.res_name ? ch.res_name : broken;
354 for(i = 0; i < LENGTH(rules); i++) {
356 if((!r->title || c->name == strstr(c->name, r->title))
357 && (!r->class || strstr(class, r->class))
358 && (!r->instance || strstr(instance, r->instance)))
360 c->isfloating = r->isfloating;
362 c->screen_hog = r->screen_hog;
363 for(m = mons; m && m->num != r->monitor; m = m->next);
372 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
376 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
378 // Monitor *m = c->mon;
380 /* set minimum possible */
388 if(*x + *w + 2 * c->bw < 0)
390 if(*y + *h + 2 * c->bw < 0)
393 // jason: let windows be offscreen
395 // if(*x >= m->wx + m->ww)
396 // *x = m->wx + m->ww - WIDTH(c);
397 // if(*y >= m->wy + m->wh)
398 // *y = m->wy + m->wh - HEIGHT(c);
399 // if(*x + *w + 2 * c->bw <= m->wx)
401 // if(*y + *h + 2 * c->bw <= m->wy)
408 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
409 /* see last two sentences in ICCCM 4.1.2.3 */
410 baseismin = c->basew == c->minw && c->baseh == c->minh;
411 if(!baseismin) { /* temporarily remove base dimensions */
415 /* adjust for aspect limits */
416 if(c->mina > 0 && c->maxa > 0) {
417 if(c->maxa < (float)*w / *h)
418 *w = *h * c->maxa + 0.5;
419 else if(c->mina < (float)*h / *w)
420 *h = *w * c->mina + 0.5;
422 if(baseismin) { /* increment calculation requires this */
426 /* adjust for increment value */
431 /* restore base dimensions */
432 *w = MAX(*w + c->basew, c->minw);
433 *h = MAX(*h + c->baseh, c->minh);
435 *w = MIN(*w, c->maxw);
437 *h = MIN(*h, c->maxh);
439 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
443 arrange(Monitor *m) {
446 else for(m = mons; m; m = m->next)
451 } else for(m = mons; m; m = m->next)
456 arrangemon(Monitor *m) {
457 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
458 if(m->lt[m->sellt]->arrange)
459 m->lt[m->sellt]->arrange(m);
463 attach_as_master(Client *c) {
464 c->next = c->mon->clients;
470 c->next = c->mon->sel->next;
471 c->mon->sel->next = c;
478 attachstack(Client *c) {
479 c->snext = c->mon->stack;
484 buttonpress(XEvent *e) {
485 unsigned int i, x, click;
489 XButtonPressedEvent *ev = &e->xbutton;
492 /* focus monitor if necessary */
493 if((m = wintomon(ev->window)) && m != selmon) {
494 unfocus(selmon->sel, True);
498 if(ev->window == selmon->barwin) {
502 while(ev->x >= x && ++i < LENGTH(tags));
503 if(i < LENGTH(tags)) {
507 else if(ev->x < x + blw)
509 else if(ev->x > selmon->ww - TEXTW(stext))
510 click = ClkStatusText;
514 else if((c = wintoclient(ev->window))) {
516 click = ClkClientWin;
518 for(i = 0; i < LENGTH(buttons); i++)
519 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
520 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
521 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
526 xerrorxlib = XSetErrorHandler(xerrorstart);
527 /* this causes an error if some other window manager is running */
528 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
530 XSetErrorHandler(xerror);
537 Layout foo = { "", NULL };
541 selmon->lt[selmon->sellt] = &foo;
542 for(m = mons; m; m = m->next)
544 unmanage(m->stack, False);
545 XUngrabKey(dpy, AnyKey, AnyModifier, root);
548 drw_cur_free(drw, cursor[CurNormal]);
549 drw_cur_free(drw, cursor[CurResize]);
550 drw_cur_free(drw, cursor[CurMove]);
551 drw_font_free(dpy, fnt);
552 drw_clr_free(scheme[SchemeNorm].border);
553 drw_clr_free(scheme[SchemeNorm].bg);
554 drw_clr_free(scheme[SchemeNorm].fg);
555 drw_clr_free(scheme[SchemeSel].border);
556 drw_clr_free(scheme[SchemeSel].bg);
557 drw_clr_free(scheme[SchemeSel].fg);
560 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
561 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
565 cleanupmon(Monitor *mon) {
571 for(m = mons; m && m->next != mon; m = m->next);
574 XUnmapWindow(dpy, mon->barwin);
575 XDestroyWindow(dpy, mon->barwin);
580 clearurgent(Client *c) {
584 if(!(wmh = XGetWMHints(dpy, c->win)))
586 wmh->flags &= ~XUrgencyHint;
587 XSetWMHints(dpy, c->win, wmh);
592 clientmessage(XEvent *e) {
593 XClientMessageEvent *cme = &e->xclient;
594 Client *c = wintoclient(cme->window);
598 if(cme->message_type == netatom[NetWMState]) {
599 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
600 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
601 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
603 else if(cme->message_type == netatom[NetActiveWindow]) {
604 // Jason added this so apps can't steal focus:
607 c->mon->seltags ^= 1;
608 c->mon->tagset[c->mon->seltags] = c->tags;
615 configure(Client *c) {
618 ce.type = ConfigureNotify;
626 ce.border_width = c->bw;
628 ce.override_redirect = False;
629 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
633 configurenotify(XEvent *e) {
635 XConfigureEvent *ev = &e->xconfigure;
638 // TODO: updategeom handling sucks, needs to be simplified
639 if(ev->window == root) {
640 dirty = (sw != ev->width || sh != ev->height);
643 if(updategeom() || dirty) {
644 drw_resize(drw, sw, bh);
646 for(m = mons; m; m = m->next)
647 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
655 configurerequest(XEvent *e) {
658 XConfigureRequestEvent *ev = &e->xconfigurerequest;
661 if((c = wintoclient(ev->window))) {
662 if(ev->value_mask & CWBorderWidth)
663 c->bw = ev->border_width;
664 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
666 if(ev->value_mask & CWX) {
668 c->x = m->mx + ev->x;
670 if(ev->value_mask & CWY) {
672 c->y = m->my + ev->y;
674 if(ev->value_mask & CWWidth) {
678 if(ev->value_mask & CWHeight) {
682 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
683 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
684 if((c->y + c->h) > m->my + m->mh && c->isfloating)
685 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
686 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
689 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
697 wc.width = ev->width;
698 wc.height = ev->height;
699 wc.border_width = ev->border_width;
700 wc.sibling = ev->above;
701 wc.stack_mode = ev->detail;
702 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
711 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
712 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
713 m->tagset[0] = m->tagset[1] = 1;
715 m->nmaster = nmaster;
716 m->showbar = showbar;
718 m->lt[0] = &layouts[0];
719 m->lt[1] = &layouts[1 % LENGTH(layouts)];
720 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
725 destroynotify(XEvent *e) {
727 XDestroyWindowEvent *ev = &e->xdestroywindow;
729 if((c = wintoclient(ev->window)))
737 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
741 // NOTE: the stack is for z-order and most-recently-focused
742 // only mon->clients determines position in visible layout
744 detachstack(Client *c) {
745 Client *prev = NULL, *next_sel = NULL, *i;
746 for(i = c->mon->stack; i && i != c; i = i->snext) {
749 if(c == c->mon->sel) {
750 // find last visible window before c
751 // WARNING if you detach() before detachstack() this will select last visible window
752 for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
754 // failing that, find first visible window (besides c)
756 for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
759 c->mon->sel = next_sel;
762 prev->snext = c->snext;
764 c->mon->stack = c->snext;
773 if(!(m = selmon->next))
776 else if(selmon == mons)
777 for(m = mons; m->next; m = m->next);
779 for(m = mons; m->next != selmon; m = m->next);
784 drawbar(Monitor *m) {
786 unsigned int i, occ = 0, urg = 0;
789 for(c = m->clients; c; c = c->next) {
795 for(i = 0; i < LENGTH(tags); i++) {
797 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
798 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
799 drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
800 occ & 1 << i, urg & 1 << i);
803 w = blw = TEXTW(m->ltsymbol);
804 drw_setscheme(drw, &scheme[SchemeNorm]);
805 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
808 if(m == selmon) { /* status is only drawn on selected monitor */
815 drw_text(drw, x, 0, w, bh, stext, 0);
819 if((w = x - xx) > bh) {
822 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
823 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
824 drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
827 drw_setscheme(drw, &scheme[SchemeNorm]);
828 drw_text(drw, x, 0, w, bh, NULL, 0);
831 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
838 for(m = mons; m; m = m->next)
843 enternotify(XEvent *e) {
846 XCrossingEvent *ev = &e->xcrossing;
848 return; // jason: added to stop mouse focus
850 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
852 c = wintoclient(ev->window);
853 m = c ? c->mon : wintomon(ev->window);
855 unfocus(selmon->sel, True);
858 else if(!c || c == selmon->sel)
866 XExposeEvent *ev = &e->xexpose;
868 if(ev->count == 0 && (m = wintomon(ev->window)))
874 if(!c || !ISVISIBLE(c))
875 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
876 /* was if(selmon->sel) */
877 if(selmon->sel && selmon->sel != c)
878 unfocus(selmon->sel, False);
886 grabbuttons(c, True);
887 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
891 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
892 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
896 update_window_opacities(selmon);
898 if(c && (!root || (c->win!=root)) )
899 window_set_opaque(c);
903 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
904 XFocusChangeEvent *ev = &e->xfocus;
906 if(selmon->sel && ev->window != selmon->sel->win)
907 setfocus(selmon->sel);
911 focusmon(const Arg *arg) {
916 if((m = dirtomon(arg->i)) == selmon)
918 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
919 in gedit and anjuta */
925 focusstack(const Arg *arg) {
926 Client *c = NULL, *i;
931 for(i = selmon->clients; i != selmon->sel; i = i->next) {
937 } else if(arg->i > 0) {
938 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
940 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
943 for(i = selmon->clients; i != selmon->sel; i = i->next)
947 for(; i; i = i->next)
958 getatomprop(Client *c, Atom prop) {
961 unsigned char *p = NULL;
962 Atom da, atom = None;
964 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
965 &da, &di, &dl, &dl, &p) == Success && p) {
973 getrootptr(int *x, int *y) {
978 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
985 unsigned char *p = NULL;
986 unsigned long n, extra;
989 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
990 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
999 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
1004 if(!text || size == 0)
1007 XGetTextProperty(dpy, w, &name, atom);
1010 if(name.encoding == XA_STRING)
1011 strncpy(text, (char *)name.value, size - 1);
1013 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1014 strncpy(text, *list, size - 1);
1015 XFreeStringList(list);
1018 text[size - 1] = '\0';
1024 grabbuttons(Client *c, Bool focused) {
1025 updatenumlockmask();
1028 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1029 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1031 for(i = 0; i < LENGTH(buttons); i++)
1032 if(buttons[i].click == ClkClientWin)
1033 for(j = 0; j < LENGTH(modifiers); j++)
1034 XGrabButton(dpy, buttons[i].button,
1035 buttons[i].mask | modifiers[j],
1036 c->win, False, BUTTONMASK,
1037 GrabModeAsync, GrabModeSync, None, None);
1040 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1041 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1047 updatenumlockmask();
1050 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1053 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1054 for(i = 0; i < LENGTH(keys); i++)
1055 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1056 for(j = 0; j < LENGTH(modifiers); j++)
1057 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1058 True, GrabModeAsync, GrabModeAsync);
1063 incnmaster(const Arg *arg) {
1064 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1070 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1072 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1073 && unique[n].width == info->width && unique[n].height == info->height)
1077 #endif /* XINERAMA */
1080 keypress(XEvent *e) {
1086 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1087 for(i = 0; i < LENGTH(keys); i++)
1088 if(keysym == keys[i].keysym
1089 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1091 keys[i].func(&(keys[i].arg));
1095 killclient(const Arg *arg) {
1098 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1100 XSetErrorHandler(xerrordummy);
1101 XSetCloseDownMode(dpy, DestroyAll);
1102 XKillClient(dpy, selmon->sel->win);
1104 XSetErrorHandler(xerror);
1110 manage(Window w, XWindowAttributes *wa) {
1111 Client *c, *t = NULL;
1112 Window trans = None;
1115 if(!(c = calloc(1, sizeof(Client))))
1116 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1117 c->opacity = -1; // who knows
1120 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1129 c->x = c->oldx = wa->x;
1130 c->y = c->oldy = wa->y;
1131 c->w = c->oldw = wa->width;
1132 c->h = c->oldh = wa->height;
1133 c->oldbw = wa->border_width;
1135 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1136 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1137 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1138 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1139 c->x = MAX(c->x, c->mon->mx);
1140 /* only fix client y-offset, if the client center might cover the bar */
1141 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1142 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1145 wc.border_width = c->bw;
1146 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1147 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
1148 configure(c); /* propagates border_width, if size doesn't change */
1149 updatewindowtype(c);
1152 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1153 grabbuttons(c, False);
1155 c->isfloating = c->oldstate = trans != None || c->isfixed;
1157 XRaiseWindow(dpy, c->win);
1160 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1161 (unsigned char *) &(c->win), 1);
1162 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1163 setclientstate(c, NormalState);
1164 if (c->mon == selmon)
1165 unfocus(selmon->sel, False);
1168 XMapWindow(dpy, c->win);
1173 mappingnotify(XEvent *e) {
1174 XMappingEvent *ev = &e->xmapping;
1176 XRefreshKeyboardMapping(ev);
1177 if(ev->request == MappingKeyboard)
1182 maprequest(XEvent *e) {
1183 static XWindowAttributes wa;
1184 XMapRequestEvent *ev = &e->xmaprequest;
1186 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1188 if(wa.override_redirect)
1190 if(!wintoclient(ev->window))
1191 manage(ev->window, &wa);
1195 monocle(Monitor *m) {
1199 for(c = m->clients; c; c = c->next)
1202 if(n > 0) /* override layout symbol */
1203 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1204 for(c = snexttiled(m->stack); c; c = snexttiled(c->snext)) {
1206 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False, 0);
1208 // this window is should not be visible. move off top, but don't change h/w
1209 resize(c, m->wx, m->wy - 4000, c->w, c->h, False, 0);
1215 motionnotify(XEvent *e) {
1216 static Monitor *mon = NULL;
1218 XMotionEvent *ev = &e->xmotion;
1220 if(ev->window != root)
1222 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1223 unfocus(selmon->sel, True);
1231 movemouse(const Arg *arg) {
1232 int x, y, ocx, ocy, nx, ny;
1238 if(!(c = selmon->sel))
1240 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1245 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1246 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1248 if(!getrootptr(&x, &y))
1251 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1253 case ConfigureRequest:
1256 handler[ev.type](&ev);
1259 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1261 lasttime = ev.xmotion.time;
1263 nx = ocx + (ev.xmotion.x - x);
1264 ny = ocy + (ev.xmotion.y - y);
1265 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1266 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1267 if(abs(selmon->wx - nx) < snap)
1269 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1270 nx = selmon->wx + selmon->ww - WIDTH(c);
1271 if(abs(selmon->wy - ny) < snap)
1273 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1274 ny = selmon->wy + selmon->wh - HEIGHT(c);
1275 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1276 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1277 togglefloating(NULL);
1279 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1280 resize(c, nx, ny, c->w, c->h, True, 0);
1283 } while(ev.type != ButtonRelease);
1284 XUngrabPointer(dpy, CurrentTime);
1285 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1293 nexttiled(Client *c) {
1294 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1299 snexttiled(Client *c) {
1300 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->snext);
1305 nextvisible(Client *c) {
1306 for(; c && !ISVISIBLE(c); c = c->next);
1313 attach_as_master(c);
1319 propertynotify(XEvent *e) {
1322 XPropertyEvent *ev = &e->xproperty;
1324 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1326 else if(ev->state == PropertyDelete)
1327 return; /* ignore */
1328 else if((c = wintoclient(ev->window))) {
1331 case XA_WM_TRANSIENT_FOR:
1332 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1333 (c->isfloating = (wintoclient(trans)) != NULL))
1336 case XA_WM_NORMAL_HINTS:
1344 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1346 if(c == c->mon->sel)
1349 if(ev->atom == netatom[NetWMWindowType])
1350 updatewindowtype(c);
1355 quit(const Arg *arg) {
1360 recttomon(int x, int y, int w, int h) {
1361 Monitor *m, *r = selmon;
1364 for(m = mons; m; m = m->next)
1365 if((a = INTERSECT(x, y, w, h, m)) > area) {
1373 resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base) {
1374 if(applysizehints(c, &x, &y, &w, &h, interact))
1375 resizeclient(c, x, y, w, h, base);
1379 resizeclient(Client *c, int x, int y, int w, int h, Client *base) {
1381 unsigned long mask = CWX|CWY|CWWidth|CWHeight|CWBorderWidth;
1383 c->oldx = c->x; c->x = wc.x = x;
1384 c->oldy = c->y; c->y = wc.y = y;
1385 c->oldw = c->w; c->w = wc.width = w;
1386 c->oldh = c->h; c->h = wc.height = h;
1389 wc.stack_mode = Above;
1390 wc.sibling = base->win;
1391 mask |= CWStackMode|CWSibling;
1393 wc.border_width = c->bw;
1394 XConfigureWindow(dpy, c->win, mask, &wc);
1400 resizemouse(const Arg *arg) {
1401 int ocx, ocy, nw, nh;
1407 if(!(c = selmon->sel))
1409 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1414 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1415 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1417 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1419 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1421 case ConfigureRequest:
1424 handler[ev.type](&ev);
1427 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1429 lasttime = ev.xmotion.time;
1431 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1432 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1433 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1434 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1436 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1437 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1438 togglefloating(NULL);
1440 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1441 resize(c, c->x, c->y, nw, nh, True, 0);
1444 } while(ev.type != ButtonRelease);
1445 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1446 XUngrabPointer(dpy, CurrentTime);
1447 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1448 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1456 restack(Monitor *m) {
1464 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1465 XRaiseWindow(dpy, m->sel->win);
1466 if(m->lt[m->sellt]->arrange) {
1467 wc.stack_mode = Below;
1468 wc.sibling = m->barwin;
1469 for(c = m->clients; c; c = c->next)
1470 if(!c->isfloating && ISVISIBLE(c)) {
1471 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1472 wc.sibling = c->win;
1473 wc.stack_mode = Above;
1477 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1483 /* main event loop */
1485 while(running && !XNextEvent(dpy, &ev))
1486 if(handler[ev.type])
1487 handler[ev.type](&ev); /* call handler */
1492 unsigned int i, num;
1493 Window d1, d2, *wins = NULL;
1494 XWindowAttributes wa;
1496 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1497 for(i = 0; i < num; i++) {
1498 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1499 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1501 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1502 manage(wins[i], &wa);
1504 for(i = 0; i < num; i++) { /* now the transients */
1505 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1507 if(XGetTransientForHint(dpy, wins[i], &d1)
1508 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1509 manage(wins[i], &wa);
1517 sendmon(Client *c, Monitor *m) {
1524 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1532 setclientstate(Client *c, long state) {
1533 long data[] = { state, None };
1535 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1536 PropModeReplace, (unsigned char *)data, 2);
1540 sendevent(Client *c, Atom proto) {
1543 Bool exists = False;
1546 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1547 while(!exists && n--)
1548 exists = protocols[n] == proto;
1552 ev.type = ClientMessage;
1553 ev.xclient.window = c->win;
1554 ev.xclient.message_type = wmatom[WMProtocols];
1555 ev.xclient.format = 32;
1556 ev.xclient.data.l[0] = proto;
1557 ev.xclient.data.l[1] = CurrentTime;
1558 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1564 setfocus(Client *c) {
1565 if(!c->neverfocus) {
1566 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1567 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1568 XA_WINDOW, 32, PropModeReplace,
1569 (unsigned char *) &(c->win), 1);
1571 sendevent(c, wmatom[WMTakeFocus]);
1575 setfullscreen(Client *c, Bool fullscreen) {
1577 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1578 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1579 c->isfullscreen = True;
1580 c->oldstate = c->isfloating;
1583 c->isfloating = True;
1584 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh, 0);
1585 XRaiseWindow(dpy, c->win);
1588 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1589 PropModeReplace, (unsigned char*)0, 0);
1590 c->isfullscreen = False;
1591 c->isfloating = c->oldstate;
1597 resizeclient(c, c->x, c->y, c->w, c->h, 0);
1603 setlayout(const Arg *arg) {
1604 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1607 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1608 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1615 /* arg > 1.0 will set mfact absolutly */
1617 setmfact(const Arg *arg) {
1620 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1622 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1623 if(f < 0.1 || f > 0.9)
1631 XSetWindowAttributes wa;
1633 /* clean up any zombies immediately */
1637 screen = DefaultScreen(dpy);
1638 root = RootWindow(dpy, screen);
1639 fnt = drw_font_create(dpy, font);
1640 sw = DisplayWidth(dpy, screen);
1641 sh = DisplayHeight(dpy, screen);
1643 drw = drw_create(dpy, screen, root, sw, sh);
1644 drw_setfont(drw, fnt);
1647 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1648 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1649 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1650 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1651 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1652 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1653 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1654 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1655 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1656 netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
1657 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1658 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1659 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1660 netatom[NetSupportingWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1662 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1663 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1664 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1665 /* init appearance */
1666 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1667 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1668 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1669 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1670 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1671 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1675 /* EWMH support per view */
1676 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1677 PropModeReplace, (unsigned char *) netatom, NetLast);
1678 XDeleteProperty(dpy, root, netatom[NetClientList]);
1679 /* select for events */
1680 wa.cursor = cursor[CurNormal]->cursor;
1681 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1682 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1683 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1684 XSelectInput(dpy, root, wa.event_mask);
1690 showhide(Client *c) {
1693 if(ISVISIBLE(c)) { /* show clients top down */
1694 XMoveWindow(dpy, c->win, c->x, c->y);
1695 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1696 resize(c, c->x, c->y, c->w, c->h, False, 0);
1699 else { /* hide clients bottom up */
1701 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1706 sigchld(int unused) {
1707 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1708 die("Can't install SIGCHLD handler");
1709 while(0 < waitpid(-1, NULL, WNOHANG));
1713 spawn(const Arg *arg) {
1715 if(arg->v == termcmd) {
1716 for(i = 0; i < 32; ++i) {
1717 if(selmon->tagset[selmon->seltags] & (1 << i)) {
1722 WORKSPACE_NUMBER[17] = workspace_numbers_str[tag][0];
1723 WORKSPACE_NUMBER[18] = workspace_numbers_str[tag][1];
1725 if(arg->v == dmenucmd)
1726 dmenumon[0] = '0' + selmon->num;
1729 close(ConnectionNumber(dpy));
1731 execvp(((char **)arg->v)[0], (char **)arg->v);
1732 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1739 tag(const Arg *arg) {
1740 if(selmon->sel && arg->ui & TAGMASK) {
1741 selmon->sel->tags = arg->ui & TAGMASK;
1748 tagmon(const Arg *arg) {
1749 if(!selmon->sel || !mons->next)
1751 sendmon(selmon->sel, dirtomon(arg->i));
1756 unsigned int i, n, h, mw, my, ty;
1759 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1764 mw = m->nmaster ? m->ww * m->mfact : 0;
1766 c = nexttiled(m->clients);
1767 if (c && !c->screen_hog)
1768 mw = m->ww * m->mfact;
1772 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1773 if(i < m->nmaster) {
1774 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1775 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False, 0);
1779 h = (m->wh - ty) / (n - i);
1780 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False, 0);
1785 #define TAB_HEIGHT 19
1789 jason_layout(Monitor *m) {
1790 unsigned int i, tiled_count, mw, right_width, tab_counts[2] = {0,0}, cur_tab = 0, *tab_count;
1792 Client *c, *vis_slave = 0, *base = 0;
1794 tab_count = &(tab_counts[0]);
1796 for(tiled_count = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), tiled_count++) {
1797 if (tiled_count == 0) { // master
1799 if (m->sel && (m->sel == c || m->sel->isfloating || !ISVISIBLE(m->sel))) {
1800 vis_slave = nexttiled(c->next);
1806 if (c == vis_slave) {
1807 tab_count = &(tab_counts[1]);
1813 if(tiled_count == 0) {
1817 if(tiled_count > 1 || (tiled_count == 1 && !nexttiled(m->clients)->screen_hog)) {
1818 mw = m->ww * m->mfact;
1822 right_width = m->ww - mw;
1823 tab_count = &(tab_counts[0]);
1824 tab_top = m->wy - (m->wh - (2 * (TAB_HEIGHT + TAB_PAD))) + TAB_HEIGHT;
1825 for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
1827 resize(c, m->wx, m->wy, mw, m->wh, False, 0);
1829 if (c == vis_slave) {
1830 resize(c, m->wx + mw, m->wy + TAB_HEIGHT + TAB_PAD, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
1831 tab_count = &(tab_counts[1]);
1832 tab_top = m->wy + m->wh - TAB_HEIGHT;
1835 // this function does not get called when focus changes
1836 // resize(c, m->wx + m->ww, m->wy, m->ww - mw, m->wh, False);
1837 resize(c, m->wx + mw + right_width * cur_tab / (*tab_count), tab_top, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
1846 togglebar(const Arg *arg) {
1847 selmon->showbar = !selmon->showbar;
1848 updatebarpos(selmon);
1849 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1854 togglefloating(const Arg *arg) {
1857 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1859 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1860 if(selmon->sel->isfloating)
1861 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1862 selmon->sel->w, selmon->sel->h, False, 0);
1867 toggletag(const Arg *arg) {
1868 unsigned int newtags;
1872 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1874 selmon->sel->tags = newtags;
1881 toggleview(const Arg *arg) {
1882 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1885 selmon->tagset[selmon->seltags] = newtagset;
1892 unfocus(Client *c, Bool setfocus) {
1895 grabbuttons(c, False);
1896 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
1898 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1899 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1904 unmanage(Client *c, Bool destroyed) {
1905 Monitor *m = c->mon;
1908 /* The server grab construct avoids race conditions. */
1912 wc.border_width = c->oldbw;
1914 XSetErrorHandler(xerrordummy);
1915 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1916 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1917 setclientstate(c, WithdrawnState);
1919 XSetErrorHandler(xerror);
1923 focus(selmon ? selmon->sel : NULL);
1929 unmapnotify(XEvent *e) {
1931 XUnmapEvent *ev = &e->xunmap;
1933 if((c = wintoclient(ev->window))) {
1935 setclientstate(c, WithdrawnState);
1944 XSetWindowAttributes wa = {
1945 .override_redirect = True,
1946 .background_pixmap = ParentRelative,
1947 .event_mask = ButtonPressMask|ExposureMask
1949 for(m = mons; m; m = m->next) {
1952 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1953 CopyFromParent, DefaultVisual(dpy, screen),
1954 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1955 XChangeProperty(dpy, root, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
1956 PropModeReplace, (unsigned char *) &(m->barwin), 1);
1957 XChangeProperty(dpy, m->barwin, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
1958 PropModeReplace, (unsigned char *) &(m->barwin), 1);
1959 XChangeProperty(dpy, m->barwin, netatom[NetWMName], XA_STRING, 8,
1960 PropModeReplace, (unsigned char *) "dwm", 3);
1961 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1962 XMapRaised(dpy, m->barwin);
1967 updatebarpos(Monitor *m) {
1972 m->by = m->topbar ? m->wy : m->wy + m->wh;
1973 m->wy = m->topbar ? m->wy + bh : m->wy;
1980 updateclientlist() {
1984 XDeleteProperty(dpy, root, netatom[NetClientList]);
1985 for(m = mons; m; m = m->next)
1986 for(c = m->clients; c; c = c->next)
1987 XChangeProperty(dpy, root, netatom[NetClientList],
1988 XA_WINDOW, 32, PropModeAppend,
1989 (unsigned char *) &(c->win), 1);
1997 if(XineramaIsActive(dpy)) {
2001 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2002 XineramaScreenInfo *unique = NULL;
2004 for(n = 0, m = mons; m; m = m->next, n++);
2005 /* only consider unique geometries as separate screens */
2006 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
2007 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
2008 for(i = 0, j = 0; i < nn; i++)
2009 if(isuniquegeom(unique, j, &info[i]))
2010 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2014 for(i = 0; i < (nn - n); i++) { /* new monitors available */
2015 for(m = mons; m && m->next; m = m->next);
2017 m->next = createmon();
2021 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
2023 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
2024 || unique[i].width != m->mw || unique[i].height != m->mh))
2028 m->mx = m->wx = unique[i].x_org;
2029 m->my = m->wy = unique[i].y_org;
2030 m->mw = m->ww = unique[i].width;
2031 m->mh = m->wh = unique[i].height;
2035 else { /* less monitors available nn < n */
2036 for(i = nn; i < n; i++) {
2037 for(m = mons; m && m->next; m = m->next);
2041 m->clients = c->next;
2055 #endif /* XINERAMA */
2056 /* default monitor setup */
2060 if(mons->mw != sw || mons->mh != sh) {
2062 mons->mw = mons->ww = sw;
2063 mons->mh = mons->wh = sh;
2069 selmon = wintomon(root);
2075 updatenumlockmask(void) {
2077 XModifierKeymap *modmap;
2080 modmap = XGetModifierMapping(dpy);
2081 for(i = 0; i < 8; i++)
2082 for(j = 0; j < modmap->max_keypermod; j++)
2083 if(modmap->modifiermap[i * modmap->max_keypermod + j]
2084 == XKeysymToKeycode(dpy, XK_Num_Lock))
2085 numlockmask = (1 << i);
2086 XFreeModifiermap(modmap);
2090 updatesizehints(Client *c) {
2094 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
2095 /* size is uninitialized, ensure that size.flags aren't used */
2097 if(size.flags & PBaseSize) {
2098 c->basew = size.base_width;
2099 c->baseh = size.base_height;
2101 else if(size.flags & PMinSize) {
2102 c->basew = size.min_width;
2103 c->baseh = size.min_height;
2106 c->basew = c->baseh = 0;
2107 if(size.flags & PResizeInc) {
2108 c->incw = size.width_inc;
2109 c->inch = size.height_inc;
2112 c->incw = c->inch = 0;
2113 if(size.flags & PMaxSize) {
2114 c->maxw = size.max_width;
2115 c->maxh = size.max_height;
2118 c->maxw = c->maxh = 0;
2119 if(size.flags & PMinSize) {
2120 c->minw = size.min_width;
2121 c->minh = size.min_height;
2123 else if(size.flags & PBaseSize) {
2124 c->minw = size.base_width;
2125 c->minh = size.base_height;
2128 c->minw = c->minh = 0;
2129 if(size.flags & PAspect) {
2130 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2131 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2134 c->maxa = c->mina = 0.0;
2135 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2136 && c->maxw == c->minw && c->maxh == c->minh);
2140 updatetitle(Client *c) {
2141 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2142 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2143 if(c->name[0] == '\0') /* hack to mark broken clients */
2144 strcpy(c->name, broken);
2148 updatestatus(void) {
2149 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2150 strcpy(stext, "dwm-"VERSION);
2155 updatewindowtype(Client *c) {
2156 Atom state = getatomprop(c, netatom[NetWMState]);
2157 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2159 if(state == netatom[NetWMFullscreen])
2160 setfullscreen(c, True);
2161 if(wtype == netatom[NetWMWindowTypeDialog])
2162 c->isfloating = True;
2166 updatewmhints(Client *c) {
2169 if((wmh = XGetWMHints(dpy, c->win))) {
2170 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2171 wmh->flags &= ~XUrgencyHint;
2172 XSetWMHints(dpy, c->win, wmh);
2175 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2176 if(wmh->flags & InputHint)
2177 c->neverfocus = !wmh->input;
2179 c->neverfocus = False;
2185 view(const Arg *arg) {
2186 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2188 selmon->seltags ^= 1; /* toggle sel tagset */
2189 if(arg->ui & TAGMASK)
2190 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2196 wintoclient(Window w) {
2200 for(m = mons; m; m = m->next)
2201 for(c = m->clients; c; c = c->next)
2208 wintomon(Window w) {
2213 if(w == root && getrootptr(&x, &y))
2214 return recttomon(x, y, 1, 1);
2215 for(m = mons; m; m = m->next)
2218 if((c = wintoclient(w)))
2223 /* There's no way to check accesses to destroyed windows, thus those cases are
2224 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2225 * default error handler, which may call exit. */
2227 xerror(Display *dpy, XErrorEvent *ee) {
2228 if(ee->error_code == BadWindow
2229 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2230 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2231 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2232 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2233 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2234 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2235 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2236 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2238 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2239 ee->request_code, ee->error_code);
2240 return xerrorxlib(dpy, ee); /* may call exit */
2244 xerrordummy(Display *dpy, XErrorEvent *ee) {
2248 /* Startup Error handler to check if another window manager
2249 * is already running. */
2251 xerrorstart(Display *dpy, XErrorEvent *ee) {
2252 die("dwm: another window manager is already running\n");
2257 zoom(const Arg *arg) {
2258 Client *c = selmon->sel;
2260 if(!selmon->lt[selmon->sellt]->arrange
2261 || (selmon->sel && selmon->sel->isfloating))
2263 if(c == nexttiled(selmon->clients))
2264 if(!c || !(c = nexttiled(c->next)))
2270 main(int argc, char *argv[]) {
2271 if(argc == 2 && !strcmp("-v", argv[1]))
2272 die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
2274 die("usage: dwm [-v]\n");
2275 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2276 fputs("warning: no locale support\n", stderr);
2277 if(!(dpy = XOpenDisplay(NULL)))
2278 die("dwm: cannot open display\n");
2285 return EXIT_SUCCESS;