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