Configuration of dwm for Mac Computers
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. c->x = wa->x + sx;
  909. c->y = wa->y + sy;
  910. c->w = wa->width;
  911. c->h = wa->height;
  912. c->oldborder = wa->border_width;
  913. if(c->w == sw && c->h == sh) {
  914. c->x = sx;
  915. c->y = sy;
  916. c->border = wa->border_width;
  917. }
  918. else {
  919. if(c->x + c->w + 2 * c->border > wax + waw)
  920. c->x = wax + waw - c->w - 2 * c->border;
  921. if(c->y + c->h + 2 * c->border > way + wah)
  922. c->y = way + wah - c->h - 2 * c->border;
  923. if(c->x < wax)
  924. c->x = wax;
  925. if(c->y < way)
  926. c->y = way;
  927. c->border = BORDERPX;
  928. }
  929. wc.border_width = c->border;
  930. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  931. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  932. configure(c); /* propagates border_width, if size doesn't change */
  933. updatesizehints(c);
  934. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  935. grabbuttons(c, False);
  936. updatetitle(c);
  937. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  938. for(t = clients; t && t->win != trans; t = t->next);
  939. if(t)
  940. memcpy(c->tags, t->tags, TAGSZ);
  941. else
  942. applyrules(c);
  943. if(!c->isfloating)
  944. c->isfloating = (rettrans == Success) || c->isfixed;
  945. attach(c);
  946. attachstack(c);
  947. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  948. ban(c);
  949. XMapWindow(dpy, c->win);
  950. setclientstate(c, NormalState);
  951. arrange();
  952. }
  953. void
  954. mappingnotify(XEvent *e) {
  955. XMappingEvent *ev = &e->xmapping;
  956. XRefreshKeyboardMapping(ev);
  957. if(ev->request == MappingKeyboard)
  958. grabkeys();
  959. }
  960. void
  961. maprequest(XEvent *e) {
  962. static XWindowAttributes wa;
  963. XMapRequestEvent *ev = &e->xmaprequest;
  964. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  965. return;
  966. if(wa.override_redirect)
  967. return;
  968. if(!getclient(ev->window))
  969. manage(ev->window, &wa);
  970. }
  971. void
  972. movemouse(Client *c) {
  973. int x1, y1, ocx, ocy, di, nx, ny;
  974. unsigned int dui;
  975. Window dummy;
  976. XEvent ev;
  977. ocx = nx = c->x;
  978. ocy = ny = c->y;
  979. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  980. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  981. return;
  982. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  983. for(;;) {
  984. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  985. switch (ev.type) {
  986. case ButtonRelease:
  987. XUngrabPointer(dpy, CurrentTime);
  988. return;
  989. case ConfigureRequest:
  990. case Expose:
  991. case MapRequest:
  992. handler[ev.type](&ev);
  993. break;
  994. case MotionNotify:
  995. XSync(dpy, False);
  996. nx = ocx + (ev.xmotion.x - x1);
  997. ny = ocy + (ev.xmotion.y - y1);
  998. if(abs(wax - nx) < SNAP)
  999. nx = wax;
  1000. else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1001. nx = wax + waw - c->w - 2 * c->border;
  1002. if(abs(way - ny) < SNAP)
  1003. ny = way;
  1004. else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1005. ny = way + wah - c->h - 2 * c->border;
  1006. if(!c->isfloating && (lt->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
  1007. togglefloating(NULL);
  1008. if((lt->arrange == floating) || c->isfloating)
  1009. resize(c, nx, ny, c->w, c->h, False);
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. Client *
  1015. nexttiled(Client *c) {
  1016. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  1017. return c;
  1018. }
  1019. void
  1020. propertynotify(XEvent *e) {
  1021. Client *c;
  1022. Window trans;
  1023. XPropertyEvent *ev = &e->xproperty;
  1024. if(ev->state == PropertyDelete)
  1025. return; /* ignore */
  1026. if((c = getclient(ev->window))) {
  1027. switch (ev->atom) {
  1028. default: break;
  1029. case XA_WM_TRANSIENT_FOR:
  1030. XGetTransientForHint(dpy, c->win, &trans);
  1031. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1032. arrange();
  1033. break;
  1034. case XA_WM_NORMAL_HINTS:
  1035. updatesizehints(c);
  1036. break;
  1037. case XA_WM_HINTS:
  1038. updatewmhints(c);
  1039. drawbar();
  1040. break;
  1041. }
  1042. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1043. updatetitle(c);
  1044. if(c == sel)
  1045. drawbar();
  1046. }
  1047. }
  1048. }
  1049. void
  1050. quit(const char *arg) {
  1051. readin = running = False;
  1052. }
  1053. void
  1054. reapply(const char *arg) {
  1055. static Bool zerotags[LENGTH(tags)] = { 0 };
  1056. Client *c;
  1057. for(c = clients; c; c = c->next) {
  1058. memcpy(c->tags, zerotags, sizeof zerotags);
  1059. applyrules(c);
  1060. }
  1061. arrange();
  1062. }
  1063. void
  1064. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1065. XWindowChanges wc;
  1066. if(sizehints) {
  1067. /* set minimum possible */
  1068. if (w < 1)
  1069. w = 1;
  1070. if (h < 1)
  1071. h = 1;
  1072. /* temporarily remove base dimensions */
  1073. w -= c->basew;
  1074. h -= c->baseh;
  1075. /* adjust for aspect limits */
  1076. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1077. if (w * c->maxay > h * c->maxax)
  1078. w = h * c->maxax / c->maxay;
  1079. else if (w * c->minay < h * c->minax)
  1080. h = w * c->minay / c->minax;
  1081. }
  1082. /* adjust for increment value */
  1083. if(c->incw)
  1084. w -= w % c->incw;
  1085. if(c->inch)
  1086. h -= h % c->inch;
  1087. /* restore base dimensions */
  1088. w += c->basew;
  1089. h += c->baseh;
  1090. if(c->minw > 0 && w < c->minw)
  1091. w = c->minw;
  1092. if(c->minh > 0 && h < c->minh)
  1093. h = c->minh;
  1094. if(c->maxw > 0 && w > c->maxw)
  1095. w = c->maxw;
  1096. if(c->maxh > 0 && h > c->maxh)
  1097. h = c->maxh;
  1098. }
  1099. if(w <= 0 || h <= 0)
  1100. return;
  1101. if(x > sx + sw)
  1102. x = sw - w - 2 * c->border;
  1103. if(y > sy + sh)
  1104. y = sh - h - 2 * c->border;
  1105. if(x + w + 2 * c->border < sx)
  1106. x = sx;
  1107. if(y + h + 2 * c->border < sy)
  1108. y = sy;
  1109. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1110. c->x = wc.x = x;
  1111. c->y = wc.y = y;
  1112. c->w = wc.width = w;
  1113. c->h = wc.height = h;
  1114. wc.border_width = c->border;
  1115. XConfigureWindow(dpy, c->win,
  1116. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1117. configure(c);
  1118. XSync(dpy, False);
  1119. }
  1120. }
  1121. void
  1122. resizemouse(Client *c) {
  1123. int ocx, ocy;
  1124. int nw, nh;
  1125. XEvent ev;
  1126. ocx = c->x;
  1127. ocy = c->y;
  1128. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1129. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1130. return;
  1131. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1132. for(;;) {
  1133. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1134. switch(ev.type) {
  1135. case ButtonRelease:
  1136. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1137. c->w + c->border - 1, c->h + c->border - 1);
  1138. XUngrabPointer(dpy, CurrentTime);
  1139. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1140. return;
  1141. case ConfigureRequest:
  1142. case Expose:
  1143. case MapRequest:
  1144. handler[ev.type](&ev);
  1145. break;
  1146. case MotionNotify:
  1147. XSync(dpy, False);
  1148. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1149. nw = 1;
  1150. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1151. nh = 1;
  1152. if(!c->isfloating && (lt->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1153. togglefloating(NULL);
  1154. if((lt->arrange == floating) || c->isfloating)
  1155. resize(c, c->x, c->y, nw, nh, True);
  1156. break;
  1157. }
  1158. }
  1159. }
  1160. void
  1161. restack(void) {
  1162. Client *c;
  1163. XEvent ev;
  1164. XWindowChanges wc;
  1165. drawbar();
  1166. if(!sel)
  1167. return;
  1168. if(sel->isfloating || (lt->arrange == floating))
  1169. XRaiseWindow(dpy, sel->win);
  1170. if(lt->arrange != floating) {
  1171. wc.stack_mode = Below;
  1172. wc.sibling = barwin;
  1173. if(!sel->isfloating) {
  1174. XConfigureWindow(dpy, sel->win, CWSibling|CWStackMode, &wc);
  1175. wc.sibling = sel->win;
  1176. }
  1177. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  1178. if(c == sel)
  1179. continue;
  1180. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1181. wc.sibling = c->win;
  1182. }
  1183. }
  1184. XSync(dpy, False);
  1185. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1186. }
  1187. void
  1188. run(void) {
  1189. char *p;
  1190. char sbuf[sizeof stext];
  1191. fd_set rd;
  1192. int r, xfd;
  1193. unsigned int len, offset;
  1194. XEvent ev;
  1195. /* main event loop, also reads status text from stdin */
  1196. XSync(dpy, False);
  1197. xfd = ConnectionNumber(dpy);
  1198. readin = True;
  1199. offset = 0;
  1200. len = sizeof stext - 1;
  1201. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1202. while(running) {
  1203. FD_ZERO(&rd);
  1204. if(readin)
  1205. FD_SET(STDIN_FILENO, &rd);
  1206. FD_SET(xfd, &rd);
  1207. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1208. if(errno == EINTR)
  1209. continue;
  1210. eprint("select failed\n");
  1211. }
  1212. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1213. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1214. case -1:
  1215. strncpy(stext, strerror(errno), len);
  1216. readin = False;
  1217. break;
  1218. case 0:
  1219. strncpy(stext, "EOF", 4);
  1220. readin = False;
  1221. break;
  1222. default:
  1223. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1224. if(*p == '\n' || *p == '\0') {
  1225. *p = '\0';
  1226. strncpy(stext, sbuf, len);
  1227. p += r - 1; /* p is sbuf + offset + r - 1 */
  1228. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1229. offset = r;
  1230. if(r)
  1231. memmove(sbuf, p - r + 1, r);
  1232. break;
  1233. }
  1234. break;
  1235. }
  1236. drawbar();
  1237. }
  1238. while(XPending(dpy)) {
  1239. XNextEvent(dpy, &ev);
  1240. if(handler[ev.type])
  1241. (handler[ev.type])(&ev); /* call handler */
  1242. }
  1243. }
  1244. }
  1245. void
  1246. scan(void) {
  1247. unsigned int i, num;
  1248. Window *wins, d1, d2;
  1249. XWindowAttributes wa;
  1250. wins = NULL;
  1251. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1252. for(i = 0; i < num; i++) {
  1253. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1254. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1255. continue;
  1256. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1257. manage(wins[i], &wa);
  1258. }
  1259. for(i = 0; i < num; i++) { /* now the transients */
  1260. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1261. continue;
  1262. if(XGetTransientForHint(dpy, wins[i], &d1)
  1263. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1264. manage(wins[i], &wa);
  1265. }
  1266. }
  1267. if(wins)
  1268. XFree(wins);
  1269. }
  1270. void
  1271. setclientstate(Client *c, long state) {
  1272. long data[] = {state, None};
  1273. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1274. PropModeReplace, (unsigned char *)data, 2);
  1275. }
  1276. void
  1277. setlayout(const char *arg) {
  1278. unsigned int i;
  1279. if(!arg) {
  1280. lt++;
  1281. if(lt == &layouts[LENGTH(layouts)])
  1282. lt = &layouts[0];
  1283. }
  1284. else {
  1285. for(i = 0; i < LENGTH(layouts); i++)
  1286. if(arg == layouts[i].symbol)
  1287. break;
  1288. if(i == LENGTH(layouts))
  1289. return;
  1290. lt = &layouts[i];
  1291. }
  1292. if(sel)
  1293. arrange();
  1294. else
  1295. drawbar();
  1296. }
  1297. void
  1298. setmwfact(const char *arg) {
  1299. double delta;
  1300. if(!domwfact)
  1301. return;
  1302. /* arg handling, manipulate mwfact */
  1303. if(arg == NULL)
  1304. mwfact = MWFACT;
  1305. else if(sscanf(arg, "%lf", &delta) == 1) {
  1306. if(arg[0] == '+' || arg[0] == '-')
  1307. mwfact += delta;
  1308. else
  1309. mwfact = delta;
  1310. if(mwfact < 0.1)
  1311. mwfact = 0.1;
  1312. else if(mwfact > 0.9)
  1313. mwfact = 0.9;
  1314. }
  1315. arrange();
  1316. }
  1317. void
  1318. setup(void) {
  1319. unsigned int i;
  1320. XSetWindowAttributes wa;
  1321. /* init screen */
  1322. screen = DefaultScreen(dpy);
  1323. root = RootWindow(dpy, screen);
  1324. sx = 0;
  1325. sy = 0;
  1326. sw = DisplayWidth(dpy, screen);
  1327. sh = DisplayHeight(dpy, screen);
  1328. /* init atoms */
  1329. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1330. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1331. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1332. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1333. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1334. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1335. /* init cursors */
  1336. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1337. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1338. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1339. #ifdef XINERAMA
  1340. if(XineramaIsActive(dpy))
  1341. info = XineramaQueryScreens(dpy, &xscreens);
  1342. #endif
  1343. /* init appearance */
  1344. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1345. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1346. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1347. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1348. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1349. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1350. initfont(FONT);
  1351. dc.h = bh = dc.font.height + 2;
  1352. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1353. dc.gc = XCreateGC(dpy, root, 0, 0);
  1354. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1355. if(!dc.font.set)
  1356. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1357. /* init tags */
  1358. seltags = emallocz(TAGSZ);
  1359. prevtags = emallocz(TAGSZ);
  1360. seltags[0] = prevtags[0] = True;
  1361. /* init layouts */
  1362. mwfact = MWFACT;
  1363. lt = &layouts[0];
  1364. /* TODO: Xinerama hints ? */
  1365. /* init bar */
  1366. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1367. i = textw(layouts[i].symbol);
  1368. if(i > blw)
  1369. blw = i;
  1370. }
  1371. bpos = BARPOS;
  1372. wa.override_redirect = 1;
  1373. wa.background_pixmap = ParentRelative;
  1374. wa.event_mask = ButtonPressMask|ExposureMask;
  1375. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0, DefaultDepth(dpy, screen),
  1376. CopyFromParent, DefaultVisual(dpy, screen),
  1377. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1378. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1379. updatebarpos();
  1380. XMapRaised(dpy, barwin);
  1381. strcpy(stext, "dwm-"VERSION);
  1382. drawbar();
  1383. /* EWMH support per view */
  1384. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1385. PropModeReplace, (unsigned char *) netatom, NetLast);
  1386. /* select for events */
  1387. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1388. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1389. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1390. XSelectInput(dpy, root, wa.event_mask);
  1391. /* grab keys */
  1392. grabkeys();
  1393. }
  1394. void
  1395. spawn(const char *arg) {
  1396. static char *shell = NULL;
  1397. if(!shell && !(shell = getenv("SHELL")))
  1398. shell = "/bin/sh";
  1399. if(!arg)
  1400. return;
  1401. /* The double-fork construct avoids zombie processes and keeps the code
  1402. * clean from stupid signal handlers. */
  1403. if(fork() == 0) {
  1404. if(fork() == 0) {
  1405. if(dpy)
  1406. close(ConnectionNumber(dpy));
  1407. setsid();
  1408. execl(shell, shell, "-c", arg, (char *)NULL);
  1409. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1410. perror(" failed");
  1411. }
  1412. exit(0);
  1413. }
  1414. wait(0);
  1415. }
  1416. void
  1417. tag(const char *arg) {
  1418. unsigned int i;
  1419. if(!sel)
  1420. return;
  1421. for(i = 0; i < LENGTH(tags); i++)
  1422. sel->tags[i] = (NULL == arg);
  1423. sel->tags[idxoftag(arg)] = True;
  1424. arrange();
  1425. }
  1426. unsigned int
  1427. textnw(const char *text, unsigned int len) {
  1428. XRectangle r;
  1429. if(dc.font.set) {
  1430. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1431. return r.width;
  1432. }
  1433. return XTextWidth(dc.font.xfont, text, len);
  1434. }
  1435. unsigned int
  1436. textw(const char *text) {
  1437. return textnw(text, strlen(text)) + dc.font.height;
  1438. }
  1439. void
  1440. tile(void) {
  1441. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1442. Client *c, *mc;
  1443. domwfact = dozoom = True;
  1444. nx = wax;
  1445. ny = way;
  1446. nw = 0;
  1447. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  1448. n++;
  1449. /* window geoms */
  1450. mw = (n == 1) ? waw : mwfact * waw;
  1451. th = (n > 1) ? wah / (n - 1) : 0;
  1452. if(n > 1 && th < bh)
  1453. th = wah;
  1454. for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next)) {
  1455. if(i == 0) { /* master */
  1456. nw = mw - 2 * c->border;
  1457. nh = wah - 2 * c->border;
  1458. }
  1459. else { /* tile window */
  1460. if(i == 1) {
  1461. ny = way;
  1462. nx += mc->w + 2 * mc->border;
  1463. nw = waw - mw - 2 * c->border;
  1464. }
  1465. if(i + 1 == n) /* remainder */
  1466. nh = (way + wah) - ny - 2 * c->border;
  1467. else
  1468. nh = th - 2 * c->border;
  1469. }
  1470. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1471. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1472. /* client doesn't accept size constraints */
  1473. resize(c, nx, ny, nw, nh, False);
  1474. if(n > 1 && th != wah)
  1475. ny = c->y + c->h + 2 * c->border;
  1476. i++;
  1477. }
  1478. }
  1479. void
  1480. togglebar(const char *arg) {
  1481. if(bpos == BarOff)
  1482. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1483. else
  1484. bpos = BarOff;
  1485. updatebarpos();
  1486. arrange();
  1487. }
  1488. void
  1489. togglefloating(const char *arg) {
  1490. if(!sel)
  1491. return;
  1492. sel->isfloating = !sel->isfloating;
  1493. if(sel->isfloating)
  1494. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1495. arrange();
  1496. }
  1497. void
  1498. toggletag(const char *arg) {
  1499. unsigned int i, j;
  1500. if(!sel)
  1501. return;
  1502. i = idxoftag(arg);
  1503. if(conflicts(sel, i))
  1504. return;
  1505. sel->tags[i] = !sel->tags[i];
  1506. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1507. if(j == LENGTH(tags))
  1508. sel->tags[i] = True; /* at least one tag must be enabled */
  1509. arrange();
  1510. }
  1511. void
  1512. toggleview(const char *arg) {
  1513. unsigned int i, j;
  1514. i = idxoftag(arg);
  1515. seltags[i] = !seltags[i];
  1516. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1517. if(j == LENGTH(tags))
  1518. seltags[i] = True; /* at least one tag must be viewed */
  1519. arrange();
  1520. }
  1521. void
  1522. unban(Client *c) {
  1523. if(!c->isbanned)
  1524. return;
  1525. XMoveWindow(dpy, c->win, c->x, c->y);
  1526. c->isbanned = False;
  1527. }
  1528. void
  1529. unmanage(Client *c) {
  1530. XWindowChanges wc;
  1531. wc.border_width = c->oldborder;
  1532. /* The server grab construct avoids race conditions. */
  1533. XGrabServer(dpy);
  1534. XSetErrorHandler(xerrordummy);
  1535. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1536. detach(c);
  1537. detachstack(c);
  1538. if(sel == c)
  1539. focus(NULL);
  1540. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1541. setclientstate(c, WithdrawnState);
  1542. free(c->tags);
  1543. free(c);
  1544. XSync(dpy, False);
  1545. XSetErrorHandler(xerror);
  1546. XUngrabServer(dpy);
  1547. arrange();
  1548. }
  1549. void
  1550. unmapnotify(XEvent *e) {
  1551. Client *c;
  1552. XUnmapEvent *ev = &e->xunmap;
  1553. if((c = getclient(ev->window)))
  1554. unmanage(c);
  1555. }
  1556. void
  1557. updatebarpos(void) {
  1558. XEvent ev;
  1559. wax = sx;
  1560. way = sy;
  1561. wah = sh;
  1562. waw = sw;
  1563. switch(bpos) {
  1564. default:
  1565. wah -= bh;
  1566. way += bh;
  1567. XMoveWindow(dpy, barwin, sx, sy);
  1568. break;
  1569. case BarBot:
  1570. wah -= bh;
  1571. XMoveWindow(dpy, barwin, sx, sy + wah);
  1572. break;
  1573. case BarOff:
  1574. XMoveWindow(dpy, barwin, sx, sy - bh);
  1575. break;
  1576. }
  1577. XSync(dpy, False);
  1578. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1579. }
  1580. void
  1581. updatesizehints(Client *c) {
  1582. long msize;
  1583. XSizeHints size;
  1584. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1585. size.flags = PSize;
  1586. c->flags = size.flags;
  1587. if(c->flags & PBaseSize) {
  1588. c->basew = size.base_width;
  1589. c->baseh = size.base_height;
  1590. }
  1591. else if(c->flags & PMinSize) {
  1592. c->basew = size.min_width;
  1593. c->baseh = size.min_height;
  1594. }
  1595. else
  1596. c->basew = c->baseh = 0;
  1597. if(c->flags & PResizeInc) {
  1598. c->incw = size.width_inc;
  1599. c->inch = size.height_inc;
  1600. }
  1601. else
  1602. c->incw = c->inch = 0;
  1603. if(c->flags & PMaxSize) {
  1604. c->maxw = size.max_width;
  1605. c->maxh = size.max_height;
  1606. }
  1607. else
  1608. c->maxw = c->maxh = 0;
  1609. if(c->flags & PMinSize) {
  1610. c->minw = size.min_width;
  1611. c->minh = size.min_height;
  1612. }
  1613. else if(c->flags & PBaseSize) {
  1614. c->minw = size.base_width;
  1615. c->minh = size.base_height;
  1616. }
  1617. else
  1618. c->minw = c->minh = 0;
  1619. if(c->flags & PAspect) {
  1620. c->minax = size.min_aspect.x;
  1621. c->maxax = size.max_aspect.x;
  1622. c->minay = size.min_aspect.y;
  1623. c->maxay = size.max_aspect.y;
  1624. }
  1625. else
  1626. c->minax = c->maxax = c->minay = c->maxay = 0;
  1627. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1628. && c->maxw == c->minw && c->maxh == c->minh);
  1629. }
  1630. void
  1631. updatetitle(Client *c) {
  1632. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1633. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1634. }
  1635. void
  1636. updatewmhints(Client *c) {
  1637. XWMHints *wmh;
  1638. if((wmh = XGetWMHints(dpy, c->win))) {
  1639. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1640. XFree(wmh);
  1641. }
  1642. }
  1643. void
  1644. view(const char *arg) {
  1645. unsigned int i;
  1646. for(i = 0; i < LENGTH(tags); i++)
  1647. tmp[i] = (NULL == arg);
  1648. tmp[idxoftag(arg)] = True;
  1649. if(memcmp(seltags, tmp, TAGSZ) != 0) {
  1650. memcpy(prevtags, seltags, TAGSZ);
  1651. memcpy(seltags, tmp, TAGSZ);
  1652. arrange();
  1653. }
  1654. }
  1655. void
  1656. viewprevtag(const char *arg) {
  1657. memcpy(tmp, seltags, TAGSZ);
  1658. memcpy(seltags, prevtags, TAGSZ);
  1659. memcpy(prevtags, tmp, TAGSZ);
  1660. arrange();
  1661. }
  1662. /* There's no way to check accesses to destroyed windows, thus those cases are
  1663. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1664. * default error handler, which may call exit. */
  1665. int
  1666. xerror(Display *dpy, XErrorEvent *ee) {
  1667. if(ee->error_code == BadWindow
  1668. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1669. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1670. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1671. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1672. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1673. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1674. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1675. return 0;
  1676. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1677. ee->request_code, ee->error_code);
  1678. return xerrorxlib(dpy, ee); /* may call exit */
  1679. }
  1680. int
  1681. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1682. return 0;
  1683. }
  1684. /* Startup Error handler to check if another window manager
  1685. * is already running. */
  1686. int
  1687. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1688. otherwm = True;
  1689. return -1;
  1690. }
  1691. void
  1692. zoom(const char *arg) {
  1693. Client *c = sel;
  1694. if(!sel || !dozoom || sel->isfloating)
  1695. return;
  1696. if(c == nexttiled(clients))
  1697. if(!(c = nexttiled(c->next)))
  1698. return;
  1699. detach(c);
  1700. attach(c);
  1701. focus(c);
  1702. arrange();
  1703. }
  1704. int
  1705. main(int argc, char *argv[]) {
  1706. if(argc == 2 && !strcmp("-v", argv[1]))
  1707. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1708. else if(argc != 1)
  1709. eprint("usage: dwm [-v]\n");
  1710. setlocale(LC_CTYPE, "");
  1711. if(!(dpy = XOpenDisplay(0)))
  1712. eprint("dwm: cannot open display\n");
  1713. checkotherwm();
  1714. setup();
  1715. scan();
  1716. run();
  1717. cleanup();
  1718. XCloseDisplay(dpy);
  1719. return 0;
  1720. }