Advertisement
Guest User

dwm.c

a guest
Jun 9th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 54.40 KB | None | 0 0
  1. /* See LICENSE file for copyright and license details.
  2.  *
  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.
  8.  *
  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
  11.  * in O(1) time.
  12.  *
  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
  17.  * client.
  18.  *
  19.  * Keys and tagging rules are organized as arrays and defined in config.h.
  20.  *
  21.  * To understand everything else, start reading main().
  22.  */
  23. #include <errno.h>
  24. #include <locale.h>
  25. #include <stdarg.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/wait.h>
  33. #include <X11/cursorfont.h>
  34. #include <X11/keysym.h>
  35. #include <X11/Xatom.h>
  36. #include <X11/Xlib.h>
  37. #include <X11/Xproto.h>
  38. #include <X11/Xutil.h>
  39. #ifdef XINERAMA
  40. #include <X11/extensions/Xinerama.h>
  41. #endif /* XINERAMA */
  42.  
  43. /* macros */
  44. #define BUTTONMASK              (ButtonPressMask|ButtonReleaseMask)
  45. #define CLEANMASK(mask)         (mask & ~(numlockmask|LockMask))
  46. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  47. #define ISVISIBLE(C)            ((C->tags & C->mon->tagset[C->mon->seltags]))
  48. #define LENGTH(X)               (sizeof X / sizeof X[0])
  49. #define MAX(A, B)               ((A) > (B) ? (A) : (B))
  50. #define MIN(A, B)               ((A) < (B) ? (A) : (B))
  51. #define MOUSEMASK               (BUTTONMASK|PointerMotionMask)
  52. #define WIDTH(X)                ((X)->w + 2 * (X)->bw)
  53. #define HEIGHT(X)               ((X)->h + 2 * (X)->bw)
  54. #define TAGMASK                 ((1 << LENGTH(tags)) - 1)
  55. #define TEXTW(X)                (textnw(X, strlen(X)) + dc.font.height)
  56.  
  57. /* enums */
  58. enum { CurNormal, CurResize, CurMove, CurLast };        /* cursor */
  59. enum { ColBorder, ColFG, ColBG, ColLast };              /* color */
  60. enum { NetSupported, NetWMName, NetWMState,
  61.        NetWMFullscreen, NetLast };                      /* EWMH atoms */
  62. enum { WMProtocols, WMDelete, WMState, WMLast };        /* default atoms */
  63. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  64.        ClkClientWin, ClkRootWin, ClkLast };             /* clicks */
  65.  
  66. typedef union {
  67.     int i;
  68.     unsigned int ui;
  69.     float f;
  70.     const void *v;
  71. } Arg;
  72.  
  73. typedef struct {
  74.     unsigned int click;
  75.     unsigned int mask;
  76.     unsigned int button;
  77.     void (*func)(const Arg *arg);
  78.     const Arg arg;
  79. } Button;
  80.  
  81. typedef struct Monitor Monitor;
  82. typedef struct Client Client;
  83. struct Client {
  84.     char name[256];
  85.     float mina, maxa;
  86.     int x, y, w, h;
  87.     int oldx, oldy, oldw, oldh;
  88.     int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  89.     int bw, oldbw;
  90.     unsigned int tags;
  91.     Bool isfixed, isfloating, isurgent, oldstate;
  92.     Client *next;
  93.     Client *snext;
  94.     Monitor *mon;
  95.     Window win;
  96. };
  97.  
  98. typedef struct {
  99.     int x, y, w, h;
  100.     unsigned long norm[ColLast];
  101.     unsigned long sel[ColLast];
  102.     Drawable drawable;
  103.     GC gc;
  104.     struct {
  105.         int ascent;
  106.         int descent;
  107.         int height;
  108.         XFontSet set;
  109.         XFontStruct *xfont;
  110.     } font;
  111. } DC; /* draw context */
  112.  
  113. typedef struct {
  114.     unsigned int mod;
  115.     KeySym keysym;
  116.     void (*func)(const Arg *);
  117.     const Arg arg;
  118. } Key;
  119.  
  120. typedef struct {
  121.     const char *symbol;
  122.     void (*arrange)(Monitor *);
  123. } Layout;
  124.  
  125. typedef struct {
  126.     const char *class;
  127.     const char *instance;
  128.     const char *title;
  129.     unsigned int tags;
  130.     Bool isfloating;
  131.     int monitor;
  132. } Rule;
  133.  
  134. /* function declarations */
  135. static void applyrules(Client *c);
  136. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
  137. static void arrange(Monitor *m);
  138. static void arrangemon(Monitor *m);
  139. static void attach(Client *c);
  140. static void attachstack(Client *c);
  141. static void bstack(Monitor *m);
  142. static void bstackhoriz(Monitor *m);
  143. static void buttonpress(XEvent *e);
  144. static void checkotherwm(void);
  145. static void cleanup(void);
  146. static void cleanupmon(Monitor *mon);
  147. static void clearurgent(Client *c);
  148. static void clientmessage(XEvent *e);
  149. static void configure(Client *c);
  150. static void configurenotify(XEvent *e);
  151. static void configurerequest(XEvent *e);
  152. static Monitor *createmon(void);
  153. static void destroynotify(XEvent *e);
  154. static void detach(Client *c);
  155. static void detachstack(Client *c);
  156. static void die(const char *errstr, ...);
  157. static Monitor *dirtomon(int dir);
  158. static void drawbar(Monitor *m);
  159. static void drawbars(void);
  160. static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  161. static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  162. static void enternotify(XEvent *e);
  163. static void expose(XEvent *e);
  164. static void focus(Client *c);
  165. static void focusin(XEvent *e);
  166. static void focusmon(const Arg *arg);
  167. static void focusstack(const Arg *arg);
  168. static unsigned long getcolor(const char *colstr);
  169. static Bool getrootptr(int *x, int *y);
  170. static long getstate(Window w);
  171. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  172. static void grabbuttons(Client *c, Bool focused);
  173. static void grabkeys(void);
  174. static void initfont(const char *fontstr);
  175. static Bool isprotodel(Client *c);
  176. static void keypress(XEvent *e);
  177. static void killclient(const Arg *arg);
  178. static void manage(Window w, XWindowAttributes *wa);
  179. static void mappingnotify(XEvent *e);
  180. static void maprequest(XEvent *e);
  181. static void monocle(Monitor *m);
  182. static void movemouse(const Arg *arg);
  183. static Client *nexttiled(Client *c);
  184. static Monitor *ptrtomon(int x, int y);
  185. static void propertynotify(XEvent *e);
  186. static void quit(const Arg *arg);
  187. static void resize(Client *c, int x, int y, int w, int h, Bool interact);
  188. static void resizeclient(Client *c, int x, int y, int w, int h);
  189. static void resizemouse(const Arg *arg);
  190. static void restack(Monitor *m);
  191. static void run(void);
  192. static void scan(void);
  193. static void sendmon(Client *c, Monitor *m);
  194. static void setclientstate(Client *c, long state);
  195. static void setlayout(const Arg *arg);
  196. static void setmfact(const Arg *arg);
  197. static void setup(void);
  198. static void showhide(Client *c);
  199. static void sigchld(int unused);
  200. static void spawn(const Arg *arg);
  201. static void tag(const Arg *arg);
  202. static void tagmon(const Arg *arg);
  203. static int textnw(const char *text, unsigned int len);
  204. static void tile(Monitor *);
  205. static void togglebar(const Arg *arg);
  206. static void togglefloating(const Arg *arg);
  207. static void toggletag(const Arg *arg);
  208. static void toggleview(const Arg *arg);
  209. static void unfocus(Client *c, Bool setfocus);
  210. static void unmanage(Client *c, Bool destroyed);
  211. static void unmapnotify(XEvent *e);
  212. static Bool updategeom(void);
  213. static void updatebarpos(Monitor *m);
  214. static void updatebars(void);
  215. static void updatenumlockmask(void);
  216. static void updatesizehints(Client *c);
  217. static void updatestatus(void);
  218. static void updatetitle(Client *c);
  219. static void updatewmhints(Client *c);
  220. static void view(const Arg *arg);
  221. static Client *wintoclient(Window w);
  222. static Monitor *wintomon(Window w);
  223. static int xerror(Display *dpy, XErrorEvent *ee);
  224. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  225. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  226. static void zoom(const Arg *arg);
  227. //ex-fibonacci.c declarations
  228. void fibonacci (Monitor *mon, int s);
  229. void dwindle (Monitor *mon);
  230. void spiral (Monitor *mon);
  231.  
  232. /* variables */
  233. static const char broken[] = "broken";
  234. static char stext[256];
  235. static int screen;
  236. static int sw, sh;           /* X display screen geometry width, height */
  237. static int bh, blw = 0;      /* bar geometry */
  238. static int (*xerrorxlib)(Display *, XErrorEvent *);
  239. static unsigned int numlockmask = 0;
  240. static void (*handler[LASTEvent]) (XEvent *) = {
  241.     [ButtonPress] = buttonpress,
  242.     [ClientMessage] = clientmessage,
  243.     [ConfigureRequest] = configurerequest,
  244.     [ConfigureNotify] = configurenotify,
  245.     [DestroyNotify] = destroynotify,
  246.     [EnterNotify] = enternotify,
  247.     [Expose] = expose,
  248.     [FocusIn] = focusin,
  249.     [KeyPress] = keypress,
  250.     [MappingNotify] = mappingnotify,
  251.     [MapRequest] = maprequest,
  252.     [PropertyNotify] = propertynotify,
  253.     [UnmapNotify] = unmapnotify
  254. };
  255. static Atom wmatom[WMLast], netatom[NetLast];
  256. static Bool otherwm;
  257. static Bool running = True;
  258. static Cursor cursor[CurLast];
  259. static Display *dpy;
  260. static DC dc;
  261. static Monitor *mons = NULL, *selmon = NULL;
  262. static Window root;
  263.  
  264. /* configuration, allows nested code to access above variables */
  265. #include "config.h"
  266.  
  267. struct Monitor {
  268.     char ltsymbol[16];
  269.     float mfact;
  270.     int num;
  271.     int by;               /* bar geometry */
  272.     int mx, my, mw, mh;   /* screen size */
  273.     int wx, wy, ww, wh;   /* window area  */
  274.     unsigned int seltags;
  275.     unsigned int sellt;
  276.     unsigned int tagset[2];
  277.     Bool showbar;
  278.     Bool topbar;
  279.     Client *clients;
  280.     Client *sel;
  281.     Client *stack;
  282.     Monitor *next;
  283.     Window barwin;
  284.     const Layout *lt[2];
  285.     int curtag;
  286.     int prevtag;
  287.     const Layout *lts[LENGTH(tags) + 1];
  288.     double mfacts[LENGTH(tags) + 1];
  289.     Bool showbars[LENGTH(tags) + 1];
  290. };
  291.  
  292. /* compile-time check if all tags fit into an unsigned int bit array. */
  293. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  294.  
  295. /* function implementations */
  296. void
  297. applyrules(Client *c) {
  298.     const char *class, *instance;
  299.     unsigned int i;
  300.     const Rule *r;
  301.     Monitor *m;
  302.     XClassHint ch = { 0 };
  303.  
  304.     /* rule matching */
  305.     c->isfloating = c->tags = 0;
  306.     if(XGetClassHint(dpy, c->win, &ch)) {
  307.         class = ch.res_class ? ch.res_class : broken;
  308.         instance = ch.res_name ? ch.res_name : broken;
  309.         for(i = 0; i < LENGTH(rules); i++) {
  310.             r = &rules[i];
  311.             if((!r->title || strstr(c->name, r->title))
  312.             && (!r->class || strstr(class, r->class))
  313.             && (!r->instance || strstr(instance, r->instance)))
  314.             {
  315.                 c->isfloating = r->isfloating;
  316.                 c->tags |= r->tags;
  317.                 for(m = mons; m && m->num != r->monitor; m = m->next);
  318.                 if(m)
  319.                     c->mon = m;
  320.             }
  321.         }
  322.         if(ch.res_class)
  323.             XFree(ch.res_class);
  324.         if(ch.res_name)
  325.             XFree(ch.res_name);
  326.     }
  327.     c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  328. }
  329.  
  330. Bool
  331. applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
  332.     Bool baseismin;
  333.     Monitor *m = c->mon;
  334.  
  335.     /* set minimum possible */
  336.     *w = MAX(1, *w);
  337.     *h = MAX(1, *h);
  338.     if(interact) {
  339.         if(*x > sw)
  340.             *x = sw - WIDTH(c);
  341.         if(*y > sh)
  342.             *y = sh - HEIGHT(c);
  343.         if(*x + *w + 2 * c->bw < 0)
  344.             *x = 0;
  345.         if(*y + *h + 2 * c->bw < 0)
  346.             *y = 0;
  347.     }
  348.     else {
  349.         if(*x > m->mx + m->mw)
  350.             *x = m->mx + m->mw - WIDTH(c);
  351.         if(*y > m->my + m->mh)
  352.             *y = m->my + m->mh - HEIGHT(c);
  353.         if(*x + *w + 2 * c->bw < m->mx)
  354.             *x = m->mx;
  355.         if(*y + *h + 2 * c->bw < m->my)
  356.             *y = m->my;
  357.     }
  358.     if(*h < bh)
  359.         *h = bh;
  360.     if(*w < bh)
  361.         *w = bh;
  362.     if(resizehints || c->isfloating) {
  363.         /* see last two sentences in ICCCM 4.1.2.3 */
  364.         baseismin = c->basew == c->minw && c->baseh == c->minh;
  365.         if(!baseismin) { /* temporarily remove base dimensions */
  366.             *w -= c->basew;
  367.             *h -= c->baseh;
  368.         }
  369.         /* adjust for aspect limits */
  370.         if(c->mina > 0 && c->maxa > 0) {
  371.             if(c->maxa < (float)*w / *h)
  372.                 *w = *h * c->maxa + 0.5;
  373.             else if(c->mina < (float)*h / *w)
  374.                 *h = *w * c->mina + 0.5;
  375.         }
  376.         if(baseismin) { /* increment calculation requires this */
  377.             *w -= c->basew;
  378.             *h -= c->baseh;
  379.         }
  380.         /* adjust for increment value */
  381.         if(c->incw)
  382.             *w -= *w % c->incw;
  383.         if(c->inch)
  384.             *h -= *h % c->inch;
  385.         /* restore base dimensions */
  386.         *w += c->basew;
  387.         *h += c->baseh;
  388.         *w = MAX(*w, c->minw);
  389.         *h = MAX(*h, c->minh);
  390.         if(c->maxw)
  391.             *w = MIN(*w, c->maxw);
  392.         if(c->maxh)
  393.             *h = MIN(*h, c->maxh);
  394.     }
  395.     return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  396. }
  397.  
  398. void
  399. arrange(Monitor *m) {
  400.     if(m)
  401.         showhide(m->stack);
  402.     else for(m = mons; m; m = m->next)
  403.         showhide(m->stack);
  404.     focus(NULL);
  405.     if(m)
  406.         arrangemon(m);
  407.     else for(m = mons; m; m = m->next)
  408.         arrangemon(m);
  409. }
  410.  
  411. void
  412. arrangemon(Monitor *m) {
  413.     strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  414.     if(m->lt[m->sellt]->arrange)
  415.         m->lt[m->sellt]->arrange(m);
  416.     restack(m);
  417. }
  418.  
  419. void
  420. attach(Client *c) {
  421.     c->next = c->mon->clients;
  422.     c->mon->clients = c;
  423. }
  424.  
  425. void
  426. attachstack(Client *c) {
  427.     c->snext = c->mon->stack;
  428.     c->mon->stack = c;
  429. }
  430.  
  431. void
  432. buttonpress(XEvent *e) {
  433.     unsigned int i, x, click;
  434.     Arg arg = {0};
  435.     Client *c;
  436.     Monitor *m;
  437.     XButtonPressedEvent *ev = &e->xbutton;
  438.  
  439.     click = ClkRootWin;
  440.     /* focus monitor if necessary */
  441.     if((m = wintomon(ev->window)) && m != selmon) {
  442.         unfocus(selmon->sel, True);
  443.         selmon = m;
  444.         focus(NULL);
  445.     }
  446.     if(ev->window == selmon->barwin) {
  447.         i = x = 0;
  448.         do {
  449.             x += TEXTW(tags[i]);
  450.         } while(ev->x >= x && ++i < LENGTH(tags));
  451.         if(i < LENGTH(tags)) {
  452.             click = ClkTagBar;
  453.             arg.ui = 1 << i;
  454.         }
  455.         else if(ev->x < x + blw)
  456.             click = ClkLtSymbol;
  457.         else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
  458.             click = ClkStatusText;
  459.         else
  460.             click = ClkWinTitle;
  461.     }
  462.     else if((c = wintoclient(ev->window))) {
  463.         focus(c);
  464.         click = ClkClientWin;
  465.     }
  466.     for(i = 0; i < LENGTH(buttons); i++)
  467.         if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  468.         && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  469.             buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  470. }
  471.  
  472. void
  473. checkotherwm(void) {
  474.     otherwm = False;
  475.     xerrorxlib = XSetErrorHandler(xerrorstart);
  476.     /* this causes an error if some other window manager is running */
  477.     XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  478.     XSync(dpy, False);
  479.     if(otherwm)
  480.         die("dwm: another window manager is already running\n");
  481.     XSetErrorHandler(xerror);
  482.     XSync(dpy, False);
  483. }
  484.  
  485. void
  486. cleanup(void) {
  487.     Arg a = {.ui = ~0};
  488.     Layout foo = { "", NULL };
  489.     Monitor *m;
  490.  
  491.     view(&a);
  492.     selmon->lt[selmon->sellt] = &foo;
  493.     for(m = mons; m; m = m->next)
  494.         while(m->stack)
  495.             unmanage(m->stack, False);
  496.     if(dc.font.set)
  497.         XFreeFontSet(dpy, dc.font.set);
  498.     else
  499.         XFreeFont(dpy, dc.font.xfont);
  500.     XUngrabKey(dpy, AnyKey, AnyModifier, root);
  501.     XFreePixmap(dpy, dc.drawable);
  502.     XFreeGC(dpy, dc.gc);
  503.     XFreeCursor(dpy, cursor[CurNormal]);
  504.     XFreeCursor(dpy, cursor[CurResize]);
  505.     XFreeCursor(dpy, cursor[CurMove]);
  506.     while(mons)
  507.         cleanupmon(mons);
  508.     XSync(dpy, False);
  509.     XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  510. }
  511.  
  512. void
  513. cleanupmon(Monitor *mon) {
  514.     Monitor *m;
  515.  
  516.     if(mon == mons)
  517.         mons = mons->next;
  518.     else {
  519.         for(m = mons; m && m->next != mon; m = m->next);
  520.         m->next = mon->next;
  521.     }
  522.     XUnmapWindow(dpy, mon->barwin);
  523.     XDestroyWindow(dpy, mon->barwin);
  524.     free(mon);
  525. }
  526.  
  527. void
  528. clearurgent(Client *c) {
  529.     XWMHints *wmh;
  530.  
  531.     c->isurgent = False;
  532.     if(!(wmh = XGetWMHints(dpy, c->win)))
  533.         return;
  534.     wmh->flags &= ~XUrgencyHint;
  535.     XSetWMHints(dpy, c->win, wmh);
  536.     XFree(wmh);
  537. }
  538.  
  539. void
  540. configure(Client *c) {
  541.     XConfigureEvent ce;
  542.  
  543.     ce.type = ConfigureNotify;
  544.     ce.display = dpy;
  545.     ce.event = c->win;
  546.     ce.window = c->win;
  547.     ce.x = c->x;
  548.     ce.y = c->y;
  549.     ce.width = c->w;
  550.     ce.height = c->h;
  551.     ce.border_width = c->bw;
  552.     ce.above = None;
  553.     ce.override_redirect = False;
  554.     XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  555. }
  556.  
  557. void
  558. configurenotify(XEvent *e) {
  559.     Monitor *m;
  560.     XConfigureEvent *ev = &e->xconfigure;
  561.  
  562.     if(ev->window == root) {
  563.         sw = ev->width;
  564.         sh = ev->height;
  565.         if(updategeom()) {
  566.             if(dc.drawable != 0)
  567.                 XFreePixmap(dpy, dc.drawable);
  568.             dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  569.             updatebars();
  570.             for(m = mons; m; m = m->next)
  571.                 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  572.             arrange(NULL);
  573.         }
  574.     }
  575. }
  576.  
  577. void
  578. configurerequest(XEvent *e) {
  579.     Client *c;
  580.     Monitor *m;
  581.     XConfigureRequestEvent *ev = &e->xconfigurerequest;
  582.     XWindowChanges wc;
  583.  
  584.     if((c = wintoclient(ev->window))) {
  585.         if(ev->value_mask & CWBorderWidth)
  586.             c->bw = ev->border_width;
  587.         else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  588.             m = c->mon;
  589.             if(ev->value_mask & CWX)
  590.                 c->x = m->mx + ev->x;
  591.             if(ev->value_mask & CWY)
  592.                 c->y = m->my + ev->y;
  593.             if(ev->value_mask & CWWidth)
  594.                 c->w = ev->width;
  595.             if(ev->value_mask & CWHeight)
  596.                 c->h = ev->height;
  597.             if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  598.                 c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
  599.             if((c->y + c->h) > m->my + m->mh && c->isfloating)
  600.                 c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
  601.             if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  602.                 configure(c);
  603.             if(ISVISIBLE(c))
  604.                 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  605.         }
  606.         else
  607.             configure(c);
  608.     }
  609.     else {
  610.         wc.x = ev->x;
  611.         wc.y = ev->y;
  612.         wc.width = ev->width;
  613.         wc.height = ev->height;
  614.         wc.border_width = ev->border_width;
  615.         wc.sibling = ev->above;
  616.         wc.stack_mode = ev->detail;
  617.         XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  618.     }
  619.     XSync(dpy, False);
  620. }
  621.  
  622. Monitor *
  623. createmon(void) {
  624.     Monitor *m;
  625.     unsigned int i;
  626.  
  627.     if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
  628.         die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  629.     m->tagset[0] = m->tagset[1] = 1;
  630.     m->mfact = mfact;
  631.     m->showbar = showbar;
  632.     m->topbar = topbar;
  633.     m->lt[0] = &layouts[0];
  634.     m->lt[1] = &layouts[1 % LENGTH(layouts)];
  635.     strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  636.  
  637.     /* pertag init */
  638.     m->curtag = m->prevtag = 1;
  639.     for(i=0; i < LENGTH(tags) + 1 ; i++) {
  640.         m->mfacts[i] = mfact;
  641.         m->lts[i] = &layouts[0];
  642.         m->showbars[i] = m->showbar;
  643.     }
  644.  
  645.     return m;
  646. }
  647.  
  648. void
  649. destroynotify(XEvent *e) {
  650.     Client *c;
  651.     XDestroyWindowEvent *ev = &e->xdestroywindow;
  652.  
  653.     if((c = wintoclient(ev->window)))
  654.         unmanage(c, True);
  655. }
  656.  
  657. void
  658. detach(Client *c) {
  659.     Client **tc;
  660.  
  661.     for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  662.     *tc = c->next;
  663. }
  664.  
  665. void
  666. detachstack(Client *c) {
  667.     Client **tc, *t;
  668.  
  669.     for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  670.     *tc = c->snext;
  671.  
  672.     if(c == c->mon->sel) {
  673.         for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  674.         c->mon->sel = t;
  675.     }
  676. }
  677.  
  678. void
  679. die(const char *errstr, ...) {
  680.     va_list ap;
  681.  
  682.     va_start(ap, errstr);
  683.     vfprintf(stderr, errstr, ap);
  684.     va_end(ap);
  685.     exit(EXIT_FAILURE);
  686. }
  687.  
  688. Monitor *
  689. dirtomon(int dir) {
  690.     Monitor *m = NULL;
  691.  
  692.     if(dir > 0) {
  693.         if(!(m = selmon->next))
  694.             m = mons;
  695.     }
  696.     else {
  697.         if(selmon == mons)
  698.             for(m = mons; m->next; m = m->next);
  699.         else
  700.             for(m = mons; m->next != selmon; m = m->next);
  701.     }
  702.     return m;
  703. }
  704.  
  705. void
  706. drawbar(Monitor *m) {
  707.     int x;
  708.     unsigned int i, occ = 0, urg = 0;
  709.     unsigned long *col;
  710.     Client *c;
  711.  
  712.     for(c = m->clients; c; c = c->next) {
  713.         occ |= c->tags;
  714.         if(c->isurgent)
  715.             urg |= c->tags;
  716.     }
  717.     dc.x = 0;
  718.     for(i = 0; i < LENGTH(tags); i++) {
  719.         dc.w = TEXTW(tags[i]);
  720.         col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  721.         drawtext(tags[i], col, urg & 1 << i);
  722.         drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  723.                    occ & 1 << i, urg & 1 << i, col);
  724.         dc.x += dc.w;
  725.     }
  726.     dc.w = blw = TEXTW(m->ltsymbol);
  727.     drawtext(m->ltsymbol, dc.norm, False);
  728.     dc.x += dc.w;
  729.     x = dc.x;
  730.     if(m == selmon) { /* status is only drawn on selected monitor */
  731.         dc.w = TEXTW(stext);
  732.         dc.x = m->ww - dc.w;
  733.         if(dc.x < x) {
  734.             dc.x = x;
  735.             dc.w = m->ww - x;
  736.         }
  737.         drawtext(stext, dc.norm, False);
  738.     }
  739.     else
  740.         dc.x = m->ww;
  741.     if((dc.w = dc.x - x) > bh) {
  742.         dc.x = x;
  743.         if(m->sel) {
  744.             col = m == selmon ? dc.sel : dc.norm;
  745.             drawtext(m->sel->name, col, False);
  746.             drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  747.         }
  748.         else
  749.             drawtext(NULL, dc.norm, False);
  750.     }
  751.     XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  752.     XSync(dpy, False);
  753. }
  754.  
  755. void
  756. drawbars(void) {
  757.     Monitor *m;
  758.  
  759.     for(m = mons; m; m = m->next)
  760.         drawbar(m);
  761. }
  762.  
  763. void
  764. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  765.     int x;
  766.     XGCValues gcv;
  767.     XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  768.  
  769.     gcv.foreground = col[invert ? ColBG : ColFG];
  770.     XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  771.     x = (dc.font.ascent + dc.font.descent + 2) / 4;
  772.     r.x = dc.x + 1;
  773.     r.y = dc.y + 1;
  774.     if(filled) {
  775.         r.width = r.height = x + 1;
  776.         XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  777.     }
  778.     else if(empty) {
  779.         r.width = r.height = x;
  780.         XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  781.     }
  782. }
  783.  
  784. void
  785. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  786.     char buf[256];
  787.     int i, x, y, h, len, olen;
  788.     XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  789.  
  790.     XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  791.     XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  792.     if(!text)
  793.         return;
  794.     olen = strlen(text);
  795.     h = dc.font.ascent + dc.font.descent;
  796.     y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  797.     x = dc.x + (h / 2);
  798.     /* shorten text if necessary */
  799.     for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  800.     if(!len)
  801.         return;
  802.     memcpy(buf, text, len);
  803.     if(len < olen)
  804.         for(i = len; i && i > len - 3; buf[--i] = '.');
  805.     XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  806.     if(dc.font.set)
  807.         XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  808.     else
  809.         XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  810. }
  811.  
  812. void
  813. enternotify(XEvent *e) {
  814.     Client *c;
  815.     Monitor *m;
  816.     XCrossingEvent *ev = &e->xcrossing;
  817.  
  818.     if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  819.         return;
  820.     if((m = wintomon(ev->window)) && m != selmon) {
  821.         unfocus(selmon->sel, True);
  822.         selmon = m;
  823.     }
  824.     if((c = wintoclient(ev->window)))
  825.         focus(c);
  826.     else
  827.         focus(NULL);
  828. }
  829.  
  830. void
  831. expose(XEvent *e) {
  832.     Monitor *m;
  833.     XExposeEvent *ev = &e->xexpose;
  834.  
  835.     if(ev->count == 0 && (m = wintomon(ev->window)))
  836.         drawbar(m);
  837. }
  838.  
  839. void
  840. focus(Client *c) {
  841.     if(!c || !ISVISIBLE(c))
  842.         for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  843.     /* was if(selmon->sel) */
  844.     if(selmon->sel && selmon->sel != c)
  845.         unfocus(selmon->sel, False);
  846.     if(c) {
  847.         if(c->mon != selmon)
  848.             selmon = c->mon;
  849.         if(c->isurgent)
  850.             clearurgent(c);
  851.         detachstack(c);
  852.         attachstack(c);
  853.         grabbuttons(c, True);
  854.         XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  855.         XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  856.     }
  857.     else
  858.         XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  859.     selmon->sel = c;
  860.     drawbars();
  861. }
  862.  
  863. void
  864. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  865.     XFocusChangeEvent *ev = &e->xfocus;
  866.  
  867.     if(selmon->sel && ev->window != selmon->sel->win)
  868.         XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  869. }
  870.  
  871. void
  872. focusmon(const Arg *arg) {
  873.     Monitor *m = NULL;
  874.  
  875.     if(!mons->next)
  876.         return;
  877.     if((m = dirtomon(arg->i)) == selmon)
  878.         return;
  879.     unfocus(selmon->sel, True);
  880.     selmon = m;
  881.     focus(NULL);
  882. }
  883.  
  884. void
  885. focusstack(const Arg *arg) {
  886.     Client *c = NULL, *i;
  887.  
  888.     if(!selmon->sel)
  889.         return;
  890.     if(arg->i > 0) {
  891.         for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  892.         if(!c)
  893.             for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  894.     }
  895.     else {
  896.         for(i = selmon->clients; i != selmon->sel; i = i->next)
  897.             if(ISVISIBLE(i))
  898.                 c = i;
  899.         if(!c)
  900.             for(; i; i = i->next)
  901.                 if(ISVISIBLE(i))
  902.                     c = i;
  903.     }
  904.     if(c) {
  905.         focus(c);
  906.         restack(selmon);
  907.     }
  908. }
  909.  
  910. unsigned long
  911. getcolor(const char *colstr) {
  912.     Colormap cmap = DefaultColormap(dpy, screen);
  913.     XColor color;
  914.  
  915.     if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  916.         die("error, cannot allocate color '%s'\n", colstr);
  917.     return color.pixel;
  918. }
  919.  
  920. Bool
  921. getrootptr(int *x, int *y) {
  922.     int di;
  923.     unsigned int dui;
  924.     Window dummy;
  925.  
  926.     return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  927. }
  928.  
  929. long
  930. getstate(Window w) {
  931.     int format;
  932.     long result = -1;
  933.     unsigned char *p = NULL;
  934.     unsigned long n, extra;
  935.     Atom real;
  936.  
  937.     if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  938.                           &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  939.         return -1;
  940.     if(n != 0)
  941.         result = *p;
  942.     XFree(p);
  943.     return result;
  944. }
  945.  
  946. Bool
  947. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  948.     char **list = NULL;
  949.     int n;
  950.     XTextProperty name;
  951.  
  952.     if(!text || size == 0)
  953.         return False;
  954.     text[0] = '\0';
  955.     XGetTextProperty(dpy, w, &name, atom);
  956.     if(!name.nitems)
  957.         return False;
  958.     if(name.encoding == XA_STRING)
  959.         strncpy(text, (char *)name.value, size - 1);
  960.     else {
  961.         if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  962.             strncpy(text, *list, size - 1);
  963.             XFreeStringList(list);
  964.         }
  965.     }
  966.     text[size - 1] = '\0';
  967.     XFree(name.value);
  968.     return True;
  969. }
  970.  
  971. void
  972. grabbuttons(Client *c, Bool focused) {
  973.     updatenumlockmask();
  974.     {
  975.         unsigned int i, j;
  976.         unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  977.         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  978.         if(focused) {
  979.             for(i = 0; i < LENGTH(buttons); i++)
  980.                 if(buttons[i].click == ClkClientWin)
  981.                     for(j = 0; j < LENGTH(modifiers); j++)
  982.                         XGrabButton(dpy, buttons[i].button,
  983.                                     buttons[i].mask | modifiers[j],
  984.                                     c->win, False, BUTTONMASK,
  985.                                     GrabModeAsync, GrabModeSync, None, None);
  986.         }
  987.         else
  988.             XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  989.                         BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  990.     }
  991. }
  992.  
  993. void
  994. grabkeys(void) {
  995.     updatenumlockmask();
  996.     {
  997.         unsigned int i, j;
  998.         unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  999.         KeyCode code;
  1000.  
  1001.         XUngrabKey(dpy, AnyKey, AnyModifier, root);
  1002.         for(i = 0; i < LENGTH(keys); i++) {
  1003.             if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  1004.                 for(j = 0; j < LENGTH(modifiers); j++)
  1005.                     XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  1006.                          True, GrabModeAsync, GrabModeAsync);
  1007.         }
  1008.     }
  1009. }
  1010.  
  1011. void
  1012. initfont(const char *fontstr) {
  1013.     char *def, **missing;
  1014.     int i, n;
  1015.  
  1016.     missing = NULL;
  1017.     dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  1018.     if(missing) {
  1019.         while(n--)
  1020.             fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  1021.         XFreeStringList(missing);
  1022.     }
  1023.     if(dc.font.set) {
  1024.         XFontSetExtents *font_extents;
  1025.         XFontStruct **xfonts;
  1026.         char **font_names;
  1027.  
  1028.         dc.font.ascent = dc.font.descent = 0;
  1029.         font_extents = XExtentsOfFontSet(dc.font.set);
  1030.         n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  1031.         for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  1032.             dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  1033.             dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  1034.             xfonts++;
  1035.         }
  1036.     }
  1037.     else {
  1038.         if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  1039.         && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  1040.             die("error, cannot load font: '%s'\n", fontstr);
  1041.         dc.font.ascent = dc.font.xfont->ascent;
  1042.         dc.font.descent = dc.font.xfont->descent;
  1043.     }
  1044.     dc.font.height = dc.font.ascent + dc.font.descent;
  1045. }
  1046.  
  1047. Bool
  1048. isprotodel(Client *c) {
  1049.     int i, n;
  1050.     Atom *protocols;
  1051.     Bool ret = False;
  1052.  
  1053.     if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1054.         for(i = 0; !ret && i < n; i++)
  1055.             if(protocols[i] == wmatom[WMDelete])
  1056.                 ret = True;
  1057.         XFree(protocols);
  1058.     }
  1059.     return ret;
  1060. }
  1061.  
  1062. #ifdef XINERAMA
  1063. static Bool
  1064. isuniquegeom(XineramaScreenInfo *unique, size_t len, XineramaScreenInfo *info) {
  1065.     unsigned int i;
  1066.  
  1067.     for(i = 0; i < len; i++)
  1068.         if(unique[i].x_org == info->x_org && unique[i].y_org == info->y_org
  1069.         && unique[i].width == info->width && unique[i].height == info->height)
  1070.             return False;
  1071.     return True;
  1072. }
  1073. #endif /* XINERAMA */
  1074.  
  1075. void
  1076. keypress(XEvent *e) {
  1077.     unsigned int i;
  1078.     KeySym keysym;
  1079.     XKeyEvent *ev;
  1080.  
  1081.     ev = &e->xkey;
  1082.     keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  1083.     for(i = 0; i < LENGTH(keys); i++)
  1084.         if(keysym == keys[i].keysym
  1085.         && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  1086.         && keys[i].func)
  1087.             keys[i].func(&(keys[i].arg));
  1088. }
  1089.  
  1090. void
  1091. killclient(const Arg *arg) {
  1092.     XEvent ev;
  1093.  
  1094.     if(!selmon->sel)
  1095.         return;
  1096.     if(isprotodel(selmon->sel)) {
  1097.         ev.type = ClientMessage;
  1098.         ev.xclient.window = selmon->sel->win;
  1099.         ev.xclient.message_type = wmatom[WMProtocols];
  1100.         ev.xclient.format = 32;
  1101.         ev.xclient.data.l[0] = wmatom[WMDelete];
  1102.         ev.xclient.data.l[1] = CurrentTime;
  1103.         XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  1104.     }
  1105.     else {
  1106.         XGrabServer(dpy);
  1107.         XSetErrorHandler(xerrordummy);
  1108.         XSetCloseDownMode(dpy, DestroyAll);
  1109.         XKillClient(dpy, selmon->sel->win);
  1110.         XSync(dpy, False);
  1111.         XSetErrorHandler(xerror);
  1112.         XUngrabServer(dpy);
  1113.     }
  1114. }
  1115.  
  1116. void
  1117. manage(Window w, XWindowAttributes *wa) {
  1118.     static Client cz;
  1119.     Client *c, *t = NULL;
  1120.     Window trans = None;
  1121.     XWindowChanges wc;
  1122.  
  1123.     if(!(c = malloc(sizeof(Client))))
  1124.         die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  1125.     *c = cz;
  1126.     c->win = w;
  1127.     updatetitle(c);
  1128.     if(XGetTransientForHint(dpy, w, &trans))
  1129.         t = wintoclient(trans);
  1130.     if(t) {
  1131.         c->mon = t->mon;
  1132.         c->tags = t->tags;
  1133.     }
  1134.     else {
  1135.         c->mon = selmon;
  1136.         applyrules(c);
  1137.     }
  1138.     /* geometry */
  1139.     c->x = c->oldx = wa->x + c->mon->wx;
  1140.     c->y = c->oldy = wa->y + c->mon->wy;
  1141.     c->w = c->oldw = wa->width;
  1142.     c->h = c->oldh = wa->height;
  1143.     c->oldbw = wa->border_width;
  1144.     if(c->w == c->mon->mw && c->h == c->mon->mh) {
  1145.         c->isfloating = 1;
  1146.         c->x = c->mon->mx;
  1147.         c->y = c->mon->my;
  1148.         c->bw = 0;
  1149.     }
  1150.     else {
  1151.         if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1152.             c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1153.         if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1154.             c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1155.         c->x = MAX(c->x, c->mon->mx);
  1156.         /* only fix client y-offset, if the client center might cover the bar */
  1157.         c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  1158.                    && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1159.         c->bw = borderpx;
  1160.     }
  1161.     wc.border_width = c->bw;
  1162.     XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1163.     XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1164.     configure(c); /* propagates border_width, if size doesn't change */
  1165.     updatesizehints(c);
  1166.     XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1167.     grabbuttons(c, False);
  1168.     if(!c->isfloating)
  1169.         c->isfloating = c->oldstate = trans != None || c->isfixed;
  1170.     if(c->isfloating)
  1171.         XRaiseWindow(dpy, c->win);
  1172.     attach(c);
  1173.     attachstack(c);
  1174.     XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1175.     XMapWindow(dpy, c->win);
  1176.     setclientstate(c, NormalState);
  1177.     arrange(c->mon);
  1178. }
  1179.  
  1180. void
  1181. mappingnotify(XEvent *e) {
  1182.     XMappingEvent *ev = &e->xmapping;
  1183.  
  1184.     XRefreshKeyboardMapping(ev);
  1185.     if(ev->request == MappingKeyboard)
  1186.         grabkeys();
  1187. }
  1188.  
  1189. void
  1190. maprequest(XEvent *e) {
  1191.     static XWindowAttributes wa;
  1192.     XMapRequestEvent *ev = &e->xmaprequest;
  1193.  
  1194.     if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1195.         return;
  1196.     if(wa.override_redirect)
  1197.         return;
  1198.     if(!wintoclient(ev->window))
  1199.         manage(ev->window, &wa);
  1200. }
  1201.  
  1202. void
  1203. monocle(Monitor *m) {
  1204.     unsigned int n = 0;
  1205.     Client *c;
  1206.  
  1207.     for(c = m->clients; c; c = c->next)
  1208.         if(ISVISIBLE(c))
  1209.             n++;
  1210.     if(n > 0) /* override layout symbol */
  1211.         snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1212.     for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1213.         resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1214. }
  1215.  
  1216. void
  1217. movemouse(const Arg *arg) {
  1218.     int x, y, ocx, ocy, nx, ny;
  1219.     Client *c;
  1220.     Monitor *m;
  1221.     XEvent ev;
  1222.  
  1223.     if(!(c = selmon->sel))
  1224.         return;
  1225.     restack(selmon);
  1226.     ocx = c->x;
  1227.     ocy = c->y;
  1228.     if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1229.     None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1230.         return;
  1231.     if(!getrootptr(&x, &y))
  1232.         return;
  1233.     do {
  1234.         XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1235.         switch (ev.type) {
  1236.         case ConfigureRequest:
  1237.         case Expose:
  1238.         case MapRequest:
  1239.             handler[ev.type](&ev);
  1240.             break;
  1241.         case MotionNotify:
  1242.             nx = ocx + (ev.xmotion.x - x);
  1243.             ny = ocy + (ev.xmotion.y - y);
  1244.             if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1245.             && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1246.                 if(abs(selmon->wx - nx) < snap)
  1247.                     nx = selmon->wx;
  1248.                 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1249.                     nx = selmon->wx + selmon->ww - WIDTH(c);
  1250.                 if(abs(selmon->wy - ny) < snap)
  1251.                     ny = selmon->wy;
  1252.                 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1253.                     ny = selmon->wy + selmon->wh - HEIGHT(c);
  1254.                 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1255.                 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1256.                     togglefloating(NULL);
  1257.             }
  1258.             if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1259.                 resize(c, nx, ny, c->w, c->h, True);
  1260.             break;
  1261.         }
  1262.     } while(ev.type != ButtonRelease);
  1263.     XUngrabPointer(dpy, CurrentTime);
  1264.     if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1265.         sendmon(c, m);
  1266.         selmon = m;
  1267.         focus(NULL);
  1268.     }
  1269. }
  1270.  
  1271. Client *
  1272. nexttiled(Client *c) {
  1273.     for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1274.     return c;
  1275. }
  1276.  
  1277. Monitor *
  1278. ptrtomon(int x, int y) {
  1279.     Monitor *m;
  1280.  
  1281.     for(m = mons; m; m = m->next)
  1282.         if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1283.             return m;
  1284.     return selmon;
  1285. }
  1286.  
  1287. void
  1288. propertynotify(XEvent *e) {
  1289.     Client *c;
  1290.     Window trans;
  1291.     XPropertyEvent *ev = &e->xproperty;
  1292.  
  1293.     if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1294.         updatestatus();
  1295.     else if(ev->state == PropertyDelete)
  1296.         return; /* ignore */
  1297.     else if((c = wintoclient(ev->window))) {
  1298.         switch (ev->atom) {
  1299.         default: break;
  1300.         case XA_WM_TRANSIENT_FOR:
  1301.             XGetTransientForHint(dpy, c->win, &trans);
  1302.             if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
  1303.                 arrange(c->mon);
  1304.             break;
  1305.         case XA_WM_NORMAL_HINTS:
  1306.             updatesizehints(c);
  1307.             break;
  1308.         case XA_WM_HINTS:
  1309.             updatewmhints(c);
  1310.             drawbars();
  1311.             break;
  1312.         }
  1313.         if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1314.             updatetitle(c);
  1315.             if(c == c->mon->sel)
  1316.                 drawbar(c->mon);
  1317.         }
  1318.     }
  1319. }
  1320.  
  1321. void
  1322. clientmessage(XEvent *e) {
  1323.     XClientMessageEvent *cme = &e->xclient;
  1324.     Client *c;
  1325.  
  1326.     if((c = wintoclient(cme->window))
  1327.     && (cme->message_type == netatom[NetWMState] && cme->data.l[1] == netatom[NetWMFullscreen]))
  1328.     {
  1329.         if(cme->data.l[0]) {
  1330.             XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
  1331.                             PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1332.             c->oldstate = c->isfloating;
  1333.             c->oldbw = c->bw;
  1334.             c->bw = 0;
  1335.             c->isfloating = 1;
  1336.             resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1337.             XRaiseWindow(dpy, c->win);
  1338.         }
  1339.         else {
  1340.             XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
  1341.                             PropModeReplace, (unsigned char*)0, 0);
  1342.             c->isfloating = c->oldstate;
  1343.             c->bw = c->oldbw;
  1344.             c->x = c->oldx;
  1345.             c->y = c->oldy;
  1346.             c->w = c->oldw;
  1347.             c->h = c->oldh;
  1348.             resizeclient(c, c->x, c->y, c->w, c->h);
  1349.             arrange(c->mon);
  1350.         }
  1351.     }
  1352. }
  1353.  
  1354. void
  1355. quit(const Arg *arg) {
  1356.     running = False;
  1357. }
  1358.  
  1359. void
  1360. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1361.     if(applysizehints(c, &x, &y, &w, &h, interact))
  1362.         resizeclient(c, x, y, w, h);
  1363. }
  1364.  
  1365. void
  1366. resizeclient(Client *c, int x, int y, int w, int h) {
  1367.     XWindowChanges wc;
  1368.  
  1369.     c->oldx = c->x; c->x = wc.x = x;
  1370.     c->oldy = c->y; c->y = wc.y = y;
  1371.     c->oldw = c->w; c->w = wc.width = w;
  1372.     c->oldh = c->h; c->h = wc.height = h;
  1373.     wc.border_width = c->bw;
  1374.     XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1375.     configure(c);
  1376.     XSync(dpy, False);
  1377. }
  1378.  
  1379. void
  1380. resizemouse(const Arg *arg) {
  1381.     int ocx, ocy;
  1382.     int nw, nh;
  1383.     Client *c;
  1384.     Monitor *m;
  1385.     XEvent ev;
  1386.  
  1387.     if(!(c = selmon->sel))
  1388.         return;
  1389.     restack(selmon);
  1390.     ocx = c->x;
  1391.     ocy = c->y;
  1392.     if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1393.                     None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1394.         return;
  1395.     XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1396.     do {
  1397.         XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1398.         switch(ev.type) {
  1399.         case ConfigureRequest:
  1400.         case Expose:
  1401.         case MapRequest:
  1402.             handler[ev.type](&ev);
  1403.             break;
  1404.         case MotionNotify:
  1405.             nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1406.             nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1407.             if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1408.             && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
  1409.             {
  1410.                 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1411.                 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1412.                     togglefloating(NULL);
  1413.             }
  1414.             if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1415.                 resize(c, c->x, c->y, nw, nh, True);
  1416.             break;
  1417.         }
  1418.     } while(ev.type != ButtonRelease);
  1419.     XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1420.     XUngrabPointer(dpy, CurrentTime);
  1421.     while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1422.     if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1423.         sendmon(c, m);
  1424.         selmon = m;
  1425.         focus(NULL);
  1426.     }
  1427. }
  1428.  
  1429. void
  1430. restack(Monitor *m) {
  1431.     Client *c;
  1432.     XEvent ev;
  1433.     XWindowChanges wc;
  1434.  
  1435.     drawbar(m);
  1436.     if(!m->sel)
  1437.         return;
  1438.     if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1439.         XRaiseWindow(dpy, m->sel->win);
  1440.     if(m->lt[m->sellt]->arrange) {
  1441.         wc.stack_mode = Below;
  1442.         wc.sibling = m->barwin;
  1443.         for(c = m->stack; c; c = c->snext)
  1444.             if(!c->isfloating && ISVISIBLE(c)) {
  1445.                 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1446.                 wc.sibling = c->win;
  1447.             }
  1448.     }
  1449.     XSync(dpy, False);
  1450.     while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1451. }
  1452.  
  1453. void
  1454. run(void) {
  1455.     XEvent ev;
  1456.     /* main event loop */
  1457.     XSync(dpy, False);
  1458.     while(running && !XNextEvent(dpy, &ev)) {
  1459.         if(handler[ev.type])
  1460.             handler[ev.type](&ev); /* call handler */
  1461.     }
  1462. }
  1463.  
  1464. void
  1465. scan(void) {
  1466.     unsigned int i, num;
  1467.     Window d1, d2, *wins = NULL;
  1468.     XWindowAttributes wa;
  1469.  
  1470.     if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1471.         for(i = 0; i < num; i++) {
  1472.             if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1473.             || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1474.                 continue;
  1475.             if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1476.                 manage(wins[i], &wa);
  1477.         }
  1478.         for(i = 0; i < num; i++) { /* now the transients */
  1479.             if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1480.                 continue;
  1481.             if(XGetTransientForHint(dpy, wins[i], &d1)
  1482.             && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1483.                 manage(wins[i], &wa);
  1484.         }
  1485.         if(wins)
  1486.             XFree(wins);
  1487.     }
  1488. }
  1489.  
  1490. void
  1491. sendmon(Client *c, Monitor *m) {
  1492.     if(c->mon == m)
  1493.         return;
  1494.     unfocus(c, True);
  1495.     detach(c);
  1496.     detachstack(c);
  1497.     c->mon = m;
  1498.     c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1499.     attach(c);
  1500.     attachstack(c);
  1501.     focus(NULL);
  1502.     arrange(NULL);
  1503. }
  1504.  
  1505. void
  1506. setclientstate(Client *c, long state) {
  1507.     long data[] = { state, None };
  1508.  
  1509.     XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1510.             PropModeReplace, (unsigned char *)data, 2);
  1511. }
  1512.  
  1513. void
  1514. setlayout(const Arg *arg) {
  1515.     if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1516.         selmon->sellt ^= 1;
  1517.     if(arg && arg->v)
  1518.         selmon->lt[selmon->sellt] = selmon->lts[selmon->curtag] = (Layout *)arg->v;
  1519.     strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1520.     if(selmon->sel)
  1521.         arrange(selmon);
  1522.     else
  1523.         drawbar(selmon);
  1524. }
  1525.  
  1526. /* arg > 1.0 will set mfact absolutly */
  1527. void
  1528. setmfact(const Arg *arg) {
  1529.     float f;
  1530.  
  1531.     if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1532.         return;
  1533.     f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1534.     if(f < 0.1 || f > 0.9)
  1535.         return;
  1536.     selmon->mfact = selmon->mfacts[selmon->curtag] = f;
  1537.     arrange(selmon);
  1538. }
  1539.  
  1540. void
  1541. setup(void) {
  1542.     XSetWindowAttributes wa;
  1543.  
  1544.     /* clean up any zombies immediately */
  1545.     sigchld(0);
  1546.  
  1547.     /* init screen */
  1548.     screen = DefaultScreen(dpy);
  1549.     root = RootWindow(dpy, screen);
  1550.     initfont(font);
  1551.     sw = DisplayWidth(dpy, screen);
  1552.     sh = DisplayHeight(dpy, screen);
  1553.     bh = dc.h = dc.font.height + 2;
  1554.     updategeom();
  1555.     /* init atoms */
  1556.     wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1557.     wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1558.     wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1559.     netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1560.     netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1561.     netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1562.     netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1563.     /* init cursors */
  1564.     cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1565.     cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1566.     cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1567.     /* init appearance */
  1568.     dc.norm[ColBorder] = getcolor(normbordercolor);
  1569.     dc.norm[ColBG] = getcolor(normbgcolor);
  1570.     dc.norm[ColFG] = getcolor(normfgcolor);
  1571.     dc.sel[ColBorder] = getcolor(selbordercolor);
  1572.     dc.sel[ColBG] = getcolor(selbgcolor);
  1573.     dc.sel[ColFG] = getcolor(selfgcolor);
  1574.     dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1575.     dc.gc = XCreateGC(dpy, root, 0, NULL);
  1576.     XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1577.     if(!dc.font.set)
  1578.         XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1579.     updatebars();
  1580.     updatestatus();
  1581.     /* EWMH support per view */
  1582.     XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1583.             PropModeReplace, (unsigned char *) netatom, NetLast);
  1584.     /* select for events */
  1585.     wa.cursor = cursor[CurNormal];
  1586.     wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1587.                     |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1588.                     |PropertyChangeMask;
  1589.     XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1590.     XSelectInput(dpy, root, wa.event_mask);
  1591.     grabkeys();
  1592. }
  1593.  
  1594. void
  1595. showhide(Client *c) {
  1596.     if(!c)
  1597.         return;
  1598.     if(ISVISIBLE(c)) { /* show clients top down */
  1599.         XMoveWindow(dpy, c->win, c->x, c->y);
  1600.         if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
  1601.             resize(c, c->x, c->y, c->w, c->h, False);
  1602.         showhide(c->snext);
  1603.     }
  1604.     else { /* hide clients bottom up */
  1605.         showhide(c->snext);
  1606.         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1607.     }
  1608. }
  1609.  
  1610.  
  1611. void
  1612. sigchld(int unused) {
  1613.     if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1614.         die("Can't install SIGCHLD handler");
  1615.     while(0 < waitpid(-1, NULL, WNOHANG));
  1616. }
  1617.  
  1618. void
  1619. spawn(const Arg *arg) {
  1620.     if(fork() == 0) {
  1621.         if(dpy)
  1622.             close(ConnectionNumber(dpy));
  1623.         setsid();
  1624.         execvp(((char **)arg->v)[0], (char **)arg->v);
  1625.         fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1626.         perror(" failed");
  1627.         exit(0);
  1628.     }
  1629. }
  1630.  
  1631. void
  1632. tag(const Arg *arg) {
  1633.     if(selmon->sel && arg->ui & TAGMASK) {
  1634.         selmon->sel->tags = arg->ui & TAGMASK;
  1635.         arrange(selmon);
  1636.     }
  1637. }
  1638.  
  1639. void
  1640. tagmon(const Arg *arg) {
  1641.     if(!selmon->sel || !mons->next)
  1642.         return;
  1643.     sendmon(selmon->sel, dirtomon(arg->i));
  1644. }
  1645.  
  1646. int
  1647. textnw(const char *text, unsigned int len) {
  1648.     XRectangle r;
  1649.  
  1650.     if(dc.font.set) {
  1651.         XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1652.         return r.width;
  1653.     }
  1654.     return XTextWidth(dc.font.xfont, text, len);
  1655. }
  1656.  
  1657. void
  1658. tile(Monitor *m) {
  1659.     int x, y, h, w, mw;
  1660.     unsigned int i, n;
  1661.     Client *c;
  1662.  
  1663.     for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1664.     if(n == 0)
  1665.         return;
  1666.     /* master */
  1667.     c = nexttiled(m->clients);
  1668.     mw = m->mfact * m->ww;
  1669.     resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
  1670.     if(--n == 0)
  1671.         return;
  1672.     /* tile stack */
  1673.     x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1674.     y = m->wy;
  1675.     w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1676.     h = m->wh / n;
  1677.     if(h < bh)
  1678.         h = m->wh;
  1679.     for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1680.         resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1681.                ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
  1682.         if(h != m->wh)
  1683.             y = c->y + HEIGHT(c);
  1684.     }
  1685. }
  1686.  
  1687. void
  1688. togglebar(const Arg *arg) {
  1689.     selmon->showbar = selmon->showbars[selmon->curtag] = !selmon->showbar;
  1690.     updatebarpos(selmon);
  1691.     XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1692.     arrange(selmon);
  1693. }
  1694.  
  1695. void
  1696. togglefloating(const Arg *arg) {
  1697.     if(!selmon->sel)
  1698.         return;
  1699.     selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1700.     if(selmon->sel->isfloating)
  1701.         resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1702.                selmon->sel->w, selmon->sel->h, False);
  1703.     arrange(selmon);
  1704. }
  1705.  
  1706. void
  1707. toggletag(const Arg *arg) {
  1708.     unsigned int newtags;
  1709.     unsigned int i;
  1710.  
  1711.     if(!selmon->sel)
  1712.         return;
  1713.     newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1714.     if(newtags) {
  1715.         selmon->sel->tags = newtags;
  1716.         if(newtags == ~0) {
  1717.             selmon->prevtag = selmon->curtag;
  1718.             selmon->curtag = 0;
  1719.         }
  1720.         if(!(newtags & 1 << (selmon->curtag - 1))) {
  1721.             selmon->prevtag = selmon->curtag;
  1722.             for (i=0; !(newtags & 1 << i); i++);
  1723.             selmon->curtag = i + 1;
  1724.         }
  1725.         selmon->sel->tags = newtags;
  1726.         selmon->lt[selmon->sellt] = selmon->lts[selmon->curtag];
  1727.         selmon->mfact = selmon->mfacts[selmon->curtag];
  1728.         if (selmon->showbar != selmon->showbars[selmon->curtag])
  1729.             togglebar(NULL);
  1730.         arrange(selmon);
  1731.     }
  1732. }
  1733.  
  1734. void
  1735. toggleview(const Arg *arg) {
  1736.     unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1737.  
  1738.     if(newtagset) {
  1739.         selmon->tagset[selmon->seltags] = newtagset;
  1740.         arrange(selmon);
  1741.     }
  1742. }
  1743.  
  1744. void
  1745. unfocus(Client *c, Bool setfocus) {
  1746.     if(!c)
  1747.         return;
  1748.     grabbuttons(c, False);
  1749.     XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1750.     if(setfocus)
  1751.         XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1752. }
  1753.  
  1754. void
  1755. unmanage(Client *c, Bool destroyed) {
  1756.     Monitor *m = c->mon;
  1757.     XWindowChanges wc;
  1758.  
  1759.     /* The server grab construct avoids race conditions. */
  1760.     detach(c);
  1761.     detachstack(c);
  1762.     if(!destroyed) {
  1763.         wc.border_width = c->oldbw;
  1764.         XGrabServer(dpy);
  1765.         XSetErrorHandler(xerrordummy);
  1766.         XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1767.         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1768.         setclientstate(c, WithdrawnState);
  1769.         XSync(dpy, False);
  1770.         XSetErrorHandler(xerror);
  1771.         XUngrabServer(dpy);
  1772.     }
  1773.     free(c);
  1774.     focus(NULL);
  1775.     arrange(m);
  1776. }
  1777.  
  1778. void
  1779. unmapnotify(XEvent *e) {
  1780.     Client *c;
  1781.     XUnmapEvent *ev = &e->xunmap;
  1782.  
  1783.     if((c = wintoclient(ev->window)))
  1784.         unmanage(c, False);
  1785. }
  1786.  
  1787. void
  1788. updatebars(void) {
  1789.     Monitor *m;
  1790.     XSetWindowAttributes wa;
  1791.  
  1792.     wa.override_redirect = True;
  1793.     wa.background_pixmap = ParentRelative;
  1794.     wa.event_mask = ButtonPressMask|ExposureMask;
  1795.     for(m = mons; m; m = m->next) {
  1796.         m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1797.                                   CopyFromParent, DefaultVisual(dpy, screen),
  1798.                                   CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1799.         XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1800.         XMapRaised(dpy, m->barwin);
  1801.     }
  1802. }
  1803.  
  1804. void
  1805. updatebarpos(Monitor *m) {
  1806.     m->wy = m->my;
  1807.     m->wh = m->mh;
  1808.     if(m->showbar) {
  1809.         m->wh -= bh;
  1810.         m->by = m->topbar ? m->wy : m->wy + m->wh;
  1811.         m->wy = m->topbar ? m->wy + bh : m->wy;
  1812.     }
  1813.     else
  1814.         m->by = -bh;
  1815. }
  1816.  
  1817. Bool
  1818. updategeom(void) {
  1819.     Bool dirty = False;
  1820.  
  1821. #ifdef XINERAMA
  1822.     if(XineramaIsActive(dpy)) {
  1823.         int i, j, n, nn;
  1824.         Client *c;
  1825.         Monitor *m;
  1826.         XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1827.         XineramaScreenInfo *unique = NULL;
  1828.  
  1829.         info = XineramaQueryScreens(dpy, &nn);
  1830.         for(n = 0, m = mons; m; m = m->next, n++);
  1831.         /* only consider unique geometries as separate screens */
  1832.         if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
  1833.             die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
  1834.         for(i = 0, j = 0; i < nn; i++)
  1835.             if(isuniquegeom(unique, j, &info[i]))
  1836.                 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1837.         XFree(info);
  1838.         nn = j;
  1839.         if(n <= nn) {
  1840.             for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1841.                 for(m = mons; m && m->next; m = m->next);
  1842.                 if(m)
  1843.                     m->next = createmon();
  1844.                 else
  1845.                     mons = createmon();
  1846.             }
  1847.             for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1848.                 if(i >= n
  1849.                 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1850.                     || unique[i].width != m->mw || unique[i].height != m->mh))
  1851.                 {
  1852.                     dirty = True;
  1853.                     m->num = i;
  1854.                     m->mx = m->wx = unique[i].x_org;
  1855.                     m->my = m->wy = unique[i].y_org;
  1856.                     m->mw = m->ww = unique[i].width;
  1857.                     m->mh = m->wh = unique[i].height;
  1858.                     updatebarpos(m);
  1859.                 }
  1860.         }
  1861.         else { /* less monitors available nn < n */
  1862.             for(i = nn; i < n; i++) {
  1863.                 for(m = mons; m && m->next; m = m->next);
  1864.                 while(m->clients) {
  1865.                     dirty = True;
  1866.                     c = m->clients;
  1867.                     m->clients = c->next;
  1868.                     detachstack(c);
  1869.                     c->mon = mons;
  1870.                     attach(c);
  1871.                     attachstack(c);
  1872.                 }
  1873.                 if(m == selmon)
  1874.                     selmon = mons;
  1875.                 cleanupmon(m);
  1876.             }
  1877.         }
  1878.         free(unique);
  1879.     }
  1880.     else
  1881. #endif /* XINERAMA */
  1882.     /* default monitor setup */
  1883.     {
  1884.         if(!mons)
  1885.             mons = createmon();
  1886.         if(mons->mw != sw || mons->mh != sh) {
  1887.             dirty = True;
  1888.             mons->mw = mons->ww = sw;
  1889.             mons->mh = mons->wh = sh;
  1890.             updatebarpos(mons);
  1891.         }
  1892.     }
  1893.     if(dirty) {
  1894.         selmon = mons;
  1895.         selmon = wintomon(root);
  1896.     }
  1897.     return dirty;
  1898. }
  1899.  
  1900. void
  1901. updatenumlockmask(void) {
  1902.     unsigned int i, j;
  1903.     XModifierKeymap *modmap;
  1904.  
  1905.     numlockmask = 0;
  1906.     modmap = XGetModifierMapping(dpy);
  1907.     for(i = 0; i < 8; i++)
  1908.         for(j = 0; j < modmap->max_keypermod; j++)
  1909.             if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1910.                == XKeysymToKeycode(dpy, XK_Num_Lock))
  1911.                 numlockmask = (1 << i);
  1912.     XFreeModifiermap(modmap);
  1913. }
  1914.  
  1915. void
  1916. updatesizehints(Client *c) {
  1917.     long msize;
  1918.     XSizeHints size;
  1919.  
  1920.     if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1921.         /* size is uninitialized, ensure that size.flags aren't used */
  1922.         size.flags = PSize;
  1923.     if(size.flags & PBaseSize) {
  1924.         c->basew = size.base_width;
  1925.         c->baseh = size.base_height;
  1926.     }
  1927.     else if(size.flags & PMinSize) {
  1928.         c->basew = size.min_width;
  1929.         c->baseh = size.min_height;
  1930.     }
  1931.     else
  1932.         c->basew = c->baseh = 0;
  1933.     if(size.flags & PResizeInc) {
  1934.         c->incw = size.width_inc;
  1935.         c->inch = size.height_inc;
  1936.     }
  1937.     else
  1938.         c->incw = c->inch = 0;
  1939.     if(size.flags & PMaxSize) {
  1940.         c->maxw = size.max_width;
  1941.         c->maxh = size.max_height;
  1942.     }
  1943.     else
  1944.         c->maxw = c->maxh = 0;
  1945.     if(size.flags & PMinSize) {
  1946.         c->minw = size.min_width;
  1947.         c->minh = size.min_height;
  1948.     }
  1949.     else if(size.flags & PBaseSize) {
  1950.         c->minw = size.base_width;
  1951.         c->minh = size.base_height;
  1952.     }
  1953.     else
  1954.         c->minw = c->minh = 0;
  1955.     if(size.flags & PAspect) {
  1956.         c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1957.         c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1958.     }
  1959.     else
  1960.         c->maxa = c->mina = 0.0;
  1961.     c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1962.                  && c->maxw == c->minw && c->maxh == c->minh);
  1963. }
  1964.  
  1965. void
  1966. updatetitle(Client *c) {
  1967.     if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1968.         gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1969.     if(c->name[0] == '\0') /* hack to mark broken clients */
  1970.         strcpy(c->name, broken);
  1971. }
  1972.  
  1973. void
  1974. updatestatus(void) {
  1975.     if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1976.         strcpy(stext, "dwm-"VERSION);
  1977.     drawbar(selmon);
  1978. }
  1979.  
  1980. void
  1981. updatewmhints(Client *c) {
  1982.     XWMHints *wmh;
  1983.  
  1984.     if((wmh = XGetWMHints(dpy, c->win))) {
  1985.         if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1986.             wmh->flags &= ~XUrgencyHint;
  1987.             XSetWMHints(dpy, c->win, wmh);
  1988.         }
  1989.         else
  1990.             c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1991.         XFree(wmh);
  1992.     }
  1993. }
  1994.  
  1995. void
  1996. view(const Arg *arg) {
  1997.     unsigned int i;
  1998.  
  1999.     if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  2000.         return;
  2001.     selmon->seltags ^= 1; /* toggle sel tagset */
  2002.     if(arg->ui & TAGMASK) {
  2003.         selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  2004.         selmon->prevtag = selmon->curtag;
  2005.         if(arg->ui == ~0)
  2006.             selmon->curtag = 0;
  2007.         else {
  2008.             for (i=0; !(arg->ui & 1 << i); i++);
  2009.             selmon->curtag = i + 1;
  2010.         }
  2011.     } else {
  2012.         selmon->prevtag= selmon->curtag ^ selmon->prevtag;
  2013.         selmon->curtag^= selmon->prevtag;
  2014.         selmon->prevtag= selmon->curtag ^ selmon->prevtag;
  2015.     }
  2016.     selmon->lt[selmon->sellt]= selmon->lts[selmon->curtag];
  2017.     selmon->mfact = selmon->mfacts[selmon->curtag];
  2018.     if(selmon->showbar != selmon->showbars[selmon->curtag])
  2019.         togglebar(NULL);
  2020.     arrange(selmon);
  2021. }
  2022.  
  2023. Client *
  2024. wintoclient(Window w) {
  2025.     Client *c;
  2026.     Monitor *m;
  2027.  
  2028.     for(m = mons; m; m = m->next)
  2029.         for(c = m->clients; c; c = c->next)
  2030.             if(c->win == w)
  2031.                 return c;
  2032.     return NULL;
  2033. }
  2034.  
  2035. Monitor *
  2036. wintomon(Window w) {
  2037.     int x, y;
  2038.     Client *c;
  2039.     Monitor *m;
  2040.  
  2041.     if(w == root && getrootptr(&x, &y))
  2042.         return ptrtomon(x, y);
  2043.     for(m = mons; m; m = m->next)
  2044.         if(w == m->barwin)
  2045.             return m;
  2046.     if((c = wintoclient(w)))
  2047.         return c->mon;
  2048.     return selmon;
  2049. }
  2050.  
  2051. /* There's no way to check accesses to destroyed windows, thus those cases are
  2052.  * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
  2053.  * default error handler, which may call exit.  */
  2054. int
  2055. xerror(Display *dpy, XErrorEvent *ee) {
  2056.     if(ee->error_code == BadWindow
  2057.     || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  2058.     || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  2059.     || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  2060.     || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  2061.     || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  2062.     || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  2063.     || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  2064.     || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  2065.         return 0;
  2066.     fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  2067.             ee->request_code, ee->error_code);
  2068.     return xerrorxlib(dpy, ee); /* may call exit */
  2069. }
  2070.  
  2071. int
  2072. xerrordummy(Display *dpy, XErrorEvent *ee) {
  2073.     return 0;
  2074. }
  2075.  
  2076. /* Startup Error handler to check if another window manager
  2077.  * is already running. */
  2078. int
  2079. xerrorstart(Display *dpy, XErrorEvent *ee) {
  2080.     otherwm = True;
  2081.     return -1;
  2082. }
  2083.  
  2084. void
  2085. zoom(const Arg *arg) {
  2086.     Client *c = selmon->sel;
  2087.  
  2088.     if(!selmon->lt[selmon->sellt]->arrange
  2089.     || selmon->lt[selmon->sellt]->arrange == monocle
  2090.     || (selmon->sel && selmon->sel->isfloating))
  2091.         return;
  2092.     if(c == nexttiled(selmon->clients))
  2093.         if(!c || !(c = nexttiled(c->next)))
  2094.             return;
  2095.     detach(c);
  2096.     attach(c);
  2097.     focus(c);
  2098.     arrange(c->mon);
  2099. }
  2100.  
  2101. int
  2102. main(int argc, char *argv[]) {
  2103.     if(argc == 2 && !strcmp("-v", argv[1]))
  2104.         die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
  2105.     else if(argc != 1)
  2106.         die("usage: dwm [-v]\n");
  2107.     if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  2108.         fputs("warning: no locale support\n", stderr);
  2109.     if(!(dpy = XOpenDisplay(NULL)))
  2110.         die("dwm: cannot open display\n");
  2111.     checkotherwm();
  2112.     setup();
  2113.     scan();
  2114.     run();
  2115.     cleanup();
  2116.     XCloseDisplay(dpy);
  2117.     return 0;
  2118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement