Guest User

dwm.c

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