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>
37 const char *const xa_names[] =
73 /////////////////////////////////////////////////////////////////////////////
75 refcounted::refcounted (const char *id)
77 this->id = strdup (id);
80 refcounted::~refcounted ()
86 T *refcache<T>::get (const char *id)
88 for (T **i = this->begin (); i < this->end (); ++i)
90 if (!strcmp (id, (*i)->id))
100 if (obj && obj->ref_init ())
103 this->push_back (obj);
114 void refcache<T>::put (T *obj)
119 if (!--obj->referenced)
121 this->erase (find (this->begin (), this->end (), obj));
127 void refcache<T>::clear ()
129 while (this->size ())
130 put (*this->begin ());
133 /////////////////////////////////////////////////////////////////////////////
138 #if XIMCB_PROTO_BROKEN
139 im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
141 im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
144 rxvt_xim *xim = (rxvt_xim *)client_data;
145 rxvt_display *display = xim->display;
149 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
150 display->im_change_cb ();
154 rxvt_xim::ref_init ()
156 display = GET_R->display; //HACK: TODO
158 xim = XOpenIM (display->display, NULL, NULL, NULL);
163 XIMCallback ximcallback;
164 ximcallback.client_data = (XPointer)this;
165 ximcallback.callback = im_destroy_cb;
167 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
172 rxvt_xim::~rxvt_xim ()
180 /////////////////////////////////////////////////////////////////////////////
183 rxvt_screen::set (rxvt_display *disp)
187 xdisp = disp->display;
189 Screen *screen = ScreenOfDisplay (xdisp, disp->screen);
191 depth = DefaultDepthOfScreen (screen);
192 visual = DefaultVisualOfScreen (screen);
193 cmap = DefaultColormapOfScreen (screen);
197 rxvt_screen::set (rxvt_display *disp, int depth)
203 if (XMatchVisualInfo (xdisp, display->screen, depth, TrueColor, &vinfo))
206 this->visual = vinfo.visual;
207 this->cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
212 rxvt_screen::clear ()
214 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
215 XFreeColormap (xdisp, cmap);
218 /////////////////////////////////////////////////////////////////////////////
220 rxvt_display::rxvt_display (const char *id)
222 , x_ev (this, &rxvt_display::x_cb)
223 , selection_owner (0)
228 rxvt_display::get_resources ()
230 char *homedir = (char *)getenv ("HOME");
234 * get resources using the X library function
236 char *displayResource, *xe;
237 XrmDatabase database, rdb1;
241 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
243 // 6. System wide per application default file.
245 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
246 if ((xe = (char *)getenv ("XAPPLRESDIR")))
248 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
250 if ((rdb1 = XrmGetFileDatabase (fname)))
251 XrmMergeDatabases (rdb1, &database);
254 // 5. User's per application default file.
257 // 4. User's defaults file.
258 /* Get any Xserver defaults */
259 displayResource = XResourceManagerString (display);
261 if (displayResource != NULL)
263 if ((rdb1 = XrmGetStringDatabase (displayResource)))
264 XrmMergeDatabases (rdb1, &database);
268 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
270 if ((rdb1 = XrmGetFileDatabase (fname)))
271 XrmMergeDatabases (rdb1, &database);
274 /* Get screen specific resources */
275 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
277 if (displayResource != NULL)
279 if ((rdb1 = XrmGetStringDatabase (displayResource)))
280 /* Merge with screen-independent resources */
281 XrmMergeDatabases (rdb1, &database);
283 XFree (displayResource);
286 // 3. User's per host defaults file
287 /* Add in XENVIRONMENT file */
288 if ((xe = (char *)getenv ("XENVIRONMENT"))
289 && (rdb1 = XrmGetFileDatabase (xe)))
290 XrmMergeDatabases (rdb1, &database);
297 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
299 if ((rdb1 = XrmGetFileDatabase (fname)))
300 XrmMergeDatabases (rdb1, &database);
307 bool rxvt_display::ref_init ()
309 #ifdef LOCAL_X_IS_UNIX
312 val = rxvt_malloc (5 + strlen (id) + 1);
313 strcpy (val, "unix/");
315 display = XOpenDisplay (val);
323 display = XOpenDisplay (id);
328 screen = DefaultScreen (display);
329 root = DefaultRootWindow (display);
331 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
332 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
334 XrmSetDatabase (display, get_resources ());
339 blackcolour.green = 0;
340 blackcolour.blue = 0;
341 Font f = XLoadFont (display, "fixed");
342 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
343 &blackcolour, &blackcolour);
344 XUnloadFont (display, f);
347 int fd = XConnectionNumber (display);
349 #ifndef NO_SLOW_LINK_SUPPORT
350 // try to detect wether we have a local connection.
351 // assume unix domains socket == local, everything else not
352 // TODO: might want to check for inet/127.0.0.1
355 socklen_t sl = sizeof (sa);
357 if (!getsockname (fd, (sockaddr *)&sa, &sl))
358 is_local = sa.sun_family == AF_LOCAL;
361 x_ev.start (fd, EVENT_READ);
362 fcntl (fd, F_SETFD, FD_CLOEXEC);
364 XSelectInput (display, root, PropertyChangeMask);
372 rxvt_display::ref_next ()
374 // TODO: somehow check wether the database files/resources changed
375 // before re-loading/parsing
376 XrmDestroyDatabase (XrmGetDatabase (display));
377 XrmSetDatabase (display, get_resources ());
380 rxvt_display::~rxvt_display ()
386 XFreeCursor (display, blank_cursor);
392 XCloseDisplay (display);
396 void rxvt_display::im_change_cb ()
398 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
402 void rxvt_display::im_change_check ()
404 // try to only call im_change_cb when a new input method
405 // registers, as xlib crashes due to a race otherwise.
406 Atom actual_type, *atoms;
408 unsigned long nitems, bytes_after;
410 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
411 False, XA_ATOM, &actual_type, &actual_format,
412 &nitems, &bytes_after, (unsigned char **)&atoms)
416 if (actual_type == XA_ATOM && actual_format == 32)
417 for (int i = 0; i < nitems; i++)
418 if (XGetSelectionOwner (display, atoms[i]))
428 void rxvt_display::x_cb (io_watcher &w, short revents)
433 XNextEvent (display, &xev);
436 if (!XFilterEvent (&xev, None))
438 if (xev.type == PropertyNotify
439 && xev.xany.window == root
440 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
443 for (int i = xw.size (); i--; )
446 xw.erase_unordered (i);
447 else if (xw[i]->window == xev.xany.window)
454 while (XEventsQueued (display, QueuedAlready));
459 void rxvt_display::flush ()
461 if (XEventsQueued (display, QueuedAlready))
462 x_cb (x_ev, EVENT_READ);
467 void rxvt_display::reg (xevent_watcher *w)
470 w->active = xw.size ();
473 void rxvt_display::unreg (xevent_watcher *w)
476 xw[w->active - 1] = 0;
479 void rxvt_display::set_selection_owner (rxvt_term *owner)
481 if (selection_owner && selection_owner != owner)
482 selection_owner->selection_clear ();
484 selection_owner = owner;
488 void rxvt_display::reg (im_watcher *w)
493 void rxvt_display::unreg (im_watcher *w)
495 imw.erase (find (imw.begin (), imw.end (), w));
498 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
504 m = strlen (modifiers);
506 if (!(id = (char *)malloc (l + m + 2)))
509 memcpy (id, locale, l); id[l] = '\n';
510 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
512 rxvt_xim *xim = xims.get (id);
519 void rxvt_display::put_xim (rxvt_xim *xim)
527 Atom rxvt_display::atom (const char *name)
529 return XInternAtom (display, name, False);
532 /////////////////////////////////////////////////////////////////////////////
534 template class refcache<rxvt_display>;
535 refcache<rxvt_display> displays;
537 /////////////////////////////////////////////////////////////////////////////
540 rxvt_color::set (rxvt_screen *screen, Pixel p)
546 if (!XQueryColor (screen->xdisp, screen->cmap, &xc))
557 XftColorAllocValue (screen->xdisp,
569 rxvt_color::set (rxvt_screen *screen, const char *name)
572 return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c);
576 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
577 return set (screen, xc.red, xc.green, xc.blue);
584 rxvt_color::set (rxvt_screen *screen, unsigned short cr, unsigned short cg, unsigned short cb)
591 xc.flags = DoRed | DoGreen | DoBlue;
593 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
594 return set (screen, xc.pixel);
600 rxvt_color::get (rxvt_screen *screen, unsigned short &cr, unsigned short &cg, unsigned short &cb)
610 XQueryColor (screen->xdisp, screen->cmap, &c);
619 rxvt_color::free (rxvt_screen *screen)
622 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
624 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
629 rxvt_color::fade (rxvt_screen *screen, int percent)
631 percent = 100 - percent;
633 unsigned short cr, cg, cb;
636 get (screen, cr, cg, cb);
648 #define LERP(a,b,p) (a * p + b * (100 - p)) / 100
651 rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
653 percent = 100 - percent;
655 unsigned short cr, cg, cb;
656 unsigned short fcr, fcg, fcb;
659 get (screen, cr, cg, cb);
660 fadeto.get (screen, fcr, fcg, fcb);
664 LERP (cr, fcr, percent),
665 LERP (cg, fcg, percent),
666 LERP (cb, fcb, percent)