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.

337 lines
9.1 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
  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, sw, sh, wax, way, waw, wah;
  19. unsigned int bh, ntags, numlockmask;
  20. Atom wmatom[WMLast], netatom[NetLast];
  21. Bool *seltag;
  22. Bool selscreen = True;
  23. Client *clients = NULL;
  24. Client *sel = NULL;
  25. Client *stack = NULL;
  26. Cursor cursor[CurLast];
  27. Display *dpy;
  28. DC dc = {0};
  29. Window root, barwin;
  30. /* static */
  31. static int (*xerrorxlib)(Display *, XErrorEvent *);
  32. static Bool otherwm, readin;
  33. static Bool running = True;
  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 long
  58. initcolor(const char *colstr) {
  59. Colormap cmap = DefaultColormap(dpy, screen);
  60. XColor color;
  61. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  62. eprint("error, cannot allocate color '%s'\n", colstr);
  63. return color.pixel;
  64. }
  65. static void
  66. initfont(const char *fontstr) {
  67. char *def, **missing;
  68. int i, n;
  69. missing = NULL;
  70. if(dc.font.set)
  71. XFreeFontSet(dpy, dc.font.set);
  72. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  73. if(missing) {
  74. while(n--)
  75. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  76. XFreeStringList(missing);
  77. }
  78. if(dc.font.set) {
  79. XFontSetExtents *font_extents;
  80. XFontStruct **xfonts;
  81. char **font_names;
  82. dc.font.ascent = dc.font.descent = 0;
  83. font_extents = XExtentsOfFontSet(dc.font.set);
  84. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  85. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  86. if(dc.font.ascent < (*xfonts)->ascent)
  87. dc.font.ascent = (*xfonts)->ascent;
  88. if(dc.font.descent < (*xfonts)->descent)
  89. dc.font.descent = (*xfonts)->descent;
  90. xfonts++;
  91. }
  92. }
  93. else {
  94. if(dc.font.xfont)
  95. XFreeFont(dpy, dc.font.xfont);
  96. dc.font.xfont = NULL;
  97. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  98. eprint("error, cannot load font: '%s'\n", fontstr);
  99. dc.font.ascent = dc.font.xfont->ascent;
  100. dc.font.descent = dc.font.xfont->descent;
  101. }
  102. dc.font.height = dc.font.ascent + dc.font.descent;
  103. }
  104. static void
  105. scan(void) {
  106. unsigned int i, num;
  107. Window *wins, d1, d2;
  108. XWindowAttributes wa;
  109. wins = NULL;
  110. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  111. for(i = 0; i < num; i++) {
  112. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  113. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  114. continue;
  115. if(wa.map_state == IsViewable)
  116. manage(wins[i], &wa);
  117. }
  118. }
  119. if(wins)
  120. XFree(wins);
  121. }
  122. static void
  123. setup(void) {
  124. int i, j;
  125. unsigned int mask;
  126. Window w;
  127. XModifierKeymap *modmap;
  128. XSetWindowAttributes wa;
  129. /* init atoms */
  130. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  131. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  132. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  133. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  134. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  135. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  136. PropModeReplace, (unsigned char *) netatom, NetLast);
  137. /* init cursors */
  138. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  139. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  140. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  141. /* init modifier map */
  142. numlockmask = 0;
  143. modmap = XGetModifierMapping(dpy);
  144. for (i = 0; i < 8; i++)
  145. for (j = 0; j < modmap->max_keypermod; j++) {
  146. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  147. == XKeysymToKeycode(dpy, XK_Num_Lock))
  148. numlockmask = (1 << i);
  149. }
  150. XFreeModifiermap(modmap);
  151. /* select for events */
  152. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  153. | EnterWindowMask | LeaveWindowMask;
  154. wa.cursor = cursor[CurNormal];
  155. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  156. grabkeys();
  157. compileregs();
  158. for(ntags = 0; tags[ntags]; ntags++);
  159. seltag = emallocz(sizeof(Bool) * ntags);
  160. seltag[0] = True;
  161. /* style */
  162. dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
  163. dc.norm[ColBG] = initcolor(NORMBGCOLOR);
  164. dc.norm[ColFG] = initcolor(NORMFGCOLOR);
  165. dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
  166. dc.sel[ColBG] = initcolor(SELBGCOLOR);
  167. dc.sel[ColFG] = initcolor(SELFGCOLOR);
  168. initfont(FONT);
  169. /* geometry */
  170. sw = DisplayWidth(dpy, screen);
  171. sh = DisplayHeight(dpy, screen);
  172. initlayouts();
  173. /* bar */
  174. dc.h = bh = dc.font.height + 2;
  175. wa.override_redirect = 1;
  176. wa.background_pixmap = ParentRelative;
  177. wa.event_mask = ButtonPressMask | ExposureMask;
  178. barwin = XCreateWindow(dpy, root, 0, (TOPBAR ? 0 : sh - bh), sw, bh, 0,
  179. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  180. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  181. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  182. XMapRaised(dpy, barwin);
  183. strcpy(stext, "dwm-"VERSION);
  184. /* windowarea */
  185. wax = 0;
  186. way = (TOPBAR ? bh : 0);
  187. wah = sh - bh;
  188. waw = sw;
  189. /* pixmap for everything */
  190. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  191. dc.gc = XCreateGC(dpy, root, 0, 0);
  192. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  193. /* multihead support */
  194. selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  195. }
  196. /*
  197. * Startup Error handler to check if another window manager
  198. * is already running.
  199. */
  200. static int
  201. xerrorstart(Display *dsply, XErrorEvent *ee) {
  202. otherwm = True;
  203. return -1;
  204. }
  205. /* extern */
  206. void
  207. sendevent(Window w, Atom a, long value) {
  208. XEvent e;
  209. e.type = ClientMessage;
  210. e.xclient.window = w;
  211. e.xclient.message_type = a;
  212. e.xclient.format = 32;
  213. e.xclient.data.l[0] = value;
  214. e.xclient.data.l[1] = CurrentTime;
  215. XSendEvent(dpy, w, False, NoEventMask, &e);
  216. XSync(dpy, False);
  217. }
  218. void
  219. quit(Arg *arg) {
  220. readin = running = False;
  221. }
  222. /* There's no way to check accesses to destroyed windows, thus those cases are
  223. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  224. * default error handler, which may call exit.
  225. */
  226. int
  227. xerror(Display *dpy, XErrorEvent *ee) {
  228. if(ee->error_code == BadWindow
  229. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  230. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  231. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  232. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  233. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  234. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  235. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  236. return 0;
  237. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  238. ee->request_code, ee->error_code);
  239. return xerrorxlib(dpy, ee); /* may call exit */
  240. }
  241. int
  242. main(int argc, char *argv[]) {
  243. char *p;
  244. int r, xfd;
  245. fd_set rd;
  246. XEvent ev;
  247. if(argc == 2 && !strncmp("-v", argv[1], 3))
  248. eprint("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
  249. else if(argc != 1)
  250. eprint("usage: dwm [-v]\n");
  251. setlocale(LC_CTYPE, "");
  252. if(!(dpy = XOpenDisplay(0)))
  253. eprint("dwm: cannot open display\n");
  254. xfd = ConnectionNumber(dpy);
  255. screen = DefaultScreen(dpy);
  256. root = RootWindow(dpy, screen);
  257. otherwm = False;
  258. XSetErrorHandler(xerrorstart);
  259. /* this causes an error if some other window manager is running */
  260. XSelectInput(dpy, root, SubstructureRedirectMask);
  261. XSync(dpy, False);
  262. if(otherwm)
  263. eprint("dwm: another window manager is already running\n");
  264. XSync(dpy, False);
  265. XSetErrorHandler(NULL);
  266. xerrorxlib = XSetErrorHandler(xerror);
  267. XSync(dpy, False);
  268. setup();
  269. drawstatus();
  270. scan();
  271. /* main event loop, also reads status text from stdin */
  272. XSync(dpy, False);
  273. readin = True;
  274. while(running) {
  275. FD_ZERO(&rd);
  276. if(readin)
  277. FD_SET(STDIN_FILENO, &rd);
  278. FD_SET(xfd, &rd);
  279. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  280. if(errno == EINTR)
  281. continue;
  282. eprint("select failed\n");
  283. }
  284. if(FD_ISSET(STDIN_FILENO, &rd)) {
  285. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  286. case -1:
  287. strncpy(stext, strerror(errno), sizeof stext - 1);
  288. stext[sizeof stext - 1] = '\0';
  289. readin = False;
  290. break;
  291. case 0:
  292. strncpy(stext, "EOF", 4);
  293. readin = False;
  294. break;
  295. default:
  296. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  297. for(; p >= stext && *p != '\n'; --p);
  298. if(p > stext)
  299. strncpy(stext, p + 1, sizeof stext);
  300. }
  301. drawstatus();
  302. }
  303. if(FD_ISSET(xfd, &rd))
  304. while(XPending(dpy)) {
  305. XNextEvent(dpy, &ev);
  306. if(handler[ev.type])
  307. (handler[ev.type])(&ev); /* call handler */
  308. }
  309. }
  310. cleanup();
  311. XCloseDisplay(dpy);
  312. return 0;
  313. }