wm.c 5.2 KB

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