wm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 *bartext, 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 char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  30. static int (*x_error_handler) (Display *, XErrorEvent *);
  31. static void
  32. usage()
  33. {
  34. fputs("usage: gridwm [-v]\n", stderr);
  35. exit(1);
  36. }
  37. static void
  38. scan_wins()
  39. {
  40. unsigned int i, num;
  41. Window *wins;
  42. XWindowAttributes wa;
  43. Window d1, d2;
  44. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  45. for(i = 0; i < num; i++) {
  46. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  47. continue;
  48. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  49. continue;
  50. if(wa.map_state == IsViewable)
  51. manage(wins[i], &wa);
  52. }
  53. }
  54. if(wins)
  55. XFree(wins);
  56. }
  57. static int
  58. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  59. {
  60. Atom real;
  61. int format;
  62. unsigned long res, extra;
  63. int status;
  64. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  65. &res, &extra, prop);
  66. if(status != Success || *prop == 0) {
  67. return 0;
  68. }
  69. if(res == 0) {
  70. free((void *) *prop);
  71. }
  72. return res;
  73. }
  74. int
  75. win_proto(Window w)
  76. {
  77. Atom *protocols;
  78. long res;
  79. int protos = 0;
  80. int i;
  81. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
  82. ((unsigned char **) &protocols));
  83. if(res <= 0) {
  84. return protos;
  85. }
  86. for(i = 0; i < res; i++) {
  87. if(protocols[i] == wm_atom[WMDelete])
  88. protos |= WM_PROTOCOL_DELWIN;
  89. }
  90. free((char *) protocols);
  91. return protos;
  92. }
  93. void
  94. send_message(Window w, Atom a, long value)
  95. {
  96. XEvent e;
  97. e.type = ClientMessage;
  98. e.xclient.window = w;
  99. e.xclient.message_type = a;
  100. e.xclient.format = 32;
  101. e.xclient.data.l[0] = value;
  102. e.xclient.data.l[1] = CurrentTime;
  103. XSendEvent(dpy, w, False, NoEventMask, &e);
  104. XFlush(dpy);
  105. }
  106. /*
  107. * There's no way to check accesses to destroyed windows, thus
  108. * those cases are ignored (especially on UnmapNotify's).
  109. * Other types of errors call Xlib's default error handler, which
  110. * calls exit().
  111. */
  112. int
  113. error_handler(Display *dpy, XErrorEvent *error)
  114. {
  115. if(error->error_code == BadWindow
  116. || (error->request_code == X_SetInputFocus
  117. && error->error_code == BadMatch)
  118. || (error->request_code == X_PolyText8
  119. && error->error_code == BadDrawable)
  120. || (error->request_code == X_PolyFillRectangle
  121. && error->error_code == BadDrawable)
  122. || (error->request_code == X_PolySegment
  123. && error->error_code == BadDrawable)
  124. || (error->request_code == X_ConfigureWindow
  125. && error->error_code == BadMatch)
  126. || (error->request_code == X_GrabKey
  127. && error->error_code == BadAccess))
  128. return 0;
  129. fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
  130. error->request_code, error->error_code);
  131. return x_error_handler(dpy, error); /* may call exit() */
  132. }
  133. /*
  134. * Startup Error handler to check if another window manager
  135. * is already running.
  136. */
  137. static int
  138. startup_error_handler(Display *dpy, XErrorEvent *error)
  139. {
  140. other_wm_running = True;
  141. return -1;
  142. }
  143. static void
  144. cleanup()
  145. {
  146. while(clients)
  147. unmanage(clients);
  148. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  149. }
  150. int
  151. main(int argc, char *argv[])
  152. {
  153. int i;
  154. XSetWindowAttributes wa;
  155. unsigned int mask;
  156. Window w;
  157. XEvent ev;
  158. fd_set fds;
  159. struct timeval t, timeout = {
  160. .tv_usec = 0,
  161. .tv_sec = STATUSDELAY,
  162. };
  163. /* command line args */
  164. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  165. switch (argv[i][1]) {
  166. case 'v':
  167. fprintf(stdout, "%s", version);
  168. exit(0);
  169. break;
  170. default:
  171. usage();
  172. break;
  173. }
  174. }
  175. dpy = XOpenDisplay(0);
  176. if(!dpy)
  177. error("gridwm: cannot connect X server\n");
  178. screen = DefaultScreen(dpy);
  179. root = RootWindow(dpy, screen);
  180. /* check if another WM is already running */
  181. other_wm_running = False;
  182. XSetErrorHandler(startup_error_handler);
  183. /* this causes an error if some other WM is running */
  184. XSelectInput(dpy, root, SubstructureRedirectMask);
  185. XFlush(dpy);
  186. if(other_wm_running)
  187. error("gridwm: another window manager is already running\n");
  188. rect.x = rect.y = 0;
  189. rect.width = DisplayWidth(dpy, screen);
  190. rect.height = DisplayHeight(dpy, screen);
  191. sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  192. XSetErrorHandler(0);
  193. x_error_handler = XSetErrorHandler(error_handler);
  194. /* init atoms */
  195. wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  196. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  197. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  198. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  199. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  200. PropModeReplace, (unsigned char *) net_atom, NetLast);
  201. /* init cursors */
  202. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  203. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  204. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  205. update_keys();
  206. brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
  207. DefaultDepth(dpy, screen));
  208. brush.gc = XCreateGC(dpy, root, 0, 0);
  209. /* style */
  210. loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
  211. loadfont(dpy, &brush.font, FONT);
  212. wa.override_redirect = 1;
  213. wa.background_pixmap = ParentRelative;
  214. wa.event_mask = ExposureMask;
  215. barrect = rect;
  216. barrect.height = labelheight(&brush.font);
  217. barrect.y = rect.height - barrect.height;
  218. barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
  219. barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
  220. CopyFromParent, DefaultVisual(dpy, screen),
  221. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  222. bartext = NULL;
  223. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  224. XMapRaised(dpy, barwin);
  225. draw_bar();
  226. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  227. | LeaveWindowMask;
  228. wa.cursor = cursor[CurNormal];
  229. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  230. scan_wins();
  231. while(running) {
  232. if(XPending(dpy) > 0) {
  233. XNextEvent(dpy, &ev);
  234. if(handler[ev.type])
  235. (handler[ev.type]) (&ev); /* call handler */
  236. continue;
  237. }
  238. FD_ZERO(&fds);
  239. FD_SET(ConnectionNumber(dpy), &fds);
  240. t = timeout;
  241. if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
  242. continue;
  243. else if(errno != EINTR)
  244. draw_bar();
  245. }
  246. cleanup();
  247. XCloseDisplay(dpy);
  248. return 0;
  249. }