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.

301 lines
6.9 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
  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 <sys/types.h>
  10. #include <sys/time.h>
  11. #include <X11/cursorfont.h>
  12. #include <X11/Xatom.h>
  13. #include <X11/Xproto.h>
  14. #include "wm.h"
  15. /* X structs */
  16. Display *dpy;
  17. Window root, barwin;
  18. Atom wm_atom[WMLast], net_atom[NetLast];
  19. Cursor cursor[CurLast];
  20. XRectangle rect, barrect;
  21. Bool running = True;
  22. Bool sel_screen;
  23. char statustext[1024], tag[256];
  24. int screen;
  25. Brush brush = {0};
  26. Client *clients = NULL;
  27. Client *stack = NULL;
  28. static Bool other_wm_running;
  29. static const char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  30. static int (*x_error_handler) (Display *, XErrorEvent *);
  31. static const char *status[] = {
  32. "sh", "-c", "echo -n `date '+%Y/%m/%d %H:%M'`"
  33. " `uptime | sed 's/.*://; s/,//g'`"
  34. " `acpi | awk '{print $4}' | sed 's/,//'`", 0
  35. };
  36. static void
  37. usage()
  38. {
  39. fputs("usage: gridwm [-v]\n", stderr);
  40. exit(1);
  41. }
  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. win_proto(Window w)
  81. {
  82. Atom *protocols;
  83. long res;
  84. int protos = 0;
  85. int i;
  86. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
  87. ((unsigned char **) &protocols));
  88. if(res <= 0) {
  89. return protos;
  90. }
  91. for(i = 0; i < res; i++) {
  92. if(protocols[i] == wm_atom[WMDelete])
  93. protos |= WM_PROTOCOL_DELWIN;
  94. }
  95. free((char *) protocols);
  96. return protos;
  97. }
  98. void
  99. send_message(Window w, Atom a, long value)
  100. {
  101. XEvent e;
  102. e.type = ClientMessage;
  103. e.xclient.window = w;
  104. e.xclient.message_type = a;
  105. e.xclient.format = 32;
  106. e.xclient.data.l[0] = value;
  107. e.xclient.data.l[1] = CurrentTime;
  108. XSendEvent(dpy, w, False, NoEventMask, &e);
  109. XFlush(dpy);
  110. }
  111. /*
  112. * There's no way to check accesses to destroyed windows, thus
  113. * those cases are ignored (especially on UnmapNotify's).
  114. * Other types of errors call Xlib's default error handler, which
  115. * calls exit().
  116. */
  117. int
  118. error_handler(Display *dpy, XErrorEvent *error)
  119. {
  120. if(error->error_code == BadWindow
  121. || (error->request_code == X_SetInputFocus
  122. && error->error_code == BadMatch)
  123. || (error->request_code == X_PolyText8
  124. && error->error_code == BadDrawable)
  125. || (error->request_code == X_PolyFillRectangle
  126. && error->error_code == BadDrawable)
  127. || (error->request_code == X_PolySegment
  128. && error->error_code == BadDrawable)
  129. || (error->request_code == X_ConfigureWindow
  130. && error->error_code == BadMatch)
  131. || (error->request_code == X_GrabKey
  132. && error->error_code == BadAccess))
  133. return 0;
  134. fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
  135. error->request_code, error->error_code);
  136. return x_error_handler(dpy, error); /* may call exit() */
  137. }
  138. /*
  139. * Startup Error handler to check if another window manager
  140. * is already running.
  141. */
  142. static int
  143. startup_error_handler(Display *dpy, XErrorEvent *error)
  144. {
  145. other_wm_running = True;
  146. return -1;
  147. }
  148. static void
  149. cleanup()
  150. {
  151. while(clients)
  152. unmanage(clients);
  153. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  154. }
  155. int
  156. main(int argc, char *argv[])
  157. {
  158. int i;
  159. XSetWindowAttributes wa;
  160. unsigned int mask;
  161. Window w;
  162. XEvent ev;
  163. fd_set fds;
  164. struct timeval t, timeout = {
  165. .tv_usec = 0,
  166. .tv_sec = STATUSDELAY,
  167. };
  168. /* command line args */
  169. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  170. switch (argv[i][1]) {
  171. case 'v':
  172. fprintf(stdout, "%s", version);
  173. exit(0);
  174. break;
  175. default:
  176. usage();
  177. break;
  178. }
  179. }
  180. dpy = XOpenDisplay(0);
  181. if(!dpy)
  182. error("gridwm: cannot connect X server\n");
  183. screen = DefaultScreen(dpy);
  184. root = RootWindow(dpy, screen);
  185. /* check if another WM is already running */
  186. other_wm_running = False;
  187. XSetErrorHandler(startup_error_handler);
  188. /* this causes an error if some other WM is running */
  189. XSelectInput(dpy, root, SubstructureRedirectMask);
  190. XFlush(dpy);
  191. if(other_wm_running)
  192. error("gridwm: another window manager is already running\n");
  193. rect.x = rect.y = 0;
  194. rect.width = DisplayWidth(dpy, screen);
  195. rect.height = DisplayHeight(dpy, screen);
  196. sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  197. XSetErrorHandler(0);
  198. x_error_handler = XSetErrorHandler(error_handler);
  199. /* init atoms */
  200. wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  201. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  202. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  203. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  204. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  205. PropModeReplace, (unsigned char *) net_atom, NetLast);
  206. /* init cursors */
  207. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  208. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  209. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  210. update_keys();
  211. brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
  212. DefaultDepth(dpy, screen));
  213. brush.gc = XCreateGC(dpy, root, 0, 0);
  214. /* style */
  215. loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
  216. loadfont(dpy, &brush.font, FONT);
  217. wa.override_redirect = 1;
  218. wa.background_pixmap = ParentRelative;
  219. wa.event_mask = ExposureMask;
  220. barrect = rect;
  221. barrect.height = labelheight(&brush.font);
  222. barrect.y = rect.height - barrect.height;
  223. barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
  224. barrect.width, barrect.height, 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. pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
  230. draw_bar();
  231. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  232. | LeaveWindowMask;
  233. wa.cursor = cursor[CurNormal];
  234. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  235. scan_wins();
  236. while(running) {
  237. if(XPending(dpy) > 0) {
  238. XNextEvent(dpy, &ev);
  239. if(handler[ev.type])
  240. (handler[ev.type]) (&ev); /* call handler */
  241. continue;
  242. }
  243. FD_ZERO(&fds);
  244. FD_SET(ConnectionNumber(dpy), &fds);
  245. t = timeout;
  246. if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
  247. continue;
  248. else if(errno != EINTR) {
  249. pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
  250. draw_bar();
  251. }
  252. }
  253. cleanup();
  254. XCloseDisplay(dpy);
  255. return 0;
  256. }