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.

1919 lines
43 KiB

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