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.

288 lines
6.7 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
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. /* X structs */
  13. Display *dpy;
  14. Window root, barwin;
  15. Atom wm_atom[WMLast], net_atom[NetLast];
  16. Cursor cursor[CurLast];
  17. XRectangle rect, barrect;
  18. Bool running = True;
  19. char *bartext;
  20. int screen, sel_screen;
  21. unsigned int lock_mask, numlock_mask;
  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. static int
  55. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  56. {
  57. Atom real;
  58. int format;
  59. unsigned long res, extra;
  60. int status;
  61. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  62. &res, &extra, prop);
  63. if(status != Success || *prop == 0) {
  64. return 0;
  65. }
  66. if(res == 0) {
  67. free((void *) *prop);
  68. }
  69. return res;
  70. }
  71. int
  72. win_proto(Window w)
  73. {
  74. Atom *protocols;
  75. long res;
  76. int protos = 0;
  77. int i;
  78. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
  79. ((unsigned char **) &protocols));
  80. if(res <= 0) {
  81. return protos;
  82. }
  83. for(i = 0; i < res; i++) {
  84. if(protocols[i] == wm_atom[WMDelete])
  85. protos |= WM_PROTOCOL_DELWIN;
  86. }
  87. free((char *) protocols);
  88. return protos;
  89. }
  90. /*
  91. * There's no way to check accesses to destroyed windows, thus
  92. * those cases are ignored (especially on UnmapNotify's).
  93. * Other types of errors call Xlib's default error handler, which
  94. * calls exit().
  95. */
  96. static int
  97. error_handler(Display *dpy, XErrorEvent *error)
  98. {
  99. if(error->error_code == BadWindow
  100. || (error->request_code == X_SetInputFocus
  101. && error->error_code == BadMatch)
  102. || (error->request_code == X_PolyText8
  103. && error->error_code == BadDrawable)
  104. || (error->request_code == X_PolyFillRectangle
  105. && error->error_code == BadDrawable)
  106. || (error->request_code == X_PolySegment
  107. && error->error_code == BadDrawable)
  108. || (error->request_code == X_ConfigureWindow
  109. && error->error_code == BadMatch)
  110. || (error->request_code == X_GrabKey
  111. && error->error_code == BadAccess))
  112. return 0;
  113. fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
  114. error->request_code, error->error_code);
  115. return x_error_handler(dpy, error); /* may call exit() */
  116. }
  117. /*
  118. * Startup Error handler to check if another window manager
  119. * is already running.
  120. */
  121. static int
  122. startup_error_handler(Display *dpy, XErrorEvent *error)
  123. {
  124. other_wm_running = True;
  125. return -1;
  126. }
  127. static void
  128. init_lock_keys()
  129. {
  130. XModifierKeymap *modmap;
  131. KeyCode numlock;
  132. int i;
  133. static int masks[] = {
  134. ShiftMask, LockMask, ControlMask, Mod1Mask,
  135. Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
  136. };
  137. numlock_mask = 0;
  138. modmap = XGetModifierMapping(dpy);
  139. numlock = XKeysymToKeycode(dpy, XStringToKeysym("Num_Lock"));
  140. if(modmap && modmap->max_keypermod > 0) {
  141. int max = (sizeof(masks) / sizeof(int)) * modmap->max_keypermod;
  142. for(i = 0; i < max; i++)
  143. if(numlock && (modmap->modifiermap[i] == numlock))
  144. numlock_mask = masks[i / modmap->max_keypermod];
  145. }
  146. XFreeModifiermap(modmap);
  147. lock_mask = 255 & ~(numlock_mask | LockMask);
  148. }
  149. static void
  150. cleanup()
  151. {
  152. /*
  153. Client *c;
  154. for(c=client; c; c=c->next)
  155. reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
  156. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  157. */
  158. }
  159. int
  160. main(int argc, char *argv[])
  161. {
  162. int i;
  163. XSetWindowAttributes wa;
  164. unsigned int mask;
  165. Window w;
  166. XEvent ev;
  167. /* command line args */
  168. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  169. switch (argv[i][1]) {
  170. case 'v':
  171. fprintf(stdout, "%s", version);
  172. exit(0);
  173. break;
  174. default:
  175. usage();
  176. break;
  177. }
  178. }
  179. dpy = XOpenDisplay(0);
  180. if(!dpy)
  181. error("gridwm: cannot connect X server\n");
  182. screen = DefaultScreen(dpy);
  183. root = RootWindow(dpy, screen);
  184. /* check if another WM is already running */
  185. other_wm_running = False;
  186. XSetErrorHandler(startup_error_handler);
  187. /* this causes an error if some other WM is running */
  188. XSelectInput(dpy, root, SubstructureRedirectMask);
  189. XFlush(dpy);
  190. if(other_wm_running)
  191. error("gridwm: another window manager is already running\n");
  192. rect.x = rect.y = 0;
  193. rect.width = DisplayWidth(dpy, screen);
  194. rect.height = DisplayHeight(dpy, screen);
  195. sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  196. XSetErrorHandler(0);
  197. x_error_handler = XSetErrorHandler(error_handler);
  198. /* init atoms */
  199. wm_atom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  200. wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  201. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  202. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  203. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  204. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  205. PropModeReplace, (unsigned char *) net_atom, NetLast);
  206. /* init cursors */
  207. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  208. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  209. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  210. init_lock_keys();
  211. brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
  212. DefaultDepth(dpy, screen));
  213. brush.gc = XCreateGC(dpy, root, 0, 0);
  214. /* style */
  215. loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
  216. loadfont(dpy, &brush.font, FONT);
  217. wa.override_redirect = 1;
  218. wa.background_pixmap = ParentRelative;
  219. wa.event_mask = ExposureMask;
  220. barrect = rect;
  221. barrect.height = labelheight(&brush.font);
  222. barrect.y = rect.height - barrect.height;
  223. barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
  224. barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
  225. CopyFromParent, DefaultVisual(dpy, screen),
  226. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  227. bartext = 0;
  228. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  229. XMapRaised(dpy, barwin);
  230. draw_bar();
  231. wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
  232. wa.cursor = cursor[CurNormal];
  233. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  234. scan_wins();
  235. while(running) {
  236. XNextEvent(dpy, &ev);
  237. if(handler[ev.type])
  238. (handler[ev.type]) (&ev); /* call handler */
  239. }
  240. cleanup();
  241. XCloseDisplay(dpy);
  242. return 0;
  243. }