My build of dwm
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

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