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.

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