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.

1952 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. unsigned int numlockmask = 0;
  206. void (*handler[LASTEvent]) (XEvent *) = {
  207. [ButtonPress] = buttonpress,
  208. [ConfigureRequest] = configurerequest,
  209. [ConfigureNotify] = configurenotify,
  210. [DestroyNotify] = destroynotify,
  211. [EnterNotify] = enternotify,
  212. [Expose] = expose,
  213. [FocusIn] = focusin,
  214. [KeyPress] = keypress,
  215. [MappingNotify] = mappingnotify,
  216. [MapRequest] = maprequest,
  217. [PropertyNotify] = propertynotify,
  218. [UnmapNotify] = unmapnotify
  219. };
  220. Atom wmatom[WMLast], netatom[NetLast];
  221. Bool otherwm, readin;
  222. Bool running = True;
  223. Bool *prevtags;
  224. Bool *seltags;
  225. Client *clients = NULL;
  226. Client *sel = NULL;
  227. Client *stack = NULL;
  228. Cursor cursor[CurLast];
  229. Display *dpy;
  230. DC dc = {0};
  231. Geom *geom = NULL;
  232. Layout *lt = NULL;
  233. Window root, barwin;
  234. /* configuration, allows nested code to access above variables */
  235. #include "config.h"
  236. #define TAGSZ (LENGTH(tags) * sizeof(Bool))
  237. static Bool tmp[LENGTH(tags)];
  238. /* function implementations */
  239. void
  240. applyrules(Client *c) {
  241. unsigned int i;
  242. Bool matched = False;
  243. Rule *r;
  244. XClassHint ch = { 0 };
  245. /* rule matching */
  246. XGetClassHint(dpy, c->win, &ch);
  247. for(i = 0; i < LENGTH(rules); i++) {
  248. r = &rules[i];
  249. if((r->title && strstr(c->name, r->title))
  250. || (ch.res_class && r->class && strstr(ch.res_class, r->class))
  251. || (ch.res_name && r->instance && strstr(ch.res_name, r->instance)))
  252. {
  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. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  739. if(focused) {
  740. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  741. GrabModeAsync, GrabModeSync, None, None);
  742. XGrabButton(dpy, Button1, MODKEY|LockMask, c->win, False, BUTTONMASK,
  743. GrabModeAsync, GrabModeSync, None, None);
  744. XGrabButton(dpy, Button1, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  745. GrabModeAsync, GrabModeSync, None, None);
  746. XGrabButton(dpy, Button1, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  747. GrabModeAsync, GrabModeSync, None, None);
  748. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  749. GrabModeAsync, GrabModeSync, None, None);
  750. XGrabButton(dpy, Button2, MODKEY|LockMask, c->win, False, BUTTONMASK,
  751. GrabModeAsync, GrabModeSync, None, None);
  752. XGrabButton(dpy, Button2, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  753. GrabModeAsync, GrabModeSync, None, None);
  754. XGrabButton(dpy, Button2, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  755. GrabModeAsync, GrabModeSync, None, None);
  756. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  757. GrabModeAsync, GrabModeSync, None, None);
  758. XGrabButton(dpy, Button3, MODKEY|LockMask, c->win, False, BUTTONMASK,
  759. GrabModeAsync, GrabModeSync, None, None);
  760. XGrabButton(dpy, Button3, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  761. GrabModeAsync, GrabModeSync, None, None);
  762. XGrabButton(dpy, Button3, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  763. GrabModeAsync, GrabModeSync, None, None);
  764. }
  765. else
  766. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  767. GrabModeAsync, GrabModeSync, None, None);
  768. }
  769. void
  770. grabkeys(void) {
  771. unsigned int i, j;
  772. KeyCode code;
  773. XModifierKeymap *modmap;
  774. /* init modifier map */
  775. modmap = XGetModifierMapping(dpy);
  776. for(i = 0; i < 8; i++)
  777. for(j = 0; j < modmap->max_keypermod; j++) {
  778. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  779. numlockmask = (1 << i);
  780. }
  781. XFreeModifiermap(modmap);
  782. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  783. for(i = 0; i < LENGTH(keys); i++) {
  784. code = XKeysymToKeycode(dpy, keys[i].keysym);
  785. XGrabKey(dpy, code, keys[i].mod, root, True,
  786. GrabModeAsync, GrabModeAsync);
  787. XGrabKey(dpy, code, keys[i].mod|LockMask, root, True,
  788. GrabModeAsync, GrabModeAsync);
  789. XGrabKey(dpy, code, keys[i].mod|numlockmask, root, True,
  790. GrabModeAsync, GrabModeAsync);
  791. XGrabKey(dpy, code, keys[i].mod|numlockmask|LockMask, root, True,
  792. GrabModeAsync, GrabModeAsync);
  793. }
  794. }
  795. unsigned int
  796. idxoftag(const char *t) {
  797. unsigned int i;
  798. for(i = 0; (i < LENGTH(tags)) && t && strcmp(tags[i], t); i++);
  799. return (i < LENGTH(tags)) ? i : 0;
  800. }
  801. void
  802. initfont(const char *fontstr) {
  803. char *def, **missing;
  804. int i, n;
  805. missing = NULL;
  806. if(dc.font.set)
  807. XFreeFontSet(dpy, dc.font.set);
  808. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  809. if(missing) {
  810. while(n--)
  811. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  812. XFreeStringList(missing);
  813. }
  814. if(dc.font.set) {
  815. XFontSetExtents *font_extents;
  816. XFontStruct **xfonts;
  817. char **font_names;
  818. dc.font.ascent = dc.font.descent = 0;
  819. font_extents = XExtentsOfFontSet(dc.font.set);
  820. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  821. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  822. if(dc.font.ascent < (*xfonts)->ascent)
  823. dc.font.ascent = (*xfonts)->ascent;
  824. if(dc.font.descent < (*xfonts)->descent)
  825. dc.font.descent = (*xfonts)->descent;
  826. xfonts++;
  827. }
  828. }
  829. else {
  830. if(dc.font.xfont)
  831. XFreeFont(dpy, dc.font.xfont);
  832. dc.font.xfont = NULL;
  833. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  834. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  835. eprint("error, cannot load font: '%s'\n", fontstr);
  836. dc.font.ascent = dc.font.xfont->ascent;
  837. dc.font.descent = dc.font.xfont->descent;
  838. }
  839. dc.font.height = dc.font.ascent + dc.font.descent;
  840. }
  841. Bool
  842. isoccupied(unsigned int t) {
  843. Client *c;
  844. for(c = clients; c; c = c->next)
  845. if(c->tags[t])
  846. return True;
  847. return False;
  848. }
  849. Bool
  850. isprotodel(Client *c) {
  851. int i, n;
  852. Atom *protocols;
  853. Bool ret = False;
  854. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  855. for(i = 0; !ret && i < n; i++)
  856. if(protocols[i] == wmatom[WMDelete])
  857. ret = True;
  858. XFree(protocols);
  859. }
  860. return ret;
  861. }
  862. Bool
  863. isurgent(unsigned int t) {
  864. Client *c;
  865. for(c = clients; c; c = c->next)
  866. if(c->isurgent && c->tags[t])
  867. return True;
  868. return False;
  869. }
  870. Bool
  871. isvisible(Client *c) {
  872. unsigned int i;
  873. for(i = 0; i < LENGTH(tags); i++)
  874. if(c->tags[i] && seltags[i])
  875. return True;
  876. return False;
  877. }
  878. void
  879. keypress(XEvent *e) {
  880. unsigned int i;
  881. KeySym keysym;
  882. XKeyEvent *ev;
  883. ev = &e->xkey;
  884. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  885. for(i = 0; i < LENGTH(keys); i++)
  886. if(keysym == keys[i].keysym
  887. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  888. {
  889. if(keys[i].func)
  890. keys[i].func(keys[i].arg);
  891. }
  892. }
  893. void
  894. killclient(const char *arg) {
  895. XEvent ev;
  896. if(!sel)
  897. return;
  898. if(isprotodel(sel)) {
  899. ev.type = ClientMessage;
  900. ev.xclient.window = sel->win;
  901. ev.xclient.message_type = wmatom[WMProtocols];
  902. ev.xclient.format = 32;
  903. ev.xclient.data.l[0] = wmatom[WMDelete];
  904. ev.xclient.data.l[1] = CurrentTime;
  905. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  906. }
  907. else
  908. XKillClient(dpy, sel->win);
  909. }
  910. void
  911. manage(Window w, XWindowAttributes *wa) {
  912. Client *c, *t = NULL;
  913. Status rettrans;
  914. Window trans;
  915. XWindowChanges wc;
  916. c = emallocz(sizeof(Client));
  917. c->tags = emallocz(TAGSZ);
  918. c->win = w;
  919. /* geometry */
  920. c->x = wa->x;
  921. c->y = wa->y;
  922. c->w = wa->width;
  923. c->h = wa->height;
  924. c->oldbw = wa->border_width;
  925. if(c->w == sw && c->h == sh) {
  926. c->x = sx;
  927. c->y = sy;
  928. c->bw = wa->border_width;
  929. }
  930. else {
  931. if(c->x + c->w + 2 * c->bw > wx + ww)
  932. c->x = wx + ww - c->w - 2 * c->bw;
  933. if(c->y + c->h + 2 * c->bw > wy + wh)
  934. c->y = wy + wh - c->h - 2 * c->bw;
  935. if(c->x < wx)
  936. c->x = wx;
  937. if(c->y < wy)
  938. c->y = wy;
  939. c->bw = BORDERPX;
  940. }
  941. wc.border_width = c->bw;
  942. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  943. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  944. configure(c); /* propagates border_width, if size doesn't change */
  945. updatesizehints(c);
  946. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  947. grabbuttons(c, False);
  948. updatetitle(c);
  949. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  950. for(t = clients; t && t->win != trans; t = t->next);
  951. if(t)
  952. memcpy(c->tags, t->tags, TAGSZ);
  953. else
  954. applyrules(c);
  955. if(!c->isfloating)
  956. c->isfloating = (rettrans == Success) || c->isfixed;
  957. attach(c);
  958. attachstack(c);
  959. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  960. ban(c);
  961. XMapWindow(dpy, c->win);
  962. setclientstate(c, NormalState);
  963. arrange();
  964. }
  965. void
  966. mappingnotify(XEvent *e) {
  967. XMappingEvent *ev = &e->xmapping;
  968. XRefreshKeyboardMapping(ev);
  969. if(ev->request == MappingKeyboard)
  970. grabkeys();
  971. }
  972. void
  973. maprequest(XEvent *e) {
  974. static XWindowAttributes wa;
  975. XMapRequestEvent *ev = &e->xmaprequest;
  976. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  977. return;
  978. if(wa.override_redirect)
  979. return;
  980. if(!getclient(ev->window))
  981. manage(ev->window, &wa);
  982. }
  983. void
  984. monocle(void) {
  985. Client *c;
  986. for(c = clients; c; c = c->next)
  987. if(isvisible(c))
  988. resize(c, mox, moy, mow - 2 * c->bw, moh - 2 * c->bw, RESIZEHINTS);
  989. }
  990. void
  991. movemouse(Client *c) {
  992. int x1, y1, ocx, ocy, di, nx, ny;
  993. unsigned int dui;
  994. Window dummy;
  995. XEvent ev;
  996. ocx = nx = c->x;
  997. ocy = ny = c->y;
  998. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  999. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1000. return;
  1001. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1002. for(;;) {
  1003. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1004. switch (ev.type) {
  1005. case ButtonRelease:
  1006. XUngrabPointer(dpy, CurrentTime);
  1007. return;
  1008. case ConfigureRequest:
  1009. case Expose:
  1010. case MapRequest:
  1011. handler[ev.type](&ev);
  1012. break;
  1013. case MotionNotify:
  1014. XSync(dpy, False);
  1015. nx = ocx + (ev.xmotion.x - x1);
  1016. ny = ocy + (ev.xmotion.y - y1);
  1017. if(abs(wx - nx) < SNAP)
  1018. nx = wx;
  1019. else if(abs((wx + ww) - (nx + c->w + 2 * c->bw)) < SNAP)
  1020. nx = wx + ww - c->w - 2 * c->bw;
  1021. if(abs(wy - ny) < SNAP)
  1022. ny = wy;
  1023. else if(abs((wy + wh) - (ny + c->h + 2 * c->bw)) < SNAP)
  1024. ny = wy + wh - c->h - 2 * c->bw;
  1025. if(!c->isfloating && !lt->isfloating && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
  1026. togglefloating(NULL);
  1027. if((lt->isfloating) || c->isfloating)
  1028. resize(c, nx, ny, c->w, c->h, False);
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. Client *
  1034. nexttiled(Client *c) {
  1035. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  1036. return c;
  1037. }
  1038. void
  1039. propertynotify(XEvent *e) {
  1040. Client *c;
  1041. Window trans;
  1042. XPropertyEvent *ev = &e->xproperty;
  1043. if(ev->state == PropertyDelete)
  1044. return; /* ignore */
  1045. if((c = getclient(ev->window))) {
  1046. switch (ev->atom) {
  1047. default: break;
  1048. case XA_WM_TRANSIENT_FOR:
  1049. XGetTransientForHint(dpy, c->win, &trans);
  1050. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1051. arrange();
  1052. break;
  1053. case XA_WM_NORMAL_HINTS:
  1054. updatesizehints(c);
  1055. break;
  1056. case XA_WM_HINTS:
  1057. updatewmhints(c);
  1058. drawbar();
  1059. break;
  1060. }
  1061. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1062. updatetitle(c);
  1063. if(c == sel)
  1064. drawbar();
  1065. }
  1066. }
  1067. }
  1068. void
  1069. quit(const char *arg) {
  1070. readin = running = False;
  1071. }
  1072. void
  1073. reapply(const char *arg) {
  1074. static Bool zerotags[LENGTH(tags)] = { 0 };
  1075. Client *c;
  1076. for(c = clients; c; c = c->next) {
  1077. memcpy(c->tags, zerotags, sizeof zerotags);
  1078. applyrules(c);
  1079. }
  1080. arrange();
  1081. }
  1082. void
  1083. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1084. XWindowChanges wc;
  1085. if(sizehints) {
  1086. /* set minimum possible */
  1087. if (w < 1)
  1088. w = 1;
  1089. if (h < 1)
  1090. h = 1;
  1091. /* temporarily remove base dimensions */
  1092. w -= c->basew;
  1093. h -= c->baseh;
  1094. /* adjust for aspect limits */
  1095. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1096. if (w * c->maxay > h * c->maxax)
  1097. w = h * c->maxax / c->maxay;
  1098. else if (w * c->minay < h * c->minax)
  1099. h = w * c->minay / c->minax;
  1100. }
  1101. /* adjust for increment value */
  1102. if(c->incw)
  1103. w -= w % c->incw;
  1104. if(c->inch)
  1105. h -= h % c->inch;
  1106. /* restore base dimensions */
  1107. w += c->basew;
  1108. h += c->baseh;
  1109. if(c->minw > 0 && w < c->minw)
  1110. w = c->minw;
  1111. if(c->minh > 0 && h < c->minh)
  1112. h = c->minh;
  1113. if(c->maxw > 0 && w > c->maxw)
  1114. w = c->maxw;
  1115. if(c->maxh > 0 && h > c->maxh)
  1116. h = c->maxh;
  1117. }
  1118. if(w <= 0 || h <= 0)
  1119. return;
  1120. if(x > sx + sw)
  1121. x = sw - w - 2 * c->bw;
  1122. if(y > sy + sh)
  1123. y = sh - h - 2 * c->bw;
  1124. if(x + w + 2 * c->bw < sx)
  1125. x = sx;
  1126. if(y + h + 2 * c->bw < sy)
  1127. y = sy;
  1128. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1129. c->x = wc.x = x;
  1130. c->y = wc.y = y;
  1131. c->w = wc.width = w;
  1132. c->h = wc.height = h;
  1133. wc.border_width = c->bw;
  1134. XConfigureWindow(dpy, c->win,
  1135. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1136. configure(c);
  1137. XSync(dpy, False);
  1138. }
  1139. }
  1140. void
  1141. resizemouse(Client *c) {
  1142. int ocx, ocy;
  1143. int nw, nh;
  1144. XEvent ev;
  1145. ocx = c->x;
  1146. ocy = c->y;
  1147. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1148. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1149. return;
  1150. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1151. for(;;) {
  1152. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1153. switch(ev.type) {
  1154. case ButtonRelease:
  1155. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1156. c->w + c->bw - 1, c->h + c->bw - 1);
  1157. XUngrabPointer(dpy, CurrentTime);
  1158. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1159. return;
  1160. case ConfigureRequest:
  1161. case Expose:
  1162. case MapRequest:
  1163. handler[ev.type](&ev);
  1164. break;
  1165. case MotionNotify:
  1166. XSync(dpy, False);
  1167. if((nw = ev.xmotion.x - ocx - 2 * c->bw + 1) <= 0)
  1168. nw = 1;
  1169. if((nh = ev.xmotion.y - ocy - 2 * c->bw + 1) <= 0)
  1170. nh = 1;
  1171. if(!c->isfloating && !lt->isfloating && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1172. togglefloating(NULL);
  1173. if((lt->isfloating) || c->isfloating)
  1174. resize(c, c->x, c->y, nw, nh, True);
  1175. break;
  1176. }
  1177. }
  1178. }
  1179. void
  1180. restack(void) {
  1181. Client *c;
  1182. XEvent ev;
  1183. XWindowChanges wc;
  1184. drawbar();
  1185. if(!sel)
  1186. return;
  1187. if(sel->isfloating || lt->isfloating)
  1188. XRaiseWindow(dpy, sel->win);
  1189. if(!lt->isfloating) {
  1190. wc.stack_mode = Below;
  1191. wc.sibling = barwin;
  1192. if(!sel->isfloating) {
  1193. XConfigureWindow(dpy, sel->win, CWSibling|CWStackMode, &wc);
  1194. wc.sibling = sel->win;
  1195. }
  1196. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  1197. if(c == sel)
  1198. continue;
  1199. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1200. wc.sibling = c->win;
  1201. }
  1202. }
  1203. XSync(dpy, False);
  1204. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1205. }
  1206. void
  1207. run(void) {
  1208. char *p;
  1209. char sbuf[sizeof stext];
  1210. fd_set rd;
  1211. int r, xfd;
  1212. unsigned int len, offset;
  1213. XEvent ev;
  1214. /* main event loop, also reads status text from stdin */
  1215. XSync(dpy, False);
  1216. xfd = ConnectionNumber(dpy);
  1217. readin = True;
  1218. offset = 0;
  1219. len = sizeof stext - 1;
  1220. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1221. while(running) {
  1222. FD_ZERO(&rd);
  1223. if(readin)
  1224. FD_SET(STDIN_FILENO, &rd);
  1225. FD_SET(xfd, &rd);
  1226. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1227. if(errno == EINTR)
  1228. continue;
  1229. eprint("select failed\n");
  1230. }
  1231. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1232. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1233. case -1:
  1234. strncpy(stext, strerror(errno), len);
  1235. readin = False;
  1236. break;
  1237. case 0:
  1238. strncpy(stext, "EOF", 4);
  1239. readin = False;
  1240. break;
  1241. default:
  1242. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1243. if(*p == '\n' || *p == '\0') {
  1244. *p = '\0';
  1245. strncpy(stext, sbuf, len);
  1246. p += r - 1; /* p is sbuf + offset + r - 1 */
  1247. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1248. offset = r;
  1249. if(r)
  1250. memmove(sbuf, p - r + 1, r);
  1251. break;
  1252. }
  1253. break;
  1254. }
  1255. drawbar();
  1256. }
  1257. while(XPending(dpy)) {
  1258. XNextEvent(dpy, &ev);
  1259. if(handler[ev.type])
  1260. (handler[ev.type])(&ev); /* call handler */
  1261. }
  1262. }
  1263. }
  1264. void
  1265. scan(void) {
  1266. unsigned int i, num;
  1267. Window *wins, d1, d2;
  1268. XWindowAttributes wa;
  1269. wins = NULL;
  1270. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1271. for(i = 0; i < num; i++) {
  1272. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1273. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1274. continue;
  1275. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1276. manage(wins[i], &wa);
  1277. }
  1278. for(i = 0; i < num; i++) { /* now the transients */
  1279. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1280. continue;
  1281. if(XGetTransientForHint(dpy, wins[i], &d1)
  1282. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1283. manage(wins[i], &wa);
  1284. }
  1285. }
  1286. if(wins)
  1287. XFree(wins);
  1288. }
  1289. void
  1290. setclientstate(Client *c, long state) {
  1291. long data[] = {state, None};
  1292. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1293. PropModeReplace, (unsigned char *)data, 2);
  1294. }
  1295. void
  1296. setgeom(const char *arg) {
  1297. unsigned int i;
  1298. if(!arg) {
  1299. if(++geom == &geoms[LENGTH(geoms)])
  1300. geom = &geoms[0];
  1301. }
  1302. else {
  1303. for(i = 0; i < LENGTH(geoms); i++)
  1304. if(!strcmp(geoms[i].symbol, arg))
  1305. break;
  1306. if(i == LENGTH(geoms))
  1307. return;
  1308. geom = &geoms[i];
  1309. }
  1310. geom->apply();
  1311. updatebarpos();
  1312. arrange();
  1313. }
  1314. void
  1315. setlayout(const char *arg) {
  1316. unsigned int i;
  1317. if(!arg) {
  1318. if(++lt == &layouts[LENGTH(layouts)])
  1319. lt = &layouts[0];
  1320. }
  1321. else {
  1322. for(i = 0; i < LENGTH(layouts); i++)
  1323. if(!strcmp(arg, layouts[i].symbol))
  1324. break;
  1325. if(i == LENGTH(layouts))
  1326. return;
  1327. lt = &layouts[i];
  1328. }
  1329. if(sel)
  1330. arrange();
  1331. else
  1332. drawbar();
  1333. }
  1334. void
  1335. setmfact(const char *arg) {
  1336. double delta;
  1337. if(!arg || lt->isfloating)
  1338. return;
  1339. delta = strtod(arg, NULL);
  1340. if(arg[0] == '-' || arg[0] == '+') {
  1341. if(mfact + delta < 0.1 || mfact + delta > 0.9)
  1342. return;
  1343. mfact += delta;
  1344. }
  1345. else {
  1346. if(delta < 0.1 || delta > 0.9)
  1347. return;
  1348. mfact = delta;
  1349. }
  1350. setgeom(geom->symbol);
  1351. }
  1352. void
  1353. setup(void) {
  1354. unsigned int i, w;
  1355. XSetWindowAttributes wa;
  1356. /* init screen */
  1357. screen = DefaultScreen(dpy);
  1358. root = RootWindow(dpy, screen);
  1359. initfont(FONT);
  1360. /* apply default geometry */
  1361. sx = 0;
  1362. sy = 0;
  1363. sw = DisplayWidth(dpy, screen);
  1364. sh = DisplayHeight(dpy, screen);
  1365. bh = dc.font.height + 2;
  1366. geom = &geoms[0];
  1367. geom->apply();
  1368. /* init atoms */
  1369. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1370. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1371. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1372. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1373. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1374. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1375. /* init cursors */
  1376. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1377. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1378. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1379. /* init appearance */
  1380. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1381. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1382. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1383. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1384. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1385. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1386. initfont(FONT);
  1387. dc.h = bh;
  1388. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1389. dc.gc = XCreateGC(dpy, root, 0, 0);
  1390. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1391. if(!dc.font.set)
  1392. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1393. /* init tags */
  1394. seltags = emallocz(TAGSZ);
  1395. prevtags = emallocz(TAGSZ);
  1396. seltags[0] = prevtags[0] = True;
  1397. /* init layouts */
  1398. lt = &layouts[0];
  1399. /* init bar */
  1400. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1401. w = textw(layouts[i].symbol);
  1402. if(w > blw)
  1403. blw = w;
  1404. }
  1405. for(bgw = i = 0; LENGTH(geoms) > 1 && i < LENGTH(geoms); i++) {
  1406. w = textw(geoms[i].symbol);
  1407. if(w > bgw)
  1408. bgw = w;
  1409. }
  1410. wa.override_redirect = 1;
  1411. wa.background_pixmap = ParentRelative;
  1412. wa.event_mask = ButtonPressMask|ExposureMask;
  1413. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  1414. CopyFromParent, DefaultVisual(dpy, screen),
  1415. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1416. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1417. XMapRaised(dpy, barwin);
  1418. strcpy(stext, "dwm-"VERSION);
  1419. drawbar();
  1420. /* EWMH support per view */
  1421. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1422. PropModeReplace, (unsigned char *) netatom, NetLast);
  1423. /* select for events */
  1424. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1425. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1426. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1427. XSelectInput(dpy, root, wa.event_mask);
  1428. /* grab keys */
  1429. grabkeys();
  1430. }
  1431. void
  1432. spawn(const char *arg) {
  1433. static char *shell = NULL;
  1434. if(!shell && !(shell = getenv("SHELL")))
  1435. shell = "/bin/sh";
  1436. if(!arg)
  1437. return;
  1438. /* The double-fork construct avoids zombie processes and keeps the code
  1439. * clean from stupid signal handlers. */
  1440. if(fork() == 0) {
  1441. if(fork() == 0) {
  1442. if(dpy)
  1443. close(ConnectionNumber(dpy));
  1444. setsid();
  1445. execl(shell, shell, "-c", arg, (char *)NULL);
  1446. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1447. perror(" failed");
  1448. }
  1449. exit(0);
  1450. }
  1451. wait(0);
  1452. }
  1453. void
  1454. tag(const char *arg) {
  1455. unsigned int i;
  1456. if(!sel)
  1457. return;
  1458. for(i = 0; i < LENGTH(tags); i++)
  1459. sel->tags[i] = (NULL == arg);
  1460. sel->tags[idxoftag(arg)] = True;
  1461. arrange();
  1462. }
  1463. unsigned int
  1464. textnw(const char *text, unsigned int len) {
  1465. XRectangle r;
  1466. if(dc.font.set) {
  1467. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1468. return r.width;
  1469. }
  1470. return XTextWidth(dc.font.xfont, text, len);
  1471. }
  1472. unsigned int
  1473. textw(const char *text) {
  1474. return textnw(text, strlen(text)) + dc.font.height;
  1475. }
  1476. void
  1477. tileh(void) {
  1478. int x, w;
  1479. unsigned int i, n = counttiled();
  1480. Client *c;
  1481. if(n == 0)
  1482. return;
  1483. c = tilemaster(n);
  1484. if(--n == 0)
  1485. return;
  1486. x = tx;
  1487. w = tw / n;
  1488. if(w < bh)
  1489. w = tw;
  1490. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1491. if(i + 1 == n) /* remainder */
  1492. tileresize(c, x, ty, (tx + tw) - x - 2 * c->bw, th - 2 * c->bw);
  1493. else
  1494. tileresize(c, x, ty, w - 2 * c->bw, th - 2 * c->bw);
  1495. if(w != tw)
  1496. x = c->x + c->w + 2 * c->bw;
  1497. }
  1498. }
  1499. Client *
  1500. tilemaster(unsigned int n) {
  1501. Client *c = nexttiled(clients);
  1502. if(n == 1)
  1503. tileresize(c, mox, moy, mow - 2 * c->bw, moh - 2 * c->bw);
  1504. else
  1505. tileresize(c, mx, my, mw - 2 * c->bw, mh - 2 * c->bw);
  1506. return c;
  1507. }
  1508. void
  1509. tileresize(Client *c, int x, int y, int w, int h) {
  1510. resize(c, x, y, w, h, RESIZEHINTS);
  1511. if((RESIZEHINTS) && ((c->h < bh) || (c->h > h) || (c->w < bh) || (c->w > w)))
  1512. /* client doesn't accept size constraints */
  1513. resize(c, x, y, w, h, False);
  1514. }
  1515. void
  1516. tilev(void) {
  1517. int y, h;
  1518. unsigned int i, n = counttiled();
  1519. Client *c;
  1520. if(n == 0)
  1521. return;
  1522. c = tilemaster(n);
  1523. if(--n == 0)
  1524. return;
  1525. y = ty;
  1526. h = th / n;
  1527. if(h < bh)
  1528. h = th;
  1529. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1530. if(i + 1 == n) /* remainder */
  1531. tileresize(c, tx, y, tw - 2 * c->bw, (ty + th) - y - 2 * c->bw);
  1532. else
  1533. tileresize(c, tx, y, tw - 2 * c->bw, h - 2 * c->bw);
  1534. if(h != th)
  1535. y = c->y + c->h + 2 * c->bw;
  1536. }
  1537. }
  1538. void
  1539. togglefloating(const char *arg) {
  1540. if(!sel)
  1541. return;
  1542. sel->isfloating = !sel->isfloating;
  1543. if(sel->isfloating)
  1544. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1545. arrange();
  1546. }
  1547. void
  1548. toggletag(const char *arg) {
  1549. unsigned int i, j;
  1550. if(!sel)
  1551. return;
  1552. i = idxoftag(arg);
  1553. sel->tags[i] = !sel->tags[i];
  1554. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1555. if(j == LENGTH(tags))
  1556. sel->tags[i] = True; /* at least one tag must be enabled */
  1557. arrange();
  1558. }
  1559. void
  1560. toggleview(const char *arg) {
  1561. unsigned int i, j;
  1562. i = idxoftag(arg);
  1563. seltags[i] = !seltags[i];
  1564. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1565. if(j == LENGTH(tags))
  1566. seltags[i] = True; /* at least one tag must be viewed */
  1567. arrange();
  1568. }
  1569. void
  1570. unban(Client *c) {
  1571. if(!c->isbanned)
  1572. return;
  1573. XMoveWindow(dpy, c->win, c->x, c->y);
  1574. c->isbanned = False;
  1575. }
  1576. void
  1577. unmanage(Client *c) {
  1578. XWindowChanges wc;
  1579. wc.border_width = c->oldbw;
  1580. /* The server grab construct avoids race conditions. */
  1581. XGrabServer(dpy);
  1582. XSetErrorHandler(xerrordummy);
  1583. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1584. detach(c);
  1585. detachstack(c);
  1586. if(sel == c)
  1587. focus(NULL);
  1588. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1589. setclientstate(c, WithdrawnState);
  1590. free(c->tags);
  1591. free(c);
  1592. XSync(dpy, False);
  1593. XSetErrorHandler(xerror);
  1594. XUngrabServer(dpy);
  1595. arrange();
  1596. }
  1597. void
  1598. unmapnotify(XEvent *e) {
  1599. Client *c;
  1600. XUnmapEvent *ev = &e->xunmap;
  1601. if((c = getclient(ev->window)))
  1602. unmanage(c);
  1603. }
  1604. void
  1605. updatebarpos(void) {
  1606. if(dc.drawable != 0)
  1607. XFreePixmap(dpy, dc.drawable);
  1608. dc.drawable = XCreatePixmap(dpy, root, bw, bh, DefaultDepth(dpy, screen));
  1609. XMoveResizeWindow(dpy, barwin, bx, by, bw, bh);
  1610. }
  1611. void
  1612. updatesizehints(Client *c) {
  1613. long msize;
  1614. XSizeHints size;
  1615. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1616. size.flags = PSize;
  1617. c->flags = size.flags;
  1618. if(c->flags & PBaseSize) {
  1619. c->basew = size.base_width;
  1620. c->baseh = size.base_height;
  1621. }
  1622. else if(c->flags & PMinSize) {
  1623. c->basew = size.min_width;
  1624. c->baseh = size.min_height;
  1625. }
  1626. else
  1627. c->basew = c->baseh = 0;
  1628. if(c->flags & PResizeInc) {
  1629. c->incw = size.width_inc;
  1630. c->inch = size.height_inc;
  1631. }
  1632. else
  1633. c->incw = c->inch = 0;
  1634. if(c->flags & PMaxSize) {
  1635. c->maxw = size.max_width;
  1636. c->maxh = size.max_height;
  1637. }
  1638. else
  1639. c->maxw = c->maxh = 0;
  1640. if(c->flags & PMinSize) {
  1641. c->minw = size.min_width;
  1642. c->minh = size.min_height;
  1643. }
  1644. else if(c->flags & PBaseSize) {
  1645. c->minw = size.base_width;
  1646. c->minh = size.base_height;
  1647. }
  1648. else
  1649. c->minw = c->minh = 0;
  1650. if(c->flags & PAspect) {
  1651. c->minax = size.min_aspect.x;
  1652. c->maxax = size.max_aspect.x;
  1653. c->minay = size.min_aspect.y;
  1654. c->maxay = size.max_aspect.y;
  1655. }
  1656. else
  1657. c->minax = c->maxax = c->minay = c->maxay = 0;
  1658. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1659. && c->maxw == c->minw && c->maxh == c->minh);
  1660. }
  1661. void
  1662. updatetitle(Client *c) {
  1663. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1664. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1665. }
  1666. void
  1667. updatewmhints(Client *c) {
  1668. XWMHints *wmh;
  1669. if((wmh = XGetWMHints(dpy, c->win))) {
  1670. if(c == sel)
  1671. sel->isurgent = False;
  1672. else
  1673. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1674. XFree(wmh);
  1675. }
  1676. }
  1677. void
  1678. view(const char *arg) {
  1679. unsigned int i;
  1680. for(i = 0; i < LENGTH(tags); i++)
  1681. tmp[i] = (NULL == arg);
  1682. tmp[idxoftag(arg)] = True;
  1683. if(memcmp(seltags, tmp, TAGSZ) != 0) {
  1684. memcpy(prevtags, seltags, TAGSZ);
  1685. memcpy(seltags, tmp, TAGSZ);
  1686. arrange();
  1687. }
  1688. }
  1689. void
  1690. viewprevtag(const char *arg) {
  1691. memcpy(tmp, seltags, TAGSZ);
  1692. memcpy(seltags, prevtags, TAGSZ);
  1693. memcpy(prevtags, tmp, TAGSZ);
  1694. arrange();
  1695. }
  1696. /* There's no way to check accesses to destroyed windows, thus those cases are
  1697. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1698. * default error handler, which may call exit. */
  1699. int
  1700. xerror(Display *dpy, XErrorEvent *ee) {
  1701. if(ee->error_code == BadWindow
  1702. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1703. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1704. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1705. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1706. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1707. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1708. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1709. return 0;
  1710. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1711. ee->request_code, ee->error_code);
  1712. return xerrorxlib(dpy, ee); /* may call exit */
  1713. }
  1714. int
  1715. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1716. return 0;
  1717. }
  1718. /* Startup Error handler to check if another window manager
  1719. * is already running. */
  1720. int
  1721. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1722. otherwm = True;
  1723. return -1;
  1724. }
  1725. void
  1726. zoom(const char *arg) {
  1727. Client *c = sel;
  1728. if(!sel || lt->isfloating || sel->isfloating)
  1729. return;
  1730. if(c == nexttiled(clients))
  1731. if(!(c = nexttiled(c->next)))
  1732. return;
  1733. detach(c);
  1734. attach(c);
  1735. focus(c);
  1736. arrange();
  1737. }
  1738. int
  1739. main(int argc, char *argv[]) {
  1740. if(argc == 2 && !strcmp("-v", argv[1]))
  1741. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1742. else if(argc != 1)
  1743. eprint("usage: dwm [-v]\n");
  1744. setlocale(LC_CTYPE, "");
  1745. if(!(dpy = XOpenDisplay(0)))
  1746. eprint("dwm: cannot open display\n");
  1747. checkotherwm();
  1748. setup();
  1749. scan();
  1750. run();
  1751. cleanup();
  1752. XCloseDisplay(dpy);
  1753. return 0;
  1754. }