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.

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