1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *----------------------------------------------------------------------*/
23 #include "../config.h"
25 #include <rxvttoolkit.h>
30 #include <sys/utsname.h>
32 #ifndef NO_SLOW_LINK_SUPPORT
33 # include <sys/socket.h>
38 # include <X11/extensions/Xrender.h>
41 const char *const xa_names[] =
82 "XDCCC_LINEAR_RGB_CORRECTION",
83 "XDCCC_LINEAR_RGB_MATRICES",
84 "WM_COLORMAP_WINDOWS",
97 /////////////////////////////////////////////////////////////////////////////
99 refcounted::refcounted (const char *id)
101 this->id = strdup (id);
104 refcounted::~refcounted ()
110 T *refcache<T>::get (const char *id)
112 for (T **i = this->begin (); i < this->end (); ++i)
114 if (!strcmp (id, (*i)->id))
124 if (obj && obj->ref_init ())
127 this->push_back (obj);
138 void refcache<T>::put (T *obj)
143 if (!--obj->referenced)
145 this->erase (find (this->begin (), this->end (), obj));
151 void refcache<T>::clear ()
153 while (this->size ())
154 put (*this->begin ());
157 /////////////////////////////////////////////////////////////////////////////
162 #if XIMCB_PROTO_BROKEN
163 im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
165 im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
168 rxvt_xim *xim = (rxvt_xim *)client_data;
169 rxvt_display *display = xim->display;
173 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
174 display->im_change_cb ();
178 rxvt_xim::ref_init ()
180 display = GET_R->display; //HACK: TODO
182 xim = XOpenIM (display->dpy, 0, 0, 0);
187 XIMCallback ximcallback;
188 ximcallback.client_data = (XPointer)this;
189 ximcallback.callback = im_destroy_cb;
191 XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0);
196 rxvt_xim::~rxvt_xim ()
204 /////////////////////////////////////////////////////////////////////////////
207 rxvt_screen::set (rxvt_display *disp)
212 Screen *screen = ScreenOfDisplay (dpy, disp->screen);
214 depth = DefaultDepthOfScreen (screen);
215 visual = DefaultVisualOfScreen (screen);
216 cmap = DefaultColormapOfScreen (screen);
220 rxvt_screen::set (rxvt_display *disp, int bitdepth)
227 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo))
230 visual = vinfo.visual;
231 cmap = XCreateColormap (dpy, disp->root, visual, AllocNone);
237 rxvt_screen::clear ()
239 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (dpy, display->screen)))
240 XFreeColormap (dpy, cmap);
243 /////////////////////////////////////////////////////////////////////////////
245 rxvt_display::rxvt_display (const char *id)
247 , x_ev (this, &rxvt_display::x_cb)
248 , selection_owner (0)
253 rxvt_display::get_resources (bool refresh)
255 char *homedir = (char *)getenv ("HOME");
259 * get resources using the X library function
261 char *displayResource, *xe;
262 XrmDatabase rdb1, database = 0;
264 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
266 // 6. System wide per application default file.
268 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
269 if ((xe = (char *)getenv ("XAPPLRESDIR")))
271 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
273 if ((rdb1 = XrmGetFileDatabase (fname)))
274 XrmMergeDatabases (rdb1, &database);
277 // 5. User's per application default file.
280 // 4. User's defaults file.
281 /* Get any Xserver defaults */
284 // fucking xlib keeps a copy of the rm string
287 unsigned long nitems, nremaining;
290 #if XLIB_ILLEGAL_ACCESS
292 XFree (dpy->xdefaults);
295 if (XGetWindowProperty (dpy, root, XA_RESOURCE_MANAGER,
296 0L, 100000000L, False,
297 XA_STRING, &actual_type, &actual_format,
298 &nitems, &nremaining,
299 (unsigned char **)&val) == Success
300 && actual_type == XA_STRING
301 && actual_format == 8)
302 displayResource = val;
310 #if XLIB_ILLEGAL_ACCESS
311 dpy->xdefaults = displayResource;
315 displayResource = XResourceManagerString (dpy);
319 if ((rdb1 = XrmGetStringDatabase (displayResource)))
320 XrmMergeDatabases (rdb1, &database);
324 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
326 if ((rdb1 = XrmGetFileDatabase (fname)))
327 XrmMergeDatabases (rdb1, &database);
330 #if !XLIB_ILLEGAL_ACCESS
331 if (refresh && displayResource)
332 XFree (displayResource);
335 /* Get screen specific resources */
336 displayResource = XScreenResourceString (ScreenOfDisplay (dpy, screen));
340 if ((rdb1 = XrmGetStringDatabase (displayResource)))
341 /* Merge with screen-independent resources */
342 XrmMergeDatabases (rdb1, &database);
344 XFree (displayResource);
347 // 3. User's per host defaults file
348 /* Add in XENVIRONMENT file */
349 if ((xe = (char *)getenv ("XENVIRONMENT"))
350 && (rdb1 = XrmGetFileDatabase (xe)))
351 XrmMergeDatabases (rdb1, &database);
358 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
360 if ((rdb1 = XrmGetFileDatabase (fname)))
361 XrmMergeDatabases (rdb1, &database);
368 bool rxvt_display::ref_init ()
370 #ifdef LOCAL_X_IS_UNIX
373 val = rxvt_malloc (5 + strlen (id) + 1);
374 strcpy (val, "unix/");
376 dpy = XOpenDisplay (val);
384 dpy = XOpenDisplay (id);
389 screen = DefaultScreen (dpy);
390 root = DefaultRootWindow (dpy);
392 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
393 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa);
395 XrmSetDatabase (dpy, get_resources (false));
400 blackcolour.green = 0;
401 blackcolour.blue = 0;
402 Font f = XLoadFont (dpy, "fixed");
403 blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ',
404 &blackcolour, &blackcolour);
405 XUnloadFont (dpy, f);
408 int fd = XConnectionNumber (dpy);
410 #ifndef NO_SLOW_LINK_SUPPORT
411 // try to detect wether we have a local connection.
412 // assume unix domains socket == local, everything else not
413 // TODO: might want to check for inet/127.0.0.1
416 socklen_t sl = sizeof (sa);
418 if (!getsockname (fd, (sockaddr *)&sa, &sl))
419 is_local = sa.sun_family == AF_LOCAL;
422 x_ev.start (fd, EVENT_READ);
423 fcntl (fd, F_SETFD, FD_CLOEXEC);
425 XSelectInput (dpy, root, PropertyChangeMask);
433 rxvt_display::ref_next ()
435 // TODO: somehow check wether the database files/resources changed
436 // before affording re-loading/parsing
437 XrmDestroyDatabase (XrmGetDatabase (dpy));
438 XrmSetDatabase (dpy, get_resources (true));
441 rxvt_display::~rxvt_display ()
447 XFreeCursor (dpy, blank_cursor);
457 void rxvt_display::im_change_cb ()
459 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
463 void rxvt_display::im_change_check ()
465 // try to only call im_change_cb when a new input method
466 // registers, as xlib crashes due to a race otherwise.
467 Atom actual_type, *atoms;
469 unsigned long nitems, bytes_after;
471 if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
472 False, XA_ATOM, &actual_type, &actual_format,
473 &nitems, &bytes_after, (unsigned char **)&atoms)
477 if (actual_type == XA_ATOM && actual_format == 32)
478 for (int i = 0; i < nitems; i++)
479 if (XGetSelectionOwner (dpy, atoms[i]))
489 void rxvt_display::x_cb (io_watcher &w, short revents)
494 XNextEvent (dpy, &xev);
497 if (!XFilterEvent (&xev, None))
499 if (xev.type == PropertyNotify
500 && xev.xany.window == root
501 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
504 for (int i = xw.size (); i--; )
507 xw.erase_unordered (i);
508 else if (xw[i]->window == xev.xany.window)
515 while (XEventsQueued (dpy, QueuedAlready));
520 void rxvt_display::flush ()
522 if (XEventsQueued (dpy, QueuedAlready))
523 x_cb (x_ev, EVENT_READ);
528 void rxvt_display::reg (xevent_watcher *w)
531 w->active = xw.size ();
534 void rxvt_display::unreg (xevent_watcher *w)
537 xw[w->active - 1] = 0;
540 void rxvt_display::set_selection_owner (rxvt_term *owner)
542 if (selection_owner && selection_owner != owner)
543 selection_owner->selection_clear ();
545 selection_owner = owner;
550 void rxvt_display::reg (im_watcher *w)
555 void rxvt_display::unreg (im_watcher *w)
557 imw.erase (find (imw.begin (), imw.end (), w));
560 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
566 m = strlen (modifiers);
568 if (!(id = (char *)malloc (l + m + 2)))
571 memcpy (id, locale, l); id[l] = '\n';
572 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
574 rxvt_xim *xim = xims.get (id);
581 void rxvt_display::put_xim (rxvt_xim *xim)
583 # if XLIB_IS_RACEFREE
590 Atom rxvt_display::atom (const char *name)
592 return XInternAtom (dpy, name, False);
595 /////////////////////////////////////////////////////////////////////////////
597 template class refcache<rxvt_display>;
598 refcache<rxvt_display> displays;
600 /////////////////////////////////////////////////////////////////////////////
603 rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
606 XRenderPictFormat *format;
608 // FUCKING Xft gets it wrong, of course, so work around it.
609 // Transparency users should eat shit and die, and then
610 // XRenderQueryPictIndexValues themselves plenty.
611 if ((screen->visual->c_class == TrueColor)
612 && (format = XRenderFindVisualFormat (screen->dpy, screen->visual)))
614 // the fun lies in doing everything manually...
615 c.color.red = color.r;
616 c.color.green = color.g;
617 c.color.blue = color.b;
618 c.color.alpha = color.a;
620 c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red )
621 | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green)
622 | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue )
623 | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha);
636 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c);
643 if (screen->visual->c_class == TrueColor)
645 c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask ))
646 / rgba::MAX_CC) << ctz (screen->visual->red_mask )
647 | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask))
648 / rgba::MAX_CC) << ctz (screen->visual->green_mask)
649 | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask ))
650 / rgba::MAX_CC) << ctz (screen->visual->blue_mask );
654 else if (XAllocColor (screen->dpy, screen->cmap, &c))
657 c.pixel = (color.r + color.g + color.b) > 128*3
658 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy))
659 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy));
666 rxvt_color::set (rxvt_screen *screen, const char *name)
672 // parse the nonstandard "[alphapercent]" prefix
673 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip))
675 c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a);
681 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
682 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos))
686 if (XParseColor (screen->dpy, screen->cmap, name, &xc))
698 rxvt_warn ("unable to parse color '%s', using pink instead.\n", name);
702 return set (screen, c);
706 rxvt_color::set (rxvt_screen *screen, const rgba &color)
708 bool got = alloc (screen, color);
711 int cmap_size = screen->visual->map_entries;
714 && screen->visual->c_class == PseudoColor
717 XColor *colors = new XColor [screen->visual->map_entries];
719 for (int i = 0; i < cmap_size; i++)
720 colors [i].pixel = i;
722 // many kilobytes transfer per colour, but pseudocolor isn't worth
723 // many extra optimisations.
724 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size);
726 int diff = 0x7fffffffUL;
727 XColor *best = colors;
729 for (int i = 0; i < cmap_size; i++)
731 int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2))
732 + (squared_diff<int> (color.g >> 2, colors [i].green >> 2))
733 + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2));
742 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n",
743 // color.r, color.g, color.b, best->red, best->green, best->blue, diff);
745 got = alloc (screen, rgba (best->red, best->green, best->blue));
755 rxvt_color::get (rgba &color)
758 color.r = c.color.red;
759 color.g = c.color.green;
760 color.b = c.color.blue;
761 color.a = c.color.alpha;
766 color.a = rgba::MAX_CC;
771 rxvt_color::get (XColor &color)
779 color.pixel = (Pixel)*this;
783 rxvt_color::free (rxvt_screen *screen)
786 XftColorFree (screen->dpy, screen->visual, screen->cmap, &c);
788 XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes);
793 rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to)
801 lerp (c.r, to.r, percent),
802 lerp (c.g, to.g, percent),
803 lerp (c.b, to.b, percent),
804 lerp (c.a, to.a, percent)