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.

1975 lines
47 KiB

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