My build of dwm
 
 
 
 
 

2665 rindas
63 KiB

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