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->display, NULL, NULL, NULL);
187 XIMCallback ximcallback;
188 ximcallback.client_data = (XPointer)this;
189 ximcallback.callback = im_destroy_cb;
191 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
196 rxvt_xim::~rxvt_xim ()
204 /////////////////////////////////////////////////////////////////////////////
207 rxvt_screen::set (rxvt_display *disp)
210 xdisp = disp->display;
212 Screen *screen = ScreenOfDisplay (xdisp, 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 (xdisp, display->screen, bitdepth, TrueColor, &vinfo))
230 visual = vinfo.visual;
231 cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
237 rxvt_screen::clear ()
239 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
240 XFreeColormap (xdisp, 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 ()
255 char *homedir = (char *)getenv ("HOME");
259 * get resources using the X library function
261 char *displayResource, *xe;
262 XrmDatabase database, rdb1;
266 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
268 // 6. System wide per application default file.
270 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
271 if ((xe = (char *)getenv ("XAPPLRESDIR")))
273 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
275 if ((rdb1 = XrmGetFileDatabase (fname)))
276 XrmMergeDatabases (rdb1, &database);
279 // 5. User's per application default file.
282 // 4. User's defaults file.
283 /* Get any Xserver defaults */
284 displayResource = XResourceManagerString (display);
286 if (displayResource != NULL)
288 if ((rdb1 = XrmGetStringDatabase (displayResource)))
289 XrmMergeDatabases (rdb1, &database);
293 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
295 if ((rdb1 = XrmGetFileDatabase (fname)))
296 XrmMergeDatabases (rdb1, &database);
299 /* Get screen specific resources */
300 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
302 if (displayResource != NULL)
304 if ((rdb1 = XrmGetStringDatabase (displayResource)))
305 /* Merge with screen-independent resources */
306 XrmMergeDatabases (rdb1, &database);
308 XFree (displayResource);
311 // 3. User's per host defaults file
312 /* Add in XENVIRONMENT file */
313 if ((xe = (char *)getenv ("XENVIRONMENT"))
314 && (rdb1 = XrmGetFileDatabase (xe)))
315 XrmMergeDatabases (rdb1, &database);
322 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
324 if ((rdb1 = XrmGetFileDatabase (fname)))
325 XrmMergeDatabases (rdb1, &database);
332 bool rxvt_display::ref_init ()
334 #ifdef LOCAL_X_IS_UNIX
337 val = rxvt_malloc (5 + strlen (id) + 1);
338 strcpy (val, "unix/");
340 display = XOpenDisplay (val);
348 display = XOpenDisplay (id);
353 screen = DefaultScreen (display);
354 root = DefaultRootWindow (display);
356 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
357 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
359 XrmSetDatabase (display, get_resources ());
364 blackcolour.green = 0;
365 blackcolour.blue = 0;
366 Font f = XLoadFont (display, "fixed");
367 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
368 &blackcolour, &blackcolour);
369 XUnloadFont (display, f);
372 int fd = XConnectionNumber (display);
374 #ifndef NO_SLOW_LINK_SUPPORT
375 // try to detect wether we have a local connection.
376 // assume unix domains socket == local, everything else not
377 // TODO: might want to check for inet/127.0.0.1
380 socklen_t sl = sizeof (sa);
382 if (!getsockname (fd, (sockaddr *)&sa, &sl))
383 is_local = sa.sun_family == AF_LOCAL;
386 x_ev.start (fd, EVENT_READ);
387 fcntl (fd, F_SETFD, FD_CLOEXEC);
389 XSelectInput (display, root, PropertyChangeMask);
397 rxvt_display::ref_next ()
399 // TODO: somehow check wether the database files/resources changed
400 // before affording re-loading/parsing
401 XrmDestroyDatabase (XrmGetDatabase (display));
402 XrmSetDatabase (display, get_resources ());
405 rxvt_display::~rxvt_display ()
411 XFreeCursor (display, blank_cursor);
417 XCloseDisplay (display);
421 void rxvt_display::im_change_cb ()
423 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
427 void rxvt_display::im_change_check ()
429 // try to only call im_change_cb when a new input method
430 // registers, as xlib crashes due to a race otherwise.
431 Atom actual_type, *atoms;
433 unsigned long nitems, bytes_after;
435 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
436 False, XA_ATOM, &actual_type, &actual_format,
437 &nitems, &bytes_after, (unsigned char **)&atoms)
441 if (actual_type == XA_ATOM && actual_format == 32)
442 for (int i = 0; i < nitems; i++)
443 if (XGetSelectionOwner (display, atoms[i]))
453 void rxvt_display::x_cb (io_watcher &w, short revents)
458 XNextEvent (display, &xev);
461 if (!XFilterEvent (&xev, None))
463 if (xev.type == PropertyNotify
464 && xev.xany.window == root
465 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
468 for (int i = xw.size (); i--; )
471 xw.erase_unordered (i);
472 else if (xw[i]->window == xev.xany.window)
479 while (XEventsQueued (display, QueuedAlready));
484 void rxvt_display::flush ()
486 if (XEventsQueued (display, QueuedAlready))
487 x_cb (x_ev, EVENT_READ);
492 void rxvt_display::reg (xevent_watcher *w)
495 w->active = xw.size ();
498 void rxvt_display::unreg (xevent_watcher *w)
501 xw[w->active - 1] = 0;
504 void rxvt_display::set_selection_owner (rxvt_term *owner)
506 if (selection_owner && selection_owner != owner)
507 selection_owner->selection_clear ();
509 selection_owner = owner;
514 void rxvt_display::reg (im_watcher *w)
519 void rxvt_display::unreg (im_watcher *w)
521 imw.erase (find (imw.begin (), imw.end (), w));
524 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
530 m = strlen (modifiers);
532 if (!(id = (char *)malloc (l + m + 2)))
535 memcpy (id, locale, l); id[l] = '\n';
536 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
538 rxvt_xim *xim = xims.get (id);
545 void rxvt_display::put_xim (rxvt_xim *xim)
547 # if XLIB_IS_RACEFREE
554 Atom rxvt_display::atom (const char *name)
556 return XInternAtom (display, name, False);
559 /////////////////////////////////////////////////////////////////////////////
561 template class refcache<rxvt_display>;
562 refcache<rxvt_display> displays;
564 /////////////////////////////////////////////////////////////////////////////
567 rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
570 XRenderPictFormat *format;
572 // FUCKING Xft gets it wrong, of course, so work around it.
573 // Transparency users should eat shit and die, and then
574 // XRenderQueryPictIndexValues themselves plenty.
575 if ((screen->visual->c_class == TrueColor)
576 && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual)))
578 // the fun lies in doing everything manually...
579 c.color.red = color.r;
580 c.color.green = color.g;
581 c.color.blue = color.b;
582 c.color.alpha = color.a;
584 c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red )
585 | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green)
586 | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue )
587 | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha);
600 return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c);
607 if (screen->visual->c_class == TrueColor)
609 c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask ))
610 / rgba::MAX_CC) << ctz (screen->visual->red_mask )
611 | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask))
612 / rgba::MAX_CC) << ctz (screen->visual->green_mask)
613 | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask ))
614 / rgba::MAX_CC) << ctz (screen->visual->blue_mask );
618 else if (XAllocColor (screen->xdisp, screen->cmap, &c))
621 c.pixel = (color.r + color.g + color.b) > 128*3
622 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp))
623 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp));
630 rxvt_color::set (rxvt_screen *screen, const char *name)
636 // parse the nonstandard "[alphapercent]" prefix
637 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip))
639 c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a);
645 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
646 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos))
650 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
662 rxvt_warn ("unable to parse color '%s', using pink instead.\n", name);
666 return set (screen, c);
670 rxvt_color::set (rxvt_screen *screen, const rgba &color)
672 bool got = alloc (screen, color);
675 int cmap_size = screen->visual->map_entries;
678 && screen->visual->c_class == PseudoColor
681 XColor *colors = new XColor [screen->visual->map_entries];
683 for (int i = 0; i < cmap_size; i++)
684 colors [i].pixel = i;
686 // many kilobytes transfer per colour, but pseudocolor isn't worth
687 // many extra optimisations.
688 XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size);
690 int diff = 0x7fffffffUL;
691 XColor *best = colors;
693 for (int i = 0; i < cmap_size; i++)
695 int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2))
696 + (squared_diff<int> (color.g >> 2, colors [i].green >> 2))
697 + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2));
706 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n",
707 // color.r, color.g, color.b, best->red, best->green, best->blue, diff);
709 got = alloc (screen, rgba (best->red, best->green, best->blue));
719 rxvt_color::get (rgba &color)
722 color.r = c.color.red;
723 color.g = c.color.green;
724 color.b = c.color.blue;
725 color.a = c.color.alpha;
730 color.a = rgba::MAX_CC;
735 rxvt_color::get (XColor &color)
743 color.pixel = (Pixel)*this;
747 rxvt_color::free (rxvt_screen *screen)
750 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
752 XFreeColors (screen->xdisp, screen->cmap, &c.pixel, 1, AllPlanes);
757 rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to)
765 lerp (c.r, to.r, percent),
766 lerp (c.g, to.g, percent),
767 lerp (c.b, to.b, percent),
768 lerp (c.a, to.a, percent)