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.

308 lines
8.2 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
  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[1024], mtext[32];
  18. Bool *seltag;
  19. int bx, by, bw, bh, bmw, masterd, screen, sx, sy, sw, sh, wax, way, waw, wah;
  20. unsigned int master, nmaster, ntags, numlockmask;
  21. Atom wmatom[WMLast], netatom[NetLast];
  22. Bool running = True;
  23. Bool issel = 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. resize(stack, True, TopLeft);
  39. unmanage(stack);
  40. }
  41. if(dc.font.set)
  42. XFreeFontSet(dpy, dc.font.set);
  43. else
  44. XFreeFont(dpy, dc.font.xfont);
  45. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  46. XFreePixmap(dpy, dc.drawable);
  47. XFreeGC(dpy, dc.gc);
  48. XDestroyWindow(dpy, barwin);
  49. XFreeCursor(dpy, cursor[CurNormal]);
  50. XFreeCursor(dpy, cursor[CurResize]);
  51. XFreeCursor(dpy, cursor[CurMove]);
  52. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  53. XSync(dpy, False);
  54. free(seltag);
  55. }
  56. static void
  57. scan(void) {
  58. unsigned int i, num;
  59. Window *wins, d1, d2;
  60. XWindowAttributes wa;
  61. wins = NULL;
  62. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  63. for(i = 0; i < num; i++) {
  64. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  65. continue;
  66. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  67. continue;
  68. if(wa.map_state == IsViewable)
  69. manage(wins[i], &wa);
  70. }
  71. }
  72. if(wins)
  73. XFree(wins);
  74. }
  75. static void
  76. setup(void) {
  77. int i, j;
  78. unsigned int mask;
  79. Window w;
  80. XModifierKeymap *modmap;
  81. XSetWindowAttributes wa;
  82. /* init atoms */
  83. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  84. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  85. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  86. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  87. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  88. PropModeReplace, (unsigned char *) netatom, NetLast);
  89. /* init cursors */
  90. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  91. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  92. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  93. /* init modifier map */
  94. modmap = XGetModifierMapping(dpy);
  95. for (i = 0; i < 8; i++) {
  96. for (j = 0; j < modmap->max_keypermod; j++) {
  97. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  98. numlockmask = (1 << i);
  99. }
  100. }
  101. XFreeModifiermap(modmap);
  102. /* select for events */
  103. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  104. | EnterWindowMask | LeaveWindowMask;
  105. wa.cursor = cursor[CurNormal];
  106. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  107. grabkeys();
  108. initrregs();
  109. for(ntags = 0; tags[ntags]; ntags++);
  110. seltag = emallocz(sizeof(Bool) * ntags);
  111. seltag[0] = True;
  112. /* style */
  113. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  114. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  115. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  116. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  117. dc.status[ColBG] = getcolor(STATUSBGCOLOR);
  118. dc.status[ColFG] = getcolor(STATUSFGCOLOR);
  119. setfont(FONT);
  120. /* geometry */
  121. sx = sy = 0;
  122. sw = DisplayWidth(dpy, screen);
  123. sh = DisplayHeight(dpy, screen);
  124. master = MASTER;
  125. nmaster = NMASTER;
  126. snprintf(mtext, sizeof mtext, arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, nmaster);
  127. bmw = textw(mtext);
  128. /* bar */
  129. bx = sx;
  130. by = sy;
  131. bw = sw;
  132. dc.h = bh = dc.font.height + 2;
  133. wa.override_redirect = 1;
  134. wa.background_pixmap = ParentRelative;
  135. wa.event_mask = ButtonPressMask | ExposureMask;
  136. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  137. CopyFromParent, DefaultVisual(dpy, screen),
  138. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  139. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  140. XMapRaised(dpy, barwin);
  141. strcpy(stext, "dwm-"VERSION);
  142. /* windowarea */
  143. wax = sx;
  144. way = sy + bh;
  145. wah = sh - bh;
  146. waw = sw;
  147. /* pixmap for everything */
  148. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  149. dc.gc = XCreateGC(dpy, root, 0, 0);
  150. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  151. /* multihead support */
  152. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  153. }
  154. /*
  155. * Startup Error handler to check if another window manager
  156. * is already running.
  157. */
  158. static int
  159. xerrorstart(Display *dsply, XErrorEvent *ee) {
  160. otherwm = True;
  161. return -1;
  162. }
  163. /* extern */
  164. int
  165. getproto(Window w) {
  166. int i, format, protos, status;
  167. unsigned long extra, res;
  168. Atom *protocols, real;
  169. protos = 0;
  170. status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
  171. XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
  172. if(status != Success || protocols == 0)
  173. return protos;
  174. for(i = 0; i < res; i++)
  175. if(protocols[i] == wmatom[WMDelete])
  176. protos |= PROTODELWIN;
  177. free(protocols);
  178. return protos;
  179. }
  180. void
  181. sendevent(Window w, Atom a, long value) {
  182. XEvent e;
  183. e.type = ClientMessage;
  184. e.xclient.window = w;
  185. e.xclient.message_type = a;
  186. e.xclient.format = 32;
  187. e.xclient.data.l[0] = value;
  188. e.xclient.data.l[1] = CurrentTime;
  189. XSendEvent(dpy, w, False, NoEventMask, &e);
  190. XSync(dpy, False);
  191. }
  192. void
  193. quit(Arg *arg) {
  194. readin = running = False;
  195. }
  196. /* There's no way to check accesses to destroyed windows, thus those cases are
  197. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  198. * default error handler, which may call exit.
  199. */
  200. int
  201. xerror(Display *dpy, XErrorEvent *ee) {
  202. if(ee->error_code == BadWindow
  203. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  204. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  205. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  206. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  207. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  208. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  209. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  210. return 0;
  211. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  212. ee->request_code, ee->error_code);
  213. return xerrorxlib(dpy, ee); /* may call exit */
  214. }
  215. int
  216. main(int argc, char *argv[]) {
  217. char *p;
  218. int r, xfd;
  219. fd_set rd;
  220. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  221. fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
  222. exit(EXIT_SUCCESS);
  223. }
  224. else if(argc != 1)
  225. eprint("usage: dwm [-v]\n");
  226. setlocale(LC_CTYPE, "");
  227. dpy = XOpenDisplay(0);
  228. if(!dpy)
  229. eprint("dwm: cannot open display\n");
  230. xfd = ConnectionNumber(dpy);
  231. screen = DefaultScreen(dpy);
  232. root = RootWindow(dpy, screen);
  233. otherwm = False;
  234. XSetErrorHandler(xerrorstart);
  235. /* this causes an error if some other window manager is running */
  236. XSelectInput(dpy, root, SubstructureRedirectMask);
  237. XSync(dpy, False);
  238. if(otherwm)
  239. eprint("dwm: another window manager is already running\n");
  240. XSync(dpy, False);
  241. XSetErrorHandler(NULL);
  242. xerrorxlib = XSetErrorHandler(xerror);
  243. XSync(dpy, False);
  244. setup();
  245. drawstatus();
  246. scan();
  247. /* main event loop, also reads status text from stdin */
  248. XSync(dpy, False);
  249. procevent();
  250. readin = True;
  251. while(running) {
  252. FD_ZERO(&rd);
  253. if(readin)
  254. FD_SET(STDIN_FILENO, &rd);
  255. FD_SET(xfd, &rd);
  256. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  257. if(errno == EINTR)
  258. continue;
  259. eprint("select failed\n");
  260. }
  261. if(FD_ISSET(STDIN_FILENO, &rd)) {
  262. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  263. case -1:
  264. strncpy(stext, strerror(errno), sizeof stext - 1);
  265. stext[sizeof stext - 1] = '\0';
  266. readin = False;
  267. break;
  268. case 0:
  269. strncpy(stext, "EOF", 4);
  270. readin = False;
  271. break;
  272. default:
  273. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  274. for(p = stext + strlen(stext) - 1; p >= stext && *p != '\n'; --p);
  275. if(p > stext)
  276. strncpy(stext, p + 1, sizeof stext);
  277. }
  278. drawstatus();
  279. }
  280. if(FD_ISSET(xfd, &rd))
  281. procevent();
  282. }
  283. cleanup();
  284. XCloseDisplay(dpy);
  285. return 0;
  286. }