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.

304 lines
7.4 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
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <errno.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, mw, screen, sx, sy, sw, sh;
  20. unsigned int ntags, numlockmask;
  21. Atom wmatom[WMLast], netatom[NetLast];
  22. Bool running = True;
  23. Bool issel = True;
  24. Bool maximized = False;
  25. Client *clients = NULL;
  26. Client *sel = NULL;
  27. Client *stack = NULL;
  28. Cursor cursor[CurLast];
  29. Display *dpy;
  30. DC dc = {0};
  31. Window root, barwin;
  32. /* static */
  33. static int (*xerrorxlib)(Display *, XErrorEvent *);
  34. static Bool otherwm, readin;
  35. static void
  36. cleanup()
  37. {
  38. close(STDIN_FILENO);
  39. while(sel) {
  40. resize(sel, True, TopLeft);
  41. unmanage(sel);
  42. }
  43. if(dc.font.set)
  44. XFreeFontSet(dpy, dc.font.set);
  45. else
  46. XFreeFont(dpy, dc.font.xfont);
  47. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  48. XFreePixmap(dpy, dc.drawable);
  49. XFreeGC(dpy, dc.gc);
  50. XDestroyWindow(dpy, barwin);
  51. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  52. XSync(dpy, False);
  53. free(seltag);
  54. }
  55. static void
  56. scan()
  57. {
  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()
  77. {
  78. int i, j;
  79. unsigned int mask;
  80. Window w;
  81. XModifierKeymap *modmap;
  82. XSetWindowAttributes wa;
  83. /* init atoms */
  84. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  85. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", 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. 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. XFree(modmap);
  102. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
  103. wa.cursor = cursor[CurNormal];
  104. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  105. grabkeys();
  106. initrregs();
  107. for(ntags = 0; tags[ntags]; ntags++);
  108. seltag = emallocz(sizeof(Bool) * ntags);
  109. seltag[0] = True;
  110. /* style */
  111. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  112. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  113. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  114. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  115. dc.status[ColBG] = getcolor(STATUSBGCOLOR);
  116. dc.status[ColFG] = getcolor(STATUSFGCOLOR);
  117. setfont(FONT);
  118. bmw = textw(FLOATSYMBOL) > textw(TILESYMBOL) ? textw(FLOATSYMBOL) : textw(TILESYMBOL);
  119. sx = sy = 0;
  120. sw = DisplayWidth(dpy, screen);
  121. sh = DisplayHeight(dpy, screen);
  122. mw = (sw * MASTERW) / 100;
  123. bx = by = 0;
  124. bw = sw;
  125. dc.h = bh = dc.font.height + 2;
  126. wa.override_redirect = 1;
  127. wa.background_pixmap = ParentRelative;
  128. wa.event_mask = ButtonPressMask | ExposureMask;
  129. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  130. CopyFromParent, DefaultVisual(dpy, screen),
  131. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  132. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  133. XMapRaised(dpy, barwin);
  134. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  135. dc.gc = XCreateGC(dpy, root, 0, 0);
  136. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  137. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  138. strcpy(stext, "dwm-"VERSION);
  139. }
  140. /*
  141. * Startup Error handler to check if another window manager
  142. * is already running.
  143. */
  144. static int
  145. xerrorstart(Display *dsply, XErrorEvent *ee)
  146. {
  147. otherwm = True;
  148. return -1;
  149. }
  150. /* extern */
  151. int
  152. getproto(Window w)
  153. {
  154. int i, format, protos, status;
  155. unsigned long extra, res;
  156. Atom *protocols, real;
  157. protos = 0;
  158. status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
  159. XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
  160. if(status != Success || protocols == 0)
  161. return protos;
  162. for(i = 0; i < res; i++)
  163. if(protocols[i] == wmatom[WMDelete])
  164. protos |= PROTODELWIN;
  165. free(protocols);
  166. return protos;
  167. }
  168. void
  169. sendevent(Window w, Atom a, long value)
  170. {
  171. XEvent e;
  172. e.type = ClientMessage;
  173. e.xclient.window = w;
  174. e.xclient.message_type = a;
  175. e.xclient.format = 32;
  176. e.xclient.data.l[0] = value;
  177. e.xclient.data.l[1] = CurrentTime;
  178. XSendEvent(dpy, w, False, NoEventMask, &e);
  179. XSync(dpy, False);
  180. }
  181. void
  182. quit(Arg *arg)
  183. {
  184. readin = running = False;
  185. }
  186. /*
  187. * There's no way to check accesses to destroyed windows, thus those cases are
  188. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  189. * default error handler, which calls exit().
  190. */
  191. int
  192. xerror(Display *dpy, XErrorEvent *ee)
  193. {
  194. if(ee->error_code == BadWindow
  195. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  196. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  197. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  198. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  199. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  200. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
  201. return 0;
  202. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  203. ee->request_code, ee->error_code);
  204. return xerrorxlib(dpy, ee); /* may call exit() */
  205. }
  206. int
  207. main(int argc, char *argv[])
  208. {
  209. int r, xfd;
  210. fd_set rd;
  211. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  212. fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  213. exit(EXIT_SUCCESS);
  214. }
  215. else if(argc != 1)
  216. eprint("usage: dwm [-v]\n");
  217. dpy = XOpenDisplay(0);
  218. if(!dpy)
  219. eprint("dwm: cannot open display\n");
  220. xfd = ConnectionNumber(dpy);
  221. screen = DefaultScreen(dpy);
  222. root = RootWindow(dpy, screen);
  223. otherwm = False;
  224. XSetErrorHandler(xerrorstart);
  225. /* this causes an error if some other window manager is running */
  226. XSelectInput(dpy, root, SubstructureRedirectMask);
  227. XSync(dpy, False);
  228. if(otherwm)
  229. eprint("dwm: another window manager is already running\n");
  230. XSync(dpy, False);
  231. XSetErrorHandler(NULL);
  232. xerrorxlib = XSetErrorHandler(xerror);
  233. XSync(dpy, False);
  234. setup();
  235. drawstatus();
  236. scan();
  237. /* main event loop, also reads status text from stdin */
  238. XSync(dpy, False);
  239. procevent();
  240. readin = True;
  241. while(running) {
  242. FD_ZERO(&rd);
  243. if(readin)
  244. FD_SET(STDIN_FILENO, &rd);
  245. FD_SET(xfd, &rd);
  246. r = select(xfd + 1, &rd, NULL, NULL, NULL);
  247. if((r == -1) && (errno == EINTR))
  248. continue;
  249. if(r > 0) {
  250. if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
  251. readin = NULL != fgets(stext, sizeof(stext), stdin);
  252. if(readin)
  253. stext[strlen(stext) - 1] = 0;
  254. else
  255. strcpy(stext, "broken pipe");
  256. drawstatus();
  257. }
  258. }
  259. else if(r < 0)
  260. eprint("select failed\n");
  261. procevent();
  262. }
  263. cleanup();
  264. XCloseDisplay(dpy);
  265. return 0;
  266. }