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.

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