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.

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