Configuration file for DWM on MacBook Air
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.

376 lines
9.9 KiB

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