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.

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