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.

287 lines
7.8 KiB

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