Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1823 lines
41 KiB

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