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/XF86keysym.h>
36 #include <X11/Xatom.h>
38 #include <X11/Xproto.h>
39 #include <X11/Xutil.h>
41 #include <X11/extensions/Xinerama.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_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
61 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
62 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
63 enum { NetSupported, NetWMName, NetWMState,
64 NetWMFullscreen, NetWMWindowOpacity, NetActiveWindow, NetWMWindowType,
65 NetWMWindowTypeDialog, NetClientList, NetSupportingWMCheck, NetLast }; /* EWMH atoms */
66 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
67 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
68 ClkClientWin, ClkRootWin, ClkAnywhere, 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;
96 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, screen_hog;
106 void (*func)(const Arg *);
112 void (*arrange)(Monitor *);
120 int by; /* bar geometry */
121 int mx, my, mw, mh; /* screen size */
122 int wx, wy, ww, wh; /* window area */
123 unsigned int seltags;
125 unsigned int tagset[2];
138 const char *instance;
146 /* function declarations */
147 static void applyrules(Client *c);
148 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
149 static void arrange(Monitor *m);
150 static void arrangemon(Monitor *m);
151 static void attach_as_master(Client *c);
152 static void attach(Client *c);
153 static void attachstack(Client *c);
154 static void buttonpress(XEvent *e);
155 static void checkotherwm(void);
156 static void cleanup(void);
157 static void cleanupmon(Monitor *mon);
158 static void clearurgent(Client *c);
159 static void clientmessage(XEvent *e);
160 static void configure(Client *c);
161 static void configurenotify(XEvent *e);
162 static void configurerequest(XEvent *e);
163 static Monitor *createmon(void);
164 static void destroynotify(XEvent *e);
165 static void detach(Client *c);
166 static void detachstack(Client *c);
167 static Monitor *dirtomon(int dir);
168 static void drawbar(Monitor *m);
169 static void drawbars(void);
170 static void enternotify(XEvent *e);
171 static void expose(XEvent *e);
172 static void focus(Client *c);
173 static void focusin(XEvent *e);
174 static void focusmon(const Arg *arg);
175 static void focusstack(const Arg *arg);
176 static Bool getrootptr(int *x, int *y);
177 static long getstate(Window w);
178 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
179 static void grabbuttons(Client *c, Bool focused);
180 static void grabkeys(void);
181 static void incnmaster(const Arg *arg);
182 static void keypress(XEvent *e);
183 static void killclient(const Arg *arg);
184 static void manage(Window w, XWindowAttributes *wa);
185 static void mappingnotify(XEvent *e);
186 static void maprequest(XEvent *e);
187 static void monocle(Monitor *m);
188 static void motionnotify(XEvent *e);
189 static void movemouse(const Arg *arg);
190 static Client *nexttiled(Client *c);
191 static Client *snexttiled(Client *c);
192 static Client *nextvisible(Client *c);
193 static void pop(Client *);
194 static void propertynotify(XEvent *e);
195 static void quit(const Arg *arg);
196 static Monitor *recttomon(int x, int y, int w, int h);
197 static void resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base);
198 static void resizeclient(Client *c, int x, int y, int w, int h, Client *base);
199 static void resizemouse(const Arg *arg);
200 static void restack(Monitor *m);
201 static void run(void);
202 static void scan(void);
203 static Bool sendevent(Client *c, Atom proto);
204 static void sendmon(Client *c, Monitor *m);
205 static void setclientstate(Client *c, long state);
206 static void setfocus(Client *c);
207 static void setfullscreen(Client *c, Bool fullscreen);
208 static void setlayout(const Arg *arg);
209 static void setmfact(const Arg *arg);
210 static void setup(void);
211 static void showhide(Client *c);
212 static void sigchld(int unused);
213 static void kbspawn(const Arg *arg);
214 static void spawn(const Arg *arg);
215 static void tag(const Arg *arg);
216 static void tagmon(const Arg *arg);
217 static void jason_layout(Monitor *);
218 static void tile(Monitor *);
219 static void togglebar(const Arg *arg);
220 static void togglefloating(const Arg *arg);
221 static void toggletag(const Arg *arg);
222 static void toggleview(const Arg *arg);
223 static void unfocus(Client *c, Bool setfocus);
224 static void unmanage(Client *c, Bool destroyed);
225 static void unmapnotify(XEvent *e);
226 static Bool updategeom(void);
227 static void updatebarpos(Monitor *m);
228 static void updatebars(void);
229 static void updateclientlist(void);
230 static void updatenumlockmask(void);
231 static void updatesizehints(Client *c);
232 static void updatestatus(void);
233 static void updatewindowtype(Client *c);
234 static void updatetitle(Client *c);
235 static void updatewmhints(Client *c);
236 static void view(const Arg *arg);
237 static Client *wintoclient(Window w);
238 static Monitor *wintomon(Window w);
239 static int xerror(Display *dpy, XErrorEvent *ee);
240 static int xerrordummy(Display *dpy, XErrorEvent *ee);
241 static int xerrorstart(Display *dpy, XErrorEvent *ee);
242 static void zoom(const Arg *arg);
245 static Bool key_buffering = False;
246 static const char broken[] = "broken";
247 static char stext[256];
249 static int sw, sh; /* X display screen geometry width, height */
250 static int bh, blw = 0; /* bar geometry */
251 static int (*xerrorxlib)(Display *, XErrorEvent *);
252 static unsigned int numlockmask = 0;
253 static void (*handler[LASTEvent]) (XEvent *) = {
254 [ButtonPress] = buttonpress,
255 [ClientMessage] = clientmessage,
256 [ConfigureRequest] = configurerequest,
257 [ConfigureNotify] = configurenotify,
258 [DestroyNotify] = destroynotify,
259 [EnterNotify] = enternotify,
262 [KeyPress] = keypress,
263 [MappingNotify] = mappingnotify,
264 [MapRequest] = maprequest,
265 [MotionNotify] = motionnotify,
266 [PropertyNotify] = propertynotify,
267 [UnmapNotify] = unmapnotify
269 static Atom wmatom[WMLast], netatom[NetLast];
270 static Bool running = True;
271 static Cur *cursor[CurLast];
272 static ClrScheme scheme[SchemeLast];
276 static Monitor *mons, *selmon;
279 // unfocused windows get transparent (feature)
280 static const unsigned long opacities[] = { 0, 0xbfffffff, 0x00000000 }; // first unused
281 static void window_set_opaque(Client *c);
282 static void window_set_translucent(Client *c);
283 static void window_set_invisible(Client *c);
284 static void window_set_opacity(Client *c, int opacity_index);
285 static void update_window_opacities(Monitor *m);
287 window_set_opacity(Client *c, int opacity_index) {
288 if (c->opacity == opacity_index) {
291 c->opacity = opacity_index;
292 if (opacity_index == 0) {
293 XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
295 XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)(&opacities[opacity_index]), 1);
299 window_set_opaque(Client *c) {
300 window_set_opacity(c, 0);
303 window_set_translucent(Client *c) {
304 window_set_opacity(c, 1);
307 window_set_invisible(Client *c) {
308 window_set_opacity(c, 2);
311 update_window_opacities(Monitor *m) {
312 Client *master, *slave, *c;
313 Bool selection_floating = False;
314 slave = master = nexttiled(m->clients);
315 if (master) slave = nexttiled(master->next);
316 if (m->sel && m->sel != master) {
317 if (nexttiled(m->sel) == m->sel) // if selection is tiled
320 selection_floating = True;
322 for (c = m->clients; c; c = c->next) {
324 if (c->isfloating || c == m->sel || (selection_floating && (c == master || c == slave))) {
325 window_set_opaque(c);
326 } else if (c == master || c == slave) {
327 window_set_translucent(c);
329 window_set_opaque(c);
336 /* configuration, allows nested code to access above variables */
339 /* compile-time check if all tags fit into an unsigned int bit array. */
340 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
342 /* function implementations */
344 applyrules(Client *c) {
345 const char *class, *instance;
349 XClassHint ch = { NULL, NULL };
352 c->isfloating = c->tags = c->screen_hog = 0;
353 XGetClassHint(dpy, c->win, &ch);
354 class = ch.res_class ? ch.res_class : broken;
355 instance = ch.res_name ? ch.res_name : broken;
357 for(i = 0; i < LENGTH(rules); i++) {
359 if((!r->title || c->name == strstr(c->name, r->title))
360 && (!r->class || strstr(class, r->class))
361 && (!r->instance || strstr(instance, r->instance)))
363 c->isfloating = r->isfloating;
365 c->x = -1; c->y = -2; // secret code for centered
368 c->screen_hog = r->screen_hog;
369 for(m = mons; m && m->num != r->monitor; m = m->next);
378 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
382 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
384 // Monitor *m = c->mon;
386 /* set minimum possible */
394 if(*x + *w + 2 * c->bw < 0)
396 if(*y + *h + 2 * c->bw < 0)
399 // jason: let windows be offscreen
401 // if(*x >= m->wx + m->ww)
402 // *x = m->wx + m->ww - WIDTH(c);
403 // if(*y >= m->wy + m->wh)
404 // *y = m->wy + m->wh - HEIGHT(c);
405 // if(*x + *w + 2 * c->bw <= m->wx)
407 // if(*y + *h + 2 * c->bw <= m->wy)
414 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
415 /* see last two sentences in ICCCM 4.1.2.3 */
416 baseismin = c->basew == c->minw && c->baseh == c->minh;
417 if(!baseismin) { /* temporarily remove base dimensions */
421 /* adjust for aspect limits */
422 if(c->mina > 0 && c->maxa > 0) {
423 if(c->maxa < (float)*w / *h)
424 *w = *h * c->maxa + 0.5;
425 else if(c->mina < (float)*h / *w)
426 *h = *w * c->mina + 0.5;
428 if(baseismin) { /* increment calculation requires this */
432 /* adjust for increment value */
437 /* restore base dimensions */
438 *w = MAX(*w + c->basew, c->minw);
439 *h = MAX(*h + c->baseh, c->minh);
441 *w = MIN(*w, c->maxw);
443 *h = MIN(*h, c->maxh);
445 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
449 arrange(Monitor *m) {
452 else for(m = mons; m; m = m->next)
457 } else for(m = mons; m; m = m->next)
462 arrangemon(Monitor *m) {
463 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
464 if(m->lt[m->sellt]->arrange)
465 m->lt[m->sellt]->arrange(m);
469 attach_as_master(Client *c) {
470 c->next = c->mon->clients;
476 c->next = c->mon->sel->next;
477 c->mon->sel->next = c;
484 attachstack(Client *c) {
485 c->snext = c->mon->stack;
490 buttonpress(XEvent *e) {
491 unsigned int i, click;
495 XButtonPressedEvent *ev = &e->xbutton;
498 for(i = 0; i < LENGTH(buttons); i++) {
499 if(buttons[i].click == ClkAnywhere && buttons[i].button == ev->button
500 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
501 buttons[i].func(&buttons[i].arg);
510 /* focus monitor if necessary */
511 if((m = wintomon(ev->window)) && m != selmon) {
512 unfocus(selmon->sel, True);
516 if(ev->window == selmon->barwin) {
518 } else if((c = wintoclient(ev->window))) {
520 click = ClkClientWin;
522 for(i = 0; i < LENGTH(buttons); i++) {
523 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
524 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
525 if (click == ClkTagBar && buttons[i].arg.i == 0) {
526 buttons[i].func(&arg);
528 buttons[i].func(&buttons[i].arg);
536 xerrorxlib = XSetErrorHandler(xerrorstart);
537 /* this causes an error if some other window manager is running */
538 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
540 XSetErrorHandler(xerror);
547 Layout foo = { "", NULL };
551 selmon->lt[selmon->sellt] = &foo;
552 for(m = mons; m; m = m->next)
554 unmanage(m->stack, False);
555 XUngrabKey(dpy, AnyKey, AnyModifier, root);
558 drw_cur_free(drw, cursor[CurNormal]);
559 drw_cur_free(drw, cursor[CurResize]);
560 drw_cur_free(drw, cursor[CurMove]);
561 drw_font_free(dpy, fnt);
562 drw_clr_free(scheme[SchemeNorm].border);
563 drw_clr_free(scheme[SchemeNorm].bg);
564 drw_clr_free(scheme[SchemeNorm].fg);
565 drw_clr_free(scheme[SchemeSel].border);
566 drw_clr_free(scheme[SchemeSel].bg);
567 drw_clr_free(scheme[SchemeSel].fg);
570 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
571 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
575 cleanupmon(Monitor *mon) {
581 for(m = mons; m && m->next != mon; m = m->next);
584 XUnmapWindow(dpy, mon->barwin);
585 XDestroyWindow(dpy, mon->barwin);
590 clearurgent(Client *c) {
594 if(!(wmh = XGetWMHints(dpy, c->win)))
596 wmh->flags &= ~XUrgencyHint;
597 XSetWMHints(dpy, c->win, wmh);
602 clientmessage(XEvent *e) {
603 XClientMessageEvent *cme = &e->xclient;
604 Client *c = wintoclient(cme->window);
608 if(cme->message_type == netatom[NetWMState]) {
609 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
610 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
611 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
613 else if(cme->message_type == netatom[NetActiveWindow]) {
614 // Jason added this so apps can't steal focus:
617 c->mon->seltags ^= 1;
618 c->mon->tagset[c->mon->seltags] = c->tags;
625 configure(Client *c) {
628 ce.type = ConfigureNotify;
636 ce.border_width = c->bw;
638 ce.override_redirect = False;
639 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
643 configurenotify(XEvent *e) {
645 XConfigureEvent *ev = &e->xconfigure;
648 // TODO: updategeom handling sucks, needs to be simplified
649 if(ev->window == root) {
650 dirty = (sw != ev->width || sh != ev->height);
653 if(updategeom() || dirty) {
654 drw_resize(drw, sw, bh);
656 for(m = mons; m; m = m->next)
657 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
665 configurerequest(XEvent *e) {
668 XConfigureRequestEvent *ev = &e->xconfigurerequest;
671 if((c = wintoclient(ev->window))) {
672 if(ev->value_mask & CWBorderWidth)
673 c->bw = ev->border_width;
674 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
676 if(ev->value_mask & CWX) {
678 c->x = m->mx + ev->x;
680 if(ev->value_mask & CWY) {
682 c->y = m->my + ev->y;
684 if(ev->value_mask & CWWidth) {
688 if(ev->value_mask & CWHeight) {
692 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
693 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
694 if((c->y + c->h) > m->my + m->mh && c->isfloating)
695 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
696 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
699 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
707 wc.width = ev->width;
708 wc.height = ev->height;
709 wc.border_width = ev->border_width;
710 wc.sibling = ev->above;
711 wc.stack_mode = ev->detail;
712 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
721 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
722 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
723 m->tagset[0] = m->tagset[1] = 1;
725 m->nmaster = nmaster;
726 m->showbar = showbar;
728 m->lt[0] = &layouts[0];
729 m->lt[1] = &layouts[1 % LENGTH(layouts)];
730 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
735 destroynotify(XEvent *e) {
737 XDestroyWindowEvent *ev = &e->xdestroywindow;
739 if((c = wintoclient(ev->window)))
747 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
751 // NOTE: the stack is for z-order and most-recently-focused
752 // only mon->clients determines position in visible layout
754 detachstack(Client *c) {
755 Client *prev = NULL, *next_sel = NULL, *i;
756 for(i = c->mon->stack; i && i != c; i = i->snext) {
759 if(c == c->mon->sel) {
760 // find last visible window before c
761 // WARNING if you detach() before detachstack() this will select last visible window
762 for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
764 // failing that, find first visible window (besides c)
766 for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
770 c->mon->sel = next_sel;
773 prev->snext = c->snext;
775 c->mon->stack = c->snext;
784 if(!(m = selmon->next))
787 else if(selmon == mons)
788 for(m = mons; m->next; m = m->next);
790 for(m = mons; m->next != selmon; m = m->next);
795 drawbar(Monitor *m) {
797 unsigned int i, occ = 0, urg = 0;
800 for(c = m->clients; c; c = c->next) {
806 for(i = 0; i < LENGTH(tags); i++) {
808 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
809 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
810 drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
811 occ & 1 << i, urg & 1 << i);
814 w = blw = TEXTW(m->ltsymbol);
815 drw_setscheme(drw, &scheme[SchemeNorm]);
816 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
819 if(m == selmon) { /* status is only drawn on selected monitor */
826 drw_text(drw, x, 0, w, bh, stext, 0);
830 if((w = x - xx) > bh) {
833 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
834 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
835 drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
838 drw_setscheme(drw, &scheme[SchemeNorm]);
839 drw_text(drw, x, 0, w, bh, NULL, 0);
842 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
849 for(m = mons; m; m = m->next)
854 enternotify(XEvent *e) {
857 XCrossingEvent *ev = &e->xcrossing;
859 return; // jason: added to stop mouse focus
861 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
863 c = wintoclient(ev->window);
864 m = c ? c->mon : wintomon(ev->window);
866 unfocus(selmon->sel, True);
869 else if(!c || c == selmon->sel)
877 XExposeEvent *ev = &e->xexpose;
879 if(ev->count == 0 && (m = wintomon(ev->window)))
885 if(!c || !ISVISIBLE(c))
886 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
887 /* was if(selmon->sel) */
888 if(selmon->sel && selmon->sel != c)
889 unfocus(selmon->sel, False);
897 grabbuttons(c, True);
898 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
902 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
903 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
907 update_window_opacities(selmon);
909 if(c && (!root || (c->win!=root)) )
910 window_set_opaque(c);
914 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
915 XFocusChangeEvent *ev = &e->xfocus;
917 if(selmon->sel && ev->window != selmon->sel->win)
918 setfocus(selmon->sel);
922 focusmon(const Arg *arg) {
927 if((m = dirtomon(arg->i)) == selmon)
929 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
930 in gedit and anjuta */
936 focusstack(const Arg *arg) {
937 Client *c = NULL, *i;
942 for(i = selmon->clients; i != selmon->sel; i = i->next) {
948 } else if(arg->i > 0) {
949 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
951 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
954 for(i = selmon->clients; i != selmon->sel; i = i->next)
958 for(; i; i = i->next)
969 getatomprop(Client *c, Atom prop) {
972 unsigned char *p = NULL;
973 Atom da, atom = None;
975 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
976 &da, &di, &dl, &dl, &p) == Success && p) {
984 getrootptr(int *x, int *y) {
989 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
996 unsigned char *p = NULL;
997 unsigned long n, extra;
1000 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
1001 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
1010 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
1015 if(!text || size == 0)
1018 XGetTextProperty(dpy, w, &name, atom);
1021 if(name.encoding == XA_STRING)
1022 strncpy(text, (char *)name.value, size - 1);
1024 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1025 strncpy(text, *list, size - 1);
1026 XFreeStringList(list);
1029 text[size - 1] = '\0';
1035 grabbuttons(Client *c, Bool focused) {
1036 updatenumlockmask();
1039 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1040 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1042 for(i = 0; i < LENGTH(buttons); i++)
1043 if(buttons[i].click == ClkClientWin || buttons[i].click == ClkAnywhere)
1044 for(j = 0; j < LENGTH(modifiers); j++)
1045 XGrabButton(dpy, buttons[i].button,
1046 buttons[i].mask | modifiers[j],
1047 c->win, False, BUTTONMASK,
1048 GrabModeAsync, GrabModeSync, None, None);
1050 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1051 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1058 updatenumlockmask();
1059 //XUngrabKey(dpy, AnyKey, AnyModifier, root);
1062 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1065 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1066 for(i = 0; i < LENGTH(keys); i++)
1067 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1068 for(j = 0; j < LENGTH(modifiers); j++)
1069 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1070 True, GrabModeAsync,
1071 keys[i].func == kbspawn ? GrabModeSync : GrabModeAsync);
1076 incnmaster(const Arg *arg) {
1077 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1083 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1085 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1086 && unique[n].width == info->width && unique[n].height == info->height)
1090 #endif /* XINERAMA */
1093 keypress(XEvent *e) {
1099 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1100 for(i = 0; i < LENGTH(keys); i++) {
1101 if(keysym == keys[i].keysym
1102 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1104 keys[i].func(&(keys[i].arg));
1110 killclient(const Arg *arg) {
1113 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1115 XSetErrorHandler(xerrordummy);
1116 XSetCloseDownMode(dpy, DestroyAll);
1117 XKillClient(dpy, selmon->sel->win);
1119 XSetErrorHandler(xerror);
1125 manage(Window w, XWindowAttributes *wa) {
1126 Client *c, *t = NULL;
1127 Window trans = None;
1130 if(!(c = calloc(1, sizeof(Client))))
1131 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1132 c->opacity = -1; // who knows
1135 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1144 if(c->x == -1 && c->y == -2) { // secret code for centered
1145 c->x = c->oldx = (c->mon->ww - wa->width) / 2;
1146 c->y = c->oldy = (c->mon->wh - wa->height) / 2;
1148 c->x = c->oldx = wa->x;
1149 c->y = c->oldy = wa->y;
1151 c->w = c->oldw = wa->width;
1152 c->h = c->oldh = wa->height;
1153 c->oldbw = wa->border_width;
1155 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1156 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1157 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1158 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1159 c->x = MAX(c->x, c->mon->mx);
1160 /* only fix client y-offset, if the client center might cover the bar */
1161 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1162 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1165 wc.border_width = c->bw;
1166 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1167 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
1168 configure(c); /* propagates border_width, if size doesn't change */
1169 updatewindowtype(c);
1172 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1173 grabbuttons(c, False);
1175 c->isfloating = c->oldstate = trans != None || c->isfixed;
1177 XRaiseWindow(dpy, c->win);
1180 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1181 (unsigned char *) &(c->win), 1);
1182 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1183 setclientstate(c, NormalState);
1184 if (c->mon == selmon)
1185 unfocus(selmon->sel, False);
1188 XMapWindow(dpy, c->win);
1191 key_buffering = False;
1192 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
1197 mappingnotify(XEvent *e) {
1198 XMappingEvent *ev = &e->xmapping;
1199 // fprintf(stderr, "MapNotify\n");
1201 XRefreshKeyboardMapping(ev);
1202 if(ev->request == MappingKeyboard)
1207 maprequest(XEvent *e) {
1208 static XWindowAttributes wa;
1209 XMapRequestEvent *ev = &e->xmaprequest;
1211 // fprintf(stderr, "MapRequest\n");
1212 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1214 if(wa.override_redirect) {
1217 if(!wintoclient(ev->window))
1218 manage(ev->window, &wa);
1222 monocle(Monitor *m) {
1226 for(c = m->clients; c; c = c->next)
1229 if(n > 0) /* override layout symbol */
1230 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1231 for(c = snexttiled(m->stack); c; c = snexttiled(c->snext)) {
1233 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False, 0);
1235 // this window is should not be visible. move off top, but don't change h/w
1236 resize(c, m->wx, m->wy - 4000, c->w, c->h, False, 0);
1242 motionnotify(XEvent *e) {
1243 static Monitor *mon = NULL;
1245 XMotionEvent *ev = &e->xmotion;
1247 if(ev->window != root)
1249 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1250 unfocus(selmon->sel, True);
1258 movemouse(const Arg *arg) {
1259 int x, y, ocx, ocy, nx, ny;
1265 if(!(c = selmon->sel))
1267 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1272 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1273 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1275 if(!getrootptr(&x, &y))
1278 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1280 case ConfigureRequest:
1283 handler[ev.type](&ev);
1286 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1288 lasttime = ev.xmotion.time;
1290 nx = ocx + (ev.xmotion.x - x);
1291 ny = ocy + (ev.xmotion.y - y);
1292 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1293 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1294 if(abs(selmon->wx - nx) < snap)
1296 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1297 nx = selmon->wx + selmon->ww - WIDTH(c);
1298 if(abs(selmon->wy - ny) < snap)
1300 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1301 ny = selmon->wy + selmon->wh - HEIGHT(c);
1302 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1303 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1304 togglefloating(NULL);
1306 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1307 resize(c, nx, ny, c->w, c->h, True, 0);
1310 } while(ev.type != ButtonRelease);
1311 XUngrabPointer(dpy, CurrentTime);
1312 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1320 nexttiled(Client *c) {
1321 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1326 snexttiled(Client *c) {
1327 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->snext);
1332 nextvisible(Client *c) {
1333 for(; c && !ISVISIBLE(c); c = c->next);
1340 attach_as_master(c);
1346 propertynotify(XEvent *e) {
1349 XPropertyEvent *ev = &e->xproperty;
1351 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1353 else if(ev->state == PropertyDelete)
1354 return; /* ignore */
1355 else if((c = wintoclient(ev->window))) {
1358 case XA_WM_TRANSIENT_FOR:
1359 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1360 (c->isfloating = (wintoclient(trans)) != NULL))
1363 case XA_WM_NORMAL_HINTS:
1371 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1373 if(c == c->mon->sel)
1376 if(ev->atom == netatom[NetWMWindowType])
1377 updatewindowtype(c);
1382 quit(const Arg *arg) {
1387 recttomon(int x, int y, int w, int h) {
1388 Monitor *m, *r = selmon;
1391 for(m = mons; m; m = m->next)
1392 if((a = INTERSECT(x, y, w, h, m)) > area) {
1400 resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base) {
1401 if(applysizehints(c, &x, &y, &w, &h, interact))
1402 resizeclient(c, x, y, w, h, base);
1406 resizeclient(Client *c, int x, int y, int w, int h, Client *base) {
1408 unsigned long mask = CWX|CWY|CWWidth|CWHeight|CWBorderWidth;
1410 c->oldx = c->x; c->x = wc.x = x;
1411 c->oldy = c->y; c->y = wc.y = y;
1412 c->oldw = c->w; c->w = wc.width = w;
1413 c->oldh = c->h; c->h = wc.height = h;
1416 wc.stack_mode = Above;
1417 wc.sibling = base->win;
1418 mask |= CWStackMode|CWSibling;
1420 wc.border_width = c->bw;
1421 XConfigureWindow(dpy, c->win, mask, &wc);
1427 resizemouse(const Arg *arg) {
1428 int ocx, ocy, nw, nh;
1434 if(!(c = selmon->sel))
1436 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1441 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1442 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1444 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1446 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1448 case ConfigureRequest:
1451 handler[ev.type](&ev);
1454 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1456 lasttime = ev.xmotion.time;
1458 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1459 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1460 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1461 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1463 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1464 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1465 togglefloating(NULL);
1467 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1468 resize(c, c->x, c->y, nw, nh, True, 0);
1471 } while(ev.type != ButtonRelease);
1472 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1473 XUngrabPointer(dpy, CurrentTime);
1474 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1475 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1483 restack(Monitor *m) {
1491 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1492 XRaiseWindow(dpy, m->sel->win);
1493 if(m->lt[m->sellt]->arrange) {
1494 wc.stack_mode = Below;
1495 wc.sibling = m->barwin;
1496 for(c = m->clients; c; c = c->next)
1497 if(!c->isfloating && ISVISIBLE(c)) {
1498 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1499 wc.sibling = c->win;
1500 wc.stack_mode = Above;
1504 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1510 /* main event loop */
1512 while(running && !XNextEvent(dpy, &ev)) {
1513 if(handler[ev.type]) {
1514 // fprintf(stderr, "handling event type %i\n", ev.type);
1515 handler[ev.type](&ev); /* call handler */
1517 // fprintf(stderr, "evt type %i\n", ev.type);
1524 unsigned int i, num;
1525 Window d1, d2, *wins = NULL;
1526 XWindowAttributes wa;
1528 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1529 for(i = 0; i < num; i++) {
1530 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1531 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1533 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1534 manage(wins[i], &wa);
1536 for(i = 0; i < num; i++) { /* now the transients */
1537 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1539 if(XGetTransientForHint(dpy, wins[i], &d1)
1540 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1541 manage(wins[i], &wa);
1549 sendmon(Client *c, Monitor *m) {
1556 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1564 setclientstate(Client *c, long state) {
1565 long data[] = { state, None };
1567 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1568 PropModeReplace, (unsigned char *)data, 2);
1572 sendevent(Client *c, Atom proto) {
1575 Bool exists = False;
1578 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1579 while(!exists && n--)
1580 exists = protocols[n] == proto;
1584 ev.type = ClientMessage;
1585 ev.xclient.window = c->win;
1586 ev.xclient.message_type = wmatom[WMProtocols];
1587 ev.xclient.format = 32;
1588 ev.xclient.data.l[0] = proto;
1589 ev.xclient.data.l[1] = CurrentTime;
1590 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1596 setfocus(Client *c) {
1597 if(!c->neverfocus) {
1598 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1599 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1600 XA_WINDOW, 32, PropModeReplace,
1601 (unsigned char *) &(c->win), 1);
1603 sendevent(c, wmatom[WMTakeFocus]);
1607 setfullscreen(Client *c, Bool fullscreen) {
1609 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1610 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1611 c->isfullscreen = True;
1612 c->oldstate = c->isfloating;
1615 c->isfloating = True;
1616 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh, 0);
1617 XRaiseWindow(dpy, c->win);
1620 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1621 PropModeReplace, (unsigned char*)0, 0);
1622 c->isfullscreen = False;
1623 c->isfloating = c->oldstate;
1629 resizeclient(c, c->x, c->y, c->w, c->h, 0);
1635 setlayout(const Arg *arg) {
1636 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1639 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1640 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1647 /* arg > 1.0 will set mfact absolutly */
1649 setmfact(const Arg *arg) {
1652 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1654 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1655 if(f < 0.1 || f > 0.9)
1663 XSetWindowAttributes wa;
1665 /* clean up any zombies immediately */
1669 screen = DefaultScreen(dpy);
1670 root = RootWindow(dpy, screen);
1671 fnt = drw_font_create(dpy, font);
1672 sw = DisplayWidth(dpy, screen);
1673 sh = DisplayHeight(dpy, screen);
1675 drw = drw_create(dpy, screen, root, sw, sh);
1676 drw_setfont(drw, fnt);
1679 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1680 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1681 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1682 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1683 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1684 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1685 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1686 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1687 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1688 netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
1689 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1690 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1691 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1692 netatom[NetSupportingWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1693 XInternAtom(dpy, "_MOTIF_WM_HINTS", False); /* clients may request borderless/fullscreen */
1695 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1696 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1697 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1698 /* init appearance */
1699 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1700 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1701 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1702 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1703 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1704 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1708 /* EWMH support per view */
1709 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1710 PropModeReplace, (unsigned char *) netatom, NetLast);
1711 XDeleteProperty(dpy, root, netatom[NetClientList]);
1712 /* select for events */
1713 wa.cursor = cursor[CurNormal]->cursor;
1714 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1715 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1716 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1717 XSelectInput(dpy, root, wa.event_mask);
1723 showhide(Client *c) {
1726 if(ISVISIBLE(c)) { /* show clients top down */
1727 XMoveWindow(dpy, c->win, c->x, c->y);
1728 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1729 resize(c, c->x, c->y, c->w, c->h, False, 0);
1732 else { /* hide clients bottom up */
1734 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1739 sigchld(int unused) {
1740 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1741 die("Can't install SIGCHLD handler");
1742 while(0 < waitpid(-1, NULL, WNOHANG));
1746 kbspawn(const Arg *arg) {
1747 key_buffering = True;
1752 spawn(const Arg *arg) {
1754 if(arg->v == termcmd || arg->v == runcmd) {
1755 for(i = 0; i < 32; ++i) {
1756 if(selmon->tagset[selmon->seltags] & (1 << i)) {
1761 WORKSPACE_NUMBER[17] = workspace_numbers_str[tag][0];
1762 WORKSPACE_NUMBER[18] = workspace_numbers_str[tag][1]; // 2nd digit or null
1764 if(arg->v == dmenucmd)
1765 dmenumon[0] = '0' + selmon->num;
1768 close(ConnectionNumber(dpy));
1770 execvp(((char **)arg->v)[0], (char **)arg->v);
1771 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1778 tag(const Arg *arg) {
1779 if(selmon->sel && arg->ui & TAGMASK) {
1780 selmon->sel->tags = arg->ui & TAGMASK;
1787 tagmon(const Arg *arg) {
1788 if(!selmon->sel || !mons->next)
1790 sendmon(selmon->sel, dirtomon(arg->i));
1795 unsigned int i, n, h, mw, my, ty;
1798 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1803 mw = m->nmaster ? m->ww * m->mfact : 0;
1805 c = nexttiled(m->clients);
1806 if (c && !c->screen_hog)
1807 mw = m->ww * m->mfact;
1811 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1812 if(i < m->nmaster) {
1813 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1814 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False, 0);
1818 h = (m->wh - ty) / (n - i);
1819 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False, 0);
1824 #define TAB_HEIGHT 19
1829 jason_layout(Monitor *m) {
1830 unsigned int i, tiled_count, mw, right_width, tab_counts[2] = {0,0}, cur_tab = 0, *tab_count;
1832 Client *c, *vis_slave = 0, *base = 0;
1834 tab_count = &(tab_counts[0]);
1836 for(tiled_count = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), tiled_count++) {
1837 if (tiled_count == 0) { // master
1839 if (m->sel && (m->sel == c || m->sel->isfloating || !ISVISIBLE(m->sel))) {
1840 vis_slave = nexttiled(c->next);
1846 if (c == vis_slave) {
1847 tab_count = &(tab_counts[1]);
1853 if(tiled_count == 0) {
1857 if(tiled_count > 1 || (tiled_count == 1 && !nexttiled(m->clients)->screen_hog)) {
1858 mw = m->ww * m->mfact;
1862 right_width = m->ww - mw - GUTTER_PX;
1863 tab_count = &(tab_counts[0]);
1864 tab_top = m->wy - (m->wh - (2 * (TAB_HEIGHT + TAB_PAD))) + TAB_HEIGHT;
1865 for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
1867 resize(c, m->wx, m->wy, mw, m->wh, False, 0);
1869 if (c == vis_slave) {
1870 resize(c, m->wx + mw + GUTTER_PX, m->wy + TAB_HEIGHT + TAB_PAD, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
1871 tab_count = &(tab_counts[1]);
1872 tab_top = m->wy + m->wh - TAB_HEIGHT;
1875 // this function does not get called when focus changes
1876 // resize(c, m->wx + m->ww, m->wy, m->ww - mw, m->wh, False);
1877 resize(c, m->wx + mw + GUTTER_PX + right_width * cur_tab / (*tab_count), tab_top, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
1886 togglebar(const Arg *arg) {
1887 selmon->showbar = !selmon->showbar;
1888 updatebarpos(selmon);
1889 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1894 togglefloating(const Arg *arg) {
1897 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1899 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1900 if(selmon->sel->isfloating)
1901 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1902 selmon->sel->w, selmon->sel->h, False, 0);
1907 toggletag(const Arg *arg) {
1908 unsigned int newtags;
1912 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1914 selmon->sel->tags = newtags;
1921 toggleview(const Arg *arg) {
1922 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1925 selmon->tagset[selmon->seltags] = newtagset;
1932 unfocus(Client *c, Bool setfocus) {
1935 grabbuttons(c, False);
1936 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
1938 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1939 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1944 unmanage(Client *c, Bool destroyed) {
1945 Monitor *m = c->mon;
1948 /* The server grab construct avoids race conditions. */
1952 wc.border_width = c->oldbw;
1954 XSetErrorHandler(xerrordummy);
1955 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1956 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1957 setclientstate(c, WithdrawnState);
1959 XSetErrorHandler(xerror);
1963 focus(selmon ? selmon->sel : NULL);
1969 unmapnotify(XEvent *e) {
1971 XUnmapEvent *ev = &e->xunmap;
1973 if((c = wintoclient(ev->window))) {
1975 setclientstate(c, WithdrawnState);
1984 XSetWindowAttributes wa = {
1985 .override_redirect = True,
1986 .background_pixmap = ParentRelative,
1987 .event_mask = ButtonPressMask|ExposureMask
1989 for(m = mons; m; m = m->next) {
1992 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1993 CopyFromParent, DefaultVisual(dpy, screen),
1994 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1995 XChangeProperty(dpy, root, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
1996 PropModeReplace, (unsigned char *) &(m->barwin), 1);
1997 XChangeProperty(dpy, m->barwin, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
1998 PropModeReplace, (unsigned char *) &(m->barwin), 1);
1999 XChangeProperty(dpy, m->barwin, netatom[NetWMName], XA_STRING, 8,
2000 PropModeReplace, (unsigned char *) "dwm", 3);
2001 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2002 XMapRaised(dpy, m->barwin);
2007 updatebarpos(Monitor *m) {
2012 m->by = m->topbar ? m->wy : m->wy + m->wh;
2013 m->wy = m->topbar ? m->wy + bh : m->wy;
2020 updateclientlist() {
2024 XDeleteProperty(dpy, root, netatom[NetClientList]);
2025 for(m = mons; m; m = m->next)
2026 for(c = m->clients; c; c = c->next)
2027 XChangeProperty(dpy, root, netatom[NetClientList],
2028 XA_WINDOW, 32, PropModeAppend,
2029 (unsigned char *) &(c->win), 1);
2037 if(XineramaIsActive(dpy)) {
2041 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2042 XineramaScreenInfo *unique = NULL;
2044 for(n = 0, m = mons; m; m = m->next, n++);
2045 /* only consider unique geometries as separate screens */
2046 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
2047 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
2048 for(i = 0, j = 0; i < nn; i++)
2049 if(isuniquegeom(unique, j, &info[i]))
2050 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2054 for(i = 0; i < (nn - n); i++) { /* new monitors available */
2055 for(m = mons; m && m->next; m = m->next);
2057 m->next = createmon();
2061 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
2063 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
2064 || unique[i].width != m->mw || unique[i].height != m->mh))
2068 m->mx = m->wx = unique[i].x_org;
2069 m->my = m->wy = unique[i].y_org;
2070 m->mw = m->ww = unique[i].width;
2071 m->mh = m->wh = unique[i].height;
2075 else { /* less monitors available nn < n */
2076 for(i = nn; i < n; i++) {
2077 for(m = mons; m && m->next; m = m->next);
2081 m->clients = c->next;
2095 #endif /* XINERAMA */
2096 /* default monitor setup */
2100 if(mons->mw != sw || mons->mh != sh) {
2102 mons->mw = mons->ww = sw;
2103 mons->mh = mons->wh = sh;
2109 selmon = wintomon(root);
2115 updatenumlockmask(void) {
2117 XModifierKeymap *modmap;
2120 modmap = XGetModifierMapping(dpy);
2121 for(i = 0; i < 8; i++)
2122 for(j = 0; j < modmap->max_keypermod; j++)
2123 if(modmap->modifiermap[i * modmap->max_keypermod + j]
2124 == XKeysymToKeycode(dpy, XK_Num_Lock))
2125 numlockmask = (1 << i);
2126 XFreeModifiermap(modmap);
2130 updatesizehints(Client *c) {
2134 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
2135 /* size is uninitialized, ensure that size.flags aren't used */
2137 if(size.flags & PBaseSize) {
2138 c->basew = size.base_width;
2139 c->baseh = size.base_height;
2141 else if(size.flags & PMinSize) {
2142 c->basew = size.min_width;
2143 c->baseh = size.min_height;
2146 c->basew = c->baseh = 0;
2147 if(size.flags & PResizeInc) {
2148 c->incw = size.width_inc;
2149 c->inch = size.height_inc;
2152 c->incw = c->inch = 0;
2153 if(size.flags & PMaxSize) {
2154 c->maxw = size.max_width;
2155 c->maxh = size.max_height;
2158 c->maxw = c->maxh = 0;
2159 if(size.flags & PMinSize) {
2160 c->minw = size.min_width;
2161 c->minh = size.min_height;
2163 else if(size.flags & PBaseSize) {
2164 c->minw = size.base_width;
2165 c->minh = size.base_height;
2168 c->minw = c->minh = 0;
2169 if(size.flags & PAspect) {
2170 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2171 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2174 c->maxa = c->mina = 0.0;
2175 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2176 && c->maxw == c->minw && c->maxh == c->minh);
2180 updatetitle(Client *c) {
2181 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2182 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2183 if(c->name[0] == '\0') /* hack to mark broken clients */
2184 strcpy(c->name, broken);
2188 updatestatus(void) {
2189 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2190 strcpy(stext, "dwm-"VERSION);
2195 updatewindowtype(Client *c) {
2196 Atom state = getatomprop(c, netatom[NetWMState]);
2197 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2199 if(state == netatom[NetWMFullscreen])
2200 setfullscreen(c, True);
2201 if(wtype == netatom[NetWMWindowTypeDialog])
2202 c->isfloating = True;
2206 updatewmhints(Client *c) {
2209 if((wmh = XGetWMHints(dpy, c->win))) {
2210 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2211 wmh->flags &= ~XUrgencyHint;
2212 XSetWMHints(dpy, c->win, wmh);
2215 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2216 if(wmh->flags & InputHint)
2217 c->neverfocus = !wmh->input;
2219 c->neverfocus = False;
2225 view(const Arg *arg) {
2226 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2228 selmon->seltags ^= 1; /* toggle sel tagset */
2229 if(arg->ui & TAGMASK)
2230 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2236 wintoclient(Window w) {
2240 for(m = mons; m; m = m->next)
2241 for(c = m->clients; c; c = c->next)
2248 wintomon(Window w) {
2253 if(w == root && getrootptr(&x, &y))
2254 return recttomon(x, y, 1, 1);
2255 for(m = mons; m; m = m->next)
2258 if((c = wintoclient(w)))
2263 /* There's no way to check accesses to destroyed windows, thus those cases are
2264 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2265 * default error handler, which may call exit. */
2267 xerror(Display *dpy, XErrorEvent *ee) {
2268 if(ee->error_code == BadWindow
2269 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2270 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2271 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2272 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2273 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2274 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2275 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2276 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2278 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2279 ee->request_code, ee->error_code);
2280 return xerrorxlib(dpy, ee); /* may call exit */
2284 xerrordummy(Display *dpy, XErrorEvent *ee) {
2288 /* Startup Error handler to check if another window manager
2289 * is already running. */
2291 xerrorstart(Display *dpy, XErrorEvent *ee) {
2292 die("dwm: another window manager is already running\n");
2297 zoom(const Arg *arg) {
2298 Client *c = selmon->sel;
2300 if(!selmon->lt[selmon->sellt]->arrange
2301 || (selmon->sel && selmon->sel->isfloating))
2303 if(c == nexttiled(selmon->clients))
2304 if(!c || !(c = nexttiled(c->next)))
2310 main(int argc, char *argv[]) {
2311 if(argc == 2 && !strcmp("-v", argv[1]))
2312 die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
2314 die("usage: dwm [-v]\n");
2315 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2316 fputs("warning: no locale support\n", stderr);
2317 if(!(dpy = XOpenDisplay(NULL)))
2318 die("dwm: cannot open display\n");
2325 return EXIT_SUCCESS;