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.

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