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.

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