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.

1891 lines
42 KiB

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