My build of dwm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

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