--- rxvt-unicode/src/rxvttoolkit.C 2006/01/30 17:43:20 1.36 +++ rxvt-unicode/src/rxvttoolkit.C 2006/02/20 22:40:35 1.56 @@ -3,7 +3,7 @@ *----------------------------------------------------------------------* * * All portions of code are copyright by their respective author/s. - * Copyright (c) 2003-2004 Marc Lehmann + * Copyright (c) 2003-2006 Marc Lehmann * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,11 +28,8 @@ #include #include - -#ifndef NO_SLOW_LINK_SUPPORT -# include -# include -#endif +#include +#include #if XFT # include @@ -83,6 +80,14 @@ "XDCCC_LINEAR_RGB_MATRICES", "WM_COLORMAP_WINDOWS", "WM_STATE", + "cursor", +# if USE_XIM + "TRANSPORT", + "LOCALES", + "_XIM_PROTOCOL", + "_XIM_XCONNECT", + "_XIM_MOREDATA", +# endif #endif }; @@ -171,7 +176,7 @@ { display = GET_R->display; //HACK: TODO - xim = XOpenIM (display->display, NULL, NULL, NULL); + xim = XOpenIM (display->dpy, 0, 0, 0); if (!xim) return false; @@ -180,7 +185,7 @@ ximcallback.client_data = (XPointer)this; ximcallback.callback = im_destroy_cb; - XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); + XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0); return true; } @@ -195,13 +200,63 @@ ///////////////////////////////////////////////////////////////////////////// +#if XFT +rxvt_drawable::~rxvt_drawable () +{ + if (xftdrawable) + XftDrawDestroy (xftdrawable); +} + +rxvt_drawable::operator XftDraw *() +{ + if (!xftdrawable) + xftdrawable = XftDrawCreate (screen->dpy, drawable, screen->visual, screen->cmap); + + return xftdrawable; +} +#endif + +///////////////////////////////////////////////////////////////////////////// + +#if XFT + +// not strictly necessary as it is only used with superclass of zero_initialised +rxvt_screen::rxvt_screen () +: scratch_area (0) +{ +} + +rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) +{ + // it's actually faster to re-allocate every time. don't ask me + // why, but its likely no big deal there are no roundtrips + // (I think/hope). + if (!scratch_area || w > scratch_w || h > scratch_h || 1/*D*/) + { + if (scratch_area) + { + XFreePixmap (dpy, scratch_area->drawable); + delete scratch_area; + } + + Pixmap pm = XCreatePixmap (dpy, RootWindowOfScreen (ScreenOfDisplay (dpy, display->screen)), + scratch_w = w, scratch_h = h, depth); + + scratch_area = new rxvt_drawable (this, pm); + } + + return *scratch_area; +} + +#endif + void rxvt_screen::set (rxvt_display *disp) { display = disp; - xdisp = disp->display; + dpy = disp->dpy; - Screen *screen = ScreenOfDisplay (xdisp, disp->screen); + Screen *screen = ScreenOfDisplay (dpy, disp->screen); depth = DefaultDepthOfScreen (screen); visual = DefaultVisualOfScreen (screen); @@ -216,11 +271,11 @@ #if XFT XVisualInfo vinfo; - if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo)) + if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) { depth = bitdepth; visual = vinfo.visual; - cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); + cmap = XCreateColormap (dpy, disp->root, visual, AllocNone); } #endif } @@ -228,8 +283,16 @@ void rxvt_screen::clear () { - if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen))) - XFreeColormap (xdisp, cmap); +#if XFT + if (scratch_area) + { + XFreePixmap (dpy, scratch_area->drawable); + delete scratch_area; + } +#endif + + if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (dpy, display->screen))) + XFreeColormap (dpy, cmap); } ///////////////////////////////////////////////////////////////////////////// @@ -242,7 +305,7 @@ } XrmDatabase -rxvt_display::get_resources () +rxvt_display::get_resources (bool refresh) { char *homedir = (char *)getenv ("HOME"); char fname[1024]; @@ -251,9 +314,7 @@ * get resources using the X library function */ char *displayResource, *xe; - XrmDatabase database, rdb1; - - database = NULL; + XrmDatabase rdb1, database = 0; // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 @@ -273,9 +334,42 @@ // 4. User's defaults file. /* Get any Xserver defaults */ - displayResource = XResourceManagerString (display); + if (refresh) + { + // fucking xlib keeps a copy of the rm string + Atom actual_type; + int actual_format; + unsigned long nitems, nremaining; + char *val = 0; + +#if XLIB_ILLEGAL_ACCESS + if (dpy->xdefaults) + XFree (dpy->xdefaults); +#endif + + if (XGetWindowProperty (dpy, RootWindow (dpy, 0), XA_RESOURCE_MANAGER, + 0L, 100000000L, False, + XA_STRING, &actual_type, &actual_format, + &nitems, &nremaining, + (unsigned char **)&val) == Success + && actual_type == XA_STRING + && actual_format == 8) + displayResource = val; + else + { + displayResource = 0; + if (val) + XFree(val); + } + +#if XLIB_ILLEGAL_ACCESS + dpy->xdefaults = displayResource; +#endif + } + else + displayResource = XResourceManagerString (dpy); - if (displayResource != NULL) + if (displayResource) { if ((rdb1 = XrmGetStringDatabase (displayResource))) XrmMergeDatabases (rdb1, &database); @@ -288,10 +382,15 @@ XrmMergeDatabases (rdb1, &database); } +#if !XLIB_ILLEGAL_ACCESS + if (refresh && displayResource) + XFree (displayResource); +#endif + /* Get screen specific resources */ - displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); + displayResource = XScreenResourceString (ScreenOfDisplay (dpy, screen)); - if (displayResource != NULL) + if (displayResource) { if ((rdb1 = XrmGetStringDatabase (displayResource))) /* Merge with screen-independent resources */ @@ -329,41 +428,40 @@ val = rxvt_malloc (5 + strlen (id) + 1); strcpy (val, "unix/"); strcat (val, id); - display = XOpenDisplay (val); + dpy = XOpenDisplay (val); free (val); } else #endif - display = 0; + dpy = 0; - if (!display) - display = XOpenDisplay (id); + if (!dpy) + dpy = XOpenDisplay (id); - if (!display) + if (!dpy) return false; - screen = DefaultScreen (display); - root = DefaultRootWindow (display); + screen = DefaultScreen (dpy); + root = DefaultRootWindow (dpy); assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); - XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); + XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); - XrmSetDatabase (display, get_resources ()); + XrmSetDatabase (dpy, get_resources (false)); #ifdef POINTER_BLANK XColor blackcolour; blackcolour.red = 0; blackcolour.green = 0; blackcolour.blue = 0; - Font f = XLoadFont (display, "fixed"); - blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', + Font f = XLoadFont (dpy, "fixed"); + blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', &blackcolour, &blackcolour); - XUnloadFont (display, f); + XUnloadFont (dpy, f); #endif - int fd = XConnectionNumber (display); + int fd = XConnectionNumber (dpy); -#ifndef NO_SLOW_LINK_SUPPORT // try to detect wether we have a local connection. // assume unix domains socket == local, everything else not // TODO: might want to check for inet/127.0.0.1 @@ -373,12 +471,11 @@ if (!getsockname (fd, (sockaddr *)&sa, &sl)) is_local = sa.sun_family == AF_LOCAL; -#endif x_ev.start (fd, EVENT_READ); fcntl (fd, F_SETFD, FD_CLOEXEC); - XSelectInput (display, root, PropertyChangeMask); + XSelectInput (dpy, root, PropertyChangeMask); flush (); @@ -389,24 +486,24 @@ rxvt_display::ref_next () { // TODO: somehow check wether the database files/resources changed - // before re-loading/parsing - XrmDestroyDatabase (XrmGetDatabase (display)); - XrmSetDatabase (display, get_resources ()); + // before affording re-loading/parsing + XrmDestroyDatabase (XrmGetDatabase (dpy)); + XrmSetDatabase (dpy, get_resources (true)); } rxvt_display::~rxvt_display () { - if (!display) + if (!dpy) return; #ifdef POINTER_BLANK - XFreeCursor (display, blank_cursor); + XFreeCursor (dpy, blank_cursor); #endif x_ev.stop (); #ifdef USE_XIM xims.clear (); #endif - XCloseDisplay (display); + XCloseDisplay (dpy); } #ifdef USE_XIM @@ -424,7 +521,7 @@ int actual_format; unsigned long nitems, bytes_after; - if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L, + if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, False, XA_ATOM, &actual_type, &actual_format, &nitems, &bytes_after, (unsigned char **)&atoms) != Success ) @@ -432,7 +529,7 @@ if (actual_type == XA_ATOM && actual_format == 32) for (int i = 0; i < nitems; i++) - if (XGetSelectionOwner (display, atoms[i])) + if (XGetSelectionOwner (dpy, atoms[i])) { im_change_cb (); break; @@ -447,7 +544,7 @@ do { XEvent xev; - XNextEvent (display, &xev); + XNextEvent (dpy, &xev); #ifdef USE_XIM if (!XFilterEvent (&xev, None)) @@ -468,17 +565,17 @@ } #endif } - while (XEventsQueued (display, QueuedAlready)); + while (XEventsQueued (dpy, QueuedAlready)); - XFlush (display); + XFlush (dpy); } void rxvt_display::flush () { - if (XEventsQueued (display, QueuedAlready)) + if (XEventsQueued (dpy, QueuedAlready)) x_cb (x_ev, EVENT_READ); - XFlush (display); + XFlush (dpy); } void rxvt_display::reg (xevent_watcher *w) @@ -502,6 +599,7 @@ } #ifdef USE_XIM + void rxvt_display::reg (im_watcher *w) { imw.push_back (w); @@ -535,15 +633,16 @@ 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) { - return XInternAtom (display, name, False); + return XInternAtom (dpy, name, False); } ///////////////////////////////////////////////////////////////////////////// @@ -552,72 +651,29 @@ refcache displays; ///////////////////////////////////////////////////////////////////////////// - -bool -rxvt_color::set (rxvt_screen *screen, const char *name) -{ -#if XFT - int l = strlen (name); - rxvt_rgba r; - char eos; - int mult; - - // 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)) - { - 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; - - // slow case: server round trip - else - return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c); - - 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 -} - + bool -rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba) +rxvt_color::alloc (rxvt_screen *screen, const rgba &color) { #if XFT XRenderPictFormat *format; - // FUCKING Xft gets it wrong, of course, so work around it - // transparency users should eat shit and die, and then + // 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 ((screen->visual->c_class == TrueColor) + && (format = XRenderFindVisualFormat (screen->dpy, 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); + 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; } @@ -625,114 +681,184 @@ { XRenderColor d; - d.red = rgba.r; - d.green = rgba.g; - d.blue = rgba.b; - d.alpha = rgba.a; + 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); + return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); } - - return false; #else + c.red = color.r; + c.green = color.g; + c.blue = color.b; + if (screen->visual->c_class == TrueColor) { - p = ((rgba.r * ((1 << screen->visual->bits_per_rgb) - 1) - / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask )) - | ((rgba.g * ((1 << screen->visual->bits_per_rgb) - 1) - / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask)) - | ((rgba.b * ((1 << screen->visual->bits_per_rgb) - 1) - / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask )); + 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->dpy, screen->cmap, &c)) + return true; else + c.pixel = (color.r + color.g + color.b) > 128*3 + ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) + : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); +#endif + + return false; +} + +bool +rxvt_color::set (rxvt_screen *screen, const char *name) +{ + rgba c; + char eos; + int skip; + + // 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; + + // 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; + XColor xc, xc_exact; - xc.red = rgba.r; - xc.green = rgba.g; - xc.blue = rgba.b; - xc.flags = DoRed | DoGreen | DoBlue; - - if (XAllocColor (screen->xdisp, screen->cmap, &xc)) - { - p = xc.pixel; - return true; - } + if (XParseColor (screen->dpy, 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 false; -#endif + return set (screen, c); } -void -rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba) +bool +rxvt_color::set (rxvt_screen *screen, const rgba &color) { -#if XFT - rgba.r = c.color.red; - rgba.g = c.color.green; - rgba.b = c.color.blue; - rgba.a = c.color.alpha; -#else - XColor c; + bool got = alloc (screen, color); + +#if !ENABLE_MINIMAL + int cmap_size = screen->visual->map_entries; + + if (!got + && screen->visual->c_class == PseudoColor + && cmap_size < 4096) + { + XColor *colors = new XColor [screen->visual->map_entries]; + + 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->dpy, screen->cmap, colors, cmap_size); + + int diff = 0x7fffffffUL; + XColor *best = colors; - c.pixel = p; - XQueryColor (screen->xdisp, screen->cmap, &c); + 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)); - rgba.r = c.red; - rgba.g = c.green; - rgba.b = c.blue; - rgba.a = rxvt_rgba::MAX_CC; + delete colors; + } #endif + + return got; } -void -rxvt_color::free (rxvt_screen *screen) +void +rxvt_color::get (rgba &color) { #if XFT - XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); + color.r = c.color.red; + color.g = c.color.green; + color.b = c.color.blue; + color.a = c.color.alpha; #else - XFreeColors (screen->xdisp, screen->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_screen *screen, int percent) +void +rxvt_color::get (XColor &color) { - rxvt_color faded; + rgba c; + get (c); - 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); + color.red = c.r; + color.green = c.g; + color.blue = c.b; + color.pixel = (Pixel)*this; +} - faded.set (screen, c); +void +rxvt_color::free (rxvt_screen *screen) +{ + if (screen->visual->c_class == TrueColor) + return; // nothing to do - return faded; +#if XFT + XftColorFree (screen->dpy, screen->visual, screen->cmap, &c); +#else + XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes); +#endif } -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) { - rxvt_rgba c, fc; - rxvt_color faded; - - get (screen, c); - fadeto.get (screen, fc); + rgba c; + get (c); - 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) + 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; }