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.3 by pcg, Sat Jan 31 00:20:21 2004 UTC vs.
Revision 1.20 by root, Fri Jul 30 16:04:05 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
109#if XIMCB_PROTO_BROKEN
110im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
111#else
112im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
113#endif
114{
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
123bool 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 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
137
138 return true;
139}
140
141rxvt_xim::~rxvt_xim ()
142{
143 if (xim)
144 XCloseIM (xim);
145}
146#endif
147
148/////////////////////////////////////////////////////////////////////////////
149
150rxvt_display::rxvt_display (const char *id)
151: refcounted (id)
152, x_ev (this, &rxvt_display::x_cb)
153, selection_owner (0)
154{
155}
156
157bool rxvt_display::init ()
158{
159 display = XOpenDisplay (id);
160
161 if (!display)
162 return false;
163
164 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 x_ev.start (fd, EVENT_READ);
191 fcntl (fd, F_SETFD, FD_CLOEXEC);
192
193 XSelectInput (display, root, PropertyChangeMask);
194#ifdef USE_XIM
195 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
196#endif
197
198 flush ();
199
200 return true;
201}
202
203rxvt_display::~rxvt_display ()
204{
205 x_ev.stop ();
206
207 if (display)
208 XCloseDisplay (display);
209}
210
211#ifdef USE_XIM
212void rxvt_display::im_change_cb ()
213{
214 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
215 (*i)->call ();
216}
217#endif
218
219void rxvt_display::x_cb (io_watcher &w, short revents)
220{
221 do
222 {
223 XEvent xev;
224 XNextEvent (display, &xev);
225
226 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
227
228#ifdef USE_XIM
229 if (xev.type == PropertyNotify
230 && xev.xany.window == root
231 && xev.xproperty.atom == xa_xim_servers)
232 im_change_cb ();
233#endif
234
235 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
245 flush ();
246}
247
248void rxvt_display::flush ()
249{
250 for (;;)
251 {
252 XFlush (display);
253
254 if (!XPending (display))
255 break;
256
257 x_cb (x_ev, 0);
258 }
259}
260
261void rxvt_display::reg (xevent_watcher *w)
262{
263 xw.push_back (w);
264 w->active = xw.size ();
265}
266
267void rxvt_display::unreg (xevent_watcher *w)
268{
269 if (w->active)
270 xw[w->active - 1] = 0;
271}
272
273void rxvt_display::set_selection_owner (rxvt_term *owner)
274{
275 if (selection_owner && selection_owner != owner)
276 selection_owner->selection_clear ();
277
278 selection_owner = owner;
279}
280
281#ifdef USE_XIM
282void rxvt_display::reg (im_watcher *w)
283{
284 imw.push_back (w);
285}
286
287void rxvt_display::unreg (im_watcher *w)
288{
289 imw.erase (find (imw.begin (), imw.end (), w));
290}
291
292rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
293{
294 char *id;
295 int l, m;
296
297 l = strlen (locale);
298 m = strlen (modifiers);
299
300 if (!(id = (char *)malloc (l + m + 2)))
301 return 0;
302
303 memcpy (id, locale, l); id[l] = '\n';
304 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
305
306 rxvt_xim *xim = xims.get (id);
307
308 free (id);
309
310 return xim;
311}
312
313void rxvt_display::put_xim (rxvt_xim *xim)
314{
315 xims.put (xim);
316}
317#endif
318
319Atom rxvt_display::atom (const char *name)
320{
321 return XInternAtom (display, name, False);
322}
323
324/////////////////////////////////////////////////////////////////////////////
325
326template class refcache<rxvt_display>;
327refcache<rxvt_display> displays;
328
329/////////////////////////////////////////////////////////////////////////////
330
6bool 331bool
7rxvt_color::set (rxvt_term *t, Pixel p) 332rxvt_color::set (rxvt_display *display, Pixel p)
8{ 333{
9#if XFT 334#if XFT
10 XColor xc; 335 XColor xc;
11 336
12 xc.pixel = p; 337 xc.pixel = p;
13 if (!XQueryColor (t->Xdisplay, t->Xcmap, &xc)) 338 if (!XQueryColor (display->display, display->cmap, &xc))
14 return false; 339 return false;
15 340
16 XRenderColor d; 341 XRenderColor d;
17 342
18 d.red = xc.red; 343 d.red = xc.red;
19 d.green = xc.green; 344 d.green = xc.green;
20 d.blue = xc.blue; 345 d.blue = xc.blue;
21 d.alpha = 0xffff; 346 d.alpha = 0xffff;
22 347
23 return 348 return
24 XftColorAllocValue (t->Xdisplay, 349 XftColorAllocValue (display->display,
25 t->Xvisual, 350 display->visual,
26 t->Xcmap, 351 display->cmap,
27 &d,
28 &c); 352 &d, &c);
29#else 353#else
30 this->p = p; 354 this->p = p;
31#endif 355#endif
32 356
33 return true; 357 return true;
34} 358}
35 359
36bool 360bool
37rxvt_color::set (rxvt_term *t, const char *name) 361rxvt_color::set (rxvt_display *display, const char *name)
38{ 362{
39 XColor xc; 363 XColor xc;
40 364
41 if (XParseColor (t->Xdisplay, t->Xcmap, name, &xc)) 365 if (XParseColor (display->display, display->cmap, name, &xc))
42 return set (t, xc.red, xc.green, xc.blue); 366 return set (display, xc.red, xc.green, xc.blue);
43 367
44 return false; 368 return false;
45} 369}
46 370
47bool 371bool
48rxvt_color::set (rxvt_term *t, unsigned short cr, unsigned short cg, unsigned short cb) 372rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
49{ 373{
50 XColor xc; 374 XColor xc;
51 375
52 xc.red = cr; 376 xc.red = cr;
53 xc.green = cg; 377 xc.green = cg;
54 xc.blue = cb; 378 xc.blue = cb;
55 xc.flags = DoRed | DoGreen | DoBlue; 379 xc.flags = DoRed | DoGreen | DoBlue;
56 380
57 if (XAllocColor (t->Xdisplay, t->Xcmap, &xc)) 381 if (XAllocColor (display->display, display->cmap, &xc))
58 return set (t, xc.pixel); 382 return set (display, xc.pixel);
59 383
60 return false; 384 return false;
61} 385}
62 386
63void 387void
64rxvt_color::get (rxvt_term *t, unsigned short &cr, unsigned short &cg, unsigned short &cb) 388rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
65{ 389{
66#if XFT 390#if XFT
67 cr = c.color.red; 391 cr = c.color.red;
68 cg = c.color.green; 392 cg = c.color.green;
69 cb = c.color.blue; 393 cb = c.color.blue;
70#else 394#else
71 XColor c; 395 XColor c;
72 396
73 c.pixel = p; 397 c.pixel = p;
74 XQueryColor (t->Xdisplay, t->Xcmap, &c); 398 XQueryColor (display->display, display->cmap, &c);
75 399
76 cr = c.red; 400 cr = c.red;
77 cg = c.green; 401 cg = c.green;
78 cb = c.blue; 402 cb = c.blue;
79#endif 403#endif
80} 404}
81 405
406void
407rxvt_color::free (rxvt_display *display)
408{
409#if XFT
410 XftColorFree (display->display, display->visual, display->cmap, &c);
411#else
412 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
413#endif
414}
415
416rxvt_color
417rxvt_color::fade (rxvt_display *display, int percent)
418{
419 unsigned short cr, cg, cb;
420 rxvt_color faded;
421
422 get (display, cr, cg, cb);
423 faded.set (display,
424 cr * percent / 100,
425 cg * percent / 100,
426 cb * percent / 100);
427
428 return faded;
429}
430

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines