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.

2142 lines
51 KiB

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