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.

466 lines
12 KiB

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