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.

455 lines
12 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. #include <errno.h>
  6. #include <locale.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/select.h>
  12. #include <X11/cursorfont.h>
  13. #include <X11/keysym.h>
  14. #include <X11/Xatom.h>
  15. #include <X11/Xproto.h>
  16. /* extern */
  17. char stext[256];
  18. int screen, sx, sy, sw, sh, wax, way, waw, wah;
  19. unsigned int bh, ntags, numlockmask;
  20. Atom wmatom[WMLast], netatom[NetLast];
  21. Bool running = True;
  22. Bool *seltag;
  23. Bool selscreen = True;
  24. Client *clients = NULL;
  25. Client *sel = NULL;
  26. Client *stack = NULL;
  27. Cursor cursor[CurLast];
  28. Display *dpy;
  29. DC dc = {0};
  30. Window root, barwin;
  31. /* static */
  32. static int (*xerrorxlib)(Display *, XErrorEvent *);
  33. static Bool otherwm, readin;
  34. static void
  35. cleanup(void) {
  36. close(STDIN_FILENO);
  37. while(stack) {
  38. if(stack->isbanned)
  39. XMoveWindow(dpy, stack->win, stack->x, stack->y);
  40. unmanage(stack);
  41. }
  42. if(dc.font.set)
  43. XFreeFontSet(dpy, dc.font.set);
  44. else
  45. XFreeFont(dpy, dc.font.xfont);
  46. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  47. XFreePixmap(dpy, dc.drawable);
  48. XFreeGC(dpy, dc.gc);
  49. XDestroyWindow(dpy, barwin);
  50. XFreeCursor(dpy, cursor[CurNormal]);
  51. XFreeCursor(dpy, cursor[CurResize]);
  52. XFreeCursor(dpy, cursor[CurMove]);
  53. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  54. XSync(dpy, False);
  55. free(seltag);
  56. }
  57. static unsigned int
  58. textnw(const char *text, unsigned int len) {
  59. XRectangle r;
  60. if(dc.font.set) {
  61. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  62. return r.width;
  63. }
  64. return XTextWidth(dc.font.xfont, text, len);
  65. }
  66. static void
  67. drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
  68. int x, y, w, h;
  69. static char buf[256];
  70. unsigned int len, olen;
  71. XGCValues gcv;
  72. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  73. XSetForeground(dpy, dc.gc, col[ColBG]);
  74. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  75. if(!text)
  76. return;
  77. w = 0;
  78. olen = len = strlen(text);
  79. if(len >= sizeof buf)
  80. len = sizeof buf - 1;
  81. memcpy(buf, text, len);
  82. buf[len] = 0;
  83. h = dc.font.ascent + dc.font.descent;
  84. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  85. x = dc.x + (h / 2);
  86. /* shorten text if necessary */
  87. while(len && (w = textnw(buf, len)) > dc.w - h)
  88. buf[--len] = 0;
  89. if(len < olen) {
  90. if(len > 1)
  91. buf[len - 1] = '.';
  92. if(len > 2)
  93. buf[len - 2] = '.';
  94. if(len > 3)
  95. buf[len - 3] = '.';
  96. }
  97. if(w > dc.w)
  98. return; /* too long */
  99. gcv.foreground = col[ColFG];
  100. if(dc.font.set) {
  101. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  102. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  103. }
  104. else {
  105. gcv.font = dc.font.xfont->fid;
  106. XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
  107. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  108. }
  109. x = (h + 2) / 4;
  110. r.x = dc.x + 1;
  111. r.y = dc.y + 1;
  112. if(filledsquare) {
  113. r.width = r.height = x + 1;
  114. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  115. }
  116. else if(emptysquare) {
  117. r.width = r.height = x;
  118. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  119. }
  120. }
  121. static unsigned long
  122. getcolor(const char *colstr) {
  123. Colormap cmap = DefaultColormap(dpy, screen);
  124. XColor color;
  125. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  126. eprint("error, cannot allocate color '%s'\n", colstr);
  127. return color.pixel;
  128. }
  129. static Bool
  130. isoccupied(unsigned int t) {
  131. Client *c;
  132. for(c = clients; c; c = c->next)
  133. if(c->tags[t])
  134. return True;
  135. return False;
  136. }
  137. static void
  138. scan(void) {
  139. unsigned int i, num;
  140. Window *wins, d1, d2;
  141. XWindowAttributes wa;
  142. wins = NULL;
  143. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  144. for(i = 0; i < num; i++) {
  145. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  146. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  147. continue;
  148. if(wa.map_state == IsViewable)
  149. manage(wins[i], &wa);
  150. }
  151. }
  152. if(wins)
  153. XFree(wins);
  154. }
  155. static void
  156. setfont(const char *fontstr) {
  157. char *def, **missing;
  158. int i, n;
  159. missing = NULL;
  160. if(dc.font.set)
  161. XFreeFontSet(dpy, dc.font.set);
  162. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  163. if(missing) {
  164. while(n--)
  165. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  166. XFreeStringList(missing);
  167. }
  168. if(dc.font.set) {
  169. XFontSetExtents *font_extents;
  170. XFontStruct **xfonts;
  171. char **font_names;
  172. dc.font.ascent = dc.font.descent = 0;
  173. font_extents = XExtentsOfFontSet(dc.font.set);
  174. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  175. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  176. if(dc.font.ascent < (*xfonts)->ascent)
  177. dc.font.ascent = (*xfonts)->ascent;
  178. if(dc.font.descent < (*xfonts)->descent)
  179. dc.font.descent = (*xfonts)->descent;
  180. xfonts++;
  181. }
  182. }
  183. else {
  184. if(dc.font.xfont)
  185. XFreeFont(dpy, dc.font.xfont);
  186. dc.font.xfont = NULL;
  187. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  188. eprint("error, cannot load font: '%s'\n", fontstr);
  189. dc.font.ascent = dc.font.xfont->ascent;
  190. dc.font.descent = dc.font.xfont->descent;
  191. }
  192. dc.font.height = dc.font.ascent + dc.font.descent;
  193. }
  194. static void
  195. setup(void) {
  196. int i, j;
  197. unsigned int mask;
  198. Window w;
  199. XModifierKeymap *modmap;
  200. XSetWindowAttributes wa;
  201. /* init atoms */
  202. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  203. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  204. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  205. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  206. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  207. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  208. PropModeReplace, (unsigned char *) netatom, NetLast);
  209. /* init cursors */
  210. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  211. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  212. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  213. /* init modifier map */
  214. numlockmask = 0;
  215. modmap = XGetModifierMapping(dpy);
  216. for (i = 0; i < 8; i++)
  217. for (j = 0; j < modmap->max_keypermod; j++) {
  218. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  219. == XKeysymToKeycode(dpy, XK_Num_Lock))
  220. numlockmask = (1 << i);
  221. }
  222. XFreeModifiermap(modmap);
  223. /* select for events */
  224. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  225. | EnterWindowMask | LeaveWindowMask;
  226. wa.cursor = cursor[CurNormal];
  227. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  228. grabkeys();
  229. compileregs();
  230. for(ntags = 0; tags[ntags]; ntags++);
  231. seltag = emallocz(sizeof(Bool) * ntags);
  232. seltag[0] = True;
  233. /* style */
  234. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  235. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  236. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  237. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  238. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  239. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  240. setfont(FONT);
  241. /* geometry */
  242. sx = sy = 0;
  243. sw = DisplayWidth(dpy, screen);
  244. sh = DisplayHeight(dpy, screen);
  245. initlayouts();
  246. /* bar */
  247. dc.h = bh = dc.font.height + 2;
  248. wa.override_redirect = 1;
  249. wa.background_pixmap = ParentRelative;
  250. wa.event_mask = ButtonPressMask | ExposureMask;
  251. barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
  252. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  253. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  254. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  255. XMapRaised(dpy, barwin);
  256. strcpy(stext, "dwm-"VERSION);
  257. /* windowarea */
  258. wax = sx;
  259. way = sy + (TOPBAR ? bh : 0);
  260. wah = sh - bh;
  261. waw = sw;
  262. /* pixmap for everything */
  263. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  264. dc.gc = XCreateGC(dpy, root, 0, 0);
  265. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  266. /* multihead support */
  267. selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  268. }
  269. /*
  270. * Startup Error handler to check if another window manager
  271. * is already running.
  272. */
  273. static int
  274. xerrorstart(Display *dsply, XErrorEvent *ee) {
  275. otherwm = True;
  276. return -1;
  277. }
  278. /* extern */
  279. void
  280. drawstatus(void) {
  281. int i, x;
  282. dc.x = dc.y = 0;
  283. for(i = 0; i < ntags; i++) {
  284. dc.w = textw(tags[i]);
  285. if(seltag[i])
  286. drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
  287. else
  288. drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
  289. dc.x += dc.w;
  290. }
  291. dc.w = blw;
  292. drawtext(lt->symbol, dc.norm, False, False);
  293. x = dc.x + dc.w;
  294. dc.w = textw(stext);
  295. dc.x = sw - dc.w;
  296. if(dc.x < x) {
  297. dc.x = x;
  298. dc.w = sw - x;
  299. }
  300. drawtext(stext, dc.norm, False, False);
  301. if((dc.w = dc.x - x) > bh) {
  302. dc.x = x;
  303. drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
  304. }
  305. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
  306. XSync(dpy, False);
  307. }
  308. void
  309. sendevent(Window w, Atom a, long value) {
  310. XEvent e;
  311. e.type = ClientMessage;
  312. e.xclient.window = w;
  313. e.xclient.message_type = a;
  314. e.xclient.format = 32;
  315. e.xclient.data.l[0] = value;
  316. e.xclient.data.l[1] = CurrentTime;
  317. XSendEvent(dpy, w, False, NoEventMask, &e);
  318. XSync(dpy, False);
  319. }
  320. unsigned int
  321. textw(const char *text) {
  322. return textnw(text, strlen(text)) + dc.font.height;
  323. }
  324. void
  325. quit(Arg *arg) {
  326. readin = running = False;
  327. }
  328. /* There's no way to check accesses to destroyed windows, thus those cases are
  329. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  330. * default error handler, which may call exit.
  331. */
  332. int
  333. xerror(Display *dpy, XErrorEvent *ee) {
  334. if(ee->error_code == BadWindow
  335. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  336. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  337. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  338. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  339. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  340. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  341. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  342. return 0;
  343. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  344. ee->request_code, ee->error_code);
  345. return xerrorxlib(dpy, ee); /* may call exit */
  346. }
  347. int
  348. main(int argc, char *argv[]) {
  349. char *p;
  350. int r, xfd;
  351. fd_set rd;
  352. XEvent ev;
  353. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  354. fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
  355. exit(EXIT_SUCCESS);
  356. }
  357. else if(argc != 1)
  358. eprint("usage: dwm [-v]\n");
  359. setlocale(LC_CTYPE, "");
  360. dpy = XOpenDisplay(0);
  361. if(!dpy)
  362. eprint("dwm: cannot open display\n");
  363. xfd = ConnectionNumber(dpy);
  364. screen = DefaultScreen(dpy);
  365. root = RootWindow(dpy, screen);
  366. otherwm = False;
  367. XSetErrorHandler(xerrorstart);
  368. /* this causes an error if some other window manager is running */
  369. XSelectInput(dpy, root, SubstructureRedirectMask);
  370. XSync(dpy, False);
  371. if(otherwm)
  372. eprint("dwm: another window manager is already running\n");
  373. XSync(dpy, False);
  374. XSetErrorHandler(NULL);
  375. xerrorxlib = XSetErrorHandler(xerror);
  376. XSync(dpy, False);
  377. setup();
  378. drawstatus();
  379. scan();
  380. /* main event loop, also reads status text from stdin */
  381. XSync(dpy, False);
  382. readin = True;
  383. while(running) {
  384. FD_ZERO(&rd);
  385. if(readin)
  386. FD_SET(STDIN_FILENO, &rd);
  387. FD_SET(xfd, &rd);
  388. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  389. if(errno == EINTR)
  390. continue;
  391. eprint("select failed\n");
  392. }
  393. if(FD_ISSET(STDIN_FILENO, &rd)) {
  394. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  395. case -1:
  396. strncpy(stext, strerror(errno), sizeof stext - 1);
  397. stext[sizeof stext - 1] = '\0';
  398. readin = False;
  399. break;
  400. case 0:
  401. strncpy(stext, "EOF", 4);
  402. readin = False;
  403. break;
  404. default:
  405. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  406. for(; p >= stext && *p != '\n'; --p);
  407. if(p > stext)
  408. strncpy(stext, p + 1, sizeof stext);
  409. }
  410. drawstatus();
  411. }
  412. if(FD_ISSET(xfd, &rd))
  413. while(XPending(dpy)) {
  414. XNextEvent(dpy, &ev);
  415. if(handler[ev.type])
  416. (handler[ev.type])(&ev); /* call handler */
  417. }
  418. }
  419. cleanup();
  420. XCloseDisplay(dpy);
  421. return 0;
  422. }