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.

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