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.

1936 lines
44 KiB

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