--- rxvt-unicode/src/rxvttoolkit.C 2004/12/29 19:59:46 1.10 +++ rxvt-unicode/src/rxvttoolkit.C 2006/01/29 20:51:28 1.28 @@ -27,11 +27,51 @@ #include #include +#include + #ifndef NO_SLOW_LINK_SUPPORT # include # include #endif +const char *const xa_names[] = + { + "TEXT", + "COMPOUND_TEXT", + "UTF8_STRING", + "MULTIPLE", + "TARGETS", + "TIMESTAMP", + "VT_SELECTION", + "INCR", + "WM_PROTOCOLS", + "WM_DELETE_WINDOW", + "CLIPBOARD", +#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 + }; + +///////////////////////////////////////////////////////////////////////////// + refcounted::refcounted (const char *id) { this->id = strdup (id); @@ -49,17 +89,17 @@ { if (!strcmp (id, (*i)->id)) { - (*i)->referenced++; + ++(*i)->referenced; + (*i)->ref_next (); return *i; } } T *obj = new T (id); - obj->referenced = 1; - - if (obj && obj->init ()) + if (obj && obj->ref_init ()) { + obj->referenced = 1; this->push_back (obj); return obj; } @@ -84,7 +124,7 @@ } template -refcache::~refcache () +void refcache::clear () { while (this->size ()) put (*this->begin ()); @@ -93,6 +133,7 @@ ///////////////////////////////////////////////////////////////////////////// #ifdef USE_XIM + static void #if XIMCB_PROTO_BROKEN im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) @@ -109,7 +150,8 @@ display->im_change_cb (); } -bool rxvt_xim::init () +bool +rxvt_xim::ref_init () { display = GET_R->display; //HACK: TODO @@ -132,10 +174,48 @@ if (xim) XCloseIM (xim); } + #endif ///////////////////////////////////////////////////////////////////////////// +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); + + XVisualInfo vinfo; + + if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo)) + { + depth = bitdepth; + visual = vinfo.visual; + cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); + } +} + +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) @@ -143,23 +223,130 @@ { } -bool rxvt_display::init () +XrmDatabase +rxvt_display::get_resources () { - display = XOpenDisplay (id); + char *homedir = (char *)getenv ("HOME"); + char fname[1024]; + + /* + * get resources using the X library function + */ + char *displayResource, *xe; + XrmDatabase database, rdb1; + + database = NULL; + + // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 + + // 6. System wide per application default file. + + /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ + if ((xe = (char *)getenv ("XAPPLRESDIR"))) + { + snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); + + if ((rdb1 = XrmGetFileDatabase (fname))) + XrmMergeDatabases (rdb1, &database); + } + + // 5. User's per application default file. + // none + + // 4. User's defaults file. + /* Get any Xserver defaults */ + displayResource = XResourceManagerString (display); + + if (displayResource != NULL) + { + if ((rdb1 = XrmGetStringDatabase (displayResource))) + XrmMergeDatabases (rdb1, &database); + } + else if (homedir) + { + snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir); + + if ((rdb1 = XrmGetFileDatabase (fname))) + XrmMergeDatabases (rdb1, &database); + } + + /* Get screen specific resources */ + displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); + + if (displayResource != NULL) + { + if ((rdb1 = XrmGetStringDatabase (displayResource))) + /* Merge with screen-independent resources */ + XrmMergeDatabases (rdb1, &database); + + XFree (displayResource); + } + + // 3. User's per host defaults file + /* Add in XENVIRONMENT file */ + if ((xe = (char *)getenv ("XENVIRONMENT")) + && (rdb1 = XrmGetFileDatabase (xe))) + XrmMergeDatabases (rdb1, &database); + else if (homedir) + { + struct utsname un; + + if (!uname (&un)) + { + snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename); + + if ((rdb1 = XrmGetFileDatabase (fname))) + XrmMergeDatabases (rdb1, &database); + } + } + + return database; +} + +bool rxvt_display::ref_init () +{ +#ifdef LOCAL_X_IS_UNIX + if (id[0] == ':') + { + val = rxvt_malloc (5 + strlen (id) + 1); + strcpy (val, "unix/"); + strcat (val, id); + display = XOpenDisplay (val); + free (val); + } + else +#endif + display = 0; + + if (!display) + display = XOpenDisplay (id); if (!display) return false; 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 ()); + +#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, ' ', ' ', + &blackcolour, &blackcolour); + XUnloadFont (display, f); +#endif int fd = XConnectionNumber (display); #ifndef NO_SLOW_LINK_SUPPORT - // try to detetc wether we have a local connection. + // 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 is_local = 0; @@ -170,44 +357,38 @@ is_local = sa.sun_family == AF_LOCAL; #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 - x_ev.start (fd, EVENT_READ); fcntl (fd, F_SETFD, FD_CLOEXEC); XSelectInput (display, root, PropertyChangeMask); -#ifdef USE_XIM - xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); -#endif flush (); return true; } +void +rxvt_display::ref_next () +{ + // TODO: somehow check wether the database files/resources changed + // before re-loading/parsing + XrmDestroyDatabase (XrmGetDatabase (display)); + XrmSetDatabase (display, get_resources ()); +} + rxvt_display::~rxvt_display () { - x_ev.stop (); + if (!display) + return; - if (display) - XCloseDisplay (display); +#ifdef POINTER_BLANK + XFreeCursor (display, blank_cursor); +#endif + x_ev.stop (); +#ifdef USE_XIM + xims.clear (); +#endif + XCloseDisplay (display); } #ifdef USE_XIM @@ -219,13 +400,13 @@ void rxvt_display::im_change_check () { - // make sure we only call im_change_cb when a new input method + // try to only call im_change_cb when a new input method // registers, as xlib crashes due to a race otherwise. Atom actual_type, *atoms; 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 ) @@ -253,10 +434,9 @@ #ifdef USE_XIM if (!XFilterEvent (&xev, None)) { - 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--; ) @@ -356,106 +536,131 @@ ///////////////////////////////////////////////////////////////////////////// bool -rxvt_color::set (rxvt_display *display, Pixel p) +rxvt_color::set (rxvt_screen *screen, const char *name) { #if XFT - XColor xc; - - xc.pixel = p; - if (!XQueryColor (display->display, display->cmap, &xc)) - return false; - - 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); -#else - this->p = p; -#endif - - return true; -} + int l = strlen (name); + rxvt_rgba r; + char eos; + int mult; + + if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) + mult = 0x1111; + else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) + mult = 0x0101; + else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) + mult = 0x0001; + else + return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c); -bool -rxvt_color::set (rxvt_display *display, const char *name) -{ -#if XFT - return XftColorAllocName (display->display, display->visual, display->cmap, - name, &c); + r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult; + return set (screen, r); #else XColor xc; - if (XParseColor (display->display, display->cmap, name, &xc)) - return set (display, xc.red, xc.green, xc.blue); + 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_display *display, unsigned short cr, unsigned short cg, unsigned short cb) +rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba) { +#if XFT + 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 XColor xc; - xc.red = cr; - xc.green = cg; - xc.blue = cb; + xc.red = rgba.r; + xc.green = rgba.g; + xc.blue = rgba.b; xc.flags = DoRed | DoGreen | DoBlue; - if (XAllocColor (display->display, display->cmap, &xc)) - return set (display, xc.pixel); + if (XAllocColor (screen->xdisp, screen->cmap, &xc)) + { + p = xc.pixel; + return true; + } return false; +#endif } void -rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) +rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba) { #if XFT - cr = c.color.red; - cg = c.color.green; - cb = c.color.blue; + rgba.r = c.color.red; + rgba.g = c.color.green; + rgba.b = c.color.blue; + rgba.a = c.color.alpha; #else XColor c; c.pixel = p; - XQueryColor (display->display, display->cmap, &c); + XQueryColor (screen->xdisp, screen->cmap, &c); - cr = c.red; - cg = c.green; - cb = c.blue; + rgba.r = c.red; + rgba.g = c.green; + rgba.b = c.blue; + rgba.a = rxvt_rgba::MAX_CC; #endif } void -rxvt_color::free (rxvt_display *display) +rxvt_color::free (rxvt_screen *screen) { #if XFT - XftColorFree (display->display, display->visual, display->cmap, &c); + XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); #else - XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); + XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); #endif } rxvt_color -rxvt_color::fade (rxvt_display *display, int percent) +rxvt_color::fade (rxvt_screen *screen, int percent) { - unsigned short cr, cg, cb; rxvt_color faded; - get (display, cr, cg, cb); - faded.set (display, - cr * percent / 100, - cg * percent / 100, - cb * percent / 100); + 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 ( + 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) + ) + ); return faded; }