--- rxvt-unicode/src/rxvttoolkit.C 2006/01/25 00:42:21 1.24 +++ rxvt-unicode/src/rxvttoolkit.C 2006/02/02 00:31:47 1.47 @@ -34,6 +34,68 @@ # 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); @@ -141,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) @@ -251,9 +352,9 @@ 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); XrmSetDatabase (display, get_resources ()); @@ -268,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 @@ -305,9 +387,6 @@ fcntl (fd, F_SETFD, FD_CLOEXEC); XSelectInput (display, root, PropertyChangeMask); -#ifdef USE_XIM - xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); -#endif flush (); @@ -318,7 +397,7 @@ rxvt_display::ref_next () { // TODO: somehow check wether the database files/resources changed - // before re-loading/parsing + // before affording re-loading/parsing XrmDestroyDatabase (XrmGetDatabase (display)); XrmSetDatabase (display, get_resources ()); } @@ -353,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 ) @@ -383,7 +462,7 @@ { 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--; ) @@ -431,6 +510,7 @@ } #ifdef USE_XIM + void rxvt_display::reg (im_watcher *w) { imw.push_back (w); @@ -464,10 +544,11 @@ void rxvt_display::put_xim (rxvt_xim *xim) { -#if XLIB_IS_RACEFREE +# if XLIB_IS_RACEFREE xims.put (xim); -#endif +# endif } + #endif Atom rxvt_display::atom (const char *name) @@ -481,138 +562,211 @@ 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); - XRenderColor d; + return true; + } + else + { + XRenderColor d; - 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); + d.red = color.r; + d.green = color.g; + d.blue = color.b; + d.alpha = color.a; + + return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); + } #else - this->p = p; + c.red = color.r; + c.green = color.g; + c.blue = color.b; + + if (screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) + { + 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 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 (XParseColor (display->display, display->cmap, name, &xc)) - return set (display, xc.red, xc.green, xc.blue); + // parse the nonstandard "[alphapercent]" prefix + 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; - return false; -#endif + // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format + if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%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 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::get (XColor &color) { - percent = 100 - percent; - - unsigned short cr, cg, cb; - rxvt_color faded; + rgba c; + get (c); - get (display, cr, cg, cb); - - faded.set ( - display, - cr * percent / 100, - cg * percent / 100, - cb * percent / 100 - ); - - return faded; + color.red = c.r; + color.green = c.g; + color.blue = c.b; + color.pixel = (Pixel)*this; } -#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::free (rxvt_screen *screen) { - percent = 100 - percent; +#if XFT + XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); +#else + XFreeColors (screen->xdisp, screen->cmap, &c.pixel, 1, AllPlanes); +#endif +} - unsigned short cr, cg, cb; - unsigned short fcr, fcg, fcb; - rxvt_color faded; - - get (display, cr, cg, cb); - fadeto.get(display, fcr, fcg, fcb); +void +rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to) +{ + 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; }