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.

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