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.

295 lines
6.7 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
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. ungrabkeys();
  22. while(sel) {
  23. resize(sel, True, TopLeft);
  24. unmanage(sel);
  25. }
  26. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  27. }
  28. static void
  29. scan()
  30. {
  31. unsigned int i, num;
  32. Window *wins, d1, d2;
  33. XWindowAttributes wa;
  34. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  35. for(i = 0; i < num; i++) {
  36. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  37. continue;
  38. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  39. continue;
  40. if(wa.map_state == IsViewable)
  41. manage(wins[i], &wa);
  42. }
  43. }
  44. if(wins)
  45. XFree(wins);
  46. }
  47. static int
  48. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  49. {
  50. int status, format;
  51. unsigned long res, extra;
  52. Atom real;
  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. Bool *seltag;
  76. int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
  77. unsigned int ntags;
  78. Atom wmatom[WMLast], netatom[NetLast];
  79. Bool running = True;
  80. Bool issel = True;
  81. Client *clients = NULL;
  82. Client *sel = NULL;
  83. Cursor cursor[CurLast];
  84. Display *dpy;
  85. DC dc = {0};
  86. Window root, barwin;
  87. int
  88. getproto(Window w)
  89. {
  90. int protos = 0;
  91. int i;
  92. long res;
  93. Atom *protocols;
  94. res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L,
  95. ((unsigned char **)&protocols));
  96. if(res <= 0) {
  97. return protos;
  98. }
  99. for(i = 0; i < res; i++) {
  100. if(protocols[i] == wmatom[WMDelete])
  101. protos |= PROTODELWIN;
  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. XSync(dpy, False);
  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 those cases are
  126. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  127. * default error handler, which calls exit().
  128. */
  129. int
  130. xerror(Display *dpy, XErrorEvent *ee)
  131. {
  132. if(ee->error_code == BadWindow
  133. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  134. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  135. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  136. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  137. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  138. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
  139. return 0;
  140. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  141. ee->request_code, ee->error_code);
  142. return xerrorxlib(dpy, ee); /* may call exit() */
  143. }
  144. int
  145. main(int argc, char *argv[])
  146. {
  147. int i, xfd;
  148. unsigned int mask;
  149. fd_set rd;
  150. Bool readin = True;
  151. Window w;
  152. XEvent ev;
  153. XSetWindowAttributes wa;
  154. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  155. fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  156. exit(EXIT_SUCCESS);
  157. }
  158. else if(argc != 1)
  159. eprint("usage: dwm [-v]\n");
  160. dpy = XOpenDisplay(0);
  161. if(!dpy)
  162. eprint("dwm: cannot open display\n");
  163. xfd = ConnectionNumber(dpy);
  164. screen = DefaultScreen(dpy);
  165. root = RootWindow(dpy, screen);
  166. otherwm = False;
  167. XSetErrorHandler(xerrorstart);
  168. /* this causes an error if some other window manager is running */
  169. XSelectInput(dpy, root, SubstructureRedirectMask);
  170. XSync(dpy, False);
  171. if(otherwm)
  172. eprint("dwm: another window manager is already running\n");
  173. XSetErrorHandler(NULL);
  174. xerrorxlib = XSetErrorHandler(xerror);
  175. /* init atoms */
  176. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  177. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  178. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  179. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  180. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  181. PropModeReplace, (unsigned char *) netatom, NetLast);
  182. /* init cursors */
  183. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  184. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  185. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  186. grabkeys();
  187. initrregs();
  188. for(ntags = 0; tags[ntags]; ntags++);
  189. seltag = emallocz(sizeof(Bool) * ntags);
  190. seltag[DEFTAG] = True;
  191. /* style */
  192. dc.bg = getcolor(BGCOLOR);
  193. dc.fg = getcolor(FGCOLOR);
  194. dc.border = getcolor(BORDERCOLOR);
  195. setfont(FONT);
  196. sx = sy = 0;
  197. sw = DisplayWidth(dpy, screen);
  198. sh = DisplayHeight(dpy, screen);
  199. mw = (sw * MASTERW) / 100;
  200. wa.override_redirect = 1;
  201. wa.background_pixmap = ParentRelative;
  202. wa.event_mask = ButtonPressMask | ExposureMask;
  203. bx = by = 0;
  204. bw = sw;
  205. dc.h = bh = dc.font.height + 4;
  206. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  207. CopyFromParent, DefaultVisual(dpy, screen),
  208. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  209. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  210. XMapRaised(dpy, barwin);
  211. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  212. dc.gc = XCreateGC(dpy, root, 0, 0);
  213. strcpy(stext, "dwm-"VERSION);
  214. drawstatus();
  215. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  216. wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
  217. wa.cursor = cursor[CurNormal];
  218. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  219. scan();
  220. /* main event loop, also reads status text from stdin */
  221. XSync(dpy, False);
  222. while(running) {
  223. FD_ZERO(&rd);
  224. if(readin)
  225. FD_SET(STDIN_FILENO, &rd);
  226. FD_SET(xfd, &rd);
  227. i = select(xfd + 1, &rd, NULL, NULL, NULL);
  228. if(i == -1 && errno == EINTR)
  229. continue;
  230. if(i < 0)
  231. eprint("select failed\n");
  232. else if(i > 0) {
  233. if(FD_ISSET(xfd, &rd)) {
  234. while(XPending(dpy)) {
  235. XNextEvent(dpy, &ev);
  236. if(handler[ev.type])
  237. (handler[ev.type])(&ev); /* call handler */
  238. }
  239. }
  240. if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
  241. readin = NULL != fgets(stext, sizeof(stext), stdin);
  242. if(readin)
  243. stext[strlen(stext) - 1] = 0;
  244. else
  245. strcpy(stext, "broken pipe");
  246. drawstatus();
  247. }
  248. }
  249. }
  250. cleanup();
  251. XCloseDisplay(dpy);
  252. return 0;
  253. }