wm.c 6.3 KB

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