ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtcolor.C
Revision: 1.15
Committed: Sun May 30 19:12:42 2004 UTC (19 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_2
Changes since 1.14: +6 -6 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 pcg 1.6 static void
108     im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
109     {
110     rxvt_xim *xim = (rxvt_xim *)client_data;
111     rxvt_display *display = xim->display;
112    
113     display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
114    
115     display->im_change_cb ();
116     }
117    
118     bool rxvt_xim::init ()
119     {
120     display = GET_R->display; //HACK: TODO
121    
122     xim = XOpenIM (display->display, NULL, NULL, NULL);
123    
124     if (!xim)
125     return false;
126    
127     XIMCallback ximcallback;
128     ximcallback.client_data = (XPointer)this;
129     ximcallback.callback = im_destroy_cb;
130    
131     XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
132    
133     return true;
134     }
135    
136     rxvt_xim::~rxvt_xim ()
137 pcg 1.4 {
138 pcg 1.6 if (xim)
139     XCloseIM (xim);
140 pcg 1.4 }
141    
142 pcg 1.6 /////////////////////////////////////////////////////////////////////////////
143    
144     rxvt_display::rxvt_display (const char *id)
145     : refcounted (id)
146 pcg 1.7 , x_ev (this, &rxvt_display::x_cb)
147 pcg 1.6 , selection_owner (0)
148 pcg 1.4 {
149     }
150    
151 pcg 1.6 bool rxvt_display::init ()
152 pcg 1.4 {
153 pcg 1.6 display = XOpenDisplay (id);
154 pcg 1.4
155 pcg 1.9 if (!display)
156     return false;
157    
158 pcg 1.4 screen = DefaultScreen (display);
159     root = DefaultRootWindow (display);
160     visual = DefaultVisual (display, screen);
161     cmap = DefaultColormap (display, screen);
162     depth = DefaultDepth (display, screen);
163    
164     #ifdef PREFER_24BIT
165     /*
166     * If depth is not 24, look for a 24bit visual.
167     */
168     if (depth != 24)
169     {
170     XVisualInfo vinfo;
171    
172     if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
173     {
174     depth = 24;
175     visual = vinfo.visual;
176     cmap = XCreateColormap (display,
177     RootWindow (display, screen),
178     visual, AllocNone);
179     }
180     }
181     #endif
182    
183     int fd = XConnectionNumber (display);
184 pcg 1.7 x_ev.start (fd, EVENT_READ);
185 pcg 1.13 fcntl (fd, F_SETFD, FD_CLOEXEC);
186 pcg 1.4
187 pcg 1.6 XSelectInput (display, root, PropertyChangeMask);
188     xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
189    
190 pcg 1.7 flush ();
191    
192 pcg 1.4 return true;
193     }
194    
195 pcg 1.6 rxvt_display::~rxvt_display ()
196 pcg 1.4 {
197 pcg 1.7 x_ev.stop ();
198 pcg 1.4
199 pcg 1.9 if (display)
200     XCloseDisplay (display);
201 pcg 1.4 }
202    
203 pcg 1.6 void rxvt_display::im_change_cb ()
204     {
205     for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
206     (*i)->call ();
207     }
208    
209 pcg 1.7 void rxvt_display::x_cb (io_watcher &w, short revents)
210 pcg 1.4 {
211     do
212     {
213     XEvent xev;
214     XNextEvent (display, &xev);
215    
216 pcg 1.6 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
217    
218     if (xev.type == PropertyNotify
219     && xev.xany.window == root
220     && xev.xproperty.atom == xa_xim_servers)
221     im_change_cb ();
222    
223 pcg 1.4 for (int i = xw.size (); i--; )
224     {
225     if (!xw[i])
226     xw.erase_unordered (i);
227     else if (xw[i]->window == xev.xany.window)
228     xw[i]->call (xev);
229     }
230     }
231     while (XPending (display));
232 pcg 1.7
233     flush ();
234     }
235    
236     void rxvt_display::flush ()
237     {
238     for (;;)
239     {
240     XFlush (display);
241    
242     if (!XPending (display))
243     break;
244    
245     x_cb (x_ev, 0);
246     }
247 pcg 1.4 }
248    
249     void rxvt_display::reg (xevent_watcher *w)
250     {
251     xw.push_back (w);
252     w->active = xw.size ();
253     }
254    
255     void rxvt_display::unreg (xevent_watcher *w)
256     {
257     if (w->active)
258     xw[w->active - 1] = 0;
259     }
260    
261 pcg 1.6 void rxvt_display::reg (im_watcher *w)
262     {
263     imw.push_back (w);
264     }
265    
266     void rxvt_display::unreg (im_watcher *w)
267     {
268     imw.erase (find (imw.begin (), imw.end (), w));
269     }
270    
271 pcg 1.5 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 pcg 1.6 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
280 pcg 1.4 {
281 pcg 1.6 char *id;
282 pcg 1.12 int l, m;
283 pcg 1.4
284 pcg 1.12 l = strlen (locale);
285     m = strlen (modifiers);
286    
287     if (!(id = (char *)malloc (l + m + 2)))
288 pcg 1.6 return 0;
289 pcg 1.4
290 pcg 1.12 memcpy (id, locale, l); id[l] = '\n';
291     memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
292    
293 pcg 1.6 rxvt_xim *xim = xims.get (id);
294 pcg 1.4
295 pcg 1.6 free (id);
296 pcg 1.4
297 pcg 1.6 return xim;
298 pcg 1.4 }
299    
300 pcg 1.6 void rxvt_display::put_xim (rxvt_xim *xim)
301 pcg 1.4 {
302 pcg 1.6 xims.put (xim);
303 pcg 1.4 }
304    
305 pcg 1.11 Atom rxvt_display::atom (const char *name)
306     {
307     return XInternAtom (display, name, False);
308     }
309    
310 pcg 1.4 /////////////////////////////////////////////////////////////////////////////
311 pcg 1.6
312 root 1.15 template class refcache<rxvt_display>;
313 pcg 1.6 refcache<rxvt_display> displays;
314    
315     /////////////////////////////////////////////////////////////////////////////
316 pcg 1.4
317 pcg 1.1 bool
318 pcg 1.4 rxvt_color::set (rxvt_display *display, Pixel p)
319 pcg 1.1 {
320     #if XFT
321     XColor xc;
322    
323     xc.pixel = p;
324 pcg 1.4 if (!XQueryColor (display->display, display->cmap, &xc))
325 pcg 1.1 return false;
326    
327     XRenderColor d;
328    
329     d.red = xc.red;
330     d.green = xc.green;
331     d.blue = xc.blue;
332     d.alpha = 0xffff;
333    
334     return
335 pcg 1.4 XftColorAllocValue (display->display,
336     display->visual,
337     display->cmap,
338     &d, &c);
339 pcg 1.1 #else
340     this->p = p;
341     #endif
342    
343     return true;
344     }
345    
346     bool
347 pcg 1.4 rxvt_color::set (rxvt_display *display, const char *name)
348 pcg 1.1 {
349     XColor xc;
350    
351 pcg 1.4 if (XParseColor (display->display, display->cmap, name, &xc))
352     return set (display, xc.red, xc.green, xc.blue);
353 pcg 1.1
354     return false;
355     }
356    
357     bool
358 pcg 1.4 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
359 pcg 1.1 {
360     XColor xc;
361    
362     xc.red = cr;
363     xc.green = cg;
364     xc.blue = cb;
365     xc.flags = DoRed | DoGreen | DoBlue;
366    
367 pcg 1.4 if (XAllocColor (display->display, display->cmap, &xc))
368     return set (display, xc.pixel);
369 pcg 1.1
370     return false;
371     }
372    
373     void
374 pcg 1.4 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
375 pcg 1.1 {
376     #if XFT
377     cr = c.color.red;
378     cg = c.color.green;
379     cb = c.color.blue;
380     #else
381     XColor c;
382    
383     c.pixel = p;
384 pcg 1.4 XQueryColor (display->display, display->cmap, &c);
385 pcg 1.1
386     cr = c.red;
387     cg = c.green;
388     cb = c.blue;
389     #endif
390     }
391    
392 pcg 1.4 void
393     rxvt_color::free (rxvt_display *display)
394     {
395     #if XFT
396     XftColorFree (display->display, display->visual, display->cmap, &c);
397     #else
398 pcg 1.8 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
399 pcg 1.4 #endif
400     }
401