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.
 
 
 
 
 

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