main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. Bool *seltag;
  75. int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
  76. unsigned int ntags;
  77. Atom wmatom[WMLast], netatom[NetLast];
  78. Bool running = True;
  79. Bool issel = True;
  80. Client *clients = NULL;
  81. Client *sel = NULL;
  82. Cursor cursor[CurLast];
  83. Display *dpy;
  84. DC dc = {0};
  85. Window root, barwin;
  86. int
  87. getproto(Window w)
  88. {
  89. int protos = 0;
  90. int i;
  91. long res;
  92. Atom *protocols;
  93. res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L,
  94. ((unsigned char **)&protocols));
  95. if(res <= 0) {
  96. return protos;
  97. }
  98. for(i = 0; i < res; i++) {
  99. if(protocols[i] == wmatom[WMDelete])
  100. protos |= PROTODELWIN;
  101. }
  102. free((char *) protocols);
  103. return protos;
  104. }
  105. void
  106. sendevent(Window w, Atom a, long value)
  107. {
  108. XEvent e;
  109. e.type = ClientMessage;
  110. e.xclient.window = w;
  111. e.xclient.message_type = a;
  112. e.xclient.format = 32;
  113. e.xclient.data.l[0] = value;
  114. e.xclient.data.l[1] = CurrentTime;
  115. XSendEvent(dpy, w, False, NoEventMask, &e);
  116. XSync(dpy, False);
  117. }
  118. void
  119. quit(Arg *arg)
  120. {
  121. running = False;
  122. }
  123. /*
  124. * There's no way to check accesses to destroyed windows, thus those cases are
  125. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  126. * default error handler, which calls exit().
  127. */
  128. int
  129. xerror(Display *dpy, XErrorEvent *ee)
  130. {
  131. if(ee->error_code == BadWindow
  132. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  133. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  134. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  135. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  136. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  137. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
  138. return 0;
  139. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  140. ee->request_code, ee->error_code);
  141. return xerrorxlib(dpy, ee); /* may call exit() */
  142. }
  143. int
  144. main(int argc, char *argv[])
  145. {
  146. int i, xfd;
  147. unsigned int mask;
  148. fd_set rd;
  149. Bool readin = True;
  150. Window w;
  151. XEvent ev;
  152. XSetWindowAttributes wa;
  153. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  154. fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  155. exit(EXIT_SUCCESS);
  156. }
  157. else if(argc != 1)
  158. eprint("usage: dwm [-v]\n");
  159. dpy = XOpenDisplay(0);
  160. if(!dpy)
  161. eprint("dwm: cannot open display\n");
  162. xfd = ConnectionNumber(dpy);
  163. screen = DefaultScreen(dpy);
  164. root = RootWindow(dpy, screen);
  165. otherwm = False;
  166. XSetErrorHandler(xerrorstart);
  167. /* this causes an error if some other window manager is running */
  168. XSelectInput(dpy, root, SubstructureRedirectMask);
  169. XSync(dpy, False);
  170. if(otherwm)
  171. eprint("dwm: another window manager is already running\n");
  172. XSetErrorHandler(NULL);
  173. xerrorxlib = XSetErrorHandler(xerror);
  174. XSync(dpy, False);
  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. wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
  187. wa.cursor = cursor[CurNormal];
  188. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  189. grabkeys();
  190. initrregs();
  191. for(ntags = 0; tags[ntags]; ntags++);
  192. seltag = emallocz(sizeof(Bool) * ntags);
  193. seltag[DEFTAG] = True;
  194. /* style */
  195. dc.bg = getcolor(BGCOLOR);
  196. dc.fg = getcolor(FGCOLOR);
  197. dc.border = getcolor(BORDERCOLOR);
  198. setfont(FONT);
  199. sx = sy = 0;
  200. sw = DisplayWidth(dpy, screen);
  201. sh = DisplayHeight(dpy, screen);
  202. mw = (sw * MASTERW) / 100;
  203. wa.override_redirect = 1;
  204. wa.background_pixmap = ParentRelative;
  205. wa.event_mask = ButtonPressMask | ExposureMask;
  206. bx = by = 0;
  207. bw = sw;
  208. dc.h = bh = dc.font.height + 4;
  209. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  210. CopyFromParent, DefaultVisual(dpy, screen),
  211. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  212. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  213. XMapRaised(dpy, barwin);
  214. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  215. dc.gc = XCreateGC(dpy, root, 0, 0);
  216. strcpy(stext, "dwm-"VERSION);
  217. drawstatus();
  218. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  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(readin && FD_ISSET(STDIN_FILENO, &rd)) {
  234. readin = NULL != fgets(stext, sizeof(stext), stdin);
  235. if(readin)
  236. stext[strlen(stext) - 1] = 0;
  237. else
  238. strcpy(stext, "broken pipe");
  239. drawstatus();
  240. }
  241. if(FD_ISSET(xfd, &rd)) {
  242. while(XPending(dpy)) {
  243. XNextEvent(dpy, &ev);
  244. if(handler[ev.type])
  245. (handler[ev.type])(&ev); /* call handler */
  246. }
  247. }
  248. }
  249. }
  250. cleanup();
  251. XCloseDisplay(dpy);
  252. return 0;
  253. }