--- rxvt-unicode/src/rxvttoolkit.C 2005/08/05 16:42:44 1.17 +++ rxvt-unicode/src/rxvttoolkit.C 2006/01/31 16:19:16 1.40 @@ -27,11 +27,75 @@ #include #include +#include + #ifndef NO_SLOW_LINK_SUPPORT # include # include #endif +#if XFT +# include +#endif + +const char *const xa_names[] = +{ + "TEXT", + "COMPOUND_TEXT", + "UTF8_STRING", + "MULTIPLE", + "TARGETS", + "TIMESTAMP", + "VT_SELECTION", + "INCR", + "WM_PROTOCOLS", + "WM_DELETE_WINDOW", + "CLIPBOARD", + "AVERAGE_WIDTH", + "WEIGHT_NAME", + "SLANT", + "CHARSET_REGISTRY", + "CHARSET_ENCODING", +#if ENABLE_FRILLS + "_MOTIF_WM_HINTS", +#endif +#if ENABLE_EWMH + "_NET_WM_PID", + "_NET_WM_NAME", + "_NET_WM_ICON_NAME", + "_NET_WM_PING", +#endif +#if USE_XIM + "WM_LOCALE_NAME", + "XIM_SERVERS", +#endif +#ifdef TRANSPARENT + "_XROOTPMAP_ID", + "ESETROOT_PMAP_ID", +#endif +#if ENABLE_XEMBED + "_XEMBED", + "_XEMBED_INFO", +#endif +#if !ENABLE_MINIMAL + "SCREEN_RESOURCES", + "XDCCC_LINEAR_RGB_CORRECTION", + "XDCCC_LINEAR_RGB_MATRICES", + "WM_COLORMAP_WINDOWS", + "WM_STATE", + "cursor", +# if USE_XIM + "TRANSPORT", + "LOCALES", + "_XIM_PROTOCOL", + "_XIM_XCONNECT", + "_XIM_MOREDATA", +# endif +#endif +}; + +///////////////////////////////////////////////////////////////////////////// + refcounted::refcounted (const char *id) { this->id = strdup (id); @@ -49,17 +113,17 @@ { if (!strcmp (id, (*i)->id)) { - (*i)->referenced++; + ++(*i)->referenced; + (*i)->ref_next (); return *i; } } T *obj = new T (id); - obj->referenced = 1; - - if (obj && obj->init ()) + if (obj && obj->ref_init ()) { + obj->referenced = 1; this->push_back (obj); return obj; } @@ -93,6 +157,7 @@ ///////////////////////////////////////////////////////////////////////////// #ifdef USE_XIM + static void #if XIMCB_PROTO_BROKEN im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) @@ -109,7 +174,8 @@ display->im_change_cb (); } -bool rxvt_xim::init () +bool +rxvt_xim::ref_init () { display = GET_R->display; //HACK: TODO @@ -132,7 +198,47 @@ if (xim) XCloseIM (xim); } + +#endif + +///////////////////////////////////////////////////////////////////////////// + +void +rxvt_screen::set (rxvt_display *disp) +{ + display = disp; + xdisp = disp->display; + + Screen *screen = ScreenOfDisplay (xdisp, disp->screen); + + depth = DefaultDepthOfScreen (screen); + visual = DefaultVisualOfScreen (screen); + cmap = DefaultColormapOfScreen (screen); +} + +void +rxvt_screen::set (rxvt_display *disp, int bitdepth) +{ + set (disp); + +#if XFT + XVisualInfo vinfo; + + if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo)) + { + depth = bitdepth; + visual = vinfo.visual; + cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); + } #endif +} + +void +rxvt_screen::clear () +{ + if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen))) + XFreeColormap (xdisp, cmap); +} ///////////////////////////////////////////////////////////////////////////// @@ -143,7 +249,87 @@ { } -bool rxvt_display::init () +XrmDatabase +rxvt_display::get_resources () +{ + char *homedir = (char *)getenv ("HOME"); + char fname[1024]; + + /* + * get resources using the X library function + */ + char *displayResource, *xe; + XrmDatabase database, rdb1; + + database = NULL; + + // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 + + // 6. System wide per application default file. + + /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ + if ((xe = (char *)getenv ("XAPPLRESDIR"))) + { + snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); + + if ((rdb1 = XrmGetFileDatabase (fname))) + XrmMergeDatabases (rdb1, &database); + } + + // 5. User's per application default file. + // none + + // 4. User's defaults file. + /* Get any Xserver defaults */ + displayResource = XResourceManagerString (display); + + if (displayResource != NULL) + { + if ((rdb1 = XrmGetStringDatabase (displayResource))) + XrmMergeDatabases (rdb1, &database); + } + else if (homedir) + { + snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir); + + if ((rdb1 = XrmGetFileDatabase (fname))) + XrmMergeDatabases (rdb1, &database); + } + + /* Get screen specific resources */ + displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); + + if (displayResource != NULL) + { + if ((rdb1 = XrmGetStringDatabase (displayResource))) + /* Merge with screen-independent resources */ + XrmMergeDatabases (rdb1, &database); + + XFree (displayResource); + } + + // 3. User's per host defaults file + /* Add in XENVIRONMENT file */ + if ((xe = (char *)getenv ("XENVIRONMENT")) + && (rdb1 = XrmGetFileDatabase (xe))) + XrmMergeDatabases (rdb1, &database); + else if (homedir) + { + struct utsname un; + + if (!uname (&un)) + { + snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename); + + if ((rdb1 = XrmGetFileDatabase (fname))) + XrmMergeDatabases (rdb1, &database); + } + } + + return database; +} + +bool rxvt_display::ref_init () { #ifdef LOCAL_X_IS_UNIX if (id[0] == ':') @@ -166,23 +352,11 @@ screen = DefaultScreen (display); root = DefaultRootWindow (display); - visual = DefaultVisual (display, screen); - cmap = DefaultColormap (display, screen); - depth = DefaultDepth (display, screen); - - int fd = XConnectionNumber (display); -#ifndef NO_SLOW_LINK_SUPPORT - // try to detect wether we have a local connection. - // assume unix domains socket == local, everything else not - // TODO: might want to check for inet/127.0.0.1 - is_local = 0; - sockaddr_un sa; - socklen_t sl = sizeof (sa); + assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); + XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); - if (!getsockname (fd, (sockaddr *)&sa, &sl)) - is_local = sa.sun_family == AF_LOCAL; -#endif + XrmSetDatabase (display, get_resources ()); #ifdef POINTER_BLANK XColor blackcolour; @@ -195,47 +369,52 @@ XUnloadFont (display, f); #endif -#ifdef PREFER_24BIT - /* - * If depth is not 24, look for a 24bit visual. - */ - if (depth != 24) - { - XVisualInfo vinfo; + int fd = XConnectionNumber (display); - if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo)) - { - depth = 24; - visual = vinfo.visual; - cmap = XCreateColormap (display, - RootWindow (display, screen), - visual, AllocNone); - } - } +#ifndef NO_SLOW_LINK_SUPPORT + // try to detect wether we have a local connection. + // assume unix domains socket == local, everything else not + // TODO: might want to check for inet/127.0.0.1 + is_local = 0; + sockaddr_un sa; + socklen_t sl = sizeof (sa); + + if (!getsockname (fd, (sockaddr *)&sa, &sl)) + is_local = sa.sun_family == AF_LOCAL; #endif x_ev.start (fd, EVENT_READ); fcntl (fd, F_SETFD, FD_CLOEXEC); XSelectInput (display, root, PropertyChangeMask); -#ifdef USE_XIM - xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); -#endif flush (); return true; } +void +rxvt_display::ref_next () +{ + // TODO: somehow check wether the database files/resources changed + // before re-loading/parsing + XrmDestroyDatabase (XrmGetDatabase (display)); + XrmSetDatabase (display, get_resources ()); +} + rxvt_display::~rxvt_display () { + if (!display) + return; + +#ifdef POINTER_BLANK + XFreeCursor (display, blank_cursor); +#endif x_ev.stop (); #ifdef USE_XIM xims.clear (); #endif - - if (display) - XCloseDisplay (display); + XCloseDisplay (display); } #ifdef USE_XIM @@ -253,7 +432,7 @@ int actual_format; unsigned long nitems, bytes_after; - if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, + if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L, False, XA_ATOM, &actual_type, &actual_format, &nitems, &bytes_after, (unsigned char **)&atoms) != Success ) @@ -281,10 +460,9 @@ #ifdef USE_XIM if (!XFilterEvent (&xev, None)) { - if (xev.type == PropertyNotify && xev.xany.window == root - && xev.xproperty.atom == xa_xim_servers) + && xev.xproperty.atom == xa[XA_XIM_SERVERS]) im_change_check (); #endif for (int i = xw.size (); i--; ) @@ -382,108 +560,218 @@ refcache displays; ///////////////////////////////////////////////////////////////////////////// - + bool -rxvt_color::set (rxvt_display *display, Pixel p) +rxvt_color::alloc (rxvt_screen *screen, const rxvt_rgba &color) { #if XFT - XColor xc; + XRenderPictFormat *format; - xc.pixel = p; - if (!XQueryColor (display->display, display->cmap, &xc)) - return false; + // FUCKING Xft gets it wrong, of course, so work around it + // transparency users should eat shit and die, and then + // XRenderQueryPictIndexValues themselves plenty. + if ((screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) + && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual))) + { + // the fun lies in doing everything manually... + c.color.red = color.r; + c.color.green = color.g; + c.color.blue = color.b; + c.color.alpha = color.a; + + c.pixel = ((color.r * format->direct.redMask / rxvt_rgba::MAX_CC) << format->direct.red ) + | ((color.g * format->direct.greenMask / rxvt_rgba::MAX_CC) << format->direct.green) + | ((color.b * format->direct.blueMask / rxvt_rgba::MAX_CC) << format->direct.blue ) + | ((color.a * format->direct.alphaMask / rxvt_rgba::MAX_CC) << format->direct.alpha); + + return true; + } + else + { + XRenderColor d; - XRenderColor d; + d.red = color.r; + d.green = color.g; + d.blue = color.b; + d.alpha = color.a; - d.red = xc.red; - d.green = xc.green; - d.blue = xc.blue; - d.alpha = 0xffff; - - return - XftColorAllocValue (display->display, - display->visual, - display->cmap, - &d, &c); + return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); + } #else - this->p = p; + if (screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) + { + p = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) + / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask ) + | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) + / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask) + | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) + / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask ); + + return true; + } + else + { + XColor xc; + + xc.red = color.r; + xc.green = color.g; + xc.blue = color.b; + + if (XAllocColor (screen->xdisp, screen->cmap, &xc)) + { + p = xc.pixel; + return true; + } + else + p = (color.r + color.g + color.b) > 128*3 + ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) + : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); + } #endif - return true; + return false; } bool -rxvt_color::set (rxvt_display *display, const char *name) +rxvt_color::set (rxvt_screen *screen, const char *name) { -#if XFT - return XftColorAllocName (display->display, display->visual, display->cmap, - name, &c); -#else - XColor xc; + rxvt_rgba c; + char eos; + int skip; - if (XParseColor (display->display, display->cmap, name, &xc)) - return set (display, xc.red, xc.green, xc.blue); + if (1 <= sscanf (name, "[%hx]%n", &c.a, &skip)) + { + switch (skip) + { + case 2 + 1: c.a *= rxvt_rgba::MAX_CC / 0x000f; break; + case 2 + 2: c.a *= rxvt_rgba::MAX_CC / 0x00ff; break; + case 2 + 3: c.a *= rxvt_rgba::MAX_CC / 0x0fff; break; + case 2 + 4: c.a *= rxvt_rgba::MAX_CC / 0xffff; break; + } - return false; -#endif + name += skip; + } + else + c.a = rxvt_rgba::MAX_CC; + + // parse the non-standard rgba format + if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) + { + XColor xc, xc_exact; + + if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) + { + c.r = xc.red; + c.g = xc.green; + c.b = xc.blue; + } + else + { + c.r = 0xffff; + c.g = 0x6969; + c.b = 0xb4b4; + + rxvt_warn ("unable to parse color '%s', using pink instead.\n", name); + } + } + + return set (screen, c); } bool -rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) +rxvt_color::set (rxvt_screen *screen, const rxvt_rgba &color) { - XColor xc; + bool got = alloc (screen, color); - xc.red = cr; - xc.green = cg; - xc.blue = cb; - xc.flags = DoRed | DoGreen | DoBlue; +#if !ENABLE_MINIMAL + int cmap_size = screen->visual->map_entries; - if (XAllocColor (display->display, display->cmap, &xc)) - return set (display, xc.pixel); + if (!got + && screen->visual->c_class == PseudoColor + && cmap_size < 4096) + { + XColor *colors = new XColor [screen->visual->map_entries]; - return false; + for (int i = 0; i < cmap_size; i++) + colors [i].pixel = i; + + XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); + + int diff = 0x7fffffffUL; + XColor *best = colors; + + for (int i = 0; i < cmap_size; i++) + { + int d = (squared_diff (color.r >> 2, colors [i].red >> 2)) + + (squared_diff (color.g >> 2, colors [i].green >> 2)) + + (squared_diff (color.b >> 2, colors [i].blue >> 2)); + + if (d < diff) + { + diff = d; + best = colors + i; + } + } + + //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", + // color.r, color.g, color.b, best->red, best->green, best->blue, diff); + + got = alloc (screen, rxvt_rgba (best->red, best->green, best->blue)); + + delete colors; + } +#endif + + return got; } -void -rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) +void +rxvt_color::get (rxvt_screen *screen, rxvt_rgba &color) { #if XFT - cr = c.color.red; - cg = c.color.green; - cb = c.color.blue; + color.r = c.color.red; + color.g = c.color.green; + color.b = c.color.blue; + color.a = c.color.alpha; #else XColor c; c.pixel = p; - XQueryColor (display->display, display->cmap, &c); + XQueryColor (screen->xdisp, screen->cmap, &c); - cr = c.red; - cg = c.green; - cb = c.blue; + color.r = c.red; + color.g = c.green; + color.b = c.blue; + color.a = rxvt_rgba::MAX_CC; #endif } void -rxvt_color::free (rxvt_display *display) +rxvt_color::free (rxvt_screen *screen) { #if XFT - XftColorFree (display->display, display->visual, display->cmap, &c); + XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); #else - XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); + XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); #endif } rxvt_color -rxvt_color::fade (rxvt_display *display, int percent) +rxvt_color::fade (rxvt_screen *screen, int percent, const rxvt_rgba &to) { - unsigned short cr, cg, cb; - rxvt_color faded; + rxvt_rgba c; + get (screen, c); - get (display, cr, cg, cb); - faded.set (display, - cr * percent / 100, - cg * percent / 100, - cb * percent / 100); + rxvt_color faded; + faded.set ( + screen, + rxvt_rgba ( + lerp (to.r, c.r, percent), + lerp (to.g, c.g, percent), + lerp (to.b, c.b, percent), + lerp (to.a, c.a, percent) + ) + ); return faded; }