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.

327 lines
7.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
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include <errno.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <X11/cursorfont.h>
  12. #include <X11/Xatom.h>
  13. #include <X11/Xproto.h>
  14. #include "dwm.h"
  15. /********** CUSTOMIZE **********/
  16. char *tags[TLast] = {
  17. [Tscratch] = "scratch",
  18. [Tdev] = "dev",
  19. [Twww] = "www",
  20. [Twork] = "work",
  21. };
  22. /********** CUSTOMIZE **********/
  23. /* X structs */
  24. Display *dpy;
  25. Window root, barwin;
  26. Atom wm_atom[WMLast], net_atom[NetLast];
  27. Cursor cursor[CurLast];
  28. Bool running = True;
  29. Bool issel;
  30. int tsel = Tdev; /* default tag */
  31. int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
  32. char stext[1024];
  33. DC dc = {0};
  34. Client *clients = NULL;
  35. Client *sel = NULL;
  36. static Bool other_wm_running;
  37. static const char version[] =
  38. "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  39. static int (*x_xerror) (Display *, XErrorEvent *);
  40. static void
  41. usage() { error("usage: dwm [-v]\n"); }
  42. static void
  43. scan_wins()
  44. {
  45. unsigned int i, num;
  46. Window *wins;
  47. XWindowAttributes wa;
  48. Window d1, d2;
  49. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  50. for(i = 0; i < num; i++) {
  51. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  52. continue;
  53. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  54. continue;
  55. if(wa.map_state == IsViewable)
  56. manage(wins[i], &wa);
  57. }
  58. }
  59. if(wins)
  60. XFree(wins);
  61. }
  62. static int
  63. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  64. {
  65. Atom real;
  66. int format;
  67. unsigned long res, extra;
  68. int status;
  69. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  70. &res, &extra, prop);
  71. if(status != Success || *prop == 0) {
  72. return 0;
  73. }
  74. if(res == 0) {
  75. free((void *) *prop);
  76. }
  77. return res;
  78. }
  79. int
  80. proto(Window w)
  81. {
  82. unsigned char *protocols;
  83. long res;
  84. int protos = 0;
  85. int i;
  86. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
  87. if(res <= 0) {
  88. return protos;
  89. }
  90. for(i = 0; i < res; i++) {
  91. if(protocols[i] == wm_atom[WMDelete])
  92. protos |= WM_PROTOCOL_DELWIN;
  93. }
  94. free((char *) protocols);
  95. return protos;
  96. }
  97. void
  98. sendevent(Window w, Atom a, long value)
  99. {
  100. XEvent e;
  101. e.type = ClientMessage;
  102. e.xclient.window = w;
  103. e.xclient.message_type = a;
  104. e.xclient.format = 32;
  105. e.xclient.data.l[0] = value;
  106. e.xclient.data.l[1] = CurrentTime;
  107. XSendEvent(dpy, w, False, NoEventMask, &e);
  108. XFlush(dpy);
  109. }
  110. /*
  111. * There's no way to check accesses to destroyed windows, thus
  112. * those cases are ignored (especially on UnmapNotify's).
  113. * Other types of errors call Xlib's default error handler, which
  114. * calls exit().
  115. */
  116. int
  117. xerror(Display *dpy, XErrorEvent *error)
  118. {
  119. if(error->error_code == BadWindow
  120. || (error->request_code == X_SetInputFocus
  121. && error->error_code == BadMatch)
  122. || (error->request_code == X_PolyText8
  123. && error->error_code == BadDrawable)
  124. || (error->request_code == X_PolyFillRectangle
  125. && error->error_code == BadDrawable)
  126. || (error->request_code == X_PolySegment
  127. && error->error_code == BadDrawable)
  128. || (error->request_code == X_ConfigureWindow
  129. && error->error_code == BadMatch)
  130. || (error->request_code == X_GrabKey
  131. && error->error_code == BadAccess))
  132. return 0;
  133. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  134. error->request_code, error->error_code);
  135. return x_xerror(dpy, error); /* may call exit() */
  136. }
  137. /*
  138. * Startup Error handler to check if another window manager
  139. * is already running.
  140. */
  141. static int
  142. startup_xerror(Display *dpy, XErrorEvent *error)
  143. {
  144. other_wm_running = True;
  145. return -1;
  146. }
  147. static void
  148. cleanup()
  149. {
  150. while(sel) {
  151. resize(sel, True);
  152. unmanage(sel);
  153. }
  154. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  155. }
  156. void
  157. quit(Arg *arg)
  158. {
  159. running = False;
  160. }
  161. int
  162. main(int argc, char *argv[])
  163. {
  164. int i, n;
  165. fd_set rd;
  166. XSetWindowAttributes wa;
  167. unsigned int mask;
  168. Bool readstdin = True;
  169. Window w;
  170. XEvent ev;
  171. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  172. switch (argv[i][1]) {
  173. case 'v':
  174. fprintf(stdout, "%s", version);
  175. exit(0);
  176. break;
  177. default:
  178. usage();
  179. break;
  180. }
  181. }
  182. dpy = XOpenDisplay(0);
  183. if(!dpy)
  184. error("dwm: cannot connect X server\n");
  185. screen = DefaultScreen(dpy);
  186. root = RootWindow(dpy, screen);
  187. /* check if another WM is already running */
  188. other_wm_running = False;
  189. XSetErrorHandler(startup_xerror);
  190. /* this causes an error if some other WM is running */
  191. XSelectInput(dpy, root, SubstructureRedirectMask);
  192. XFlush(dpy);
  193. if(other_wm_running)
  194. error("dwm: another window manager is already running\n");
  195. XSetErrorHandler(0);
  196. x_xerror = XSetErrorHandler(xerror);
  197. /* init atoms */
  198. wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  199. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  200. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  201. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  202. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  203. PropModeReplace, (unsigned char *) net_atom, NetLast);
  204. /* init cursors */
  205. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  206. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  207. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  208. grabkeys();
  209. /* style */
  210. dc.bg = getcolor(BGCOLOR);
  211. dc.fg = getcolor(FGCOLOR);
  212. dc.border = getcolor(BORDERCOLOR);
  213. setfont(FONT);
  214. sx = sy = 0;
  215. sw = DisplayWidth(dpy, screen);
  216. sh = DisplayHeight(dpy, screen);
  217. mw = (sw * MASTERW) / 100;
  218. wa.override_redirect = 1;
  219. wa.background_pixmap = ParentRelative;
  220. wa.event_mask = ButtonPressMask | ExposureMask;
  221. bx = by = 0;
  222. bw = sw;
  223. dc.h = bh = dc.font.height + 4;
  224. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  225. CopyFromParent, DefaultVisual(dpy, screen),
  226. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  227. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  228. XMapRaised(dpy, barwin);
  229. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  230. dc.gc = XCreateGC(dpy, root, 0, 0);
  231. drawstatus();
  232. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  233. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  234. | LeaveWindowMask;
  235. wa.cursor = cursor[CurNormal];
  236. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  237. strcpy(stext, "dwm-"VERSION);
  238. scan_wins();
  239. /* main event loop, reads status text from stdin as well */
  240. Mainloop:
  241. while(running) {
  242. FD_ZERO(&rd);
  243. if(readstdin)
  244. FD_SET(STDIN_FILENO, &rd);
  245. FD_SET(ConnectionNumber(dpy), &rd);
  246. i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
  247. if(i == -1 && errno == EINTR)
  248. continue;
  249. if(i < 0)
  250. error("select failed\n");
  251. else if(i > 0) {
  252. if(FD_ISSET(ConnectionNumber(dpy), &rd)) {
  253. while(XPending(dpy)) {
  254. XNextEvent(dpy, &ev);
  255. if(handler[ev.type])
  256. (handler[ev.type])(&ev); /* call handler */
  257. }
  258. }
  259. if(readstdin && FD_ISSET(STDIN_FILENO, &rd)) {
  260. i = n = 0;
  261. for(;;) {
  262. if((i = getchar()) == EOF) {
  263. /* broken pipe/end of producer */
  264. readstdin = False;
  265. strcpy(stext, "broken pipe");
  266. goto Mainloop;
  267. }
  268. if(i == '\n' || n >= sizeof(stext) - 1)
  269. break;
  270. stext[n++] = i;
  271. }
  272. stext[n] = 0;
  273. drawstatus();
  274. }
  275. }
  276. }
  277. cleanup();
  278. XCloseDisplay(dpy);
  279. return 0;
  280. }