ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtcolor.C
Revision: 1.21
Committed: Mon Aug 2 04:53:34 2004 UTC (19 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.20: +0 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.14 /*--------------------------------*-C-*---------------------------------*
2     * File: rxvtcolor.C
3     *----------------------------------------------------------------------*
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 pcg 1.1 #include "../config.h"
24     #include <rxvt.h>
25 pcg 1.4 #include <rxvtcolor.h>
26 pcg 1.1
27 pcg 1.4 #include <unistd.h>
28     #include <fcntl.h>
29 pcg 1.1
30 pcg 1.10 class byteorder byteorder;
31    
32     byteorder::byteorder ()
33     {
34     union {
35     uint32_t u;
36     uint8_t b[4];
37     } w;
38    
39     w.b[0] = 0x11;
40     w.b[1] = 0x22;
41     w.b[2] = 0x33;
42     w.b[3] = 0x44;
43    
44     e = w.u;
45     }
46    
47 pcg 1.6 refcounted::refcounted (const char *id)
48     {
49     this->id = STRDUP (id);
50     }
51    
52     refcounted::~refcounted ()
53     {
54     free (id);
55     }
56    
57     template<class T>
58     T *refcache<T>::get (const char *id)
59     {
60 root 1.15 for (T **i = this->begin (); i < this->end (); ++i)
61 pcg 1.6 {
62     if (!strcmp (id, (*i)->id))
63     {
64     (*i)->referenced++;
65     return *i;
66     }
67     }
68    
69     T *obj = new T (id);
70    
71     obj->referenced = 1;
72    
73     if (obj && obj->init ())
74     {
75 root 1.15 this->push_back (obj);
76 pcg 1.6 return obj;
77     }
78     else
79     {
80     delete obj;
81     return 0;
82     }
83     }
84    
85     template<class T>
86     void refcache<T>::put (T *obj)
87     {
88     if (!obj)
89     return;
90    
91     if (!--obj->referenced)
92     {
93 root 1.15 this->erase (find (this->begin (), this->end (), obj));
94 pcg 1.6 delete obj;
95     }
96     }
97    
98     template<class T>
99     refcache<T>::~refcache ()
100     {
101 root 1.15 while (this->size ())
102     put (*this->begin ());
103 pcg 1.6 }
104    
105 pcg 1.4 /////////////////////////////////////////////////////////////////////////////
106    
107 root 1.16 #ifdef USE_XIM
108 pcg 1.6 static void
109 root 1.20 #if XIMCB_PROTO_BROKEN
110     im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
111     #else
112 pcg 1.6 im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
113 root 1.20 #endif
114 pcg 1.6 {
115     rxvt_xim *xim = (rxvt_xim *)client_data;
116     rxvt_display *display = xim->display;
117    
118     display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
119    
120     display->im_change_cb ();
121     }
122    
123     bool rxvt_xim::init ()
124     {
125     display = GET_R->display; //HACK: TODO
126    
127     xim = XOpenIM (display->display, NULL, NULL, NULL);
128    
129     if (!xim)
130     return false;
131    
132     XIMCallback ximcallback;
133     ximcallback.client_data = (XPointer)this;
134     ximcallback.callback = im_destroy_cb;
135    
136 root 1.18 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
137 pcg 1.6
138     return true;
139     }
140    
141     rxvt_xim::~rxvt_xim ()
142 pcg 1.4 {
143 pcg 1.6 if (xim)
144     XCloseIM (xim);
145 pcg 1.4 }
146 root 1.16 #endif
147 pcg 1.4
148 pcg 1.6 /////////////////////////////////////////////////////////////////////////////
149    
150     rxvt_display::rxvt_display (const char *id)
151     : refcounted (id)
152 pcg 1.7 , x_ev (this, &rxvt_display::x_cb)
153 pcg 1.6 , selection_owner (0)
154 pcg 1.4 {
155     }
156    
157 pcg 1.6 bool rxvt_display::init ()
158 pcg 1.4 {
159 pcg 1.6 display = XOpenDisplay (id);
160 pcg 1.4
161 pcg 1.9 if (!display)
162     return false;
163    
164 pcg 1.4 screen = DefaultScreen (display);
165     root = DefaultRootWindow (display);
166     visual = DefaultVisual (display, screen);
167     cmap = DefaultColormap (display, screen);
168     depth = DefaultDepth (display, screen);
169    
170     #ifdef PREFER_24BIT
171     /*
172     * If depth is not 24, look for a 24bit visual.
173     */
174     if (depth != 24)
175     {
176     XVisualInfo vinfo;
177    
178     if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
179     {
180     depth = 24;
181     visual = vinfo.visual;
182     cmap = XCreateColormap (display,
183     RootWindow (display, screen),
184     visual, AllocNone);
185     }
186     }
187     #endif
188    
189     int fd = XConnectionNumber (display);
190 pcg 1.7 x_ev.start (fd, EVENT_READ);
191 pcg 1.13 fcntl (fd, F_SETFD, FD_CLOEXEC);
192 pcg 1.4
193 pcg 1.6 XSelectInput (display, root, PropertyChangeMask);
194 root 1.16 #ifdef USE_XIM
195 pcg 1.6 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
196 root 1.16 #endif
197 pcg 1.6
198 pcg 1.7 flush ();
199    
200 pcg 1.4 return true;
201     }
202    
203 pcg 1.6 rxvt_display::~rxvt_display ()
204 pcg 1.4 {
205 pcg 1.7 x_ev.stop ();
206 pcg 1.4
207 pcg 1.9 if (display)
208     XCloseDisplay (display);
209 pcg 1.4 }
210    
211 root 1.16 #ifdef USE_XIM
212 pcg 1.6 void rxvt_display::im_change_cb ()
213     {
214     for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
215     (*i)->call ();
216     }
217 root 1.16 #endif
218 pcg 1.6
219 pcg 1.7 void rxvt_display::x_cb (io_watcher &w, short revents)
220 pcg 1.4 {
221     do
222     {
223     XEvent xev;
224     XNextEvent (display, &xev);
225    
226 pcg 1.6 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
227    
228 root 1.16 #ifdef USE_XIM
229 pcg 1.6 if (xev.type == PropertyNotify
230     && xev.xany.window == root
231     && xev.xproperty.atom == xa_xim_servers)
232     im_change_cb ();
233 root 1.16 #endif
234 pcg 1.6
235 pcg 1.4 for (int i = xw.size (); i--; )
236     {
237     if (!xw[i])
238     xw.erase_unordered (i);
239     else if (xw[i]->window == xev.xany.window)
240     xw[i]->call (xev);
241     }
242     }
243     while (XPending (display));
244 pcg 1.7
245     flush ();
246     }
247    
248     void rxvt_display::flush ()
249     {
250     for (;;)
251     {
252     if (!XPending (display))
253     break;
254    
255     x_cb (x_ev, 0);
256     }
257 pcg 1.4 }
258    
259     void rxvt_display::reg (xevent_watcher *w)
260     {
261     xw.push_back (w);
262     w->active = xw.size ();
263     }
264    
265     void rxvt_display::unreg (xevent_watcher *w)
266     {
267     if (w->active)
268     xw[w->active - 1] = 0;
269     }
270    
271 root 1.16 void rxvt_display::set_selection_owner (rxvt_term *owner)
272     {
273     if (selection_owner && selection_owner != owner)
274     selection_owner->selection_clear ();
275    
276     selection_owner = owner;
277     }
278    
279     #ifdef USE_XIM
280 pcg 1.6 void rxvt_display::reg (im_watcher *w)
281     {
282     imw.push_back (w);
283     }
284    
285     void rxvt_display::unreg (im_watcher *w)
286     {
287     imw.erase (find (imw.begin (), imw.end (), w));
288     }
289    
290     rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
291 pcg 1.4 {
292 pcg 1.6 char *id;
293 pcg 1.12 int l, m;
294 pcg 1.4
295 pcg 1.12 l = strlen (locale);
296     m = strlen (modifiers);
297    
298     if (!(id = (char *)malloc (l + m + 2)))
299 pcg 1.6 return 0;
300 pcg 1.4
301 pcg 1.12 memcpy (id, locale, l); id[l] = '\n';
302     memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
303    
304 pcg 1.6 rxvt_xim *xim = xims.get (id);
305 pcg 1.4
306 pcg 1.6 free (id);
307 pcg 1.4
308 pcg 1.6 return xim;
309 pcg 1.4 }
310    
311 pcg 1.6 void rxvt_display::put_xim (rxvt_xim *xim)
312 pcg 1.4 {
313 pcg 1.6 xims.put (xim);
314 pcg 1.4 }
315 root 1.16 #endif
316 pcg 1.4
317 pcg 1.11 Atom rxvt_display::atom (const char *name)
318     {
319     return XInternAtom (display, name, False);
320     }
321    
322 pcg 1.4 /////////////////////////////////////////////////////////////////////////////
323 pcg 1.6
324 root 1.15 template class refcache<rxvt_display>;
325 pcg 1.6 refcache<rxvt_display> displays;
326    
327     /////////////////////////////////////////////////////////////////////////////
328 pcg 1.4
329 pcg 1.1 bool
330 pcg 1.4 rxvt_color::set (rxvt_display *display, Pixel p)
331 pcg 1.1 {
332     #if XFT
333     XColor xc;
334    
335     xc.pixel = p;
336 pcg 1.4 if (!XQueryColor (display->display, display->cmap, &xc))
337 pcg 1.1 return false;
338    
339     XRenderColor d;
340    
341     d.red = xc.red;
342     d.green = xc.green;
343     d.blue = xc.blue;
344     d.alpha = 0xffff;
345    
346     return
347 pcg 1.4 XftColorAllocValue (display->display,
348     display->visual,
349     display->cmap,
350     &d, &c);
351 pcg 1.1 #else
352     this->p = p;
353     #endif
354    
355     return true;
356     }
357    
358     bool
359 pcg 1.4 rxvt_color::set (rxvt_display *display, const char *name)
360 pcg 1.1 {
361     XColor xc;
362    
363 pcg 1.4 if (XParseColor (display->display, display->cmap, name, &xc))
364     return set (display, xc.red, xc.green, xc.blue);
365 pcg 1.1
366     return false;
367     }
368    
369     bool
370 pcg 1.4 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
371 pcg 1.1 {
372     XColor xc;
373    
374     xc.red = cr;
375     xc.green = cg;
376     xc.blue = cb;
377     xc.flags = DoRed | DoGreen | DoBlue;
378    
379 pcg 1.4 if (XAllocColor (display->display, display->cmap, &xc))
380     return set (display, xc.pixel);
381 pcg 1.1
382     return false;
383     }
384    
385     void
386 pcg 1.4 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
387 pcg 1.1 {
388     #if XFT
389     cr = c.color.red;
390     cg = c.color.green;
391     cb = c.color.blue;
392     #else
393     XColor c;
394    
395     c.pixel = p;
396 pcg 1.4 XQueryColor (display->display, display->cmap, &c);
397 pcg 1.1
398     cr = c.red;
399     cg = c.green;
400     cb = c.blue;
401     #endif
402     }
403    
404 pcg 1.4 void
405     rxvt_color::free (rxvt_display *display)
406     {
407     #if XFT
408     XftColorFree (display->display, display->visual, display->cmap, &c);
409     #else
410 pcg 1.8 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
411 pcg 1.4 #endif
412     }
413    
414 root 1.19 rxvt_color
415     rxvt_color::fade (rxvt_display *display, int percent)
416     {
417     unsigned short cr, cg, cb;
418     rxvt_color faded;
419    
420     get (display, cr, cg, cb);
421     faded.set (display,
422     cr * percent / 100,
423     cg * percent / 100,
424     cb * percent / 100);
425    
426     return faded;
427     }
428