Advertisement
Guest User

Untitled

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