ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
Revision: 1.22
Committed: Mon Jan 9 05:08:02 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.21: +4 -3 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 root 1.22 if (!display)
233     return;
234    
235 root 1.21 #ifdef POINTER_BLANK
236     XFreeCursor (display, blank_cursor);
237     #endif
238 root 1.1 x_ev.stop ();
239 root 1.12 #ifdef USE_XIM
240     xims.clear ();
241     #endif
242 root 1.22 XCloseDisplay (display);
243 root 1.1 }
244    
245     #ifdef USE_XIM
246     void rxvt_display::im_change_cb ()
247     {
248     for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
249     (*i)->call ();
250     }
251 root 1.5
252     void rxvt_display::im_change_check ()
253     {
254 root 1.13 // try to only call im_change_cb when a new input method
255 root 1.5 // registers, as xlib crashes due to a race otherwise.
256     Atom actual_type, *atoms;
257     int actual_format;
258     unsigned long nitems, bytes_after;
259    
260     if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L,
261     False, XA_ATOM, &actual_type, &actual_format,
262     &nitems, &bytes_after, (unsigned char **)&atoms)
263     != Success )
264     return;
265    
266     if (actual_type == XA_ATOM && actual_format == 32)
267     for (int i = 0; i < nitems; i++)
268     if (XGetSelectionOwner (display, atoms[i]))
269     {
270     im_change_cb ();
271     break;
272     }
273    
274     XFree (atoms);
275     }
276 root 1.1 #endif
277    
278     void rxvt_display::x_cb (io_watcher &w, short revents)
279     {
280     do
281     {
282     XEvent xev;
283     XNextEvent (display, &xev);
284    
285 root 1.5 #ifdef USE_XIM
286     if (!XFilterEvent (&xev, None))
287     {
288     if (xev.type == PropertyNotify
289     && xev.xany.window == root
290     && xev.xproperty.atom == xa_xim_servers)
291     im_change_check ();
292     #endif
293     for (int i = xw.size (); i--; )
294     {
295     if (!xw[i])
296     xw.erase_unordered (i);
297     else if (xw[i]->window == xev.xany.window)
298     xw[i]->call (xev);
299     }
300 root 1.1 #ifdef USE_XIM
301 root 1.5 }
302 root 1.1 #endif
303     }
304 root 1.6 while (XEventsQueued (display, QueuedAlready));
305 root 1.1
306 root 1.10 XFlush (display);
307 root 1.1 }
308    
309     void rxvt_display::flush ()
310     {
311 root 1.10 if (XEventsQueued (display, QueuedAlready))
312     x_cb (x_ev, EVENT_READ);
313    
314 root 1.8 XFlush (display);
315 root 1.1 }
316    
317     void rxvt_display::reg (xevent_watcher *w)
318     {
319     xw.push_back (w);
320     w->active = xw.size ();
321     }
322    
323     void rxvt_display::unreg (xevent_watcher *w)
324     {
325     if (w->active)
326     xw[w->active - 1] = 0;
327     }
328    
329     void rxvt_display::set_selection_owner (rxvt_term *owner)
330     {
331     if (selection_owner && selection_owner != owner)
332     selection_owner->selection_clear ();
333    
334     selection_owner = owner;
335     }
336    
337     #ifdef USE_XIM
338     void rxvt_display::reg (im_watcher *w)
339     {
340     imw.push_back (w);
341     }
342    
343     void rxvt_display::unreg (im_watcher *w)
344     {
345     imw.erase (find (imw.begin (), imw.end (), w));
346     }
347    
348     rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
349     {
350     char *id;
351     int l, m;
352    
353     l = strlen (locale);
354     m = strlen (modifiers);
355    
356     if (!(id = (char *)malloc (l + m + 2)))
357     return 0;
358    
359     memcpy (id, locale, l); id[l] = '\n';
360     memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
361    
362     rxvt_xim *xim = xims.get (id);
363    
364     free (id);
365    
366     return xim;
367     }
368    
369     void rxvt_display::put_xim (rxvt_xim *xim)
370     {
371 root 1.10 #if XLIB_IS_RACEFREE
372 root 1.1 xims.put (xim);
373 root 1.10 #endif
374 root 1.1 }
375     #endif
376    
377     Atom rxvt_display::atom (const char *name)
378     {
379     return XInternAtom (display, name, False);
380     }
381    
382     /////////////////////////////////////////////////////////////////////////////
383    
384     template class refcache<rxvt_display>;
385     refcache<rxvt_display> displays;
386    
387     /////////////////////////////////////////////////////////////////////////////
388    
389     bool
390     rxvt_color::set (rxvt_display *display, Pixel p)
391     {
392     #if XFT
393     XColor xc;
394    
395     xc.pixel = p;
396     if (!XQueryColor (display->display, display->cmap, &xc))
397     return false;
398    
399     XRenderColor d;
400    
401     d.red = xc.red;
402     d.green = xc.green;
403     d.blue = xc.blue;
404     d.alpha = 0xffff;
405    
406     return
407     XftColorAllocValue (display->display,
408     display->visual,
409     display->cmap,
410     &d, &c);
411     #else
412     this->p = p;
413     #endif
414    
415     return true;
416     }
417    
418     bool
419     rxvt_color::set (rxvt_display *display, const char *name)
420     {
421 root 1.7 #if XFT
422     return XftColorAllocName (display->display, display->visual, display->cmap,
423     name, &c);
424     #else
425 root 1.1 XColor xc;
426    
427     if (XParseColor (display->display, display->cmap, name, &xc))
428     return set (display, xc.red, xc.green, xc.blue);
429    
430     return false;
431 root 1.7 #endif
432 root 1.1 }
433    
434     bool
435     rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
436     {
437     XColor xc;
438    
439     xc.red = cr;
440     xc.green = cg;
441     xc.blue = cb;
442     xc.flags = DoRed | DoGreen | DoBlue;
443    
444     if (XAllocColor (display->display, display->cmap, &xc))
445     return set (display, xc.pixel);
446    
447     return false;
448     }
449    
450     void
451     rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
452     {
453     #if XFT
454     cr = c.color.red;
455     cg = c.color.green;
456     cb = c.color.blue;
457     #else
458     XColor c;
459    
460     c.pixel = p;
461     XQueryColor (display->display, display->cmap, &c);
462    
463     cr = c.red;
464     cg = c.green;
465     cb = c.blue;
466     #endif
467     }
468    
469     void
470     rxvt_color::free (rxvt_display *display)
471     {
472     #if XFT
473     XftColorFree (display->display, display->visual, display->cmap, &c);
474     #else
475     XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
476     #endif
477     }
478    
479     rxvt_color
480     rxvt_color::fade (rxvt_display *display, int percent)
481     {
482 root 1.18 percent = 100 - percent;
483    
484 root 1.1 unsigned short cr, cg, cb;
485     rxvt_color faded;
486    
487     get (display, cr, cg, cb);
488 root 1.18
489     faded.set (
490     display,
491     cr * percent / 100,
492     cg * percent / 100,
493     cb * percent / 100
494     );
495    
496     return faded;
497     }
498    
499 root 1.19 #define LERP(a,b,p) (a * p + b * (100 - p)) / 100
500 root 1.18
501     rxvt_color
502     rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto)
503     {
504     percent = 100 - percent;
505    
506     unsigned short cr, cg, cb;
507     unsigned short fcr, fcg, fcb;
508     rxvt_color faded;
509    
510     get (display, cr, cg, cb);
511     fadeto.get(display, fcr, fcg, fcb);
512    
513     faded.set (
514     display,
515     LERP (cr, fcr, percent),
516     LERP (cg, fcg, percent),
517     LERP (cb, fcb, percent)
518     );
519 root 1.1
520     return faded;
521     }
522