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.

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