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.

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