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.

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