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.

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