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 refcounted::refcounted (const char *id)
39 this->id = strdup (id);
42 refcounted::~refcounted ()
48 T *refcache<T>::get (const char *id)
50 for (T **i = this->begin (); i < this->end (); ++i)
52 if (!strcmp (id, (*i)->id))
62 if (obj && obj->ref_init ())
65 this->push_back (obj);
76 void refcache<T>::put (T *obj)
81 if (!--obj->referenced)
83 this->erase (find (this->begin (), this->end (), obj));
89 void refcache<T>::clear ()
92 put (*this->begin ());
95 /////////////////////////////////////////////////////////////////////////////
99 #if XIMCB_PROTO_BROKEN
100 im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
102 im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
105 rxvt_xim *xim = (rxvt_xim *)client_data;
106 rxvt_display *display = xim->display;
110 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
111 display->im_change_cb ();
115 rxvt_xim::ref_init ()
117 display = GET_R->display; //HACK: TODO
119 xim = XOpenIM (display->display, NULL, NULL, NULL);
124 XIMCallback ximcallback;
125 ximcallback.client_data = (XPointer)this;
126 ximcallback.callback = im_destroy_cb;
128 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
133 rxvt_xim::~rxvt_xim ()
140 /////////////////////////////////////////////////////////////////////////////
142 rxvt_display::rxvt_display (const char *id)
144 , x_ev (this, &rxvt_display::x_cb)
145 , selection_owner (0)
150 rxvt_display::get_resources ()
152 char *homedir = (char *)getenv ("HOME");
156 * get resources using the X library function
158 char *displayResource, *xe;
159 XrmDatabase database, rdb1;
163 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
165 // 6. System wide per application default file.
167 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
168 if ((xe = (char *)getenv ("XAPPLRESDIR")))
170 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
172 if ((rdb1 = XrmGetFileDatabase (fname)))
173 XrmMergeDatabases (rdb1, &database);
176 // 5. User's per application default file.
179 // 4. User's defaults file.
180 /* Get any Xserver defaults */
181 displayResource = XResourceManagerString (display);
183 if (displayResource != NULL)
185 if ((rdb1 = XrmGetStringDatabase (displayResource)))
186 XrmMergeDatabases (rdb1, &database);
190 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
192 if ((rdb1 = XrmGetFileDatabase (fname)))
193 XrmMergeDatabases (rdb1, &database);
196 /* Get screen specific resources */
197 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
199 if (displayResource != NULL)
201 if ((rdb1 = XrmGetStringDatabase (displayResource)))
202 /* Merge with screen-independent resources */
203 XrmMergeDatabases (rdb1, &database);
205 XFree (displayResource);
208 // 3. User's per host defaults file
209 /* Add in XENVIRONMENT file */
210 if ((xe = (char *)getenv ("XENVIRONMENT"))
211 && (rdb1 = XrmGetFileDatabase (xe)))
212 XrmMergeDatabases (rdb1, &database);
219 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
221 if ((rdb1 = XrmGetFileDatabase (fname)))
222 XrmMergeDatabases (rdb1, &database);
229 bool rxvt_display::ref_init ()
231 #ifdef LOCAL_X_IS_UNIX
234 val = rxvt_malloc (5 + strlen (id) + 1);
235 strcpy (val, "unix/");
237 display = XOpenDisplay (val);
245 display = XOpenDisplay (id);
250 screen = DefaultScreen (display);
251 root = DefaultRootWindow (display);
252 visual = DefaultVisual (display, screen);
253 cmap = DefaultColormap (display, screen);
254 depth = DefaultDepth (display, screen);
256 XrmSetDatabase (display, get_resources ());
261 blackcolour.green = 0;
262 blackcolour.blue = 0;
263 Font f = XLoadFont (display, "fixed");
264 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
265 &blackcolour, &blackcolour);
266 XUnloadFont (display, f);
271 * If depth is not 24, look for a 24bit visual.
277 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
280 visual = vinfo.visual;
281 cmap = XCreateColormap (display,
282 RootWindow (display, screen),
288 int fd = XConnectionNumber (display);
290 #ifndef NO_SLOW_LINK_SUPPORT
291 // try to detect wether we have a local connection.
292 // assume unix domains socket == local, everything else not
293 // TODO: might want to check for inet/127.0.0.1
296 socklen_t sl = sizeof (sa);
298 if (!getsockname (fd, (sockaddr *)&sa, &sl))
299 is_local = sa.sun_family == AF_LOCAL;
302 x_ev.start (fd, EVENT_READ);
303 fcntl (fd, F_SETFD, FD_CLOEXEC);
305 XSelectInput (display, root, PropertyChangeMask);
307 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
316 rxvt_display::ref_next ()
318 // TODO: somehow check wether the database files/resources changed
319 // before re-loading/parsing
320 XrmDestroyDatabase (XrmGetDatabase (display));
321 XrmSetDatabase (display, get_resources ());
324 rxvt_display::~rxvt_display ()
330 XFreeCursor (display, blank_cursor);
336 XCloseDisplay (display);
340 void rxvt_display::im_change_cb ()
342 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
346 void rxvt_display::im_change_check ()
348 // try to only call im_change_cb when a new input method
349 // registers, as xlib crashes due to a race otherwise.
350 Atom actual_type, *atoms;
352 unsigned long nitems, bytes_after;
354 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L,
355 False, XA_ATOM, &actual_type, &actual_format,
356 &nitems, &bytes_after, (unsigned char **)&atoms)
360 if (actual_type == XA_ATOM && actual_format == 32)
361 for (int i = 0; i < nitems; i++)
362 if (XGetSelectionOwner (display, atoms[i]))
372 void rxvt_display::x_cb (io_watcher &w, short revents)
377 XNextEvent (display, &xev);
380 if (!XFilterEvent (&xev, None))
382 if (xev.type == PropertyNotify
383 && xev.xany.window == root
384 && xev.xproperty.atom == xa_xim_servers)
387 for (int i = xw.size (); i--; )
390 xw.erase_unordered (i);
391 else if (xw[i]->window == xev.xany.window)
398 while (XEventsQueued (display, QueuedAlready));
403 void rxvt_display::flush ()
405 if (XEventsQueued (display, QueuedAlready))
406 x_cb (x_ev, EVENT_READ);
411 void rxvt_display::reg (xevent_watcher *w)
414 w->active = xw.size ();
417 void rxvt_display::unreg (xevent_watcher *w)
420 xw[w->active - 1] = 0;
423 void rxvt_display::set_selection_owner (rxvt_term *owner)
425 if (selection_owner && selection_owner != owner)
426 selection_owner->selection_clear ();
428 selection_owner = owner;
432 void rxvt_display::reg (im_watcher *w)
437 void rxvt_display::unreg (im_watcher *w)
439 imw.erase (find (imw.begin (), imw.end (), w));
442 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
448 m = strlen (modifiers);
450 if (!(id = (char *)malloc (l + m + 2)))
453 memcpy (id, locale, l); id[l] = '\n';
454 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
456 rxvt_xim *xim = xims.get (id);
463 void rxvt_display::put_xim (rxvt_xim *xim)
471 Atom rxvt_display::atom (const char *name)
473 return XInternAtom (display, name, False);
476 /////////////////////////////////////////////////////////////////////////////
478 template class refcache<rxvt_display>;
479 refcache<rxvt_display> displays;
481 /////////////////////////////////////////////////////////////////////////////
484 rxvt_color::set (rxvt_display *display, Pixel p)
490 if (!XQueryColor (display->display, display->cmap, &xc))
501 XftColorAllocValue (display->display,
513 rxvt_color::set (rxvt_display *display, const char *name)
516 return XftColorAllocName (display->display, display->visual, display->cmap,
521 if (XParseColor (display->display, display->cmap, name, &xc))
522 return set (display, xc.red, xc.green, xc.blue);
529 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
536 xc.flags = DoRed | DoGreen | DoBlue;
538 if (XAllocColor (display->display, display->cmap, &xc))
539 return set (display, xc.pixel);
545 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
555 XQueryColor (display->display, display->cmap, &c);
564 rxvt_color::free (rxvt_display *display)
567 XftColorFree (display->display, display->visual, display->cmap, &c);
569 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
574 rxvt_color::fade (rxvt_display *display, int percent)
576 percent = 100 - percent;
578 unsigned short cr, cg, cb;
581 get (display, cr, cg, cb);
593 #define LERP(a,b,p) (a * p + b * (100 - p)) / 100
596 rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto)
598 percent = 100 - percent;
600 unsigned short cr, cg, cb;
601 unsigned short fcr, fcg, fcb;
604 get (display, cr, cg, cb);
605 fadeto.get(display, fcr, fcg, fcb);
609 LERP (cr, fcr, percent),
610 LERP (cg, fcg, percent),
611 LERP (cb, fcb, percent)