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