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_NOPAD(X) drw_font_getexts_width(drw->font, X, strlen(X))
59 #define TEXTW(X) (TEXTW_NOPAD(X) + drw->font->h)
62 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
63 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
64 enum { NetSupported, NetWMName, NetWMState,
65 NetWMFullscreen, NetWMWindowOpacity, NetActiveWindow, NetWMWindowType,
66 NetWMWindowTypeDialog, NetClientList, NetSupportingWMCheck, NetLast }; /* EWMH atoms */
67 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
68 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
69 ClkClientWin, ClkRootWin, ClkAnywhere, ClkLast }; /* clicks */
82 void (*func)(const Arg *arg);
86 typedef struct Monitor Monitor;
87 typedef struct Client Client;
92 int oldx, oldy, oldw, oldh;
93 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
97 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, screen_hog;
107 void (*func)(const Arg *);
113 void (*arrange)(Monitor *);
121 int by; /* bar geometry */
122 int mx, my, mw, mh; /* screen size */
123 int wx, wy, ww, wh; /* window area */
124 unsigned int seltags;
126 unsigned int tagset[2];
139 const char *instance;
147 /* function declarations */
148 static void applyrules(Client *c);
149 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
150 static void arrange(Monitor *m);
151 static void arrangemon(Monitor *m);
152 static void attach_as_master(Client *c);
153 static void attach(Client *c);
154 static void attachstack(Client *c);
155 static void buttonpress(XEvent *e);
156 static void checkotherwm(void);
157 static void cleanup(void);
158 static void cleanupmon(Monitor *mon);
159 static void clearurgent(Client *c);
160 static void clientmessage(XEvent *e);
161 static void configure(Client *c);
162 static void configurenotify(XEvent *e);
163 static void configurerequest(XEvent *e);
164 static Monitor *createmon(void);
165 static void destroynotify(XEvent *e);
166 static void detach(Client *c);
167 static void detachstack(Client *c);
168 static Monitor *dirtomon(int dir);
169 static void drawbar(Monitor *m);
170 static void drawbars(void);
171 static void enternotify(XEvent *e);
172 static void expose(XEvent *e);
173 static void focus(Client *c);
174 static void focusin(XEvent *e);
175 static void focusmon(const Arg *arg);
176 static void focusstack(const Arg *arg);
177 static Bool getrootptr(int *x, int *y);
178 static long getstate(Window w);
179 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
180 static void grabbuttons(Client *c, Bool focused);
181 static void grabkeys(void);
182 static void incnmaster(const Arg *arg);
183 static void keypress(XEvent *e);
184 static void killclient(const Arg *arg);
185 static void manage(Window w, XWindowAttributes *wa);
186 static void mappingnotify(XEvent *e);
187 static void maprequest(XEvent *e);
188 static void monocle(Monitor *m);
189 static void motionnotify(XEvent *e);
190 static void movemouse(const Arg *arg);
191 static Client *nexttiled(Client *c);
192 static Client *snexttiled(Client *c);
193 static Client *nextvisible(Client *c);
194 static void pop(Client *);
195 static void propertynotify(XEvent *e);
196 static void quit(const Arg *arg);
197 static Monitor *recttomon(int x, int y, int w, int h);
198 static void resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base);
199 static void resizeclient(Client *c, int x, int y, int w, int h, Client *base);
200 static void resizemouse(const Arg *arg);
201 static void restack(Monitor *m);
202 static void run(void);
203 static void scan(void);
204 static Bool sendevent(Client *c, Atom proto);
205 static void sendmon(Client *c, Monitor *m);
206 static void setclientstate(Client *c, long state);
207 static void setfocus(Client *c);
208 static void setfullscreen(Client *c, Bool fullscreen);
209 static void setlayout(const Arg *arg);
210 static void setmfact(const Arg *arg);
211 static void setup(void);
212 static void showhide(Client *c);
213 static void sigchld(int unused);
214 static void kbspawn(const Arg *arg);
215 static void spawn(const Arg *arg);
216 static void tag(const Arg *arg);
217 static void tagmon(const Arg *arg);
218 static void jason_layout(Monitor *);
219 static void tile(Monitor *);
220 static void togglebar(const Arg *arg);
221 static void togglefloating(const Arg *arg);
222 static void toggletag(const Arg *arg);
223 static void toggleview(const Arg *arg);
224 static void unfocus(Client *c, Bool setfocus);
225 static void unmanage(Client *c, Bool destroyed);
226 static void unmapnotify(XEvent *e);
227 static Bool updategeom(void);
228 static void updatebarpos(Monitor *m);
229 static void updatebars(void);
230 static void updateclientlist(void);
231 static void updatenumlockmask(void);
232 static void updatesizehints(Client *c);
233 static void updatestatus(void);
234 static void updatewindowtype(Client *c);
235 static void updatetitle(Client *c);
236 static void updatewmhints(Client *c);
237 static void view(const Arg *arg);
238 static Client *wintoclient(Window w);
239 static Monitor *wintomon(Window w);
240 static int xerror(Display *dpy, XErrorEvent *ee);
241 static int xerrordummy(Display *dpy, XErrorEvent *ee);
242 static int xerrorstart(Display *dpy, XErrorEvent *ee);
243 static void zoom(const Arg *arg);
246 enum { KeyBufferingOff, KeyBufferingAwaitingWindow, KeyBufferingAwaitingFocus };
247 static unsigned int key_buffering = 0;
248 static const char broken[] = "broken";
249 static char stext[256];
251 static int sw, sh; // X display screen geometry width, height
252 static int bh; // bar height
253 static int (*xerrorxlib)(Display *, XErrorEvent *);
254 static unsigned int numlockmask = 0;
255 static void (*handler[LASTEvent]) (XEvent *) = {
256 [ButtonPress] = buttonpress,
257 [ClientMessage] = clientmessage,
258 [ConfigureRequest] = configurerequest,
259 [ConfigureNotify] = configurenotify,
260 [DestroyNotify] = destroynotify,
261 [EnterNotify] = enternotify,
264 [KeyPress] = keypress,
265 [MappingNotify] = mappingnotify,
266 [MapRequest] = maprequest,
267 [MotionNotify] = motionnotify,
268 [PropertyNotify] = propertynotify,
269 [UnmapNotify] = unmapnotify
271 static Atom wmatom[WMLast], netatom[NetLast];
272 static Bool running = True;
273 static Cur *cursor[CurLast];
274 static ClrScheme scheme[SchemeLast];
278 static Monitor *mons, *selmon;
281 // unfocused windows get transparent (feature)
282 static const unsigned long opacities[] = { 0, 0xbfffffff, 0x00000000 }; // first unused
283 static void window_set_opaque(Client *c);
284 static void window_set_translucent(Client *c);
285 static void window_set_invisible(Client *c);
286 static void window_set_opacity(Client *c, int opacity_index);
287 static Client* choose_slave (Client *master, Client *focused);
288 static void update_window_opacities(Monitor *m);
290 window_set_opacity(Client *c, int opacity_index) {
291 if (c->opacity == opacity_index) {
294 c->opacity = opacity_index;
295 if (opacity_index == 0) {
296 XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
298 XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)(&opacities[opacity_index]), 1);
302 window_set_opaque(Client *c) {
303 window_set_opacity(c, 0);
306 window_set_translucent(Client *c) {
307 window_set_opacity(c, 1);
310 window_set_invisible(Client *c) {
311 window_set_opacity(c, 2);
314 update_window_opacities(Monitor *m) {
315 Client *master, *slave, *focused, *c;
316 Bool focused_is_floating = False;
317 master = nexttiled(m->clients);
318 focused = (m->sel && ISVISIBLE(m->sel)) ? m->sel : NULL;
319 slave = choose_slave(master, focused);
320 // detect if the focused window is floating (and visible)
321 if (master && focused && focused != slave && focused != master) {
322 if (nexttiled(focused) != focused) {
323 focused_is_floating = True;
326 // set opacity of visible floaters, master and slave
327 for (c = nextvisible(m->clients); c; c = nextvisible(c->next)) {
328 if (c->isfloating || c == focused || (focused_is_floating && (c == master || c == slave))) {
329 window_set_opaque(c);
330 } else if (c == master || c == slave) {
331 window_set_translucent(c);
337 /* configuration, allows nested code to access above variables */
340 /* compile-time check if all tags fit into an unsigned int bit array. */
341 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
343 /* function implementations */
345 applyrules(Client *c) {
346 const char *class, *instance;
350 XClassHint ch = { NULL, NULL };
353 c->isfloating = c->tags = c->screen_hog = 0;
354 XGetClassHint(dpy, c->win, &ch);
355 class = ch.res_class ? ch.res_class : broken;
356 instance = ch.res_name ? ch.res_name : broken;
358 for(i = 0; i < LENGTH(rules); i++) {
360 if((!r->title || c->name == strstr(c->name, r->title))
361 && (!r->class || strstr(class, r->class))
362 && (!r->instance || strstr(instance, r->instance)))
364 c->isfloating = r->isfloating;
366 c->x = -1; c->y = -2; // secret code for centered
369 c->screen_hog = r->screen_hog;
370 for(m = mons; m && m->num != r->monitor; m = m->next);
379 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
383 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
385 // Monitor *m = c->mon;
387 /* set minimum possible */
395 if(*x + *w + 2 * c->bw < 0)
397 if(*y + *h + 2 * c->bw < 0)
400 // jason: let windows be offscreen
402 // if(*x >= m->wx + m->ww)
403 // *x = m->wx + m->ww - WIDTH(c);
404 // if(*y >= m->wy + m->wh)
405 // *y = m->wy + m->wh - HEIGHT(c);
406 // if(*x + *w + 2 * c->bw <= m->wx)
408 // if(*y + *h + 2 * c->bw <= m->wy)
415 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
416 /* see last two sentences in ICCCM 4.1.2.3 */
417 baseismin = c->basew == c->minw && c->baseh == c->minh;
418 if(!baseismin) { /* temporarily remove base dimensions */
422 /* adjust for aspect limits */
423 if(c->mina > 0 && c->maxa > 0) {
424 if(c->maxa < (float)*w / *h)
425 *w = *h * c->maxa + 0.5;
426 else if(c->mina < (float)*h / *w)
427 *h = *w * c->mina + 0.5;
429 if(baseismin) { /* increment calculation requires this */
433 /* adjust for increment value */
438 /* restore base dimensions */
439 *w = MAX(*w + c->basew, c->minw);
440 *h = MAX(*h + c->baseh, c->minh);
442 *w = MIN(*w, c->maxw);
444 *h = MIN(*h, c->maxh);
446 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
450 arrange(Monitor *m) {
453 else for(m = mons; m; m = m->next)
458 } else for(m = mons; m; m = m->next)
463 arrangemon(Monitor *m) {
464 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
465 if(m->lt[m->sellt]->arrange)
466 m->lt[m->sellt]->arrange(m);
470 attach_as_master(Client *c) {
471 c->next = c->mon->clients;
477 c->next = c->mon->sel->next;
478 c->mon->sel->next = c;
485 attachstack(Client *c) {
486 c->snext = c->mon->stack;
491 buttonpress(XEvent *e) {
492 unsigned int i, click;
496 XButtonPressedEvent *ev = &e->xbutton;
499 for(i = 0; i < LENGTH(buttons); i++) {
500 if(buttons[i].click == ClkAnywhere && buttons[i].button == ev->button
501 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
502 buttons[i].func(&buttons[i].arg);
511 /* focus monitor if necessary */
512 if((m = wintomon(ev->window)) && m != selmon) {
513 unfocus(selmon->sel, True);
517 if(ev->window == selmon->barwin) {
519 } else if((c = wintoclient(ev->window))) {
521 click = ClkClientWin;
523 for(i = 0; i < LENGTH(buttons); i++) {
524 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
525 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
526 if (click == ClkTagBar && buttons[i].arg.i == 0) {
527 buttons[i].func(&arg);
529 buttons[i].func(&buttons[i].arg);
537 xerrorxlib = XSetErrorHandler(xerrorstart);
538 /* this causes an error if some other window manager is running */
539 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
541 XSetErrorHandler(xerror);
548 Layout foo = { "", NULL };
552 selmon->lt[selmon->sellt] = &foo;
553 for(m = mons; m; m = m->next)
555 unmanage(m->stack, False);
556 XUngrabKey(dpy, AnyKey, AnyModifier, root);
559 drw_cur_free(drw, cursor[CurNormal]);
560 drw_cur_free(drw, cursor[CurResize]);
561 drw_cur_free(drw, cursor[CurMove]);
562 drw_font_free(dpy, fnt);
563 drw_clr_free(scheme[SchemeNorm].border);
564 drw_clr_free(scheme[SchemeNorm].bg);
565 drw_clr_free(scheme[SchemeNorm].fg);
566 drw_clr_free(scheme[SchemeSel].border);
567 drw_clr_free(scheme[SchemeSel].bg);
568 drw_clr_free(scheme[SchemeSel].fg);
571 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
572 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
576 cleanupmon(Monitor *mon) {
582 for(m = mons; m && m->next != mon; m = m->next);
585 XUnmapWindow(dpy, mon->barwin);
586 XDestroyWindow(dpy, mon->barwin);
591 clearurgent(Client *c) {
595 if(!(wmh = XGetWMHints(dpy, c->win)))
597 wmh->flags &= ~XUrgencyHint;
598 XSetWMHints(dpy, c->win, wmh);
603 clientmessage(XEvent *e) {
604 XClientMessageEvent *cme = &e->xclient;
605 Client *c = wintoclient(cme->window);
609 if(cme->message_type == netatom[NetWMState]) {
610 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
611 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
612 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
614 else if(cme->message_type == netatom[NetActiveWindow]) {
615 // Jason added this so apps can't steal focus:
618 c->mon->seltags ^= 1;
619 c->mon->tagset[c->mon->seltags] = c->tags;
626 configure(Client *c) {
629 ce.type = ConfigureNotify;
637 ce.border_width = c->bw;
639 ce.override_redirect = False;
640 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
644 configurenotify(XEvent *e) {
646 XConfigureEvent *ev = &e->xconfigure;
649 // TODO: updategeom handling sucks, needs to be simplified
650 if(ev->window == root) {
651 dirty = (sw != ev->width || sh != ev->height);
654 if(updategeom() || dirty) {
655 drw_resize(drw, sw, bh);
657 for(m = mons; m; m = m->next)
658 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
666 configurerequest(XEvent *e) {
669 XConfigureRequestEvent *ev = &e->xconfigurerequest;
672 if((c = wintoclient(ev->window))) {
673 if(ev->value_mask & CWBorderWidth)
674 c->bw = ev->border_width;
675 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
677 if(ev->value_mask & CWX) {
679 c->x = m->mx + ev->x;
681 if(ev->value_mask & CWY) {
683 c->y = m->my + ev->y;
685 if(ev->value_mask & CWWidth) {
689 if(ev->value_mask & CWHeight) {
693 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
694 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
695 if((c->y + c->h) > m->my + m->mh && c->isfloating)
696 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
697 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
700 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
708 wc.width = ev->width;
709 wc.height = ev->height;
710 wc.border_width = ev->border_width;
711 wc.sibling = ev->above;
712 wc.stack_mode = ev->detail;
713 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
722 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
723 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
724 m->tagset[0] = m->tagset[1] = 1;
726 m->nmaster = nmaster;
727 m->showbar = showbar;
729 m->lt[0] = &layouts[0];
730 m->lt[1] = &layouts[1 % LENGTH(layouts)];
731 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
736 destroynotify(XEvent *e) {
738 XDestroyWindowEvent *ev = &e->xdestroywindow;
740 if((c = wintoclient(ev->window)))
748 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
752 // NOTE: the stack is for z-order and most-recently-focused
753 // only mon->clients determines position in visible layout
755 detachstack(Client *c) {
756 Client *prev = NULL, *next_sel = NULL, *i;
757 for(i = c->mon->stack; i && i != c; i = i->snext) {
760 if(c == c->mon->sel) {
761 // find last visible window before c
762 // WARNING if you detach() before detachstack() this will select last visible window
763 for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
765 // failing that, find first visible window (besides c)
767 for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
771 c->mon->sel = next_sel;
774 prev->snext = c->snext;
776 c->mon->stack = c->snext;
785 if(!(m = selmon->next))
788 else if(selmon == mons)
789 for(m = mons; m->next; m = m->next);
791 for(m = mons; m->next != selmon; m = m->next);
796 drawbar(Monitor *m) {
798 unsigned int i, occ = 0, urg = 0;
801 for(c = m->clients; c; c = c->next) {
807 for(i = 0; i < LENGTH(tags); i++) {
808 w = TEXTW_NOPAD(tags[i]);
809 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
810 drw_text_nopad(drw, x, 0, w, bh, tags[i], urg & 1 << i);
814 if(m == selmon) { /* status is only drawn on selected monitor */
821 drw_text(drw, x, 0, w, bh, stext, 0);
825 if((w = x - xx) > bh) {
828 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
829 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
830 drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
833 drw_setscheme(drw, &scheme[SchemeNorm]);
834 drw_text(drw, x, 0, w, bh, NULL, 0);
837 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
844 for(m = mons; m; m = m->next)
849 enternotify(XEvent *e) {
852 XCrossingEvent *ev = &e->xcrossing;
854 return; // jason: added to stop mouse focus
856 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
858 c = wintoclient(ev->window);
859 m = c ? c->mon : wintomon(ev->window);
861 unfocus(selmon->sel, True);
864 else if(!c || c == selmon->sel)
872 XExposeEvent *ev = &e->xexpose;
874 if(ev->count == 0 && (m = wintomon(ev->window)))
880 if(!c || !ISVISIBLE(c))
881 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
882 /* was if(selmon->sel) */
883 if(selmon->sel && selmon->sel != c)
884 unfocus(selmon->sel, False);
892 grabbuttons(c, True);
893 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
896 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
897 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
901 update_window_opacities(selmon);
903 if(c && (!root || (c->win!=root)) )
904 window_set_opaque(c);
908 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
909 XFocusChangeEvent *ev = &e->xfocus;
911 if(selmon->sel && ev->window != selmon->sel->win)
912 setfocus(selmon->sel);
914 if(key_buffering == KeyBufferingAwaitingFocus) {
915 key_buffering = KeyBufferingOff;
916 // there's a few ways to release the grab, with different effects (below)
917 // In this example F2 is mapped to kbspawn
918 // XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
919 // swallows key presses while F2 is still down.
920 // XAllowEvents(dpy, ReplayKeyboard, CurrentTime);
921 // sends the F2 keydown and all the queued events
922 // XUngrabKeyboard(dpy, CurrentTime);
923 // sends everything queued and the F2 key_UP_
924 XUngrabKeyboard(dpy, CurrentTime);
929 focusmon(const Arg *arg) {
934 if((m = dirtomon(arg->i)) == selmon)
936 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
937 in gedit and anjuta */
943 focusstack(const Arg *arg) {
944 Client *c = NULL, *i;
949 for(i = selmon->clients; i != selmon->sel; i = i->next) {
955 } else if(arg->i > 0) {
956 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
958 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
961 for(i = selmon->clients; i != selmon->sel; i = i->next)
965 for(; i; i = i->next)
976 getatomprop(Client *c, Atom prop) {
979 unsigned char *p = NULL;
980 Atom da, atom = None;
982 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
983 &da, &di, &dl, &dl, &p) == Success && p) {
991 getrootptr(int *x, int *y) {
996 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
1000 getstate(Window w) {
1003 unsigned char *p = NULL;
1004 unsigned long n, extra;
1007 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
1008 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
1017 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
1022 if(!text || size == 0)
1025 XGetTextProperty(dpy, w, &name, atom);
1028 if(name.encoding == XA_STRING)
1029 strncpy(text, (char *)name.value, size - 1);
1031 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1032 strncpy(text, *list, size - 1);
1033 XFreeStringList(list);
1036 text[size - 1] = '\0';
1042 grabbuttons(Client *c, Bool focused) {
1043 updatenumlockmask();
1046 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1047 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1049 for(i = 0; i < LENGTH(buttons); i++)
1050 if(buttons[i].click == ClkClientWin || buttons[i].click == ClkAnywhere)
1051 for(j = 0; j < LENGTH(modifiers); j++)
1052 XGrabButton(dpy, buttons[i].button,
1053 buttons[i].mask | modifiers[j],
1054 c->win, False, BUTTONMASK,
1055 GrabModeAsync, GrabModeSync, None, None);
1057 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1058 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1065 updatenumlockmask();
1066 //XUngrabKey(dpy, AnyKey, AnyModifier, root);
1069 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1072 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1073 for(i = 0; i < LENGTH(keys); i++)
1074 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1075 for(j = 0; j < LENGTH(modifiers); j++)
1076 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1077 False, // report keys to focused window too?
1078 GrabModeAsync, // mouse grab mode
1079 keys[i].func == kbspawn ? GrabModeSync : GrabModeAsync);
1084 incnmaster(const Arg *arg) {
1085 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1091 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1093 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1094 && unique[n].width == info->width && unique[n].height == info->height)
1098 #endif /* XINERAMA */
1101 keypress(XEvent *e) {
1107 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1108 for(i = 0; i < LENGTH(keys); i++) {
1109 if(keysym == keys[i].keysym
1110 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1112 keys[i].func(&(keys[i].arg));
1118 killclient(const Arg *arg) {
1121 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1123 XSetErrorHandler(xerrordummy);
1124 XSetCloseDownMode(dpy, DestroyAll);
1125 XKillClient(dpy, selmon->sel->win);
1127 XSetErrorHandler(xerror);
1133 manage(Window w, XWindowAttributes *wa) {
1134 Client *c, *t = NULL;
1135 Window trans = None;
1138 if(!(c = calloc(1, sizeof(Client))))
1139 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1140 c->opacity = -1; // who knows
1143 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1152 if(c->x == -1 && c->y == -2) { // secret code for centered
1153 c->x = c->oldx = (c->mon->ww - wa->width) / 2;
1154 c->y = c->oldy = (c->mon->wh - wa->height) / 2;
1156 c->x = c->oldx = wa->x;
1157 c->y = c->oldy = wa->y;
1159 c->w = c->oldw = wa->width;
1160 c->h = c->oldh = wa->height;
1161 c->oldbw = wa->border_width;
1163 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1164 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1165 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1166 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1167 c->x = MAX(c->x, c->mon->mx);
1168 /* only fix client y-offset, if the client center might cover the bar */
1169 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1170 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1173 wc.border_width = c->bw;
1174 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1175 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
1176 configure(c); /* propagates border_width, if size doesn't change */
1177 updatewindowtype(c);
1180 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1181 grabbuttons(c, False);
1183 c->isfloating = c->oldstate = trans != None || c->isfixed;
1185 XRaiseWindow(dpy, c->win);
1188 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1189 (unsigned char *) &(c->win), 1);
1190 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1191 setclientstate(c, NormalState);
1192 if (c->mon == selmon)
1193 unfocus(selmon->sel, False);
1196 XMapWindow(dpy, c->win);
1198 if(key_buffering == KeyBufferingAwaitingWindow) {
1199 // almost there! but wait until we are notified that it has focus
1200 key_buffering = KeyBufferingAwaitingFocus;
1205 mappingnotify(XEvent *e) {
1206 XMappingEvent *ev = &e->xmapping;
1207 // fprintf(stderr, "MapNotify\n");
1209 XRefreshKeyboardMapping(ev);
1210 if(ev->request == MappingKeyboard)
1215 maprequest(XEvent *e) {
1216 static XWindowAttributes wa;
1217 XMapRequestEvent *ev = &e->xmaprequest;
1219 // fprintf(stderr, "MapRequest\n");
1220 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1222 if(wa.override_redirect) {
1225 if(!wintoclient(ev->window))
1226 manage(ev->window, &wa);
1230 monocle(Monitor *m) {
1234 for(c = m->clients; c; c = c->next)
1237 if(n > 0) /* override layout symbol */
1238 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1239 for(c = snexttiled(m->stack); c; c = snexttiled(c->snext)) {
1241 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False, 0);
1243 // this window is should not be visible. move off top, but don't change h/w
1244 resize(c, m->wx, m->wy - 4000, c->w, c->h, False, 0);
1250 motionnotify(XEvent *e) {
1251 static Monitor *mon = NULL;
1253 XMotionEvent *ev = &e->xmotion;
1255 if(ev->window != root)
1257 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1258 unfocus(selmon->sel, True);
1266 movemouse(const Arg *arg) {
1267 int x, y, ocx, ocy, nx, ny;
1273 if(!(c = selmon->sel))
1275 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1280 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1281 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1283 if(!getrootptr(&x, &y))
1286 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1288 case ConfigureRequest:
1291 handler[ev.type](&ev);
1294 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1296 lasttime = ev.xmotion.time;
1298 nx = ocx + (ev.xmotion.x - x);
1299 ny = ocy + (ev.xmotion.y - y);
1300 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1301 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1302 if(abs(selmon->wx - nx) < snap)
1304 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1305 nx = selmon->wx + selmon->ww - WIDTH(c);
1306 if(abs(selmon->wy - ny) < snap)
1308 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1309 ny = selmon->wy + selmon->wh - HEIGHT(c);
1310 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1311 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1312 togglefloating(NULL);
1314 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1315 resize(c, nx, ny, c->w, c->h, True, 0);
1318 } while(ev.type != ButtonRelease);
1319 XUngrabPointer(dpy, CurrentTime);
1320 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1328 nexttiled(Client *c) {
1329 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1334 snexttiled(Client *c) {
1335 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->snext);
1340 nextvisible(Client *c) {
1341 for(; c && !ISVISIBLE(c); c = c->next);
1348 attach_as_master(c);
1354 propertynotify(XEvent *e) {
1357 XPropertyEvent *ev = &e->xproperty;
1359 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1361 else if(ev->state == PropertyDelete)
1362 return; /* ignore */
1363 else if((c = wintoclient(ev->window))) {
1366 case XA_WM_TRANSIENT_FOR:
1367 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1368 (c->isfloating = (wintoclient(trans)) != NULL))
1371 case XA_WM_NORMAL_HINTS:
1379 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1381 if(c == c->mon->sel)
1384 if(ev->atom == netatom[NetWMWindowType])
1385 updatewindowtype(c);
1390 quit(const Arg *arg) {
1395 recttomon(int x, int y, int w, int h) {
1396 Monitor *m, *r = selmon;
1399 for(m = mons; m; m = m->next)
1400 if((a = INTERSECT(x, y, w, h, m)) > area) {
1408 resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base) {
1409 if(applysizehints(c, &x, &y, &w, &h, interact))
1410 resizeclient(c, x, y, w, h, base);
1414 resizeclient(Client *c, int x, int y, int w, int h, Client *base) {
1416 unsigned long mask = CWX|CWY|CWWidth|CWHeight|CWBorderWidth;
1418 c->oldx = c->x; c->x = wc.x = x;
1419 c->oldy = c->y; c->y = wc.y = y;
1420 c->oldw = c->w; c->w = wc.width = w;
1421 c->oldh = c->h; c->h = wc.height = h;
1424 wc.stack_mode = Above;
1425 wc.sibling = base->win;
1426 mask |= CWStackMode|CWSibling;
1428 wc.border_width = c->bw;
1429 XConfigureWindow(dpy, c->win, mask, &wc);
1435 resizemouse(const Arg *arg) {
1436 int ocx, ocy, nw, nh;
1442 if(!(c = selmon->sel))
1444 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1449 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1450 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1452 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1454 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1456 case ConfigureRequest:
1459 handler[ev.type](&ev);
1462 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1464 lasttime = ev.xmotion.time;
1466 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1467 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1468 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1469 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1471 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1472 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1473 togglefloating(NULL);
1475 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1476 resize(c, c->x, c->y, nw, nh, True, 0);
1479 } while(ev.type != ButtonRelease);
1480 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1481 XUngrabPointer(dpy, CurrentTime);
1482 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1483 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1491 restack(Monitor *m) {
1499 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1500 XRaiseWindow(dpy, m->sel->win);
1501 if(m->lt[m->sellt]->arrange) {
1502 wc.stack_mode = Below;
1503 wc.sibling = m->barwin;
1504 for(c = m->clients; c; c = c->next)
1505 if(!c->isfloating && ISVISIBLE(c)) {
1506 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1507 wc.sibling = c->win;
1508 wc.stack_mode = Above;
1512 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1518 /* main event loop */
1520 while(running && !XNextEvent(dpy, &ev)) {
1521 if(handler[ev.type]) {
1522 // fprintf(stderr, "handling event type %i\n", ev.type);
1523 handler[ev.type](&ev); /* call handler */
1525 // fprintf(stderr, "evt type %i\n", ev.type);
1532 unsigned int i, num;
1533 Window d1, d2, *wins = NULL;
1534 XWindowAttributes wa;
1536 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1537 for(i = 0; i < num; i++) {
1538 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1539 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1541 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1542 manage(wins[i], &wa);
1544 for(i = 0; i < num; i++) { /* now the transients */
1545 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1547 if(XGetTransientForHint(dpy, wins[i], &d1)
1548 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1549 manage(wins[i], &wa);
1557 sendmon(Client *c, Monitor *m) {
1564 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1572 setclientstate(Client *c, long state) {
1573 long data[] = { state, None };
1575 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1576 PropModeReplace, (unsigned char *)data, 2);
1580 sendevent(Client *c, Atom proto) {
1583 Bool exists = False;
1586 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1587 while(!exists && n--)
1588 exists = protocols[n] == proto;
1592 ev.type = ClientMessage;
1593 ev.xclient.window = c->win;
1594 ev.xclient.message_type = wmatom[WMProtocols];
1595 ev.xclient.format = 32;
1596 ev.xclient.data.l[0] = proto;
1597 ev.xclient.data.l[1] = CurrentTime;
1598 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1604 setfocus(Client *c) {
1605 if(!c->neverfocus) {
1606 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1607 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1608 XA_WINDOW, 32, PropModeReplace,
1609 (unsigned char *) &(c->win), 1);
1611 sendevent(c, wmatom[WMTakeFocus]);
1615 setfullscreen(Client *c, Bool fullscreen) {
1617 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1618 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1619 c->isfullscreen = True;
1620 c->oldstate = c->isfloating;
1623 c->isfloating = True;
1624 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh, 0);
1625 XRaiseWindow(dpy, c->win);
1628 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1629 PropModeReplace, (unsigned char*)0, 0);
1630 c->isfullscreen = False;
1631 c->isfloating = c->oldstate;
1637 resizeclient(c, c->x, c->y, c->w, c->h, 0);
1643 setlayout(const Arg *arg) {
1644 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1647 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1648 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1655 /* arg > 1.0 will set mfact absolutly */
1657 setmfact(const Arg *arg) {
1660 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1662 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1663 if(f < 0.1 || f > 0.9)
1671 XSetWindowAttributes wa;
1673 /* clean up any zombies immediately */
1677 screen = DefaultScreen(dpy);
1678 root = RootWindow(dpy, screen);
1679 fnt = drw_font_create(dpy, font);
1680 sw = DisplayWidth(dpy, screen);
1681 sh = DisplayHeight(dpy, screen);
1683 drw = drw_create(dpy, screen, root, sw, sh);
1684 drw_setfont(drw, fnt);
1687 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1688 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1689 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1690 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1691 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1692 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1693 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1694 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1695 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1696 netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
1697 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1698 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1699 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1700 netatom[NetSupportingWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1701 XInternAtom(dpy, "_MOTIF_WM_HINTS", False); /* clients may request borderless/fullscreen */
1703 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1704 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1705 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1706 /* init appearance */
1707 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1708 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1709 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1710 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1711 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1712 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1716 /* EWMH support per view */
1717 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1718 PropModeReplace, (unsigned char *) netatom, NetLast);
1719 XDeleteProperty(dpy, root, netatom[NetClientList]);
1720 /* select for events */
1721 wa.cursor = cursor[CurNormal]->cursor;
1722 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1723 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1724 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1725 XSelectInput(dpy, root, wa.event_mask);
1731 showhide(Client *c) {
1734 if(ISVISIBLE(c)) { /* show clients top down */
1735 XMoveWindow(dpy, c->win, c->x, c->y);
1736 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1737 resize(c, c->x, c->y, c->w, c->h, False, 0);
1740 else { /* hide clients bottom up */
1742 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1747 sigchld(int unused) {
1748 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1749 die("Can't install SIGCHLD handler");
1750 while(0 < waitpid(-1, NULL, WNOHANG));
1754 kbspawn(const Arg *arg) {
1755 key_buffering = True;
1760 spawn(const Arg *arg) {
1762 if(arg->v == termcmd || arg->v == belltermcmd || arg->v == runcmd) {
1763 for(i = 0; i < 32; ++i) {
1764 if(selmon->tagset[selmon->seltags] & (1 << i)) {
1769 WORKSPACE_NUMBER[17] = workspace_numbers_str[tag][0];
1770 WORKSPACE_NUMBER[18] = workspace_numbers_str[tag][1]; // 2nd digit or null
1772 if(arg->v == dmenucmd)
1773 dmenumon[0] = '0' + selmon->num;
1776 close(ConnectionNumber(dpy));
1778 execvp(((char **)arg->v)[0], (char **)arg->v);
1779 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1786 tag(const Arg *arg) {
1787 if(selmon->sel && arg->ui & TAGMASK) {
1788 selmon->sel->tags = arg->ui & TAGMASK;
1795 tagmon(const Arg *arg) {
1796 if(!selmon->sel || !mons->next)
1798 sendmon(selmon->sel, dirtomon(arg->i));
1803 unsigned int i, n, h, mw, my, ty;
1806 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1811 mw = m->nmaster ? m->ww * m->mfact : 0;
1813 c = nexttiled(m->clients);
1814 if (c && !c->screen_hog)
1815 mw = m->ww * m->mfact;
1819 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1820 if(i < m->nmaster) {
1821 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1822 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False, 0);
1826 h = (m->wh - ty) / (n - i);
1827 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False, 0);
1832 // search forward from start, return the last client before end
1834 prev_tiled (Client *start, Client *end) {
1836 for (w = start; w && (n = nexttiled(w->next)); w = n) {
1844 // return the window to show on the right side
1846 choose_slave (Client *master, Client *focused) {
1851 // FIXME put this same algorithm in update_window_opacities
1852 if (focused->isfloating) {
1853 // show the window just "under" it
1854 Client *prev = prev_tiled(master, focused);
1855 // fall back to the first slave
1856 if (prev && prev != master) {
1859 return nexttiled(master->next);
1862 // focused window is tiled
1863 if (focused == master) {
1864 // master is focused, show first slave
1865 return nexttiled(master->next);
1867 // focused window is a slave, so show that one
1872 // maybe we get called when there's no focused?
1873 return nexttiled(master->next);
1879 jason_layout(Monitor *m) {
1880 Client *c, *master, *slave, *focused, *base = 0;
1881 unsigned int master_width, slave_width, slave_left;
1884 master = nexttiled(m->clients);
1886 // no master? nothing to do
1891 // find focused and slave
1892 focused = (m->sel && ISVISIBLE(m->sel)) ? m->sel : NULL;
1893 slave = choose_slave(master, focused);
1895 // calculate window widths
1896 if (!slave && master->screen_hog) {
1897 master_width = m->ww;
1899 master_width = m->ww * m->mfact - GUTTER_PX / 2;
1901 slave_left = m->ww * m->mfact + GUTTER_PX / 2;
1902 slave_width = m->ww * (1 - m->mfact) - GUTTER_PX / 2;
1904 // resize/reposition master
1913 // resize/reposition slaves
1914 base = master; // window to be above in the stacking order
1915 for (c = nexttiled(master->next); c; c = nexttiled(c->next)) {
1918 c == slave ? m->wy : m->wy - m->wh,
1929 togglebar(const Arg *arg) {
1930 selmon->showbar = !selmon->showbar;
1931 updatebarpos(selmon);
1932 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1937 togglefloating(const Arg *arg) {
1940 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1942 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1943 if(selmon->sel->isfloating)
1944 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1945 selmon->sel->w, selmon->sel->h, False, 0);
1950 toggletag(const Arg *arg) {
1951 unsigned int newtags;
1955 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1957 selmon->sel->tags = newtags;
1964 toggleview(const Arg *arg) {
1965 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1968 selmon->tagset[selmon->seltags] = newtagset;
1975 unfocus(Client *c, Bool setfocus) {
1978 grabbuttons(c, False);
1979 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
1981 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1982 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1987 unmanage(Client *c, Bool destroyed) {
1988 Monitor *m = c->mon;
1991 /* The server grab construct avoids race conditions. */
1995 wc.border_width = c->oldbw;
1997 XSetErrorHandler(xerrordummy);
1998 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1999 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
2000 setclientstate(c, WithdrawnState);
2002 XSetErrorHandler(xerror);
2006 focus(selmon ? selmon->sel : NULL);
2012 unmapnotify(XEvent *e) {
2014 XUnmapEvent *ev = &e->xunmap;
2016 if((c = wintoclient(ev->window))) {
2018 setclientstate(c, WithdrawnState);
2027 XSetWindowAttributes wa = {
2028 .override_redirect = True,
2029 .background_pixmap = ParentRelative,
2030 .event_mask = ButtonPressMask|ExposureMask
2032 for(m = mons; m; m = m->next) {
2035 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
2036 CopyFromParent, DefaultVisual(dpy, screen),
2037 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
2038 XChangeProperty(dpy, root, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
2039 PropModeReplace, (unsigned char *) &(m->barwin), 1);
2040 XChangeProperty(dpy, m->barwin, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
2041 PropModeReplace, (unsigned char *) &(m->barwin), 1);
2042 XChangeProperty(dpy, m->barwin, netatom[NetWMName], XA_STRING, 8,
2043 PropModeReplace, (unsigned char *) "dwm", 3);
2044 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2045 XMapRaised(dpy, m->barwin);
2050 updatebarpos(Monitor *m) {
2055 m->by = m->topbar ? m->wy : m->wy + m->wh;
2056 m->wy = m->topbar ? m->wy + bh : m->wy;
2063 updateclientlist() {
2067 XDeleteProperty(dpy, root, netatom[NetClientList]);
2068 for(m = mons; m; m = m->next)
2069 for(c = m->clients; c; c = c->next)
2070 XChangeProperty(dpy, root, netatom[NetClientList],
2071 XA_WINDOW, 32, PropModeAppend,
2072 (unsigned char *) &(c->win), 1);
2080 if(XineramaIsActive(dpy)) {
2084 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2085 XineramaScreenInfo *unique = NULL;
2087 for(n = 0, m = mons; m; m = m->next, n++);
2088 /* only consider unique geometries as separate screens */
2089 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
2090 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
2091 for(i = 0, j = 0; i < nn; i++)
2092 if(isuniquegeom(unique, j, &info[i]))
2093 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2097 for(i = 0; i < (nn - n); i++) { /* new monitors available */
2098 for(m = mons; m && m->next; m = m->next);
2100 m->next = createmon();
2104 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
2106 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
2107 || unique[i].width != m->mw || unique[i].height != m->mh))
2111 m->mx = m->wx = unique[i].x_org;
2112 m->my = m->wy = unique[i].y_org;
2113 m->mw = m->ww = unique[i].width;
2114 m->mh = m->wh = unique[i].height;
2118 else { /* less monitors available nn < n */
2119 for(i = nn; i < n; i++) {
2120 for(m = mons; m && m->next; m = m->next);
2124 m->clients = c->next;
2138 #endif /* XINERAMA */
2139 /* default monitor setup */
2143 if(mons->mw != sw || mons->mh != sh) {
2145 mons->mw = mons->ww = sw;
2146 mons->mh = mons->wh = sh;
2152 selmon = wintomon(root);
2158 updatenumlockmask(void) {
2160 XModifierKeymap *modmap;
2163 modmap = XGetModifierMapping(dpy);
2164 for(i = 0; i < 8; i++)
2165 for(j = 0; j < modmap->max_keypermod; j++)
2166 if(modmap->modifiermap[i * modmap->max_keypermod + j]
2167 == XKeysymToKeycode(dpy, XK_Num_Lock))
2168 numlockmask = (1 << i);
2169 XFreeModifiermap(modmap);
2173 updatesizehints(Client *c) {
2177 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
2178 /* size is uninitialized, ensure that size.flags aren't used */
2180 if(size.flags & PBaseSize) {
2181 c->basew = size.base_width;
2182 c->baseh = size.base_height;
2184 else if(size.flags & PMinSize) {
2185 c->basew = size.min_width;
2186 c->baseh = size.min_height;
2189 c->basew = c->baseh = 0;
2190 if(size.flags & PResizeInc) {
2191 c->incw = size.width_inc;
2192 c->inch = size.height_inc;
2195 c->incw = c->inch = 0;
2196 if(size.flags & PMaxSize) {
2197 c->maxw = size.max_width;
2198 c->maxh = size.max_height;
2201 c->maxw = c->maxh = 0;
2202 if(size.flags & PMinSize) {
2203 c->minw = size.min_width;
2204 c->minh = size.min_height;
2206 else if(size.flags & PBaseSize) {
2207 c->minw = size.base_width;
2208 c->minh = size.base_height;
2211 c->minw = c->minh = 0;
2212 if(size.flags & PAspect) {
2213 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2214 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2217 c->maxa = c->mina = 0.0;
2218 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2219 && c->maxw == c->minw && c->maxh == c->minh);
2223 updatetitle(Client *c) {
2224 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2225 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2226 if(c->name[0] == '\0') /* hack to mark broken clients */
2227 strcpy(c->name, broken);
2231 updatestatus(void) {
2232 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2233 strcpy(stext, "dwm-"VERSION);
2238 updatewindowtype(Client *c) {
2239 Atom state = getatomprop(c, netatom[NetWMState]);
2240 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2242 if(state == netatom[NetWMFullscreen])
2243 setfullscreen(c, True);
2244 if(wtype == netatom[NetWMWindowTypeDialog])
2245 c->isfloating = True;
2249 updatewmhints(Client *c) {
2252 if((wmh = XGetWMHints(dpy, c->win))) {
2253 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2254 wmh->flags &= ~XUrgencyHint;
2255 XSetWMHints(dpy, c->win, wmh);
2258 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2259 if(wmh->flags & InputHint)
2260 c->neverfocus = !wmh->input;
2262 c->neverfocus = False;
2268 view(const Arg *arg) {
2269 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2271 selmon->seltags ^= 1; /* toggle sel tagset */
2272 if(arg->ui & TAGMASK)
2273 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2279 wintoclient(Window w) {
2283 for(m = mons; m; m = m->next)
2284 for(c = m->clients; c; c = c->next)
2291 wintomon(Window w) {
2296 if(w == root && getrootptr(&x, &y))
2297 return recttomon(x, y, 1, 1);
2298 for(m = mons; m; m = m->next)
2301 if((c = wintoclient(w)))
2306 /* There's no way to check accesses to destroyed windows, thus those cases are
2307 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2308 * default error handler, which may call exit. */
2310 xerror(Display *dpy, XErrorEvent *ee) {
2311 if(ee->error_code == BadWindow
2312 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2313 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2314 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2315 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2316 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2317 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2318 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2319 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2321 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2322 ee->request_code, ee->error_code);
2323 return xerrorxlib(dpy, ee); /* may call exit */
2327 xerrordummy(Display *dpy, XErrorEvent *ee) {
2331 /* Startup Error handler to check if another window manager
2332 * is already running. */
2334 xerrorstart(Display *dpy, XErrorEvent *ee) {
2335 die("dwm: another window manager is already running\n");
2340 zoom(const Arg *arg) {
2341 Client *c = selmon->sel;
2343 if(!selmon->lt[selmon->sellt]->arrange
2344 || (selmon->sel && selmon->sel->isfloating))
2346 if(c == nexttiled(selmon->clients))
2347 if(!c || !(c = nexttiled(c->next)))
2353 main(int argc, char *argv[]) {
2354 if(argc == 2 && !strcmp("-v", argv[1]))
2355 die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
2357 die("usage: dwm [-v]\n");
2358 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2359 fputs("warning: no locale support\n", stderr);
2360 if(!(dpy = XOpenDisplay(NULL)))
2361 die("dwm: cannot open display\n");
2368 return EXIT_SUCCESS;