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.

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