Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2213 lines
53 KiB

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