… | |
… | |
32 | #ifndef NO_SLOW_LINK_SUPPORT |
32 | #ifndef NO_SLOW_LINK_SUPPORT |
33 | # include <sys/socket.h> |
33 | # include <sys/socket.h> |
34 | # include <sys/un.h> |
34 | # include <sys/un.h> |
35 | #endif |
35 | #endif |
36 | |
36 | |
|
|
37 | #if XFT |
|
|
38 | # include <X11/extensions/Xrender.h> |
|
|
39 | #endif |
|
|
40 | |
37 | const char *const xa_names[] = |
41 | const char *const xa_names[] = |
38 | { |
42 | { |
39 | "TEXT", |
43 | "TEXT", |
40 | "COMPOUND_TEXT", |
44 | "COMPOUND_TEXT", |
41 | "UTF8_STRING", |
45 | "UTF8_STRING", |
42 | "MULTIPLE", |
46 | "MULTIPLE", |
43 | "TARGETS", |
47 | "TARGETS", |
44 | "TIMESTAMP", |
48 | "TIMESTAMP", |
45 | "VT_SELECTION", |
49 | "VT_SELECTION", |
46 | "INCR", |
50 | "INCR", |
47 | "WM_PROTOCOLS", |
51 | "WM_PROTOCOLS", |
48 | "WM_DELETE_WINDOW", |
52 | "WM_DELETE_WINDOW", |
49 | "CLIPBOARD", |
53 | "CLIPBOARD", |
|
|
54 | "AVERAGE_WIDTH", |
|
|
55 | "WEIGHT_NAME", |
|
|
56 | "SLANT", |
|
|
57 | "CHARSET_REGISTRY", |
|
|
58 | "CHARSET_ENCODING", |
50 | #if ENABLE_FRILLS |
59 | #if ENABLE_FRILLS |
51 | "_MOTIF_WM_HINTS", |
60 | "_MOTIF_WM_HINTS", |
52 | #endif |
61 | #endif |
53 | #if ENABLE_EWMH |
62 | #if ENABLE_EWMH |
54 | "_NET_WM_PID", |
63 | "_NET_WM_PID", |
55 | "_NET_WM_NAME", |
64 | "_NET_WM_NAME", |
56 | "_NET_WM_ICON_NAME", |
65 | "_NET_WM_ICON_NAME", |
57 | "_NET_WM_PING", |
66 | "_NET_WM_PING", |
58 | #endif |
67 | #endif |
59 | #if USE_XIM |
68 | #if USE_XIM |
60 | "WM_LOCALE_NAME", |
69 | "WM_LOCALE_NAME", |
61 | "XIM_SERVERS", |
70 | "XIM_SERVERS", |
62 | #endif |
71 | #endif |
63 | #ifdef TRANSPARENT |
72 | #ifdef TRANSPARENT |
64 | "_XROOTPMAP_ID", |
73 | "_XROOTPMAP_ID", |
65 | "ESETROOT_PMAP_ID", |
74 | "ESETROOT_PMAP_ID", |
66 | #endif |
75 | #endif |
67 | #if ENABLE_XEMBED |
76 | #if ENABLE_XEMBED |
68 | "_XEMBED", |
77 | "_XEMBED", |
69 | "_XEMBED_INFO", |
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 | "cursor", |
|
|
87 | # if USE_XIM |
|
|
88 | "TRANSPORT", |
|
|
89 | "LOCALES", |
|
|
90 | "_XIM_PROTOCOL", |
|
|
91 | "_XIM_XCONNECT", |
|
|
92 | "_XIM_MOREDATA", |
70 | #endif |
93 | # endif |
71 | }; |
94 | #endif |
|
|
95 | }; |
72 | |
96 | |
73 | ///////////////////////////////////////////////////////////////////////////// |
97 | ///////////////////////////////////////////////////////////////////////////// |
74 | |
98 | |
75 | refcounted::refcounted (const char *id) |
99 | refcounted::refcounted (const char *id) |
76 | { |
100 | { |
… | |
… | |
153 | bool |
177 | bool |
154 | rxvt_xim::ref_init () |
178 | rxvt_xim::ref_init () |
155 | { |
179 | { |
156 | display = GET_R->display; //HACK: TODO |
180 | display = GET_R->display; //HACK: TODO |
157 | |
181 | |
158 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
182 | xim = XOpenIM (display->display, 0, 0, 0); |
159 | |
183 | |
160 | if (!xim) |
184 | if (!xim) |
161 | return false; |
185 | return false; |
162 | |
186 | |
163 | XIMCallback ximcallback; |
187 | XIMCallback ximcallback; |
164 | ximcallback.client_data = (XPointer)this; |
188 | ximcallback.client_data = (XPointer)this; |
165 | ximcallback.callback = im_destroy_cb; |
189 | ximcallback.callback = im_destroy_cb; |
166 | |
190 | |
167 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); |
191 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0); |
168 | |
192 | |
169 | return true; |
193 | return true; |
170 | } |
194 | } |
171 | |
195 | |
172 | rxvt_xim::~rxvt_xim () |
196 | rxvt_xim::~rxvt_xim () |
… | |
… | |
191 | visual = DefaultVisualOfScreen (screen); |
215 | visual = DefaultVisualOfScreen (screen); |
192 | cmap = DefaultColormapOfScreen (screen); |
216 | cmap = DefaultColormapOfScreen (screen); |
193 | } |
217 | } |
194 | |
218 | |
195 | void |
219 | void |
196 | rxvt_screen::set (rxvt_display *disp, int depth) |
220 | rxvt_screen::set (rxvt_display *disp, int bitdepth) |
197 | { |
221 | { |
198 | set (disp); |
222 | set (disp); |
199 | |
223 | |
|
|
224 | #if XFT |
200 | XVisualInfo vinfo; |
225 | XVisualInfo vinfo; |
201 | |
226 | |
202 | if (XMatchVisualInfo (xdisp, display->screen, depth, TrueColor, &vinfo)) |
227 | if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo)) |
203 | { |
228 | { |
204 | this->depth = depth; |
229 | depth = bitdepth; |
205 | this->visual = vinfo.visual; |
230 | visual = vinfo.visual; |
206 | this->cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); |
231 | cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); |
207 | } |
232 | } |
|
|
233 | #endif |
208 | } |
234 | } |
209 | |
235 | |
210 | void |
236 | void |
211 | rxvt_screen::clear () |
237 | rxvt_screen::clear () |
212 | { |
238 | { |
… | |
… | |
222 | , selection_owner (0) |
248 | , selection_owner (0) |
223 | { |
249 | { |
224 | } |
250 | } |
225 | |
251 | |
226 | XrmDatabase |
252 | XrmDatabase |
227 | rxvt_display::get_resources () |
253 | rxvt_display::get_resources (bool refresh) |
228 | { |
254 | { |
229 | char *homedir = (char *)getenv ("HOME"); |
255 | char *homedir = (char *)getenv ("HOME"); |
230 | char fname[1024]; |
256 | char fname[1024]; |
231 | |
257 | |
232 | /* |
258 | /* |
233 | * get resources using the X library function |
259 | * get resources using the X library function |
234 | */ |
260 | */ |
235 | char *displayResource, *xe; |
261 | char *displayResource, *xe; |
236 | XrmDatabase database, rdb1; |
262 | XrmDatabase rdb1, database = 0; |
237 | |
|
|
238 | database = NULL; |
|
|
239 | |
263 | |
240 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
264 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
241 | |
265 | |
242 | // 6. System wide per application default file. |
266 | // 6. System wide per application default file. |
243 | |
267 | |
… | |
… | |
253 | // 5. User's per application default file. |
277 | // 5. User's per application default file. |
254 | // none |
278 | // none |
255 | |
279 | |
256 | // 4. User's defaults file. |
280 | // 4. User's defaults file. |
257 | /* Get any Xserver defaults */ |
281 | /* Get any Xserver defaults */ |
|
|
282 | if (refresh) |
|
|
283 | { |
|
|
284 | // fucking xlib keeps a copy of the rm string |
|
|
285 | Atom actual_type; |
|
|
286 | int actual_format; |
|
|
287 | unsigned long nitems, nremaining; |
|
|
288 | char *val = 0; |
|
|
289 | |
|
|
290 | #if XLIB_ILLEGAL_ACCESS |
|
|
291 | if (display->xdefaults) |
|
|
292 | XFree (display->xdefaults); |
|
|
293 | #endif |
|
|
294 | |
|
|
295 | if (XGetWindowProperty (display, root, XA_RESOURCE_MANAGER, |
|
|
296 | 0L, 100000000L, False, |
|
|
297 | XA_STRING, &actual_type, &actual_format, |
|
|
298 | &nitems, &nremaining, |
|
|
299 | (unsigned char **)&val) == Success |
|
|
300 | && actual_type == XA_STRING |
|
|
301 | && actual_format == 8) |
|
|
302 | displayResource = val; |
|
|
303 | else |
|
|
304 | { |
|
|
305 | displayResource = 0; |
|
|
306 | if (val) |
|
|
307 | XFree(val); |
|
|
308 | } |
|
|
309 | |
|
|
310 | #if XLIB_ILLEGAL_ACCESS |
|
|
311 | display->xdefaults = displayResource; |
|
|
312 | #endif |
|
|
313 | } |
|
|
314 | else |
258 | displayResource = XResourceManagerString (display); |
315 | displayResource = XResourceManagerString (display); |
259 | |
316 | |
260 | if (displayResource != NULL) |
317 | if (displayResource) |
261 | { |
318 | { |
262 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
319 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
263 | XrmMergeDatabases (rdb1, &database); |
320 | XrmMergeDatabases (rdb1, &database); |
264 | } |
321 | } |
265 | else if (homedir) |
322 | else if (homedir) |
… | |
… | |
268 | |
325 | |
269 | if ((rdb1 = XrmGetFileDatabase (fname))) |
326 | if ((rdb1 = XrmGetFileDatabase (fname))) |
270 | XrmMergeDatabases (rdb1, &database); |
327 | XrmMergeDatabases (rdb1, &database); |
271 | } |
328 | } |
272 | |
329 | |
|
|
330 | #if !XLIB_ILLEGAL_ACCESS |
|
|
331 | if (refresh && displayResource) |
|
|
332 | XFree (displayResource); |
|
|
333 | #endif |
|
|
334 | |
273 | /* Get screen specific resources */ |
335 | /* Get screen specific resources */ |
274 | displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); |
336 | displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); |
275 | |
337 | |
276 | if (displayResource != NULL) |
338 | if (displayResource) |
277 | { |
339 | { |
278 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
340 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
279 | /* Merge with screen-independent resources */ |
341 | /* Merge with screen-independent resources */ |
280 | XrmMergeDatabases (rdb1, &database); |
342 | XrmMergeDatabases (rdb1, &database); |
281 | |
343 | |
… | |
… | |
322 | display = XOpenDisplay (id); |
384 | display = XOpenDisplay (id); |
323 | |
385 | |
324 | if (!display) |
386 | if (!display) |
325 | return false; |
387 | return false; |
326 | |
388 | |
327 | screen = DefaultScreen (display); |
389 | screen = DefaultScreen (display); |
328 | root = DefaultRootWindow (display); |
390 | root = DefaultRootWindow (display); |
329 | |
391 | |
330 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
392 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
331 | XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); |
393 | XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); |
332 | |
394 | |
333 | XrmSetDatabase (display, get_resources ()); |
395 | XrmSetDatabase (display, get_resources (false)); |
334 | |
396 | |
335 | #ifdef POINTER_BLANK |
397 | #ifdef POINTER_BLANK |
336 | XColor blackcolour; |
398 | XColor blackcolour; |
337 | blackcolour.red = 0; |
399 | blackcolour.red = 0; |
338 | blackcolour.green = 0; |
400 | blackcolour.green = 0; |
… | |
… | |
369 | |
431 | |
370 | void |
432 | void |
371 | rxvt_display::ref_next () |
433 | rxvt_display::ref_next () |
372 | { |
434 | { |
373 | // TODO: somehow check wether the database files/resources changed |
435 | // TODO: somehow check wether the database files/resources changed |
374 | // before re-loading/parsing |
436 | // before affording re-loading/parsing |
375 | XrmDestroyDatabase (XrmGetDatabase (display)); |
437 | XrmDestroyDatabase (XrmGetDatabase (display)); |
376 | XrmSetDatabase (display, get_resources ()); |
438 | XrmSetDatabase (display, get_resources (true)); |
377 | } |
439 | } |
378 | |
440 | |
379 | rxvt_display::~rxvt_display () |
441 | rxvt_display::~rxvt_display () |
380 | { |
442 | { |
381 | if (!display) |
443 | if (!display) |
… | |
… | |
482 | |
544 | |
483 | selection_owner = owner; |
545 | selection_owner = owner; |
484 | } |
546 | } |
485 | |
547 | |
486 | #ifdef USE_XIM |
548 | #ifdef USE_XIM |
|
|
549 | |
487 | void rxvt_display::reg (im_watcher *w) |
550 | void rxvt_display::reg (im_watcher *w) |
488 | { |
551 | { |
489 | imw.push_back (w); |
552 | imw.push_back (w); |
490 | } |
553 | } |
491 | |
554 | |
… | |
… | |
515 | return xim; |
578 | return xim; |
516 | } |
579 | } |
517 | |
580 | |
518 | void rxvt_display::put_xim (rxvt_xim *xim) |
581 | void rxvt_display::put_xim (rxvt_xim *xim) |
519 | { |
582 | { |
520 | #if XLIB_IS_RACEFREE |
583 | # if XLIB_IS_RACEFREE |
521 | xims.put (xim); |
584 | xims.put (xim); |
522 | #endif |
585 | # endif |
523 | } |
586 | } |
|
|
587 | |
524 | #endif |
588 | #endif |
525 | |
589 | |
526 | Atom rxvt_display::atom (const char *name) |
590 | Atom rxvt_display::atom (const char *name) |
527 | { |
591 | { |
528 | return XInternAtom (display, name, False); |
592 | return XInternAtom (display, name, False); |
… | |
… | |
532 | |
596 | |
533 | template class refcache<rxvt_display>; |
597 | template class refcache<rxvt_display>; |
534 | refcache<rxvt_display> displays; |
598 | refcache<rxvt_display> displays; |
535 | |
599 | |
536 | ///////////////////////////////////////////////////////////////////////////// |
600 | ///////////////////////////////////////////////////////////////////////////// |
537 | |
601 | |
538 | bool |
602 | bool |
539 | rxvt_color::set (rxvt_screen *screen, Pixel p) |
603 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
540 | { |
604 | { |
541 | #if XFT |
605 | #if XFT |
542 | XColor xc; |
606 | XRenderPictFormat *format; |
543 | |
607 | |
544 | xc.pixel = p; |
608 | // FUCKING Xft gets it wrong, of course, so work around it. |
545 | if (!XQueryColor (screen->xdisp, screen->cmap, &xc)) |
609 | // Transparency users should eat shit and die, and then |
546 | return false; |
610 | // XRenderQueryPictIndexValues themselves plenty. |
|
|
611 | if ((screen->visual->c_class == TrueColor) |
|
|
612 | && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual))) |
|
|
613 | { |
|
|
614 | // the fun lies in doing everything manually... |
|
|
615 | c.color.red = color.r; |
|
|
616 | c.color.green = color.g; |
|
|
617 | c.color.blue = color.b; |
|
|
618 | c.color.alpha = color.a; |
547 | |
619 | |
|
|
620 | c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red ) |
|
|
621 | | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green) |
|
|
622 | | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue ) |
|
|
623 | | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha); |
|
|
624 | |
|
|
625 | return true; |
|
|
626 | } |
|
|
627 | else |
|
|
628 | { |
548 | XRenderColor d; |
629 | XRenderColor d; |
549 | |
630 | |
550 | d.red = xc.red; |
631 | d.red = color.r; |
551 | d.green = xc.green; |
632 | d.green = color.g; |
552 | d.blue = xc.blue; |
633 | d.blue = color.b; |
553 | d.alpha = 0xffff; |
634 | d.alpha = color.a; |
554 | |
635 | |
555 | return |
636 | return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); |
556 | XftColorAllocValue (screen->xdisp, |
637 | } |
557 | screen->visual, |
|
|
558 | screen->cmap, |
|
|
559 | &d, &c); |
|
|
560 | #else |
638 | #else |
561 | this->p = p; |
639 | c.red = color.r; |
562 | #endif |
640 | c.green = color.g; |
|
|
641 | c.blue = color.b; |
563 | |
642 | |
|
|
643 | if (screen->visual->c_class == TrueColor) |
|
|
644 | { |
|
|
645 | c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
|
|
646 | / rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
|
|
647 | | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
|
|
648 | / rgba::MAX_CC) << ctz (screen->visual->green_mask) |
|
|
649 | | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
|
|
650 | / rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
|
|
651 | |
|
|
652 | return true; |
|
|
653 | } |
|
|
654 | else if (XAllocColor (screen->xdisp, screen->cmap, &c)) |
564 | return true; |
655 | return true; |
|
|
656 | else |
|
|
657 | c.pixel = (color.r + color.g + color.b) > 128*3 |
|
|
658 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) |
|
|
659 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); |
|
|
660 | #endif |
|
|
661 | |
|
|
662 | return false; |
565 | } |
663 | } |
566 | |
664 | |
567 | bool |
665 | bool |
568 | rxvt_color::set (rxvt_screen *screen, const char *name) |
666 | rxvt_color::set (rxvt_screen *screen, const char *name) |
569 | { |
667 | { |
|
|
668 | rgba c; |
|
|
669 | char eos; |
|
|
670 | int skip; |
|
|
671 | |
|
|
672 | // parse the nonstandard "[alphapercent]" prefix |
|
|
673 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
|
|
674 | { |
|
|
675 | c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); |
|
|
676 | name += skip; |
|
|
677 | } |
|
|
678 | else |
|
|
679 | c.a = rgba::MAX_CC; |
|
|
680 | |
|
|
681 | // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format |
|
|
682 | if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
|
|
683 | { |
|
|
684 | XColor xc, xc_exact; |
|
|
685 | |
|
|
686 | if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) |
|
|
687 | { |
|
|
688 | c.r = xc.red; |
|
|
689 | c.g = xc.green; |
|
|
690 | c.b = xc.blue; |
|
|
691 | } |
|
|
692 | else |
|
|
693 | { |
|
|
694 | c.r = 0xffff; |
|
|
695 | c.g = 0x6969; |
|
|
696 | c.b = 0xb4b4; |
|
|
697 | |
|
|
698 | rxvt_warn ("unable to parse color '%s', using pink instead.\n", name); |
|
|
699 | } |
|
|
700 | } |
|
|
701 | |
|
|
702 | return set (screen, c); |
|
|
703 | } |
|
|
704 | |
|
|
705 | bool |
|
|
706 | rxvt_color::set (rxvt_screen *screen, const rgba &color) |
|
|
707 | { |
|
|
708 | bool got = alloc (screen, color); |
|
|
709 | |
|
|
710 | #if !ENABLE_MINIMAL |
|
|
711 | int cmap_size = screen->visual->map_entries; |
|
|
712 | |
|
|
713 | if (!got |
|
|
714 | && screen->visual->c_class == PseudoColor |
|
|
715 | && cmap_size < 4096) |
|
|
716 | { |
|
|
717 | XColor *colors = new XColor [screen->visual->map_entries]; |
|
|
718 | |
|
|
719 | for (int i = 0; i < cmap_size; i++) |
|
|
720 | colors [i].pixel = i; |
|
|
721 | |
|
|
722 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
|
|
723 | // many extra optimisations. |
|
|
724 | XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); |
|
|
725 | |
|
|
726 | int diff = 0x7fffffffUL; |
|
|
727 | XColor *best = colors; |
|
|
728 | |
|
|
729 | for (int i = 0; i < cmap_size; i++) |
|
|
730 | { |
|
|
731 | int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2)) |
|
|
732 | + (squared_diff<int> (color.g >> 2, colors [i].green >> 2)) |
|
|
733 | + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2)); |
|
|
734 | |
|
|
735 | if (d < diff) |
|
|
736 | { |
|
|
737 | diff = d; |
|
|
738 | best = colors + i; |
|
|
739 | } |
|
|
740 | } |
|
|
741 | |
|
|
742 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
|
|
743 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff); |
|
|
744 | |
|
|
745 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
|
|
746 | |
|
|
747 | delete colors; |
|
|
748 | } |
|
|
749 | #endif |
|
|
750 | |
|
|
751 | return got; |
|
|
752 | } |
|
|
753 | |
|
|
754 | void |
|
|
755 | rxvt_color::get (rgba &color) |
|
|
756 | { |
570 | #if XFT |
757 | #if XFT |
571 | return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c); |
758 | color.r = c.color.red; |
|
|
759 | color.g = c.color.green; |
|
|
760 | color.b = c.color.blue; |
|
|
761 | color.a = c.color.alpha; |
572 | #else |
762 | #else |
573 | XColor xc; |
763 | color.r = c.red; |
574 | |
764 | color.g = c.green; |
575 | if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) |
765 | color.b = c.blue; |
576 | return set (screen, xc.red, xc.green, xc.blue); |
766 | color.a = rgba::MAX_CC; |
577 | |
|
|
578 | return false; |
|
|
579 | #endif |
767 | #endif |
580 | } |
768 | } |
581 | |
769 | |
582 | bool |
|
|
583 | rxvt_color::set (rxvt_screen *screen, unsigned short cr, unsigned short cg, unsigned short cb) |
|
|
584 | { |
|
|
585 | XColor xc; |
|
|
586 | |
|
|
587 | xc.red = cr; |
|
|
588 | xc.green = cg; |
|
|
589 | xc.blue = cb; |
|
|
590 | xc.flags = DoRed | DoGreen | DoBlue; |
|
|
591 | |
|
|
592 | if (XAllocColor (screen->xdisp, screen->cmap, &xc)) |
|
|
593 | return set (screen, xc.pixel); |
|
|
594 | |
|
|
595 | return false; |
|
|
596 | } |
|
|
597 | |
|
|
598 | void |
770 | void |
599 | rxvt_color::get (rxvt_screen *screen, unsigned short &cr, unsigned short &cg, unsigned short &cb) |
771 | rxvt_color::get (XColor &color) |
600 | { |
772 | { |
601 | #if XFT |
773 | rgba c; |
602 | cr = c.color.red; |
774 | get (c); |
603 | cg = c.color.green; |
|
|
604 | cb = c.color.blue; |
|
|
605 | #else |
|
|
606 | XColor c; |
|
|
607 | |
775 | |
608 | c.pixel = p; |
776 | color.red = c.r; |
609 | XQueryColor (screen->xdisp, screen->cmap, &c); |
777 | color.green = c.g; |
610 | |
778 | color.blue = c.b; |
611 | cr = c.red; |
779 | color.pixel = (Pixel)*this; |
612 | cg = c.green; |
|
|
613 | cb = c.blue; |
|
|
614 | #endif |
|
|
615 | } |
780 | } |
616 | |
781 | |
617 | void |
782 | void |
618 | rxvt_color::free (rxvt_screen *screen) |
783 | rxvt_color::free (rxvt_screen *screen) |
619 | { |
784 | { |
620 | #if XFT |
785 | #if XFT |
621 | XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); |
786 | XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); |
622 | #else |
787 | #else |
623 | XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); |
788 | XFreeColors (screen->xdisp, screen->cmap, &c.pixel, 1, AllPlanes); |
624 | #endif |
789 | #endif |
625 | } |
790 | } |
626 | |
791 | |
627 | rxvt_color |
792 | void |
628 | rxvt_color::fade (rxvt_screen *screen, int percent) |
793 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to) |
629 | { |
794 | { |
630 | percent = 100 - percent; |
795 | rgba c; |
|
|
796 | get (c); |
631 | |
797 | |
632 | unsigned short cr, cg, cb; |
798 | result.set ( |
633 | rxvt_color faded; |
|
|
634 | |
|
|
635 | get (screen, cr, cg, cb); |
|
|
636 | |
|
|
637 | faded.set ( |
|
|
638 | screen, |
799 | screen, |
639 | cr * percent / 100, |
800 | rgba ( |
640 | cg * percent / 100, |
801 | lerp (c.r, to.r, percent), |
641 | cb * percent / 100 |
802 | lerp (c.g, to.g, percent), |
|
|
803 | lerp (c.b, to.b, percent), |
|
|
804 | lerp (c.a, to.a, percent) |
|
|
805 | ) |
642 | ); |
806 | ); |
643 | |
|
|
644 | return faded; |
|
|
645 | } |
807 | } |
646 | |
808 | |
647 | #define LERP(a,b,p) (a * p + b * (100 - p)) / 100 |
|
|
648 | |
|
|
649 | rxvt_color |
|
|
650 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto) |
|
|
651 | { |
|
|
652 | percent = 100 - percent; |
|
|
653 | |
|
|
654 | unsigned short cr, cg, cb; |
|
|
655 | unsigned short fcr, fcg, fcb; |
|
|
656 | rxvt_color faded; |
|
|
657 | |
|
|
658 | get (screen, cr, cg, cb); |
|
|
659 | fadeto.get (screen, fcr, fcg, fcb); |
|
|
660 | |
|
|
661 | faded.set ( |
|
|
662 | screen, |
|
|
663 | LERP (cr, fcr, percent), |
|
|
664 | LERP (cg, fcg, percent), |
|
|
665 | LERP (cb, fcb, percent) |
|
|
666 | ); |
|
|
667 | |
|
|
668 | return faded; |
|
|
669 | } |
|
|
670 | |
|
|