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.

306 lines
8.1 KiB

19 years ago
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
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. /* (C)opyright MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. #include <errno.h>
  6. #include <locale.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/select.h>
  12. #include <X11/cursorfont.h>
  13. #include <X11/keysym.h>
  14. #include <X11/Xatom.h>
  15. #include <X11/Xproto.h>
  16. /* extern */
  17. char stext[1024];
  18. Bool *seltag;
  19. int bx, by, bw, bh, bmw, masterd, screen, sx, sy, sw, sh, wax, way, waw, wah;
  20. unsigned int master, ntags, numlockmask;
  21. Atom wmatom[WMLast], netatom[NetLast];
  22. Bool running = True;
  23. Bool issel = 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 void
  35. cleanup(void) {
  36. close(STDIN_FILENO);
  37. while(clients) {
  38. resize(clients, True, TopLeft);
  39. unmanage(clients);
  40. }
  41. if(dc.font.set)
  42. XFreeFontSet(dpy, dc.font.set);
  43. else
  44. XFreeFont(dpy, dc.font.xfont);
  45. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  46. XFreePixmap(dpy, dc.drawable);
  47. XFreeGC(dpy, dc.gc);
  48. XDestroyWindow(dpy, barwin);
  49. XFreeCursor(dpy, cursor[CurNormal]);
  50. XFreeCursor(dpy, cursor[CurResize]);
  51. XFreeCursor(dpy, cursor[CurMove]);
  52. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  53. XSync(dpy, False);
  54. free(seltag);
  55. }
  56. static void
  57. scan(void) {
  58. unsigned int i, num;
  59. Window *wins, d1, d2;
  60. XWindowAttributes wa;
  61. wins = NULL;
  62. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  63. for(i = 0; i < num; i++) {
  64. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  65. continue;
  66. if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  67. continue;
  68. if(wa.map_state == IsViewable)
  69. manage(wins[i], &wa);
  70. }
  71. }
  72. if(wins)
  73. XFree(wins);
  74. }
  75. static void
  76. setup(void) {
  77. int i, j;
  78. unsigned int mask;
  79. Window w;
  80. XModifierKeymap *modmap;
  81. XSetWindowAttributes wa;
  82. /* init atoms */
  83. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  84. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  85. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  86. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  87. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  88. PropModeReplace, (unsigned char *) netatom, NetLast);
  89. /* init cursors */
  90. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  91. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  92. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  93. /* init modifier map */
  94. modmap = XGetModifierMapping(dpy);
  95. for (i = 0; i < 8; i++) {
  96. for (j = 0; j < modmap->max_keypermod; j++) {
  97. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  98. numlockmask = (1 << i);
  99. }
  100. }
  101. XFreeModifiermap(modmap);
  102. /* select for events */
  103. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  104. | EnterWindowMask | LeaveWindowMask;
  105. wa.cursor = cursor[CurNormal];
  106. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  107. grabkeys();
  108. initrregs();
  109. for(ntags = 0; tags[ntags]; ntags++);
  110. seltag = emallocz(sizeof(Bool) * ntags);
  111. seltag[0] = True;
  112. /* style */
  113. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  114. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  115. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  116. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  117. dc.status[ColBG] = getcolor(STATUSBGCOLOR);
  118. dc.status[ColFG] = getcolor(STATUSFGCOLOR);
  119. setfont(FONT);
  120. /* geometry */
  121. bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
  122. sx = sy = 0;
  123. sw = DisplayWidth(dpy, screen);
  124. sh = DisplayHeight(dpy, screen);
  125. master = MASTER;
  126. /* bar */
  127. bx = sx;
  128. by = sy;
  129. bw = sw;
  130. dc.h = bh = dc.font.height + 2;
  131. wa.override_redirect = 1;
  132. wa.background_pixmap = ParentRelative;
  133. wa.event_mask = ButtonPressMask | ExposureMask;
  134. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  135. CopyFromParent, DefaultVisual(dpy, screen),
  136. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  137. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  138. XMapRaised(dpy, barwin);
  139. strcpy(stext, "dwm-"VERSION);
  140. /* windowarea */
  141. wax = sx;
  142. way = sy + bh;
  143. wah = sh - bh;
  144. waw = sw;
  145. /* pixmap for everything */
  146. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  147. dc.gc = XCreateGC(dpy, root, 0, 0);
  148. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  149. /* multihead support */
  150. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  151. }
  152. /*
  153. * Startup Error handler to check if another window manager
  154. * is already running.
  155. */
  156. static int
  157. xerrorstart(Display *dsply, XErrorEvent *ee) {
  158. otherwm = True;
  159. return -1;
  160. }
  161. /* extern */
  162. int
  163. getproto(Window w) {
  164. int i, format, protos, status;
  165. unsigned long extra, res;
  166. Atom *protocols, real;
  167. protos = 0;
  168. status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
  169. XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
  170. if(status != Success || protocols == 0)
  171. return protos;
  172. for(i = 0; i < res; i++)
  173. if(protocols[i] == wmatom[WMDelete])
  174. protos |= PROTODELWIN;
  175. free(protocols);
  176. return protos;
  177. }
  178. void
  179. sendevent(Window w, Atom a, long value) {
  180. XEvent e;
  181. e.type = ClientMessage;
  182. e.xclient.window = w;
  183. e.xclient.message_type = a;
  184. e.xclient.format = 32;
  185. e.xclient.data.l[0] = value;
  186. e.xclient.data.l[1] = CurrentTime;
  187. XSendEvent(dpy, w, False, NoEventMask, &e);
  188. XSync(dpy, False);
  189. }
  190. void
  191. quit(Arg *arg) {
  192. readin = running = False;
  193. }
  194. /* There's no way to check accesses to destroyed windows, thus those cases are
  195. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  196. * default error handler, which may call exit.
  197. */
  198. int
  199. xerror(Display *dpy, XErrorEvent *ee) {
  200. if(ee->error_code == BadWindow
  201. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  202. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  203. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  204. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  205. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  206. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  207. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  208. return 0;
  209. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  210. ee->request_code, ee->error_code);
  211. return xerrorxlib(dpy, ee); /* may call exit */
  212. }
  213. int
  214. main(int argc, char *argv[]) {
  215. char *p;
  216. int r, xfd;
  217. fd_set rd;
  218. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  219. fputs("dwm-"VERSION", (C)opyright MMVII Anselm R. Garbe\n", stdout);
  220. exit(EXIT_SUCCESS);
  221. }
  222. else if(argc != 1)
  223. eprint("usage: dwm [-v]\n");
  224. setlocale(LC_CTYPE, "");
  225. dpy = XOpenDisplay(0);
  226. if(!dpy)
  227. eprint("dwm: cannot open display\n");
  228. xfd = ConnectionNumber(dpy);
  229. screen = DefaultScreen(dpy);
  230. root = RootWindow(dpy, screen);
  231. otherwm = False;
  232. XSetErrorHandler(xerrorstart);
  233. /* this causes an error if some other window manager is running */
  234. XSelectInput(dpy, root, SubstructureRedirectMask);
  235. XSync(dpy, False);
  236. if(otherwm)
  237. eprint("dwm: another window manager is already running\n");
  238. XSync(dpy, False);
  239. XSetErrorHandler(NULL);
  240. xerrorxlib = XSetErrorHandler(xerror);
  241. XSync(dpy, False);
  242. setup();
  243. drawstatus();
  244. scan();
  245. /* main event loop, also reads status text from stdin */
  246. XSync(dpy, False);
  247. procevent();
  248. readin = True;
  249. while(running) {
  250. FD_ZERO(&rd);
  251. if(readin)
  252. FD_SET(STDIN_FILENO, &rd);
  253. FD_SET(xfd, &rd);
  254. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  255. if(errno == EINTR)
  256. continue;
  257. eprint("select failed\n");
  258. }
  259. if(FD_ISSET(STDIN_FILENO, &rd)) {
  260. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  261. case -1:
  262. strncpy(stext, strerror(errno), sizeof stext - 1);
  263. stext[sizeof stext - 1] = '\0';
  264. readin = False;
  265. break;
  266. case 0:
  267. strncpy(stext, "EOF", 4);
  268. readin = False;
  269. break;
  270. default:
  271. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  272. for(p = stext + strlen(stext) - 1; p >= stext && *p != '\n'; --p);
  273. if(p > stext)
  274. strncpy(stext, p + 1, sizeof stext);
  275. }
  276. drawstatus();
  277. }
  278. if(FD_ISSET(xfd, &rd))
  279. procevent();
  280. }
  281. cleanup();
  282. XCloseDisplay(dpy);
  283. return 0;
  284. }