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.

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