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