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.

1895 lines
43 KiB

17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
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.
  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. {
  1058. if(w * c->maxay > h * c->maxax)
  1059. w = h * c->maxax / c->maxay;
  1060. else if(w * c->minay < h * c->minax)
  1061. h = w * c->minay / c->minax;
  1062. }
  1063. /* adjust for increment value */
  1064. if(c->incw)
  1065. w -= w % c->incw;
  1066. if(c->inch)
  1067. h -= h % c->inch;
  1068. /* restore base dimensions */
  1069. w += c->basew;
  1070. h += c->baseh;
  1071. w = MAX(w, c->minw);
  1072. h = MAX(h, c->minh);
  1073. if (c->maxw)
  1074. w = MIN(w, c->maxw);
  1075. if (c->maxh)
  1076. h = MIN(h, c->maxh);
  1077. }
  1078. if(w <= 0 || h <= 0)
  1079. return;
  1080. if(x > sx + sw)
  1081. x = sw - w - 2 * c->bw;
  1082. if(y > sy + sh)
  1083. y = sh - h - 2 * c->bw;
  1084. if(x + w + 2 * c->bw < sx)
  1085. x = sx;
  1086. if(y + h + 2 * c->bw < sy)
  1087. y = sy;
  1088. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1089. c->x = wc.x = x;
  1090. c->y = wc.y = y;
  1091. c->w = wc.width = w;
  1092. c->h = wc.height = h;
  1093. wc.border_width = c->bw;
  1094. XConfigureWindow(dpy, c->win,
  1095. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1096. configure(c);
  1097. XSync(dpy, False);
  1098. }
  1099. }
  1100. void
  1101. resizemouse(Client *c) {
  1102. int ocx, ocy;
  1103. int nw, nh;
  1104. XEvent ev;
  1105. ocx = c->x;
  1106. ocy = c->y;
  1107. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1108. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1109. return;
  1110. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1111. for(;;) {
  1112. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1113. switch(ev.type) {
  1114. case ButtonRelease:
  1115. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1116. c->w + c->bw - 1, c->h + c->bw - 1);
  1117. XUngrabPointer(dpy, CurrentTime);
  1118. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1119. return;
  1120. case ConfigureRequest:
  1121. case Expose:
  1122. case MapRequest:
  1123. handler[ev.type](&ev);
  1124. break;
  1125. case MotionNotify:
  1126. XSync(dpy, False);
  1127. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1128. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1129. if(!c->isfloating && !lt->isfloating && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP)) {
  1130. c->fx = c->x;
  1131. c->fy = c->y;
  1132. togglefloating(NULL);
  1133. }
  1134. if((lt->isfloating) || c->isfloating) {
  1135. resize(c, c->x, c->y, nw, nh, True);
  1136. c->fw = nw;
  1137. c->fh = nh;
  1138. }
  1139. break;
  1140. }
  1141. }
  1142. }
  1143. void
  1144. restack(void) {
  1145. Client *c;
  1146. XEvent ev;
  1147. XWindowChanges wc;
  1148. drawbar();
  1149. if(!sel)
  1150. return;
  1151. if(sel->isfloating || lt->isfloating)
  1152. XRaiseWindow(dpy, sel->win);
  1153. if(!lt->isfloating) {
  1154. wc.stack_mode = Below;
  1155. wc.sibling = barwin;
  1156. for(c = stack; c; c = c->snext)
  1157. if(!c->isfloating && isvisible(c, NULL)) {
  1158. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1159. wc.sibling = c->win;
  1160. }
  1161. }
  1162. XSync(dpy, False);
  1163. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1164. }
  1165. void
  1166. run(void) {
  1167. char *p;
  1168. char sbuf[sizeof stext];
  1169. fd_set rd;
  1170. int r, xfd;
  1171. unsigned int len, offset;
  1172. XEvent ev;
  1173. /* main event loop, also reads status text from stdin */
  1174. XSync(dpy, False);
  1175. xfd = ConnectionNumber(dpy);
  1176. readin = True;
  1177. offset = 0;
  1178. len = sizeof stext - 1;
  1179. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1180. while(running) {
  1181. FD_ZERO(&rd);
  1182. if(readin)
  1183. FD_SET(STDIN_FILENO, &rd);
  1184. FD_SET(xfd, &rd);
  1185. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1186. if(errno == EINTR)
  1187. continue;
  1188. eprint("select failed\n");
  1189. }
  1190. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1191. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1192. case -1:
  1193. strncpy(stext, strerror(errno), len);
  1194. readin = False;
  1195. break;
  1196. case 0:
  1197. strncpy(stext, "EOF", 4);
  1198. readin = False;
  1199. break;
  1200. default:
  1201. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1202. if(*p == '\n' || *p == '\0') {
  1203. *p = '\0';
  1204. strncpy(stext, sbuf, len);
  1205. p += r - 1; /* p is sbuf + offset + r - 1 */
  1206. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1207. offset = r;
  1208. if(r)
  1209. memmove(sbuf, p - r + 1, r);
  1210. break;
  1211. }
  1212. break;
  1213. }
  1214. drawbar();
  1215. }
  1216. while(XPending(dpy)) {
  1217. XNextEvent(dpy, &ev);
  1218. if(handler[ev.type])
  1219. (handler[ev.type])(&ev); /* call handler */
  1220. }
  1221. }
  1222. }
  1223. void
  1224. scan(void) {
  1225. unsigned int i, num;
  1226. Window *wins, d1, d2;
  1227. XWindowAttributes wa;
  1228. wins = NULL;
  1229. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1230. for(i = 0; i < num; i++) {
  1231. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1232. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1233. continue;
  1234. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1235. manage(wins[i], &wa);
  1236. }
  1237. for(i = 0; i < num; i++) { /* now the transients */
  1238. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1239. continue;
  1240. if(XGetTransientForHint(dpy, wins[i], &d1)
  1241. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1242. manage(wins[i], &wa);
  1243. }
  1244. }
  1245. if(wins)
  1246. XFree(wins);
  1247. }
  1248. void
  1249. setclientstate(Client *c, long state) {
  1250. long data[] = {state, None};
  1251. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1252. PropModeReplace, (unsigned char *)data, 2);
  1253. }
  1254. void
  1255. setlayout(const char *arg) {
  1256. unsigned int i;
  1257. if(!arg) {
  1258. if(++lt == &layouts[LENGTH(layouts)])
  1259. lt = &layouts[0];
  1260. }
  1261. else {
  1262. for(i = 0; i < LENGTH(layouts); i++)
  1263. if(!strcmp(arg, layouts[i].symbol))
  1264. break;
  1265. if(i == LENGTH(layouts))
  1266. return;
  1267. lt = &layouts[i];
  1268. }
  1269. if(sel)
  1270. arrange();
  1271. else
  1272. drawbar();
  1273. }
  1274. void
  1275. setmfact(const char *arg) {
  1276. double d;
  1277. if(lt->isfloating)
  1278. return;
  1279. if(!arg)
  1280. mfact = MFACT;
  1281. else {
  1282. d = strtod(arg, NULL);
  1283. if(arg[0] == '-' || arg[0] == '+')
  1284. d += mfact;
  1285. if(d < 0.1 || d > 0.9)
  1286. return;
  1287. mfact = d;
  1288. }
  1289. updategeom();
  1290. arrange();
  1291. }
  1292. void
  1293. setup(void) {
  1294. unsigned int i, w;
  1295. XSetWindowAttributes wa;
  1296. /* init screen */
  1297. screen = DefaultScreen(dpy);
  1298. root = RootWindow(dpy, screen);
  1299. initfont(FONT);
  1300. sx = 0;
  1301. sy = 0;
  1302. sw = DisplayWidth(dpy, screen);
  1303. sh = DisplayHeight(dpy, screen);
  1304. bh = dc.font.height + 2;
  1305. /* update geometry */
  1306. updategeom();
  1307. /* init atoms */
  1308. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1309. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1310. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1311. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1312. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1313. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1314. /* init cursors */
  1315. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1316. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1317. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1318. /* init appearance */
  1319. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1320. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1321. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1322. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1323. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1324. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1325. initfont(FONT);
  1326. dc.h = bh;
  1327. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1328. dc.gc = XCreateGC(dpy, root, 0, 0);
  1329. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1330. if(!dc.font.set)
  1331. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1332. /* init tags */
  1333. tagset[0] = emallocz(TAGSZ);
  1334. tagset[1] = emallocz(TAGSZ);
  1335. tagset[0][0] = tagset[1][0] = True;
  1336. /* init bar */
  1337. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1338. w = textw(layouts[i].symbol);
  1339. blw = MAX(blw, w);
  1340. }
  1341. wa.override_redirect = 1;
  1342. wa.background_pixmap = ParentRelative;
  1343. wa.event_mask = ButtonPressMask|ExposureMask;
  1344. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  1345. CopyFromParent, DefaultVisual(dpy, screen),
  1346. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1347. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1348. XMapRaised(dpy, barwin);
  1349. strcpy(stext, "dwm-"VERSION);
  1350. drawbar();
  1351. /* EWMH support per view */
  1352. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1353. PropModeReplace, (unsigned char *) netatom, NetLast);
  1354. /* select for events */
  1355. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1356. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1357. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1358. XSelectInput(dpy, root, wa.event_mask);
  1359. /* grab keys */
  1360. grabkeys();
  1361. }
  1362. void
  1363. spawn(const char *arg) {
  1364. static char *shell = NULL;
  1365. if(!shell && !(shell = getenv("SHELL")))
  1366. shell = "/bin/sh";
  1367. if(!arg)
  1368. return;
  1369. /* The double-fork construct avoids zombie processes and keeps the code
  1370. * clean from stupid signal handlers. */
  1371. if(fork() == 0) {
  1372. if(fork() == 0) {
  1373. if(dpy)
  1374. close(ConnectionNumber(dpy));
  1375. setsid();
  1376. execl(shell, shell, "-c", arg, (char *)NULL);
  1377. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1378. perror(" failed");
  1379. }
  1380. exit(0);
  1381. }
  1382. wait(0);
  1383. }
  1384. void
  1385. tag(const char *arg) {
  1386. unsigned int i;
  1387. if(!sel)
  1388. return;
  1389. for(i = 0; i < LENGTH(tags); i++)
  1390. sel->tags[i] = (NULL == arg);
  1391. sel->tags[idxoftag(arg)] = True;
  1392. arrange();
  1393. }
  1394. unsigned int
  1395. textnw(const char *text, unsigned int len) {
  1396. XRectangle r;
  1397. if(dc.font.set) {
  1398. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1399. return r.width;
  1400. }
  1401. return XTextWidth(dc.font.xfont, text, len);
  1402. }
  1403. unsigned int
  1404. textw(const char *text) {
  1405. return textnw(text, strlen(text)) + dc.font.height;
  1406. }
  1407. void
  1408. tileh(void) {
  1409. int x, w;
  1410. unsigned int i, n = counttiled();
  1411. Client *c;
  1412. if(n == 0)
  1413. return;
  1414. c = tilemaster(n);
  1415. if(--n == 0)
  1416. return;
  1417. x = tx;
  1418. w = tw / n;
  1419. if(w < bh)
  1420. w = tw;
  1421. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1422. if(i + 1 == n) /* remainder */
  1423. tileresize(c, x, ty, (tx + tw) - x - 2 * c->bw, th - 2 * c->bw);
  1424. else
  1425. tileresize(c, x, ty, w - 2 * c->bw, th - 2 * c->bw);
  1426. if(w != tw)
  1427. x = c->x + c->w + 2 * c->bw;
  1428. }
  1429. }
  1430. Client *
  1431. tilemaster(unsigned int n) {
  1432. Client *c = nexttiled(clients);
  1433. if(n == 1)
  1434. tileresize(c, wx, wy, ww - 2 * c->bw, wh - 2 * c->bw);
  1435. else
  1436. tileresize(c, mx, my, mw - 2 * c->bw, mh - 2 * c->bw);
  1437. return c;
  1438. }
  1439. void
  1440. tileresize(Client *c, int x, int y, int w, int h) {
  1441. resize(c, x, y, w, h, RESIZEHINTS);
  1442. if((RESIZEHINTS) && ((c->h < bh) || (c->h > h) || (c->w < bh) || (c->w > w)))
  1443. /* client doesn't accept size constraints */
  1444. resize(c, x, y, w, h, False);
  1445. }
  1446. void
  1447. tilev(void) {
  1448. int y, h;
  1449. unsigned int i, n = counttiled();
  1450. Client *c;
  1451. if(n == 0)
  1452. return;
  1453. c = tilemaster(n);
  1454. if(--n == 0)
  1455. return;
  1456. y = ty;
  1457. h = th / n;
  1458. if(h < bh)
  1459. h = th;
  1460. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1461. if(i + 1 == n) /* remainder */
  1462. tileresize(c, tx, y, tw - 2 * c->bw, (ty + th) - y - 2 * c->bw);
  1463. else
  1464. tileresize(c, tx, y, tw - 2 * c->bw, h - 2 * c->bw);
  1465. if(h != th)
  1466. y = c->y + c->h + 2 * c->bw;
  1467. }
  1468. }
  1469. void
  1470. togglefloating(const char *arg) {
  1471. if(!sel)
  1472. return;
  1473. sel->isfloating = !sel->isfloating;
  1474. if(sel->isfloating)
  1475. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1476. arrange();
  1477. }
  1478. void
  1479. toggletag(const char *arg) {
  1480. unsigned int i, j;
  1481. if(!sel)
  1482. return;
  1483. i = idxoftag(arg);
  1484. sel->tags[i] = !sel->tags[i];
  1485. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1486. if(j == LENGTH(tags))
  1487. sel->tags[i] = True; /* at least one tag must be enabled */
  1488. arrange();
  1489. }
  1490. void
  1491. toggleview(const char *arg) {
  1492. unsigned int i, j;
  1493. i = idxoftag(arg);
  1494. tagset[seltags][i] = !tagset[seltags][i];
  1495. for(j = 0; j < LENGTH(tags) && !tagset[seltags][j]; j++);
  1496. if(j == LENGTH(tags))
  1497. tagset[seltags][i] = True; /* at least one tag must be viewed */
  1498. arrange();
  1499. }
  1500. void
  1501. unban(Client *c) {
  1502. if(!c->isbanned)
  1503. return;
  1504. XMoveWindow(dpy, c->win, c->x, c->y);
  1505. c->isbanned = False;
  1506. }
  1507. void
  1508. unmanage(Client *c) {
  1509. XWindowChanges wc;
  1510. wc.border_width = c->oldbw;
  1511. /* The server grab construct avoids race conditions. */
  1512. XGrabServer(dpy);
  1513. XSetErrorHandler(xerrordummy);
  1514. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1515. detach(c);
  1516. detachstack(c);
  1517. if(sel == c)
  1518. focus(NULL);
  1519. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1520. setclientstate(c, WithdrawnState);
  1521. free(c->tags);
  1522. free(c);
  1523. XSync(dpy, False);
  1524. XSetErrorHandler(xerror);
  1525. XUngrabServer(dpy);
  1526. arrange();
  1527. }
  1528. void
  1529. unmapnotify(XEvent *e) {
  1530. Client *c;
  1531. XUnmapEvent *ev = &e->xunmap;
  1532. if((c = getclient(ev->window)))
  1533. unmanage(c);
  1534. }
  1535. void
  1536. updatebar(void) {
  1537. if(dc.drawable != 0)
  1538. XFreePixmap(dpy, dc.drawable);
  1539. dc.drawable = XCreatePixmap(dpy, root, bw, bh, DefaultDepth(dpy, screen));
  1540. XMoveResizeWindow(dpy, barwin, bx, by, bw, bh);
  1541. }
  1542. void
  1543. updategeom(void) {
  1544. /* bar geometry */
  1545. bx = 0;
  1546. by = 0;
  1547. bw = sw;
  1548. /* window area geometry */
  1549. wx = sx;
  1550. wy = sy;
  1551. ww = sw;
  1552. sh = sh - bh;
  1553. /* master area geometry */
  1554. mfact = MFACT;
  1555. mx = wx;
  1556. my = wy;
  1557. mw = mfact * ww;
  1558. mh = wh;
  1559. /* tile area geometry */
  1560. tx = mx + mw;
  1561. ty = wy;
  1562. tw = ww - mw;
  1563. th = wh;
  1564. }
  1565. void
  1566. updatesizehints(Client *c) {
  1567. long msize;
  1568. XSizeHints size;
  1569. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1570. size.flags = PSize;
  1571. c->flags = size.flags;
  1572. if(c->flags & PBaseSize) {
  1573. c->basew = size.base_width;
  1574. c->baseh = size.base_height;
  1575. }
  1576. else if(c->flags & PMinSize) {
  1577. c->basew = size.min_width;
  1578. c->baseh = size.min_height;
  1579. }
  1580. else
  1581. c->basew = c->baseh = 0;
  1582. if(c->flags & PResizeInc) {
  1583. c->incw = size.width_inc;
  1584. c->inch = size.height_inc;
  1585. }
  1586. else
  1587. c->incw = c->inch = 0;
  1588. if(c->flags & PMaxSize) {
  1589. c->maxw = size.max_width;
  1590. c->maxh = size.max_height;
  1591. }
  1592. else
  1593. c->maxw = c->maxh = 0;
  1594. if(c->flags & PMinSize) {
  1595. c->minw = size.min_width;
  1596. c->minh = size.min_height;
  1597. }
  1598. else if(c->flags & PBaseSize) {
  1599. c->minw = size.base_width;
  1600. c->minh = size.base_height;
  1601. }
  1602. else
  1603. c->minw = c->minh = 0;
  1604. if(c->flags & PAspect) {
  1605. c->minax = size.min_aspect.x;
  1606. c->maxax = size.max_aspect.x;
  1607. c->minay = size.min_aspect.y;
  1608. c->maxay = size.max_aspect.y;
  1609. }
  1610. else
  1611. c->minax = c->maxax = c->minay = c->maxay = 0;
  1612. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1613. && c->maxw == c->minw && c->maxh == c->minh);
  1614. }
  1615. void
  1616. updatetitle(Client *c) {
  1617. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1618. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1619. }
  1620. void
  1621. updatewmhints(Client *c) {
  1622. XWMHints *wmh;
  1623. if((wmh = XGetWMHints(dpy, c->win))) {
  1624. if(c == sel)
  1625. sel->isurgent = False;
  1626. else
  1627. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1628. XFree(wmh);
  1629. }
  1630. }
  1631. void
  1632. view(const char *arg) {
  1633. seltags ^= 1;
  1634. memset(tagset[seltags], (NULL == arg), TAGSZ);
  1635. tagset[seltags][idxoftag(arg)] = True;
  1636. arrange();
  1637. }
  1638. void
  1639. viewprevtag(const char *arg) {
  1640. seltags ^= 1; /* toggle sel tagset */
  1641. arrange();
  1642. }
  1643. /* There's no way to check accesses to destroyed windows, thus those cases are
  1644. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1645. * default error handler, which may call exit. */
  1646. int
  1647. xerror(Display *dpy, XErrorEvent *ee) {
  1648. if(ee->error_code == BadWindow
  1649. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1650. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1651. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1652. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1653. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1654. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1655. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1656. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1657. return 0;
  1658. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1659. ee->request_code, ee->error_code);
  1660. return xerrorxlib(dpy, ee); /* may call exit */
  1661. }
  1662. int
  1663. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1664. return 0;
  1665. }
  1666. /* Startup Error handler to check if another window manager
  1667. * is already running. */
  1668. int
  1669. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1670. otherwm = True;
  1671. return -1;
  1672. }
  1673. void
  1674. zoom(const char *arg) {
  1675. Client *c = sel;
  1676. if(c == nexttiled(clients))
  1677. if(!c || !(c = nexttiled(c->next)))
  1678. return;
  1679. if(!lt->isfloating && !sel->isfloating) {
  1680. detach(c);
  1681. attach(c);
  1682. focus(c);
  1683. }
  1684. arrange();
  1685. }
  1686. int
  1687. main(int argc, char *argv[]) {
  1688. if(argc == 2 && !strcmp("-v", argv[1]))
  1689. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1690. else if(argc != 1)
  1691. eprint("usage: dwm [-v]\n");
  1692. setlocale(LC_CTYPE, "");
  1693. if(!(dpy = XOpenDisplay(0)))
  1694. eprint("dwm: cannot open display\n");
  1695. checkotherwm();
  1696. setup();
  1697. scan();
  1698. run();
  1699. cleanup();
  1700. XCloseDisplay(dpy);
  1701. return 0;
  1702. }