apoorv569

dwm.c new

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