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.

1973 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 Monitor *idxtomon(unsigned int n);
  172. static void initfont(const char *fontstr);
  173. static Bool isprotodel(Client *c);
  174. static void keypress(XEvent *e);
  175. static void killclient(const Arg *arg);
  176. static void manage(Window w, XWindowAttributes *wa);
  177. static void mappingnotify(XEvent *e);
  178. static void maprequest(XEvent *e);
  179. static void monocle(Monitor *m);
  180. static void movemouse(const Arg *arg);
  181. static Client *nexttiled(Client *c);
  182. static Monitor *pointertomon(int x, int y);
  183. static void propertynotify(XEvent *e);
  184. static void quit(const Arg *arg);
  185. static void resize(Client *c, int x, int y, int w, int h);
  186. static void resizemouse(const Arg *arg);
  187. static void restack(Monitor *m);
  188. static void run(void);
  189. static void scan(void);
  190. static void sendmon(Client *c, Monitor *m);
  191. static void setclientstate(Client *c, long state);
  192. static void setlayout(const Arg *arg);
  193. static void setmfact(const Arg *arg);
  194. static void setup(void);
  195. static void showhide(Client *c);
  196. static void sigchld(int signal);
  197. static void spawn(const Arg *arg);
  198. static void tag(const Arg *arg);
  199. static int textnw(const char *text, unsigned int len);
  200. static void tile(Monitor *);
  201. static void togglebar(const Arg *arg);
  202. static void togglefloating(const Arg *arg);
  203. static void toggletag(const Arg *arg);
  204. static void toggleview(const Arg *arg);
  205. static void unfocus(Client *c);
  206. static void unmanage(Client *c);
  207. static void unmapnotify(XEvent *e);
  208. static void updategeom(void);
  209. static void updatebarpos(Monitor *m);
  210. static void updatebars(void);
  211. static void updatenumlockmask(void);
  212. static void updatesizehints(Client *c);
  213. static void updatestatus(void);
  214. static void updatetitle(Client *c);
  215. static void updatewmhints(Client *c);
  216. static void view(const Arg *arg);
  217. static Client *wintoclient(Window w);
  218. static Monitor *wintomon(Window w);
  219. static int xerror(Display *dpy, XErrorEvent *ee);
  220. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  221. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  222. static void zoom(const Arg *arg);
  223. #ifdef XINERAMA
  224. static void focusmon(const Arg *arg);
  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. Monitor *
  859. idxtomon(unsigned int n) {
  860. unsigned int i;
  861. Monitor *m;
  862. for(m = mons, i = 0; m && i != n; m = m->next, i++);
  863. return m;
  864. }
  865. void
  866. initfont(const char *fontstr) {
  867. char *def, **missing;
  868. int i, n;
  869. missing = NULL;
  870. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  871. if(missing) {
  872. while(n--)
  873. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  874. XFreeStringList(missing);
  875. }
  876. if(dc.font.set) {
  877. XFontSetExtents *font_extents;
  878. XFontStruct **xfonts;
  879. char **font_names;
  880. dc.font.ascent = dc.font.descent = 0;
  881. font_extents = XExtentsOfFontSet(dc.font.set);
  882. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  883. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  884. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  885. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  886. xfonts++;
  887. }
  888. }
  889. else {
  890. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  891. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  892. die("error, cannot load font: '%s'\n", fontstr);
  893. dc.font.ascent = dc.font.xfont->ascent;
  894. dc.font.descent = dc.font.xfont->descent;
  895. }
  896. dc.font.height = dc.font.ascent + dc.font.descent;
  897. }
  898. Bool
  899. isprotodel(Client *c) {
  900. int i, n;
  901. Atom *protocols;
  902. Bool ret = False;
  903. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  904. for(i = 0; !ret && i < n; i++)
  905. if(protocols[i] == wmatom[WMDelete])
  906. ret = True;
  907. XFree(protocols);
  908. }
  909. return ret;
  910. }
  911. void
  912. keypress(XEvent *e) {
  913. unsigned int i;
  914. KeySym keysym;
  915. XKeyEvent *ev;
  916. ev = &e->xkey;
  917. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  918. for(i = 0; i < LENGTH(keys); i++)
  919. if(keysym == keys[i].keysym
  920. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  921. && keys[i].func)
  922. keys[i].func(&(keys[i].arg));
  923. }
  924. void
  925. killclient(const Arg *arg) {
  926. XEvent ev;
  927. if(!selmon->sel)
  928. return;
  929. if(isprotodel(selmon->sel)) {
  930. ev.type = ClientMessage;
  931. ev.xclient.window = selmon->sel->win;
  932. ev.xclient.message_type = wmatom[WMProtocols];
  933. ev.xclient.format = 32;
  934. ev.xclient.data.l[0] = wmatom[WMDelete];
  935. ev.xclient.data.l[1] = CurrentTime;
  936. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  937. }
  938. else
  939. XKillClient(dpy, selmon->sel->win);
  940. }
  941. void
  942. manage(Window w, XWindowAttributes *wa) {
  943. static Client cz;
  944. Client *c, *t = NULL;
  945. Window trans = None;
  946. XWindowChanges wc;
  947. if(!(c = malloc(sizeof(Client))))
  948. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  949. *c = cz;
  950. c->win = w;
  951. if(XGetTransientForHint(dpy, w, &trans))
  952. t = wintoclient(trans);
  953. if(t) {
  954. c->mon = t->mon;
  955. c->tags = t->tags;
  956. }
  957. else {
  958. c->mon = selmon;
  959. applyrules(c);
  960. }
  961. /* geometry */
  962. c->x = wa->x + c->mon->wx;
  963. c->y = wa->y + c->mon->wy;
  964. c->w = wa->width;
  965. c->h = wa->height;
  966. c->oldbw = wa->border_width;
  967. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  968. c->x = c->mon->mx;
  969. c->y = c->mon->my;
  970. c->bw = 0;
  971. }
  972. else {
  973. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  974. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  975. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  976. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  977. c->x = MAX(c->x, c->mon->mx);
  978. /* only fix client y-offset, if the client center might cover the bar */
  979. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  980. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  981. c->bw = borderpx;
  982. }
  983. wc.border_width = c->bw;
  984. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  985. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  986. configure(c); /* propagates border_width, if size doesn't change */
  987. updatesizehints(c);
  988. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  989. grabbuttons(c, False);
  990. updatetitle(c);
  991. if(!c->isfloating)
  992. c->isfloating = trans != None || c->isfixed;
  993. if(c->isfloating)
  994. XRaiseWindow(dpy, c->win);
  995. attach(c);
  996. attachstack(c);
  997. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  998. XMapWindow(dpy, c->win);
  999. setclientstate(c, NormalState);
  1000. arrange();
  1001. }
  1002. void
  1003. mappingnotify(XEvent *e) {
  1004. XMappingEvent *ev = &e->xmapping;
  1005. XRefreshKeyboardMapping(ev);
  1006. if(ev->request == MappingKeyboard)
  1007. grabkeys();
  1008. }
  1009. void
  1010. maprequest(XEvent *e) {
  1011. static XWindowAttributes wa;
  1012. XMapRequestEvent *ev = &e->xmaprequest;
  1013. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1014. return;
  1015. if(wa.override_redirect)
  1016. return;
  1017. if(!wintoclient(ev->window))
  1018. manage(ev->window, &wa);
  1019. }
  1020. void
  1021. monocle(Monitor *m) {
  1022. Client *c;
  1023. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1024. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw);
  1025. }
  1026. void
  1027. movemouse(const Arg *arg) {
  1028. int x, y, ocx, ocy, nx, ny;
  1029. Client *c;
  1030. Monitor *m;
  1031. XEvent ev;
  1032. if(!(c = selmon->sel))
  1033. return;
  1034. restack(selmon);
  1035. ocx = c->x;
  1036. ocy = c->y;
  1037. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1038. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1039. return;
  1040. if(!getrootpointer(&x, &y))
  1041. return;
  1042. do {
  1043. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1044. switch (ev.type) {
  1045. case ConfigureRequest:
  1046. case Expose:
  1047. case MapRequest:
  1048. handler[ev.type](&ev);
  1049. break;
  1050. case MotionNotify:
  1051. nx = ocx + (ev.xmotion.x - x);
  1052. ny = ocy + (ev.xmotion.y - y);
  1053. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1054. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1055. if(abs(selmon->wx - nx) < snap)
  1056. nx = selmon->wx;
  1057. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1058. nx = selmon->wx + selmon->ww - WIDTH(c);
  1059. if(abs(selmon->wy - ny) < snap)
  1060. ny = selmon->wy;
  1061. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1062. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1063. if(!c->isfloating && lt[selmon->sellt]->arrange
  1064. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1065. togglefloating(NULL);
  1066. }
  1067. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1068. resize(c, nx, ny, c->w, c->h);
  1069. break;
  1070. }
  1071. }
  1072. while(ev.type != ButtonRelease);
  1073. XUngrabPointer(dpy, CurrentTime);
  1074. if((m = pointertomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1075. sendmon(c, m);
  1076. selmon = m;
  1077. focus(NULL);
  1078. }
  1079. }
  1080. Client *
  1081. nexttiled(Client *c) {
  1082. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1083. return c;
  1084. }
  1085. Monitor *
  1086. pointertomon(int x, int y) {
  1087. Monitor *m;
  1088. for(m = mons; m; m = m->next)
  1089. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1090. return m;
  1091. return mons;
  1092. }
  1093. void
  1094. propertynotify(XEvent *e) {
  1095. Client *c;
  1096. Window trans;
  1097. XPropertyEvent *ev = &e->xproperty;
  1098. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1099. updatestatus();
  1100. else if(ev->state == PropertyDelete)
  1101. return; /* ignore */
  1102. else if((c = wintoclient(ev->window))) {
  1103. switch (ev->atom) {
  1104. default: break;
  1105. case XA_WM_TRANSIENT_FOR:
  1106. XGetTransientForHint(dpy, c->win, &trans);
  1107. if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
  1108. arrange();
  1109. break;
  1110. case XA_WM_NORMAL_HINTS:
  1111. updatesizehints(c);
  1112. break;
  1113. case XA_WM_HINTS:
  1114. updatewmhints(c);
  1115. drawbars();
  1116. break;
  1117. }
  1118. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1119. updatetitle(c);
  1120. if(c == selmon->sel)
  1121. drawbars();
  1122. }
  1123. }
  1124. }
  1125. void
  1126. quit(const Arg *arg) {
  1127. running = False;
  1128. }
  1129. void
  1130. resize(Client *c, int x, int y, int w, int h) {
  1131. XWindowChanges wc;
  1132. if(applysizehints(c, &x, &y, &w, &h)) {
  1133. c->x = wc.x = x;
  1134. c->y = wc.y = y;
  1135. c->w = wc.width = w;
  1136. c->h = wc.height = h;
  1137. wc.border_width = c->bw;
  1138. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1139. configure(c);
  1140. XSync(dpy, False);
  1141. }
  1142. }
  1143. void
  1144. resizemouse(const Arg *arg) {
  1145. int ocx, ocy;
  1146. int nw, nh;
  1147. Client *c;
  1148. Monitor *m;
  1149. XEvent ev;
  1150. if(!(c = selmon->sel))
  1151. return;
  1152. restack(selmon);
  1153. ocx = c->x;
  1154. ocy = c->y;
  1155. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1156. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1157. return;
  1158. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1159. do {
  1160. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1161. switch(ev.type) {
  1162. case ConfigureRequest:
  1163. case Expose:
  1164. case MapRequest:
  1165. handler[ev.type](&ev);
  1166. break;
  1167. case MotionNotify:
  1168. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1169. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1170. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1171. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh) {
  1172. if(!c->isfloating && lt[selmon->sellt]->arrange
  1173. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1174. togglefloating(NULL);
  1175. }
  1176. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1177. resize(c, c->x, c->y, nw, nh);
  1178. break;
  1179. }
  1180. }
  1181. while(ev.type != ButtonRelease);
  1182. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1183. XUngrabPointer(dpy, CurrentTime);
  1184. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1185. if((m = pointertomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1186. sendmon(c, m);
  1187. selmon = m;
  1188. focus(NULL);
  1189. }
  1190. }
  1191. void
  1192. restack(Monitor *m) {
  1193. Client *c;
  1194. XEvent ev;
  1195. XWindowChanges wc;
  1196. drawbars();
  1197. if(!m->sel)
  1198. return;
  1199. if(m->sel->isfloating || !lt[m->sellt]->arrange)
  1200. XRaiseWindow(dpy, m->sel->win);
  1201. if(lt[m->sellt]->arrange) {
  1202. wc.stack_mode = Below;
  1203. wc.sibling = m->barwin;
  1204. for(c = m->stack; c; c = c->snext)
  1205. if(!c->isfloating && ISVISIBLE(c)) {
  1206. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1207. wc.sibling = c->win;
  1208. }
  1209. }
  1210. XSync(dpy, False);
  1211. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1212. }
  1213. void
  1214. run(void) {
  1215. XEvent ev;
  1216. /* main event loop */
  1217. XSync(dpy, False);
  1218. while(running && !XNextEvent(dpy, &ev)) {
  1219. if(handler[ev.type])
  1220. (handler[ev.type])(&ev); /* call handler */
  1221. }
  1222. }
  1223. void
  1224. scan(void) {
  1225. unsigned int i, num;
  1226. Window d1, d2, *wins = NULL;
  1227. XWindowAttributes wa;
  1228. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1229. for(i = 0; i < num; i++) {
  1230. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1231. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1232. continue;
  1233. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1234. manage(wins[i], &wa);
  1235. }
  1236. for(i = 0; i < num; i++) { /* now the transients */
  1237. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1238. continue;
  1239. if(XGetTransientForHint(dpy, wins[i], &d1)
  1240. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1241. manage(wins[i], &wa);
  1242. }
  1243. if(wins)
  1244. XFree(wins);
  1245. }
  1246. }
  1247. void
  1248. sendmon(Client *c, Monitor *m) {
  1249. if(c->mon == m)
  1250. return;
  1251. detach(c);
  1252. detachstack(c);
  1253. c->mon = m;
  1254. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1255. attach(c);
  1256. attachstack(c);
  1257. focus(NULL);
  1258. arrange();
  1259. }
  1260. void
  1261. setclientstate(Client *c, long state) {
  1262. long data[] = {state, None};
  1263. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1264. PropModeReplace, (unsigned char *)data, 2);
  1265. }
  1266. void
  1267. setlayout(const Arg *arg) {
  1268. if(!arg || !arg->v || arg->v != lt[selmon->sellt])
  1269. selmon->sellt ^= 1;
  1270. if(arg && arg->v)
  1271. lt[selmon->sellt] = (Layout *)arg->v;
  1272. if(selmon->sel)
  1273. arrange();
  1274. else
  1275. drawbars();
  1276. }
  1277. /* arg > 1.0 will set mfact absolutly */
  1278. void
  1279. setmfact(const Arg *arg) {
  1280. float f;
  1281. if(!arg || !lt[selmon->sellt]->arrange)
  1282. return;
  1283. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1284. if(f < 0.1 || f > 0.9)
  1285. return;
  1286. selmon->mfact = f;
  1287. arrange();
  1288. }
  1289. void
  1290. setup(void) {
  1291. unsigned int i;
  1292. int w;
  1293. XSetWindowAttributes wa;
  1294. /* init screen */
  1295. screen = DefaultScreen(dpy);
  1296. root = RootWindow(dpy, screen);
  1297. initfont(font);
  1298. sw = DisplayWidth(dpy, screen);
  1299. sh = DisplayHeight(dpy, screen);
  1300. bh = dc.h = dc.font.height + 2;
  1301. lt[0] = &layouts[0];
  1302. lt[1] = &layouts[1 % LENGTH(layouts)];
  1303. updategeom();
  1304. /* init atoms */
  1305. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1306. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1307. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1308. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1309. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1310. /* init cursors */
  1311. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1312. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1313. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1314. /* init appearance */
  1315. dc.norm[ColBorder] = getcolor(normbordercolor);
  1316. dc.norm[ColBG] = getcolor(normbgcolor);
  1317. dc.norm[ColFG] = getcolor(normfgcolor);
  1318. dc.sel[ColBorder] = getcolor(selbordercolor);
  1319. dc.sel[ColBG] = getcolor(selbgcolor);
  1320. dc.sel[ColFG] = getcolor(selfgcolor);
  1321. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1322. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1323. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1324. if(!dc.font.set)
  1325. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1326. /* init bars */
  1327. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1328. w = TEXTW(layouts[i].symbol);
  1329. blw = MAX(blw, w);
  1330. }
  1331. updatebars();
  1332. updatestatus();
  1333. /* EWMH support per view */
  1334. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1335. PropModeReplace, (unsigned char *) netatom, NetLast);
  1336. /* select for events */
  1337. wa.cursor = cursor[CurNormal];
  1338. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1339. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1340. |PropertyChangeMask;
  1341. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1342. XSelectInput(dpy, root, wa.event_mask);
  1343. grabkeys();
  1344. }
  1345. void
  1346. showhide(Client *c) {
  1347. if(!c)
  1348. return;
  1349. if(ISVISIBLE(c)) { /* show clients top down */
  1350. XMoveWindow(dpy, c->win, c->x, c->y);
  1351. if(!lt[c->mon->sellt]->arrange || c->isfloating)
  1352. resize(c, c->x, c->y, c->w, c->h);
  1353. showhide(c->snext);
  1354. }
  1355. else { /* hide clients bottom up */
  1356. showhide(c->snext);
  1357. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1358. }
  1359. }
  1360. void
  1361. sigchld(int signal) {
  1362. while(0 < waitpid(-1, NULL, WNOHANG));
  1363. }
  1364. void
  1365. spawn(const Arg *arg) {
  1366. signal(SIGCHLD, sigchld);
  1367. if(fork() == 0) {
  1368. if(dpy)
  1369. close(ConnectionNumber(dpy));
  1370. setsid();
  1371. execvp(((char **)arg->v)[0], (char **)arg->v);
  1372. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1373. perror(" failed");
  1374. exit(0);
  1375. }
  1376. }
  1377. void
  1378. tag(const Arg *arg) {
  1379. if(selmon->sel && arg->ui & TAGMASK) {
  1380. selmon->sel->tags = arg->ui & TAGMASK;
  1381. arrange();
  1382. }
  1383. }
  1384. #ifdef XINERAMA
  1385. void
  1386. tagmon(const Arg *arg) {
  1387. Monitor *m;
  1388. if(!selmon->sel || !(m = idxtomon(arg->ui)))
  1389. return;
  1390. sendmon(selmon->sel, m);
  1391. }
  1392. #endif /* XINERAMA */
  1393. int
  1394. textnw(const char *text, unsigned int len) {
  1395. XRectangle r;
  1396. if(dc.font.set) {
  1397. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1398. return r.width;
  1399. }
  1400. return XTextWidth(dc.font.xfont, text, len);
  1401. }
  1402. void
  1403. tile(Monitor *m) {
  1404. int x, y, h, w, mw;
  1405. unsigned int i, n;
  1406. Client *c;
  1407. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1408. if(n == 0)
  1409. return;
  1410. /* master */
  1411. c = nexttiled(m->clients);
  1412. mw = m->mfact * m->ww;
  1413. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw);
  1414. if(--n == 0)
  1415. return;
  1416. /* tile stack */
  1417. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1418. y = m->wy;
  1419. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1420. h = m->wh / n;
  1421. if(h < bh)
  1422. h = m->wh;
  1423. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1424. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1425. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw));
  1426. if(h != m->wh)
  1427. y = c->y + HEIGHT(c);
  1428. }
  1429. }
  1430. void
  1431. togglebar(const Arg *arg) {
  1432. selmon->showbar = !selmon->showbar;
  1433. updatebarpos(selmon);
  1434. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1435. arrange();
  1436. }
  1437. void
  1438. togglefloating(const Arg *arg) {
  1439. if(!selmon->sel)
  1440. return;
  1441. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1442. if(selmon->sel->isfloating)
  1443. resize(selmon->sel, selmon->sel->x, selmon->sel->y, selmon->sel->w, selmon->sel->h);
  1444. arrange();
  1445. }
  1446. void
  1447. toggletag(const Arg *arg) {
  1448. unsigned int mask;
  1449. if(!selmon->sel)
  1450. return;
  1451. mask = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1452. if(mask) {
  1453. selmon->sel->tags = mask;
  1454. arrange();
  1455. }
  1456. }
  1457. void
  1458. toggleview(const Arg *arg) {
  1459. unsigned int mask = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1460. if(mask) {
  1461. selmon->tagset[selmon->seltags] = mask;
  1462. arrange();
  1463. }
  1464. }
  1465. void
  1466. unfocus(Client *c) {
  1467. if(!c)
  1468. return;
  1469. grabbuttons(c, False);
  1470. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1471. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1472. }
  1473. void
  1474. unmanage(Client *c) {
  1475. XWindowChanges wc;
  1476. wc.border_width = c->oldbw;
  1477. /* The server grab construct avoids race conditions. */
  1478. XGrabServer(dpy);
  1479. XSetErrorHandler(xerrordummy);
  1480. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1481. detach(c);
  1482. detachstack(c);
  1483. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1484. setclientstate(c, WithdrawnState);
  1485. free(c);
  1486. XSync(dpy, False);
  1487. XSetErrorHandler(xerror);
  1488. XUngrabServer(dpy);
  1489. focus(NULL);
  1490. arrange();
  1491. }
  1492. void
  1493. unmapnotify(XEvent *e) {
  1494. Client *c;
  1495. XUnmapEvent *ev = &e->xunmap;
  1496. if((c = wintoclient(ev->window)))
  1497. unmanage(c);
  1498. }
  1499. void
  1500. updatebars(void) {
  1501. Monitor *m;
  1502. XSetWindowAttributes wa;
  1503. wa.override_redirect = True;
  1504. wa.background_pixmap = ParentRelative;
  1505. wa.event_mask = ButtonPressMask|ExposureMask;
  1506. for(m = mons; m; m = m->next) {
  1507. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1508. CopyFromParent, DefaultVisual(dpy, screen),
  1509. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1510. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1511. XMapRaised(dpy, m->barwin);
  1512. }
  1513. }
  1514. void
  1515. updatebarpos(Monitor *m) {
  1516. m->wy = m->my;
  1517. m->wh = m->mh;
  1518. if(m->showbar) {
  1519. m->wh -= bh;
  1520. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1521. m->wy = m->topbar ? m->wy + bh : m->wy;
  1522. }
  1523. else
  1524. m->by = -bh;
  1525. }
  1526. void
  1527. updategeom(void) {
  1528. int i, n = 1;
  1529. Client *c;
  1530. Monitor *newmons = NULL, *m, *tm;
  1531. #ifdef XINERAMA
  1532. XineramaScreenInfo *info = NULL;
  1533. if(XineramaIsActive(dpy))
  1534. info = XineramaQueryScreens(dpy, &n);
  1535. #endif /* XINERAMA */
  1536. /* allocate monitor(s) for the new geometry setup */
  1537. for(i = 0; i < n; i++) {
  1538. m = (Monitor *)malloc(sizeof(Monitor));
  1539. m->next = newmons;
  1540. newmons = m;
  1541. }
  1542. /* initialise monitor(s) */
  1543. #ifdef XINERAMA
  1544. if(XineramaIsActive(dpy)) {
  1545. for(i = 0, m = newmons; m; m = m->next, i++) {
  1546. m->screen_number = info[i].screen_number;
  1547. m->mx = m->wx = info[i].x_org;
  1548. m->my = m->wy = info[i].y_org;
  1549. m->mw = m->ww = info[i].width;
  1550. m->mh = m->wh = info[i].height;
  1551. }
  1552. XFree(info);
  1553. }
  1554. else
  1555. #endif /* XINERAMA */
  1556. /* default monitor setup */
  1557. {
  1558. m->screen_number = 0;
  1559. m->wx = 0;
  1560. m->my = m->wy = 0;
  1561. m->ww = sw;
  1562. m->mh = m->wh = sh;
  1563. }
  1564. /* bar geometry setup */
  1565. for(m = newmons; m; m = m->next) {
  1566. m->sel = m->stack = m->clients = NULL;
  1567. m->seltags = 0;
  1568. m->sellt = 0;
  1569. m->tagset[0] = m->tagset[1] = 1;
  1570. m->mfact = mfact;
  1571. m->showbar = SHOWBAR;
  1572. m->topbar = TOPBAR;
  1573. updatebarpos(m);
  1574. }
  1575. /* reassign left over clients of disappeared monitors */
  1576. for(tm = mons; tm; tm = tm->next)
  1577. while(tm->clients) {
  1578. c = tm->clients;
  1579. tm->clients = c->next;
  1580. detachstack(c);
  1581. c->mon = newmons;
  1582. attach(c);
  1583. attachstack(c);
  1584. }
  1585. /* select focused monitor */
  1586. cleanupmons();
  1587. mons = newmons;
  1588. selmon = wintomon(root);
  1589. }
  1590. void
  1591. updatenumlockmask(void) {
  1592. unsigned int i, j;
  1593. XModifierKeymap *modmap;
  1594. numlockmask = 0;
  1595. modmap = XGetModifierMapping(dpy);
  1596. for(i = 0; i < 8; i++)
  1597. for(j = 0; j < modmap->max_keypermod; j++)
  1598. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1599. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1600. numlockmask = (1 << i);
  1601. XFreeModifiermap(modmap);
  1602. }
  1603. void
  1604. updatesizehints(Client *c) {
  1605. long msize;
  1606. XSizeHints size;
  1607. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1608. /* size is uninitialized, ensure that size.flags aren't used */
  1609. size.flags = PSize;
  1610. if(size.flags & PBaseSize) {
  1611. c->basew = size.base_width;
  1612. c->baseh = size.base_height;
  1613. }
  1614. else if(size.flags & PMinSize) {
  1615. c->basew = size.min_width;
  1616. c->baseh = size.min_height;
  1617. }
  1618. else
  1619. c->basew = c->baseh = 0;
  1620. if(size.flags & PResizeInc) {
  1621. c->incw = size.width_inc;
  1622. c->inch = size.height_inc;
  1623. }
  1624. else
  1625. c->incw = c->inch = 0;
  1626. if(size.flags & PMaxSize) {
  1627. c->maxw = size.max_width;
  1628. c->maxh = size.max_height;
  1629. }
  1630. else
  1631. c->maxw = c->maxh = 0;
  1632. if(size.flags & PMinSize) {
  1633. c->minw = size.min_width;
  1634. c->minh = size.min_height;
  1635. }
  1636. else if(size.flags & PBaseSize) {
  1637. c->minw = size.base_width;
  1638. c->minh = size.base_height;
  1639. }
  1640. else
  1641. c->minw = c->minh = 0;
  1642. if(size.flags & PAspect) {
  1643. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1644. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1645. }
  1646. else
  1647. c->maxa = c->mina = 0.0;
  1648. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1649. && c->maxw == c->minw && c->maxh == c->minh);
  1650. }
  1651. void
  1652. updatetitle(Client *c) {
  1653. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1654. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1655. }
  1656. void
  1657. updatestatus(void) {
  1658. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1659. strcpy(stext, "dwm-"VERSION);
  1660. drawbar(selmon);
  1661. }
  1662. void
  1663. updatewmhints(Client *c) {
  1664. XWMHints *wmh;
  1665. if((wmh = XGetWMHints(dpy, c->win))) {
  1666. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1667. wmh->flags &= ~XUrgencyHint;
  1668. XSetWMHints(dpy, c->win, wmh);
  1669. }
  1670. else
  1671. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1672. XFree(wmh);
  1673. }
  1674. }
  1675. void
  1676. view(const Arg *arg) {
  1677. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1678. return;
  1679. selmon->seltags ^= 1; /* toggle sel tagset */
  1680. if(arg->ui & TAGMASK)
  1681. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1682. arrange();
  1683. }
  1684. Client *
  1685. wintoclient(Window w) {
  1686. Client *c;
  1687. Monitor *m;
  1688. for(m = mons; m; m = m->next)
  1689. for(c = m->clients; c; c = c->next)
  1690. if(c->win == w)
  1691. return c;
  1692. return NULL;
  1693. }
  1694. Monitor *
  1695. wintomon(Window w) {
  1696. int x, y;
  1697. Client *c;
  1698. Monitor *m;
  1699. if(w == root && getrootpointer(&x, &y))
  1700. return pointertomon(x, y);
  1701. for(m = mons; m; m = m->next)
  1702. if(w == m->barwin)
  1703. return m;
  1704. if((c = wintoclient(w)))
  1705. return c->mon;
  1706. return mons;
  1707. }
  1708. /* There's no way to check accesses to destroyed windows, thus those cases are
  1709. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1710. * default error handler, which may call exit. */
  1711. int
  1712. xerror(Display *dpy, XErrorEvent *ee) {
  1713. if(ee->error_code == BadWindow
  1714. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1715. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1716. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1717. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1718. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1719. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1720. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1721. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1722. return 0;
  1723. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1724. ee->request_code, ee->error_code);
  1725. return xerrorxlib(dpy, ee); /* may call exit */
  1726. }
  1727. int
  1728. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1729. return 0;
  1730. }
  1731. /* Startup Error handler to check if another window manager
  1732. * is already running. */
  1733. int
  1734. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1735. otherwm = True;
  1736. return -1;
  1737. }
  1738. void
  1739. zoom(const Arg *arg) {
  1740. Client *c = selmon->sel;
  1741. if(!lt[selmon->sellt]->arrange || lt[selmon->sellt]->arrange == monocle || (selmon->sel && selmon->sel->isfloating))
  1742. return;
  1743. if(c == nexttiled(selmon->clients))
  1744. if(!c || !(c = nexttiled(c->next)))
  1745. return;
  1746. detach(c);
  1747. attach(c);
  1748. focus(c);
  1749. arrange();
  1750. }
  1751. int
  1752. main(int argc, char *argv[]) {
  1753. if(argc == 2 && !strcmp("-v", argv[1]))
  1754. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1755. else if(argc != 1)
  1756. die("usage: dwm [-v]\n");
  1757. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1758. fputs("warning: no locale support\n", stderr);
  1759. if(!(dpy = XOpenDisplay(NULL)))
  1760. die("dwm: cannot open display\n");
  1761. checkotherwm();
  1762. setup();
  1763. scan();
  1764. run();
  1765. cleanup();
  1766. XCloseDisplay(dpy);
  1767. return 0;
  1768. }