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. |
… | |
… | |
227 | { |
228 | { |
228 | } |
229 | } |
229 | |
230 | |
230 | rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) |
231 | rxvt_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; |
… | |
… | |
314 | /* |
312 | /* |
315 | * get resources using the X library function |
313 | * get resources using the X library function |
316 | */ |
314 | */ |
317 | char *displayResource, *xe; |
315 | char *displayResource, *xe; |
318 | XrmDatabase rdb1, database = 0; |
316 | XrmDatabase rdb1, database = 0; |
|
|
317 | |
|
|
318 | #if !XLIB_ILLEGAL_ACCESS |
|
|
319 | /* work around a bug in XrmSetDatabase where it frees the db, see ref_next */ |
|
|
320 | database = XrmGetStringDatabase (""); |
|
|
321 | #endif |
319 | |
322 | |
320 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
323 | // 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 |
324 | // as opposed to "standard practise", we always read in ~/.Xdefaults |
322 | |
325 | |
323 | // 6. System wide per application default file. |
326 | // 6. System wide per application default file. |
… | |
… | |
491 | rxvt_display::ref_next () |
494 | rxvt_display::ref_next () |
492 | { |
495 | { |
493 | // TODO: somehow check whether the database files/resources changed |
496 | // TODO: somehow check whether the database files/resources changed |
494 | // before affording re-loading/parsing |
497 | // before affording re-loading/parsing |
495 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
498 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
|
|
499 | #if XLIB_ILLEGAL_ACCESS |
|
|
500 | /* work around a bug in XrmSetDatabase where it frees the db */ |
|
|
501 | dpy->db = 0; |
|
|
502 | #endif |
496 | XrmSetDatabase (dpy, get_resources (true)); |
503 | XrmSetDatabase (dpy, get_resources (true)); |
497 | } |
504 | } |
498 | |
505 | |
499 | rxvt_display::~rxvt_display () |
506 | rxvt_display::~rxvt_display () |
500 | { |
507 | { |
… | |
… | |
507 | x_ev.stop (); |
514 | x_ev.stop (); |
508 | flush_ev.stop (); |
515 | flush_ev.stop (); |
509 | #ifdef USE_XIM |
516 | #ifdef USE_XIM |
510 | xims.clear (); |
517 | xims.clear (); |
511 | #endif |
518 | #endif |
|
|
519 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
512 | XCloseDisplay (dpy); |
520 | XCloseDisplay (dpy); |
513 | } |
521 | } |
514 | |
522 | |
515 | #ifdef USE_XIM |
523 | #ifdef USE_XIM |
516 | void rxvt_display::im_change_cb () |
524 | void rxvt_display::im_change_cb () |
… | |
… | |
884 | lerp (c.a, to.a, percent) |
892 | lerp (c.a, to.a, percent) |
885 | ) |
893 | ) |
886 | ); |
894 | ); |
887 | } |
895 | } |
888 | |
896 | |
|
|
897 | 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), term (term) |
|
|
899 | { |
|
|
900 | assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard); |
|
|
901 | |
|
|
902 | timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this); |
|
|
903 | timer_ev.repeat = 10.; |
|
|
904 | x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this); |
|
|
905 | |
|
|
906 | incr_buf = 0; |
|
|
907 | incr_buf_size = incr_buf_fill = 0; |
|
|
908 | selection_wait = Sel_normal; |
|
|
909 | selection_type = selnum; |
|
|
910 | } |
|
|
911 | |
|
|
912 | void |
|
|
913 | rxvt_selection::stop () |
|
|
914 | { |
|
|
915 | free (incr_buf); |
|
|
916 | incr_buf = 0; |
|
|
917 | timer_ev.stop (); |
|
|
918 | x_ev.stop (display); |
|
|
919 | } |
|
|
920 | |
|
|
921 | rxvt_selection::~rxvt_selection () |
|
|
922 | { |
|
|
923 | stop (); |
|
|
924 | } |
|
|
925 | |
|
|
926 | void |
|
|
927 | rxvt_selection::run () |
|
|
928 | { |
|
|
929 | int selnum = selection_type; |
|
|
930 | |
|
|
931 | #if ENABLE_FRILLS |
|
|
932 | if (selnum == Sel_Primary && display->selection_owner) |
|
|
933 | { |
|
|
934 | /* internal selection */ |
|
|
935 | char *str = rxvt_wcstombs (display->selection_owner->selection.text, display->selection_owner->selection.len); |
|
|
936 | finish (str, strlen (str)); |
|
|
937 | free (str); |
|
|
938 | return; |
|
|
939 | } |
|
|
940 | #endif |
|
|
941 | |
|
|
942 | #if X_HAVE_UTF8_STRING |
|
|
943 | selection_type = Sel_UTF8String; |
|
|
944 | if (request (display->xa[XA_UTF8_STRING], selnum)) |
|
|
945 | return; |
|
|
946 | #else |
|
|
947 | selection_type = Sel_CompoundText; |
|
|
948 | if (request (display->xa[XA_COMPOUND_TEXT], selnum)) |
|
|
949 | return; |
|
|
950 | #endif |
|
|
951 | |
|
|
952 | // fallback to CUT_BUFFER0 if the requested property has no owner |
|
|
953 | handle_selection (display->root, XA_CUT_BUFFER0, false); |
|
|
954 | } |
|
|
955 | |
|
|
956 | void |
|
|
957 | rxvt_selection::finish (char *data, unsigned int len) |
|
|
958 | { |
|
|
959 | if (term) |
|
|
960 | { |
|
|
961 | if (data) |
|
|
962 | term->paste (data, len); |
|
|
963 | |
|
|
964 | term->selection_req = 0; |
|
|
965 | delete this; |
|
|
966 | } |
|
|
967 | #if ENABLE_PERL |
|
|
968 | else |
|
|
969 | { |
|
|
970 | stop (); // we do not really trust perl callbacks |
|
|
971 | rxvt_perl.selection_finish (this, data, len); |
|
|
972 | } |
|
|
973 | #endif |
|
|
974 | } |
|
|
975 | |
|
|
976 | bool |
|
|
977 | rxvt_selection::request (Atom target, int selnum) |
|
|
978 | { |
|
|
979 | Atom sel; |
|
|
980 | |
|
|
981 | selection_type |= selnum; |
|
|
982 | |
|
|
983 | if (selnum == Sel_Primary) |
|
|
984 | sel = XA_PRIMARY; |
|
|
985 | else if (selnum == Sel_Secondary) |
|
|
986 | sel = XA_SECONDARY; |
|
|
987 | else |
|
|
988 | sel = display->xa[XA_CLIPBOARD]; |
|
|
989 | |
|
|
990 | if (XGetSelectionOwner (display->dpy, sel) != None) |
|
|
991 | { |
|
|
992 | XConvertSelection (display->dpy, sel, target, request_prop, |
|
|
993 | request_win, request_time); |
|
|
994 | x_ev.start (display, request_win); |
|
|
995 | timer_ev.again (); |
|
|
996 | return true; |
|
|
997 | } |
|
|
998 | |
|
|
999 | return false; |
|
|
1000 | } |
|
|
1001 | |
|
|
1002 | void |
|
|
1003 | rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop) |
|
|
1004 | { |
|
|
1005 | Display *dpy = display->dpy; |
|
|
1006 | char *data = 0; |
|
|
1007 | unsigned int data_len = 0; |
|
|
1008 | unsigned long bytes_after; |
|
|
1009 | XTextProperty ct; |
|
|
1010 | |
|
|
1011 | // check for failed XConvertSelection |
|
|
1012 | if (prop == None) |
|
|
1013 | { |
|
|
1014 | bool error = true; |
|
|
1015 | int selnum = selection_type & Sel_whereMask; |
|
|
1016 | |
|
|
1017 | if (selection_type & Sel_CompoundText) |
|
|
1018 | { |
|
|
1019 | selection_type = 0; |
|
|
1020 | error = !request (XA_STRING, selnum); |
|
|
1021 | } |
|
|
1022 | |
|
|
1023 | if (selection_type & Sel_UTF8String) |
|
|
1024 | { |
|
|
1025 | selection_type = Sel_CompoundText; |
|
|
1026 | error = !request (display->xa[XA_COMPOUND_TEXT], selnum); |
|
|
1027 | } |
|
|
1028 | |
|
|
1029 | if (error) |
|
|
1030 | { |
|
|
1031 | ct.value = 0; |
|
|
1032 | goto bailout; |
|
|
1033 | } |
|
|
1034 | |
|
|
1035 | return; |
|
|
1036 | } |
|
|
1037 | |
|
|
1038 | // length == (2^31 - 1) / 4, as gdk |
|
|
1039 | if (XGetWindowProperty (dpy, win, prop, |
|
|
1040 | 0, 0x1fffffff, |
|
|
1041 | delete_prop, AnyPropertyType, |
|
|
1042 | &ct.encoding, &ct.format, |
|
|
1043 | &ct.nitems, &bytes_after, |
|
|
1044 | &ct.value) != Success) |
|
|
1045 | { |
|
|
1046 | ct.value = 0; |
|
|
1047 | goto bailout; |
|
|
1048 | } |
|
|
1049 | |
|
|
1050 | if (ct.encoding == None) |
|
|
1051 | goto bailout; |
|
|
1052 | |
|
|
1053 | if (ct.value == 0) |
|
|
1054 | goto bailout; |
|
|
1055 | |
|
|
1056 | if (ct.encoding == display->xa[XA_INCR]) |
|
|
1057 | { |
|
|
1058 | // INCR selection, start handshake |
|
|
1059 | if (!delete_prop) |
|
|
1060 | XDeleteProperty (dpy, win, prop); |
|
|
1061 | |
|
|
1062 | selection_wait = Sel_incr; |
|
|
1063 | timer_ev.again (); |
|
|
1064 | |
|
|
1065 | goto bailout; |
|
|
1066 | } |
|
|
1067 | |
|
|
1068 | if (ct.nitems == 0) |
|
|
1069 | { |
|
|
1070 | if (selection_wait == Sel_incr) |
|
|
1071 | { |
|
|
1072 | XFree (ct.value); |
|
|
1073 | |
|
|
1074 | // finally complete, now paste the whole thing |
|
|
1075 | selection_wait = Sel_normal; |
|
|
1076 | ct.value = (unsigned char *)incr_buf; |
|
|
1077 | ct.nitems = incr_buf_fill; |
|
|
1078 | incr_buf = 0; |
|
|
1079 | timer_ev.stop (); |
|
|
1080 | } |
|
|
1081 | else |
|
|
1082 | { |
|
|
1083 | // avoid recursion |
|
|
1084 | if (win != display->root || prop != XA_CUT_BUFFER0) |
|
|
1085 | { |
|
|
1086 | XFree (ct.value); |
|
|
1087 | |
|
|
1088 | // fallback to CUT_BUFFER0 if the requested property |
|
|
1089 | // has an owner but is empty |
|
|
1090 | handle_selection (display->root, XA_CUT_BUFFER0, False); |
|
|
1091 | return; |
|
|
1092 | } |
|
|
1093 | |
|
|
1094 | goto bailout; |
|
|
1095 | } |
|
|
1096 | } |
|
|
1097 | else if (selection_wait == Sel_incr) |
|
|
1098 | { |
|
|
1099 | timer_ev.again (); |
|
|
1100 | |
|
|
1101 | while (incr_buf_fill + ct.nitems > incr_buf_size) |
|
|
1102 | { |
|
|
1103 | incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024; |
|
|
1104 | incr_buf = (char *)rxvt_realloc (incr_buf, incr_buf_size); |
|
|
1105 | } |
|
|
1106 | |
|
|
1107 | memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems); |
|
|
1108 | incr_buf_fill += ct.nitems; |
|
|
1109 | |
|
|
1110 | goto bailout; |
|
|
1111 | } |
|
|
1112 | |
|
|
1113 | char **cl; |
|
|
1114 | int cr; |
|
|
1115 | |
|
|
1116 | // we honour the first item only |
|
|
1117 | |
|
|
1118 | #if !ENABLE_MINIMAL |
|
|
1119 | // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it |
|
|
1120 | // so recode it manually |
|
|
1121 | if (ct.encoding == display->xa[XA_UTF8_STRING]) |
|
|
1122 | { |
|
|
1123 | wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems); |
|
|
1124 | data = rxvt_wcstombs (w); |
|
|
1125 | free (w); |
|
|
1126 | } |
|
|
1127 | else |
|
|
1128 | #endif |
|
|
1129 | if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0 |
|
|
1130 | && cl) |
|
|
1131 | { |
|
|
1132 | data = strdup (cl[0]); |
|
|
1133 | XFreeStringList (cl); |
|
|
1134 | } |
|
|
1135 | else |
|
|
1136 | { |
|
|
1137 | // paste raw |
|
|
1138 | data = strdup ((const char *)ct.value); |
|
|
1139 | } |
|
|
1140 | |
|
|
1141 | data_len = strlen (data); |
|
|
1142 | |
|
|
1143 | bailout: |
|
|
1144 | XFree (ct.value); |
|
|
1145 | |
|
|
1146 | if (selection_wait == Sel_normal) |
|
|
1147 | { |
|
|
1148 | finish (data, data_len); |
|
|
1149 | free (data); |
|
|
1150 | } |
|
|
1151 | } |
|
|
1152 | |
|
|
1153 | void |
|
|
1154 | rxvt_selection::timer_cb (ev::timer &w, int revents) |
|
|
1155 | { |
|
|
1156 | if (selection_wait == Sel_incr) |
|
|
1157 | rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n"); |
|
|
1158 | |
|
|
1159 | finish (); |
|
|
1160 | } |
|
|
1161 | |
|
|
1162 | void |
|
|
1163 | rxvt_selection::x_cb (XEvent &xev) |
|
|
1164 | { |
|
|
1165 | switch (xev.type) |
|
|
1166 | { |
|
|
1167 | case PropertyNotify: |
|
|
1168 | if (selection_wait == Sel_incr |
|
|
1169 | && xev.xproperty.atom == request_prop |
|
|
1170 | && xev.xproperty.state == PropertyNewValue) |
|
|
1171 | handle_selection (xev.xproperty.window, xev.xproperty.atom, true); |
|
|
1172 | break; |
|
|
1173 | |
|
|
1174 | case SelectionNotify: |
|
|
1175 | if (selection_wait == Sel_normal |
|
|
1176 | && xev.xselection.time == request_time |
|
|
1177 | && xev.xselection.property == request_prop) |
|
|
1178 | { |
|
|
1179 | timer_ev.stop (); |
|
|
1180 | handle_selection (xev.xselection.requestor, xev.xselection.property, true); |
|
|
1181 | } |
|
|
1182 | break; |
|
|
1183 | } |
|
|
1184 | } |