1 |
pcg |
1.1 |
#include "../config.h" |
2 |
|
|
#include <rxvt.h> |
3 |
pcg |
1.4 |
#include <rxvtcolor.h> |
4 |
pcg |
1.1 |
|
5 |
pcg |
1.4 |
#include <unistd.h> |
6 |
|
|
#include <fcntl.h> |
7 |
pcg |
1.1 |
|
8 |
pcg |
1.10 |
class byteorder byteorder; |
9 |
|
|
|
10 |
|
|
byteorder::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 |
|
|
|
25 |
pcg |
1.6 |
refcounted::refcounted (const char *id) |
26 |
|
|
{ |
27 |
|
|
this->id = STRDUP (id); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
refcounted::~refcounted () |
31 |
|
|
{ |
32 |
|
|
free (id); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
template<class T> |
36 |
|
|
T *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 |
|
|
|
63 |
|
|
template<class T> |
64 |
|
|
void 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 |
|
|
|
76 |
|
|
template<class T> |
77 |
|
|
refcache<T>::~refcache () |
78 |
|
|
{ |
79 |
|
|
while (size ()) |
80 |
|
|
put (*begin ()); |
81 |
|
|
} |
82 |
|
|
|
83 |
pcg |
1.4 |
///////////////////////////////////////////////////////////////////////////// |
84 |
|
|
|
85 |
pcg |
1.6 |
static void |
86 |
|
|
im_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 |
|
|
|
96 |
|
|
bool 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 |
|
|
|
114 |
|
|
rxvt_xim::~rxvt_xim () |
115 |
pcg |
1.4 |
{ |
116 |
pcg |
1.6 |
if (xim) |
117 |
|
|
XCloseIM (xim); |
118 |
pcg |
1.4 |
} |
119 |
|
|
|
120 |
pcg |
1.6 |
///////////////////////////////////////////////////////////////////////////// |
121 |
|
|
|
122 |
|
|
rxvt_display::rxvt_display (const char *id) |
123 |
|
|
: refcounted (id) |
124 |
pcg |
1.7 |
, x_ev (this, &rxvt_display::x_cb) |
125 |
pcg |
1.6 |
, selection_owner (0) |
126 |
pcg |
1.4 |
{ |
127 |
|
|
} |
128 |
|
|
|
129 |
pcg |
1.6 |
bool rxvt_display::init () |
130 |
pcg |
1.4 |
{ |
131 |
pcg |
1.6 |
display = XOpenDisplay (id); |
132 |
pcg |
1.4 |
|
133 |
pcg |
1.9 |
if (!display) |
134 |
|
|
return false; |
135 |
|
|
|
136 |
pcg |
1.4 |
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 |
pcg |
1.7 |
x_ev.start (fd, EVENT_READ); |
163 |
pcg |
1.4 |
fcntl (fd, F_SETFL, FD_CLOEXEC); |
164 |
|
|
|
165 |
pcg |
1.6 |
XSelectInput (display, root, PropertyChangeMask); |
166 |
|
|
xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
167 |
|
|
|
168 |
pcg |
1.7 |
flush (); |
169 |
|
|
|
170 |
pcg |
1.4 |
return true; |
171 |
|
|
} |
172 |
|
|
|
173 |
pcg |
1.6 |
rxvt_display::~rxvt_display () |
174 |
pcg |
1.4 |
{ |
175 |
pcg |
1.7 |
x_ev.stop (); |
176 |
pcg |
1.4 |
|
177 |
pcg |
1.9 |
if (display) |
178 |
|
|
XCloseDisplay (display); |
179 |
pcg |
1.4 |
} |
180 |
|
|
|
181 |
pcg |
1.6 |
void rxvt_display::im_change_cb () |
182 |
|
|
{ |
183 |
|
|
for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
184 |
|
|
(*i)->call (); |
185 |
|
|
} |
186 |
|
|
|
187 |
pcg |
1.7 |
void rxvt_display::x_cb (io_watcher &w, short revents) |
188 |
pcg |
1.4 |
{ |
189 |
|
|
do |
190 |
|
|
{ |
191 |
|
|
XEvent xev; |
192 |
|
|
XNextEvent (display, &xev); |
193 |
|
|
|
194 |
pcg |
1.6 |
//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 |
pcg |
1.4 |
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 |
pcg |
1.7 |
|
211 |
|
|
flush (); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
void 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 |
pcg |
1.4 |
} |
226 |
|
|
|
227 |
|
|
void rxvt_display::reg (xevent_watcher *w) |
228 |
|
|
{ |
229 |
|
|
xw.push_back (w); |
230 |
|
|
w->active = xw.size (); |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
void rxvt_display::unreg (xevent_watcher *w) |
234 |
|
|
{ |
235 |
|
|
if (w->active) |
236 |
|
|
xw[w->active - 1] = 0; |
237 |
|
|
} |
238 |
|
|
|
239 |
pcg |
1.6 |
void rxvt_display::reg (im_watcher *w) |
240 |
|
|
{ |
241 |
|
|
imw.push_back (w); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
void rxvt_display::unreg (im_watcher *w) |
245 |
|
|
{ |
246 |
|
|
imw.erase (find (imw.begin (), imw.end (), w)); |
247 |
|
|
} |
248 |
|
|
|
249 |
pcg |
1.5 |
void 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 |
|
|
|
257 |
pcg |
1.6 |
rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
258 |
pcg |
1.4 |
{ |
259 |
pcg |
1.6 |
char *id; |
260 |
pcg |
1.12 |
int l, m; |
261 |
pcg |
1.4 |
|
262 |
pcg |
1.12 |
l = strlen (locale); |
263 |
|
|
m = strlen (modifiers); |
264 |
|
|
|
265 |
|
|
if (!(id = (char *)malloc (l + m + 2))) |
266 |
pcg |
1.6 |
return 0; |
267 |
pcg |
1.4 |
|
268 |
pcg |
1.12 |
memcpy (id, locale, l); id[l] = '\n'; |
269 |
|
|
memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
270 |
|
|
|
271 |
pcg |
1.6 |
rxvt_xim *xim = xims.get (id); |
272 |
pcg |
1.4 |
|
273 |
pcg |
1.6 |
free (id); |
274 |
pcg |
1.4 |
|
275 |
pcg |
1.6 |
return xim; |
276 |
pcg |
1.4 |
} |
277 |
|
|
|
278 |
pcg |
1.6 |
void rxvt_display::put_xim (rxvt_xim *xim) |
279 |
pcg |
1.4 |
{ |
280 |
pcg |
1.6 |
xims.put (xim); |
281 |
pcg |
1.4 |
} |
282 |
|
|
|
283 |
pcg |
1.11 |
Atom rxvt_display::atom (const char *name) |
284 |
|
|
{ |
285 |
|
|
return XInternAtom (display, name, False); |
286 |
|
|
} |
287 |
|
|
|
288 |
pcg |
1.4 |
///////////////////////////////////////////////////////////////////////////// |
289 |
pcg |
1.6 |
|
290 |
|
|
template refcache<rxvt_display>; |
291 |
|
|
refcache<rxvt_display> displays; |
292 |
|
|
|
293 |
|
|
///////////////////////////////////////////////////////////////////////////// |
294 |
pcg |
1.4 |
|
295 |
pcg |
1.1 |
bool |
296 |
pcg |
1.4 |
rxvt_color::set (rxvt_display *display, Pixel p) |
297 |
pcg |
1.1 |
{ |
298 |
|
|
#if XFT |
299 |
|
|
XColor xc; |
300 |
|
|
|
301 |
|
|
xc.pixel = p; |
302 |
pcg |
1.4 |
if (!XQueryColor (display->display, display->cmap, &xc)) |
303 |
pcg |
1.1 |
return false; |
304 |
|
|
|
305 |
|
|
XRenderColor d; |
306 |
|
|
|
307 |
|
|
d.red = xc.red; |
308 |
|
|
d.green = xc.green; |
309 |
|
|
d.blue = xc.blue; |
310 |
|
|
d.alpha = 0xffff; |
311 |
|
|
|
312 |
|
|
return |
313 |
pcg |
1.4 |
XftColorAllocValue (display->display, |
314 |
|
|
display->visual, |
315 |
|
|
display->cmap, |
316 |
|
|
&d, &c); |
317 |
pcg |
1.1 |
#else |
318 |
|
|
this->p = p; |
319 |
|
|
#endif |
320 |
|
|
|
321 |
|
|
return true; |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
bool |
325 |
pcg |
1.4 |
rxvt_color::set (rxvt_display *display, const char *name) |
326 |
pcg |
1.1 |
{ |
327 |
|
|
XColor xc; |
328 |
|
|
|
329 |
pcg |
1.4 |
if (XParseColor (display->display, display->cmap, name, &xc)) |
330 |
|
|
return set (display, xc.red, xc.green, xc.blue); |
331 |
pcg |
1.1 |
|
332 |
|
|
return false; |
333 |
|
|
} |
334 |
|
|
|
335 |
|
|
bool |
336 |
pcg |
1.4 |
rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) |
337 |
pcg |
1.1 |
{ |
338 |
|
|
XColor xc; |
339 |
|
|
|
340 |
|
|
xc.red = cr; |
341 |
|
|
xc.green = cg; |
342 |
|
|
xc.blue = cb; |
343 |
|
|
xc.flags = DoRed | DoGreen | DoBlue; |
344 |
|
|
|
345 |
pcg |
1.4 |
if (XAllocColor (display->display, display->cmap, &xc)) |
346 |
|
|
return set (display, xc.pixel); |
347 |
pcg |
1.1 |
|
348 |
|
|
return false; |
349 |
|
|
} |
350 |
|
|
|
351 |
|
|
void |
352 |
pcg |
1.4 |
rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) |
353 |
pcg |
1.1 |
{ |
354 |
|
|
#if XFT |
355 |
|
|
cr = c.color.red; |
356 |
|
|
cg = c.color.green; |
357 |
|
|
cb = c.color.blue; |
358 |
|
|
#else |
359 |
|
|
XColor c; |
360 |
|
|
|
361 |
|
|
c.pixel = p; |
362 |
pcg |
1.4 |
XQueryColor (display->display, display->cmap, &c); |
363 |
pcg |
1.1 |
|
364 |
|
|
cr = c.red; |
365 |
|
|
cg = c.green; |
366 |
|
|
cb = c.blue; |
367 |
|
|
#endif |
368 |
|
|
} |
369 |
|
|
|
370 |
pcg |
1.4 |
void |
371 |
|
|
rxvt_color::free (rxvt_display *display) |
372 |
|
|
{ |
373 |
|
|
#if XFT |
374 |
|
|
XftColorFree (display->display, display->visual, display->cmap, &c); |
375 |
|
|
#else |
376 |
pcg |
1.8 |
XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
377 |
pcg |
1.4 |
#endif |
378 |
|
|
} |
379 |
|
|
|