Configuration of dwm for Mac Computers
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.

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