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