Configuration of dwm for Mac Computers
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.

2146 lines
51 KiB

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