--- rxvt-unicode/src/rxvttoolkit.C 2006/01/25 02:42:06 1.25 +++ rxvt-unicode/src/rxvttoolkit.C 2006/02/01 01:58:47 1.45 @@ -34,41 +34,65 @@ # 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", +{ + "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", + "_MOTIF_WM_HINTS", #endif #if ENABLE_EWMH - "_NET_WM_PID", - "_NET_WM_NAME", - "_NET_WM_ICON_NAME", - "_NET_WM_PING", + "_NET_WM_PID", + "_NET_WM_NAME", + "_NET_WM_ICON_NAME", + "_NET_WM_PING", #endif #if USE_XIM - "WM_LOCALE_NAME", - "XIM_SERVERS", + "WM_LOCALE_NAME", + "XIM_SERVERS", #endif #ifdef TRANSPARENT - "_XROOTPMAP_ID", - "ESETROOT_PMAP_ID", + "_XROOTPMAP_ID", + "ESETROOT_PMAP_ID", #endif #if ENABLE_XEMBED - "_XEMBED", - "_XEMBED_INFO", + "_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 - }; +}; ///////////////////////////////////////////////////////////////////////////// @@ -179,6 +203,45 @@ ///////////////////////////////////////////////////////////////////////////// +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); +} + +///////////////////////////////////////////////////////////////////////////// + rxvt_display::rxvt_display (const char *id) : refcounted (id) , x_ev (this, &rxvt_display::x_cb) @@ -289,9 +352,6 @@ screen = DefaultScreen (display); root = DefaultRootWindow (display); - visual = DefaultVisual (display, screen); - cmap = DefaultColormap (display, screen); - depth = DefaultDepth (display, screen); assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); @@ -309,25 +369,6 @@ XUnloadFont (display, f); #endif -#ifdef PREFER_24BIT - /* - * If depth is not 24, look for a 24bit visual. - */ - if (depth != 24) - { - XVisualInfo vinfo; - - if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo)) - { - depth = 24; - visual = vinfo.visual; - cmap = XCreateColormap (display, - RootWindow (display, screen), - visual, AllocNone); - } - } -#endif - int fd = XConnectionNumber (display); #ifndef NO_SLOW_LINK_SUPPORT @@ -519,138 +560,204 @@ refcache displays; ///////////////////////////////////////////////////////////////////////////// - + bool -rxvt_color::set (rxvt_display *display, Pixel p) +rxvt_color::alloc (rxvt_screen *screen, const 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 / rgba::MAX_CC) << format->direct.red ) + | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green) + | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue ) + | ((color.a * format->direct.alphaMask / 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) + { + c.red = color.g; + c.green = color.g; + c.blue = color.g; + c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) + / rgba::MAX_CC) << ctz (screen->visual->red_mask ) + | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) + / rgba::MAX_CC) << ctz (screen->visual->green_mask) + | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) + / rgba::MAX_CC) << ctz (screen->visual->blue_mask ); + + return true; + } + else + { + c.red = color.r; + c.green = color.g; + c.blue = color.b; + + if (XAllocColor (screen->xdisp, screen->cmap, &c)) + return true; + else + c.pixel = (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; + rgba c; + char eos; + int skip; + + if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) + { + c.a = lerp (0, rgba::MAX_CC, c.a); + name += skip; + } + else + c.a = rgba::MAX_CC; - if (XParseColor (display->display, display->cmap, name, &xc)) - return set (display, xc.red, xc.green, xc.blue); + // 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; - return false; -#endif + 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 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; + + // many kilobytes transfer per colour, but pseudocolor isn't worth + // many extra optimisations. + XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); -void -rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) -{ -#if XFT - cr = c.color.red; - cg = c.color.green; - cb = c.color.blue; -#else - XColor c; + 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; + } + } - c.pixel = p; - XQueryColor (display->display, display->cmap, &c); + //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, rgba (best->red, best->green, best->blue)); - cr = c.red; - cg = c.green; - cb = c.blue; + delete colors; + } #endif + + return got; } -void -rxvt_color::free (rxvt_display *display) +void +rxvt_color::get (rgba &color) { #if XFT - XftColorFree (display->display, display->visual, display->cmap, &c); + color.r = c.color.red; + color.g = c.color.green; + color.b = c.color.blue; + color.a = c.color.alpha; #else - XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); + color.r = c.red; + color.g = c.green; + color.b = c.blue; + color.a = rgba::MAX_CC; #endif } -rxvt_color -rxvt_color::fade (rxvt_display *display, int percent) +void +rxvt_color::free (rxvt_screen *screen) { - percent = 100 - percent; - - unsigned short cr, cg, cb; - rxvt_color faded; - - get (display, cr, cg, cb); - - faded.set ( - display, - cr * percent / 100, - cg * percent / 100, - cb * percent / 100 - ); - - return faded; +#if XFT + XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); +#else + XFreeColors (screen->xdisp, screen->cmap, &c.pixel, 1, AllPlanes); +#endif } -#define LERP(a,b,p) (a * p + b * (100 - p)) / 100 - -rxvt_color -rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto) +void +rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to) { - percent = 100 - percent; - - unsigned short cr, cg, cb; - unsigned short fcr, fcg, fcb; - rxvt_color faded; - - get (display, cr, cg, cb); - fadeto.get(display, fcr, fcg, fcb); + rgba c; + get (c); - faded.set ( - display, - LERP (cr, fcr, percent), - LERP (cg, fcg, percent), - LERP (cb, fcb, percent) + result.set ( + screen, + rgba ( + lerp (c.r, to.r, percent), + lerp (c.g, to.g, percent), + lerp (c.b, to.b, percent), + lerp (c.a, to.a, percent) + ) ); - - return faded; }