1 |
/*--------------------------------*-C-*---------------------------------* |
2 |
* File: rxvttoolkit.C |
3 |
*----------------------------------------------------------------------* |
4 |
* |
5 |
* All portions of code are copyright by their respective author/s. |
6 |
* Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
7 |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
10 |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 |
*----------------------------------------------------------------------*/ |
22 |
|
23 |
#include "../config.h" |
24 |
#include <rxvt.h> |
25 |
#include <rxvttoolkit.h> |
26 |
|
27 |
#include <unistd.h> |
28 |
#include <fcntl.h> |
29 |
|
30 |
#ifndef NO_SLOW_LINK_SUPPORT |
31 |
# include <sys/socket.h> |
32 |
# include <sys/un.h> |
33 |
#endif |
34 |
|
35 |
refcounted::refcounted (const char *id) |
36 |
{ |
37 |
this->id = strdup (id); |
38 |
} |
39 |
|
40 |
refcounted::~refcounted () |
41 |
{ |
42 |
free (id); |
43 |
} |
44 |
|
45 |
template<class T> |
46 |
T *refcache<T>::get (const char *id) |
47 |
{ |
48 |
for (T **i = this->begin (); i < this->end (); ++i) |
49 |
{ |
50 |
if (!strcmp (id, (*i)->id)) |
51 |
{ |
52 |
(*i)->referenced++; |
53 |
return *i; |
54 |
} |
55 |
} |
56 |
|
57 |
T *obj = new T (id); |
58 |
|
59 |
obj->referenced = 1; |
60 |
|
61 |
if (obj && obj->init ()) |
62 |
{ |
63 |
this->push_back (obj); |
64 |
return obj; |
65 |
} |
66 |
else |
67 |
{ |
68 |
delete obj; |
69 |
return 0; |
70 |
} |
71 |
} |
72 |
|
73 |
template<class T> |
74 |
void refcache<T>::put (T *obj) |
75 |
{ |
76 |
if (!obj) |
77 |
return; |
78 |
|
79 |
if (!--obj->referenced) |
80 |
{ |
81 |
this->erase (find (this->begin (), this->end (), obj)); |
82 |
delete obj; |
83 |
} |
84 |
} |
85 |
|
86 |
template<class T> |
87 |
refcache<T>::~refcache () |
88 |
{ |
89 |
while (this->size ()) |
90 |
put (*this->begin ()); |
91 |
} |
92 |
|
93 |
///////////////////////////////////////////////////////////////////////////// |
94 |
|
95 |
#ifdef USE_XIM |
96 |
static void |
97 |
#if XIMCB_PROTO_BROKEN |
98 |
im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
99 |
#else |
100 |
im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
101 |
#endif |
102 |
{ |
103 |
rxvt_xim *xim = (rxvt_xim *)client_data; |
104 |
rxvt_display *display = xim->display; |
105 |
|
106 |
xim->xim = 0; |
107 |
|
108 |
display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
109 |
display->im_change_cb (); |
110 |
} |
111 |
|
112 |
bool rxvt_xim::init () |
113 |
{ |
114 |
display = GET_R->display; //HACK: TODO |
115 |
|
116 |
xim = XOpenIM (display->display, NULL, NULL, NULL); |
117 |
|
118 |
if (!xim) |
119 |
return false; |
120 |
|
121 |
XIMCallback ximcallback; |
122 |
ximcallback.client_data = (XPointer)this; |
123 |
ximcallback.callback = im_destroy_cb; |
124 |
|
125 |
XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); |
126 |
|
127 |
return true; |
128 |
} |
129 |
|
130 |
rxvt_xim::~rxvt_xim () |
131 |
{ |
132 |
if (xim) |
133 |
XCloseIM (xim); |
134 |
} |
135 |
#endif |
136 |
|
137 |
///////////////////////////////////////////////////////////////////////////// |
138 |
|
139 |
rxvt_display::rxvt_display (const char *id) |
140 |
: refcounted (id) |
141 |
, x_ev (this, &rxvt_display::x_cb) |
142 |
, selection_owner (0) |
143 |
{ |
144 |
} |
145 |
|
146 |
bool rxvt_display::init () |
147 |
{ |
148 |
display = XOpenDisplay (id); |
149 |
|
150 |
if (!display) |
151 |
return false; |
152 |
|
153 |
screen = DefaultScreen (display); |
154 |
root = DefaultRootWindow (display); |
155 |
visual = DefaultVisual (display, screen); |
156 |
cmap = DefaultColormap (display, screen); |
157 |
depth = DefaultDepth (display, screen); |
158 |
|
159 |
int fd = XConnectionNumber (display); |
160 |
|
161 |
#ifndef NO_SLOW_LINK_SUPPORT |
162 |
// try to detetc wether we have a local connection. |
163 |
// assume unix domains socket == local, everything else not |
164 |
// TODO: might want to check for inet/127.0.0.1 |
165 |
is_local = 0; |
166 |
sockaddr_un sa; |
167 |
socklen_t sl = sizeof (sa); |
168 |
|
169 |
if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
170 |
is_local = sa.sun_family == AF_LOCAL; |
171 |
#endif |
172 |
|
173 |
#ifdef PREFER_24BIT |
174 |
/* |
175 |
* If depth is not 24, look for a 24bit visual. |
176 |
*/ |
177 |
if (depth != 24) |
178 |
{ |
179 |
XVisualInfo vinfo; |
180 |
|
181 |
if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo)) |
182 |
{ |
183 |
depth = 24; |
184 |
visual = vinfo.visual; |
185 |
cmap = XCreateColormap (display, |
186 |
RootWindow (display, screen), |
187 |
visual, AllocNone); |
188 |
} |
189 |
} |
190 |
#endif |
191 |
|
192 |
x_ev.start (fd, EVENT_READ); |
193 |
fcntl (fd, F_SETFD, FD_CLOEXEC); |
194 |
|
195 |
XSelectInput (display, root, PropertyChangeMask); |
196 |
#ifdef USE_XIM |
197 |
xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
198 |
#endif |
199 |
|
200 |
flush (); |
201 |
|
202 |
return true; |
203 |
} |
204 |
|
205 |
rxvt_display::~rxvt_display () |
206 |
{ |
207 |
x_ev.stop (); |
208 |
|
209 |
if (display) |
210 |
XCloseDisplay (display); |
211 |
} |
212 |
|
213 |
#ifdef USE_XIM |
214 |
void rxvt_display::im_change_cb () |
215 |
{ |
216 |
for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
217 |
(*i)->call (); |
218 |
} |
219 |
|
220 |
void rxvt_display::im_change_check () |
221 |
{ |
222 |
// make sure we only call im_change_cb when a new input method |
223 |
// registers, as xlib crashes due to a race otherwise. |
224 |
Atom actual_type, *atoms; |
225 |
int actual_format; |
226 |
unsigned long nitems, bytes_after; |
227 |
|
228 |
if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, |
229 |
False, XA_ATOM, &actual_type, &actual_format, |
230 |
&nitems, &bytes_after, (unsigned char **)&atoms) |
231 |
!= Success ) |
232 |
return; |
233 |
|
234 |
if (actual_type == XA_ATOM && actual_format == 32) |
235 |
for (int i = 0; i < nitems; i++) |
236 |
if (XGetSelectionOwner (display, atoms[i])) |
237 |
{ |
238 |
im_change_cb (); |
239 |
break; |
240 |
} |
241 |
|
242 |
XFree (atoms); |
243 |
} |
244 |
#endif |
245 |
|
246 |
void rxvt_display::x_cb (io_watcher &w, short revents) |
247 |
{ |
248 |
do |
249 |
{ |
250 |
XEvent xev; |
251 |
XNextEvent (display, &xev); |
252 |
|
253 |
#ifdef USE_XIM |
254 |
if (!XFilterEvent (&xev, None)) |
255 |
{ |
256 |
|
257 |
if (xev.type == PropertyNotify |
258 |
&& xev.xany.window == root |
259 |
&& xev.xproperty.atom == xa_xim_servers) |
260 |
im_change_check (); |
261 |
#endif |
262 |
for (int i = xw.size (); i--; ) |
263 |
{ |
264 |
if (!xw[i]) |
265 |
xw.erase_unordered (i); |
266 |
else if (xw[i]->window == xev.xany.window) |
267 |
xw[i]->call (xev); |
268 |
} |
269 |
#ifdef USE_XIM |
270 |
} |
271 |
#endif |
272 |
} |
273 |
while (XEventsQueued (display, QueuedAlready)); |
274 |
|
275 |
flush (); |
276 |
} |
277 |
|
278 |
void rxvt_display::flush () |
279 |
{ |
280 |
XFlush (display); |
281 |
} |
282 |
|
283 |
void rxvt_display::reg (xevent_watcher *w) |
284 |
{ |
285 |
xw.push_back (w); |
286 |
w->active = xw.size (); |
287 |
} |
288 |
|
289 |
void rxvt_display::unreg (xevent_watcher *w) |
290 |
{ |
291 |
if (w->active) |
292 |
xw[w->active - 1] = 0; |
293 |
} |
294 |
|
295 |
void rxvt_display::set_selection_owner (rxvt_term *owner) |
296 |
{ |
297 |
if (selection_owner && selection_owner != owner) |
298 |
selection_owner->selection_clear (); |
299 |
|
300 |
selection_owner = owner; |
301 |
} |
302 |
|
303 |
#ifdef USE_XIM |
304 |
void rxvt_display::reg (im_watcher *w) |
305 |
{ |
306 |
imw.push_back (w); |
307 |
} |
308 |
|
309 |
void rxvt_display::unreg (im_watcher *w) |
310 |
{ |
311 |
imw.erase (find (imw.begin (), imw.end (), w)); |
312 |
} |
313 |
|
314 |
rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
315 |
{ |
316 |
char *id; |
317 |
int l, m; |
318 |
|
319 |
l = strlen (locale); |
320 |
m = strlen (modifiers); |
321 |
|
322 |
if (!(id = (char *)malloc (l + m + 2))) |
323 |
return 0; |
324 |
|
325 |
memcpy (id, locale, l); id[l] = '\n'; |
326 |
memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
327 |
|
328 |
rxvt_xim *xim = xims.get (id); |
329 |
|
330 |
free (id); |
331 |
|
332 |
return xim; |
333 |
} |
334 |
|
335 |
void rxvt_display::put_xim (rxvt_xim *xim) |
336 |
{ |
337 |
xims.put (xim); |
338 |
} |
339 |
#endif |
340 |
|
341 |
Atom rxvt_display::atom (const char *name) |
342 |
{ |
343 |
return XInternAtom (display, name, False); |
344 |
} |
345 |
|
346 |
///////////////////////////////////////////////////////////////////////////// |
347 |
|
348 |
template class refcache<rxvt_display>; |
349 |
refcache<rxvt_display> displays; |
350 |
|
351 |
///////////////////////////////////////////////////////////////////////////// |
352 |
|
353 |
bool |
354 |
rxvt_color::set (rxvt_display *display, Pixel p) |
355 |
{ |
356 |
#if XFT |
357 |
XColor xc; |
358 |
|
359 |
xc.pixel = p; |
360 |
if (!XQueryColor (display->display, display->cmap, &xc)) |
361 |
return false; |
362 |
|
363 |
XRenderColor d; |
364 |
|
365 |
d.red = xc.red; |
366 |
d.green = xc.green; |
367 |
d.blue = xc.blue; |
368 |
d.alpha = 0xffff; |
369 |
|
370 |
return |
371 |
XftColorAllocValue (display->display, |
372 |
display->visual, |
373 |
display->cmap, |
374 |
&d, &c); |
375 |
#else |
376 |
this->p = p; |
377 |
#endif |
378 |
|
379 |
return true; |
380 |
} |
381 |
|
382 |
bool |
383 |
rxvt_color::set (rxvt_display *display, const char *name) |
384 |
{ |
385 |
#if XFT |
386 |
return XftColorAllocName (display->display, display->visual, display->cmap, |
387 |
name, &c); |
388 |
#else |
389 |
XColor xc; |
390 |
|
391 |
if (XParseColor (display->display, display->cmap, name, &xc)) |
392 |
return set (display, xc.red, xc.green, xc.blue); |
393 |
|
394 |
return false; |
395 |
#endif |
396 |
} |
397 |
|
398 |
bool |
399 |
rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) |
400 |
{ |
401 |
XColor xc; |
402 |
|
403 |
xc.red = cr; |
404 |
xc.green = cg; |
405 |
xc.blue = cb; |
406 |
xc.flags = DoRed | DoGreen | DoBlue; |
407 |
|
408 |
if (XAllocColor (display->display, display->cmap, &xc)) |
409 |
return set (display, xc.pixel); |
410 |
|
411 |
return false; |
412 |
} |
413 |
|
414 |
void |
415 |
rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) |
416 |
{ |
417 |
#if XFT |
418 |
cr = c.color.red; |
419 |
cg = c.color.green; |
420 |
cb = c.color.blue; |
421 |
#else |
422 |
XColor c; |
423 |
|
424 |
c.pixel = p; |
425 |
XQueryColor (display->display, display->cmap, &c); |
426 |
|
427 |
cr = c.red; |
428 |
cg = c.green; |
429 |
cb = c.blue; |
430 |
#endif |
431 |
} |
432 |
|
433 |
void |
434 |
rxvt_color::free (rxvt_display *display) |
435 |
{ |
436 |
#if XFT |
437 |
XftColorFree (display->display, display->visual, display->cmap, &c); |
438 |
#else |
439 |
XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
440 |
#endif |
441 |
} |
442 |
|
443 |
rxvt_color |
444 |
rxvt_color::fade (rxvt_display *display, int percent) |
445 |
{ |
446 |
unsigned short cr, cg, cb; |
447 |
rxvt_color faded; |
448 |
|
449 |
get (display, cr, cg, cb); |
450 |
faded.set (display, |
451 |
cr * percent / 100, |
452 |
cg * percent / 100, |
453 |
cb * percent / 100); |
454 |
|
455 |
return faded; |
456 |
} |
457 |
|