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.

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