ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
Revision: 1.15
Committed: Wed Jul 13 00:51:25 2005 UTC (18 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +3 -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 root 1.14 #ifdef LOCAL_X_IS_UNIX
149     if (id[0] == ':')
150     {
151     val = rxvt_malloc (5 + strlen (id) + 1);
152     strcpy (val, "unix/");
153     strcat (val, id);
154     display = XOpenDisplay (val);
155 root 1.15 printf ("OD %s => %p\n", val, display);//D
156 root 1.14 free (val);
157     }
158     else
159 root 1.15 #endif
160 root 1.14 display = 0;
161    
162     if (!display)
163     display = XOpenDisplay (id);
164 root 1.15 printf ("O2 %s => %p\n", id, display);//D
165 root 1.1
166     if (!display)
167     return false;
168    
169     screen = DefaultScreen (display);
170     root = DefaultRootWindow (display);
171     visual = DefaultVisual (display, screen);
172     cmap = DefaultColormap (display, screen);
173     depth = DefaultDepth (display, screen);
174    
175     int fd = XConnectionNumber (display);
176    
177     #ifndef NO_SLOW_LINK_SUPPORT
178     // try to detetc wether we have a local connection.
179     // assume unix domains socket == local, everything else not
180     // TODO: might want to check for inet/127.0.0.1
181     is_local = 0;
182     sockaddr_un sa;
183     socklen_t sl = sizeof (sa);
184    
185     if (!getsockname (fd, (sockaddr *)&sa, &sl))
186     is_local = sa.sun_family == AF_LOCAL;
187     #endif
188    
189 root 1.12 #ifdef POINTER_BLANK
190     XColor blackcolour;
191     blackcolour.red = 0;
192     blackcolour.green = 0;
193     blackcolour.blue = 0;
194     Font f = XLoadFont (display, "fixed");
195     blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
196     &blackcolour, &blackcolour);
197     XUnloadFont (display, f);
198     #endif
199    
200 root 1.1 #ifdef PREFER_24BIT
201     /*
202     * If depth is not 24, look for a 24bit visual.
203     */
204     if (depth != 24)
205     {
206     XVisualInfo vinfo;
207    
208     if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
209     {
210     depth = 24;
211     visual = vinfo.visual;
212     cmap = XCreateColormap (display,
213     RootWindow (display, screen),
214     visual, AllocNone);
215     }
216     }
217     #endif
218    
219     x_ev.start (fd, EVENT_READ);
220     fcntl (fd, F_SETFD, FD_CLOEXEC);
221    
222     XSelectInput (display, root, PropertyChangeMask);
223     #ifdef USE_XIM
224     xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
225     #endif
226    
227     flush ();
228    
229     return true;
230     }
231    
232     rxvt_display::~rxvt_display ()
233     {
234     x_ev.stop ();
235 root 1.12 #ifdef USE_XIM
236     xims.clear ();
237     #endif
238 root 1.1
239     if (display)
240     XCloseDisplay (display);
241     }
242    
243     #ifdef USE_XIM
244     void rxvt_display::im_change_cb ()
245     {
246     for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
247     (*i)->call ();
248     }
249 root 1.5
250     void rxvt_display::im_change_check ()
251     {
252 root 1.13 // try to only call im_change_cb when a new input method
253 root 1.5 // registers, as xlib crashes due to a race otherwise.
254     Atom actual_type, *atoms;
255     int actual_format;
256     unsigned long nitems, bytes_after;
257    
258     if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L,
259     False, XA_ATOM, &actual_type, &actual_format,
260     &nitems, &bytes_after, (unsigned char **)&atoms)
261     != Success )
262     return;
263    
264     if (actual_type == XA_ATOM && actual_format == 32)
265     for (int i = 0; i < nitems; i++)
266     if (XGetSelectionOwner (display, atoms[i]))
267     {
268     im_change_cb ();
269     break;
270     }
271    
272     XFree (atoms);
273     }
274 root 1.1 #endif
275    
276     void rxvt_display::x_cb (io_watcher &w, short revents)
277     {
278     do
279     {
280     XEvent xev;
281     XNextEvent (display, &xev);
282    
283 root 1.5 #ifdef USE_XIM
284     if (!XFilterEvent (&xev, None))
285     {
286 root 1.1
287 root 1.5 if (xev.type == PropertyNotify
288     && xev.xany.window == root
289     && xev.xproperty.atom == xa_xim_servers)
290     im_change_check ();
291     #endif
292     for (int i = xw.size (); i--; )
293     {
294     if (!xw[i])
295     xw.erase_unordered (i);
296     else if (xw[i]->window == xev.xany.window)
297     xw[i]->call (xev);
298     }
299 root 1.1 #ifdef USE_XIM
300 root 1.5 }
301 root 1.1 #endif
302     }
303 root 1.6 while (XEventsQueued (display, QueuedAlready));
304 root 1.1
305 root 1.10 XFlush (display);
306 root 1.1 }
307    
308     void rxvt_display::flush ()
309     {
310 root 1.10 if (XEventsQueued (display, QueuedAlready))
311     x_cb (x_ev, EVENT_READ);
312    
313 root 1.8 XFlush (display);
314 root 1.1 }
315    
316     void rxvt_display::reg (xevent_watcher *w)
317     {
318     xw.push_back (w);
319     w->active = xw.size ();
320     }
321    
322     void rxvt_display::unreg (xevent_watcher *w)
323     {
324     if (w->active)
325     xw[w->active - 1] = 0;
326     }
327    
328     void rxvt_display::set_selection_owner (rxvt_term *owner)
329     {
330     if (selection_owner && selection_owner != owner)
331     selection_owner->selection_clear ();
332    
333     selection_owner = owner;
334     }
335    
336     #ifdef USE_XIM
337     void rxvt_display::reg (im_watcher *w)
338     {
339     imw.push_back (w);
340     }
341    
342     void rxvt_display::unreg (im_watcher *w)
343     {
344     imw.erase (find (imw.begin (), imw.end (), w));
345     }
346    
347     rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
348     {
349     char *id;
350     int l, m;
351    
352     l = strlen (locale);
353     m = strlen (modifiers);
354    
355     if (!(id = (char *)malloc (l + m + 2)))
356     return 0;
357    
358     memcpy (id, locale, l); id[l] = '\n';
359     memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
360    
361     rxvt_xim *xim = xims.get (id);
362    
363     free (id);
364    
365     return xim;
366     }
367    
368     void rxvt_display::put_xim (rxvt_xim *xim)
369     {
370 root 1.10 #if XLIB_IS_RACEFREE
371 root 1.1 xims.put (xim);
372 root 1.10 #endif
373 root 1.1 }
374     #endif
375    
376     Atom rxvt_display::atom (const char *name)
377     {
378     return XInternAtom (display, name, False);
379     }
380    
381     /////////////////////////////////////////////////////////////////////////////
382    
383     template class refcache<rxvt_display>;
384     refcache<rxvt_display> displays;
385    
386     /////////////////////////////////////////////////////////////////////////////
387    
388     bool
389     rxvt_color::set (rxvt_display *display, Pixel p)
390     {
391     #if XFT
392     XColor xc;
393    
394     xc.pixel = p;
395     if (!XQueryColor (display->display, display->cmap, &xc))
396     return false;
397    
398     XRenderColor d;
399    
400     d.red = xc.red;
401     d.green = xc.green;
402     d.blue = xc.blue;
403     d.alpha = 0xffff;
404    
405     return
406     XftColorAllocValue (display->display,
407     display->visual,
408     display->cmap,
409     &d, &c);
410     #else
411     this->p = p;
412     #endif
413    
414     return true;
415     }
416    
417     bool
418     rxvt_color::set (rxvt_display *display, const char *name)
419     {
420 root 1.7 #if XFT
421     return XftColorAllocName (display->display, display->visual, display->cmap,
422     name, &c);
423     #else
424 root 1.1 XColor xc;
425    
426     if (XParseColor (display->display, display->cmap, name, &xc))
427     return set (display, xc.red, xc.green, xc.blue);
428    
429     return false;
430 root 1.7 #endif
431 root 1.1 }
432    
433     bool
434     rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
435     {
436     XColor xc;
437    
438     xc.red = cr;
439     xc.green = cg;
440     xc.blue = cb;
441     xc.flags = DoRed | DoGreen | DoBlue;
442    
443     if (XAllocColor (display->display, display->cmap, &xc))
444     return set (display, xc.pixel);
445    
446     return false;
447     }
448    
449     void
450     rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
451     {
452     #if XFT
453     cr = c.color.red;
454     cg = c.color.green;
455     cb = c.color.blue;
456     #else
457     XColor c;
458    
459     c.pixel = p;
460     XQueryColor (display->display, display->cmap, &c);
461    
462     cr = c.red;
463     cg = c.green;
464     cb = c.blue;
465     #endif
466     }
467    
468     void
469     rxvt_color::free (rxvt_display *display)
470     {
471     #if XFT
472     XftColorFree (display->display, display->visual, display->cmap, &c);
473     #else
474     XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
475     #endif
476     }
477    
478     rxvt_color
479     rxvt_color::fade (rxvt_display *display, int percent)
480     {
481     unsigned short cr, cg, cb;
482     rxvt_color faded;
483    
484     get (display, cr, cg, cb);
485     faded.set (display,
486     cr * percent / 100,
487     cg * percent / 100,
488     cb * percent / 100);
489    
490     return faded;
491     }
492