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 |
#include <sys/utsname.h> |
31 |
|
32 |
#ifndef NO_SLOW_LINK_SUPPORT |
33 |
# include <sys/socket.h> |
34 |
# include <sys/un.h> |
35 |
#endif |
36 |
|
37 |
const char *const xa_names[] = |
38 |
{ |
39 |
"TEXT", |
40 |
"COMPOUND_TEXT", |
41 |
"UTF8_STRING", |
42 |
"MULTIPLE", |
43 |
"TARGETS", |
44 |
"TIMESTAMP", |
45 |
"VT_SELECTION", |
46 |
"INCR", |
47 |
"WM_PROTOCOLS", |
48 |
"WM_DELETE_WINDOW", |
49 |
"CLIPBOARD", |
50 |
#if ENABLE_FRILLS |
51 |
"_MOTIF_WM_HINTS", |
52 |
#endif |
53 |
#if ENABLE_EWMH |
54 |
"_NET_WM_PID", |
55 |
"_NET_WM_NAME", |
56 |
"_NET_WM_ICON_NAME", |
57 |
"_NET_WM_PING", |
58 |
#endif |
59 |
#if USE_XIM |
60 |
"WM_LOCALE_NAME", |
61 |
"XIM_SERVERS", |
62 |
#endif |
63 |
#ifdef TRANSPARENT |
64 |
"_XROOTPMAP_ID", |
65 |
"ESETROOT_PMAP_ID", |
66 |
#endif |
67 |
#if ENABLE_XEMBED |
68 |
"_XEMBED", |
69 |
"_XEMBED_INFO", |
70 |
#endif |
71 |
}; |
72 |
|
73 |
///////////////////////////////////////////////////////////////////////////// |
74 |
|
75 |
refcounted::refcounted (const char *id) |
76 |
{ |
77 |
this->id = strdup (id); |
78 |
} |
79 |
|
80 |
refcounted::~refcounted () |
81 |
{ |
82 |
free (id); |
83 |
} |
84 |
|
85 |
template<class T> |
86 |
T *refcache<T>::get (const char *id) |
87 |
{ |
88 |
for (T **i = this->begin (); i < this->end (); ++i) |
89 |
{ |
90 |
if (!strcmp (id, (*i)->id)) |
91 |
{ |
92 |
++(*i)->referenced; |
93 |
(*i)->ref_next (); |
94 |
return *i; |
95 |
} |
96 |
} |
97 |
|
98 |
T *obj = new T (id); |
99 |
|
100 |
if (obj && obj->ref_init ()) |
101 |
{ |
102 |
obj->referenced = 1; |
103 |
this->push_back (obj); |
104 |
return obj; |
105 |
} |
106 |
else |
107 |
{ |
108 |
delete obj; |
109 |
return 0; |
110 |
} |
111 |
} |
112 |
|
113 |
template<class T> |
114 |
void refcache<T>::put (T *obj) |
115 |
{ |
116 |
if (!obj) |
117 |
return; |
118 |
|
119 |
if (!--obj->referenced) |
120 |
{ |
121 |
this->erase (find (this->begin (), this->end (), obj)); |
122 |
delete obj; |
123 |
} |
124 |
} |
125 |
|
126 |
template<class T> |
127 |
void refcache<T>::clear () |
128 |
{ |
129 |
while (this->size ()) |
130 |
put (*this->begin ()); |
131 |
} |
132 |
|
133 |
///////////////////////////////////////////////////////////////////////////// |
134 |
|
135 |
#ifdef USE_XIM |
136 |
|
137 |
static void |
138 |
#if XIMCB_PROTO_BROKEN |
139 |
im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
140 |
#else |
141 |
im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
142 |
#endif |
143 |
{ |
144 |
rxvt_xim *xim = (rxvt_xim *)client_data; |
145 |
rxvt_display *display = xim->display; |
146 |
|
147 |
xim->xim = 0; |
148 |
|
149 |
display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
150 |
display->im_change_cb (); |
151 |
} |
152 |
|
153 |
bool |
154 |
rxvt_xim::ref_init () |
155 |
{ |
156 |
display = GET_R->display; //HACK: TODO |
157 |
|
158 |
xim = XOpenIM (display->display, NULL, NULL, NULL); |
159 |
|
160 |
if (!xim) |
161 |
return false; |
162 |
|
163 |
XIMCallback ximcallback; |
164 |
ximcallback.client_data = (XPointer)this; |
165 |
ximcallback.callback = im_destroy_cb; |
166 |
|
167 |
XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); |
168 |
|
169 |
return true; |
170 |
} |
171 |
|
172 |
rxvt_xim::~rxvt_xim () |
173 |
{ |
174 |
if (xim) |
175 |
XCloseIM (xim); |
176 |
} |
177 |
|
178 |
#endif |
179 |
|
180 |
///////////////////////////////////////////////////////////////////////////// |
181 |
|
182 |
rxvt_display::rxvt_display (const char *id) |
183 |
: refcounted (id) |
184 |
, x_ev (this, &rxvt_display::x_cb) |
185 |
, selection_owner (0) |
186 |
{ |
187 |
} |
188 |
|
189 |
XrmDatabase |
190 |
rxvt_display::get_resources () |
191 |
{ |
192 |
char *homedir = (char *)getenv ("HOME"); |
193 |
char fname[1024]; |
194 |
|
195 |
/* |
196 |
* get resources using the X library function |
197 |
*/ |
198 |
char *displayResource, *xe; |
199 |
XrmDatabase database, rdb1; |
200 |
|
201 |
database = NULL; |
202 |
|
203 |
// for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
204 |
|
205 |
// 6. System wide per application default file. |
206 |
|
207 |
/* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ |
208 |
if ((xe = (char *)getenv ("XAPPLRESDIR"))) |
209 |
{ |
210 |
snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); |
211 |
|
212 |
if ((rdb1 = XrmGetFileDatabase (fname))) |
213 |
XrmMergeDatabases (rdb1, &database); |
214 |
} |
215 |
|
216 |
// 5. User's per application default file. |
217 |
// none |
218 |
|
219 |
// 4. User's defaults file. |
220 |
/* Get any Xserver defaults */ |
221 |
displayResource = XResourceManagerString (display); |
222 |
|
223 |
if (displayResource != NULL) |
224 |
{ |
225 |
if ((rdb1 = XrmGetStringDatabase (displayResource))) |
226 |
XrmMergeDatabases (rdb1, &database); |
227 |
} |
228 |
else if (homedir) |
229 |
{ |
230 |
snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir); |
231 |
|
232 |
if ((rdb1 = XrmGetFileDatabase (fname))) |
233 |
XrmMergeDatabases (rdb1, &database); |
234 |
} |
235 |
|
236 |
/* Get screen specific resources */ |
237 |
displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); |
238 |
|
239 |
if (displayResource != NULL) |
240 |
{ |
241 |
if ((rdb1 = XrmGetStringDatabase (displayResource))) |
242 |
/* Merge with screen-independent resources */ |
243 |
XrmMergeDatabases (rdb1, &database); |
244 |
|
245 |
XFree (displayResource); |
246 |
} |
247 |
|
248 |
// 3. User's per host defaults file |
249 |
/* Add in XENVIRONMENT file */ |
250 |
if ((xe = (char *)getenv ("XENVIRONMENT")) |
251 |
&& (rdb1 = XrmGetFileDatabase (xe))) |
252 |
XrmMergeDatabases (rdb1, &database); |
253 |
else if (homedir) |
254 |
{ |
255 |
struct utsname un; |
256 |
|
257 |
if (!uname (&un)) |
258 |
{ |
259 |
snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename); |
260 |
|
261 |
if ((rdb1 = XrmGetFileDatabase (fname))) |
262 |
XrmMergeDatabases (rdb1, &database); |
263 |
} |
264 |
} |
265 |
|
266 |
return database; |
267 |
} |
268 |
|
269 |
bool rxvt_display::ref_init () |
270 |
{ |
271 |
#ifdef LOCAL_X_IS_UNIX |
272 |
if (id[0] == ':') |
273 |
{ |
274 |
val = rxvt_malloc (5 + strlen (id) + 1); |
275 |
strcpy (val, "unix/"); |
276 |
strcat (val, id); |
277 |
display = XOpenDisplay (val); |
278 |
free (val); |
279 |
} |
280 |
else |
281 |
#endif |
282 |
display = 0; |
283 |
|
284 |
if (!display) |
285 |
display = XOpenDisplay (id); |
286 |
|
287 |
if (!display) |
288 |
return false; |
289 |
|
290 |
screen = DefaultScreen (display); |
291 |
root = DefaultRootWindow (display); |
292 |
visual = DefaultVisual (display, screen); |
293 |
cmap = DefaultColormap (display, screen); |
294 |
depth = DefaultDepth (display, screen); |
295 |
|
296 |
assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
297 |
XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); |
298 |
|
299 |
XrmSetDatabase (display, get_resources ()); |
300 |
|
301 |
#ifdef POINTER_BLANK |
302 |
XColor blackcolour; |
303 |
blackcolour.red = 0; |
304 |
blackcolour.green = 0; |
305 |
blackcolour.blue = 0; |
306 |
Font f = XLoadFont (display, "fixed"); |
307 |
blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', |
308 |
&blackcolour, &blackcolour); |
309 |
XUnloadFont (display, f); |
310 |
#endif |
311 |
|
312 |
#ifdef PREFER_24BIT |
313 |
/* |
314 |
* If depth is not 24, look for a 24bit visual. |
315 |
*/ |
316 |
if (depth != 24) |
317 |
{ |
318 |
XVisualInfo vinfo; |
319 |
|
320 |
if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo)) |
321 |
{ |
322 |
depth = 24; |
323 |
visual = vinfo.visual; |
324 |
cmap = XCreateColormap (display, |
325 |
RootWindow (display, screen), |
326 |
visual, AllocNone); |
327 |
} |
328 |
} |
329 |
#endif |
330 |
|
331 |
int fd = XConnectionNumber (display); |
332 |
|
333 |
#ifndef NO_SLOW_LINK_SUPPORT |
334 |
// try to detect wether we have a local connection. |
335 |
// assume unix domains socket == local, everything else not |
336 |
// TODO: might want to check for inet/127.0.0.1 |
337 |
is_local = 0; |
338 |
sockaddr_un sa; |
339 |
socklen_t sl = sizeof (sa); |
340 |
|
341 |
if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
342 |
is_local = sa.sun_family == AF_LOCAL; |
343 |
#endif |
344 |
|
345 |
x_ev.start (fd, EVENT_READ); |
346 |
fcntl (fd, F_SETFD, FD_CLOEXEC); |
347 |
|
348 |
XSelectInput (display, root, PropertyChangeMask); |
349 |
|
350 |
flush (); |
351 |
|
352 |
return true; |
353 |
} |
354 |
|
355 |
void |
356 |
rxvt_display::ref_next () |
357 |
{ |
358 |
// TODO: somehow check wether the database files/resources changed |
359 |
// before re-loading/parsing |
360 |
XrmDestroyDatabase (XrmGetDatabase (display)); |
361 |
XrmSetDatabase (display, get_resources ()); |
362 |
} |
363 |
|
364 |
rxvt_display::~rxvt_display () |
365 |
{ |
366 |
if (!display) |
367 |
return; |
368 |
|
369 |
#ifdef POINTER_BLANK |
370 |
XFreeCursor (display, blank_cursor); |
371 |
#endif |
372 |
x_ev.stop (); |
373 |
#ifdef USE_XIM |
374 |
xims.clear (); |
375 |
#endif |
376 |
XCloseDisplay (display); |
377 |
} |
378 |
|
379 |
#ifdef USE_XIM |
380 |
void rxvt_display::im_change_cb () |
381 |
{ |
382 |
for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
383 |
(*i)->call (); |
384 |
} |
385 |
|
386 |
void rxvt_display::im_change_check () |
387 |
{ |
388 |
// try to only call im_change_cb when a new input method |
389 |
// registers, as xlib crashes due to a race otherwise. |
390 |
Atom actual_type, *atoms; |
391 |
int actual_format; |
392 |
unsigned long nitems, bytes_after; |
393 |
|
394 |
if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
395 |
False, XA_ATOM, &actual_type, &actual_format, |
396 |
&nitems, &bytes_after, (unsigned char **)&atoms) |
397 |
!= Success ) |
398 |
return; |
399 |
|
400 |
if (actual_type == XA_ATOM && actual_format == 32) |
401 |
for (int i = 0; i < nitems; i++) |
402 |
if (XGetSelectionOwner (display, atoms[i])) |
403 |
{ |
404 |
im_change_cb (); |
405 |
break; |
406 |
} |
407 |
|
408 |
XFree (atoms); |
409 |
} |
410 |
#endif |
411 |
|
412 |
void rxvt_display::x_cb (io_watcher &w, short revents) |
413 |
{ |
414 |
do |
415 |
{ |
416 |
XEvent xev; |
417 |
XNextEvent (display, &xev); |
418 |
|
419 |
#ifdef USE_XIM |
420 |
if (!XFilterEvent (&xev, None)) |
421 |
{ |
422 |
if (xev.type == PropertyNotify |
423 |
&& xev.xany.window == root |
424 |
&& xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
425 |
im_change_check (); |
426 |
#endif |
427 |
for (int i = xw.size (); i--; ) |
428 |
{ |
429 |
if (!xw[i]) |
430 |
xw.erase_unordered (i); |
431 |
else if (xw[i]->window == xev.xany.window) |
432 |
xw[i]->call (xev); |
433 |
} |
434 |
#ifdef USE_XIM |
435 |
} |
436 |
#endif |
437 |
} |
438 |
while (XEventsQueued (display, QueuedAlready)); |
439 |
|
440 |
XFlush (display); |
441 |
} |
442 |
|
443 |
void rxvt_display::flush () |
444 |
{ |
445 |
if (XEventsQueued (display, QueuedAlready)) |
446 |
x_cb (x_ev, EVENT_READ); |
447 |
|
448 |
XFlush (display); |
449 |
} |
450 |
|
451 |
void rxvt_display::reg (xevent_watcher *w) |
452 |
{ |
453 |
xw.push_back (w); |
454 |
w->active = xw.size (); |
455 |
} |
456 |
|
457 |
void rxvt_display::unreg (xevent_watcher *w) |
458 |
{ |
459 |
if (w->active) |
460 |
xw[w->active - 1] = 0; |
461 |
} |
462 |
|
463 |
void rxvt_display::set_selection_owner (rxvt_term *owner) |
464 |
{ |
465 |
if (selection_owner && selection_owner != owner) |
466 |
selection_owner->selection_clear (); |
467 |
|
468 |
selection_owner = owner; |
469 |
} |
470 |
|
471 |
#ifdef USE_XIM |
472 |
void rxvt_display::reg (im_watcher *w) |
473 |
{ |
474 |
imw.push_back (w); |
475 |
} |
476 |
|
477 |
void rxvt_display::unreg (im_watcher *w) |
478 |
{ |
479 |
imw.erase (find (imw.begin (), imw.end (), w)); |
480 |
} |
481 |
|
482 |
rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
483 |
{ |
484 |
char *id; |
485 |
int l, m; |
486 |
|
487 |
l = strlen (locale); |
488 |
m = strlen (modifiers); |
489 |
|
490 |
if (!(id = (char *)malloc (l + m + 2))) |
491 |
return 0; |
492 |
|
493 |
memcpy (id, locale, l); id[l] = '\n'; |
494 |
memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
495 |
|
496 |
rxvt_xim *xim = xims.get (id); |
497 |
|
498 |
free (id); |
499 |
|
500 |
return xim; |
501 |
} |
502 |
|
503 |
void rxvt_display::put_xim (rxvt_xim *xim) |
504 |
{ |
505 |
#if XLIB_IS_RACEFREE |
506 |
xims.put (xim); |
507 |
#endif |
508 |
} |
509 |
#endif |
510 |
|
511 |
Atom rxvt_display::atom (const char *name) |
512 |
{ |
513 |
return XInternAtom (display, name, False); |
514 |
} |
515 |
|
516 |
///////////////////////////////////////////////////////////////////////////// |
517 |
|
518 |
template class refcache<rxvt_display>; |
519 |
refcache<rxvt_display> displays; |
520 |
|
521 |
///////////////////////////////////////////////////////////////////////////// |
522 |
|
523 |
bool |
524 |
rxvt_color::set (rxvt_display *display, Pixel p) |
525 |
{ |
526 |
#if XFT |
527 |
XColor xc; |
528 |
|
529 |
xc.pixel = p; |
530 |
if (!XQueryColor (display->display, display->cmap, &xc)) |
531 |
return false; |
532 |
|
533 |
XRenderColor d; |
534 |
|
535 |
d.red = xc.red; |
536 |
d.green = xc.green; |
537 |
d.blue = xc.blue; |
538 |
d.alpha = 0xffff; |
539 |
|
540 |
return |
541 |
XftColorAllocValue (display->display, |
542 |
display->visual, |
543 |
display->cmap, |
544 |
&d, &c); |
545 |
#else |
546 |
this->p = p; |
547 |
#endif |
548 |
|
549 |
return true; |
550 |
} |
551 |
|
552 |
bool |
553 |
rxvt_color::set (rxvt_display *display, const char *name) |
554 |
{ |
555 |
#if XFT |
556 |
return XftColorAllocName (display->display, display->visual, display->cmap, |
557 |
name, &c); |
558 |
#else |
559 |
XColor xc; |
560 |
|
561 |
if (XParseColor (display->display, display->cmap, name, &xc)) |
562 |
return set (display, xc.red, xc.green, xc.blue); |
563 |
|
564 |
return false; |
565 |
#endif |
566 |
} |
567 |
|
568 |
bool |
569 |
rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) |
570 |
{ |
571 |
XColor xc; |
572 |
|
573 |
xc.red = cr; |
574 |
xc.green = cg; |
575 |
xc.blue = cb; |
576 |
xc.flags = DoRed | DoGreen | DoBlue; |
577 |
|
578 |
if (XAllocColor (display->display, display->cmap, &xc)) |
579 |
return set (display, xc.pixel); |
580 |
|
581 |
return false; |
582 |
} |
583 |
|
584 |
void |
585 |
rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) |
586 |
{ |
587 |
#if XFT |
588 |
cr = c.color.red; |
589 |
cg = c.color.green; |
590 |
cb = c.color.blue; |
591 |
#else |
592 |
XColor c; |
593 |
|
594 |
c.pixel = p; |
595 |
XQueryColor (display->display, display->cmap, &c); |
596 |
|
597 |
cr = c.red; |
598 |
cg = c.green; |
599 |
cb = c.blue; |
600 |
#endif |
601 |
} |
602 |
|
603 |
void |
604 |
rxvt_color::free (rxvt_display *display) |
605 |
{ |
606 |
#if XFT |
607 |
XftColorFree (display->display, display->visual, display->cmap, &c); |
608 |
#else |
609 |
XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
610 |
#endif |
611 |
} |
612 |
|
613 |
rxvt_color |
614 |
rxvt_color::fade (rxvt_display *display, int percent) |
615 |
{ |
616 |
percent = 100 - percent; |
617 |
|
618 |
unsigned short cr, cg, cb; |
619 |
rxvt_color faded; |
620 |
|
621 |
get (display, cr, cg, cb); |
622 |
|
623 |
faded.set ( |
624 |
display, |
625 |
cr * percent / 100, |
626 |
cg * percent / 100, |
627 |
cb * percent / 100 |
628 |
); |
629 |
|
630 |
return faded; |
631 |
} |
632 |
|
633 |
#define LERP(a,b,p) (a * p + b * (100 - p)) / 100 |
634 |
|
635 |
rxvt_color |
636 |
rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto) |
637 |
{ |
638 |
percent = 100 - percent; |
639 |
|
640 |
unsigned short cr, cg, cb; |
641 |
unsigned short fcr, fcg, fcb; |
642 |
rxvt_color faded; |
643 |
|
644 |
get (display, cr, cg, cb); |
645 |
fadeto.get(display, fcr, fcg, fcb); |
646 |
|
647 |
faded.set ( |
648 |
display, |
649 |
LERP (cr, fcr, percent), |
650 |
LERP (cg, fcg, percent), |
651 |
LERP (cb, fcb, percent) |
652 |
); |
653 |
|
654 |
return faded; |
655 |
} |
656 |
|