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.

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