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.

1716 lines
41 KiB

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