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",
89 /////////////////////////////////////////////////////////////////////////////
91 refcounted::refcounted (const char *id)
93 this->id = strdup (id);
96 refcounted::~refcounted ()
102 T *refcache<T>::get (const char *id)
104 for (T **i = this->begin (); i < this->end (); ++i)
106 if (!strcmp (id, (*i)->id))
116 if (obj && obj->ref_init ())
119 this->push_back (obj);
130 void refcache<T>::put (T *obj)
135 if (!--obj->referenced)
137 this->erase (find (this->begin (), this->end (), obj));
143 void refcache<T>::clear ()
145 while (this->size ())
146 put (*this->begin ());
149 /////////////////////////////////////////////////////////////////////////////
154 #if XIMCB_PROTO_BROKEN
155 im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
157 im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
160 rxvt_xim *xim = (rxvt_xim *)client_data;
161 rxvt_display *display = xim->display;
165 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
166 display->im_change_cb ();
170 rxvt_xim::ref_init ()
172 display = GET_R->display; //HACK: TODO
174 xim = XOpenIM (display->display, NULL, NULL, NULL);
179 XIMCallback ximcallback;
180 ximcallback.client_data = (XPointer)this;
181 ximcallback.callback = im_destroy_cb;
183 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
188 rxvt_xim::~rxvt_xim ()
196 /////////////////////////////////////////////////////////////////////////////
199 rxvt_screen::set (rxvt_display *disp)
202 xdisp = disp->display;
204 Screen *screen = ScreenOfDisplay (xdisp, disp->screen);
206 depth = DefaultDepthOfScreen (screen);
207 visual = DefaultVisualOfScreen (screen);
208 cmap = DefaultColormapOfScreen (screen);
212 rxvt_screen::set (rxvt_display *disp, int bitdepth)
219 if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo))
222 visual = vinfo.visual;
223 cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
229 rxvt_screen::clear ()
231 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
232 XFreeColormap (xdisp, cmap);
235 /////////////////////////////////////////////////////////////////////////////
237 rxvt_display::rxvt_display (const char *id)
239 , x_ev (this, &rxvt_display::x_cb)
240 , selection_owner (0)
245 rxvt_display::get_resources ()
247 char *homedir = (char *)getenv ("HOME");
251 * get resources using the X library function
253 char *displayResource, *xe;
254 XrmDatabase database, rdb1;
258 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
260 // 6. System wide per application default file.
262 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
263 if ((xe = (char *)getenv ("XAPPLRESDIR")))
265 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
267 if ((rdb1 = XrmGetFileDatabase (fname)))
268 XrmMergeDatabases (rdb1, &database);
271 // 5. User's per application default file.
274 // 4. User's defaults file.
275 /* Get any Xserver defaults */
276 displayResource = XResourceManagerString (display);
278 if (displayResource != NULL)
280 if ((rdb1 = XrmGetStringDatabase (displayResource)))
281 XrmMergeDatabases (rdb1, &database);
285 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
287 if ((rdb1 = XrmGetFileDatabase (fname)))
288 XrmMergeDatabases (rdb1, &database);
291 /* Get screen specific resources */
292 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
294 if (displayResource != NULL)
296 if ((rdb1 = XrmGetStringDatabase (displayResource)))
297 /* Merge with screen-independent resources */
298 XrmMergeDatabases (rdb1, &database);
300 XFree (displayResource);
303 // 3. User's per host defaults file
304 /* Add in XENVIRONMENT file */
305 if ((xe = (char *)getenv ("XENVIRONMENT"))
306 && (rdb1 = XrmGetFileDatabase (xe)))
307 XrmMergeDatabases (rdb1, &database);
314 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
316 if ((rdb1 = XrmGetFileDatabase (fname)))
317 XrmMergeDatabases (rdb1, &database);
324 bool rxvt_display::ref_init ()
326 #ifdef LOCAL_X_IS_UNIX
329 val = rxvt_malloc (5 + strlen (id) + 1);
330 strcpy (val, "unix/");
332 display = XOpenDisplay (val);
340 display = XOpenDisplay (id);
345 screen = DefaultScreen (display);
346 root = DefaultRootWindow (display);
348 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
349 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
351 XrmSetDatabase (display, get_resources ());
356 blackcolour.green = 0;
357 blackcolour.blue = 0;
358 Font f = XLoadFont (display, "fixed");
359 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
360 &blackcolour, &blackcolour);
361 XUnloadFont (display, f);
364 int fd = XConnectionNumber (display);
366 #ifndef NO_SLOW_LINK_SUPPORT
367 // try to detect wether we have a local connection.
368 // assume unix domains socket == local, everything else not
369 // TODO: might want to check for inet/127.0.0.1
372 socklen_t sl = sizeof (sa);
374 if (!getsockname (fd, (sockaddr *)&sa, &sl))
375 is_local = sa.sun_family == AF_LOCAL;
378 x_ev.start (fd, EVENT_READ);
379 fcntl (fd, F_SETFD, FD_CLOEXEC);
381 XSelectInput (display, root, PropertyChangeMask);
389 rxvt_display::ref_next ()
391 // TODO: somehow check wether the database files/resources changed
392 // before re-loading/parsing
393 XrmDestroyDatabase (XrmGetDatabase (display));
394 XrmSetDatabase (display, get_resources ());
397 rxvt_display::~rxvt_display ()
403 XFreeCursor (display, blank_cursor);
409 XCloseDisplay (display);
413 void rxvt_display::im_change_cb ()
415 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
419 void rxvt_display::im_change_check ()
421 // try to only call im_change_cb when a new input method
422 // registers, as xlib crashes due to a race otherwise.
423 Atom actual_type, *atoms;
425 unsigned long nitems, bytes_after;
427 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
428 False, XA_ATOM, &actual_type, &actual_format,
429 &nitems, &bytes_after, (unsigned char **)&atoms)
433 if (actual_type == XA_ATOM && actual_format == 32)
434 for (int i = 0; i < nitems; i++)
435 if (XGetSelectionOwner (display, atoms[i]))
445 void rxvt_display::x_cb (io_watcher &w, short revents)
450 XNextEvent (display, &xev);
453 if (!XFilterEvent (&xev, None))
455 if (xev.type == PropertyNotify
456 && xev.xany.window == root
457 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
460 for (int i = xw.size (); i--; )
463 xw.erase_unordered (i);
464 else if (xw[i]->window == xev.xany.window)
471 while (XEventsQueued (display, QueuedAlready));
476 void rxvt_display::flush ()
478 if (XEventsQueued (display, QueuedAlready))
479 x_cb (x_ev, EVENT_READ);
484 void rxvt_display::reg (xevent_watcher *w)
487 w->active = xw.size ();
490 void rxvt_display::unreg (xevent_watcher *w)
493 xw[w->active - 1] = 0;
496 void rxvt_display::set_selection_owner (rxvt_term *owner)
498 if (selection_owner && selection_owner != owner)
499 selection_owner->selection_clear ();
501 selection_owner = owner;
505 void rxvt_display::reg (im_watcher *w)
510 void rxvt_display::unreg (im_watcher *w)
512 imw.erase (find (imw.begin (), imw.end (), w));
515 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
521 m = strlen (modifiers);
523 if (!(id = (char *)malloc (l + m + 2)))
526 memcpy (id, locale, l); id[l] = '\n';
527 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
529 rxvt_xim *xim = xims.get (id);
536 void rxvt_display::put_xim (rxvt_xim *xim)
544 Atom rxvt_display::atom (const char *name)
546 return XInternAtom (display, name, False);
549 /////////////////////////////////////////////////////////////////////////////
551 template class refcache<rxvt_display>;
552 refcache<rxvt_display> displays;
554 /////////////////////////////////////////////////////////////////////////////
557 rxvt_color::set (rxvt_screen *screen, const char *name)
560 int l = strlen (name);
565 // shortcutting this saves countless server RTTs for the built-in colours
566 if (l == 3+3*3 && 3 == sscanf (name, "rgb:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos))
568 r.a = rxvt_rgba::MAX_CC;
569 mult = rxvt_rgba::MAX_CC / 0x00ff;
572 // parse a number of non-standard ARGB colour specifications
573 else if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
574 mult = rxvt_rgba::MAX_CC / 0x000f;
575 else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
576 mult = rxvt_rgba::MAX_CC / 0x00ff;
577 else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
578 mult = rxvt_rgba::MAX_CC / 0xffff;
579 else if (l == 4+5*4 && 4 == sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos))
580 mult = rxvt_rgba::MAX_CC / 0xffff;
582 // slow case: server round trip
584 return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c);
586 r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult;
588 return set (screen, r);
592 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
593 return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue));
600 rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba)
603 XRenderPictFormat *format;
605 // FUCKING Xft gets it wrong, of course, so work around it
606 // transparency users should eat shit and die, and then
607 // XRenderQueryPictIndexValues themselves plenty.
608 if (screen->visual->c_class == TrueColor
609 && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual)))
611 // the fun lies in doing everything manually...
612 c.color.red = rgba.r;
613 c.color.green = rgba.g;
614 c.color.blue = rgba.b;
615 c.color.alpha = rgba.a;
617 c.pixel = ((rgba.r * format->direct.redMask / rxvt_rgba::MAX_CC) << format->direct.red )
618 | ((rgba.g * format->direct.greenMask / rxvt_rgba::MAX_CC) << format->direct.green)
619 | ((rgba.b * format->direct.blueMask / rxvt_rgba::MAX_CC) << format->direct.blue )
620 | ((rgba.a * format->direct.alphaMask / rxvt_rgba::MAX_CC) << format->direct.alpha);
633 return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c);
638 if (screen->visual->c_class == TrueColor)
640 p = ((rgba.r * ((1 << screen->visual->bits_per_rgb) - 1)
641 / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask ))
642 | ((rgba.g * ((1 << screen->visual->bits_per_rgb) - 1)
643 / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask))
644 | ((rgba.b * ((1 << screen->visual->bits_per_rgb) - 1)
645 / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask ));
656 xc.flags = DoRed | DoGreen | DoBlue;
658 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
670 rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba)
673 rgba.r = c.color.red;
674 rgba.g = c.color.green;
675 rgba.b = c.color.blue;
676 rgba.a = c.color.alpha;
681 XQueryColor (screen->xdisp, screen->cmap, &c);
686 rgba.a = rxvt_rgba::MAX_CC;
691 rxvt_color::free (rxvt_screen *screen)
694 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
696 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
701 rxvt_color::fade (rxvt_screen *screen, int percent)
708 c.r = lerp (0, c.r, percent);
709 c.g = lerp (0, c.g, percent);
710 c.b = lerp (0, c.b, percent);
712 faded.set (screen, c);
718 rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
724 fadeto.get (screen, fc);
729 lerp (fc.r, c.r, percent),
730 lerp (fc.g, c.g, percent),
731 lerp (fc.b, c.b, percent),
732 lerp (fc.a, c.a, percent)