Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1711 lines
38 KiB

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