Configuration file for DWM on MacBook Air
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.

1718 lines
41 KiB

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