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.

284 lines
6.5 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. Window w;
  148. XEvent ev;
  149. XSetWindowAttributes wa;
  150. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  151. fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  152. exit(EXIT_SUCCESS);
  153. }
  154. else if(argc != 1)
  155. eprint("usage: dwm [-v]\n");
  156. dpy = XOpenDisplay(0);
  157. if(!dpy)
  158. eprint("dwm: cannot connect X server\n");
  159. screen = DefaultScreen(dpy);
  160. root = RootWindow(dpy, screen);
  161. /* check if another WM is already running */
  162. otherwm = False;
  163. XSetErrorHandler(xerrorstart);
  164. /* this causes an error if some other WM is running */
  165. XSelectInput(dpy, root, SubstructureRedirectMask);
  166. XSync(dpy, False);
  167. if(otherwm)
  168. eprint("dwm: another window manager is already running\n");
  169. XSetErrorHandler(NULL);
  170. xerrorxlib = XSetErrorHandler(xerror);
  171. /* init atoms */
  172. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  173. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  174. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  175. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  176. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  177. PropModeReplace, (unsigned char *) netatom, NetLast);
  178. /* init cursors */
  179. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  180. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  181. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  182. grabkeys();
  183. /* style */
  184. dc.bg = getcolor(BGCOLOR);
  185. dc.fg = getcolor(FGCOLOR);
  186. dc.border = getcolor(BORDERCOLOR);
  187. setfont(FONT);
  188. sx = sy = 0;
  189. sw = DisplayWidth(dpy, screen);
  190. sh = DisplayHeight(dpy, screen);
  191. mw = (sw * MASTERW) / 100;
  192. wa.override_redirect = 1;
  193. wa.background_pixmap = ParentRelative;
  194. wa.event_mask = ButtonPressMask | ExposureMask;
  195. bx = by = 0;
  196. bw = sw;
  197. dc.h = bh = dc.font.height + 4;
  198. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  199. CopyFromParent, DefaultVisual(dpy, screen),
  200. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  201. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  202. XMapRaised(dpy, barwin);
  203. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  204. dc.gc = XCreateGC(dpy, root, 0, 0);
  205. drawstatus();
  206. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  207. wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
  208. wa.cursor = cursor[CurNormal];
  209. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  210. strcpy(stext, "dwm-"VERSION);
  211. scan();
  212. /* main event loop, reads status text from stdin as well */
  213. while(running) {
  214. FD_ZERO(&rd);
  215. FD_SET(STDIN_FILENO, &rd);
  216. FD_SET(ConnectionNumber(dpy), &rd);
  217. i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
  218. if(i == -1 && errno == EINTR)
  219. continue;
  220. if(i < 0)
  221. eprint("select failed\n");
  222. else if(i > 0) {
  223. if(FD_ISSET(ConnectionNumber(dpy), &rd)) {
  224. while(XPending(dpy)) {
  225. XNextEvent(dpy, &ev);
  226. if(handler[ev.type])
  227. (handler[ev.type])(&ev); /* call handler */
  228. }
  229. }
  230. if(FD_ISSET(STDIN_FILENO, &rd)) {
  231. if(!fgets(stext, sizeof(stext), stdin))
  232. break;
  233. else
  234. stext[strlen(stext) - 1] = 0;
  235. drawstatus();
  236. }
  237. }
  238. }
  239. cleanup();
  240. XCloseDisplay(dpy);
  241. return 0;
  242. }