--- rxvt-unicode/src/rxvttoolkit.C 2006/01/25 21:03:04 1.26 +++ rxvt-unicode/src/rxvttoolkit.C 2006/01/31 19:53:31 1.44 @@ -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 - }; +}; ///////////////////////////////////////////////////////////////////////////// @@ -183,29 +207,30 @@ rxvt_screen::set (rxvt_display *disp) { display = disp; - - xdisp = disp->display; + xdisp = disp->display; Screen *screen = ScreenOfDisplay (xdisp, disp->screen); - depth = DefaultDepthOfScreen (screen); - visual = DefaultVisualOfScreen (screen); - cmap = DefaultColormapOfScreen (screen); + depth = DefaultDepthOfScreen (screen); + visual = DefaultVisualOfScreen (screen); + cmap = DefaultColormapOfScreen (screen); } void -rxvt_screen::set (rxvt_display *disp, int depth) +rxvt_screen::set (rxvt_display *disp, int bitdepth) { set (disp); +#if XFT XVisualInfo vinfo; - if (XMatchVisualInfo (xdisp, display->screen, depth, TrueColor, &vinfo)) + if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo)) { - this->depth = depth; - this->visual = vinfo.visual; - this->cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); + depth = bitdepth; + visual = vinfo.visual; + cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); } +#endif } void @@ -535,83 +560,179 @@ refcache displays; ///////////////////////////////////////////////////////////////////////////// - + bool -rxvt_color::set (rxvt_screen *screen, Pixel p) +rxvt_color::alloc (rxvt_screen *screen, const rgba &color) { #if XFT - XColor xc; + XRenderPictFormat *format; - xc.pixel = p; - if (!XQueryColor (screen->xdisp, screen->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); - XRenderColor d; + return true; + } + else + { + 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 (screen->xdisp, - screen->visual, - screen->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_screen *screen, const char *name) { -#if XFT - return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c); -#else - XColor xc; + rgba c; + char eos; + int skip; + + if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) + { + printf ("X %d\n", c.a); + c.a = lerp (0, rgba::MAX_CC, c.a); + name += skip; + printf ("y %04x\n", c.a); + } + else + c.a = rgba::MAX_CC; - if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) - return set (screen, 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_screen *screen, 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 (screen->xdisp, screen->cmap, &xc)) - return set (screen, 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); + + 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, rgba (best->red, best->green, best->blue)); + + delete colors; + } +#endif + + return got; } -void -rxvt_color::get (rxvt_screen *screen, unsigned short &cr, unsigned short &cg, unsigned short &cb) +void +rxvt_color::get (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 (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 = rgba::MAX_CC; #endif } @@ -621,51 +742,24 @@ #if XFT XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); #else - XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); + XFreeColors (screen->xdisp, screen->cmap, &c.pixel, 1, AllPlanes); #endif } -rxvt_color -rxvt_color::fade (rxvt_screen *screen, int percent) -{ - percent = 100 - percent; - - unsigned short cr, cg, cb; - rxvt_color faded; - - get (screen, cr, cg, cb); - - faded.set ( - screen, - cr * percent / 100, - cg * percent / 100, - cb * percent / 100 - ); - - return faded; -} - -#define LERP(a,b,p) (a * p + b * (100 - p)) / 100 - -rxvt_color -rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto) +void +rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to) { - percent = 100 - percent; + rgba c; + get (c); - unsigned short cr, cg, cb; - unsigned short fcr, fcg, fcb; - rxvt_color faded; - - get (screen, cr, cg, cb); - fadeto.get (screen, fcr, fcg, fcb); - - faded.set ( + result.set ( screen, - LERP (cr, fcr, percent), - LERP (cg, fcg, percent), - LERP (cb, fcb, percent) + 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; }