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.2 by pcg, Mon Jan 19 17:26:43 2004 UTC vs.
Revision 1.11 by pcg, Tue Mar 30 14:45:13 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines