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.

327 lines
7.1 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
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 <errno.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <X11/cursorfont.h>
  12. #include <X11/Xatom.h>
  13. #include <X11/Xproto.h>
  14. #include "dwm.h"
  15. /********** CUSTOMIZE **********/
  16. char *tags[TLast] = {
  17. [Tscratch] = "scratch",
  18. [Tdev] = "dev",
  19. [Twww] = "www",
  20. [Twork] = "work",
  21. };
  22. /********** CUSTOMIZE **********/
  23. /* X structs */
  24. Display *dpy;
  25. Window root, barwin;
  26. Atom wm_atom[WMLast], net_atom[NetLast];
  27. Cursor cursor[CurLast];
  28. Bool running = True;
  29. Bool issel;
  30. int tsel = Tdev; /* default tag */
  31. int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
  32. char stext[1024];
  33. DC dc = {0};
  34. Client *clients = NULL;
  35. Client *sel = NULL;
  36. static Bool otherwm;
  37. static const char version[] =
  38. "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  39. static int (*xerrorxlib)(Display *, XErrorEvent *);
  40. static void
  41. usage() { eprint("usage: dwm [-v]\n"); }
  42. static void
  43. scan()
  44. {
  45. unsigned int i, num;
  46. Window *wins;
  47. XWindowAttributes wa;
  48. Window d1, d2;
  49. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  50. for(i = 0; i < num; i++) {
  51. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  52. continue;
  53. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  54. continue;
  55. if(wa.map_state == IsViewable)
  56. manage(wins[i], &wa);
  57. }
  58. }
  59. if(wins)
  60. XFree(wins);
  61. }
  62. static void
  63. cleanup()
  64. {
  65. while(sel) {
  66. resize(sel, True);
  67. unmanage(sel);
  68. }
  69. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  70. }
  71. void
  72. quit(Arg *arg)
  73. {
  74. running = False;
  75. }
  76. static int
  77. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  78. {
  79. Atom real;
  80. int format;
  81. unsigned long res, extra;
  82. int status;
  83. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  84. &res, &extra, prop);
  85. if(status != Success || *prop == 0) {
  86. return 0;
  87. }
  88. if(res == 0) {
  89. free((void *) *prop);
  90. }
  91. return res;
  92. }
  93. int
  94. getproto(Window w)
  95. {
  96. unsigned char *protocols;
  97. long res;
  98. int protos = 0;
  99. int i;
  100. res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
  101. if(res <= 0) {
  102. return protos;
  103. }
  104. for(i = 0; i < res; i++) {
  105. if(protocols[i] == wm_atom[WMDelete])
  106. protos |= WM_PROTOCOL_DELWIN;
  107. }
  108. free((char *) protocols);
  109. return protos;
  110. }
  111. void
  112. sendevent(Window w, Atom a, long value)
  113. {
  114. XEvent e;
  115. e.type = ClientMessage;
  116. e.xclient.window = w;
  117. e.xclient.message_type = a;
  118. e.xclient.format = 32;
  119. e.xclient.data.l[0] = value;
  120. e.xclient.data.l[1] = CurrentTime;
  121. XSendEvent(dpy, w, False, NoEventMask, &e);
  122. XFlush(dpy);
  123. }
  124. /*
  125. * Startup Error handler to check if another window manager
  126. * is already running.
  127. */
  128. static int
  129. xerrorstart(Display *dsply, XErrorEvent *ee)
  130. {
  131. otherwm = True;
  132. return -1;
  133. }
  134. /*
  135. * There's no way to check accesses to destroyed windows, thus
  136. * those cases are ignored (especially on UnmapNotify's).
  137. * Other types of errors call Xlib's default error handler, which
  138. * calls exit().
  139. */
  140. int
  141. xerror(Display *dpy, XErrorEvent *ee)
  142. {
  143. if(ee->error_code == BadWindow
  144. || (ee->request_code == X_SetInputFocus
  145. && ee->error_code == BadMatch)
  146. || (ee->request_code == X_PolyText8
  147. && ee->error_code == BadDrawable)
  148. || (ee->request_code == X_PolyFillRectangle
  149. && ee->error_code == BadDrawable)
  150. || (ee->request_code == X_PolySegment
  151. && ee->error_code == BadDrawable)
  152. || (ee->request_code == X_ConfigureWindow
  153. && ee->error_code == BadMatch)
  154. || (ee->request_code == X_GrabKey
  155. && ee->error_code == BadAccess))
  156. return 0;
  157. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  158. ee->request_code, ee->error_code);
  159. return xerrorxlib(dpy, ee); /* may call exit() */
  160. }
  161. int
  162. main(int argc, char *argv[])
  163. {
  164. int i, n;
  165. fd_set rd;
  166. XSetWindowAttributes wa;
  167. unsigned int mask;
  168. Bool readstdin = True;
  169. Window w;
  170. XEvent ev;
  171. for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
  172. switch (argv[i][1]) {
  173. case 'v':
  174. fprintf(stdout, "%s", version);
  175. exit(0);
  176. break;
  177. default:
  178. usage();
  179. break;
  180. }
  181. }
  182. dpy = XOpenDisplay(0);
  183. if(!dpy)
  184. eprint("dwm: cannot connect X server\n");
  185. screen = DefaultScreen(dpy);
  186. root = RootWindow(dpy, screen);
  187. /* check if another WM is already running */
  188. otherwm = False;
  189. XSetErrorHandler(xerrorstart);
  190. /* this causes an error if some other WM is running */
  191. XSelectInput(dpy, root, SubstructureRedirectMask);
  192. XFlush(dpy);
  193. if(otherwm)
  194. eprint("dwm: another window manager is already running\n");
  195. XSetErrorHandler(0);
  196. xerrorxlib = XSetErrorHandler(xerror);
  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. grabkeys();
  209. /* style */
  210. dc.bg = getcolor(BGCOLOR);
  211. dc.fg = getcolor(FGCOLOR);
  212. dc.border = getcolor(BORDERCOLOR);
  213. setfont(FONT);
  214. sx = sy = 0;
  215. sw = DisplayWidth(dpy, screen);
  216. sh = DisplayHeight(dpy, screen);
  217. mw = (sw * MASTERW) / 100;
  218. wa.override_redirect = 1;
  219. wa.background_pixmap = ParentRelative;
  220. wa.event_mask = ButtonPressMask | ExposureMask;
  221. bx = by = 0;
  222. bw = sw;
  223. dc.h = bh = dc.font.height + 4;
  224. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  225. CopyFromParent, DefaultVisual(dpy, screen),
  226. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  227. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  228. XMapRaised(dpy, barwin);
  229. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  230. dc.gc = XCreateGC(dpy, root, 0, 0);
  231. drawstatus();
  232. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  233. wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
  234. | LeaveWindowMask;
  235. wa.cursor = cursor[CurNormal];
  236. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  237. strcpy(stext, "dwm-"VERSION);
  238. scan();
  239. /* main event loop, reads status text from stdin as well */
  240. Mainloop:
  241. while(running) {
  242. FD_ZERO(&rd);
  243. if(readstdin)
  244. FD_SET(STDIN_FILENO, &rd);
  245. FD_SET(ConnectionNumber(dpy), &rd);
  246. i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
  247. if(i == -1 && errno == EINTR)
  248. continue;
  249. if(i < 0)
  250. eprint("select failed\n");
  251. else if(i > 0) {
  252. if(FD_ISSET(ConnectionNumber(dpy), &rd)) {
  253. while(XPending(dpy)) {
  254. XNextEvent(dpy, &ev);
  255. if(handler[ev.type])
  256. (handler[ev.type])(&ev); /* call handler */
  257. }
  258. }
  259. if(readstdin && FD_ISSET(STDIN_FILENO, &rd)) {
  260. i = n = 0;
  261. for(;;) {
  262. if((i = getchar()) == EOF) {
  263. /* broken pipe/end of producer */
  264. readstdin = False;
  265. strcpy(stext, "broken pipe");
  266. goto Mainloop;
  267. }
  268. if(i == '\n' || n >= sizeof(stext) - 1)
  269. break;
  270. stext[n++] = i;
  271. }
  272. stext[n] = 0;
  273. drawstatus();
  274. }
  275. }
  276. }
  277. cleanup();
  278. XCloseDisplay(dpy);
  279. return 0;
  280. }