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.

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