My build of dwm
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

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