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.

276 lines
5.9 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
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 "dwm.h"
  12. /********** CUSTOMIZE **********/
  13. char *tags[TLast] = {
  14. [Tscratch] = "scratch",
  15. [Tdev] = "dev",
  16. [Tirc] = "irc",
  17. [Twww] = "www",
  18. [Twork] = "work",
  19. };
  20. /********** CUSTOMIZE **********/
  21. /* X structs */
  22. Display *dpy;
  23. Window root, barwin;
  24. Atom wm_atom[WMLast], net_atom[NetLast];
  25. Cursor cursor[CurLast];
  26. Bool running = True;
  27. Bool issel;
  28. char stext[1024];
  29. int tsel = Tdev; /* default tag */
  30. int screen, sx, sy, sw, sh, th;
  31. DC dc = {0};
  32. Client *clients = NULL;
  33. Client *sel = NULL;
  34. static Bool other_wm_running;
  35. static const char version[] =
  36. "dwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  37. static int (*x_error_handler) (Display *, XErrorEvent *);
  38. static void
  39. usage() { error("usage: dwm [-v]\n"); }
  40. static void
  41. scan_wins()
  42. {
  43. unsigned int i, num;
  44. Window *wins;
  45. XWindowAttributes wa;
  46. Window d1, d2;
  47. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  48. for(i = 0; i < num; i++) {
  49. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  50. continue;
  51. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  52. continue;
  53. if(wa.map_state == IsViewable)
  54. manage(wins[i], &wa);
  55. }
  56. }
  57. if(wins)
  58. XFree(wins);
  59. }
  60. static int
  61. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  62. {
  63. Atom real;
  64. int format;
  65. unsigned long res, extra;
  66. int status;
  67. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  68. &res, &extra, prop);
  69. if(status != Success || *prop == 0) {
  70. return 0;
  71. }
  72. if(res == 0) {
  73. free((void *) *prop);
  74. }
  75. return res;
  76. }
  77. int
  78. win_proto(Window w)
  79. {
  80. unsigned char *protocols;
  81. long res;
  82. int protos = 0;
  83. int i;
  84. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &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. void
  96. send_message(Window w, Atom a, long value)
  97. {
  98. XEvent e;
  99. e.type = ClientMessage;
  100. e.xclient.window = w;
  101. e.xclient.message_type = a;
  102. e.xclient.format = 32;
  103. e.xclient.data.l[0] = value;
  104. e.xclient.data.l[1] = CurrentTime;
  105. XSendEvent(dpy, w, False, NoEventMask, &e);
  106. XFlush(dpy);
  107. }
  108. /*
  109. * There's no way to check accesses to destroyed windows, thus
  110. * those cases are ignored (especially on UnmapNotify's).
  111. * Other types of errors call Xlib's default error handler, which
  112. * calls exit().
  113. */
  114. int
  115. error_handler(Display *dpy, XErrorEvent *error)
  116. {
  117. if(error->error_code == BadWindow
  118. || (error->request_code == X_SetInputFocus
  119. && error->error_code == BadMatch)
  120. || (error->request_code == X_PolyText8
  121. && error->error_code == BadDrawable)
  122. || (error->request_code == X_PolyFillRectangle
  123. && error->error_code == BadDrawable)
  124. || (error->request_code == X_PolySegment
  125. && error->error_code == BadDrawable)
  126. || (error->request_code == X_ConfigureWindow
  127. && error->error_code == BadMatch)
  128. || (error->request_code == X_GrabKey
  129. && error->error_code == BadAccess))
  130. return 0;
  131. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  132. error->request_code, error->error_code);
  133. return x_error_handler(dpy, error); /* may call exit() */
  134. }
  135. /*
  136. * Startup Error handler to check if another window manager
  137. * is already running.
  138. */
  139. static int
  140. startup_error_handler(Display *dpy, XErrorEvent *error)
  141. {
  142. other_wm_running = True;
  143. return -1;
  144. }
  145. static void
  146. cleanup()
  147. {
  148. while(sel) {
  149. resize(sel);
  150. unmanage(sel);
  151. }
  152. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  153. }
  154. void
  155. quit(Arg *arg)
  156. {
  157. running = False;
  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("dwm: 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("dwm: another window manager is already running\n");
  192. sx = sy = 0;
  193. sw = DisplayWidth(dpy, screen);
  194. sh = DisplayHeight(dpy, screen);
  195. issel = 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[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  200. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  201. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  202. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  203. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  204. PropModeReplace, (unsigned char *) net_atom, NetLast);
  205. /* init cursors */
  206. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  207. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  208. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  209. update_keys();
  210. /* style */
  211. dc.bg = initcolor(BGCOLOR);
  212. dc.fg = initcolor(FGCOLOR);
  213. dc.border = initcolor(BORDERCOLOR);
  214. initfont(FONT);
  215. th = dc.font.height + 4;
  216. dc.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen));
  217. dc.gc = XCreateGC(dpy, root, 0, 0);
  218. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  219. | LeaveWindowMask;
  220. wa.cursor = cursor[CurNormal];
  221. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  222. scan_wins();
  223. while(running) {
  224. XNextEvent(dpy, &ev);
  225. if(handler[ev.type])
  226. (handler[ev.type])(&ev); /* call handler */
  227. }
  228. cleanup();
  229. XCloseDisplay(dpy);
  230. return 0;
  231. }