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.

1916 lines
46 KiB

17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
15 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /* TODO: cleanup nexttiled, ISVISIBLE, etc */
  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(M, C) ((M) == (C->mon) && (C->tags & M->tagset[M->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(Monitor *m, 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 = selmon->clients;
  350. selmon->clients = c;
  351. }
  352. void
  353. attachstack(Client *c) {
  354. c->snext = selmon->stack;
  355. selmon->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->mon), 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) {
  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. if((dc.w = dc.x - x) > bh) {
  596. dc.x = x;
  597. if(selmon->sel) {
  598. drawtext(selmon->sel->name, dc.sel, False);
  599. drawsquare(selmon->sel->isfixed, selmon->sel->isfloating, False, dc.sel);
  600. }
  601. else
  602. drawtext(NULL, dc.norm, False);
  603. }
  604. }
  605. else {
  606. dc.x = x;
  607. dc.w = m->ww - x;
  608. drawtext(NULL, dc.norm, False);
  609. }
  610. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  611. XSync(dpy, False);
  612. }
  613. void
  614. drawbars() {
  615. Monitor *m;
  616. for(m = mons; m; m = m->next)
  617. drawbar(m);
  618. }
  619. void
  620. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  621. int x;
  622. XGCValues gcv;
  623. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  624. gcv.foreground = col[invert ? ColBG : ColFG];
  625. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  626. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  627. r.x = dc.x + 1;
  628. r.y = dc.y + 1;
  629. if(filled) {
  630. r.width = r.height = x + 1;
  631. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  632. }
  633. else if(empty) {
  634. r.width = r.height = x;
  635. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  636. }
  637. }
  638. void
  639. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  640. char buf[256];
  641. int i, x, y, h, len, olen;
  642. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  643. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  644. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  645. if(!text)
  646. return;
  647. olen = strlen(text);
  648. h = dc.font.ascent + dc.font.descent;
  649. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  650. x = dc.x + (h / 2);
  651. /* shorten text if necessary */
  652. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  653. if(!len)
  654. return;
  655. memcpy(buf, text, len);
  656. if(len < olen)
  657. for(i = len; i && i > len - 3; buf[--i] = '.');
  658. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  659. if(dc.font.set)
  660. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  661. else
  662. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  663. }
  664. void
  665. enternotify(XEvent *e) {
  666. Client *c;
  667. XCrossingEvent *ev = &e->xcrossing;
  668. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  669. return;
  670. if((c = getclient(ev->window)))
  671. focus(c);
  672. else
  673. focus(NULL);
  674. }
  675. void
  676. expose(XEvent *e) {
  677. Monitor *m;
  678. XExposeEvent *ev = &e->xexpose;
  679. if(ev->count == 0)
  680. for(m = mons; m; m = m->next)
  681. if(ev->window == m->barwin) {
  682. drawbar(m);
  683. break;
  684. }
  685. }
  686. void
  687. focus(Client *c) {
  688. if(!c || !ISVISIBLE((c->mon), c))
  689. for(c = selmon->stack; c && !ISVISIBLE(selmon, c); c = c->snext);
  690. if(selmon->sel && selmon->sel != c) {
  691. grabbuttons(selmon->sel, False);
  692. XSetWindowBorder(dpy, selmon->sel->win, dc.norm[ColBorder]);
  693. }
  694. if(c) {
  695. if(c->isurgent)
  696. clearurgent(c);
  697. detachstack(c);
  698. attachstack(c);
  699. grabbuttons(c, True);
  700. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  701. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  702. }
  703. else
  704. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  705. selmon->sel = c;
  706. drawbars();
  707. }
  708. void
  709. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  710. XFocusChangeEvent *ev = &e->xfocus;
  711. if(selmon->sel && ev->window != selmon->sel->win)
  712. XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  713. }
  714. #ifdef XINERAMA
  715. void
  716. focusmon(const Arg *arg) {
  717. unsigned int i;
  718. Monitor *m;
  719. for(i = 0, m = mons; m; m = m->next, i++)
  720. if(i == arg->ui) {
  721. selmon = m;
  722. focus(NULL);
  723. drawbars();
  724. break;
  725. }
  726. }
  727. #endif /* XINERAMA */
  728. void
  729. focusstack(const Arg *arg) {
  730. Client *c = NULL, *i;
  731. if(!selmon->sel)
  732. return;
  733. if(arg->i > 0) {
  734. for(c = selmon->sel->next; c && !ISVISIBLE(selmon, c); c = c->next);
  735. if(!c)
  736. for(c = selmon->clients; c && !ISVISIBLE(selmon, c); c = c->next);
  737. }
  738. else {
  739. for(i = selmon->clients; i != selmon->sel; i = i->next)
  740. if(ISVISIBLE(selmon, i))
  741. c = i;
  742. if(!c)
  743. for(; i; i = i->next)
  744. if(ISVISIBLE(selmon, i))
  745. c = i;
  746. }
  747. if(c) {
  748. focus(c);
  749. restack(selmon);
  750. }
  751. }
  752. Client *
  753. getclient(Window w) {
  754. Client *c = NULL;
  755. Monitor *m;
  756. for(m = mons; m; m = m->next)
  757. for(c = m->clients; c && c->win != w; c = c->next);
  758. return c;
  759. }
  760. unsigned long
  761. getcolor(const char *colstr) {
  762. Colormap cmap = DefaultColormap(dpy, screen);
  763. XColor color;
  764. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  765. die("error, cannot allocate color '%s'\n", colstr);
  766. return color.pixel;
  767. }
  768. long
  769. getstate(Window w) {
  770. int format, status;
  771. long result = -1;
  772. unsigned char *p = NULL;
  773. unsigned long n, extra;
  774. Atom real;
  775. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  776. &real, &format, &n, &extra, (unsigned char **)&p);
  777. if(status != Success)
  778. return -1;
  779. if(n != 0)
  780. result = *p;
  781. XFree(p);
  782. return result;
  783. }
  784. Bool
  785. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  786. char **list = NULL;
  787. int n;
  788. XTextProperty name;
  789. if(!text || size == 0)
  790. return False;
  791. text[0] = '\0';
  792. XGetTextProperty(dpy, w, &name, atom);
  793. if(!name.nitems)
  794. return False;
  795. if(name.encoding == XA_STRING)
  796. strncpy(text, (char *)name.value, size - 1);
  797. else {
  798. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  799. && n > 0 && *list) {
  800. strncpy(text, *list, size - 1);
  801. XFreeStringList(list);
  802. }
  803. }
  804. text[size - 1] = '\0';
  805. XFree(name.value);
  806. return True;
  807. }
  808. void
  809. grabbuttons(Client *c, Bool focused) {
  810. updatenumlockmask();
  811. {
  812. unsigned int i, j;
  813. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  814. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  815. if(focused) {
  816. for(i = 0; i < LENGTH(buttons); i++)
  817. if(buttons[i].click == ClkClientWin)
  818. for(j = 0; j < LENGTH(modifiers); j++)
  819. XGrabButton(dpy, buttons[i].button,
  820. buttons[i].mask | modifiers[j],
  821. c->win, False, BUTTONMASK,
  822. GrabModeAsync, GrabModeSync, None, None);
  823. } else
  824. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  825. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  826. }
  827. }
  828. void
  829. grabkeys(void) {
  830. updatenumlockmask();
  831. { /* grab keys */
  832. unsigned int i, j;
  833. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  834. KeyCode code;
  835. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  836. for(i = 0; i < LENGTH(keys); i++) {
  837. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  838. for(j = 0; j < LENGTH(modifiers); j++)
  839. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  840. True, GrabModeAsync, GrabModeAsync);
  841. }
  842. }
  843. }
  844. void
  845. initfont(const char *fontstr) {
  846. char *def, **missing;
  847. int i, n;
  848. missing = NULL;
  849. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  850. if(missing) {
  851. while(n--)
  852. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  853. XFreeStringList(missing);
  854. }
  855. if(dc.font.set) {
  856. XFontSetExtents *font_extents;
  857. XFontStruct **xfonts;
  858. char **font_names;
  859. dc.font.ascent = dc.font.descent = 0;
  860. font_extents = XExtentsOfFontSet(dc.font.set);
  861. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  862. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  863. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  864. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  865. xfonts++;
  866. }
  867. }
  868. else {
  869. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  870. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  871. die("error, cannot load font: '%s'\n", fontstr);
  872. dc.font.ascent = dc.font.xfont->ascent;
  873. dc.font.descent = dc.font.xfont->descent;
  874. }
  875. dc.font.height = dc.font.ascent + dc.font.descent;
  876. }
  877. Bool
  878. isprotodel(Client *c) {
  879. int i, n;
  880. Atom *protocols;
  881. Bool ret = False;
  882. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  883. for(i = 0; !ret && i < n; i++)
  884. if(protocols[i] == wmatom[WMDelete])
  885. ret = True;
  886. XFree(protocols);
  887. }
  888. return ret;
  889. }
  890. void
  891. keypress(XEvent *e) {
  892. unsigned int i;
  893. KeySym keysym;
  894. XKeyEvent *ev;
  895. ev = &e->xkey;
  896. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  897. for(i = 0; i < LENGTH(keys); i++)
  898. if(keysym == keys[i].keysym
  899. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  900. && keys[i].func)
  901. keys[i].func(&(keys[i].arg));
  902. }
  903. void
  904. killclient(const Arg *arg) {
  905. XEvent ev;
  906. if(!selmon->sel)
  907. return;
  908. if(isprotodel(selmon->sel)) {
  909. ev.type = ClientMessage;
  910. ev.xclient.window = selmon->sel->win;
  911. ev.xclient.message_type = wmatom[WMProtocols];
  912. ev.xclient.format = 32;
  913. ev.xclient.data.l[0] = wmatom[WMDelete];
  914. ev.xclient.data.l[1] = CurrentTime;
  915. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  916. }
  917. else
  918. XKillClient(dpy, selmon->sel->win);
  919. }
  920. void
  921. manage(Window w, XWindowAttributes *wa) {
  922. static Client cz;
  923. Client *c, *t = NULL;
  924. Window trans = None;
  925. XWindowChanges wc;
  926. if(!(c = malloc(sizeof(Client))))
  927. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  928. *c = cz;
  929. c->win = w;
  930. c->mon = selmon;
  931. /* geometry */
  932. c->x = wa->x;
  933. c->y = wa->y;
  934. c->w = wa->width;
  935. c->h = wa->height;
  936. c->oldbw = wa->border_width;
  937. if(c->w == sw && c->h == sh) {
  938. c->x = sx;
  939. c->y = sy;
  940. c->bw = 0;
  941. }
  942. else {
  943. if(c->x + WIDTH(c) > sx + sw)
  944. c->x = sx + sw - WIDTH(c);
  945. if(c->y + HEIGHT(c) > sy + sh)
  946. c->y = sy + sh - HEIGHT(c);
  947. c->x = MAX(c->x, sx);
  948. /* only fix client y-offset, if the client center might cover the bar */
  949. c->y = MAX(c->y, ((selmon->by == 0) && (c->x + (c->w / 2) >= selmon->wx)
  950. && (c->x + (c->w / 2) < selmon->wx + selmon->ww)) ? bh : sy);
  951. c->bw = borderpx;
  952. }
  953. wc.border_width = c->bw;
  954. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  955. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  956. configure(c); /* propagates border_width, if size doesn't change */
  957. updatesizehints(c);
  958. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  959. grabbuttons(c, False);
  960. updatetitle(c);
  961. if(XGetTransientForHint(dpy, w, &trans))
  962. t = getclient(trans);
  963. if(t)
  964. c->tags = t->tags;
  965. else
  966. applyrules(c);
  967. if(!c->isfloating)
  968. c->isfloating = trans != None || c->isfixed;
  969. if(c->isfloating)
  970. XRaiseWindow(dpy, c->win);
  971. attach(c);
  972. attachstack(c);
  973. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  974. XMapWindow(dpy, c->win);
  975. setclientstate(c, NormalState);
  976. arrange();
  977. }
  978. void
  979. mappingnotify(XEvent *e) {
  980. XMappingEvent *ev = &e->xmapping;
  981. XRefreshKeyboardMapping(ev);
  982. if(ev->request == MappingKeyboard)
  983. grabkeys();
  984. }
  985. void
  986. maprequest(XEvent *e) {
  987. static XWindowAttributes wa;
  988. XMapRequestEvent *ev = &e->xmaprequest;
  989. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  990. return;
  991. if(wa.override_redirect)
  992. return;
  993. if(!getclient(ev->window))
  994. manage(ev->window, &wa);
  995. }
  996. void
  997. monocle(Monitor *m) {
  998. Client *c;
  999. for(c = nexttiled(m, selmon->clients); c; c = nexttiled(m, c->next))
  1000. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw);
  1001. }
  1002. void
  1003. movemouse(const Arg *arg) {
  1004. int x, y, ocx, ocy, di, nx, ny;
  1005. unsigned int dui;
  1006. Client *c;
  1007. Window dummy;
  1008. XEvent ev;
  1009. if(!(c = selmon->sel))
  1010. return;
  1011. restack(selmon);
  1012. ocx = c->x;
  1013. ocy = c->y;
  1014. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1015. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1016. return;
  1017. XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
  1018. do {
  1019. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1020. switch (ev.type) {
  1021. case ConfigureRequest:
  1022. case Expose:
  1023. case MapRequest:
  1024. handler[ev.type](&ev);
  1025. break;
  1026. case MotionNotify:
  1027. nx = ocx + (ev.xmotion.x - x);
  1028. ny = ocy + (ev.xmotion.y - y);
  1029. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1030. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1031. if(abs(selmon->wx - nx) < snap)
  1032. nx = selmon->wx;
  1033. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1034. nx = selmon->wx + selmon->ww - WIDTH(c);
  1035. if(abs(selmon->wy - ny) < snap)
  1036. ny = selmon->wy;
  1037. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1038. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1039. if(!c->isfloating && lt[selmon->sellt]->arrange
  1040. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1041. togglefloating(NULL);
  1042. }
  1043. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1044. resize(c, nx, ny, c->w, c->h);
  1045. break;
  1046. }
  1047. }
  1048. while(ev.type != ButtonRelease);
  1049. XUngrabPointer(dpy, CurrentTime);
  1050. }
  1051. Client *
  1052. nexttiled(Monitor *m, Client *c) {
  1053. // TODO: m handling
  1054. for(; c && (c->isfloating || !ISVISIBLE(m, 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(!selmon->sel)
  1157. return;
  1158. if(m == selmon && (selmon->sel->isfloating || !lt[m->sellt]->arrange))
  1159. XRaiseWindow(dpy, selmon->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(m, 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->mon), 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. Monitor *m;
  1337. for(i = 0, m = mons; m; m = m->next, i++)
  1338. if(i == arg->ui) {
  1339. selmon->sel->m = m;
  1340. arrange();
  1341. break;
  1342. }
  1343. }
  1344. #endif /* XINERAMA */
  1345. int
  1346. textnw(const char *text, unsigned int len) {
  1347. XRectangle r;
  1348. if(dc.font.set) {
  1349. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1350. return r.width;
  1351. }
  1352. return XTextWidth(dc.font.xfont, text, len);
  1353. }
  1354. void
  1355. tile(Monitor *m) {
  1356. int x, y, h, w, mw;
  1357. unsigned int i, n;
  1358. Client *c;
  1359. for(n = 0, c = nexttiled(m, m->clients); c; c = nexttiled(m, c->next), n++);
  1360. if(n == 0)
  1361. return;
  1362. /* master */
  1363. c = nexttiled(m, m->clients);
  1364. mw = m->mfact * m->ww;
  1365. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw);
  1366. if(--n == 0)
  1367. return;
  1368. /* tile stack */
  1369. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1370. y = m->wy;
  1371. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1372. h = m->wh / n;
  1373. if(h < bh)
  1374. h = m->wh;
  1375. for(i = 0, c = nexttiled(m, c->next); c; c = nexttiled(m, c->next), i++) {
  1376. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1377. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw));
  1378. if(h != m->wh)
  1379. y = c->y + HEIGHT(c);
  1380. }
  1381. }
  1382. void
  1383. togglebar(const Arg *arg) {
  1384. selmon->showbar = !selmon->showbar;
  1385. updatebarpos(selmon);
  1386. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1387. arrange();
  1388. }
  1389. void
  1390. togglefloating(const Arg *arg) {
  1391. if(!selmon->sel)
  1392. return;
  1393. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1394. if(selmon->sel->isfloating)
  1395. resize(selmon->sel, selmon->sel->x, selmon->sel->y, selmon->sel->w, selmon->sel->h);
  1396. arrange();
  1397. }
  1398. void
  1399. toggletag(const Arg *arg) {
  1400. unsigned int mask;
  1401. if(!selmon->sel)
  1402. return;
  1403. mask = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1404. if(mask) {
  1405. selmon->sel->tags = mask;
  1406. arrange();
  1407. }
  1408. }
  1409. void
  1410. toggleview(const Arg *arg) {
  1411. unsigned int mask = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1412. if(mask) {
  1413. selmon->tagset[selmon->seltags] = mask;
  1414. arrange();
  1415. }
  1416. }
  1417. void
  1418. unmanage(Client *c) {
  1419. XWindowChanges wc;
  1420. wc.border_width = c->oldbw;
  1421. /* The server grab construct avoids race conditions. */
  1422. XGrabServer(dpy);
  1423. XSetErrorHandler(xerrordummy);
  1424. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1425. detach(c);
  1426. detachstack(c);
  1427. if(selmon->sel == c)
  1428. focus(NULL);
  1429. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1430. setclientstate(c, WithdrawnState);
  1431. free(c);
  1432. XSync(dpy, False);
  1433. XSetErrorHandler(xerror);
  1434. XUngrabServer(dpy);
  1435. arrange();
  1436. }
  1437. void
  1438. unmapnotify(XEvent *e) {
  1439. Client *c;
  1440. XUnmapEvent *ev = &e->xunmap;
  1441. if((c = getclient(ev->window)))
  1442. unmanage(c);
  1443. }
  1444. void
  1445. updatebars(void) {
  1446. Monitor *m;
  1447. XSetWindowAttributes wa;
  1448. wa.override_redirect = True;
  1449. wa.background_pixmap = ParentRelative;
  1450. wa.event_mask = ButtonPressMask|ExposureMask;
  1451. for(m = mons; m; m = m->next) {
  1452. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1453. CopyFromParent, DefaultVisual(dpy, screen),
  1454. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1455. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1456. XMapRaised(dpy, m->barwin);
  1457. }
  1458. }
  1459. void
  1460. updatebarpos(Monitor *m) {
  1461. m->wy = m->my;
  1462. m->wh = m->mh;
  1463. if(m->showbar) {
  1464. m->wh -= bh;
  1465. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1466. m->wy = m->topbar ? m->wy + bh : m->wy;
  1467. }
  1468. else
  1469. m->by = -bh;
  1470. }
  1471. void
  1472. updategeom(void) {
  1473. int i, n = 1;
  1474. Client *c;
  1475. Monitor *newmons = NULL, *m, *tm;
  1476. #ifdef XINERAMA
  1477. XineramaScreenInfo *info = NULL;
  1478. if(XineramaIsActive(dpy))
  1479. info = XineramaQueryScreens(dpy, &n);
  1480. #endif
  1481. /* allocate monitor(s) for the new geometry setup */
  1482. for(i = 0; i < n; i++) {
  1483. m = (Monitor *)malloc(sizeof(Monitor));
  1484. m->next = newmons;
  1485. newmons = m;
  1486. }
  1487. /* initialise monitor(s) */
  1488. #ifdef XINERAMA
  1489. if(XineramaIsActive(dpy)) {
  1490. for(i = 0, m = newmons; m; m = m->next, i++) {
  1491. m->screen_number = info[i].screen_number;
  1492. m->wx = info[i].x_org;
  1493. m->my = m->wy = info[i].y_org;
  1494. m->ww = info[i].width;
  1495. m->mh = m->wh = info[i].height;
  1496. }
  1497. XFree(info);
  1498. }
  1499. else
  1500. #endif
  1501. /* default monitor setup */
  1502. {
  1503. m->screen_number = 0;
  1504. m->wx = sx;
  1505. m->my = m->wy = sy;
  1506. m->ww = sw;
  1507. m->mh = m->wh = sh;
  1508. }
  1509. /* bar geometry setup */
  1510. for(m = newmons; m; m = m->next) {
  1511. /* TODO: consider removing the following values from config.h */
  1512. m->clients = NULL;
  1513. m->sel = NULL;
  1514. m->stack = NULL;
  1515. m->seltags = 0;
  1516. m->sellt = 0;
  1517. m->tagset[0] = m->tagset[1] = 1;
  1518. m->mfact = mfact;
  1519. m->showbar = showbar;
  1520. m->topbar = topbar;
  1521. updatebarpos(m);
  1522. /* reassign all clients with same screen number */
  1523. for(tm = mons; tm; tm = tm->next)
  1524. if(tm->screen_number == m->screen_number) {
  1525. m->clients = tm->clients;
  1526. m->stack = tm->stack;
  1527. tm->clients = NULL;
  1528. tm->stack = NULL;
  1529. for(c = m->clients; c; c = c->next)
  1530. c->mon = m;
  1531. }
  1532. }
  1533. /* reassign left over clients of disappeared monitors */
  1534. for(tm = mons; tm; tm = tm->next) {
  1535. while(tm->clients) {
  1536. c = tm->clients->next;
  1537. tm->clients->next = newmons->clients;
  1538. tm->clients->mon = newmons;
  1539. newmons->clients = tm->clients;
  1540. tm->clients = c;
  1541. }
  1542. while(tm->stack) {
  1543. c = tm->stack->snext;
  1544. tm->stack->snext = newmons->stack;
  1545. newmons->stack = tm->stack;
  1546. tm->stack = c;
  1547. }
  1548. }
  1549. /* select focused monitor */
  1550. if(!selmon) {
  1551. selmon = newmons;
  1552. int di, x, y;
  1553. unsigned int dui;
  1554. Window dummy;
  1555. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  1556. for(m = newmons; m; m = m->next)
  1557. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh)) {
  1558. selmon = m;
  1559. break;
  1560. }
  1561. }
  1562. /* final assignment of new monitors */
  1563. cleanupmons();
  1564. mons = newmons;
  1565. }
  1566. void
  1567. updatenumlockmask(void) {
  1568. unsigned int i, j;
  1569. XModifierKeymap *modmap;
  1570. numlockmask = 0;
  1571. modmap = XGetModifierMapping(dpy);
  1572. for(i = 0; i < 8; i++)
  1573. for(j = 0; j < modmap->max_keypermod; j++)
  1574. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1575. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1576. numlockmask = (1 << i);
  1577. XFreeModifiermap(modmap);
  1578. }
  1579. void
  1580. updatesizehints(Client *c) {
  1581. long msize;
  1582. XSizeHints size;
  1583. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1584. /* size is uninitialized, ensure that size.flags aren't used */
  1585. size.flags = PSize;
  1586. if(size.flags & PBaseSize) {
  1587. c->basew = size.base_width;
  1588. c->baseh = size.base_height;
  1589. }
  1590. else if(size.flags & PMinSize) {
  1591. c->basew = size.min_width;
  1592. c->baseh = size.min_height;
  1593. }
  1594. else
  1595. c->basew = c->baseh = 0;
  1596. if(size.flags & PResizeInc) {
  1597. c->incw = size.width_inc;
  1598. c->inch = size.height_inc;
  1599. }
  1600. else
  1601. c->incw = c->inch = 0;
  1602. if(size.flags & PMaxSize) {
  1603. c->maxw = size.max_width;
  1604. c->maxh = size.max_height;
  1605. }
  1606. else
  1607. c->maxw = c->maxh = 0;
  1608. if(size.flags & PMinSize) {
  1609. c->minw = size.min_width;
  1610. c->minh = size.min_height;
  1611. }
  1612. else if(size.flags & PBaseSize) {
  1613. c->minw = size.base_width;
  1614. c->minh = size.base_height;
  1615. }
  1616. else
  1617. c->minw = c->minh = 0;
  1618. if(size.flags & PAspect) {
  1619. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1620. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1621. }
  1622. else
  1623. c->maxa = c->mina = 0.0;
  1624. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1625. && c->maxw == c->minw && c->maxh == c->minh);
  1626. }
  1627. void
  1628. updatetitle(Client *c) {
  1629. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1630. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1631. }
  1632. void
  1633. updatestatus() {
  1634. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1635. strcpy(stext, "dwm-"VERSION);
  1636. drawbar(selmon);
  1637. }
  1638. void
  1639. updatewmhints(Client *c) {
  1640. XWMHints *wmh;
  1641. if((wmh = XGetWMHints(dpy, c->win))) {
  1642. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1643. wmh->flags &= ~XUrgencyHint;
  1644. XSetWMHints(dpy, c->win, wmh);
  1645. }
  1646. else
  1647. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1648. XFree(wmh);
  1649. }
  1650. }
  1651. void
  1652. view(const Arg *arg) {
  1653. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1654. return;
  1655. selmon->seltags ^= 1; /* toggle sel tagset */
  1656. if(arg->ui & TAGMASK)
  1657. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1658. arrange();
  1659. }
  1660. /* There's no way to check accesses to destroyed windows, thus those cases are
  1661. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1662. * default error handler, which may call exit. */
  1663. int
  1664. xerror(Display *dpy, XErrorEvent *ee) {
  1665. if(ee->error_code == BadWindow
  1666. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1667. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1668. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1669. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1670. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1671. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1672. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1673. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1674. return 0;
  1675. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1676. ee->request_code, ee->error_code);
  1677. return xerrorxlib(dpy, ee); /* may call exit */
  1678. }
  1679. int
  1680. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1681. return 0;
  1682. }
  1683. /* Startup Error handler to check if another window manager
  1684. * is already running. */
  1685. int
  1686. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1687. otherwm = True;
  1688. return -1;
  1689. }
  1690. void
  1691. zoom(const Arg *arg) {
  1692. Client *c = selmon->sel;
  1693. if(!lt[selmon->sellt]->arrange || lt[selmon->sellt]->arrange == monocle || (selmon->sel && selmon->sel->isfloating))
  1694. return;
  1695. if(c == nexttiled(selmon, selmon->clients))
  1696. if(!c || !(c = nexttiled(selmon, c->next)))
  1697. return;
  1698. detach(c);
  1699. attach(c);
  1700. focus(c);
  1701. arrange();
  1702. }
  1703. int
  1704. main(int argc, char *argv[]) {
  1705. if(argc == 2 && !strcmp("-v", argv[1]))
  1706. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1707. else if(argc != 1)
  1708. die("usage: dwm [-v]\n");
  1709. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1710. fputs("warning: no locale support\n", stderr);
  1711. if(!(dpy = XOpenDisplay(NULL)))
  1712. die("dwm: cannot open display\n");
  1713. checkotherwm();
  1714. setup();
  1715. scan();
  1716. run();
  1717. cleanup();
  1718. XCloseDisplay(dpy);
  1719. return 0;
  1720. }