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.

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