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.

1717 lines
40 KiB

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