You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
8.2 KiB

18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
  1. /* (C)opyright MMVI-MMVII 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 <locale.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[256];
  18. Bool *seltag;
  19. int bh, bmw, screen, sx, sy, sw, sh, wax, way, waw, wah;
  20. unsigned int master, nmaster, ntags, numlockmask;
  21. Atom wmatom[WMLast], netatom[NetLast];
  22. Bool running = True;
  23. Bool selscreen = 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(stack) {
  38. resize(stack, True);
  39. unmanage(stack);
  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. XFreeCursor(dpy, cursor[CurNormal]);
  50. XFreeCursor(dpy, cursor[CurResize]);
  51. XFreeCursor(dpy, cursor[CurMove]);
  52. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  53. XSync(dpy, False);
  54. free(seltag);
  55. }
  56. static void
  57. scan(void) {
  58. unsigned int i, num;
  59. Window *wins, d1, d2;
  60. XWindowAttributes wa;
  61. wins = NULL;
  62. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  63. for(i = 0; i < num; i++) {
  64. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  65. continue;
  66. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  67. continue;
  68. if(wa.map_state == IsViewable)
  69. manage(wins[i], &wa);
  70. }
  71. }
  72. if(wins)
  73. XFree(wins);
  74. }
  75. static void
  76. setup(void) {
  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. /* init modifier map */
  94. numlockmask = 0;
  95. modmap = XGetModifierMapping(dpy);
  96. for (i = 0; i < 8; i++) {
  97. for (j = 0; j < modmap->max_keypermod; j++) {
  98. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  99. numlockmask = (1 << i);
  100. }
  101. }
  102. XFreeModifiermap(modmap);
  103. /* select for events */
  104. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  105. | EnterWindowMask | LeaveWindowMask;
  106. wa.cursor = cursor[CurNormal];
  107. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  108. grabkeys();
  109. initrregs();
  110. for(ntags = 0; tags[ntags]; ntags++);
  111. seltag = emallocz(sizeof(Bool) * ntags);
  112. seltag[0] = True;
  113. /* style */
  114. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  115. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  116. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  117. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  118. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  119. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  120. setfont(FONT);
  121. /* geometry */
  122. sx = sy = 0;
  123. sw = DisplayWidth(dpy, screen);
  124. sh = DisplayHeight(dpy, screen);
  125. master = MASTER;
  126. nmaster = NMASTER;
  127. bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
  128. /* bar */
  129. dc.h = bh = dc.font.height + 2;
  130. wa.override_redirect = 1;
  131. wa.background_pixmap = ParentRelative;
  132. wa.event_mask = ButtonPressMask | ExposureMask;
  133. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0, DefaultDepth(dpy, screen),
  134. CopyFromParent, DefaultVisual(dpy, screen),
  135. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  136. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  137. XMapRaised(dpy, barwin);
  138. strcpy(stext, "dwm-"VERSION);
  139. /* windowarea */
  140. wax = sx;
  141. way = sy + bh;
  142. wah = sh - bh;
  143. waw = sw;
  144. /* pixmap for everything */
  145. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  146. dc.gc = XCreateGC(dpy, root, 0, 0);
  147. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  148. /* multihead support */
  149. selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  150. }
  151. /*
  152. * Startup Error handler to check if another window manager
  153. * is already running.
  154. */
  155. static int
  156. xerrorstart(Display *dsply, XErrorEvent *ee) {
  157. otherwm = True;
  158. return -1;
  159. }
  160. /* extern */
  161. int
  162. getproto(Window w) {
  163. int i, format, protos, status;
  164. unsigned long extra, res;
  165. Atom *protocols, real;
  166. protos = 0;
  167. status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
  168. XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
  169. if(status != Success || protocols == 0)
  170. return protos;
  171. for(i = 0; i < res; i++)
  172. if(protocols[i] == wmatom[WMDelete])
  173. protos |= PROTODELWIN;
  174. free(protocols);
  175. return protos;
  176. }
  177. void
  178. sendevent(Window w, Atom a, long value) {
  179. XEvent e;
  180. e.type = ClientMessage;
  181. e.xclient.window = w;
  182. e.xclient.message_type = a;
  183. e.xclient.format = 32;
  184. e.xclient.data.l[0] = value;
  185. e.xclient.data.l[1] = CurrentTime;
  186. XSendEvent(dpy, w, False, NoEventMask, &e);
  187. XSync(dpy, False);
  188. }
  189. void
  190. quit(Arg *arg) {
  191. readin = running = False;
  192. }
  193. /* There's no way to check accesses to destroyed windows, thus those cases are
  194. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  195. * default error handler, which may call exit.
  196. */
  197. int
  198. xerror(Display *dpy, XErrorEvent *ee) {
  199. if(ee->error_code == BadWindow
  200. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  201. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  202. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  203. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  204. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  205. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  206. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  207. return 0;
  208. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  209. ee->request_code, ee->error_code);
  210. return xerrorxlib(dpy, ee); /* may call exit */
  211. }
  212. int
  213. main(int argc, char *argv[]) {
  214. char *p;
  215. int r, xfd;
  216. fd_set rd;
  217. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  218. fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
  219. exit(EXIT_SUCCESS);
  220. }
  221. else if(argc != 1)
  222. eprint("usage: dwm [-v]\n");
  223. setlocale(LC_CTYPE, "");
  224. dpy = XOpenDisplay(0);
  225. if(!dpy)
  226. eprint("dwm: cannot open display\n");
  227. xfd = ConnectionNumber(dpy);
  228. screen = DefaultScreen(dpy);
  229. root = RootWindow(dpy, screen);
  230. otherwm = False;
  231. XSetErrorHandler(xerrorstart);
  232. /* this causes an error if some other window manager is running */
  233. XSelectInput(dpy, root, SubstructureRedirectMask);
  234. XSync(dpy, False);
  235. if(otherwm)
  236. eprint("dwm: another window manager is already running\n");
  237. XSync(dpy, False);
  238. XSetErrorHandler(NULL);
  239. xerrorxlib = XSetErrorHandler(xerror);
  240. XSync(dpy, False);
  241. setup();
  242. drawstatus();
  243. scan();
  244. /* main event loop, also reads status text from stdin */
  245. XSync(dpy, False);
  246. procevent();
  247. readin = True;
  248. while(running) {
  249. FD_ZERO(&rd);
  250. if(readin)
  251. FD_SET(STDIN_FILENO, &rd);
  252. FD_SET(xfd, &rd);
  253. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  254. if(errno == EINTR)
  255. continue;
  256. eprint("select failed\n");
  257. }
  258. if(FD_ISSET(STDIN_FILENO, &rd)) {
  259. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  260. case -1:
  261. strncpy(stext, strerror(errno), sizeof stext - 1);
  262. stext[sizeof stext - 1] = '\0';
  263. readin = False;
  264. break;
  265. case 0:
  266. strncpy(stext, "EOF", 4);
  267. readin = False;
  268. break;
  269. default:
  270. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  271. for(p = stext + strlen(stext) - 1; p >= stext && *p != '\n'; --p);
  272. if(p > stext)
  273. strncpy(stext, p + 1, sizeof stext);
  274. }
  275. drawstatus();
  276. }
  277. if(FD_ISSET(xfd, &rd))
  278. procevent();
  279. }
  280. cleanup();
  281. XCloseDisplay(dpy);
  282. return 0;
  283. }