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.

1966 lines
47 KiB

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