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.

373 lines
10 KiB

19 years ago
19 years ago
19 years ago
17 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
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 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);
  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 long
  105. getstate(Window w) {
  106. int format, status;
  107. long result = -1;
  108. unsigned char *p = NULL;
  109. unsigned long n, extra;
  110. Atom real;
  111. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  112. &real, &format, &n, &extra, (unsigned char **)&p);
  113. if(status != Success)
  114. return -1;
  115. if(n != 0)
  116. result = *p;
  117. XFree(p);
  118. return result;
  119. }
  120. static void
  121. scan(void) {
  122. unsigned int i, num;
  123. Window *wins, d1, d2;
  124. XWindowAttributes wa;
  125. wins = NULL;
  126. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  127. for(i = 0; i < num; i++) {
  128. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  129. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  130. continue;
  131. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  132. manage(wins[i], &wa);
  133. }
  134. for(i = 0; i < num; i++) { /* now the transients */
  135. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  136. continue;
  137. if(XGetTransientForHint(dpy, wins[i], &d1)
  138. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  139. manage(wins[i], &wa);
  140. }
  141. }
  142. if(wins)
  143. XFree(wins);
  144. }
  145. static void
  146. setup(void) {
  147. int i, j;
  148. unsigned int mask;
  149. Window w;
  150. XModifierKeymap *modmap;
  151. XSetWindowAttributes wa;
  152. /* init atoms */
  153. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  154. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  155. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  156. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  157. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  158. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  159. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  160. PropModeReplace, (unsigned char *) netatom, NetLast);
  161. /* init cursors */
  162. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  163. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  164. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  165. /* init modifier map */
  166. modmap = XGetModifierMapping(dpy);
  167. for (i = 0; i < 8; i++)
  168. for (j = 0; j < modmap->max_keypermod; j++) {
  169. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  170. == XKeysymToKeycode(dpy, XK_Num_Lock))
  171. numlockmask = (1 << i);
  172. }
  173. XFreeModifiermap(modmap);
  174. /* select for events */
  175. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  176. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  177. wa.cursor = cursor[CurNormal];
  178. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  179. XSelectInput(dpy, root, wa.event_mask);
  180. grabkeys();
  181. compileregs();
  182. for(ntags = 0; tags[ntags]; ntags++);
  183. seltags = emallocz(sizeof(Bool) * ntags);
  184. seltags[0] = True;
  185. /* style */
  186. dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
  187. dc.norm[ColBG] = initcolor(NORMBGCOLOR);
  188. dc.norm[ColFG] = initcolor(NORMFGCOLOR);
  189. dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
  190. dc.sel[ColBG] = initcolor(SELBGCOLOR);
  191. dc.sel[ColFG] = initcolor(SELFGCOLOR);
  192. initfont(FONT);
  193. /* geometry */
  194. sx = sy = 0;
  195. sw = DisplayWidth(dpy, screen);
  196. sh = DisplayHeight(dpy, screen);
  197. initlayouts();
  198. /* bar */
  199. dc.h = bh = dc.font.height + 2;
  200. wa.override_redirect = 1;
  201. wa.background_pixmap = ParentRelative;
  202. wa.event_mask = ButtonPressMask | ExposureMask;
  203. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  204. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  205. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  206. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  207. updatebarpos();
  208. XMapRaised(dpy, barwin);
  209. strcpy(stext, "dwm-"VERSION);
  210. /* pixmap for everything */
  211. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  212. dc.gc = XCreateGC(dpy, root, 0, 0);
  213. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  214. if(!dc.font.set)
  215. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  216. /* multihead support */
  217. selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  218. }
  219. /*
  220. * Startup Error handler to check if another window manager
  221. * is already running.
  222. */
  223. static int
  224. xerrorstart(Display *dsply, XErrorEvent *ee) {
  225. otherwm = True;
  226. return -1;
  227. }
  228. /* extern */
  229. Bool
  230. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  231. char **list = NULL;
  232. int n;
  233. XTextProperty name;
  234. if(!text || size == 0)
  235. return False;
  236. text[0] = '\0';
  237. XGetTextProperty(dpy, w, &name, atom);
  238. if(!name.nitems)
  239. return False;
  240. if(name.encoding == XA_STRING)
  241. strncpy(text, (char *)name.value, size - 1);
  242. else {
  243. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  244. && n > 0 && *list)
  245. {
  246. strncpy(text, *list, size - 1);
  247. XFreeStringList(list);
  248. }
  249. }
  250. text[size - 1] = '\0';
  251. XFree(name.value);
  252. return True;
  253. }
  254. void
  255. quit(const char *arg) {
  256. readin = running = False;
  257. }
  258. /* There's no way to check accesses to destroyed windows, thus those cases are
  259. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  260. * default error handler, which may call exit.
  261. */
  262. int
  263. xerror(Display *dpy, XErrorEvent *ee) {
  264. if(ee->error_code == BadWindow
  265. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  266. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  267. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  268. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  269. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  270. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  271. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  272. return 0;
  273. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  274. ee->request_code, ee->error_code);
  275. return xerrorxlib(dpy, ee); /* may call exit */
  276. }
  277. int
  278. main(int argc, char *argv[]) {
  279. char *p;
  280. int r, xfd;
  281. fd_set rd;
  282. XEvent ev;
  283. if(argc == 2 && !strcmp("-v", argv[1]))
  284. eprint("dwm-"VERSION", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
  285. else if(argc != 1)
  286. eprint("usage: dwm [-v]\n");
  287. setlocale(LC_CTYPE, "");
  288. if(!(dpy = XOpenDisplay(0)))
  289. eprint("dwm: cannot open display\n");
  290. xfd = ConnectionNumber(dpy);
  291. screen = DefaultScreen(dpy);
  292. root = RootWindow(dpy, screen);
  293. otherwm = False;
  294. XSetErrorHandler(xerrorstart);
  295. /* this causes an error if some other window manager is running */
  296. XSelectInput(dpy, root, SubstructureRedirectMask);
  297. XSync(dpy, False);
  298. if(otherwm)
  299. eprint("dwm: another window manager is already running\n");
  300. XSync(dpy, False);
  301. XSetErrorHandler(NULL);
  302. xerrorxlib = XSetErrorHandler(xerror);
  303. XSync(dpy, False);
  304. setup();
  305. drawstatus();
  306. scan();
  307. /* main event loop, also reads status text from stdin */
  308. XSync(dpy, False);
  309. readin = True;
  310. while(running) {
  311. FD_ZERO(&rd);
  312. if(readin)
  313. FD_SET(STDIN_FILENO, &rd);
  314. FD_SET(xfd, &rd);
  315. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  316. if(errno == EINTR)
  317. continue;
  318. eprint("select failed\n");
  319. }
  320. if(FD_ISSET(STDIN_FILENO, &rd)) {
  321. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  322. case -1:
  323. strncpy(stext, strerror(errno), sizeof stext - 1);
  324. stext[sizeof stext - 1] = '\0';
  325. readin = False;
  326. break;
  327. case 0:
  328. strncpy(stext, "EOF", 4);
  329. readin = False;
  330. break;
  331. default:
  332. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  333. for(; p >= stext && *p != '\n'; --p);
  334. if(p > stext)
  335. strncpy(stext, p + 1, sizeof stext);
  336. }
  337. drawstatus();
  338. }
  339. while(XPending(dpy)) {
  340. XNextEvent(dpy, &ev);
  341. if(handler[ev.type])
  342. (handler[ev.type])(&ev); /* call handler */
  343. }
  344. }
  345. cleanup();
  346. XCloseDisplay(dpy);
  347. return 0;
  348. }