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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines