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.

275 lines
5.9 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 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 *cstart = NULL;
  33. Client *cend = NULL;
  34. Client *csel = NULL;
  35. static Bool other_wm_running;
  36. static const char version[] =
  37. "dwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  38. static int (*x_error_handler) (Display *, XErrorEvent *);
  39. static void
  40. usage() { error("usage: dwm [-v]\n"); }
  41. static void
  42. scan_wins()
  43. {
  44. unsigned int i, num;
  45. Window *wins;
  46. XWindowAttributes wa;
  47. Window d1, d2;
  48. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  49. for(i = 0; i < num; i++) {
  50. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  51. continue;
  52. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  53. continue;
  54. if(wa.map_state == IsViewable)
  55. manage(wins[i], &wa);
  56. }
  57. }
  58. if(wins)
  59. XFree(wins);
  60. }
  61. static int
  62. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  63. {
  64. Atom real;
  65. int format;
  66. unsigned long res, extra;
  67. int status;
  68. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  69. &res, &extra, prop);
  70. if(status != Success || *prop == 0) {
  71. return 0;
  72. }
  73. if(res == 0) {
  74. free((void *) *prop);
  75. }
  76. return res;
  77. }
  78. int
  79. win_proto(Window w)
  80. {
  81. unsigned char *protocols;
  82. long res;
  83. int protos = 0;
  84. int i;
  85. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
  86. if(res <= 0) {
  87. return protos;
  88. }
  89. for(i = 0; i < res; i++) {
  90. if(protocols[i] == wm_atom[WMDelete])
  91. protos |= WM_PROTOCOL_DELWIN;
  92. }
  93. free((char *) protocols);
  94. return protos;
  95. }
  96. void
  97. send_message(Window w, Atom a, long value)
  98. {
  99. XEvent e;
  100. e.type = ClientMessage;
  101. e.xclient.window = w;
  102. e.xclient.message_type = a;
  103. e.xclient.format = 32;
  104. e.xclient.data.l[0] = value;
  105. e.xclient.data.l[1] = CurrentTime;
  106. XSendEvent(dpy, w, False, NoEventMask, &e);
  107. XFlush(dpy);
  108. }
  109. /*
  110. * There's no way to check accesses to destroyed windows, thus
  111. * those cases are ignored (especially on UnmapNotify's).
  112. * Other types of errors call Xlib's default error handler, which
  113. * calls exit().
  114. */
  115. int
  116. error_handler(Display *dpy, XErrorEvent *error)
  117. {
  118. if(error->error_code == BadWindow
  119. || (error->request_code == X_SetInputFocus
  120. && error->error_code == BadMatch)
  121. || (error->request_code == X_PolyText8
  122. && error->error_code == BadDrawable)
  123. || (error->request_code == X_PolyFillRectangle
  124. && error->error_code == BadDrawable)
  125. || (error->request_code == X_PolySegment
  126. && error->error_code == BadDrawable)
  127. || (error->request_code == X_ConfigureWindow
  128. && error->error_code == BadMatch)
  129. || (error->request_code == X_GrabKey
  130. && error->error_code == BadAccess))
  131. return 0;
  132. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  133. error->request_code, error->error_code);
  134. return x_error_handler(dpy, error); /* may call exit() */
  135. }
  136. /*
  137. * Startup Error handler to check if another window manager
  138. * is already running.
  139. */
  140. static int
  141. startup_error_handler(Display *dpy, XErrorEvent *error)
  142. {
  143. other_wm_running = True;
  144. return -1;
  145. }
  146. static void
  147. cleanup()
  148. {
  149. while(csel)
  150. unmanage(csel);
  151. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  152. }
  153. void
  154. quit(Arg *arg)
  155. {
  156. running = False;
  157. }
  158. int
  159. main(int argc, char *argv[])
  160. {
  161. int i;
  162. XSetWindowAttributes wa;
  163. unsigned int mask;
  164. Window w;
  165. XEvent ev;
  166. /* command line args */
  167. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  168. switch (argv[i][1]) {
  169. case 'v':
  170. fprintf(stdout, "%s", version);
  171. exit(0);
  172. break;
  173. default:
  174. usage();
  175. break;
  176. }
  177. }
  178. dpy = XOpenDisplay(0);
  179. if(!dpy)
  180. error("dwm: cannot connect X server\n");
  181. screen = DefaultScreen(dpy);
  182. root = RootWindow(dpy, screen);
  183. /* check if another WM is already running */
  184. other_wm_running = False;
  185. XSetErrorHandler(startup_error_handler);
  186. /* this causes an error if some other WM is running */
  187. XSelectInput(dpy, root, SubstructureRedirectMask);
  188. XFlush(dpy);
  189. if(other_wm_running)
  190. error("dwm: another window manager is already running\n");
  191. sx = sy = 0;
  192. sw = DisplayWidth(dpy, screen);
  193. sh = DisplayHeight(dpy, screen);
  194. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  195. XSetErrorHandler(0);
  196. x_error_handler = XSetErrorHandler(error_handler);
  197. /* init atoms */
  198. wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  199. wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  200. net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  201. net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  202. XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
  203. PropModeReplace, (unsigned char *) net_atom, NetLast);
  204. /* init cursors */
  205. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  206. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  207. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  208. update_keys();
  209. /* style */
  210. dc.bg = initcolor(BGCOLOR);
  211. dc.fg = initcolor(FGCOLOR);
  212. dc.border = initcolor(BORDERCOLOR);
  213. initfont(FONT);
  214. th = dc.font.height + 4;
  215. dc.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen));
  216. dc.gc = XCreateGC(dpy, root, 0, 0);
  217. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  218. | LeaveWindowMask;
  219. wa.cursor = cursor[CurNormal];
  220. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  221. scan_wins();
  222. while(running) {
  223. XNextEvent(dpy, &ev);
  224. if(handler[ev.type])
  225. (handler[ev.type])(&ev); /* call handler */
  226. }
  227. cleanup();
  228. XCloseDisplay(dpy);
  229. return 0;
  230. }