Configuration file for DWM on MacBook Air
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.

2165 lines
52 KiB

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