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.

311 lines
6.9 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 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. Bool running = True;
  21. Bool sel_screen;
  22. char statustext[1024], tag[256];
  23. int screen, sx, sy, sw, sh, bx, by, bw, bh;
  24. Brush brush = {0};
  25. Client *clients = NULL;
  26. Client *stack = NULL;
  27. static Bool other_wm_running;
  28. static const char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  29. static int (*x_error_handler) (Display *, XErrorEvent *);
  30. static const char *status[] = {
  31. "sh", "-c", "echo -n `date '+%Y-%m-%d %H:%M'`"
  32. " `uptime | sed 's/.*://; s/,//g'`"
  33. " `acpi | awk '{print $4}' | sed 's/,//'`", 0
  34. };
  35. static void
  36. usage()
  37. {
  38. fputs("usage: gridwm [-v]\n", stderr);
  39. exit(1);
  40. }
  41. static void
  42. scan_wins()
  43. {
  44. unsigned int i, num;
  45. Window *wins;
  46. XWindowAttributes wa;
  47. Window d1, d2;
  48. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  49. for(i = 0; i < num; i++) {
  50. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  51. continue;
  52. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  53. continue;
  54. if(wa.map_state == IsViewable)
  55. manage(wins[i], &wa);
  56. }
  57. }
  58. if(wins)
  59. XFree(wins);
  60. }
  61. static int
  62. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  63. {
  64. Atom real;
  65. int format;
  66. unsigned long res, extra;
  67. int status;
  68. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  69. &res, &extra, prop);
  70. if(status != Success || *prop == 0) {
  71. return 0;
  72. }
  73. if(res == 0) {
  74. free((void *) *prop);
  75. }
  76. return res;
  77. }
  78. int
  79. win_proto(Window w)
  80. {
  81. Atom *protocols;
  82. long res;
  83. int protos = 0;
  84. int i;
  85. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
  86. ((unsigned char **) &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. send_message(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. error_handler(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, "gridwm: fatal error: request code=%d, error code=%d\n",
  134. error->request_code, error->error_code);
  135. return x_error_handler(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_error_handler(Display *dpy, XErrorEvent *error)
  143. {
  144. other_wm_running = True;
  145. return -1;
  146. }
  147. static void
  148. cleanup()
  149. {
  150. while(clients)
  151. unmanage(clients);
  152. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  153. }
  154. void
  155. run(void *aux)
  156. {
  157. spawn(dpy, aux);
  158. }
  159. void
  160. quit(void *aux)
  161. {
  162. running = False;
  163. }
  164. int
  165. main(int argc, char *argv[])
  166. {
  167. int i;
  168. XSetWindowAttributes wa;
  169. unsigned int mask;
  170. Window w;
  171. XEvent ev;
  172. fd_set fds;
  173. struct timeval t, timeout = {
  174. .tv_usec = 0,
  175. .tv_sec = STATUSDELAY,
  176. };
  177. /* command line args */
  178. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  179. switch (argv[i][1]) {
  180. case 'v':
  181. fprintf(stdout, "%s", version);
  182. exit(0);
  183. break;
  184. default:
  185. usage();
  186. break;
  187. }
  188. }
  189. dpy = XOpenDisplay(0);
  190. if(!dpy)
  191. error("gridwm: cannot connect X server\n");
  192. screen = DefaultScreen(dpy);
  193. root = RootWindow(dpy, screen);
  194. /* check if another WM is already running */
  195. other_wm_running = False;
  196. XSetErrorHandler(startup_error_handler);
  197. /* this causes an error if some other WM is running */
  198. XSelectInput(dpy, root, SubstructureRedirectMask);
  199. XFlush(dpy);
  200. if(other_wm_running)
  201. error("gridwm: another window manager is already running\n");
  202. sx = sy = 0;
  203. sw = DisplayWidth(dpy, screen);
  204. sh = DisplayHeight(dpy, screen);
  205. sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  206. XSetErrorHandler(0);
  207. x_error_handler = XSetErrorHandler(error_handler);
  208. /* init atoms */
  209. wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  210. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  211. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  212. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  213. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  214. PropModeReplace, (unsigned char *) net_atom, NetLast);
  215. /* init cursors */
  216. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  217. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  218. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  219. update_keys();
  220. /* style */
  221. loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
  222. loadfont(dpy, &brush.font, FONT);
  223. wa.override_redirect = 1;
  224. wa.background_pixmap = ParentRelative;
  225. wa.event_mask = ExposureMask;
  226. bx = by = 0;
  227. bw = sw;
  228. bh = texth(&brush.font);
  229. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  230. CopyFromParent, DefaultVisual(dpy, screen),
  231. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  232. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  233. XMapRaised(dpy, barwin);
  234. brush.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  235. brush.gc = XCreateGC(dpy, root, 0, 0);
  236. pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
  237. draw_bar();
  238. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  239. | LeaveWindowMask;
  240. wa.cursor = cursor[CurNormal];
  241. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  242. scan_wins();
  243. while(running) {
  244. if(XPending(dpy) > 0) {
  245. XNextEvent(dpy, &ev);
  246. if(handler[ev.type])
  247. (handler[ev.type]) (&ev); /* call handler */
  248. continue;
  249. }
  250. FD_ZERO(&fds);
  251. FD_SET(ConnectionNumber(dpy), &fds);
  252. t = timeout;
  253. if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
  254. continue;
  255. else if(errno != EINTR) {
  256. pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
  257. draw_bar();
  258. }
  259. }
  260. cleanup();
  261. XCloseDisplay(dpy);
  262. return 0;
  263. }