main.c 7.3 KB

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