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.

305 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 may call 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. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  202. return 0;
  203. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  204. ee->request_code, ee->error_code);
  205. return xerrorxlib(dpy, ee); /* may call exit */
  206. }
  207. int
  208. main(int argc, char *argv[])
  209. {
  210. int r, xfd;
  211. fd_set rd;
  212. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  213. fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  214. exit(EXIT_SUCCESS);
  215. }
  216. else if(argc != 1)
  217. eprint("usage: dwm [-v]\n");
  218. dpy = XOpenDisplay(0);
  219. if(!dpy)
  220. eprint("dwm: cannot open display\n");
  221. xfd = ConnectionNumber(dpy);
  222. screen = DefaultScreen(dpy);
  223. root = RootWindow(dpy, screen);
  224. otherwm = False;
  225. XSetErrorHandler(xerrorstart);
  226. /* this causes an error if some other window manager is running */
  227. XSelectInput(dpy, root, SubstructureRedirectMask);
  228. XSync(dpy, False);
  229. if(otherwm)
  230. eprint("dwm: another window manager is already running\n");
  231. XSync(dpy, False);
  232. XSetErrorHandler(NULL);
  233. xerrorxlib = XSetErrorHandler(xerror);
  234. XSync(dpy, False);
  235. setup();
  236. drawstatus();
  237. scan();
  238. /* main event loop, also reads status text from stdin */
  239. XSync(dpy, False);
  240. procevent();
  241. readin = True;
  242. while(running) {
  243. FD_ZERO(&rd);
  244. if(readin)
  245. FD_SET(STDIN_FILENO, &rd);
  246. FD_SET(xfd, &rd);
  247. r = select(xfd + 1, &rd, NULL, NULL, NULL);
  248. if((r == -1) && (errno == EINTR))
  249. continue;
  250. if(r > 0) {
  251. if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
  252. readin = NULL != fgets(stext, sizeof(stext), stdin);
  253. if(readin)
  254. stext[strlen(stext) - 1] = 0;
  255. else
  256. strcpy(stext, "broken pipe");
  257. drawstatus();
  258. }
  259. }
  260. else if(r < 0)
  261. eprint("select failed\n");
  262. procevent();
  263. }
  264. cleanup();
  265. XCloseDisplay(dpy);
  266. return 0;
  267. }