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.4 by pcg, Mon Feb 9 07:11:49 2004 UTC vs.
Revision 1.9 by pcg, Sun Mar 14 17:33:08 2004 UTC

3#include <rxvtcolor.h> 3#include <rxvtcolor.h>
4 4
5#include <unistd.h> 5#include <unistd.h>
6#include <fcntl.h> 6#include <fcntl.h>
7 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
8///////////////////////////////////////////////////////////////////////////// 66/////////////////////////////////////////////////////////////////////////////
9 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
10rxvt_display::rxvt_display (const char *name) 105rxvt_display::rxvt_display (const char *id)
106: refcounted (id)
11: x_watcher (this, &rxvt_display::x_event) 107, x_ev (this, &rxvt_display::x_cb)
108, selection_owner (0)
12{ 109{
13 this->name = STRDUP (name);
14} 110}
15 111
16rxvt_display::~rxvt_display ()
17{
18 free (name);
19}
20
21bool rxvt_display::open () 112bool rxvt_display::init ()
22{ 113{
23 display = XOpenDisplay (name); 114 display = XOpenDisplay (id);
115
116 if (!display)
117 return false;
24 118
25 screen = DefaultScreen (display); 119 screen = DefaultScreen (display);
26 root = DefaultRootWindow (display); 120 root = DefaultRootWindow (display);
27 visual = DefaultVisual (display, screen); 121 visual = DefaultVisual (display, screen);
28 cmap = DefaultColormap (display, screen); 122 cmap = DefaultColormap (display, screen);
46 } 140 }
47 } 141 }
48#endif 142#endif
49 143
50 int fd = XConnectionNumber (display); 144 int fd = XConnectionNumber (display);
51 x_watcher.start (fd, EVENT_READ); 145 x_ev.start (fd, EVENT_READ);
52 fcntl (fd, F_SETFL, FD_CLOEXEC); 146 fcntl (fd, F_SETFL, FD_CLOEXEC);
53 147
148 XSelectInput (display, root, PropertyChangeMask);
149 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
150
151 flush ();
152
54 return true; 153 return true;
55} 154}
56 155
57void rxvt_display::close () 156rxvt_display::~rxvt_display ()
58{ 157{
59 x_watcher.stop (); 158 x_ev.stop ();
60 159
160 if (display)
61 XCloseDisplay (display); 161 XCloseDisplay (display);
62} 162}
63 163
164void rxvt_display::im_change_cb ()
165{
166 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
167 (*i)->call ();
168}
169
64void rxvt_display::x_event (io_watcher &w, short revents) 170void rxvt_display::x_cb (io_watcher &w, short revents)
65{ 171{
66 do 172 do
67 { 173 {
68 XEvent xev; 174 XEvent xev;
69 XNextEvent (display, &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 ();
70 183
71 for (int i = xw.size (); i--; ) 184 for (int i = xw.size (); i--; )
72 { 185 {
73 if (!xw[i]) 186 if (!xw[i])
74 xw.erase_unordered (i); 187 xw.erase_unordered (i);
75 else if (xw[i]->window == xev.xany.window) 188 else if (xw[i]->window == xev.xany.window)
76 xw[i]->call (xev); 189 xw[i]->call (xev);
77 } 190 }
78 } 191 }
79 while (XPending (display)); 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 }
80} 208}
81 209
82void rxvt_display::reg (xevent_watcher *w) 210void rxvt_display::reg (xevent_watcher *w)
83{ 211{
84 xw.push_back (w); 212 xw.push_back (w);
89{ 217{
90 if (w->active) 218 if (w->active)
91 xw[w->active - 1] = 0; 219 xw[w->active - 1] = 0;
92} 220}
93 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
94///////////////////////////////////////////////////////////////////////////// 260/////////////////////////////////////////////////////////////////////////////
95 261
262template refcache<rxvt_display>;
96rxvt_displays displays; 263refcache<rxvt_display> displays;
97
98rxvt_display *rxvt_displays::get (const char *name)
99{
100 for (rxvt_display **i = list.begin (); i < list.end (); ++i)
101 {
102 if (!strcmp (name, (*i)->name))
103 {
104 (*i)->referenced++;
105 return *i;
106 }
107 }
108
109 rxvt_display *display = new rxvt_display (name);
110
111 display->referenced = 1;
112
113 if (display && display->open ())
114 list.push_back (display);
115 else
116 {
117 delete display;
118 display = 0;
119 }
120
121 return display;
122}
123
124void rxvt_displays::release (rxvt_display *display)
125{
126 if (!--display->referenced)
127 {
128 display->close ();
129 delete display;
130 list.erase (find (list.begin (), list.end (), display));
131 }
132}
133 264
134///////////////////////////////////////////////////////////////////////////// 265/////////////////////////////////////////////////////////////////////////////
135 266
136bool 267bool
137rxvt_color::set (rxvt_display *display, Pixel p) 268rxvt_color::set (rxvt_display *display, Pixel p)
212rxvt_color::free (rxvt_display *display) 343rxvt_color::free (rxvt_display *display)
213{ 344{
214#if XFT 345#if XFT
215 XftColorFree (display->display, display->visual, display->cmap, &c); 346 XftColorFree (display->display, display->visual, display->cmap, &c);
216#else 347#else
217 XFreeColors (display->display, display->cmap, &c, 1, AllPlanes); 348 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
218#endif 349#endif
219} 350}
220 351

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines