Configuration file for DWM on MacBook Air
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.

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