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.

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