wm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 net_atom[NetLast];
  16. Cursor cursor[CurLast];
  17. XRectangle rect, barrect;
  18. Bool running = True;
  19. char *bartext, tag[256];
  20. int screen, sel_screen;
  21. Brush brush = {0};
  22. Client *clients = NULL;
  23. enum { WM_PROTOCOL_DELWIN = 1 };
  24. static Bool other_wm_running;
  25. static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  26. static int (*x_error_handler) (Display *, XErrorEvent *);
  27. static void
  28. usage()
  29. {
  30. fputs("usage: gridwm [-v]\n", stderr);
  31. exit(1);
  32. }
  33. static void
  34. scan_wins()
  35. {
  36. unsigned int i, num;
  37. Window *wins;
  38. XWindowAttributes wa;
  39. Window d1, d2;
  40. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  41. for(i = 0; i < num; i++) {
  42. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  43. continue;
  44. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  45. continue;
  46. if(wa.map_state == IsViewable)
  47. manage(wins[i], &wa);
  48. }
  49. }
  50. if(wins)
  51. XFree(wins);
  52. }
  53. /*
  54. * There's no way to check accesses to destroyed windows, thus
  55. * those cases are ignored (especially on UnmapNotify's).
  56. * Other types of errors call Xlib's default error handler, which
  57. * calls exit().
  58. */
  59. int
  60. error_handler(Display *dpy, XErrorEvent *error)
  61. {
  62. if(error->error_code == BadWindow
  63. || (error->request_code == X_SetInputFocus
  64. && error->error_code == BadMatch)
  65. || (error->request_code == X_PolyText8
  66. && error->error_code == BadDrawable)
  67. || (error->request_code == X_PolyFillRectangle
  68. && error->error_code == BadDrawable)
  69. || (error->request_code == X_PolySegment
  70. && error->error_code == BadDrawable)
  71. || (error->request_code == X_ConfigureWindow
  72. && error->error_code == BadMatch)
  73. || (error->request_code == X_GrabKey
  74. && error->error_code == BadAccess))
  75. return 0;
  76. fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
  77. error->request_code, error->error_code);
  78. return x_error_handler(dpy, error); /* may call exit() */
  79. }
  80. /*
  81. * Startup Error handler to check if another window manager
  82. * is already running.
  83. */
  84. static int
  85. startup_error_handler(Display *dpy, XErrorEvent *error)
  86. {
  87. other_wm_running = True;
  88. return -1;
  89. }
  90. static void
  91. cleanup()
  92. {
  93. /*
  94. Client *c;
  95. for(c=client; c; c=c->next)
  96. reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
  97. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  98. */
  99. }
  100. int
  101. main(int argc, char *argv[])
  102. {
  103. int i;
  104. XSetWindowAttributes wa;
  105. unsigned int mask;
  106. Window w;
  107. XEvent ev;
  108. /* command line args */
  109. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  110. switch (argv[i][1]) {
  111. case 'v':
  112. fprintf(stdout, "%s", version);
  113. exit(0);
  114. break;
  115. default:
  116. usage();
  117. break;
  118. }
  119. }
  120. dpy = XOpenDisplay(0);
  121. if(!dpy)
  122. error("gridwm: cannot connect X server\n");
  123. screen = DefaultScreen(dpy);
  124. root = RootWindow(dpy, screen);
  125. /* check if another WM is already running */
  126. other_wm_running = False;
  127. XSetErrorHandler(startup_error_handler);
  128. /* this causes an error if some other WM is running */
  129. XSelectInput(dpy, root, SubstructureRedirectMask);
  130. XFlush(dpy);
  131. if(other_wm_running)
  132. error("gridwm: another window manager is already running\n");
  133. rect.x = rect.y = 0;
  134. rect.width = DisplayWidth(dpy, screen);
  135. rect.height = DisplayHeight(dpy, screen);
  136. sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  137. XSetErrorHandler(0);
  138. x_error_handler = XSetErrorHandler(error_handler);
  139. /* init atoms */
  140. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  141. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  142. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  143. PropModeReplace, (unsigned char *) net_atom, NetLast);
  144. /* init cursors */
  145. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  146. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  147. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  148. update_keys();
  149. brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
  150. DefaultDepth(dpy, screen));
  151. brush.gc = XCreateGC(dpy, root, 0, 0);
  152. /* style */
  153. loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
  154. loadfont(dpy, &brush.font, FONT);
  155. wa.override_redirect = 1;
  156. wa.background_pixmap = ParentRelative;
  157. wa.event_mask = ExposureMask;
  158. barrect = rect;
  159. barrect.height = labelheight(&brush.font);
  160. barrect.y = rect.height - barrect.height;
  161. barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
  162. barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
  163. CopyFromParent, DefaultVisual(dpy, screen),
  164. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  165. bartext = NULL;
  166. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  167. XMapRaised(dpy, barwin);
  168. draw_bar();
  169. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  170. | LeaveWindowMask;
  171. wa.cursor = cursor[CurNormal];
  172. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  173. scan_wins();
  174. while(running) {
  175. XNextEvent(dpy, &ev);
  176. if(handler[ev.type])
  177. (handler[ev.type]) (&ev); /* call handler */
  178. }
  179. cleanup();
  180. XCloseDisplay(dpy);
  181. return 0;
  182. }