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