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.

349 lines
9.4 KiB

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
18 years ago
19 years ago
18 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
19 years ago
19 years ago
19 years ago
  1. /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
  2. * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
  3. * © 2007 Premysl Hruby <dfenze at gmail dot com>
  4. * See LICENSE file for license details. */
  5. #include "dwm.h"
  6. #include <errno.h>
  7. #include <locale.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/select.h>
  13. #include <X11/cursorfont.h>
  14. #include <X11/keysym.h>
  15. #include <X11/Xatom.h>
  16. #include <X11/Xproto.h>
  17. /* extern */
  18. char stext[256];
  19. int screen, sx, sy, sw, sh, wax, way, waw, wah;
  20. unsigned int bh, bpos, ntags, numlockmask;
  21. Atom wmatom[WMLast], netatom[NetLast];
  22. Bool *seltag;
  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 Bool running = True;
  35. static void
  36. cleanup(void) {
  37. close(STDIN_FILENO);
  38. while(stack) {
  39. if(stack->isbanned)
  40. XMoveWindow(dpy, stack->win, stack->x, stack->y);
  41. unmanage(stack);
  42. }
  43. if(dc.font.set)
  44. XFreeFontSet(dpy, dc.font.set);
  45. else
  46. XFreeFont(dpy, dc.font.xfont);
  47. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  48. XFreePixmap(dpy, dc.drawable);
  49. XFreeGC(dpy, dc.gc);
  50. XDestroyWindow(dpy, barwin);
  51. XFreeCursor(dpy, cursor[CurNormal]);
  52. XFreeCursor(dpy, cursor[CurResize]);
  53. XFreeCursor(dpy, cursor[CurMove]);
  54. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  55. XSync(dpy, False);
  56. free(seltag);
  57. }
  58. static unsigned long
  59. initcolor(const char *colstr) {
  60. Colormap cmap = DefaultColormap(dpy, screen);
  61. XColor color;
  62. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  63. eprint("error, cannot allocate color '%s'\n", colstr);
  64. return color.pixel;
  65. }
  66. static void
  67. initfont(const char *fontstr) {
  68. char *def, **missing;
  69. int i, n;
  70. missing = NULL;
  71. if(dc.font.set)
  72. XFreeFontSet(dpy, dc.font.set);
  73. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  74. if(missing) {
  75. while(n--)
  76. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  77. XFreeStringList(missing);
  78. }
  79. if(dc.font.set) {
  80. XFontSetExtents *font_extents;
  81. XFontStruct **xfonts;
  82. char **font_names;
  83. dc.font.ascent = dc.font.descent = 0;
  84. font_extents = XExtentsOfFontSet(dc.font.set);
  85. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  86. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  87. if(dc.font.ascent < (*xfonts)->ascent)
  88. dc.font.ascent = (*xfonts)->ascent;
  89. if(dc.font.descent < (*xfonts)->descent)
  90. dc.font.descent = (*xfonts)->descent;
  91. xfonts++;
  92. }
  93. }
  94. else {
  95. if(dc.font.xfont)
  96. XFreeFont(dpy, dc.font.xfont);
  97. dc.font.xfont = NULL;
  98. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  99. eprint("error, cannot load font: '%s'\n", fontstr);
  100. dc.font.ascent = dc.font.xfont->ascent;
  101. dc.font.descent = dc.font.xfont->descent;
  102. }
  103. dc.font.height = dc.font.ascent + dc.font.descent;
  104. }
  105. static void
  106. scan(void) {
  107. unsigned int i, num;
  108. Window *wins, d1, d2;
  109. XWindowAttributes wa;
  110. wins = NULL;
  111. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  112. for(i = 0; i < num; i++) {
  113. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  114. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  115. continue;
  116. if(wa.map_state == IsViewable)
  117. manage(wins[i], &wa);
  118. }
  119. }
  120. if(wins)
  121. XFree(wins);
  122. }
  123. static void
  124. setup(void) {
  125. int i, j;
  126. unsigned int mask;
  127. Window w;
  128. XModifierKeymap *modmap;
  129. XSetWindowAttributes wa;
  130. /* init atoms */
  131. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  132. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  133. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  134. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  135. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  136. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  137. PropModeReplace, (unsigned char *) netatom, NetLast);
  138. /* init cursors */
  139. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  140. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  141. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  142. /* init modifier map */
  143. numlockmask = 0;
  144. modmap = XGetModifierMapping(dpy);
  145. for (i = 0; i < 8; i++)
  146. for (j = 0; j < modmap->max_keypermod; j++) {
  147. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  148. == XKeysymToKeycode(dpy, XK_Num_Lock))
  149. numlockmask = (1 << i);
  150. }
  151. XFreeModifiermap(modmap);
  152. /* select for events */
  153. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  154. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  155. wa.cursor = cursor[CurNormal];
  156. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  157. XSelectInput(dpy, root, wa.event_mask);
  158. grabkeys();
  159. compileregs();
  160. for(ntags = 0; tags[ntags]; ntags++);
  161. seltag = emallocz(sizeof(Bool) * ntags);
  162. seltag[0] = True;
  163. /* style */
  164. dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
  165. dc.norm[ColBG] = initcolor(NORMBGCOLOR);
  166. dc.norm[ColFG] = initcolor(NORMFGCOLOR);
  167. dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
  168. dc.sel[ColBG] = initcolor(SELBGCOLOR);
  169. dc.sel[ColFG] = initcolor(SELFGCOLOR);
  170. initfont(FONT);
  171. /* geometry */
  172. sx = sy = 0;
  173. sw = DisplayWidth(dpy, screen);
  174. sh = DisplayHeight(dpy, screen);
  175. initlayouts();
  176. /* bar */
  177. dc.h = bh = dc.font.height + 2;
  178. wa.override_redirect = 1;
  179. wa.background_pixmap = ParentRelative;
  180. wa.event_mask = ButtonPressMask | ExposureMask;
  181. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  182. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  183. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  184. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  185. bpos = BARPOS;
  186. updatebarpos();
  187. XMapRaised(dpy, barwin);
  188. strcpy(stext, "dwm-"VERSION);
  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. void
  213. updatebarpos(void) {
  214. XEvent ev;
  215. wax = sx;
  216. way = sy;
  217. wah = sh;
  218. waw = sw;
  219. switch(bpos) {
  220. default:
  221. wah -= bh;
  222. way += bh;
  223. XMoveWindow(dpy, barwin, sx, sy);
  224. break;
  225. case BarBot:
  226. wah -= bh;
  227. XMoveWindow(dpy, barwin, sx, sy + wah);
  228. break;
  229. case BarOff:
  230. XMoveWindow(dpy, barwin, sx, sy - bh);
  231. break;
  232. }
  233. XSync(dpy, False);
  234. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  235. }
  236. /* There's no way to check accesses to destroyed windows, thus those cases are
  237. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  238. * default error handler, which may call exit.
  239. */
  240. int
  241. xerror(Display *dpy, XErrorEvent *ee) {
  242. if(ee->error_code == BadWindow
  243. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  244. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  245. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  246. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  247. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  248. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  249. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  250. return 0;
  251. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  252. ee->request_code, ee->error_code);
  253. return xerrorxlib(dpy, ee); /* may call exit */
  254. }
  255. int
  256. main(int argc, char *argv[]) {
  257. char *p;
  258. int r, xfd;
  259. fd_set rd;
  260. XEvent ev;
  261. if(argc == 2 && !strcmp("-v", argv[1]))
  262. eprint("dwm-"VERSION", © 2004-2007 Anselm R. Garbe, Sander van Dijk\n");
  263. else if(argc != 1)
  264. eprint("usage: dwm [-v]\n");
  265. setlocale(LC_CTYPE, "");
  266. if(!(dpy = XOpenDisplay(0)))
  267. eprint("dwm: cannot open display\n");
  268. xfd = ConnectionNumber(dpy);
  269. screen = DefaultScreen(dpy);
  270. root = RootWindow(dpy, screen);
  271. otherwm = False;
  272. XSetErrorHandler(xerrorstart);
  273. /* this causes an error if some other window manager is running */
  274. XSelectInput(dpy, root, SubstructureRedirectMask);
  275. XSync(dpy, False);
  276. if(otherwm)
  277. eprint("dwm: another window manager is already running\n");
  278. XSync(dpy, False);
  279. XSetErrorHandler(NULL);
  280. xerrorxlib = XSetErrorHandler(xerror);
  281. XSync(dpy, False);
  282. setup();
  283. drawstatus();
  284. scan();
  285. /* main event loop, also reads status text from stdin */
  286. XSync(dpy, False);
  287. readin = True;
  288. while(running) {
  289. FD_ZERO(&rd);
  290. if(readin)
  291. FD_SET(STDIN_FILENO, &rd);
  292. FD_SET(xfd, &rd);
  293. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  294. if(errno == EINTR)
  295. continue;
  296. eprint("select failed\n");
  297. }
  298. if(FD_ISSET(STDIN_FILENO, &rd)) {
  299. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  300. case -1:
  301. strncpy(stext, strerror(errno), sizeof stext - 1);
  302. stext[sizeof stext - 1] = '\0';
  303. readin = False;
  304. break;
  305. case 0:
  306. strncpy(stext, "EOF", 4);
  307. readin = False;
  308. break;
  309. default:
  310. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  311. for(; p >= stext && *p != '\n'; --p);
  312. if(p > stext)
  313. strncpy(stext, p + 1, sizeof stext);
  314. }
  315. drawstatus();
  316. }
  317. while(XPending(dpy)) {
  318. XNextEvent(dpy, &ev);
  319. if(handler[ev.type])
  320. (handler[ev.type])(&ev); /* call handler */
  321. }
  322. }
  323. cleanup();
  324. XCloseDisplay(dpy);
  325. return 0;
  326. }