main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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, wax, way, waw, wah;
  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. /* windowarea */
  136. wax = sx;
  137. way = bh;
  138. wah = sh - bh;
  139. waw = sw;
  140. /* pixmap for everything */
  141. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  142. dc.gc = XCreateGC(dpy, root, 0, 0);
  143. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  144. /* multihead support */
  145. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  146. }
  147. /*
  148. * Startup Error handler to check if another window manager
  149. * is already running.
  150. */
  151. static int
  152. xerrorstart(Display *dsply, XErrorEvent *ee) {
  153. otherwm = True;
  154. return -1;
  155. }
  156. /* extern */
  157. int
  158. getproto(Window w) {
  159. int i, format, protos, status;
  160. unsigned long extra, res;
  161. Atom *protocols, real;
  162. protos = 0;
  163. status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
  164. XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
  165. if(status != Success || protocols == 0)
  166. return protos;
  167. for(i = 0; i < res; i++)
  168. if(protocols[i] == wmatom[WMDelete])
  169. protos |= PROTODELWIN;
  170. free(protocols);
  171. return protos;
  172. }
  173. void
  174. sendevent(Window w, Atom a, long value) {
  175. XEvent e;
  176. e.type = ClientMessage;
  177. e.xclient.window = w;
  178. e.xclient.message_type = a;
  179. e.xclient.format = 32;
  180. e.xclient.data.l[0] = value;
  181. e.xclient.data.l[1] = CurrentTime;
  182. XSendEvent(dpy, w, False, NoEventMask, &e);
  183. XSync(dpy, False);
  184. }
  185. void
  186. quit(Arg *arg) {
  187. readin = running = False;
  188. }
  189. /* There's no way to check accesses to destroyed windows, thus those cases are
  190. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  191. * default error handler, which may call exit.
  192. */
  193. int
  194. xerror(Display *dpy, XErrorEvent *ee) {
  195. if(ee->error_code == BadWindow
  196. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  197. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  198. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  199. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  200. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  201. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  202. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  203. return 0;
  204. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  205. ee->request_code, ee->error_code);
  206. return xerrorxlib(dpy, ee); /* may call exit */
  207. }
  208. int
  209. main(int argc, char *argv[]) {
  210. int r, xfd;
  211. fd_set rd;
  212. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  213. fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  214. exit(EXIT_SUCCESS);
  215. }
  216. else if(argc != 1)
  217. eprint("usage: dwm [-v]\n");
  218. dpy = XOpenDisplay(0);
  219. if(!dpy)
  220. eprint("dwm: cannot open display\n");
  221. xfd = ConnectionNumber(dpy);
  222. screen = DefaultScreen(dpy);
  223. root = RootWindow(dpy, screen);
  224. otherwm = False;
  225. XSetErrorHandler(xerrorstart);
  226. /* this causes an error if some other window manager is running */
  227. XSelectInput(dpy, root, SubstructureRedirectMask);
  228. XSync(dpy, False);
  229. if(otherwm)
  230. eprint("dwm: another window manager is already running\n");
  231. XSync(dpy, False);
  232. XSetErrorHandler(NULL);
  233. xerrorxlib = XSetErrorHandler(xerror);
  234. XSync(dpy, False);
  235. setup();
  236. drawstatus();
  237. scan();
  238. /* main event loop, also reads status text from stdin */
  239. XSync(dpy, False);
  240. procevent();
  241. readin = True;
  242. while(running) {
  243. FD_ZERO(&rd);
  244. if(readin)
  245. FD_SET(STDIN_FILENO, &rd);
  246. FD_SET(xfd, &rd);
  247. r = select(xfd + 1, &rd, NULL, NULL, NULL);
  248. if((r == -1) && (errno == EINTR))
  249. continue;
  250. if(r > 0) {
  251. if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
  252. readin = NULL != fgets(stext, sizeof(stext), stdin);
  253. if(readin)
  254. stext[strlen(stext) - 1] = 0;
  255. else
  256. strcpy(stext, "broken pipe");
  257. drawstatus();
  258. }
  259. }
  260. else if(r < 0)
  261. eprint("select failed\n");
  262. procevent();
  263. }
  264. cleanup();
  265. XCloseDisplay(dpy);
  266. return 0;
  267. }