--- rxvt-unicode/src/rxvtcolor.C 2004/02/09 07:11:49 1.4 +++ rxvt-unicode/src/rxvtcolor.C 2004/08/04 03:29:28 1.22 @@ -1,3 +1,25 @@ +/*--------------------------------*-C-*---------------------------------* + * File: rxvtcolor.C + *----------------------------------------------------------------------* + * + * All portions of code are copyright by their respective author/s. + * Copyright (c) 2003-2004 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + *----------------------------------------------------------------------*/ + #include "../config.h" #include #include @@ -5,22 +27,144 @@ #include #include +#ifndef NO_SLOW_LINK_SUPPORT +# include +# include +#endif + +class byteorder byteorder; + +byteorder::byteorder () +{ + union { + uint32_t u; + uint8_t b[4]; + } w; + + w.b[0] = 0x11; + w.b[1] = 0x22; + w.b[2] = 0x33; + w.b[3] = 0x44; + + e = w.u; +} + +refcounted::refcounted (const char *id) +{ + this->id = STRDUP (id); +} + +refcounted::~refcounted () +{ + free (id); +} + +template +T *refcache::get (const char *id) +{ + for (T **i = this->begin (); i < this->end (); ++i) + { + if (!strcmp (id, (*i)->id)) + { + (*i)->referenced++; + return *i; + } + } + + T *obj = new T (id); + + obj->referenced = 1; + + if (obj && obj->init ()) + { + this->push_back (obj); + return obj; + } + else + { + delete obj; + return 0; + } +} + +template +void refcache::put (T *obj) +{ + if (!obj) + return; + + if (!--obj->referenced) + { + this->erase (find (this->begin (), this->end (), obj)); + delete obj; + } +} + +template +refcache::~refcache () +{ + while (this->size ()) + put (*this->begin ()); +} + ///////////////////////////////////////////////////////////////////////////// -rxvt_display::rxvt_display (const char *name) -: x_watcher (this, &rxvt_display::x_event) +#ifdef USE_XIM +static void +#if XIMCB_PROTO_BROKEN +im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) +#else +im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) +#endif { - this->name = STRDUP (name); + rxvt_xim *xim = (rxvt_xim *)client_data; + rxvt_display *display = xim->display; + + display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); + + display->im_change_cb (); } -rxvt_display::~rxvt_display () +bool rxvt_xim::init () +{ + display = GET_R->display; //HACK: TODO + + xim = XOpenIM (display->display, NULL, NULL, NULL); + + if (!xim) + return false; + + XIMCallback ximcallback; + ximcallback.client_data = (XPointer)this; + ximcallback.callback = im_destroy_cb; + + XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); + + return true; +} + +rxvt_xim::~rxvt_xim () +{ + if (xim) + XCloseIM (xim); +} +#endif + +///////////////////////////////////////////////////////////////////////////// + +rxvt_display::rxvt_display (const char *id) +: refcounted (id) +, x_ev (this, &rxvt_display::x_cb) +, selection_owner (0) { - free (name); } -bool rxvt_display::open () +bool rxvt_display::init () { - display = XOpenDisplay (name); + display = XOpenDisplay (id); + + if (!display) + return false; screen = DefaultScreen (display); root = DefaultRootWindow (display); @@ -28,6 +172,20 @@ cmap = DefaultColormap (display, screen); depth = DefaultDepth (display, screen); + int fd = XConnectionNumber (display); + +#ifndef NO_SLOW_LINK_SUPPORT + // try to detetc 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; + sockaddr_un sa; + socklen_t sl = sizeof (sa); + + if (!getsockname (fd, (sockaddr *)&sa, &sl)) + is_local = sa.sun_family == AF_LOCAL; +#endif + #ifdef PREFER_24BIT /* * If depth is not 24, look for a 24bit visual. @@ -47,27 +205,51 @@ } #endif - int fd = XConnectionNumber (display); - x_watcher.start (fd, EVENT_READ); - fcntl (fd, F_SETFL, FD_CLOEXEC); + 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::close () +rxvt_display::~rxvt_display () { - x_watcher.stop (); + x_ev.stop (); - XCloseDisplay (display); + if (display) + XCloseDisplay (display); } -void rxvt_display::x_event (io_watcher &w, short revents) +#ifdef USE_XIM +void rxvt_display::im_change_cb () +{ + for (im_watcher **i = imw.begin (); i != imw.end (); ++i) + (*i)->call (); +} +#endif + +void rxvt_display::x_cb (io_watcher &w, short revents) { do { XEvent xev; XNextEvent (display, &xev); + //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D + +#ifdef USE_XIM + if (xev.type == PropertyNotify + && xev.xany.window == root + && xev.xproperty.atom == xa_xim_servers) + im_change_cb (); +#endif + for (int i = xw.size (); i--; ) { if (!xw[i]) @@ -77,6 +259,19 @@ } } while (XPending (display)); + + flush (); +} + +void rxvt_display::flush () +{ + for (;;) + { + if (!XPending (display)) + break; + + x_cb (x_ev, 0); + } } void rxvt_display::reg (xevent_watcher *w) @@ -91,47 +286,63 @@ xw[w->active - 1] = 0; } -///////////////////////////////////////////////////////////////////////////// +void rxvt_display::set_selection_owner (rxvt_term *owner) +{ + if (selection_owner && selection_owner != owner) + selection_owner->selection_clear (); -rxvt_displays displays; + selection_owner = owner; +} -rxvt_display *rxvt_displays::get (const char *name) +#ifdef USE_XIM +void rxvt_display::reg (im_watcher *w) { - for (rxvt_display **i = list.begin (); i < list.end (); ++i) - { - if (!strcmp (name, (*i)->name)) - { - (*i)->referenced++; - return *i; - } - } + imw.push_back (w); +} - rxvt_display *display = new rxvt_display (name); +void rxvt_display::unreg (im_watcher *w) +{ + imw.erase (find (imw.begin (), imw.end (), w)); +} - display->referenced = 1; +rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) +{ + char *id; + int l, m; - if (display && display->open ()) - list.push_back (display); - else - { - delete display; - display = 0; - } + l = strlen (locale); + m = strlen (modifiers); + + if (!(id = (char *)malloc (l + m + 2))) + return 0; + + memcpy (id, locale, l); id[l] = '\n'; + memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; + + rxvt_xim *xim = xims.get (id); - return display; + free (id); + + return xim; } -void rxvt_displays::release (rxvt_display *display) +void rxvt_display::put_xim (rxvt_xim *xim) { - if (!--display->referenced) - { - display->close (); - delete display; - list.erase (find (list.begin (), list.end (), display)); - } + xims.put (xim); +} +#endif + +Atom rxvt_display::atom (const char *name) +{ + return XInternAtom (display, name, False); } ///////////////////////////////////////////////////////////////////////////// + +template class refcache; +refcache displays; + +///////////////////////////////////////////////////////////////////////////// bool rxvt_color::set (rxvt_display *display, Pixel p) @@ -214,7 +425,22 @@ #if XFT XftColorFree (display->display, display->visual, display->cmap, &c); #else - XFreeColors (display->display, display->cmap, &c, 1, AllPlanes); + XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); #endif } +rxvt_color +rxvt_color::fade (rxvt_display *display, 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); + + return faded; +} +