main.c 7.2 KB

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