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 #ifndef NO_SLOW_LINK_SUPPORT
31 # include <sys/socket.h>
35 refcounted::refcounted (const char *id)
37 this->id = strdup (id);
40 refcounted::~refcounted ()
46 T *refcache<T>::get (const char *id)
48 for (T **i = this->begin (); i < this->end (); ++i)
50 if (!strcmp (id, (*i)->id))
61 if (obj && obj->init ())
63 this->push_back (obj);
74 void refcache<T>::put (T *obj)
79 if (!--obj->referenced)
81 this->erase (find (this->begin (), this->end (), obj));
87 refcache<T>::~refcache ()
90 put (*this->begin ());
93 /////////////////////////////////////////////////////////////////////////////
97 #if XIMCB_PROTO_BROKEN
98 im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
100 im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
103 rxvt_xim *xim = (rxvt_xim *)client_data;
104 rxvt_display *display = xim->display;
108 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
109 display->im_change_cb ();
112 bool rxvt_xim::init ()
114 display = GET_R->display; //HACK: TODO
116 xim = XOpenIM (display->display, NULL, NULL, NULL);
121 XIMCallback ximcallback;
122 ximcallback.client_data = (XPointer)this;
123 ximcallback.callback = im_destroy_cb;
125 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
130 rxvt_xim::~rxvt_xim ()
137 /////////////////////////////////////////////////////////////////////////////
139 rxvt_display::rxvt_display (const char *id)
141 , x_ev (this, &rxvt_display::x_cb)
142 , selection_owner (0)
146 bool rxvt_display::init ()
148 display = XOpenDisplay (id);
153 screen = DefaultScreen (display);
154 root = DefaultRootWindow (display);
155 visual = DefaultVisual (display, screen);
156 cmap = DefaultColormap (display, screen);
157 depth = DefaultDepth (display, screen);
159 int fd = XConnectionNumber (display);
161 #ifndef NO_SLOW_LINK_SUPPORT
162 // try to detetc wether we have a local connection.
163 // assume unix domains socket == local, everything else not
164 // TODO: might want to check for inet/127.0.0.1
167 socklen_t sl = sizeof (sa);
169 if (!getsockname (fd, (sockaddr *)&sa, &sl))
170 is_local = sa.sun_family == AF_LOCAL;
175 * If depth is not 24, look for a 24bit visual.
181 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
184 visual = vinfo.visual;
185 cmap = XCreateColormap (display,
186 RootWindow (display, screen),
192 x_ev.start (fd, EVENT_READ);
193 fcntl (fd, F_SETFD, FD_CLOEXEC);
195 XSelectInput (display, root, PropertyChangeMask);
197 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
205 rxvt_display::~rxvt_display ()
210 XCloseDisplay (display);
214 void rxvt_display::im_change_cb ()
216 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
220 void rxvt_display::im_change_check ()
222 // make sure we only call im_change_cb when a new input method
223 // registers, as xlib crashes due to a race otherwise.
224 Atom actual_type, *atoms;
226 unsigned long nitems, bytes_after;
228 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L,
229 False, XA_ATOM, &actual_type, &actual_format,
230 &nitems, &bytes_after, (unsigned char **)&atoms)
234 if (actual_type == XA_ATOM && actual_format == 32)
235 for (int i = 0; i < nitems; i++)
236 if (XGetSelectionOwner (display, atoms[i]))
246 void rxvt_display::x_cb (io_watcher &w, short revents)
251 XNextEvent (display, &xev);
254 if (!XFilterEvent (&xev, None))
257 if (xev.type == PropertyNotify
258 && xev.xany.window == root
259 && xev.xproperty.atom == xa_xim_servers)
262 for (int i = xw.size (); i--; )
265 xw.erase_unordered (i);
266 else if (xw[i]->window == xev.xany.window)
273 while (XPending (display));
278 void rxvt_display::flush ()
282 if (!XPending (display))
289 void rxvt_display::reg (xevent_watcher *w)
292 w->active = xw.size ();
295 void rxvt_display::unreg (xevent_watcher *w)
298 xw[w->active - 1] = 0;
301 void rxvt_display::set_selection_owner (rxvt_term *owner)
303 if (selection_owner && selection_owner != owner)
304 selection_owner->selection_clear ();
306 selection_owner = owner;
310 void rxvt_display::reg (im_watcher *w)
315 void rxvt_display::unreg (im_watcher *w)
317 imw.erase (find (imw.begin (), imw.end (), w));
320 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
326 m = strlen (modifiers);
328 if (!(id = (char *)malloc (l + m + 2)))
331 memcpy (id, locale, l); id[l] = '\n';
332 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
334 rxvt_xim *xim = xims.get (id);
341 void rxvt_display::put_xim (rxvt_xim *xim)
347 Atom rxvt_display::atom (const char *name)
349 return XInternAtom (display, name, False);
352 /////////////////////////////////////////////////////////////////////////////
354 template class refcache<rxvt_display>;
355 refcache<rxvt_display> displays;
357 /////////////////////////////////////////////////////////////////////////////
360 rxvt_color::set (rxvt_display *display, Pixel p)
366 if (!XQueryColor (display->display, display->cmap, &xc))
377 XftColorAllocValue (display->display,
389 rxvt_color::set (rxvt_display *display, const char *name)
393 if (XParseColor (display->display, display->cmap, name, &xc))
394 return set (display, xc.red, xc.green, xc.blue);
400 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
407 xc.flags = DoRed | DoGreen | DoBlue;
409 if (XAllocColor (display->display, display->cmap, &xc))
410 return set (display, xc.pixel);
416 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
426 XQueryColor (display->display, display->cmap, &c);
435 rxvt_color::free (rxvt_display *display)
438 XftColorFree (display->display, display->visual, display->cmap, &c);
440 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
445 rxvt_color::fade (rxvt_display *display, int percent)
447 unsigned short cr, cg, cb;
450 get (display, cr, cg, cb);