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.

286 lines
7.8 KiB

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
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
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
  1. /* (C)opyright MMVI-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[256];
  18. int bh, bmw, screen, sx, sy, sw, sh, wax, way, waw, wah;
  19. unsigned int ntags, numlockmask;
  20. Atom wmatom[WMLast], netatom[NetLast];
  21. Bool running = True;
  22. Bool *seltag;
  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 void
  35. cleanup(void) {
  36. close(STDIN_FILENO);
  37. while(stack) {
  38. if(stack->isbanned)
  39. XMoveWindow(dpy, stack->win, stack->x, stack->y);
  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(seltag);
  56. }
  57. static void
  58. scan(void) {
  59. unsigned int i, num;
  60. Window *wins, d1, d2;
  61. XWindowAttributes wa;
  62. wins = NULL;
  63. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  64. for(i = 0; i < num; i++) {
  65. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  66. || 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. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  86. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  87. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  88. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  89. PropModeReplace, (unsigned char *) netatom, NetLast);
  90. /* init cursors */
  91. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  92. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  93. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  94. /* init modifier map */
  95. numlockmask = 0;
  96. modmap = XGetModifierMapping(dpy);
  97. for (i = 0; i < 8; i++)
  98. for (j = 0; j < modmap->max_keypermod; j++) {
  99. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  100. == XKeysymToKeycode(dpy, XK_Num_Lock))
  101. numlockmask = (1 << i);
  102. }
  103. XFreeModifiermap(modmap);
  104. /* select for events */
  105. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  106. | EnterWindowMask | LeaveWindowMask;
  107. wa.cursor = cursor[CurNormal];
  108. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  109. grabkeys();
  110. compileregexps();
  111. for(ntags = 0; tags[ntags]; ntags++);
  112. seltag = emallocz(sizeof(Bool) * ntags);
  113. seltag[0] = True;
  114. /* style */
  115. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  116. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  117. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  118. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  119. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  120. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  121. setfont(FONT);
  122. /* geometry */
  123. sx = sy = 0;
  124. sw = DisplayWidth(dpy, screen);
  125. sh = DisplayHeight(dpy, screen);
  126. bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
  127. /* bar */
  128. dc.h = bh = dc.font.height + 2;
  129. wa.override_redirect = 1;
  130. wa.background_pixmap = ParentRelative;
  131. wa.event_mask = ButtonPressMask | ExposureMask;
  132. barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
  133. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  134. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  135. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  136. XMapRaised(dpy, barwin);
  137. strcpy(stext, "dwm-"VERSION);
  138. /* windowarea */
  139. wax = sx;
  140. way = sy + (TOPBAR ? bh : 0);
  141. wah = sh - bh;
  142. waw = sw;
  143. /* pixmap for everything */
  144. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  145. dc.gc = XCreateGC(dpy, root, 0, 0);
  146. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  147. /* multihead support */
  148. selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  149. }
  150. /*
  151. * Startup Error handler to check if another window manager
  152. * is already running.
  153. */
  154. static int
  155. xerrorstart(Display *dsply, XErrorEvent *ee) {
  156. otherwm = True;
  157. return -1;
  158. }
  159. /* extern */
  160. void
  161. sendevent(Window w, Atom a, long value) {
  162. XEvent e;
  163. e.type = ClientMessage;
  164. e.xclient.window = w;
  165. e.xclient.message_type = a;
  166. e.xclient.format = 32;
  167. e.xclient.data.l[0] = value;
  168. e.xclient.data.l[1] = CurrentTime;
  169. XSendEvent(dpy, w, False, NoEventMask, &e);
  170. XSync(dpy, False);
  171. }
  172. void
  173. quit(Arg *arg) {
  174. readin = running = False;
  175. }
  176. /* There's no way to check accesses to destroyed windows, thus those cases are
  177. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  178. * default error handler, which may call exit.
  179. */
  180. int
  181. xerror(Display *dpy, XErrorEvent *ee) {
  182. if(ee->error_code == BadWindow
  183. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  184. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  185. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  186. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  187. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  188. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  189. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  190. return 0;
  191. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  192. ee->request_code, ee->error_code);
  193. return xerrorxlib(dpy, ee); /* may call exit */
  194. }
  195. int
  196. main(int argc, char *argv[]) {
  197. char *p;
  198. int r, xfd;
  199. fd_set rd;
  200. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  201. fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
  202. exit(EXIT_SUCCESS);
  203. }
  204. else if(argc != 1)
  205. eprint("usage: dwm [-v]\n");
  206. setlocale(LC_CTYPE, "");
  207. dpy = XOpenDisplay(0);
  208. if(!dpy)
  209. eprint("dwm: cannot open display\n");
  210. xfd = ConnectionNumber(dpy);
  211. screen = DefaultScreen(dpy);
  212. root = RootWindow(dpy, screen);
  213. otherwm = False;
  214. XSetErrorHandler(xerrorstart);
  215. /* this causes an error if some other window manager is running */
  216. XSelectInput(dpy, root, SubstructureRedirectMask);
  217. XSync(dpy, False);
  218. if(otherwm)
  219. eprint("dwm: another window manager is already running\n");
  220. XSync(dpy, False);
  221. XSetErrorHandler(NULL);
  222. xerrorxlib = XSetErrorHandler(xerror);
  223. XSync(dpy, False);
  224. setup();
  225. drawstatus();
  226. scan();
  227. /* main event loop, also reads status text from stdin */
  228. XSync(dpy, False);
  229. procevent();
  230. readin = True;
  231. while(running) {
  232. FD_ZERO(&rd);
  233. if(readin)
  234. FD_SET(STDIN_FILENO, &rd);
  235. FD_SET(xfd, &rd);
  236. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  237. if(errno == EINTR)
  238. continue;
  239. eprint("select failed\n");
  240. }
  241. if(FD_ISSET(STDIN_FILENO, &rd)) {
  242. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  243. case -1:
  244. strncpy(stext, strerror(errno), sizeof stext - 1);
  245. stext[sizeof stext - 1] = '\0';
  246. readin = False;
  247. break;
  248. case 0:
  249. strncpy(stext, "EOF", 4);
  250. readin = False;
  251. break;
  252. default:
  253. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  254. for(; p >= stext && *p != '\n'; --p);
  255. if(p > stext)
  256. strncpy(stext, p + 1, sizeof stext);
  257. }
  258. drawstatus();
  259. }
  260. if(FD_ISSET(xfd, &rd))
  261. procevent();
  262. }
  263. cleanup();
  264. XCloseDisplay(dpy);
  265. return 0;
  266. }