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.

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