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