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.

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