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.115 by sf-exg, Fri Feb 11 01:24:46 2011 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.
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
259 depth = DefaultDepthOfScreen (screen); 260 depth = DefaultDepthOfScreen (screen);
260 visual = DefaultVisualOfScreen (screen); 261 visual = DefaultVisualOfScreen (screen);
261 cmap = DefaultColormapOfScreen (screen); 262 cmap = DefaultColormapOfScreen (screen);
262} 263}
263 264
265#if ENABLE_FRILLS
266
264void 267void
265rxvt_screen::select_visual (int bitdepth) 268rxvt_screen::select_visual (int id)
266{ 269{
267#if XFT
268 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;
269 291
270 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) 292 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo))
271 {
272 depth = bitdepth;
273 visual = vinfo.visual; 293 select_visual (vinfo.visualid);
274 cmap = XCreateColormap (dpy, display->root, visual, AllocNone); 294 else
275 } 295 rxvt_warn ("no visual found for requested depth %d, using default visual.\n", bitdepth);
276#endif
277} 296}
297
298#endif
278 299
279void 300void
280rxvt_screen::clear () 301rxvt_screen::clear ()
281{ 302{
282#if XFT 303#if XFT
429bool rxvt_display::ref_init () 450bool rxvt_display::ref_init ()
430{ 451{
431#ifdef LOCAL_X_IS_UNIX 452#ifdef LOCAL_X_IS_UNIX
432 if (id[0] == ':') 453 if (id[0] == ':')
433 { 454 {
434 if (!(val = rxvt_temp_buf<char> (5 + strlen (id) + 1))) 455 char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1);
435 return false; 456
436 strcpy (val, "unix/"); 457 strcpy (val, "unix/");
437 strcat (val, id); 458 strcat (val, id);
459
438 dpy = XOpenDisplay (val); 460 dpy = XOpenDisplay (val);
439 } 461 }
440 else 462 else
441#endif 463#endif
442 dpy = 0; 464 dpy = 0;
448 return false; 470 return false;
449 471
450 screen = DefaultScreen (dpy); 472 screen = DefaultScreen (dpy);
451 root = DefaultRootWindow (dpy); 473 root = DefaultRootWindow (dpy);
452 474
453 assert (ARRAY_LENGTH(xa_names) == NUM_XA); 475 assert (ecb_array_length (xa_names) == NUM_XA);
454 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); 476 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa);
455 477
456 XrmSetDatabase (dpy, get_resources (false)); 478 XrmSetDatabase (dpy, get_resources (false));
457 479
458#ifdef POINTER_BLANK 480#ifdef POINTER_BLANK
464 blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', 486 blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ',
465 &blackcolour, &blackcolour); 487 &blackcolour, &blackcolour);
466 XUnloadFont (dpy, f); 488 XUnloadFont (dpy, f);
467#endif 489#endif
468 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
469 int fd = XConnectionNumber (dpy); 516 int fd = XConnectionNumber (dpy);
470 517
471 // try to detect whether we have a local connection. 518 // try to detect whether we have a local connection.
472 // assume unix domain socket == local, everything else not 519 // assume unix domain socket == local, everything else not
473 // TODO: might want to check for inet/127.0.0.1 520 // TODO: might want to check for inet/127.0.0.1
510#ifdef POINTER_BLANK 557#ifdef POINTER_BLANK
511 XFreeCursor (dpy, blank_cursor); 558 XFreeCursor (dpy, blank_cursor);
512#endif 559#endif
513 x_ev.stop (); 560 x_ev.stop ();
514 flush_ev.stop (); 561 flush_ev.stop ();
515#ifdef USE_XIM 562#if USE_XIM
516 xims.clear (); 563 xims.clear ();
517#endif 564#endif
518 XrmDestroyDatabase (XrmGetDatabase (dpy)); 565 XrmDestroyDatabase (XrmGetDatabase (dpy));
519 XCloseDisplay (dpy); 566 XCloseDisplay (dpy);
520} 567}
521 568
522#ifdef USE_XIM 569#if USE_XIM
523void rxvt_display::im_change_cb () 570void rxvt_display::im_change_cb ()
524{ 571{
525 for (im_watcher **i = imw.begin (); i != imw.end (); ++i) 572 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
526 (*i)->call (); 573 (*i)->call ();
527} 574}
563 do 610 do
564 { 611 {
565 XEvent xev; 612 XEvent xev;
566 XNextEvent (dpy, &xev); 613 XNextEvent (dpy, &xev);
567 614
568#ifdef USE_XIM 615#if USE_XIM
569 if (!XFilterEvent (&xev, None)) 616 if (!XFilterEvent (&xev, None))
570 { 617 {
571 if (xev.type == PropertyNotify 618 if (xev.type == PropertyNotify
572 && xev.xany.window == root 619 && xev.xany.window == root
573 && xev.xproperty.atom == xa[XA_XIM_SERVERS]) 620 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
581 if (!xw[i]) 628 if (!xw[i])
582 xw.erase_unordered (i); 629 xw.erase_unordered (i);
583 else if (xw[i]->window == xev.xany.window) 630 else if (xw[i]->window == xev.xany.window)
584 xw[i]->call (xev); 631 xw[i]->call (xev);
585 } 632 }
586#ifdef USE_XIM 633#if USE_XIM
587 } 634 }
588#endif 635#endif
589 } 636 }
590 while (XEventsQueued (dpy, QueuedAlready)); 637 while (XEventsQueued (dpy, QueuedAlready));
591 638
622 } 669 }
623 670
624 cur_owner = owner; 671 cur_owner = owner;
625} 672}
626 673
627#ifdef USE_XIM 674#if USE_XIM
628 675
629void rxvt_display::reg (im_watcher *w) 676void rxvt_display::reg (im_watcher *w)
630{ 677{
631 imw.push_back (w); 678 imw.push_back (w);
632} 679}
667Atom rxvt_display::atom (const char *name) 714Atom rxvt_display::atom (const char *name)
668{ 715{
669 return XInternAtom (dpy, name, False); 716 return XInternAtom (dpy, name, False);
670} 717}
671 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
672///////////////////////////////////////////////////////////////////////////// 741/////////////////////////////////////////////////////////////////////////////
673 742
674template class refcache<rxvt_display>; 743template class refcache<rxvt_display>;
675refcache<rxvt_display> displays; 744refcache<rxvt_display> displays;
676 745
685 754
686bool 755bool
687rxvt_color::alloc (rxvt_screen *screen, const rgba &color) 756rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
688{ 757{
689 //TODO: only supports 24 bit 758 //TODO: only supports 24 bit
690 int alpha = color.a >= 0xff00 ? 0xffff : color.a; 759 unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a;
691 760
692#if XFT 761#if XFT
693 XRenderPictFormat *format; 762 XRenderPictFormat *format;
694 763
695 // FUCKING Xft gets it wrong, of course, so work around it. 764 // FUCKING Xft gets it wrong, of course, so work around it.
702 c.color.red = color.r; 771 c.color.red = color.r;
703 c.color.green = color.g; 772 c.color.green = color.g;
704 c.color.blue = color.b; 773 c.color.blue = color.b;
705 c.color.alpha = alpha; 774 c.color.alpha = alpha;
706 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
707 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 )
708 | insert_component (color.g, format->direct.greenMask, format->direct.green) 783 | insert_component (c.color.green, format->direct.greenMask, format->direct.green)
709 | insert_component (color.b, format->direct.blueMask , format->direct.blue ) 784 | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue )
710 | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); 785 | insert_component (alpha , format->direct.alphaMask, format->direct.alpha);
711 786
712 return true; 787 return true;
713 } 788 }
714 else 789 else
715 { 790 {
718 d.red = color.r; 793 d.red = color.r;
719 d.green = color.g; 794 d.green = color.g;
720 d.blue = color.b; 795 d.blue = color.b;
721 d.alpha = alpha; 796 d.alpha = alpha;
722 797
723 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); 798 if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c))
799 return true;
724 } 800 }
725#else 801#else
726 c.red = color.r; 802 c.red = color.r;
727 c.green = color.g; 803 c.green = color.g;
728 c.blue = color.b; 804 c.blue = color.b;
729 805
730 if (screen->visual->c_class == TrueColor) 806 if (screen->visual->c_class == TrueColor)
731 { 807 {
732 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 ))
733 | (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))
734 | (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 ));
735 811
736 return true; 812 return true;
737 } 813 }
738 else if (XAllocColor (screen->dpy, screen->cmap, &c)) 814 else if (XAllocColor (screen->dpy, screen->cmap, &c))
739 return true; 815 return true;
740 else 816#endif
817
741 c.pixel = (color.r + color.g + color.b) > 128*3 818 c.pixel = (color.r + color.g + color.b) > 128*3
742 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) 819 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy))
743 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); 820 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy));
744#endif
745 821
746 return false; 822 return false;
747} 823}
748 824
749bool 825bool
834 910
835 return got; 911 return got;
836} 912}
837 913
838void 914void
839rxvt_color::get (rgba &color) 915rxvt_color::get (rgba &color) const
840{ 916{
841#if XFT 917#if XFT
842 color.r = c.color.red; 918 color.r = c.color.red;
843 color.g = c.color.green; 919 color.g = c.color.green;
844 color.b = c.color.blue; 920 color.b = c.color.blue;
850 color.a = rgba::MAX_CC; 926 color.a = rgba::MAX_CC;
851#endif 927#endif
852} 928}
853 929
854void 930void
855rxvt_color::get (XColor &color) 931rxvt_color::get (XColor &color) const
856{ 932{
857 rgba c; 933 rgba c;
858 get (c); 934 get (c);
859 935
860 color.red = c.r; 936 color.red = c.r;
891 lerp (c.a, to.a, percent) 967 lerp (c.a, to.a, percent)
892 ) 968 )
893 ); 969 );
894} 970}
895 971
896rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, sel_cb cb, void *ptr) 972rxvt_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) 973: display (disp), request_time (tm), request_win (win), request_prop (prop), term (term)
898{ 974{
899 assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard); 975 assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard);
900 976
901 timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this); 977 timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this);
902 timer_ev.repeat = 10.; 978 timer_ev.repeat = 10.;
903 x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this); 979 x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this);
904 980
905 incr_buf = 0; 981 incr_buf = 0;
906 incr_buf_size = incr_buf_fill = 0; 982 incr_buf_size = incr_buf_fill = 0;
907 selection_wait = Sel_normal; 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
908 1017
909#if X_HAVE_UTF8_STRING 1018#if X_HAVE_UTF8_STRING
910 selection_type = Sel_UTF8String; 1019 selection_type = Sel_UTF8String;
911 if (request (display->xa[XA_UTF8_STRING], selnum)) 1020 if (request (display->xa[XA_UTF8_STRING], selnum))
912 return; 1021 return;
918 1027
919 // fallback to CUT_BUFFER0 if the requested property has no owner 1028 // fallback to CUT_BUFFER0 if the requested property has no owner
920 handle_selection (display->root, XA_CUT_BUFFER0, false); 1029 handle_selection (display->root, XA_CUT_BUFFER0, false);
921} 1030}
922 1031
923rxvt_selection::~rxvt_selection ()
924{
925 stop ();
926}
927
928void 1032void
929rxvt_selection::stop () 1033rxvt_selection::finish (char *data, unsigned int len)
930{ 1034{
931 free (incr_buf); 1035 if (!cb_sv)
932 incr_buf = 0; 1036 {
933 timer_ev.stop (); 1037 if (data)
934 x_ev.stop (display); 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
935} 1050}
936 1051
937bool 1052bool
938rxvt_selection::request (Atom target, int selnum) 1053rxvt_selection::request (Atom target, int selnum)
939{ 1054{
1104bailout: 1219bailout:
1105 XFree (ct.value); 1220 XFree (ct.value);
1106 1221
1107 if (selection_wait == Sel_normal) 1222 if (selection_wait == Sel_normal)
1108 { 1223 {
1109 stop (); 1224 finish (data, data_len);
1110 request_cb (data, data_len, this, user_data);
1111 free (data); 1225 free (data);
1112 } 1226 }
1113} 1227}
1114 1228
1115void 1229void
1116rxvt_selection::timer_cb (ev::timer &w, int revents) 1230rxvt_selection::timer_cb (ev::timer &w, int revents)
1117{ 1231{
1118 if (selection_wait == Sel_incr) 1232 if (selection_wait == Sel_incr)
1119 rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n"); 1233 rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n");
1120 1234
1121 stop (); 1235 finish ();
1122 request_cb (NULL, 0, this, user_data);
1123} 1236}
1124 1237
1125void 1238void
1126rxvt_selection::x_cb (XEvent &xev) 1239rxvt_selection::x_cb (XEvent &xev)
1127{ 1240{
1134 handle_selection (xev.xproperty.window, xev.xproperty.atom, true); 1247 handle_selection (xev.xproperty.window, xev.xproperty.atom, true);
1135 break; 1248 break;
1136 1249
1137 case SelectionNotify: 1250 case SelectionNotify:
1138 if (selection_wait == Sel_normal 1251 if (selection_wait == Sel_normal
1139 && xev.xselection.time == request_time 1252 && xev.xselection.time == request_time)
1140 && xev.xselection.property == request_prop)
1141 { 1253 {
1142 timer_ev.stop (); 1254 timer_ev.stop ();
1143 handle_selection (xev.xselection.requestor, xev.xselection.property, true); 1255 handle_selection (xev.xselection.requestor, xev.xselection.property, true);
1144 } 1256 }
1145 break; 1257 break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines