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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines