Configuration of dwm for Mac Computers
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.

264 lines
6.0 KiB

18 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. Display *dpy;
  13. Window root;
  14. XRectangle rect;
  15. int screen, sel_screen;
  16. Atom wm_atom[WMLast];
  17. Atom net_atom[NetLast];
  18. Cursor cursor[CurLast];
  19. unsigned int kmask, numlock_mask;
  20. Pixmap pmap;
  21. enum { WM_PROTOCOL_DELWIN = 1 };
  22. static Bool other_wm_running;
  23. static int (*x_error_handler) (Display *, XErrorEvent *);
  24. static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  25. static void
  26. usage()
  27. {
  28. fputs("usage: gridwm [-v]\n", stderr);
  29. exit(1);
  30. }
  31. void
  32. error(char *errstr, ...) {
  33. va_list ap;
  34. va_start(ap, errstr);
  35. vfprintf(stderr, errstr, ap);
  36. va_end(ap);
  37. exit(1);
  38. }
  39. static void
  40. scan_wins()
  41. {
  42. unsigned int i, num;
  43. Window *wins;
  44. XWindowAttributes wa;
  45. Window d1, d2;
  46. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  47. for(i = 0; i < num; i++) {
  48. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  49. continue;
  50. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  51. continue;
  52. if(wa.map_state == IsViewable)
  53. /*manage*/;
  54. }
  55. }
  56. if(wins)
  57. XFree(wins);
  58. }
  59. static int
  60. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  61. {
  62. Atom real;
  63. int format;
  64. unsigned long res, extra;
  65. int status;
  66. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  67. &res, &extra, prop);
  68. if(status != Success || *prop == 0) {
  69. return 0;
  70. }
  71. if(res == 0) {
  72. free((void *) *prop);
  73. }
  74. return res;
  75. }
  76. int
  77. win_proto(Window w)
  78. {
  79. Atom *protocols;
  80. long res;
  81. int protos = 0;
  82. int i;
  83. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
  84. ((unsigned char **) &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. /*
  96. * There's no way to check accesses to destroyed windows, thus
  97. * those cases are ignored (especially on UnmapNotify's).
  98. * Other types of errors call Xlib's default error handler, which
  99. * calls exit().
  100. */
  101. static int
  102. error_handler(Display *dpy, XErrorEvent *error)
  103. {
  104. if(error->error_code == BadWindow
  105. || (error->request_code == X_SetInputFocus
  106. && error->error_code == BadMatch)
  107. || (error->request_code == X_PolyText8
  108. && error->error_code == BadDrawable)
  109. || (error->request_code == X_PolyFillRectangle
  110. && error->error_code == BadDrawable)
  111. || (error->request_code == X_PolySegment
  112. && error->error_code == BadDrawable)
  113. || (error->request_code == X_ConfigureWindow
  114. && error->error_code == BadMatch)
  115. || (error->request_code == X_GrabKey
  116. && error->error_code == BadAccess))
  117. return 0;
  118. fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
  119. error->request_code, error->error_code);
  120. return x_error_handler(dpy, error); /* may call exit() */
  121. }
  122. /*
  123. * Startup Error handler to check if another window manager
  124. * is already running.
  125. */
  126. static int
  127. startup_error_handler(Display *dpy, XErrorEvent *error)
  128. {
  129. other_wm_running = True;
  130. return -1;
  131. }
  132. static void
  133. init_lock_keys()
  134. {
  135. XModifierKeymap *modmap;
  136. KeyCode numlock;
  137. int i;
  138. static int masks[] = {
  139. ShiftMask, LockMask, ControlMask, Mod1Mask,
  140. Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
  141. };
  142. numlock_mask = 0;
  143. modmap = XGetModifierMapping(dpy);
  144. numlock = XKeysymToKeycode(dpy, XStringToKeysym("Num_Lock"));
  145. if(modmap && modmap->max_keypermod > 0) {
  146. int max = (sizeof(masks) / sizeof(int)) * modmap->max_keypermod;
  147. for(i = 0; i < max; i++)
  148. if(numlock && (modmap->modifiermap[i] == numlock))
  149. numlock_mask = masks[i / modmap->max_keypermod];
  150. }
  151. XFreeModifiermap(modmap);
  152. kmask = 255 & ~(numlock_mask | LockMask);
  153. }
  154. static void
  155. cleanup()
  156. {
  157. /*
  158. Client *c;
  159. for(c=client; c; c=c->next)
  160. reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
  161. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  162. */
  163. }
  164. int
  165. main(int argc, char *argv[])
  166. {
  167. int i;
  168. XSetWindowAttributes wa;
  169. unsigned int mask;
  170. Window w;
  171. /* command line args */
  172. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  173. switch (argv[i][1]) {
  174. case 'v':
  175. fprintf(stdout, "%s", version);
  176. exit(0);
  177. break;
  178. default:
  179. usage();
  180. break;
  181. }
  182. }
  183. dpy = XOpenDisplay(0);
  184. if(!dpy)
  185. error("gridwm: cannot connect X server\n");
  186. screen = DefaultScreen(dpy);
  187. root = RootWindow(dpy, screen);
  188. /* check if another WM is already running */
  189. other_wm_running = False;
  190. XSetErrorHandler(startup_error_handler);
  191. /* this causes an error if some other WM is running */
  192. XSelectInput(dpy, root, SubstructureRedirectMask);
  193. XSync(dpy, False);
  194. if(other_wm_running)
  195. error("gridwm: another window manager is already running\n");
  196. rect.x = rect.y = 0;
  197. rect.width = DisplayWidth(dpy, screen);
  198. rect.height = DisplayHeight(dpy, screen);
  199. sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  200. XSetErrorHandler(0);
  201. x_error_handler = XSetErrorHandler(error_handler);
  202. /* init atoms */
  203. wm_atom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  204. wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  205. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  206. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  207. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  208. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  209. PropModeReplace, (unsigned char *) net_atom, NetLast);
  210. /* init cursors */
  211. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  212. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  213. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  214. init_lock_keys();
  215. pmap = XCreatePixmap(dpy, root, rect.width, rect.height,
  216. DefaultDepth(dpy, screen));
  217. wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
  218. wa.cursor = cursor[CurNormal];
  219. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  220. scan_wins();
  221. cleanup();
  222. XCloseDisplay(dpy);
  223. return 0;
  224. }