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.

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