wm.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. unsigned char *protocols;
  82. long res;
  83. int protos = 0;
  84. int i;
  85. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
  86. if(res <= 0) {
  87. return protos;
  88. }
  89. for(i = 0; i < res; i++) {
  90. if(protocols[i] == wm_atom[WMDelete])
  91. protos |= WM_PROTOCOL_DELWIN;
  92. }
  93. free((char *) protocols);
  94. return protos;
  95. }
  96. void
  97. send_message(Window w, Atom a, long value)
  98. {
  99. XEvent e;
  100. e.type = ClientMessage;
  101. e.xclient.window = w;
  102. e.xclient.message_type = a;
  103. e.xclient.format = 32;
  104. e.xclient.data.l[0] = value;
  105. e.xclient.data.l[1] = CurrentTime;
  106. XSendEvent(dpy, w, False, NoEventMask, &e);
  107. XFlush(dpy);
  108. }
  109. /*
  110. * There's no way to check accesses to destroyed windows, thus
  111. * those cases are ignored (especially on UnmapNotify's).
  112. * Other types of errors call Xlib's default error handler, which
  113. * calls exit().
  114. */
  115. int
  116. error_handler(Display *dpy, XErrorEvent *error)
  117. {
  118. if(error->error_code == BadWindow
  119. || (error->request_code == X_SetInputFocus
  120. && error->error_code == BadMatch)
  121. || (error->request_code == X_PolyText8
  122. && error->error_code == BadDrawable)
  123. || (error->request_code == X_PolyFillRectangle
  124. && error->error_code == BadDrawable)
  125. || (error->request_code == X_PolySegment
  126. && error->error_code == BadDrawable)
  127. || (error->request_code == X_ConfigureWindow
  128. && error->error_code == BadMatch)
  129. || (error->request_code == X_GrabKey
  130. && error->error_code == BadAccess))
  131. return 0;
  132. fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
  133. error->request_code, error->error_code);
  134. return x_error_handler(dpy, error); /* may call exit() */
  135. }
  136. /*
  137. * Startup Error handler to check if another window manager
  138. * is already running.
  139. */
  140. static int
  141. startup_error_handler(Display *dpy, XErrorEvent *error)
  142. {
  143. other_wm_running = True;
  144. return -1;
  145. }
  146. static void
  147. cleanup()
  148. {
  149. while(clients)
  150. unmanage(clients);
  151. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  152. }
  153. void
  154. run(void *aux)
  155. {
  156. spawn(dpy, aux);
  157. }
  158. void
  159. quit(void *aux)
  160. {
  161. running = False;
  162. }
  163. int
  164. main(int argc, char *argv[])
  165. {
  166. int i;
  167. XSetWindowAttributes wa;
  168. unsigned int mask;
  169. Window w;
  170. XEvent ev;
  171. fd_set fds;
  172. struct timeval t, timeout = {
  173. .tv_usec = 0,
  174. .tv_sec = STATUSDELAY,
  175. };
  176. /* command line args */
  177. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  178. switch (argv[i][1]) {
  179. case 'v':
  180. fprintf(stdout, "%s", version);
  181. exit(0);
  182. break;
  183. default:
  184. usage();
  185. break;
  186. }
  187. }
  188. dpy = XOpenDisplay(0);
  189. if(!dpy)
  190. error("gridwm: cannot connect X server\n");
  191. screen = DefaultScreen(dpy);
  192. root = RootWindow(dpy, screen);
  193. /* check if another WM is already running */
  194. other_wm_running = False;
  195. XSetErrorHandler(startup_error_handler);
  196. /* this causes an error if some other WM is running */
  197. XSelectInput(dpy, root, SubstructureRedirectMask);
  198. XFlush(dpy);
  199. if(other_wm_running)
  200. error("gridwm: another window manager is already running\n");
  201. sx = sy = 0;
  202. sw = DisplayWidth(dpy, screen);
  203. sh = DisplayHeight(dpy, screen);
  204. sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  205. XSetErrorHandler(0);
  206. x_error_handler = XSetErrorHandler(error_handler);
  207. /* init atoms */
  208. wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  209. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  210. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  211. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  212. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  213. PropModeReplace, (unsigned char *) net_atom, NetLast);
  214. /* init cursors */
  215. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  216. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  217. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  218. update_keys();
  219. /* style */
  220. loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
  221. loadfont(dpy, &brush.font, FONT);
  222. wa.override_redirect = 1;
  223. wa.background_pixmap = ParentRelative;
  224. wa.event_mask = ExposureMask;
  225. bx = by = 0;
  226. bw = sw;
  227. bh = texth(&brush.font);
  228. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  229. CopyFromParent, DefaultVisual(dpy, screen),
  230. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  231. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  232. XMapRaised(dpy, barwin);
  233. brush.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  234. brush.gc = XCreateGC(dpy, root, 0, 0);
  235. pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
  236. draw_bar();
  237. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  238. | LeaveWindowMask;
  239. wa.cursor = cursor[CurNormal];
  240. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  241. scan_wins();
  242. while(running) {
  243. if(XPending(dpy) > 0) {
  244. XNextEvent(dpy, &ev);
  245. if(handler[ev.type])
  246. (handler[ev.type]) (&ev); /* call handler */
  247. continue;
  248. }
  249. FD_ZERO(&fds);
  250. FD_SET(ConnectionNumber(dpy), &fds);
  251. t = timeout;
  252. if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
  253. continue;
  254. else if(errno != EINTR) {
  255. pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
  256. draw_bar();
  257. }
  258. }
  259. cleanup();
  260. XCloseDisplay(dpy);
  261. return 0;
  262. }