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.

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