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 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;
513 void rxvt_display::reg (im_watcher *w)
518 void rxvt_display::unreg (im_watcher *w)
520 imw.erase (find (imw.begin (), imw.end (), w));
523 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
529 m = strlen (modifiers);
531 if (!(id = (char *)malloc (l + m + 2)))
534 memcpy (id, locale, l); id[l] = '\n';
535 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
537 rxvt_xim *xim = xims.get (id);
544 void rxvt_display::put_xim (rxvt_xim *xim)
552 Atom rxvt_display::atom (const char *name)
554 return XInternAtom (display, name, False);
557 /////////////////////////////////////////////////////////////////////////////
559 template class refcache<rxvt_display>;
560 refcache<rxvt_display> displays;
562 /////////////////////////////////////////////////////////////////////////////
565 rxvt_color::alloc (rxvt_screen *screen, rxvt_rgba rgba)
568 XRenderPictFormat *format;
570 // FUCKING Xft gets it wrong, of course, so work around it
571 // transparency users should eat shit and die, and then
572 // XRenderQueryPictIndexValues themselves plenty.
573 if ((screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor)
574 && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual)))
576 // the fun lies in doing everything manually...
577 c.color.red = rgba.r;
578 c.color.green = rgba.g;
579 c.color.blue = rgba.b;
580 c.color.alpha = rgba.a;
582 c.pixel = ((rgba.r * format->direct.redMask / rxvt_rgba::MAX_CC) << format->direct.red )
583 | ((rgba.g * format->direct.greenMask / rxvt_rgba::MAX_CC) << format->direct.green)
584 | ((rgba.b * format->direct.blueMask / rxvt_rgba::MAX_CC) << format->direct.blue )
585 | ((rgba.a * format->direct.alphaMask / rxvt_rgba::MAX_CC) << format->direct.alpha);
598 return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c);
601 if (screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor)
603 p = (rgba.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask ))
604 / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask )
605 | (rgba.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask))
606 / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask)
607 | (rgba.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask ))
608 / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask );
620 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
626 p = (rgba.r + rgba.g + rgba.b) > 128*3
627 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp))
628 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp));
636 rxvt_color::set (rxvt_screen *screen, const char *name)
642 if (1 <= sscanf (name, "[%hx]%n", &c.a, &skip))
646 case 2 + 1: c.a *= rxvt_rgba::MAX_CC / 0x000f; break;
647 case 2 + 2: c.a *= rxvt_rgba::MAX_CC / 0x00ff; break;
648 case 2 + 3: c.a *= rxvt_rgba::MAX_CC / 0x0fff; break;
649 case 2 + 4: c.a *= rxvt_rgba::MAX_CC / 0xffff; break;
655 c.a = rxvt_rgba::MAX_CC;
657 // parse the non-standard rgba format
658 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &c.r, &c.g, &c.b, &c.a, &eos))
662 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
674 rxvt_warn ("unable to parse color '%s', using pink instead.\n", name);
678 return set (screen, c);
682 rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba)
684 bool got = alloc (screen, rgba);
687 int cmap_size = screen->visual->map_entries;
690 && screen->visual->c_class == PseudoColor
693 XColor *colors = new XColor [screen->visual->map_entries];
695 for (int i = 0; i < cmap_size; i++)
696 colors [i].pixel = i;
698 XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size);
700 int diff = 0x7fffffffUL;
701 XColor *best = colors;
703 for (int i = 0; i < cmap_size; i++)
705 int d = (squared_diff<int> (rgba.r >> 2, colors [i].red >> 2))
706 + (squared_diff<int> (rgba.g >> 2, colors [i].green >> 2))
707 + (squared_diff<int> (rgba.b >> 2, colors [i].blue >> 2));
716 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n",
717 // rgba.r, rgba.g, rgba.b, best->red, best->green, best->blue, diff);
719 got = alloc (screen, rxvt_rgba (best->red, best->green, best->blue));
729 rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba)
732 rgba.r = c.color.red;
733 rgba.g = c.color.green;
734 rgba.b = c.color.blue;
735 rgba.a = c.color.alpha;
740 XQueryColor (screen->xdisp, screen->cmap, &c);
745 rgba.a = rxvt_rgba::MAX_CC;
750 rxvt_color::free (rxvt_screen *screen)
753 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
755 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
760 rxvt_color::fade (rxvt_screen *screen, int percent)
767 c.r = lerp (0, c.r, percent);
768 c.g = lerp (0, c.g, percent);
769 c.b = lerp (0, c.b, percent);
771 faded.set (screen, c);
777 rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
783 fadeto.get (screen, fc);
788 lerp (fc.r, c.r, percent),
789 lerp (fc.g, c.g, percent),
790 lerp (fc.b, c.b, percent),
791 lerp (fc.a, c.a, percent)