1 /* See LICENSE file for copyright and license details.
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
31 #include <sys/types.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
47 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
49 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
51 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
52 #define LENGTH(X) (sizeof X / sizeof X[0])
53 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
54 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
56 #define TAGMASK ((1 << LENGTH(tags)) - 1)
57 #define TEXTW(X) (drw_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
60 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
61 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
62 enum { NetSupported, NetWMName, NetWMState,
63 NetWMFullscreen, NetWMWindowOpacity, NetActiveWindow, NetWMWindowType,
64 NetWMWindowTypeDialog, NetClientList, NetSupportingWMCheck, NetLast }; /* EWMH atoms */
65 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
66 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
67 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
80 void (*func)(const Arg *arg);
84 typedef struct Monitor Monitor;
85 typedef struct Client Client;
90 int oldx, oldy, oldw, oldh;
91 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
95 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, screen_hog;
105 void (*func)(const Arg *);
111 void (*arrange)(Monitor *);
119 int by; /* bar geometry */
120 int mx, my, mw, mh; /* screen size */
121 int wx, wy, ww, wh; /* window area */
122 unsigned int seltags;
124 unsigned int tagset[2];
137 const char *instance;
145 /* function declarations */
146 static void applyrules(Client *c);
147 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
148 static void arrange(Monitor *m);
149 static void arrangemon(Monitor *m);
150 static void attach_as_master(Client *c);
151 static void attach(Client *c);
152 static void attachstack(Client *c);
153 static void buttonpress(XEvent *e);
154 static void checkotherwm(void);
155 static void cleanup(void);
156 static void cleanupmon(Monitor *mon);
157 static void clearurgent(Client *c);
158 static void clientmessage(XEvent *e);
159 static void configure(Client *c);
160 static void configurenotify(XEvent *e);
161 static void configurerequest(XEvent *e);
162 static Monitor *createmon(void);
163 static void destroynotify(XEvent *e);
164 static void detach(Client *c);
165 static void detachstack(Client *c);
166 static Monitor *dirtomon(int dir);
167 static void drawbar(Monitor *m);
168 static void drawbars(void);
169 static void enternotify(XEvent *e);
170 static void expose(XEvent *e);
171 static void focus(Client *c);
172 static void focusin(XEvent *e);
173 static void focusmon(const Arg *arg);
174 static void focusstack(const Arg *arg);
175 static Bool getrootptr(int *x, int *y);
176 static long getstate(Window w);
177 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
178 static void grabbuttons(Client *c, Bool focused);
179 static void grabkeys(void);
180 static void incnmaster(const Arg *arg);
181 static void keypress(XEvent *e);
182 static void killclient(const Arg *arg);
183 static void manage(Window w, XWindowAttributes *wa);
184 static void mappingnotify(XEvent *e);
185 static void maprequest(XEvent *e);
186 static void monocle(Monitor *m);
187 static void motionnotify(XEvent *e);
188 static void movemouse(const Arg *arg);
189 static Client *nexttiled(Client *c);
190 static Client *snexttiled(Client *c);
191 static Client *nextvisible(Client *c);
192 static void pop(Client *);
193 static void propertynotify(XEvent *e);
194 static void quit(const Arg *arg);
195 static Monitor *recttomon(int x, int y, int w, int h);
196 static void resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base);
197 static void resizeclient(Client *c, int x, int y, int w, int h, Client *base);
198 static void resizemouse(const Arg *arg);
199 static void restack(Monitor *m);
200 static void run(void);
201 static void scan(void);
202 static Bool sendevent(Client *c, Atom proto);
203 static void send_keycode(KeyCode key, unsigned int state);
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);
249 static BufferedKey key_buffer[200];
250 static int key_buffer_len = 0;
251 static Bool key_buffering = False;
252 static const char broken[] = "broken";
253 static char stext[256];
255 static int sw, sh; /* X display screen geometry width, height */
256 static int bh, blw = 0; /* bar geometry */
257 static int (*xerrorxlib)(Display *, XErrorEvent *);
258 static unsigned int numlockmask = 0;
259 static void (*handler[LASTEvent]) (XEvent *) = {
260 [ButtonPress] = buttonpress,
261 [ClientMessage] = clientmessage,
262 [ConfigureRequest] = configurerequest,
263 [ConfigureNotify] = configurenotify,
264 [DestroyNotify] = destroynotify,
265 [EnterNotify] = enternotify,
268 [KeyPress] = keypress,
269 [MappingNotify] = mappingnotify,
270 [MapRequest] = maprequest,
271 [MotionNotify] = motionnotify,
272 [PropertyNotify] = propertynotify,
273 [UnmapNotify] = unmapnotify
275 static Atom wmatom[WMLast], netatom[NetLast];
276 static Bool running = True;
277 static Cur *cursor[CurLast];
278 static ClrScheme scheme[SchemeLast];
282 static Monitor *mons, *selmon;
285 // unfocused windows get transparent (feature)
286 static const unsigned long opacities[] = { 0, 0xbfffffff, 0x00000000 }; // first unused
287 static void window_set_opaque(Client *c);
288 static void window_set_translucent(Client *c);
289 static void window_set_invisible(Client *c);
290 static void window_set_opacity(Client *c, int opacity_index);
291 static void update_window_opacities(Monitor *m);
293 window_set_opacity(Client *c, int opacity_index) {
294 if (c->opacity == opacity_index) {
297 c->opacity = opacity_index;
298 if (opacity_index == 0) {
299 XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
301 XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)(&opacities[opacity_index]), 1);
305 window_set_opaque(Client *c) {
306 window_set_opacity(c, 0);
309 window_set_translucent(Client *c) {
310 window_set_opacity(c, 1);
313 window_set_invisible(Client *c) {
314 window_set_opacity(c, 2);
317 update_window_opacities(Monitor *m) {
318 Client *master, *slave, *c;
319 Bool selection_floating = False;
320 slave = master = nexttiled(m->clients);
321 if (master) slave = nexttiled(master->next);
322 if (m->sel && m->sel != master) {
323 if (nexttiled(m->sel) == m->sel) // if selection is tiled
326 selection_floating = True;
328 for (c = m->clients; c; c = c->next) {
330 if (c->isfloating || c == m->sel || (selection_floating && (c == master || c == slave))) {
331 window_set_opaque(c);
332 } else if (c == master || c == slave) {
333 window_set_translucent(c);
335 window_set_opaque(c);
342 /* configuration, allows nested code to access above variables */
345 /* compile-time check if all tags fit into an unsigned int bit array. */
346 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
348 /* function implementations */
350 applyrules(Client *c) {
351 const char *class, *instance;
355 XClassHint ch = { NULL, NULL };
358 c->isfloating = c->tags = c->screen_hog = 0;
359 XGetClassHint(dpy, c->win, &ch);
360 class = ch.res_class ? ch.res_class : broken;
361 instance = ch.res_name ? ch.res_name : broken;
363 for(i = 0; i < LENGTH(rules); i++) {
365 if((!r->title || c->name == strstr(c->name, r->title))
366 && (!r->class || strstr(class, r->class))
367 && (!r->instance || strstr(instance, r->instance)))
369 c->isfloating = r->isfloating;
371 c->x = -1; c->y = -2; // secret code for centered
374 c->screen_hog = r->screen_hog;
375 for(m = mons; m && m->num != r->monitor; m = m->next);
384 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
388 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
390 // Monitor *m = c->mon;
392 /* set minimum possible */
400 if(*x + *w + 2 * c->bw < 0)
402 if(*y + *h + 2 * c->bw < 0)
405 // jason: let windows be offscreen
407 // if(*x >= m->wx + m->ww)
408 // *x = m->wx + m->ww - WIDTH(c);
409 // if(*y >= m->wy + m->wh)
410 // *y = m->wy + m->wh - HEIGHT(c);
411 // if(*x + *w + 2 * c->bw <= m->wx)
413 // if(*y + *h + 2 * c->bw <= m->wy)
420 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
421 /* see last two sentences in ICCCM 4.1.2.3 */
422 baseismin = c->basew == c->minw && c->baseh == c->minh;
423 if(!baseismin) { /* temporarily remove base dimensions */
427 /* adjust for aspect limits */
428 if(c->mina > 0 && c->maxa > 0) {
429 if(c->maxa < (float)*w / *h)
430 *w = *h * c->maxa + 0.5;
431 else if(c->mina < (float)*h / *w)
432 *h = *w * c->mina + 0.5;
434 if(baseismin) { /* increment calculation requires this */
438 /* adjust for increment value */
443 /* restore base dimensions */
444 *w = MAX(*w + c->basew, c->minw);
445 *h = MAX(*h + c->baseh, c->minh);
447 *w = MIN(*w, c->maxw);
449 *h = MIN(*h, c->maxh);
451 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
455 arrange(Monitor *m) {
458 else for(m = mons; m; m = m->next)
463 } else for(m = mons; m; m = m->next)
468 arrangemon(Monitor *m) {
469 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
470 if(m->lt[m->sellt]->arrange)
471 m->lt[m->sellt]->arrange(m);
475 attach_as_master(Client *c) {
476 c->next = c->mon->clients;
482 c->next = c->mon->sel->next;
483 c->mon->sel->next = c;
490 attachstack(Client *c) {
491 c->snext = c->mon->stack;
496 buttonpress(XEvent *e) {
497 unsigned int i, click;
501 XButtonPressedEvent *ev = &e->xbutton;
504 /* focus monitor if necessary */
505 if((m = wintomon(ev->window)) && m != selmon) {
506 unfocus(selmon->sel, True);
510 if(ev->window == selmon->barwin) {
513 else if((c = wintoclient(ev->window))) {
515 click = ClkClientWin;
517 for(i = 0; i < LENGTH(buttons); i++)
518 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
519 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
520 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
525 xerrorxlib = XSetErrorHandler(xerrorstart);
526 /* this causes an error if some other window manager is running */
527 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
529 XSetErrorHandler(xerror);
536 Layout foo = { "", NULL };
540 selmon->lt[selmon->sellt] = &foo;
541 for(m = mons; m; m = m->next)
543 unmanage(m->stack, False);
544 XUngrabKey(dpy, AnyKey, AnyModifier, root);
547 drw_cur_free(drw, cursor[CurNormal]);
548 drw_cur_free(drw, cursor[CurResize]);
549 drw_cur_free(drw, cursor[CurMove]);
550 drw_font_free(dpy, fnt);
551 drw_clr_free(scheme[SchemeNorm].border);
552 drw_clr_free(scheme[SchemeNorm].bg);
553 drw_clr_free(scheme[SchemeNorm].fg);
554 drw_clr_free(scheme[SchemeSel].border);
555 drw_clr_free(scheme[SchemeSel].bg);
556 drw_clr_free(scheme[SchemeSel].fg);
559 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
560 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
564 cleanupmon(Monitor *mon) {
570 for(m = mons; m && m->next != mon; m = m->next);
573 XUnmapWindow(dpy, mon->barwin);
574 XDestroyWindow(dpy, mon->barwin);
579 clearurgent(Client *c) {
583 if(!(wmh = XGetWMHints(dpy, c->win)))
585 wmh->flags &= ~XUrgencyHint;
586 XSetWMHints(dpy, c->win, wmh);
591 clientmessage(XEvent *e) {
592 XClientMessageEvent *cme = &e->xclient;
593 Client *c = wintoclient(cme->window);
597 if(cme->message_type == netatom[NetWMState]) {
598 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
599 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
600 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
602 else if(cme->message_type == netatom[NetActiveWindow]) {
603 // Jason added this so apps can't steal focus:
606 c->mon->seltags ^= 1;
607 c->mon->tagset[c->mon->seltags] = c->tags;
614 configure(Client *c) {
617 ce.type = ConfigureNotify;
625 ce.border_width = c->bw;
627 ce.override_redirect = False;
628 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
632 configurenotify(XEvent *e) {
634 XConfigureEvent *ev = &e->xconfigure;
637 // TODO: updategeom handling sucks, needs to be simplified
638 if(ev->window == root) {
639 dirty = (sw != ev->width || sh != ev->height);
642 if(updategeom() || dirty) {
643 drw_resize(drw, sw, bh);
645 for(m = mons; m; m = m->next)
646 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
654 configurerequest(XEvent *e) {
657 XConfigureRequestEvent *ev = &e->xconfigurerequest;
660 if((c = wintoclient(ev->window))) {
661 if(ev->value_mask & CWBorderWidth)
662 c->bw = ev->border_width;
663 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
665 if(ev->value_mask & CWX) {
667 c->x = m->mx + ev->x;
669 if(ev->value_mask & CWY) {
671 c->y = m->my + ev->y;
673 if(ev->value_mask & CWWidth) {
677 if(ev->value_mask & CWHeight) {
681 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
682 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
683 if((c->y + c->h) > m->my + m->mh && c->isfloating)
684 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
685 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
688 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
696 wc.width = ev->width;
697 wc.height = ev->height;
698 wc.border_width = ev->border_width;
699 wc.sibling = ev->above;
700 wc.stack_mode = ev->detail;
701 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
710 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
711 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
712 m->tagset[0] = m->tagset[1] = 1;
714 m->nmaster = nmaster;
715 m->showbar = showbar;
717 m->lt[0] = &layouts[0];
718 m->lt[1] = &layouts[1 % LENGTH(layouts)];
719 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
724 destroynotify(XEvent *e) {
726 XDestroyWindowEvent *ev = &e->xdestroywindow;
728 if((c = wintoclient(ev->window)))
736 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
740 // NOTE: the stack is for z-order and most-recently-focused
741 // only mon->clients determines position in visible layout
743 detachstack(Client *c) {
744 Client *prev = NULL, *next_sel = NULL, *i;
745 for(i = c->mon->stack; i && i != c; i = i->snext) {
748 if(c == c->mon->sel) {
749 // find last visible window before c
750 // WARNING if you detach() before detachstack() this will select last visible window
751 for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
753 // failing that, find first visible window (besides c)
755 for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
759 c->mon->sel = next_sel;
762 prev->snext = c->snext;
764 c->mon->stack = c->snext;
773 if(!(m = selmon->next))
776 else if(selmon == mons)
777 for(m = mons; m->next; m = m->next);
779 for(m = mons; m->next != selmon; m = m->next);
784 drawbar(Monitor *m) {
786 unsigned int i, occ = 0, urg = 0;
789 for(c = m->clients; c; c = c->next) {
795 for(i = 0; i < LENGTH(tags); i++) {
797 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
798 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
799 drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
800 occ & 1 << i, urg & 1 << i);
803 w = blw = TEXTW(m->ltsymbol);
804 drw_setscheme(drw, &scheme[SchemeNorm]);
805 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
808 if(m == selmon) { /* status is only drawn on selected monitor */
815 drw_text(drw, x, 0, w, bh, stext, 0);
819 if((w = x - xx) > bh) {
822 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
823 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
824 drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
827 drw_setscheme(drw, &scheme[SchemeNorm]);
828 drw_text(drw, x, 0, w, bh, NULL, 0);
831 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
838 for(m = mons; m; m = m->next)
843 enternotify(XEvent *e) {
846 XCrossingEvent *ev = &e->xcrossing;
848 return; // jason: added to stop mouse focus
850 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
852 c = wintoclient(ev->window);
853 m = c ? c->mon : wintomon(ev->window);
855 unfocus(selmon->sel, True);
858 else if(!c || c == selmon->sel)
866 XExposeEvent *ev = &e->xexpose;
868 if(ev->count == 0 && (m = wintomon(ev->window)))
874 if(!c || !ISVISIBLE(c))
875 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
876 /* was if(selmon->sel) */
877 if(selmon->sel && selmon->sel != c)
878 unfocus(selmon->sel, False);
886 grabbuttons(c, True);
887 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
891 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
892 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
896 update_window_opacities(selmon);
898 if(c && (!root || (c->win!=root)) )
899 window_set_opaque(c);
903 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
904 XFocusChangeEvent *ev = &e->xfocus;
906 if(selmon->sel && ev->window != selmon->sel->win)
907 setfocus(selmon->sel);
911 focusmon(const Arg *arg) {
916 if((m = dirtomon(arg->i)) == selmon)
918 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
919 in gedit and anjuta */
925 focusstack(const Arg *arg) {
926 Client *c = NULL, *i;
931 for(i = selmon->clients; i != selmon->sel; i = i->next) {
937 } else if(arg->i > 0) {
938 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
940 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
943 for(i = selmon->clients; i != selmon->sel; i = i->next)
947 for(; i; i = i->next)
958 getatomprop(Client *c, Atom prop) {
961 unsigned char *p = NULL;
962 Atom da, atom = None;
964 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
965 &da, &di, &dl, &dl, &p) == Success && p) {
973 getrootptr(int *x, int *y) {
978 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
985 unsigned char *p = NULL;
986 unsigned long n, extra;
989 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
990 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
999 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
1004 if(!text || size == 0)
1007 XGetTextProperty(dpy, w, &name, atom);
1010 if(name.encoding == XA_STRING)
1011 strncpy(text, (char *)name.value, size - 1);
1013 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1014 strncpy(text, *list, size - 1);
1015 XFreeStringList(list);
1018 text[size - 1] = '\0';
1024 grabbuttons(Client *c, Bool focused) {
1025 updatenumlockmask();
1028 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1029 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1031 for(i = 0; i < LENGTH(buttons); i++)
1032 if(buttons[i].click == ClkClientWin)
1033 for(j = 0; j < LENGTH(modifiers); j++)
1034 XGrabButton(dpy, buttons[i].button,
1035 buttons[i].mask | modifiers[j],
1036 c->win, False, BUTTONMASK,
1037 GrabModeAsync, GrabModeSync, None, None);
1040 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1041 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1047 updatenumlockmask();
1048 //XUngrabKey(dpy, AnyKey, AnyModifier, root);
1049 XGrabKey(dpy, AnyKey, AnyModifier, root, True, GrabModeAsync, GrabModeAsync);
1053 incnmaster(const Arg *arg) {
1054 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1060 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1062 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1063 && unique[n].width == info->width && unique[n].height == info->height)
1067 #endif /* XINERAMA */
1070 send_keycode(KeyCode key, unsigned int state) {
1075 event.display = dpy;
1077 event.window = selmon->sel->win;
1078 event.subwindow = None;
1079 event.same_screen = True;
1084 event.time = CurrentTime;
1085 event.state = state; // modifiers
1086 event.keycode = key;
1087 event.type = KeyPress;
1088 XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
1089 event.type = KeyRelease;
1090 XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
1094 keypress(XEvent *e) {
1098 Bool called = False;
1101 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1102 for(i = 0; i < LENGTH(keys); i++)
1103 if(keysym == keys[i].keysym
1104 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1106 keys[i].func(&(keys[i].arg));
1111 if(key_buffer_len == LENGTH(key_buffer)) {
1112 // buffer full, bail
1114 key_buffering = False;
1116 key_buffer[key_buffer_len].keycode = (KeyCode)ev->keycode;
1117 key_buffer[key_buffer_len].state = (KeyCode)ev->state;
1118 key_buffer_len += 1;
1121 send_keycode(ev->keycode, ev->state);
1127 killclient(const Arg *arg) {
1130 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1132 XSetErrorHandler(xerrordummy);
1133 XSetCloseDownMode(dpy, DestroyAll);
1134 XKillClient(dpy, selmon->sel->win);
1136 XSetErrorHandler(xerror);
1142 manage(Window w, XWindowAttributes *wa) {
1143 Client *c, *t = NULL;
1144 Window trans = None;
1148 if(!(c = calloc(1, sizeof(Client))))
1149 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1150 c->opacity = -1; // who knows
1153 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1162 if(c->x == -1 && c->y == -2) { // secret code for centered
1163 c->x = c->oldx = (c->mon->ww - wa->width) / 2;
1164 c->y = c->oldy = (c->mon->wh - wa->height) / 2;
1166 c->x = c->oldx = wa->x;
1167 c->y = c->oldy = wa->y;
1169 c->w = c->oldw = wa->width;
1170 c->h = c->oldh = wa->height;
1171 c->oldbw = wa->border_width;
1173 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1174 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1175 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1176 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1177 c->x = MAX(c->x, c->mon->mx);
1178 /* only fix client y-offset, if the client center might cover the bar */
1179 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1180 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1183 wc.border_width = c->bw;
1184 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1185 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
1186 configure(c); /* propagates border_width, if size doesn't change */
1187 updatewindowtype(c);
1190 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1191 grabbuttons(c, False);
1193 c->isfloating = c->oldstate = trans != None || c->isfixed;
1195 XRaiseWindow(dpy, c->win);
1198 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1199 (unsigned char *) &(c->win), 1);
1200 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1201 setclientstate(c, NormalState);
1202 if (c->mon == selmon)
1203 unfocus(selmon->sel, False);
1206 XMapWindow(dpy, c->win);
1209 if(key_buffer_len > 0) {
1210 for(i = 0; i < key_buffer_len; ++i) {
1211 send_keycode(key_buffer[i].keycode, key_buffer[i].state);
1215 key_buffering = False;
1220 mappingnotify(XEvent *e) {
1221 XMappingEvent *ev = &e->xmapping;
1223 XRefreshKeyboardMapping(ev);
1224 if(ev->request == MappingKeyboard)
1229 maprequest(XEvent *e) {
1230 static XWindowAttributes wa;
1231 XMapRequestEvent *ev = &e->xmaprequest;
1233 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1235 if(wa.override_redirect) {
1237 key_buffering = False;
1240 if(!wintoclient(ev->window))
1241 manage(ev->window, &wa);
1245 monocle(Monitor *m) {
1249 for(c = m->clients; c; c = c->next)
1252 if(n > 0) /* override layout symbol */
1253 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1254 for(c = snexttiled(m->stack); c; c = snexttiled(c->snext)) {
1256 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False, 0);
1258 // this window is should not be visible. move off top, but don't change h/w
1259 resize(c, m->wx, m->wy - 4000, c->w, c->h, False, 0);
1265 motionnotify(XEvent *e) {
1266 static Monitor *mon = NULL;
1268 XMotionEvent *ev = &e->xmotion;
1270 if(ev->window != root)
1272 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1273 unfocus(selmon->sel, True);
1281 movemouse(const Arg *arg) {
1282 int x, y, ocx, ocy, nx, ny;
1288 if(!(c = selmon->sel))
1290 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1295 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1296 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1298 if(!getrootptr(&x, &y))
1301 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1303 case ConfigureRequest:
1306 handler[ev.type](&ev);
1309 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1311 lasttime = ev.xmotion.time;
1313 nx = ocx + (ev.xmotion.x - x);
1314 ny = ocy + (ev.xmotion.y - y);
1315 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1316 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1317 if(abs(selmon->wx - nx) < snap)
1319 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1320 nx = selmon->wx + selmon->ww - WIDTH(c);
1321 if(abs(selmon->wy - ny) < snap)
1323 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1324 ny = selmon->wy + selmon->wh - HEIGHT(c);
1325 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1326 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1327 togglefloating(NULL);
1329 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1330 resize(c, nx, ny, c->w, c->h, True, 0);
1333 } while(ev.type != ButtonRelease);
1334 XUngrabPointer(dpy, CurrentTime);
1335 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1343 nexttiled(Client *c) {
1344 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1349 snexttiled(Client *c) {
1350 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->snext);
1355 nextvisible(Client *c) {
1356 for(; c && !ISVISIBLE(c); c = c->next);
1363 attach_as_master(c);
1369 propertynotify(XEvent *e) {
1372 XPropertyEvent *ev = &e->xproperty;
1374 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1376 else if(ev->state == PropertyDelete)
1377 return; /* ignore */
1378 else if((c = wintoclient(ev->window))) {
1381 case XA_WM_TRANSIENT_FOR:
1382 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1383 (c->isfloating = (wintoclient(trans)) != NULL))
1386 case XA_WM_NORMAL_HINTS:
1394 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1396 if(c == c->mon->sel)
1399 if(ev->atom == netatom[NetWMWindowType])
1400 updatewindowtype(c);
1405 quit(const Arg *arg) {
1410 recttomon(int x, int y, int w, int h) {
1411 Monitor *m, *r = selmon;
1414 for(m = mons; m; m = m->next)
1415 if((a = INTERSECT(x, y, w, h, m)) > area) {
1423 resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base) {
1424 if(applysizehints(c, &x, &y, &w, &h, interact))
1425 resizeclient(c, x, y, w, h, base);
1429 resizeclient(Client *c, int x, int y, int w, int h, Client *base) {
1431 unsigned long mask = CWX|CWY|CWWidth|CWHeight|CWBorderWidth;
1433 c->oldx = c->x; c->x = wc.x = x;
1434 c->oldy = c->y; c->y = wc.y = y;
1435 c->oldw = c->w; c->w = wc.width = w;
1436 c->oldh = c->h; c->h = wc.height = h;
1439 wc.stack_mode = Above;
1440 wc.sibling = base->win;
1441 mask |= CWStackMode|CWSibling;
1443 wc.border_width = c->bw;
1444 XConfigureWindow(dpy, c->win, mask, &wc);
1450 resizemouse(const Arg *arg) {
1451 int ocx, ocy, nw, nh;
1457 if(!(c = selmon->sel))
1459 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1464 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1465 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1467 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1469 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1471 case ConfigureRequest:
1474 handler[ev.type](&ev);
1477 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1479 lasttime = ev.xmotion.time;
1481 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1482 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1483 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1484 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1486 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1487 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1488 togglefloating(NULL);
1490 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1491 resize(c, c->x, c->y, nw, nh, True, 0);
1494 } while(ev.type != ButtonRelease);
1495 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1496 XUngrabPointer(dpy, CurrentTime);
1497 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1498 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1506 restack(Monitor *m) {
1514 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1515 XRaiseWindow(dpy, m->sel->win);
1516 if(m->lt[m->sellt]->arrange) {
1517 wc.stack_mode = Below;
1518 wc.sibling = m->barwin;
1519 for(c = m->clients; c; c = c->next)
1520 if(!c->isfloating && ISVISIBLE(c)) {
1521 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1522 wc.sibling = c->win;
1523 wc.stack_mode = Above;
1527 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1533 /* main event loop */
1535 while(running && !XNextEvent(dpy, &ev))
1536 if(handler[ev.type])
1537 handler[ev.type](&ev); /* call handler */
1542 unsigned int i, num;
1543 Window d1, d2, *wins = NULL;
1544 XWindowAttributes wa;
1546 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1547 for(i = 0; i < num; i++) {
1548 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1549 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1551 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1552 manage(wins[i], &wa);
1554 for(i = 0; i < num; i++) { /* now the transients */
1555 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1557 if(XGetTransientForHint(dpy, wins[i], &d1)
1558 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1559 manage(wins[i], &wa);
1567 sendmon(Client *c, Monitor *m) {
1574 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1582 setclientstate(Client *c, long state) {
1583 long data[] = { state, None };
1585 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1586 PropModeReplace, (unsigned char *)data, 2);
1590 sendevent(Client *c, Atom proto) {
1593 Bool exists = False;
1596 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1597 while(!exists && n--)
1598 exists = protocols[n] == proto;
1602 ev.type = ClientMessage;
1603 ev.xclient.window = c->win;
1604 ev.xclient.message_type = wmatom[WMProtocols];
1605 ev.xclient.format = 32;
1606 ev.xclient.data.l[0] = proto;
1607 ev.xclient.data.l[1] = CurrentTime;
1608 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1614 setfocus(Client *c) {
1615 if(!c->neverfocus) {
1616 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1617 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1618 XA_WINDOW, 32, PropModeReplace,
1619 (unsigned char *) &(c->win), 1);
1621 sendevent(c, wmatom[WMTakeFocus]);
1625 setfullscreen(Client *c, Bool fullscreen) {
1627 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1628 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1629 c->isfullscreen = True;
1630 c->oldstate = c->isfloating;
1633 c->isfloating = True;
1634 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh, 0);
1635 XRaiseWindow(dpy, c->win);
1638 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1639 PropModeReplace, (unsigned char*)0, 0);
1640 c->isfullscreen = False;
1641 c->isfloating = c->oldstate;
1647 resizeclient(c, c->x, c->y, c->w, c->h, 0);
1653 setlayout(const Arg *arg) {
1654 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1657 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1658 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1665 /* arg > 1.0 will set mfact absolutly */
1667 setmfact(const Arg *arg) {
1670 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1672 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1673 if(f < 0.1 || f > 0.9)
1681 XSetWindowAttributes wa;
1683 /* clean up any zombies immediately */
1687 screen = DefaultScreen(dpy);
1688 root = RootWindow(dpy, screen);
1689 fnt = drw_font_create(dpy, font);
1690 sw = DisplayWidth(dpy, screen);
1691 sh = DisplayHeight(dpy, screen);
1693 drw = drw_create(dpy, screen, root, sw, sh);
1694 drw_setfont(drw, fnt);
1697 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1698 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1699 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1700 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1701 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1702 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1703 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1704 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1705 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1706 netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
1707 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1708 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1709 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1710 netatom[NetSupportingWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1711 XInternAtom(dpy, "_MOTIF_WM_HINTS", False); /* clients may request borderless/fullscreen */
1713 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1714 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1715 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1716 /* init appearance */
1717 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1718 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1719 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1720 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1721 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1722 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1726 /* EWMH support per view */
1727 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1728 PropModeReplace, (unsigned char *) netatom, NetLast);
1729 XDeleteProperty(dpy, root, netatom[NetClientList]);
1730 /* select for events */
1731 wa.cursor = cursor[CurNormal]->cursor;
1732 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1733 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1734 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1735 XSelectInput(dpy, root, wa.event_mask);
1741 showhide(Client *c) {
1744 if(ISVISIBLE(c)) { /* show clients top down */
1745 XMoveWindow(dpy, c->win, c->x, c->y);
1746 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1747 resize(c, c->x, c->y, c->w, c->h, False, 0);
1750 else { /* hide clients bottom up */
1752 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1757 sigchld(int unused) {
1758 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1759 die("Can't install SIGCHLD handler");
1760 while(0 < waitpid(-1, NULL, WNOHANG));
1764 kbspawn(const Arg *arg) {
1765 key_buffering = True;
1771 spawn(const Arg *arg) {
1773 if(arg->v == termcmd) {
1774 for(i = 0; i < 32; ++i) {
1775 if(selmon->tagset[selmon->seltags] & (1 << i)) {
1780 WORKSPACE_NUMBER[17] = workspace_numbers_str[tag][0];
1781 WORKSPACE_NUMBER[18] = workspace_numbers_str[tag][1];
1783 if(arg->v == dmenucmd)
1784 dmenumon[0] = '0' + selmon->num;
1787 close(ConnectionNumber(dpy));
1789 execvp(((char **)arg->v)[0], (char **)arg->v);
1790 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1797 tag(const Arg *arg) {
1798 if(selmon->sel && arg->ui & TAGMASK) {
1799 selmon->sel->tags = arg->ui & TAGMASK;
1806 tagmon(const Arg *arg) {
1807 if(!selmon->sel || !mons->next)
1809 sendmon(selmon->sel, dirtomon(arg->i));
1814 unsigned int i, n, h, mw, my, ty;
1817 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1822 mw = m->nmaster ? m->ww * m->mfact : 0;
1824 c = nexttiled(m->clients);
1825 if (c && !c->screen_hog)
1826 mw = m->ww * m->mfact;
1830 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1831 if(i < m->nmaster) {
1832 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1833 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False, 0);
1837 h = (m->wh - ty) / (n - i);
1838 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False, 0);
1843 #define TAB_HEIGHT 19
1847 jason_layout(Monitor *m) {
1848 unsigned int i, tiled_count, mw, right_width, tab_counts[2] = {0,0}, cur_tab = 0, *tab_count;
1850 Client *c, *vis_slave = 0, *base = 0;
1852 tab_count = &(tab_counts[0]);
1854 for(tiled_count = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), tiled_count++) {
1855 if (tiled_count == 0) { // master
1857 if (m->sel && (m->sel == c || m->sel->isfloating || !ISVISIBLE(m->sel))) {
1858 vis_slave = nexttiled(c->next);
1864 if (c == vis_slave) {
1865 tab_count = &(tab_counts[1]);
1871 if(tiled_count == 0) {
1875 if(tiled_count > 1 || (tiled_count == 1 && !nexttiled(m->clients)->screen_hog)) {
1876 mw = m->ww * m->mfact;
1880 right_width = m->ww - mw;
1881 tab_count = &(tab_counts[0]);
1882 tab_top = m->wy - (m->wh - (2 * (TAB_HEIGHT + TAB_PAD))) + TAB_HEIGHT;
1883 for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
1885 resize(c, m->wx, m->wy, mw, m->wh, False, 0);
1887 if (c == vis_slave) {
1888 resize(c, m->wx + mw, m->wy + TAB_HEIGHT + TAB_PAD, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
1889 tab_count = &(tab_counts[1]);
1890 tab_top = m->wy + m->wh - TAB_HEIGHT;
1893 // this function does not get called when focus changes
1894 // resize(c, m->wx + m->ww, m->wy, m->ww - mw, m->wh, False);
1895 resize(c, m->wx + mw + right_width * cur_tab / (*tab_count), tab_top, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
1904 togglebar(const Arg *arg) {
1905 selmon->showbar = !selmon->showbar;
1906 updatebarpos(selmon);
1907 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1912 togglefloating(const Arg *arg) {
1915 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1917 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1918 if(selmon->sel->isfloating)
1919 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1920 selmon->sel->w, selmon->sel->h, False, 0);
1925 toggletag(const Arg *arg) {
1926 unsigned int newtags;
1930 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1932 selmon->sel->tags = newtags;
1939 toggleview(const Arg *arg) {
1940 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1943 selmon->tagset[selmon->seltags] = newtagset;
1950 unfocus(Client *c, Bool setfocus) {
1953 grabbuttons(c, False);
1954 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
1956 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1957 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1962 unmanage(Client *c, Bool destroyed) {
1963 Monitor *m = c->mon;
1966 /* The server grab construct avoids race conditions. */
1970 wc.border_width = c->oldbw;
1972 XSetErrorHandler(xerrordummy);
1973 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1974 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1975 setclientstate(c, WithdrawnState);
1977 XSetErrorHandler(xerror);
1981 focus(selmon ? selmon->sel : NULL);
1987 unmapnotify(XEvent *e) {
1989 XUnmapEvent *ev = &e->xunmap;
1991 if((c = wintoclient(ev->window))) {
1993 setclientstate(c, WithdrawnState);
2002 XSetWindowAttributes wa = {
2003 .override_redirect = True,
2004 .background_pixmap = ParentRelative,
2005 .event_mask = ButtonPressMask|ExposureMask
2007 for(m = mons; m; m = m->next) {
2010 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
2011 CopyFromParent, DefaultVisual(dpy, screen),
2012 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
2013 XChangeProperty(dpy, root, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
2014 PropModeReplace, (unsigned char *) &(m->barwin), 1);
2015 XChangeProperty(dpy, m->barwin, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
2016 PropModeReplace, (unsigned char *) &(m->barwin), 1);
2017 XChangeProperty(dpy, m->barwin, netatom[NetWMName], XA_STRING, 8,
2018 PropModeReplace, (unsigned char *) "dwm", 3);
2019 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2020 XMapRaised(dpy, m->barwin);
2025 updatebarpos(Monitor *m) {
2030 m->by = m->topbar ? m->wy : m->wy + m->wh;
2031 m->wy = m->topbar ? m->wy + bh : m->wy;
2038 updateclientlist() {
2042 XDeleteProperty(dpy, root, netatom[NetClientList]);
2043 for(m = mons; m; m = m->next)
2044 for(c = m->clients; c; c = c->next)
2045 XChangeProperty(dpy, root, netatom[NetClientList],
2046 XA_WINDOW, 32, PropModeAppend,
2047 (unsigned char *) &(c->win), 1);
2055 if(XineramaIsActive(dpy)) {
2059 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2060 XineramaScreenInfo *unique = NULL;
2062 for(n = 0, m = mons; m; m = m->next, n++);
2063 /* only consider unique geometries as separate screens */
2064 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
2065 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
2066 for(i = 0, j = 0; i < nn; i++)
2067 if(isuniquegeom(unique, j, &info[i]))
2068 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2072 for(i = 0; i < (nn - n); i++) { /* new monitors available */
2073 for(m = mons; m && m->next; m = m->next);
2075 m->next = createmon();
2079 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
2081 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
2082 || unique[i].width != m->mw || unique[i].height != m->mh))
2086 m->mx = m->wx = unique[i].x_org;
2087 m->my = m->wy = unique[i].y_org;
2088 m->mw = m->ww = unique[i].width;
2089 m->mh = m->wh = unique[i].height;
2093 else { /* less monitors available nn < n */
2094 for(i = nn; i < n; i++) {
2095 for(m = mons; m && m->next; m = m->next);
2099 m->clients = c->next;
2113 #endif /* XINERAMA */
2114 /* default monitor setup */
2118 if(mons->mw != sw || mons->mh != sh) {
2120 mons->mw = mons->ww = sw;
2121 mons->mh = mons->wh = sh;
2127 selmon = wintomon(root);
2133 updatenumlockmask(void) {
2135 XModifierKeymap *modmap;
2138 modmap = XGetModifierMapping(dpy);
2139 for(i = 0; i < 8; i++)
2140 for(j = 0; j < modmap->max_keypermod; j++)
2141 if(modmap->modifiermap[i * modmap->max_keypermod + j]
2142 == XKeysymToKeycode(dpy, XK_Num_Lock))
2143 numlockmask = (1 << i);
2144 XFreeModifiermap(modmap);
2148 updatesizehints(Client *c) {
2152 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
2153 /* size is uninitialized, ensure that size.flags aren't used */
2155 if(size.flags & PBaseSize) {
2156 c->basew = size.base_width;
2157 c->baseh = size.base_height;
2159 else if(size.flags & PMinSize) {
2160 c->basew = size.min_width;
2161 c->baseh = size.min_height;
2164 c->basew = c->baseh = 0;
2165 if(size.flags & PResizeInc) {
2166 c->incw = size.width_inc;
2167 c->inch = size.height_inc;
2170 c->incw = c->inch = 0;
2171 if(size.flags & PMaxSize) {
2172 c->maxw = size.max_width;
2173 c->maxh = size.max_height;
2176 c->maxw = c->maxh = 0;
2177 if(size.flags & PMinSize) {
2178 c->minw = size.min_width;
2179 c->minh = size.min_height;
2181 else if(size.flags & PBaseSize) {
2182 c->minw = size.base_width;
2183 c->minh = size.base_height;
2186 c->minw = c->minh = 0;
2187 if(size.flags & PAspect) {
2188 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2189 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2192 c->maxa = c->mina = 0.0;
2193 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2194 && c->maxw == c->minw && c->maxh == c->minh);
2198 updatetitle(Client *c) {
2199 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2200 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2201 if(c->name[0] == '\0') /* hack to mark broken clients */
2202 strcpy(c->name, broken);
2206 updatestatus(void) {
2207 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2208 strcpy(stext, "dwm-"VERSION);
2213 updatewindowtype(Client *c) {
2214 Atom state = getatomprop(c, netatom[NetWMState]);
2215 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2217 if(state == netatom[NetWMFullscreen])
2218 setfullscreen(c, True);
2219 if(wtype == netatom[NetWMWindowTypeDialog])
2220 c->isfloating = True;
2224 updatewmhints(Client *c) {
2227 if((wmh = XGetWMHints(dpy, c->win))) {
2228 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2229 wmh->flags &= ~XUrgencyHint;
2230 XSetWMHints(dpy, c->win, wmh);
2233 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2234 if(wmh->flags & InputHint)
2235 c->neverfocus = !wmh->input;
2237 c->neverfocus = False;
2243 view(const Arg *arg) {
2244 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2246 selmon->seltags ^= 1; /* toggle sel tagset */
2247 if(arg->ui & TAGMASK)
2248 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2254 wintoclient(Window w) {
2258 for(m = mons; m; m = m->next)
2259 for(c = m->clients; c; c = c->next)
2266 wintomon(Window w) {
2271 if(w == root && getrootptr(&x, &y))
2272 return recttomon(x, y, 1, 1);
2273 for(m = mons; m; m = m->next)
2276 if((c = wintoclient(w)))
2281 /* There's no way to check accesses to destroyed windows, thus those cases are
2282 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2283 * default error handler, which may call exit. */
2285 xerror(Display *dpy, XErrorEvent *ee) {
2286 if(ee->error_code == BadWindow
2287 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2288 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2289 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2290 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2291 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2292 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2293 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2294 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2296 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2297 ee->request_code, ee->error_code);
2298 return xerrorxlib(dpy, ee); /* may call exit */
2302 xerrordummy(Display *dpy, XErrorEvent *ee) {
2306 /* Startup Error handler to check if another window manager
2307 * is already running. */
2309 xerrorstart(Display *dpy, XErrorEvent *ee) {
2310 die("dwm: another window manager is already running\n");
2315 zoom(const Arg *arg) {
2316 Client *c = selmon->sel;
2318 if(!selmon->lt[selmon->sellt]->arrange
2319 || (selmon->sel && selmon->sel->isfloating))
2321 if(c == nexttiled(selmon->clients))
2322 if(!c || !(c = nexttiled(c->next)))
2328 main(int argc, char *argv[]) {
2329 if(argc == 2 && !strcmp("-v", argv[1]))
2330 die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
2332 die("usage: dwm [-v]\n");
2333 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2334 fputs("warning: no locale support\n", stderr);
2335 if(!(dpy = XOpenDisplay(NULL)))
2336 die("dwm: cannot open display\n");
2343 return EXIT_SUCCESS;