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.

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