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.

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