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.

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