main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. static int
  59. win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
  60. {
  61. int status, format;
  62. unsigned long res, extra;
  63. Atom real;
  64. status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
  65. &res, &extra, prop);
  66. if(status != Success || *prop == 0) {
  67. return 0;
  68. }
  69. if(res == 0) {
  70. free((void *) *prop);
  71. }
  72. return res;
  73. }
  74. /*
  75. * Startup Error handler to check if another window manager
  76. * is already running.
  77. */
  78. static int
  79. xerrorstart(Display *dsply, XErrorEvent *ee)
  80. {
  81. otherwm = True;
  82. return -1;
  83. }
  84. /* extern */
  85. char stext[1024];
  86. Bool *seltag;
  87. int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
  88. unsigned int ntags, numlockmask;
  89. Atom wmatom[WMLast], netatom[NetLast];
  90. Bool running = True;
  91. Bool issel = True;
  92. Client *clients = NULL;
  93. Client *sel = NULL;
  94. Cursor cursor[CurLast];
  95. Display *dpy;
  96. DC dc = {0};
  97. Window root, barwin;
  98. int
  99. getproto(Window w)
  100. {
  101. int protos = 0;
  102. int i;
  103. long res;
  104. Atom *protocols;
  105. res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L,
  106. ((unsigned char **)&protocols));
  107. if(res <= 0) {
  108. return protos;
  109. }
  110. for(i = 0; i < res; i++) {
  111. if(protocols[i] == wmatom[WMDelete])
  112. protos |= PROTODELWIN;
  113. }
  114. free((char *) protocols);
  115. return protos;
  116. }
  117. void
  118. sendevent(Window w, Atom a, long value)
  119. {
  120. XEvent e;
  121. e.type = ClientMessage;
  122. e.xclient.window = w;
  123. e.xclient.message_type = a;
  124. e.xclient.format = 32;
  125. e.xclient.data.l[0] = value;
  126. e.xclient.data.l[1] = CurrentTime;
  127. XSendEvent(dpy, w, False, NoEventMask, &e);
  128. XSync(dpy, False);
  129. }
  130. void
  131. quit(Arg *arg)
  132. {
  133. readin = running = False;
  134. }
  135. /*
  136. * There's no way to check accesses to destroyed windows, thus those cases are
  137. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  138. * default error handler, which calls exit().
  139. */
  140. int
  141. xerror(Display *dpy, XErrorEvent *ee)
  142. {
  143. if(ee->error_code == BadWindow
  144. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  145. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  146. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  147. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  148. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  149. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
  150. return 0;
  151. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  152. ee->request_code, ee->error_code);
  153. return xerrorxlib(dpy, ee); /* may call exit() */
  154. }
  155. int
  156. main(int argc, char *argv[])
  157. {
  158. int i, j, xfd;
  159. unsigned int mask;
  160. fd_set rd;
  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. readin = True;
  244. while(running) {
  245. FD_ZERO(&rd);
  246. if(readin)
  247. FD_SET(STDIN_FILENO, &rd);
  248. FD_SET(xfd, &rd);
  249. i = select(xfd + 1, &rd, NULL, NULL, NULL);
  250. if((i == -1) && (errno == EINTR))
  251. continue;
  252. if(i > 0) {
  253. if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
  254. readin = NULL != fgets(stext, sizeof(stext), stdin);
  255. if(readin)
  256. stext[strlen(stext) - 1] = 0;
  257. else
  258. strcpy(stext, "broken pipe");
  259. drawstatus();
  260. }
  261. }
  262. else if(i < 0)
  263. eprint("select failed\n");
  264. procevent();
  265. }
  266. cleanup();
  267. XCloseDisplay(dpy);
  268. return 0;
  269. }