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.

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