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.

1879 lines
42 KiB

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