main.c 7.5 KB

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