--- rxvt-unicode/src/rxvttoolkit.C 2006/01/30 02:21:20 1.34 +++ rxvt-unicode/src/rxvttoolkit.C 2006/01/31 16:57:35 1.42 @@ -83,6 +83,14 @@ "XDCCC_LINEAR_RGB_MATRICES", "WM_COLORMAP_WINDOWS", "WM_STATE", + "cursor", +# if USE_XIM + "TRANSPORT", + "LOCALES", + "_XIM_PROTOCOL", + "_XIM_XCONNECT", + "_XIM_MOREDATA", +# endif #endif }; @@ -552,124 +560,184 @@ refcache displays; ///////////////////////////////////////////////////////////////////////////// - + bool -rxvt_color::set (rxvt_screen *screen, const char *name) +rxvt_color::alloc (rxvt_screen *screen, const rxvt_rgba &color) { #if XFT - int l = strlen (name); - rxvt_rgba r; - char eos; - int mult; + XRenderPictFormat *format; - // shortcutting this saves countless server RTTs for the built-in colours - if (l == 3+3*3 && 3 == sscanf (name, "rgb:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos)) + // 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))) { - r.a = rxvt_rgba::MAX_CC; - mult = rxvt_rgba::MAX_CC / 0x00ff; - } - - // parse a number of non-standard ARGB colour specifications - else if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) - mult = rxvt_rgba::MAX_CC / 0x000f; - else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) - mult = rxvt_rgba::MAX_CC / 0x00ff; - else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) - mult = rxvt_rgba::MAX_CC / 0xffff; - else if (l == 4+5*4 && 4 == sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos)) - mult = rxvt_rgba::MAX_CC / 0xffff; + // 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); - // slow case: server round trip + return true; + } else - return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c); + { + XRenderColor d; - r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult; + d.red = color.r; + d.green = color.g; + d.blue = color.b; + d.alpha = color.a; - return set (screen, r); + return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); + } #else - XColor xc; + 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; - if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) - return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue)); + 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 false; -#endif } bool -rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba) +rxvt_color::set (rxvt_screen *screen, const char *name) { -#if XFT - XRenderPictFormat *format; + rxvt_rgba c; + char eos; + int skip; - // 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 - && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual))) + if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) { - // the fun lies in doing everything manually... - c.color.red = rgba.r; - c.color.green = rgba.g; - c.color.blue = rgba.b; - c.color.alpha = rgba.a; - - c.pixel = ((rgba.r * format->direct.redMask / rxvt_rgba::MAX_CC) << format->direct.red ) - | ((rgba.g * format->direct.greenMask / rxvt_rgba::MAX_CC) << format->direct.green) - | ((rgba.b * format->direct.blueMask / rxvt_rgba::MAX_CC) << format->direct.blue ) - | ((rgba.a * format->direct.alphaMask / rxvt_rgba::MAX_CC) << format->direct.alpha); - - return true; + printf ("X %d\n", c.a); + c.a = lerp (0, rxvt_rgba::MAX_CC, c.a); + name += skip; + printf ("y %04x\n", c.a); } 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)) { - XRenderColor d; + XColor xc, xc_exact; - d.red = rgba.r; - d.green = rgba.g; - d.blue = rgba.b; - d.alpha = rgba.a; + 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; - return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); + rxvt_warn ("unable to parse color '%s', using pink instead.\n", name); + } } - return false; -#else - XColor xc; + return set (screen, c); +} - xc.red = rgba.r; - xc.green = rgba.g; - xc.blue = rgba.b; - xc.flags = DoRed | DoGreen | DoBlue; +bool +rxvt_color::set (rxvt_screen *screen, const rxvt_rgba &color) +{ + bool got = alloc (screen, color); + +#if !ENABLE_MINIMAL + int cmap_size = screen->visual->map_entries; - if (XAllocColor (screen->xdisp, screen->cmap, &xc)) + if (!got + && screen->visual->c_class == PseudoColor + && cmap_size < 4096) { - p = xc.pixel; - return true; - } + 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_screen *screen, rxvt_rgba &rgba) +void +rxvt_color::get (rxvt_screen *screen, rxvt_rgba &color) { #if XFT - rgba.r = c.color.red; - rgba.g = c.color.green; - rgba.b = c.color.blue; - rgba.a = c.color.alpha; + 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 (screen->xdisp, screen->cmap, &c); - rgba.r = c.red; - rgba.g = c.green; - rgba.b = c.blue; - rgba.a = rxvt_rgba::MAX_CC; + color.r = c.red; + color.g = c.green; + color.b = c.blue; + color.a = rxvt_rgba::MAX_CC; #endif } @@ -683,42 +751,20 @@ #endif } -rxvt_color -rxvt_color::fade (rxvt_screen *screen, int percent) +void +rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rxvt_rgba &to) { - rxvt_color faded; - rxvt_rgba c; get (screen, c); - c.r = lerp (0, c.r, percent); - c.g = lerp (0, c.g, percent); - c.b = lerp (0, c.b, percent); - - faded.set (screen, c); - - return faded; -} - -rxvt_color -rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto) -{ - rxvt_rgba c, fc; - rxvt_color faded; - - get (screen, c); - fadeto.get (screen, fc); - - faded.set ( + result.set ( screen, rxvt_rgba ( - lerp (fc.r, c.r, percent), - lerp (fc.g, c.g, percent), - lerp (fc.b, c.b, percent), - lerp (fc.a, c.a, percent) + 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; }