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.

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