Guest User

dwm.c

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