ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtcolor.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtcolor.C (file contents):
Revision 1.1 by pcg, Mon Nov 24 17:28:08 2003 UTC vs.
Revision 1.17 by root, Thu Jul 29 22:34:12 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines