1 | /*----------------------------------------------------------------------* |
1 | /*----------------------------------------------------------------------* |
2 | * File: rxvttoolkit.C |
2 | * File: rxvttoolkit.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 2003-2007 Marc Lehmann <pcg@goof.com> |
6 | * Copyright (c) 2003-2011 Marc Lehmann <schmorp@schmorp.de> |
|
|
7 | * Copyright (c) 2011 Emanuele Giaquinta <e.giaquinta@glauco.it> |
7 | * |
8 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * 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 | * 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 | * the Free Software Foundation; either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * (at your option) any later version. |
12 | * |
13 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
… | |
… | |
21 | *----------------------------------------------------------------------*/ |
22 | *----------------------------------------------------------------------*/ |
22 | |
23 | |
23 | #include "../config.h" |
24 | #include "../config.h" |
24 | #include <rxvt.h> |
25 | #include <rxvt.h> |
25 | #include <rxvttoolkit.h> |
26 | #include <rxvttoolkit.h> |
|
|
27 | |
|
|
28 | #include <stdlib.h> |
26 | |
29 | |
27 | #include <unistd.h> |
30 | #include <unistd.h> |
28 | #include <fcntl.h> |
31 | #include <fcntl.h> |
29 | |
32 | |
30 | #include <sys/utsname.h> |
33 | #include <sys/utsname.h> |
… | |
… | |
65 | #endif |
68 | #endif |
66 | #if USE_XIM |
69 | #if USE_XIM |
67 | "WM_LOCALE_NAME", |
70 | "WM_LOCALE_NAME", |
68 | "XIM_SERVERS", |
71 | "XIM_SERVERS", |
69 | #endif |
72 | #endif |
70 | #ifdef ENABLE_TRANSPARENCY |
73 | #if HAVE_IMG || ENABLE_PERL |
71 | "_XROOTPMAP_ID", |
74 | "_XROOTPMAP_ID", |
72 | "ESETROOT_PMAP_ID", |
75 | "ESETROOT_PMAP_ID", |
73 | #endif |
76 | #endif |
74 | #if ENABLE_XEMBED |
77 | #if ENABLE_XEMBED |
75 | "_XEMBED", |
78 | "_XEMBED", |
… | |
… | |
152 | put (*this->begin ()); |
155 | put (*this->begin ()); |
153 | } |
156 | } |
154 | |
157 | |
155 | ///////////////////////////////////////////////////////////////////////////// |
158 | ///////////////////////////////////////////////////////////////////////////// |
156 | |
159 | |
157 | #ifdef USE_XIM |
160 | #if USE_XIM |
158 | |
161 | |
159 | static void |
162 | static void |
160 | #if XIMCB_PROTO_BROKEN |
163 | #if XIMCB_PROTO_BROKEN |
161 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
164 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
162 | #else |
165 | #else |
… | |
… | |
259 | depth = DefaultDepthOfScreen (screen); |
262 | depth = DefaultDepthOfScreen (screen); |
260 | visual = DefaultVisualOfScreen (screen); |
263 | visual = DefaultVisualOfScreen (screen); |
261 | cmap = DefaultColormapOfScreen (screen); |
264 | cmap = DefaultColormapOfScreen (screen); |
262 | } |
265 | } |
263 | |
266 | |
|
|
267 | #if ENABLE_FRILLS |
|
|
268 | |
264 | void |
269 | void |
265 | rxvt_screen::select_visual (int bitdepth) |
270 | rxvt_screen::select_visual (int id) |
266 | { |
271 | { |
267 | #if XFT |
|
|
268 | XVisualInfo vinfo; |
272 | XVisualInfo vinfo; |
|
|
273 | vinfo.visualid = id; |
|
|
274 | int n; |
|
|
275 | |
|
|
276 | if (XVisualInfo *vi = XGetVisualInfo (dpy, VisualIDMask, &vinfo, &n)) |
|
|
277 | { |
|
|
278 | depth = vi->depth; |
|
|
279 | visual = vi->visual; |
|
|
280 | |
|
|
281 | XFree (vi); |
|
|
282 | |
|
|
283 | cmap = XCreateColormap (dpy, display->root, visual, AllocNone); |
|
|
284 | } |
|
|
285 | else |
|
|
286 | rxvt_warn ("no visual found for requested id 0x%02x, using default visual.\n", id); |
|
|
287 | } |
|
|
288 | |
|
|
289 | void |
|
|
290 | rxvt_screen::select_depth (int bitdepth) |
|
|
291 | { |
|
|
292 | XVisualInfo vinfo; |
269 | |
293 | |
270 | if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) |
294 | if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) |
271 | { |
|
|
272 | depth = bitdepth; |
|
|
273 | visual = vinfo.visual; |
295 | select_visual (vinfo.visualid); |
274 | cmap = XCreateColormap (dpy, display->root, visual, AllocNone); |
296 | else |
275 | } |
297 | rxvt_warn ("no visual found for requested depth %d, using default visual.\n", bitdepth); |
276 | #endif |
|
|
277 | } |
298 | } |
|
|
299 | |
|
|
300 | #endif |
278 | |
301 | |
279 | void |
302 | void |
280 | rxvt_screen::clear () |
303 | rxvt_screen::clear () |
281 | { |
304 | { |
282 | #if XFT |
305 | #if XFT |
… | |
… | |
429 | bool rxvt_display::ref_init () |
452 | bool rxvt_display::ref_init () |
430 | { |
453 | { |
431 | #ifdef LOCAL_X_IS_UNIX |
454 | #ifdef LOCAL_X_IS_UNIX |
432 | if (id[0] == ':') |
455 | if (id[0] == ':') |
433 | { |
456 | { |
434 | if (!(val = rxvt_temp_buf<char> (5 + strlen (id) + 1))) |
457 | char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1); |
435 | return false; |
458 | |
436 | strcpy (val, "unix/"); |
459 | strcpy (val, "unix/"); |
437 | strcat (val, id); |
460 | strcat (val, id); |
|
|
461 | |
438 | dpy = XOpenDisplay (val); |
462 | dpy = XOpenDisplay (val); |
439 | } |
463 | } |
440 | else |
464 | else |
441 | #endif |
465 | #endif |
442 | dpy = 0; |
466 | dpy = 0; |
… | |
… | |
448 | return false; |
472 | return false; |
449 | |
473 | |
450 | screen = DefaultScreen (dpy); |
474 | screen = DefaultScreen (dpy); |
451 | root = DefaultRootWindow (dpy); |
475 | root = DefaultRootWindow (dpy); |
452 | |
476 | |
453 | assert (ARRAY_LENGTH(xa_names) == NUM_XA); |
477 | assert (ecb_array_length (xa_names) == NUM_XA); |
454 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
478 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
455 | |
479 | |
456 | XrmSetDatabase (dpy, get_resources (false)); |
480 | XrmSetDatabase (dpy, get_resources (false)); |
457 | |
481 | |
458 | #ifdef POINTER_BLANK |
482 | #ifdef POINTER_BLANK |
… | |
… | |
464 | blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', |
488 | blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', |
465 | &blackcolour, &blackcolour); |
489 | &blackcolour, &blackcolour); |
466 | XUnloadFont (dpy, f); |
490 | XUnloadFont (dpy, f); |
467 | #endif |
491 | #endif |
468 | |
492 | |
|
|
493 | flags = 0; |
|
|
494 | #if XRENDER |
|
|
495 | int major, minor; |
|
|
496 | if (XRenderQueryVersion (dpy, &major, &minor)) |
|
|
497 | if (major > 0 || (major == 0 && minor >= 11)) |
|
|
498 | { |
|
|
499 | flags |= DISPLAY_HAS_RENDER; |
|
|
500 | |
|
|
501 | if (XFilters *filters = XRenderQueryFilters (dpy, root)) |
|
|
502 | { |
|
|
503 | for (int i = 0; i < filters->nfilter; i++) |
|
|
504 | if (!strcmp (filters->filter [i], FilterConvolution)) |
|
|
505 | flags |= DISPLAY_HAS_RENDER_CONV; |
|
|
506 | |
|
|
507 | XFree (filters); |
|
|
508 | } |
|
|
509 | } |
|
|
510 | #endif |
|
|
511 | |
469 | int fd = XConnectionNumber (dpy); |
512 | int fd = XConnectionNumber (dpy); |
470 | |
513 | |
471 | // try to detect whether we have a local connection. |
514 | // try to detect whether we have a local connection. |
472 | // assume unix domain socket == local, everything else not |
515 | // assume unix domain socket == local, everything else not |
473 | // TODO: might want to check for inet/127.0.0.1 |
516 | // TODO: might want to check for inet/127.0.0.1 |
… | |
… | |
510 | #ifdef POINTER_BLANK |
553 | #ifdef POINTER_BLANK |
511 | XFreeCursor (dpy, blank_cursor); |
554 | XFreeCursor (dpy, blank_cursor); |
512 | #endif |
555 | #endif |
513 | x_ev.stop (); |
556 | x_ev.stop (); |
514 | flush_ev.stop (); |
557 | flush_ev.stop (); |
515 | #ifdef USE_XIM |
558 | #if USE_XIM |
516 | xims.clear (); |
559 | xims.clear (); |
517 | #endif |
560 | #endif |
518 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
561 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
519 | XCloseDisplay (dpy); |
562 | XCloseDisplay (dpy); |
520 | } |
563 | } |
521 | |
564 | |
522 | #ifdef USE_XIM |
565 | #if USE_XIM |
523 | void rxvt_display::im_change_cb () |
566 | void rxvt_display::im_change_cb () |
524 | { |
567 | { |
525 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
568 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
526 | (*i)->call (); |
569 | (*i)->call (); |
527 | } |
570 | } |
… | |
… | |
563 | do |
606 | do |
564 | { |
607 | { |
565 | XEvent xev; |
608 | XEvent xev; |
566 | XNextEvent (dpy, &xev); |
609 | XNextEvent (dpy, &xev); |
567 | |
610 | |
568 | #ifdef USE_XIM |
611 | #if USE_XIM |
569 | if (!XFilterEvent (&xev, None)) |
612 | if (!XFilterEvent (&xev, None)) |
570 | { |
613 | { |
571 | if (xev.type == PropertyNotify |
614 | if (xev.type == PropertyNotify |
572 | && xev.xany.window == root |
615 | && xev.xany.window == root |
573 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
616 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
… | |
… | |
581 | if (!xw[i]) |
624 | if (!xw[i]) |
582 | xw.erase_unordered (i); |
625 | xw.erase_unordered (i); |
583 | else if (xw[i]->window == xev.xany.window) |
626 | else if (xw[i]->window == xev.xany.window) |
584 | xw[i]->call (xev); |
627 | xw[i]->call (xev); |
585 | } |
628 | } |
586 | #ifdef USE_XIM |
629 | #if USE_XIM |
587 | } |
630 | } |
588 | #endif |
631 | #endif |
589 | } |
632 | } |
590 | while (XEventsQueued (dpy, QueuedAlready)); |
633 | while (XEventsQueued (dpy, QueuedAlready)); |
591 | |
634 | |
… | |
… | |
622 | } |
665 | } |
623 | |
666 | |
624 | cur_owner = owner; |
667 | cur_owner = owner; |
625 | } |
668 | } |
626 | |
669 | |
627 | #ifdef USE_XIM |
670 | #if USE_XIM |
628 | |
671 | |
629 | void rxvt_display::reg (im_watcher *w) |
672 | void rxvt_display::reg (im_watcher *w) |
630 | { |
673 | { |
631 | imw.push_back (w); |
674 | imw.push_back (w); |
632 | } |
675 | } |
… | |
… | |
667 | Atom rxvt_display::atom (const char *name) |
710 | Atom rxvt_display::atom (const char *name) |
668 | { |
711 | { |
669 | return XInternAtom (dpy, name, False); |
712 | return XInternAtom (dpy, name, False); |
670 | } |
713 | } |
671 | |
714 | |
|
|
715 | Pixmap |
|
|
716 | rxvt_display::get_pixmap_property (Atom property) |
|
|
717 | { |
|
|
718 | Pixmap pixmap = None; |
|
|
719 | |
|
|
720 | int aformat; |
|
|
721 | unsigned long nitems, bytes_after; |
|
|
722 | Atom atype; |
|
|
723 | unsigned char *prop; |
|
|
724 | int result = XGetWindowProperty (dpy, root, property, |
|
|
725 | 0L, 1L, False, XA_PIXMAP, &atype, &aformat, |
|
|
726 | &nitems, &bytes_after, &prop); |
|
|
727 | if (result == Success) |
|
|
728 | { |
|
|
729 | if (atype == XA_PIXMAP) |
|
|
730 | pixmap = *(Pixmap *)prop; |
|
|
731 | XFree (prop); |
|
|
732 | } |
|
|
733 | |
|
|
734 | return pixmap; |
|
|
735 | } |
|
|
736 | |
672 | ///////////////////////////////////////////////////////////////////////////// |
737 | ///////////////////////////////////////////////////////////////////////////// |
673 | |
738 | |
674 | template class refcache<rxvt_display>; |
739 | template class refcache<rxvt_display>; |
675 | refcache<rxvt_display> displays; |
740 | refcache<rxvt_display> displays; |
676 | |
741 | |
… | |
… | |
685 | |
750 | |
686 | bool |
751 | bool |
687 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
752 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
688 | { |
753 | { |
689 | //TODO: only supports 24 bit |
754 | //TODO: only supports 24 bit |
690 | int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
755 | unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
691 | |
756 | |
692 | #if XFT |
757 | #if XFT |
693 | XRenderPictFormat *format; |
758 | XRenderPictFormat *format; |
694 | |
759 | |
695 | // FUCKING Xft gets it wrong, of course, so work around it. |
760 | // FUCKING Xft gets it wrong, of course, so work around it. |
… | |
… | |
702 | c.color.red = color.r; |
767 | c.color.red = color.r; |
703 | c.color.green = color.g; |
768 | c.color.green = color.g; |
704 | c.color.blue = color.b; |
769 | c.color.blue = color.b; |
705 | c.color.alpha = alpha; |
770 | c.color.alpha = alpha; |
706 | |
771 | |
|
|
772 | // Xft wants premultiplied alpha, but abuses the alpha channel |
|
|
773 | // as blend factor, and doesn't allow us to set the alpha channel |
|
|
774 | c.color.red = c.color.red * alpha / 0xffff; |
|
|
775 | c.color.green = c.color.green * alpha / 0xffff; |
|
|
776 | c.color.blue = c.color.blue * alpha / 0xffff; |
|
|
777 | |
707 | c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red ) |
778 | c.pixel = insert_component (c.color.red , format->direct.redMask , format->direct.red ) |
708 | | insert_component (color.g, format->direct.greenMask, format->direct.green) |
779 | | insert_component (c.color.green, format->direct.greenMask, format->direct.green) |
709 | | insert_component (color.b, format->direct.blueMask , format->direct.blue ) |
780 | | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue ) |
710 | | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); |
781 | | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); |
711 | |
782 | |
712 | return true; |
783 | return true; |
713 | } |
784 | } |
714 | else |
785 | else |
715 | { |
786 | { |
… | |
… | |
718 | d.red = color.r; |
789 | d.red = color.r; |
719 | d.green = color.g; |
790 | d.green = color.g; |
720 | d.blue = color.b; |
791 | d.blue = color.b; |
721 | d.alpha = alpha; |
792 | d.alpha = alpha; |
722 | |
793 | |
723 | return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); |
794 | if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c)) |
|
|
795 | return true; |
724 | } |
796 | } |
725 | #else |
797 | #else |
726 | c.red = color.r; |
798 | c.red = color.r; |
727 | c.green = color.g; |
799 | c.green = color.g; |
728 | c.blue = color.b; |
800 | c.blue = color.b; |
729 | |
801 | |
730 | if (screen->visual->c_class == TrueColor) |
802 | if (screen->visual->c_class == TrueColor) |
731 | { |
803 | { |
732 | c.pixel = (color.r >> (16 - rxvt_popcount (screen->visual->red_mask )) << rxvt_ctz (screen->visual->red_mask )) |
804 | c.pixel = (color.r >> (16 - ecb_popcount32 (screen->visual->red_mask )) << ecb_ctz32 (screen->visual->red_mask )) |
733 | | (color.g >> (16 - rxvt_popcount (screen->visual->green_mask)) << rxvt_ctz (screen->visual->green_mask)) |
805 | | (color.g >> (16 - ecb_popcount32 (screen->visual->green_mask)) << ecb_ctz32 (screen->visual->green_mask)) |
734 | | (color.b >> (16 - rxvt_popcount (screen->visual->blue_mask )) << rxvt_ctz (screen->visual->blue_mask )); |
806 | | (color.b >> (16 - ecb_popcount32 (screen->visual->blue_mask )) << ecb_ctz32 (screen->visual->blue_mask )); |
735 | |
807 | |
736 | return true; |
808 | return true; |
737 | } |
809 | } |
738 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
810 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
739 | return true; |
811 | return true; |
740 | else |
812 | #endif |
|
|
813 | |
|
|
814 | //TODO: set c.color* or c.* |
741 | c.pixel = (color.r + color.g + color.b) > 128*3 |
815 | c.pixel = (color.r * 2 + color.g * 3 + color.b) >= 0x8000 * 6 |
742 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
816 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
743 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
817 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
744 | #endif |
|
|
745 | |
818 | |
746 | return false; |
819 | return false; |
747 | } |
820 | } |
748 | |
821 | |
749 | bool |
822 | bool |
… | |
… | |
805 | |
878 | |
806 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
879 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
807 | // many extra optimisations. |
880 | // many extra optimisations. |
808 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
881 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
809 | |
882 | |
|
|
883 | while (cmap_size) |
|
|
884 | { |
810 | int diff = 0x7fffffffUL; |
885 | int diff = 0x7fffffffL; |
811 | XColor *best = colors; |
886 | XColor *best = colors; |
812 | |
887 | |
813 | for (int i = 0; i < cmap_size; i++) |
888 | for (int i = 0; i < cmap_size; i++) |
814 | { |
|
|
815 | int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2)) |
|
|
816 | + (squared_diff<int> (color.g >> 2, colors [i].green >> 2)) |
|
|
817 | + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2)); |
|
|
818 | |
|
|
819 | if (d < diff) |
|
|
820 | { |
889 | { |
|
|
890 | // simple weighted rgb distance sucks, but keeps it simple |
|
|
891 | int d = abs (color.r - colors [i].red ) * 2 |
|
|
892 | + abs (color.g - colors [i].green) * 3 |
|
|
893 | + abs (color.b - colors [i].blue ); |
|
|
894 | |
|
|
895 | if (d < diff) |
|
|
896 | { |
821 | diff = d; |
897 | diff = d; |
822 | best = colors + i; |
898 | best = colors + i; |
|
|
899 | } |
823 | } |
900 | } |
824 | } |
|
|
825 | |
901 | |
826 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
902 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d,%d)\n", |
827 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff); |
903 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff, best - colors); |
828 | |
904 | |
829 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
905 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
|
|
906 | |
|
|
907 | if (got) |
|
|
908 | break; |
|
|
909 | |
|
|
910 | *best = colors [--cmap_size]; |
|
|
911 | } |
830 | |
912 | |
831 | delete [] colors; |
913 | delete [] colors; |
832 | } |
914 | } |
833 | #endif |
915 | #endif |
834 | |
916 | |
835 | return got; |
917 | return got; |
836 | } |
918 | } |
837 | |
919 | |
838 | void |
920 | void |
839 | rxvt_color::get (rgba &color) |
921 | rxvt_color::get (rgba &color) const |
840 | { |
922 | { |
841 | #if XFT |
923 | #if XFT |
|
|
924 | //TODO premultiplied alpha?? |
842 | color.r = c.color.red; |
925 | color.r = c.color.red; |
843 | color.g = c.color.green; |
926 | color.g = c.color.green; |
844 | color.b = c.color.blue; |
927 | color.b = c.color.blue; |
845 | color.a = c.color.alpha; |
928 | color.a = c.color.alpha; |
846 | #else |
929 | #else |
… | |
… | |
850 | color.a = rgba::MAX_CC; |
933 | color.a = rgba::MAX_CC; |
851 | #endif |
934 | #endif |
852 | } |
935 | } |
853 | |
936 | |
854 | void |
937 | void |
855 | rxvt_color::get (XColor &color) |
938 | rxvt_color::get (XColor &color) const |
856 | { |
939 | { |
857 | rgba c; |
940 | rgba c; |
858 | get (c); |
941 | get (c); |
859 | |
942 | |
860 | color.red = c.r; |
943 | color.red = c.r; |
… | |
… | |
891 | lerp (c.a, to.a, percent) |
974 | lerp (c.a, to.a, percent) |
892 | ) |
975 | ) |
893 | ); |
976 | ); |
894 | } |
977 | } |
895 | |
978 | |
896 | rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, sel_cb cb, void *ptr) |
979 | rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term) |
897 | : display (disp), request_time (tm), request_win (win), request_prop (prop), request_cb (cb), user_data (ptr) |
980 | : display (disp), request_time (tm), request_win (win), request_prop (prop), term (term) |
898 | { |
981 | { |
899 | assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard); |
982 | assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard); |
900 | |
983 | |
901 | timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this); |
984 | timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this); |
902 | timer_ev.repeat = 10.; |
985 | timer_ev.repeat = 10.; |
903 | x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this); |
986 | x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this); |
904 | |
987 | |
905 | incr_buf = 0; |
988 | incr_buf = 0; |
906 | incr_buf_size = incr_buf_fill = 0; |
989 | incr_buf_size = incr_buf_fill = 0; |
907 | selection_wait = Sel_normal; |
990 | selection_wait = Sel_normal; |
|
|
991 | selection_type = selnum; |
|
|
992 | cb_sv = 0; |
|
|
993 | } |
|
|
994 | |
|
|
995 | void |
|
|
996 | rxvt_selection::stop () |
|
|
997 | { |
|
|
998 | free (incr_buf); |
|
|
999 | incr_buf = 0; |
|
|
1000 | timer_ev.stop (); |
|
|
1001 | x_ev.stop (display); |
|
|
1002 | } |
|
|
1003 | |
|
|
1004 | rxvt_selection::~rxvt_selection () |
|
|
1005 | { |
|
|
1006 | stop (); |
|
|
1007 | } |
|
|
1008 | |
|
|
1009 | void |
|
|
1010 | rxvt_selection::run () |
|
|
1011 | { |
|
|
1012 | int selnum = selection_type; |
|
|
1013 | |
|
|
1014 | #if ENABLE_FRILLS |
|
|
1015 | if (selnum == Sel_Primary && display->selection_owner) |
|
|
1016 | { |
|
|
1017 | /* internal selection */ |
|
|
1018 | char *str = rxvt_wcstombs (display->selection_owner->selection.text, display->selection_owner->selection.len); |
|
|
1019 | finish (str, strlen (str)); |
|
|
1020 | free (str); |
|
|
1021 | return; |
|
|
1022 | } |
|
|
1023 | #endif |
908 | |
1024 | |
909 | #if X_HAVE_UTF8_STRING |
1025 | #if X_HAVE_UTF8_STRING |
910 | selection_type = Sel_UTF8String; |
1026 | selection_type = Sel_UTF8String; |
911 | if (request (display->xa[XA_UTF8_STRING], selnum)) |
1027 | if (request (display->xa[XA_UTF8_STRING], selnum)) |
912 | return; |
1028 | return; |
… | |
… | |
918 | |
1034 | |
919 | // fallback to CUT_BUFFER0 if the requested property has no owner |
1035 | // fallback to CUT_BUFFER0 if the requested property has no owner |
920 | handle_selection (display->root, XA_CUT_BUFFER0, false); |
1036 | handle_selection (display->root, XA_CUT_BUFFER0, false); |
921 | } |
1037 | } |
922 | |
1038 | |
923 | rxvt_selection::~rxvt_selection () |
|
|
924 | { |
|
|
925 | stop (); |
|
|
926 | } |
|
|
927 | |
|
|
928 | void |
1039 | void |
929 | rxvt_selection::stop () |
1040 | rxvt_selection::finish (char *data, unsigned int len) |
930 | { |
1041 | { |
931 | free (incr_buf); |
1042 | if (!cb_sv) |
932 | incr_buf = 0; |
1043 | { |
933 | timer_ev.stop (); |
1044 | if (data) |
934 | x_ev.stop (display); |
1045 | term->paste (data, len); |
|
|
1046 | |
|
|
1047 | term->selection_req = 0; |
|
|
1048 | delete this; |
|
|
1049 | } |
|
|
1050 | #if ENABLE_PERL |
|
|
1051 | else |
|
|
1052 | { |
|
|
1053 | stop (); // we do not really trust perl callbacks |
|
|
1054 | rxvt_perl.selection_finish (this, data, len); |
|
|
1055 | } |
|
|
1056 | #endif |
935 | } |
1057 | } |
936 | |
1058 | |
937 | bool |
1059 | bool |
938 | rxvt_selection::request (Atom target, int selnum) |
1060 | rxvt_selection::request (Atom target, int selnum) |
939 | { |
1061 | { |
… | |
… | |
1104 | bailout: |
1226 | bailout: |
1105 | XFree (ct.value); |
1227 | XFree (ct.value); |
1106 | |
1228 | |
1107 | if (selection_wait == Sel_normal) |
1229 | if (selection_wait == Sel_normal) |
1108 | { |
1230 | { |
1109 | stop (); |
1231 | finish (data, data_len); |
1110 | request_cb (data, data_len, this, user_data); |
|
|
1111 | free (data); |
1232 | free (data); |
1112 | } |
1233 | } |
1113 | } |
1234 | } |
1114 | |
1235 | |
1115 | void |
1236 | void |
1116 | rxvt_selection::timer_cb (ev::timer &w, int revents) |
1237 | rxvt_selection::timer_cb (ev::timer &w, int revents) |
1117 | { |
1238 | { |
1118 | if (selection_wait == Sel_incr) |
1239 | if (selection_wait == Sel_incr) |
1119 | rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n"); |
1240 | rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n"); |
1120 | |
1241 | |
1121 | stop (); |
1242 | finish (); |
1122 | request_cb (NULL, 0, this, user_data); |
|
|
1123 | } |
1243 | } |
1124 | |
1244 | |
1125 | void |
1245 | void |
1126 | rxvt_selection::x_cb (XEvent &xev) |
1246 | rxvt_selection::x_cb (XEvent &xev) |
1127 | { |
1247 | { |
… | |
… | |
1134 | handle_selection (xev.xproperty.window, xev.xproperty.atom, true); |
1254 | handle_selection (xev.xproperty.window, xev.xproperty.atom, true); |
1135 | break; |
1255 | break; |
1136 | |
1256 | |
1137 | case SelectionNotify: |
1257 | case SelectionNotify: |
1138 | if (selection_wait == Sel_normal |
1258 | if (selection_wait == Sel_normal |
1139 | && xev.xselection.time == request_time |
1259 | && xev.xselection.time == request_time) |
1140 | && xev.xselection.property == request_prop) |
|
|
1141 | { |
1260 | { |
1142 | timer_ev.stop (); |
1261 | timer_ev.stop (); |
1143 | handle_selection (xev.xselection.requestor, xev.xselection.property, true); |
1262 | handle_selection (xev.xselection.requestor, xev.xselection.property, true); |
1144 | } |
1263 | } |
1145 | break; |
1264 | break; |