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.

347 lines
9.3 KiB

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