… | |
… | |
395 | |
395 | |
396 | void |
396 | void |
397 | rxvt_display::ref_next () |
397 | rxvt_display::ref_next () |
398 | { |
398 | { |
399 | // TODO: somehow check wether the database files/resources changed |
399 | // TODO: somehow check wether the database files/resources changed |
400 | // before re-loading/parsing |
400 | // before affording re-loading/parsing |
401 | XrmDestroyDatabase (XrmGetDatabase (display)); |
401 | XrmDestroyDatabase (XrmGetDatabase (display)); |
402 | XrmSetDatabase (display, get_resources ()); |
402 | XrmSetDatabase (display, get_resources ()); |
403 | } |
403 | } |
404 | |
404 | |
405 | rxvt_display::~rxvt_display () |
405 | rxvt_display::~rxvt_display () |
… | |
… | |
508 | |
508 | |
509 | selection_owner = owner; |
509 | selection_owner = owner; |
510 | } |
510 | } |
511 | |
511 | |
512 | #ifdef USE_XIM |
512 | #ifdef USE_XIM |
|
|
513 | |
513 | void rxvt_display::reg (im_watcher *w) |
514 | void rxvt_display::reg (im_watcher *w) |
514 | { |
515 | { |
515 | imw.push_back (w); |
516 | imw.push_back (w); |
516 | } |
517 | } |
517 | |
518 | |
… | |
… | |
541 | return xim; |
542 | return xim; |
542 | } |
543 | } |
543 | |
544 | |
544 | void rxvt_display::put_xim (rxvt_xim *xim) |
545 | void rxvt_display::put_xim (rxvt_xim *xim) |
545 | { |
546 | { |
546 | #if XLIB_IS_RACEFREE |
547 | # if XLIB_IS_RACEFREE |
547 | xims.put (xim); |
548 | xims.put (xim); |
548 | #endif |
549 | # endif |
549 | } |
550 | } |
|
|
551 | |
550 | #endif |
552 | #endif |
551 | |
553 | |
552 | Atom rxvt_display::atom (const char *name) |
554 | Atom rxvt_display::atom (const char *name) |
553 | { |
555 | { |
554 | return XInternAtom (display, name, False); |
556 | return XInternAtom (display, name, False); |
… | |
… | |
560 | refcache<rxvt_display> displays; |
562 | refcache<rxvt_display> displays; |
561 | |
563 | |
562 | ///////////////////////////////////////////////////////////////////////////// |
564 | ///////////////////////////////////////////////////////////////////////////// |
563 | |
565 | |
564 | bool |
566 | bool |
565 | rxvt_color::alloc (rxvt_screen *screen, rxvt_rgba rgba) |
567 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
566 | { |
568 | { |
567 | #if XFT |
569 | #if XFT |
568 | XRenderPictFormat *format; |
570 | XRenderPictFormat *format; |
569 | |
571 | |
570 | // FUCKING Xft gets it wrong, of course, so work around it |
572 | // FUCKING Xft gets it wrong, of course, so work around it. |
571 | // transparency users should eat shit and die, and then |
573 | // Transparency users should eat shit and die, and then |
572 | // XRenderQueryPictIndexValues themselves plenty. |
574 | // XRenderQueryPictIndexValues themselves plenty. |
573 | if ((screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) |
575 | if ((screen->visual->c_class == TrueColor) |
574 | && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual))) |
576 | && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual))) |
575 | { |
577 | { |
576 | // the fun lies in doing everything manually... |
578 | // the fun lies in doing everything manually... |
577 | c.color.red = rgba.r; |
579 | c.color.red = color.r; |
578 | c.color.green = rgba.g; |
580 | c.color.green = color.g; |
579 | c.color.blue = rgba.b; |
581 | c.color.blue = color.b; |
580 | c.color.alpha = rgba.a; |
582 | c.color.alpha = color.a; |
581 | |
583 | |
582 | c.pixel = ((rgba.r * format->direct.redMask / rxvt_rgba::MAX_CC) << format->direct.red ) |
584 | c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red ) |
583 | | ((rgba.g * format->direct.greenMask / rxvt_rgba::MAX_CC) << format->direct.green) |
585 | | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green) |
584 | | ((rgba.b * format->direct.blueMask / rxvt_rgba::MAX_CC) << format->direct.blue ) |
586 | | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue ) |
585 | | ((rgba.a * format->direct.alphaMask / rxvt_rgba::MAX_CC) << format->direct.alpha); |
587 | | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha); |
586 | |
588 | |
587 | return true; |
589 | return true; |
588 | } |
590 | } |
589 | else |
591 | else |
590 | { |
592 | { |
591 | XRenderColor d; |
593 | XRenderColor d; |
592 | |
594 | |
593 | d.red = rgba.r; |
595 | d.red = color.r; |
594 | d.green = rgba.g; |
596 | d.green = color.g; |
595 | d.blue = rgba.b; |
597 | d.blue = color.b; |
596 | d.alpha = rgba.a; |
598 | d.alpha = color.a; |
597 | |
599 | |
598 | return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); |
600 | return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); |
599 | } |
601 | } |
600 | #else |
602 | #else |
601 | if (screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) |
603 | c.red = color.r; |
|
|
604 | c.green = color.g; |
|
|
605 | c.blue = color.b; |
|
|
606 | |
|
|
607 | if (screen->visual->c_class == TrueColor) |
602 | { |
608 | { |
603 | p = (rgba.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
609 | c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
604 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
610 | / rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
605 | | (rgba.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
611 | | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
606 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask) |
612 | / rgba::MAX_CC) << ctz (screen->visual->green_mask) |
607 | | (rgba.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
613 | | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
608 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
614 | / rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
609 | |
615 | |
610 | return true; |
616 | return true; |
611 | } |
617 | } |
|
|
618 | else if (XAllocColor (screen->xdisp, screen->cmap, &c)) |
|
|
619 | return true; |
612 | else |
620 | else |
613 | { |
621 | c.pixel = (color.r + color.g + color.b) > 128*3 |
614 | XColor xc; |
|
|
615 | |
|
|
616 | xc.red = rgba.r; |
|
|
617 | xc.green = rgba.g; |
|
|
618 | xc.blue = rgba.b; |
|
|
619 | |
|
|
620 | if (XAllocColor (screen->xdisp, screen->cmap, &xc)) |
|
|
621 | { |
|
|
622 | p = xc.pixel; |
|
|
623 | return true; |
|
|
624 | } |
|
|
625 | else |
|
|
626 | p = (rgba.r + rgba.g + rgba.b) > 128*3 |
|
|
627 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) |
622 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) |
628 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); |
623 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); |
629 | } |
|
|
630 | #endif |
624 | #endif |
631 | |
625 | |
632 | return false; |
626 | return false; |
633 | } |
627 | } |
634 | |
628 | |
635 | bool |
629 | bool |
636 | rxvt_color::set (rxvt_screen *screen, const char *name) |
630 | rxvt_color::set (rxvt_screen *screen, const char *name) |
637 | { |
631 | { |
638 | int l = strlen (name); |
632 | rgba c; |
639 | rxvt_rgba r; |
|
|
640 | char eos; |
633 | char eos; |
641 | int mult; |
634 | int skip; |
642 | XColor xc, xc_exact; |
|
|
643 | |
635 | |
644 | // parse a number of non-standard ARGB colour specifications |
636 | // parse the nonstandard "[alphapercent]" prefix |
645 | if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
637 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
646 | mult = rxvt_rgba::MAX_CC / 0x0010; |
|
|
647 | else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
648 | mult = rxvt_rgba::MAX_CC / 0x0100; |
|
|
649 | else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
650 | mult = rxvt_rgba::MAX_CC / 0x0100; |
|
|
651 | else if (l == 4+5*4 && 4 == sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos)) |
|
|
652 | mult = rxvt_rgba::MAX_CC / 0xffff; |
|
|
653 | else if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) |
|
|
654 | { |
638 | { |
655 | r.r = xc.red; |
639 | c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); |
656 | r.g = xc.green; |
640 | name += skip; |
657 | r.b = xc.blue; |
|
|
658 | mult = rxvt_rgba::MAX_CC / 0xffff; |
|
|
659 | } |
641 | } |
660 | else |
642 | else |
|
|
643 | c.a = rgba::MAX_CC; |
|
|
644 | |
|
|
645 | // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format |
|
|
646 | if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
661 | { |
647 | { |
|
|
648 | XColor xc, xc_exact; |
|
|
649 | |
|
|
650 | if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) |
|
|
651 | { |
|
|
652 | c.r = xc.red; |
|
|
653 | c.g = xc.green; |
|
|
654 | c.b = xc.blue; |
|
|
655 | } |
|
|
656 | else |
|
|
657 | { |
|
|
658 | c.r = 0xffff; |
|
|
659 | c.g = 0x6969; |
|
|
660 | c.b = 0xb4b4; |
|
|
661 | |
662 | rxvt_warn ("failed to allocate color '%s', using pink instead.\n", name); |
662 | rxvt_warn ("unable to parse color '%s', using pink instead.\n", name); |
663 | r.r = 255; |
663 | } |
664 | r.g = 105; |
|
|
665 | r.b = 180; |
|
|
666 | mult = rxvt_rgba::MAX_CC / 0x00ff; |
|
|
667 | } |
664 | } |
668 | |
665 | |
669 | r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult; |
|
|
670 | |
|
|
671 | return set (screen, r); |
666 | return set (screen, c); |
672 | } |
667 | } |
673 | |
668 | |
674 | bool |
669 | bool |
675 | rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba) |
670 | rxvt_color::set (rxvt_screen *screen, const rgba &color) |
676 | { |
671 | { |
677 | bool got = alloc (screen, rgba); |
672 | bool got = alloc (screen, color); |
678 | |
673 | |
679 | #if !ENABLE_MINIMAL |
674 | #if !ENABLE_MINIMAL |
680 | int cmap_size = screen->visual->map_entries; |
675 | int cmap_size = screen->visual->map_entries; |
681 | |
676 | |
682 | if (!got |
677 | if (!got |
… | |
… | |
686 | XColor *colors = new XColor [screen->visual->map_entries]; |
681 | XColor *colors = new XColor [screen->visual->map_entries]; |
687 | |
682 | |
688 | for (int i = 0; i < cmap_size; i++) |
683 | for (int i = 0; i < cmap_size; i++) |
689 | colors [i].pixel = i; |
684 | colors [i].pixel = i; |
690 | |
685 | |
|
|
686 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
|
|
687 | // many extra optimisations. |
691 | XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); |
688 | XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); |
692 | |
689 | |
693 | int diff = 0x7fffffffUL; |
690 | int diff = 0x7fffffffUL; |
694 | XColor *best = colors; |
691 | XColor *best = colors; |
695 | |
692 | |
696 | for (int i = 0; i < cmap_size; i++) |
693 | for (int i = 0; i < cmap_size; i++) |
697 | { |
694 | { |
698 | int d = (squared_diff<int> (rgba.r >> 2, colors [i].red >> 2)) |
695 | int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2)) |
699 | + (squared_diff<int> (rgba.g >> 2, colors [i].green >> 2)) |
696 | + (squared_diff<int> (color.g >> 2, colors [i].green >> 2)) |
700 | + (squared_diff<int> (rgba.b >> 2, colors [i].blue >> 2)); |
697 | + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2)); |
701 | |
698 | |
702 | if (d < diff) |
699 | if (d < diff) |
703 | { |
700 | { |
704 | diff = d; |
701 | diff = d; |
705 | best = colors + i; |
702 | best = colors + i; |
706 | } |
703 | } |
707 | } |
704 | } |
708 | |
705 | |
709 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
706 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
710 | // rgba.r, rgba.g, rgba.b, best->red, best->green, best->blue, diff); |
707 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff); |
711 | |
708 | |
712 | got = alloc (screen, rxvt_rgba (best->red, best->green, best->blue)); |
709 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
713 | |
710 | |
714 | delete colors; |
711 | delete colors; |
715 | } |
712 | } |
716 | #endif |
713 | #endif |
717 | |
714 | |
718 | return got; |
715 | return got; |
719 | } |
716 | } |
720 | |
717 | |
721 | void |
718 | void |
722 | rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba) |
719 | rxvt_color::get (rgba &color) |
723 | { |
720 | { |
724 | #if XFT |
721 | #if XFT |
725 | rgba.r = c.color.red; |
722 | color.r = c.color.red; |
726 | rgba.g = c.color.green; |
723 | color.g = c.color.green; |
727 | rgba.b = c.color.blue; |
724 | color.b = c.color.blue; |
728 | rgba.a = c.color.alpha; |
725 | color.a = c.color.alpha; |
729 | #else |
726 | #else |
730 | XColor c; |
|
|
731 | |
|
|
732 | c.pixel = p; |
|
|
733 | XQueryColor (screen->xdisp, screen->cmap, &c); |
|
|
734 | |
|
|
735 | rgba.r = c.red; |
727 | color.r = c.red; |
736 | rgba.g = c.green; |
728 | color.g = c.green; |
737 | rgba.b = c.blue; |
729 | color.b = c.blue; |
738 | rgba.a = rxvt_rgba::MAX_CC; |
730 | color.a = rgba::MAX_CC; |
739 | #endif |
731 | #endif |
|
|
732 | } |
|
|
733 | |
|
|
734 | void |
|
|
735 | rxvt_color::get (XColor &color) |
|
|
736 | { |
|
|
737 | rgba c; |
|
|
738 | get (c); |
|
|
739 | |
|
|
740 | color.red = c.r; |
|
|
741 | color.green = c.g; |
|
|
742 | color.blue = c.b; |
|
|
743 | color.pixel = (Pixel)*this; |
740 | } |
744 | } |
741 | |
745 | |
742 | void |
746 | void |
743 | rxvt_color::free (rxvt_screen *screen) |
747 | rxvt_color::free (rxvt_screen *screen) |
744 | { |
748 | { |
745 | #if XFT |
749 | #if XFT |
746 | XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); |
750 | XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); |
747 | #else |
751 | #else |
748 | XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); |
752 | XFreeColors (screen->xdisp, screen->cmap, &c.pixel, 1, AllPlanes); |
749 | #endif |
753 | #endif |
750 | } |
754 | } |
751 | |
755 | |
752 | rxvt_color |
756 | void |
753 | rxvt_color::fade (rxvt_screen *screen, int percent) |
|
|
754 | { |
|
|
755 | rxvt_color faded; |
|
|
756 | |
|
|
757 | rxvt_rgba c; |
|
|
758 | get (screen, c); |
|
|
759 | |
|
|
760 | c.r = lerp (0, c.r, percent); |
|
|
761 | c.g = lerp (0, c.g, percent); |
|
|
762 | c.b = lerp (0, c.b, percent); |
|
|
763 | |
|
|
764 | faded.set (screen, c); |
|
|
765 | |
|
|
766 | return faded; |
|
|
767 | } |
|
|
768 | |
|
|
769 | rxvt_color |
|
|
770 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto) |
757 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to) |
771 | { |
758 | { |
772 | rxvt_rgba c, fc; |
759 | rgba c; |
773 | rxvt_color faded; |
760 | get (c); |
774 | |
|
|
775 | get (screen, c); |
|
|
776 | fadeto.get (screen, fc); |
|
|
777 | |
761 | |
778 | faded.set ( |
762 | result.set ( |
779 | screen, |
763 | screen, |
780 | rxvt_rgba ( |
764 | rgba ( |
781 | lerp (fc.r, c.r, percent), |
765 | lerp (c.r, to.r, percent), |
782 | lerp (fc.g, c.g, percent), |
766 | lerp (c.g, to.g, percent), |
783 | lerp (fc.b, c.b, percent), |
767 | lerp (c.b, to.b, percent), |
784 | lerp (fc.a, c.a, percent) |
768 | lerp (c.a, to.a, percent) |
785 | ) |
769 | ) |
786 | ); |
770 | ); |
787 | |
|
|
788 | return faded; |
|
|
789 | } |
771 | } |
790 | |
772 | |