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

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
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. /* 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, ntags;
  18. unsigned int bpos = BARPOS;
  19. unsigned int numlockmask = 0;
  20. Atom dwmtags, wmatom[WMLast], netatom[NetLast];
  21. Bool *seltag;
  22. Bool selscreen = 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 Bool running = True;
  34. static void
  35. cleanup(void) {
  36. close(STDIN_FILENO);
  37. while(stack) {
  38. unban(stack);
  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, "dwm: 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. dwmtags = XInternAtom(dpy, "__DWM_TAGS", False);
  130. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  131. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  132. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  133. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  134. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  135. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  136. PropModeReplace, (unsigned char *) netatom, NetLast);
  137. /* init cursors */
  138. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  139. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  140. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  141. /* init modifier map */
  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 | StructureNotifyMask;
  153. wa.cursor = cursor[CurNormal];
  154. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  155. XSelectInput(dpy, root, wa.event_mask);
  156. grabkeys();
  157. compileregs();
  158. for(ntags = 0; tags[ntags]; ntags++);
  159. seltag = emallocz(sizeof(Bool) * ntags);
  160. seltag[0] = True;
  161. /* style */
  162. dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
  163. dc.norm[ColBG] = initcolor(NORMBGCOLOR);
  164. dc.norm[ColFG] = initcolor(NORMFGCOLOR);
  165. dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
  166. dc.sel[ColBG] = initcolor(SELBGCOLOR);
  167. dc.sel[ColFG] = initcolor(SELFGCOLOR);
  168. initfont(FONT);
  169. /* geometry */
  170. sx = sy = 0;
  171. sw = DisplayWidth(dpy, screen);
  172. sh = DisplayHeight(dpy, screen);
  173. initlayouts();
  174. /* bar */
  175. dc.h = bh = dc.font.height + 2;
  176. wa.override_redirect = 1;
  177. wa.background_pixmap = ParentRelative;
  178. wa.event_mask = ButtonPressMask | ExposureMask;
  179. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  180. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  181. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  182. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  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. }