ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
(Generate patch)

Comparing rxvt-unicode/src/rxvttoolkit.C (file contents):
Revision 1.109 by sf-exg, Mon Dec 13 08:29:25 2010 UTC vs.
Revision 1.137 by root, Thu Jun 7 16:04:31 2012 UTC

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 2 of the License, or
11 * (at your option) any later version. 12 * (at your option) any later version.
33 34
34#if XFT 35#if XFT
35# include <X11/extensions/Xrender.h> 36# include <X11/extensions/Xrender.h>
36#endif 37#endif
37 38
38const char *const xa_names[] = 39static const char *const xa_names[] =
39{ 40{
40 "TEXT", 41 "TEXT",
41 "COMPOUND_TEXT", 42 "COMPOUND_TEXT",
42 "UTF8_STRING", 43 "UTF8_STRING",
43 "MULTIPLE", 44 "MULTIPLE",
65#endif 66#endif
66#if USE_XIM 67#if USE_XIM
67 "WM_LOCALE_NAME", 68 "WM_LOCALE_NAME",
68 "XIM_SERVERS", 69 "XIM_SERVERS",
69#endif 70#endif
70#ifdef ENABLE_TRANSPARENCY 71#if ENABLE_TRANSPARENCY
71 "_XROOTPMAP_ID", 72 "_XROOTPMAP_ID",
72 "ESETROOT_PMAP_ID", 73 "ESETROOT_PMAP_ID",
73#endif 74#endif
74#if ENABLE_XEMBED 75#if ENABLE_XEMBED
75 "_XEMBED", 76 "_XEMBED",
152 put (*this->begin ()); 153 put (*this->begin ());
153} 154}
154 155
155///////////////////////////////////////////////////////////////////////////// 156/////////////////////////////////////////////////////////////////////////////
156 157
157#ifdef USE_XIM 158#if USE_XIM
158 159
159static void 160static void
160#if XIMCB_PROTO_BROKEN 161#if XIMCB_PROTO_BROKEN
161im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) 162im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
162#else 163#else
227{ 228{
228} 229}
229 230
230rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) 231rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h)
231{ 232{
232 // it's actually faster to re-allocate every time. don't ask me
233 // why, but its likely no big deal there are no roundtrips
234 // (I think/hope).
235 if (!scratch_area || w > scratch_w || h > scratch_h || 1/*D*/) 233 if (!scratch_area || w > scratch_w || h > scratch_h)
236 { 234 {
237 if (scratch_area) 235 if (scratch_area)
238 { 236 {
239 XFreePixmap (dpy, scratch_area->drawable); 237 XFreePixmap (dpy, scratch_area->drawable);
240 delete scratch_area; 238 delete scratch_area;
262 depth = DefaultDepthOfScreen (screen); 260 depth = DefaultDepthOfScreen (screen);
263 visual = DefaultVisualOfScreen (screen); 261 visual = DefaultVisualOfScreen (screen);
264 cmap = DefaultColormapOfScreen (screen); 262 cmap = DefaultColormapOfScreen (screen);
265} 263}
266 264
265#if ENABLE_FRILLS
266
267void 267void
268rxvt_screen::select_visual (int bitdepth) 268rxvt_screen::select_visual (int id)
269{ 269{
270#if XFT
271 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 ("cannot requested visual id 0x%02x, using default visual.\n", id);
285}
286
287void
288rxvt_screen::select_depth (int bitdepth)
289{
290 XVisualInfo vinfo;
272 291
273 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) 292 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo))
274 {
275 depth = bitdepth;
276 visual = vinfo.visual; 293 select_visual (vinfo.visualid);
277 cmap = XCreateColormap (dpy, display->root, visual, AllocNone); 294 else
278 } 295 rxvt_warn ("no visual found for requested depth %d, using default visual.\n", bitdepth);
279#endif
280} 296}
297
298#endif
281 299
282void 300void
283rxvt_screen::clear () 301rxvt_screen::clear ()
284{ 302{
285#if XFT 303#if XFT
314 /* 332 /*
315 * get resources using the X library function 333 * get resources using the X library function
316 */ 334 */
317 char *displayResource, *xe; 335 char *displayResource, *xe;
318 XrmDatabase rdb1, database = 0; 336 XrmDatabase rdb1, database = 0;
337
338#if !XLIB_ILLEGAL_ACCESS
339 /* work around a bug in XrmSetDatabase where it frees the db, see ref_next */
340 database = XrmGetStringDatabase ("");
341#endif
319 342
320 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 343 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
321 // as opposed to "standard practise", we always read in ~/.Xdefaults 344 // as opposed to "standard practise", we always read in ~/.Xdefaults
322 345
323 // 6. System wide per application default file. 346 // 6. System wide per application default file.
427bool rxvt_display::ref_init () 450bool rxvt_display::ref_init ()
428{ 451{
429#ifdef LOCAL_X_IS_UNIX 452#ifdef LOCAL_X_IS_UNIX
430 if (id[0] == ':') 453 if (id[0] == ':')
431 { 454 {
432 if (!(val = rxvt_temp_buf<char> (5 + strlen (id) + 1))) 455 char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1);
433 return false; 456
434 strcpy (val, "unix/"); 457 strcpy (val, "unix/");
435 strcat (val, id); 458 strcat (val, id);
459
436 dpy = XOpenDisplay (val); 460 dpy = XOpenDisplay (val);
437 } 461 }
438 else 462 else
439#endif 463#endif
440 dpy = 0; 464 dpy = 0;
446 return false; 470 return false;
447 471
448 screen = DefaultScreen (dpy); 472 screen = DefaultScreen (dpy);
449 root = DefaultRootWindow (dpy); 473 root = DefaultRootWindow (dpy);
450 474
451 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); 475 assert (ecb_array_length (xa_names) == NUM_XA);
452 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); 476 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa);
453 477
454 XrmSetDatabase (dpy, get_resources (false)); 478 XrmSetDatabase (dpy, get_resources (false));
455 479
456#ifdef POINTER_BLANK 480#ifdef POINTER_BLANK
462 blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', 486 blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ',
463 &blackcolour, &blackcolour); 487 &blackcolour, &blackcolour);
464 XUnloadFont (dpy, f); 488 XUnloadFont (dpy, f);
465#endif 489#endif
466 490
491 flags = 0;
492#if XRENDER
493 int major, minor;
494 if (XRenderQueryVersion (dpy, &major, &minor))
495 {
496 flags |= DISPLAY_HAS_RENDER;
497
498 if (major > 0 || (major == 0 && minor >= 10))
499 flags |= DISPLAY_HAS_RENDER_10;
500
501 if (major > 0 || (major == 0 && minor >= 11))
502 flags |= DISPLAY_HAS_RENDER_MUL;
503
504 XFilters *filters = XRenderQueryFilters (dpy, root);
505 if (filters)
506 {
507 for (int i = 0; i < filters->nfilter; i++)
508 if (!strcmp (filters->filter[i], FilterConvolution))
509 flags |= DISPLAY_HAS_RENDER_CONV;
510
511 XFree (filters);
512 }
513 }
514#endif
515
467 int fd = XConnectionNumber (dpy); 516 int fd = XConnectionNumber (dpy);
468 517
469 // try to detect whether we have a local connection. 518 // try to detect whether we have a local connection.
470 // assume unix domain socket == local, everything else not 519 // assume unix domain socket == local, everything else not
471 // TODO: might want to check for inet/127.0.0.1 520 // TODO: might want to check for inet/127.0.0.1
491rxvt_display::ref_next () 540rxvt_display::ref_next ()
492{ 541{
493 // TODO: somehow check whether the database files/resources changed 542 // TODO: somehow check whether the database files/resources changed
494 // before affording re-loading/parsing 543 // before affording re-loading/parsing
495 XrmDestroyDatabase (XrmGetDatabase (dpy)); 544 XrmDestroyDatabase (XrmGetDatabase (dpy));
545#if XLIB_ILLEGAL_ACCESS
546 /* work around a bug in XrmSetDatabase where it frees the db */
547 dpy->db = 0;
548#endif
496 XrmSetDatabase (dpy, get_resources (true)); 549 XrmSetDatabase (dpy, get_resources (true));
497} 550}
498 551
499rxvt_display::~rxvt_display () 552rxvt_display::~rxvt_display ()
500{ 553{
504#ifdef POINTER_BLANK 557#ifdef POINTER_BLANK
505 XFreeCursor (dpy, blank_cursor); 558 XFreeCursor (dpy, blank_cursor);
506#endif 559#endif
507 x_ev.stop (); 560 x_ev.stop ();
508 flush_ev.stop (); 561 flush_ev.stop ();
509#ifdef USE_XIM 562#if USE_XIM
510 xims.clear (); 563 xims.clear ();
511#endif 564#endif
565 XrmDestroyDatabase (XrmGetDatabase (dpy));
512 XCloseDisplay (dpy); 566 XCloseDisplay (dpy);
513} 567}
514 568
515#ifdef USE_XIM 569#if USE_XIM
516void rxvt_display::im_change_cb () 570void rxvt_display::im_change_cb ()
517{ 571{
518 for (im_watcher **i = imw.begin (); i != imw.end (); ++i) 572 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
519 (*i)->call (); 573 (*i)->call ();
520} 574}
556 do 610 do
557 { 611 {
558 XEvent xev; 612 XEvent xev;
559 XNextEvent (dpy, &xev); 613 XNextEvent (dpy, &xev);
560 614
561#ifdef USE_XIM 615#if USE_XIM
562 if (!XFilterEvent (&xev, None)) 616 if (!XFilterEvent (&xev, None))
563 { 617 {
564 if (xev.type == PropertyNotify 618 if (xev.type == PropertyNotify
565 && xev.xany.window == root 619 && xev.xany.window == root
566 && xev.xproperty.atom == xa[XA_XIM_SERVERS]) 620 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
574 if (!xw[i]) 628 if (!xw[i])
575 xw.erase_unordered (i); 629 xw.erase_unordered (i);
576 else if (xw[i]->window == xev.xany.window) 630 else if (xw[i]->window == xev.xany.window)
577 xw[i]->call (xev); 631 xw[i]->call (xev);
578 } 632 }
579#ifdef USE_XIM 633#if USE_XIM
580 } 634 }
581#endif 635#endif
582 } 636 }
583 while (XEventsQueued (dpy, QueuedAlready)); 637 while (XEventsQueued (dpy, QueuedAlready));
584 638
615 } 669 }
616 670
617 cur_owner = owner; 671 cur_owner = owner;
618} 672}
619 673
620#ifdef USE_XIM 674#if USE_XIM
621 675
622void rxvt_display::reg (im_watcher *w) 676void rxvt_display::reg (im_watcher *w)
623{ 677{
624 imw.push_back (w); 678 imw.push_back (w);
625} 679}
660Atom rxvt_display::atom (const char *name) 714Atom rxvt_display::atom (const char *name)
661{ 715{
662 return XInternAtom (dpy, name, False); 716 return XInternAtom (dpy, name, False);
663} 717}
664 718
719Pixmap
720rxvt_display::get_pixmap_property (Atom property)
721{
722 Pixmap pixmap = None;
723
724 int aformat;
725 unsigned long nitems, bytes_after;
726 Atom atype;
727 unsigned char *prop;
728 int result = XGetWindowProperty (dpy, root, property,
729 0L, 1L, False, XA_PIXMAP, &atype, &aformat,
730 &nitems, &bytes_after, &prop);
731 if (result == Success)
732 {
733 if (atype == XA_PIXMAP)
734 pixmap = *(Pixmap *)prop;
735 XFree (prop);
736 }
737
738 return pixmap;
739}
740
665///////////////////////////////////////////////////////////////////////////// 741/////////////////////////////////////////////////////////////////////////////
666 742
667template class refcache<rxvt_display>; 743template class refcache<rxvt_display>;
668refcache<rxvt_display> displays; 744refcache<rxvt_display> displays;
669 745
678 754
679bool 755bool
680rxvt_color::alloc (rxvt_screen *screen, const rgba &color) 756rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
681{ 757{
682 //TODO: only supports 24 bit 758 //TODO: only supports 24 bit
683 int alpha = color.a >= 0xff00 ? 0xffff : color.a; 759 unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a;
684 760
685#if XFT 761#if XFT
686 XRenderPictFormat *format; 762 XRenderPictFormat *format;
687 763
688 // FUCKING Xft gets it wrong, of course, so work around it. 764 // FUCKING Xft gets it wrong, of course, so work around it.
695 c.color.red = color.r; 771 c.color.red = color.r;
696 c.color.green = color.g; 772 c.color.green = color.g;
697 c.color.blue = color.b; 773 c.color.blue = color.b;
698 c.color.alpha = alpha; 774 c.color.alpha = alpha;
699 775
776 // Xft wants premultiplied alpha, but abuses the alpha channel
777 // as blend factor, and doesn't allow us to set the alpha channel
778 c.color.red = c.color.red * alpha / 0xffff;
779 c.color.green = c.color.green * alpha / 0xffff;
780 c.color.blue = c.color.blue * alpha / 0xffff;
781
700 c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red ) 782 c.pixel = insert_component (c.color.red , format->direct.redMask , format->direct.red )
701 | insert_component (color.g, format->direct.greenMask, format->direct.green) 783 | insert_component (c.color.green, format->direct.greenMask, format->direct.green)
702 | insert_component (color.b, format->direct.blueMask , format->direct.blue ) 784 | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue )
703 | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); 785 | insert_component (alpha , format->direct.alphaMask, format->direct.alpha);
704 786
705 return true; 787 return true;
706 } 788 }
707 else 789 else
708 { 790 {
711 d.red = color.r; 793 d.red = color.r;
712 d.green = color.g; 794 d.green = color.g;
713 d.blue = color.b; 795 d.blue = color.b;
714 d.alpha = alpha; 796 d.alpha = alpha;
715 797
716 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); 798 if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c))
799 return true;
717 } 800 }
718#else 801#else
719 c.red = color.r; 802 c.red = color.r;
720 c.green = color.g; 803 c.green = color.g;
721 c.blue = color.b; 804 c.blue = color.b;
722 805
723 if (screen->visual->c_class == TrueColor) 806 if (screen->visual->c_class == TrueColor)
724 { 807 {
725 c.pixel = (color.r >> (16 - rxvt_popcount (screen->visual->red_mask )) << rxvt_ctz (screen->visual->red_mask )) 808 c.pixel = (color.r >> (16 - ecb_popcount32 (screen->visual->red_mask )) << ecb_ctz32 (screen->visual->red_mask ))
726 | (color.g >> (16 - rxvt_popcount (screen->visual->green_mask)) << rxvt_ctz (screen->visual->green_mask)) 809 | (color.g >> (16 - ecb_popcount32 (screen->visual->green_mask)) << ecb_ctz32 (screen->visual->green_mask))
727 | (color.b >> (16 - rxvt_popcount (screen->visual->blue_mask )) << rxvt_ctz (screen->visual->blue_mask )); 810 | (color.b >> (16 - ecb_popcount32 (screen->visual->blue_mask )) << ecb_ctz32 (screen->visual->blue_mask ));
728 811
729 return true; 812 return true;
730 } 813 }
731 else if (XAllocColor (screen->dpy, screen->cmap, &c)) 814 else if (XAllocColor (screen->dpy, screen->cmap, &c))
732 return true; 815 return true;
733 else 816#endif
817
734 c.pixel = (color.r + color.g + color.b) > 128*3 818 c.pixel = (color.r + color.g + color.b) > 128*3
735 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) 819 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy))
736 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); 820 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy));
737#endif
738 821
739 return false; 822 return false;
740} 823}
741 824
742bool 825bool
827 910
828 return got; 911 return got;
829} 912}
830 913
831void 914void
832rxvt_color::get (rgba &color) 915rxvt_color::get (rgba &color) const
833{ 916{
834#if XFT 917#if XFT
835 color.r = c.color.red; 918 color.r = c.color.red;
836 color.g = c.color.green; 919 color.g = c.color.green;
837 color.b = c.color.blue; 920 color.b = c.color.blue;
843 color.a = rgba::MAX_CC; 926 color.a = rgba::MAX_CC;
844#endif 927#endif
845} 928}
846 929
847void 930void
848rxvt_color::get (XColor &color) 931rxvt_color::get (XColor &color) const
849{ 932{
850 rgba c; 933 rgba c;
851 get (c); 934 get (c);
852 935
853 color.red = c.r; 936 color.red = c.r;
884 lerp (c.a, to.a, percent) 967 lerp (c.a, to.a, percent)
885 ) 968 )
886 ); 969 );
887} 970}
888 971
972rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term)
973: display (disp), request_time (tm), request_win (win), request_prop (prop), term (term)
974{
975 assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard);
976
977 timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this);
978 timer_ev.repeat = 10.;
979 x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this);
980
981 incr_buf = 0;
982 incr_buf_size = incr_buf_fill = 0;
983 selection_wait = Sel_normal;
984 selection_type = selnum;
985 cb_sv = 0;
986}
987
988void
989rxvt_selection::stop ()
990{
991 free (incr_buf);
992 incr_buf = 0;
993 timer_ev.stop ();
994 x_ev.stop (display);
995}
996
997rxvt_selection::~rxvt_selection ()
998{
999 stop ();
1000}
1001
1002void
1003rxvt_selection::run ()
1004{
1005 int selnum = selection_type;
1006
1007#if ENABLE_FRILLS
1008 if (selnum == Sel_Primary && display->selection_owner)
1009 {
1010 /* internal selection */
1011 char *str = rxvt_wcstombs (display->selection_owner->selection.text, display->selection_owner->selection.len);
1012 finish (str, strlen (str));
1013 free (str);
1014 return;
1015 }
1016#endif
1017
1018#if X_HAVE_UTF8_STRING
1019 selection_type = Sel_UTF8String;
1020 if (request (display->xa[XA_UTF8_STRING], selnum))
1021 return;
1022#else
1023 selection_type = Sel_CompoundText;
1024 if (request (display->xa[XA_COMPOUND_TEXT], selnum))
1025 return;
1026#endif
1027
1028 // fallback to CUT_BUFFER0 if the requested property has no owner
1029 handle_selection (display->root, XA_CUT_BUFFER0, false);
1030}
1031
1032void
1033rxvt_selection::finish (char *data, unsigned int len)
1034{
1035 if (!cb_sv)
1036 {
1037 if (data)
1038 term->paste (data, len);
1039
1040 term->selection_req = 0;
1041 delete this;
1042 }
1043#if ENABLE_PERL
1044 else
1045 {
1046 stop (); // we do not really trust perl callbacks
1047 rxvt_perl.selection_finish (this, data, len);
1048 }
1049#endif
1050}
1051
1052bool
1053rxvt_selection::request (Atom target, int selnum)
1054{
1055 Atom sel;
1056
1057 selection_type |= selnum;
1058
1059 if (selnum == Sel_Primary)
1060 sel = XA_PRIMARY;
1061 else if (selnum == Sel_Secondary)
1062 sel = XA_SECONDARY;
1063 else
1064 sel = display->xa[XA_CLIPBOARD];
1065
1066 if (XGetSelectionOwner (display->dpy, sel) != None)
1067 {
1068 XConvertSelection (display->dpy, sel, target, request_prop,
1069 request_win, request_time);
1070 x_ev.start (display, request_win);
1071 timer_ev.again ();
1072 return true;
1073 }
1074
1075 return false;
1076}
1077
1078void
1079rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop)
1080{
1081 Display *dpy = display->dpy;
1082 char *data = 0;
1083 unsigned int data_len = 0;
1084 unsigned long bytes_after;
1085 XTextProperty ct;
1086
1087 // check for failed XConvertSelection
1088 if (prop == None)
1089 {
1090 bool error = true;
1091 int selnum = selection_type & Sel_whereMask;
1092
1093 if (selection_type & Sel_CompoundText)
1094 {
1095 selection_type = 0;
1096 error = !request (XA_STRING, selnum);
1097 }
1098
1099 if (selection_type & Sel_UTF8String)
1100 {
1101 selection_type = Sel_CompoundText;
1102 error = !request (display->xa[XA_COMPOUND_TEXT], selnum);
1103 }
1104
1105 if (error)
1106 {
1107 ct.value = 0;
1108 goto bailout;
1109 }
1110
1111 return;
1112 }
1113
1114 // length == (2^31 - 1) / 4, as gdk
1115 if (XGetWindowProperty (dpy, win, prop,
1116 0, 0x1fffffff,
1117 delete_prop, AnyPropertyType,
1118 &ct.encoding, &ct.format,
1119 &ct.nitems, &bytes_after,
1120 &ct.value) != Success)
1121 {
1122 ct.value = 0;
1123 goto bailout;
1124 }
1125
1126 if (ct.encoding == None)
1127 goto bailout;
1128
1129 if (ct.value == 0)
1130 goto bailout;
1131
1132 if (ct.encoding == display->xa[XA_INCR])
1133 {
1134 // INCR selection, start handshake
1135 if (!delete_prop)
1136 XDeleteProperty (dpy, win, prop);
1137
1138 selection_wait = Sel_incr;
1139 timer_ev.again ();
1140
1141 goto bailout;
1142 }
1143
1144 if (ct.nitems == 0)
1145 {
1146 if (selection_wait == Sel_incr)
1147 {
1148 XFree (ct.value);
1149
1150 // finally complete, now paste the whole thing
1151 selection_wait = Sel_normal;
1152 ct.value = (unsigned char *)incr_buf;
1153 ct.nitems = incr_buf_fill;
1154 incr_buf = 0;
1155 timer_ev.stop ();
1156 }
1157 else
1158 {
1159 // avoid recursion
1160 if (win != display->root || prop != XA_CUT_BUFFER0)
1161 {
1162 XFree (ct.value);
1163
1164 // fallback to CUT_BUFFER0 if the requested property
1165 // has an owner but is empty
1166 handle_selection (display->root, XA_CUT_BUFFER0, False);
1167 return;
1168 }
1169
1170 goto bailout;
1171 }
1172 }
1173 else if (selection_wait == Sel_incr)
1174 {
1175 timer_ev.again ();
1176
1177 while (incr_buf_fill + ct.nitems > incr_buf_size)
1178 {
1179 incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024;
1180 incr_buf = (char *)rxvt_realloc (incr_buf, incr_buf_size);
1181 }
1182
1183 memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems);
1184 incr_buf_fill += ct.nitems;
1185
1186 goto bailout;
1187 }
1188
1189 char **cl;
1190 int cr;
1191
1192 // we honour the first item only
1193
1194#if !ENABLE_MINIMAL
1195 // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it
1196 // so recode it manually
1197 if (ct.encoding == display->xa[XA_UTF8_STRING])
1198 {
1199 wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems);
1200 data = rxvt_wcstombs (w);
1201 free (w);
1202 }
1203 else
1204#endif
1205 if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0
1206 && cl)
1207 {
1208 data = strdup (cl[0]);
1209 XFreeStringList (cl);
1210 }
1211 else
1212 {
1213 // paste raw
1214 data = strdup ((const char *)ct.value);
1215 }
1216
1217 data_len = strlen (data);
1218
1219bailout:
1220 XFree (ct.value);
1221
1222 if (selection_wait == Sel_normal)
1223 {
1224 finish (data, data_len);
1225 free (data);
1226 }
1227}
1228
1229void
1230rxvt_selection::timer_cb (ev::timer &w, int revents)
1231{
1232 if (selection_wait == Sel_incr)
1233 rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n");
1234
1235 finish ();
1236}
1237
1238void
1239rxvt_selection::x_cb (XEvent &xev)
1240{
1241 switch (xev.type)
1242 {
1243 case PropertyNotify:
1244 if (selection_wait == Sel_incr
1245 && xev.xproperty.atom == request_prop
1246 && xev.xproperty.state == PropertyNewValue)
1247 handle_selection (xev.xproperty.window, xev.xproperty.atom, true);
1248 break;
1249
1250 case SelectionNotify:
1251 if (selection_wait == Sel_normal
1252 && xev.xselection.time == request_time)
1253 {
1254 timer_ev.stop ();
1255 handle_selection (xev.xselection.requestor, xev.xselection.property, true);
1256 }
1257 break;
1258 }
1259}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines