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.

1932 lines
44 KiB

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