--- rxvt-unicode/src/rxvttoolkit.C 2006/01/29 21:45:47 1.29 +++ rxvt-unicode/src/rxvttoolkit.C 2006/01/30 19:46:13 1.38 @@ -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 +}; ///////////////////////////////////////////////////////////////////////////// @@ -197,6 +221,7 @@ { set (disp); +#if XFT XVisualInfo vinfo; if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo)) @@ -205,6 +230,7 @@ visual = vinfo.visual; cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); } +#endif } void @@ -534,82 +560,162 @@ refcache displays; ///////////////////////////////////////////////////////////////////////////// - + bool -rxvt_color::set (rxvt_screen *screen, const char *name) +rxvt_color::alloc (rxvt_screen *screen, rxvt_rgba rgba) { #if XFT + XRenderPictFormat *format; + + // 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 = 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; + } + else + { + XRenderColor d; + + d.red = rgba.r; + d.green = rgba.g; + d.blue = rgba.b; + d.alpha = rgba.a; + + return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); + } +#else + if (screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) + { + p = (rgba.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) + / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask ) + | (rgba.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) + / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask) + | (rgba.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 = rgba.r; + xc.green = rgba.g; + xc.blue = rgba.b; + + if (XAllocColor (screen->xdisp, screen->cmap, &xc)) + { + p = xc.pixel; + return true; + } + else + p = (rgba.r + rgba.g + rgba.b) > 128*3 + ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) + : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); + } +#endif + + return false; +} + +bool +rxvt_color::set (rxvt_screen *screen, const char *name) +{ int l = strlen (name); rxvt_rgba r; char eos; int mult; + XColor xc, xc_exact; + // parse a number of non-standard ARGB colour specifications 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; + mult = rxvt_rgba::MAX_CC / 0x0010; 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; + mult = rxvt_rgba::MAX_CC / 0x0100; 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; + mult = rxvt_rgba::MAX_CC / 0x0100; 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; + else if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) + { + r.r = xc.red; + r.g = xc.green; + r.b = xc.blue; + mult = rxvt_rgba::MAX_CC / 0xffff; + } else - return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c); + { + rxvt_warn ("failed to allocate color '%s', using pink instead.\n", name); + r.r = 255; + r.g = 105; + r.b = 180; + mult = rxvt_rgba::MAX_CC / 0x00ff; + } r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult; - return set (screen, r); -#else - XColor xc; - - if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) - return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue)); - return false; -#endif + return set (screen, r); } bool rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba) { -#if XFT - XRenderColor d; + bool got = alloc (screen, rgba); - d.red = rgba.r; - d.green = rgba.g; - d.blue = rgba.b; - d.alpha = rgba.a; - - if (XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c)) - { - // FUCKING Xft gets it wrong, of course, fix it for the common case - // transparency users should eat shit and die, and then - // XRenderQueryPictIndexValues themselves plenty. - if (screen->depth == 32 - && screen->visual->c_class == TrueColor - && screen->visual->red_mask == 0x00ff0000 - && screen->visual->green_mask == 0x0000ff00 - && screen->visual->blue_mask == 0x000000ff) - c.pixel = c.pixel & 0x00ffffffUL | ((rgba.a >> 8) << 24); +#if !ENABLE_MINIMAL + int cmap_size = screen->visual->map_entries; - return true; - } + if (!got + && screen->visual->c_class == PseudoColor + && cmap_size < 4096) + { + XColor *colors = new XColor [screen->visual->map_entries]; - return false; -#else - XColor xc; + for (int i = 0; i < cmap_size; i++) + colors [i].pixel = i; + + XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); - xc.red = rgba.r; - xc.green = rgba.g; - xc.blue = rgba.b; - xc.flags = DoRed | DoGreen | DoBlue; + int diff = 0x7fffffffUL; + XColor *best = colors; - if (XAllocColor (screen->xdisp, screen->cmap, &xc)) - { - p = xc.pixel; - return true; - } + for (int i = 0; i < cmap_size; i++) + { + int d = (squared_diff (rgba.r >> 2, colors [i].red >> 2)) + + (squared_diff (rgba.g >> 2, colors [i].green >> 2)) + + (squared_diff (rgba.b >> 2, colors [i].blue >> 2)); - return false; + if (d < diff) + { + diff = d; + best = colors + i; + } + } + + //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", + // rgba.r, rgba.g, rgba.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