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.

323 lines
8.9 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /* © 2004-2007 Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details. */
  3. #include "dwm.h"
  4. #include <errno.h>
  5. #include <locale.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[256];
  17. int screen, sx, sy, sw, sh, wax, way, waw, wah;
  18. unsigned int bh, ntags, numlockmask;
  19. Atom wmatom[WMLast], netatom[NetLast];
  20. Bool *seltag;
  21. Bool selscreen = True;
  22. Client *clients = NULL;
  23. Client *sel = NULL;
  24. Client *stack = NULL;
  25. Cursor cursor[CurLast];
  26. Display *dpy;
  27. DC dc = {0};
  28. Window root, barwin;
  29. /* static */
  30. static int (*xerrorxlib)(Display *, XErrorEvent *);
  31. static Bool otherwm, readin;
  32. static Bool running = True;
  33. static void
  34. cleanup(void) {
  35. close(STDIN_FILENO);
  36. while(stack) {
  37. if(stack->isbanned)
  38. XMoveWindow(dpy, stack->win, stack->x, stack->y);
  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 unsigned long
  57. initcolor(const char *colstr) {
  58. Colormap cmap = DefaultColormap(dpy, screen);
  59. XColor color;
  60. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  61. eprint("error, cannot allocate color '%s'\n", colstr);
  62. return color.pixel;
  63. }
  64. static void
  65. initfont(const char *fontstr) {
  66. char *def, **missing;
  67. int i, n;
  68. missing = NULL;
  69. if(dc.font.set)
  70. XFreeFontSet(dpy, dc.font.set);
  71. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  72. if(missing) {
  73. while(n--)
  74. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  75. XFreeStringList(missing);
  76. }
  77. if(dc.font.set) {
  78. XFontSetExtents *font_extents;
  79. XFontStruct **xfonts;
  80. char **font_names;
  81. dc.font.ascent = dc.font.descent = 0;
  82. font_extents = XExtentsOfFontSet(dc.font.set);
  83. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  84. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  85. if(dc.font.ascent < (*xfonts)->ascent)
  86. dc.font.ascent = (*xfonts)->ascent;
  87. if(dc.font.descent < (*xfonts)->descent)
  88. dc.font.descent = (*xfonts)->descent;
  89. xfonts++;
  90. }
  91. }
  92. else {
  93. if(dc.font.xfont)
  94. XFreeFont(dpy, dc.font.xfont);
  95. dc.font.xfont = NULL;
  96. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  97. eprint("error, cannot load font: '%s'\n", fontstr);
  98. dc.font.ascent = dc.font.xfont->ascent;
  99. dc.font.descent = dc.font.xfont->descent;
  100. }
  101. dc.font.height = dc.font.ascent + dc.font.descent;
  102. }
  103. static void
  104. scan(void) {
  105. unsigned int i, num;
  106. Window *wins, d1, d2;
  107. XWindowAttributes wa;
  108. wins = NULL;
  109. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  110. for(i = 0; i < num; i++) {
  111. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  112. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  113. continue;
  114. if(wa.map_state == IsViewable)
  115. manage(wins[i], &wa);
  116. }
  117. }
  118. if(wins)
  119. XFree(wins);
  120. }
  121. static void
  122. setup(void) {
  123. int i, j;
  124. unsigned int mask;
  125. Window w;
  126. XModifierKeymap *modmap;
  127. XSetWindowAttributes wa;
  128. /* init atoms */
  129. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  130. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  131. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  132. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  133. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  134. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  135. PropModeReplace, (unsigned char *) netatom, NetLast);
  136. /* init cursors */
  137. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  138. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  139. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  140. /* init modifier map */
  141. numlockmask = 0;
  142. modmap = XGetModifierMapping(dpy);
  143. for (i = 0; i < 8; i++)
  144. for (j = 0; j < modmap->max_keypermod; j++) {
  145. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  146. == XKeysymToKeycode(dpy, XK_Num_Lock))
  147. numlockmask = (1 << i);
  148. }
  149. XFreeModifiermap(modmap);
  150. /* select for events */
  151. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  152. | EnterWindowMask | LeaveWindowMask;
  153. wa.cursor = cursor[CurNormal];
  154. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  155. grabkeys();
  156. compileregs();
  157. for(ntags = 0; tags[ntags]; ntags++);
  158. seltag = emallocz(sizeof(Bool) * ntags);
  159. seltag[0] = True;
  160. /* style */
  161. dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
  162. dc.norm[ColBG] = initcolor(NORMBGCOLOR);
  163. dc.norm[ColFG] = initcolor(NORMFGCOLOR);
  164. dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
  165. dc.sel[ColBG] = initcolor(SELBGCOLOR);
  166. dc.sel[ColFG] = initcolor(SELFGCOLOR);
  167. initfont(FONT);
  168. /* geometry */
  169. sx = sy = 0;
  170. sw = DisplayWidth(dpy, screen);
  171. sh = DisplayHeight(dpy, screen);
  172. initlayouts();
  173. /* bar */
  174. dc.h = bh = dc.font.height + 2;
  175. wa.override_redirect = 1;
  176. wa.background_pixmap = ParentRelative;
  177. wa.event_mask = ButtonPressMask | ExposureMask;
  178. barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
  179. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  180. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  181. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  182. XMapRaised(dpy, barwin);
  183. strcpy(stext, "dwm-"VERSION);
  184. /* windowarea */
  185. wax = sx;
  186. way = sy + (TOPBAR ? bh : 0);
  187. wah = sh - bh;
  188. waw = sw;
  189. /* pixmap for everything */
  190. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  191. dc.gc = XCreateGC(dpy, root, 0, 0);
  192. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  193. if(!dc.font.set)
  194. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  195. /* multihead support */
  196. selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  197. }
  198. /*
  199. * Startup Error handler to check if another window manager
  200. * is already running.
  201. */
  202. static int
  203. xerrorstart(Display *dsply, XErrorEvent *ee) {
  204. otherwm = True;
  205. return -1;
  206. }
  207. /* extern */
  208. void
  209. quit(const char *arg) {
  210. readin = running = False;
  211. }
  212. /* There's no way to check accesses to destroyed windows, thus those cases are
  213. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  214. * default error handler, which may call exit.
  215. */
  216. int
  217. xerror(Display *dpy, XErrorEvent *ee) {
  218. if(ee->error_code == BadWindow
  219. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  220. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  221. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  222. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  223. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  224. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  225. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  226. return 0;
  227. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  228. ee->request_code, ee->error_code);
  229. return xerrorxlib(dpy, ee); /* may call exit */
  230. }
  231. int
  232. main(int argc, char *argv[]) {
  233. char *p;
  234. int r, xfd;
  235. fd_set rd;
  236. XEvent ev;
  237. if(argc == 2 && !strncmp("-v", argv[1], 3))
  238. eprint("dwm-"VERSION", © 2004-2007 Anselm R. Garbe\n");
  239. else if(argc != 1)
  240. eprint("usage: dwm [-v]\n");
  241. setlocale(LC_CTYPE, "");
  242. if(!(dpy = XOpenDisplay(0)))
  243. eprint("dwm: cannot open display\n");
  244. xfd = ConnectionNumber(dpy);
  245. screen = DefaultScreen(dpy);
  246. root = RootWindow(dpy, screen);
  247. otherwm = False;
  248. XSetErrorHandler(xerrorstart);
  249. /* this causes an error if some other window manager is running */
  250. XSelectInput(dpy, root, SubstructureRedirectMask);
  251. XSync(dpy, False);
  252. if(otherwm)
  253. eprint("dwm: another window manager is already running\n");
  254. XSync(dpy, False);
  255. XSetErrorHandler(NULL);
  256. xerrorxlib = XSetErrorHandler(xerror);
  257. XSync(dpy, False);
  258. setup();
  259. drawstatus();
  260. scan();
  261. /* main event loop, also reads status text from stdin */
  262. XSync(dpy, False);
  263. readin = True;
  264. while(running) {
  265. FD_ZERO(&rd);
  266. if(readin)
  267. FD_SET(STDIN_FILENO, &rd);
  268. FD_SET(xfd, &rd);
  269. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  270. if(errno == EINTR)
  271. continue;
  272. eprint("select failed\n");
  273. }
  274. if(FD_ISSET(STDIN_FILENO, &rd)) {
  275. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  276. case -1:
  277. strncpy(stext, strerror(errno), sizeof stext - 1);
  278. stext[sizeof stext - 1] = '\0';
  279. readin = False;
  280. break;
  281. case 0:
  282. strncpy(stext, "EOF", 4);
  283. readin = False;
  284. break;
  285. default:
  286. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  287. for(; p >= stext && *p != '\n'; --p);
  288. if(p > stext)
  289. strncpy(stext, p + 1, sizeof stext);
  290. }
  291. drawstatus();
  292. }
  293. while(XPending(dpy)) {
  294. XNextEvent(dpy, &ev);
  295. if(handler[ev.type])
  296. (handler[ev.type])(&ev); /* call handler */
  297. }
  298. }
  299. cleanup();
  300. XCloseDisplay(dpy);
  301. return 0;
  302. }