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