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.

288 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
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
  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 i, format, protos, status;
  86. unsigned long extra, res;
  87. Atom *protocols, real;
  88. protos = 0;
  89. status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
  90. 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. free(protocols);
  97. return protos;
  98. }
  99. void
  100. sendevent(Window w, Atom a, long value)
  101. {
  102. XEvent e;
  103. e.type = ClientMessage;
  104. e.xclient.window = w;
  105. e.xclient.message_type = a;
  106. e.xclient.format = 32;
  107. e.xclient.data.l[0] = value;
  108. e.xclient.data.l[1] = CurrentTime;
  109. XSendEvent(dpy, w, False, NoEventMask, &e);
  110. XSync(dpy, False);
  111. }
  112. void
  113. quit(Arg *arg)
  114. {
  115. readin = running = False;
  116. }
  117. /*
  118. * There's no way to check accesses to destroyed windows, thus those cases are
  119. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  120. * default error handler, which calls exit().
  121. */
  122. int
  123. xerror(Display *dpy, XErrorEvent *ee)
  124. {
  125. if(ee->error_code == BadWindow
  126. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  127. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  128. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  129. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  130. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  131. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
  132. return 0;
  133. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  134. ee->request_code, ee->error_code);
  135. return xerrorxlib(dpy, ee); /* may call exit() */
  136. }
  137. int
  138. main(int argc, char *argv[])
  139. {
  140. int i, j, xfd;
  141. unsigned int mask;
  142. fd_set rd;
  143. Window w;
  144. XModifierKeymap *modmap;
  145. XSetWindowAttributes wa;
  146. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  147. fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  148. exit(EXIT_SUCCESS);
  149. }
  150. else if(argc != 1)
  151. eprint("usage: dwm [-v]\n");
  152. dpy = XOpenDisplay(0);
  153. if(!dpy)
  154. eprint("dwm: cannot open display\n");
  155. xfd = ConnectionNumber(dpy);
  156. screen = DefaultScreen(dpy);
  157. root = RootWindow(dpy, screen);
  158. otherwm = False;
  159. XSetErrorHandler(xerrorstart);
  160. /* this causes an error if some other window manager is running */
  161. XSelectInput(dpy, root, SubstructureRedirectMask);
  162. XSync(dpy, False);
  163. if(otherwm)
  164. eprint("dwm: another window manager is already running\n");
  165. XSync(dpy, False);
  166. XSetErrorHandler(NULL);
  167. xerrorxlib = XSetErrorHandler(xerror);
  168. XSync(dpy, False);
  169. /* init atoms */
  170. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  171. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  172. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  173. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  174. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  175. PropModeReplace, (unsigned char *) netatom, NetLast);
  176. /* init cursors */
  177. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  178. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  179. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  180. modmap = XGetModifierMapping(dpy);
  181. for (i = 0; i < 8; i++) {
  182. for (j = 0; j < modmap->max_keypermod; j++) {
  183. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  184. numlockmask = (1 << i);
  185. }
  186. }
  187. XFree(modmap);
  188. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
  189. wa.cursor = cursor[CurNormal];
  190. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  191. grabkeys();
  192. initrregs();
  193. for(ntags = 0; tags[ntags]; ntags++);
  194. seltag = emallocz(sizeof(Bool) * ntags);
  195. seltag[0] = True;
  196. /* style */
  197. dc.bg = getcolor(BGCOLOR);
  198. dc.fg = getcolor(FGCOLOR);
  199. dc.border = getcolor(BORDERCOLOR);
  200. setfont(FONT);
  201. sx = sy = 0;
  202. sw = DisplayWidth(dpy, screen);
  203. sh = DisplayHeight(dpy, screen);
  204. mw = (sw * MASTERW) / 100;
  205. bx = by = 0;
  206. bw = sw;
  207. dc.h = bh = dc.font.height + 4;
  208. wa.override_redirect = 1;
  209. wa.background_pixmap = ParentRelative;
  210. wa.event_mask = ButtonPressMask | ExposureMask;
  211. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  212. CopyFromParent, DefaultVisual(dpy, screen),
  213. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  214. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  215. XMapRaised(dpy, barwin);
  216. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  217. dc.gc = XCreateGC(dpy, root, 0, 0);
  218. strcpy(stext, "dwm-"VERSION);
  219. drawstatus();
  220. issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  221. scan();
  222. /* main event loop, also reads status text from stdin */
  223. XSync(dpy, False);
  224. procevent();
  225. readin = True;
  226. while(running) {
  227. FD_ZERO(&rd);
  228. if(readin)
  229. FD_SET(STDIN_FILENO, &rd);
  230. FD_SET(xfd, &rd);
  231. i = select(xfd + 1, &rd, NULL, NULL, NULL);
  232. if((i == -1) && (errno == EINTR))
  233. continue;
  234. if(i > 0) {
  235. if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
  236. readin = NULL != fgets(stext, sizeof(stext), stdin);
  237. if(readin)
  238. stext[strlen(stext) - 1] = 0;
  239. else
  240. strcpy(stext, "broken pipe");
  241. drawstatus();
  242. }
  243. }
  244. else if(i < 0)
  245. eprint("select failed\n");
  246. procevent();
  247. }
  248. cleanup();
  249. XCloseDisplay(dpy);
  250. return 0;
  251. }