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.

1910 lines
44 KiB

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