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