main.c 7.5 KB

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