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.

2154 lines
52 KiB

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