ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
Revision: 1.12
Committed: Mon Jan 17 00:59:24 2005 UTC (19 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-5_0, rel-4_8, rel-4_9
Changes since 1.10: +15 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*--------------------------------*-C-*---------------------------------*
2 root 1.5 * File: rxvttoolkit.C
3 root 1.1 *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6     * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
7     *
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.
12     *
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.
17     *
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     *----------------------------------------------------------------------*/
22    
23     #include "../config.h"
24     #include <rxvt.h>
25     #include <rxvttoolkit.h>
26    
27     #include <unistd.h>
28     #include <fcntl.h>
29    
30     #ifndef NO_SLOW_LINK_SUPPORT
31     # include <sys/socket.h>
32     # include <sys/un.h>
33     #endif
34    
35     refcounted::refcounted (const char *id)
36     {
37 root 1.2 this->id = strdup (id);
38 root 1.1 }
39    
40     refcounted::~refcounted ()
41     {
42     free (id);
43     }
44    
45     template<class T>
46     T *refcache<T>::get (const char *id)
47     {
48     for (T **i = this->begin (); i < this->end (); ++i)
49     {
50     if (!strcmp (id, (*i)->id))
51     {
52     (*i)->referenced++;
53     return *i;
54     }
55     }
56    
57     T *obj = new T (id);
58    
59     obj->referenced = 1;
60    
61     if (obj && obj->init ())
62     {
63     this->push_back (obj);
64     return obj;
65     }
66     else
67     {
68     delete obj;
69     return 0;
70     }
71     }
72    
73     template<class T>
74     void refcache<T>::put (T *obj)
75     {
76     if (!obj)
77     return;
78    
79     if (!--obj->referenced)
80     {
81     this->erase (find (this->begin (), this->end (), obj));
82     delete obj;
83     }
84     }
85    
86     template<class T>
87 root 1.12 void refcache<T>::clear ()
88 root 1.1 {
89     while (this->size ())
90     put (*this->begin ());
91     }
92    
93     /////////////////////////////////////////////////////////////////////////////
94    
95     #ifdef USE_XIM
96     static void
97     #if XIMCB_PROTO_BROKEN
98     im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
99     #else
100     im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
101     #endif
102     {
103     rxvt_xim *xim = (rxvt_xim *)client_data;
104     rxvt_display *display = xim->display;
105    
106 root 1.5 xim->xim = 0;
107    
108 root 1.1 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
109     display->im_change_cb ();
110     }
111    
112     bool rxvt_xim::init ()
113     {
114     display = GET_R->display; //HACK: TODO
115    
116     xim = XOpenIM (display->display, NULL, NULL, NULL);
117    
118     if (!xim)
119     return false;
120    
121     XIMCallback ximcallback;
122     ximcallback.client_data = (XPointer)this;
123     ximcallback.callback = im_destroy_cb;
124    
125     XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
126    
127     return true;
128     }
129    
130     rxvt_xim::~rxvt_xim ()
131     {
132     if (xim)
133     XCloseIM (xim);
134     }
135     #endif
136    
137     /////////////////////////////////////////////////////////////////////////////
138    
139     rxvt_display::rxvt_display (const char *id)
140     : refcounted (id)
141     , x_ev (this, &rxvt_display::x_cb)
142     , selection_owner (0)
143     {
144     }
145    
146     bool rxvt_display::init ()
147     {
148     display = XOpenDisplay (id);
149    
150     if (!display)
151     return false;
152    
153     screen = DefaultScreen (display);
154     root = DefaultRootWindow (display);
155     visual = DefaultVisual (display, screen);
156     cmap = DefaultColormap (display, screen);
157     depth = DefaultDepth (display, screen);
158    
159     int fd = XConnectionNumber (display);
160    
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
165     is_local = 0;
166     sockaddr_un sa;
167     socklen_t sl = sizeof (sa);
168    
169     if (!getsockname (fd, (sockaddr *)&sa, &sl))
170     is_local = sa.sun_family == AF_LOCAL;
171     #endif
172    
173 root 1.12 #ifdef POINTER_BLANK
174     XColor blackcolour;
175     blackcolour.red = 0;
176     blackcolour.green = 0;
177     blackcolour.blue = 0;
178     Font f = XLoadFont (display, "fixed");
179     blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
180     &blackcolour, &blackcolour);
181     XUnloadFont (display, f);
182     #endif
183    
184 root 1.1 #ifdef PREFER_24BIT
185     /*
186     * If depth is not 24, look for a 24bit visual.
187     */
188     if (depth != 24)
189     {
190     XVisualInfo vinfo;
191    
192     if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
193     {
194     depth = 24;
195     visual = vinfo.visual;
196     cmap = XCreateColormap (display,
197     RootWindow (display, screen),
198     visual, AllocNone);
199     }
200     }
201     #endif
202    
203     x_ev.start (fd, EVENT_READ);
204     fcntl (fd, F_SETFD, FD_CLOEXEC);
205    
206     XSelectInput (display, root, PropertyChangeMask);
207     #ifdef USE_XIM
208     xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
209     #endif
210    
211     flush ();
212    
213     return true;
214     }
215    
216     rxvt_display::~rxvt_display ()
217     {
218     x_ev.stop ();
219 root 1.12 #ifdef USE_XIM
220     xims.clear ();
221     #endif
222 root 1.1
223     if (display)
224     XCloseDisplay (display);
225     }
226    
227     #ifdef USE_XIM
228     void rxvt_display::im_change_cb ()
229     {
230     for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
231     (*i)->call ();
232     }
233 root 1.5
234     void rxvt_display::im_change_check ()
235     {
236     // make sure we only call im_change_cb when a new input method
237     // registers, as xlib crashes due to a race otherwise.
238     Atom actual_type, *atoms;
239     int actual_format;
240     unsigned long nitems, bytes_after;
241    
242     if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L,
243     False, XA_ATOM, &actual_type, &actual_format,
244     &nitems, &bytes_after, (unsigned char **)&atoms)
245     != Success )
246     return;
247    
248     if (actual_type == XA_ATOM && actual_format == 32)
249     for (int i = 0; i < nitems; i++)
250     if (XGetSelectionOwner (display, atoms[i]))
251     {
252     im_change_cb ();
253     break;
254     }
255    
256     XFree (atoms);
257     }
258 root 1.1 #endif
259    
260     void rxvt_display::x_cb (io_watcher &w, short revents)
261     {
262     do
263     {
264     XEvent xev;
265     XNextEvent (display, &xev);
266    
267 root 1.5 #ifdef USE_XIM
268     if (!XFilterEvent (&xev, None))
269     {
270 root 1.1
271 root 1.5 if (xev.type == PropertyNotify
272     && xev.xany.window == root
273     && xev.xproperty.atom == xa_xim_servers)
274     im_change_check ();
275     #endif
276     for (int i = xw.size (); i--; )
277     {
278     if (!xw[i])
279     xw.erase_unordered (i);
280     else if (xw[i]->window == xev.xany.window)
281     xw[i]->call (xev);
282     }
283 root 1.1 #ifdef USE_XIM
284 root 1.5 }
285 root 1.1 #endif
286     }
287 root 1.6 while (XEventsQueued (display, QueuedAlready));
288 root 1.1
289 root 1.10 XFlush (display);
290 root 1.1 }
291    
292     void rxvt_display::flush ()
293     {
294 root 1.10 if (XEventsQueued (display, QueuedAlready))
295     x_cb (x_ev, EVENT_READ);
296    
297 root 1.8 XFlush (display);
298 root 1.1 }
299    
300     void rxvt_display::reg (xevent_watcher *w)
301     {
302     xw.push_back (w);
303     w->active = xw.size ();
304     }
305    
306     void rxvt_display::unreg (xevent_watcher *w)
307     {
308     if (w->active)
309     xw[w->active - 1] = 0;
310     }
311    
312     void rxvt_display::set_selection_owner (rxvt_term *owner)
313     {
314     if (selection_owner && selection_owner != owner)
315     selection_owner->selection_clear ();
316    
317     selection_owner = owner;
318     }
319    
320     #ifdef USE_XIM
321     void rxvt_display::reg (im_watcher *w)
322     {
323     imw.push_back (w);
324     }
325    
326     void rxvt_display::unreg (im_watcher *w)
327     {
328     imw.erase (find (imw.begin (), imw.end (), w));
329     }
330    
331     rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
332     {
333     char *id;
334     int l, m;
335    
336     l = strlen (locale);
337     m = strlen (modifiers);
338    
339     if (!(id = (char *)malloc (l + m + 2)))
340     return 0;
341    
342     memcpy (id, locale, l); id[l] = '\n';
343     memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
344    
345     rxvt_xim *xim = xims.get (id);
346    
347     free (id);
348    
349     return xim;
350     }
351    
352     void rxvt_display::put_xim (rxvt_xim *xim)
353     {
354 root 1.10 #if XLIB_IS_RACEFREE
355 root 1.1 xims.put (xim);
356 root 1.10 #endif
357 root 1.1 }
358     #endif
359    
360     Atom rxvt_display::atom (const char *name)
361     {
362     return XInternAtom (display, name, False);
363     }
364    
365     /////////////////////////////////////////////////////////////////////////////
366    
367     template class refcache<rxvt_display>;
368     refcache<rxvt_display> displays;
369    
370     /////////////////////////////////////////////////////////////////////////////
371    
372     bool
373     rxvt_color::set (rxvt_display *display, Pixel p)
374     {
375     #if XFT
376     XColor xc;
377    
378     xc.pixel = p;
379     if (!XQueryColor (display->display, display->cmap, &xc))
380     return false;
381    
382     XRenderColor d;
383    
384     d.red = xc.red;
385     d.green = xc.green;
386     d.blue = xc.blue;
387     d.alpha = 0xffff;
388    
389     return
390     XftColorAllocValue (display->display,
391     display->visual,
392     display->cmap,
393     &d, &c);
394     #else
395     this->p = p;
396     #endif
397    
398     return true;
399     }
400    
401     bool
402     rxvt_color::set (rxvt_display *display, const char *name)
403     {
404 root 1.7 #if XFT
405     return XftColorAllocName (display->display, display->visual, display->cmap,
406     name, &c);
407     #else
408 root 1.1 XColor xc;
409    
410     if (XParseColor (display->display, display->cmap, name, &xc))
411     return set (display, xc.red, xc.green, xc.blue);
412    
413     return false;
414 root 1.7 #endif
415 root 1.1 }
416    
417     bool
418     rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
419     {
420     XColor xc;
421    
422     xc.red = cr;
423     xc.green = cg;
424     xc.blue = cb;
425     xc.flags = DoRed | DoGreen | DoBlue;
426    
427     if (XAllocColor (display->display, display->cmap, &xc))
428     return set (display, xc.pixel);
429    
430     return false;
431     }
432    
433     void
434     rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
435     {
436     #if XFT
437     cr = c.color.red;
438     cg = c.color.green;
439     cb = c.color.blue;
440     #else
441     XColor c;
442    
443     c.pixel = p;
444     XQueryColor (display->display, display->cmap, &c);
445    
446     cr = c.red;
447     cg = c.green;
448     cb = c.blue;
449     #endif
450     }
451    
452     void
453     rxvt_color::free (rxvt_display *display)
454     {
455     #if XFT
456     XftColorFree (display->display, display->visual, display->cmap, &c);
457     #else
458     XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
459     #endif
460     }
461    
462     rxvt_color
463     rxvt_color::fade (rxvt_display *display, int percent)
464     {
465     unsigned short cr, cg, cb;
466     rxvt_color faded;
467    
468     get (display, cr, cg, cb);
469     faded.set (display,
470     cr * percent / 100,
471     cg * percent / 100,
472     cb * percent / 100);
473    
474     return faded;
475     }
476