Ticket #2956: mc-4.8.7-mouse-1006.patch
File mc-4.8.7-mouse-1006.patch, 13.5 KB (added by egmont, 12 years ago) |
---|
-
lib/tty/key.c
diff -ur mc-4.8.7.orig/lib/tty/key.c mc-4.8.7/lib/tty/key.c
old new 521 521 522 522 static int *pending_keys = NULL; 523 523 524 static int mouse_btn, mouse_x, mouse_y;525 526 524 #ifdef __QNXNTO__ 527 525 ph_dv_f ph_attach; 528 526 ph_ov_f ph_input_group; … … 711 709 /* --------------------------------------------------------------------------------------------- */ 712 710 713 711 static void 714 xmouse_get_event (Gpm_Event * ev )712 xmouse_get_event (Gpm_Event * ev, gboolean extended) 715 713 { 716 714 static struct timeval tv1 = { 0, 0 }; /* Force first click as single */ 717 715 static struct timeval tv2; 718 716 static int clicks = 0; 719 717 static int last_btn = 0; 720 int btn = mouse_btn;718 int btn; 721 719 722 720 /* Decode Xterm mouse information to a GPM style event */ 723 721 722 if (!extended) { 723 /* Variable btn has following meaning: */ 724 /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */ 725 btn = tty_lowlevel_getch () - 32; 726 /* Coordinates are 33-based */ 727 /* Transform them to 1-based */ 728 ev->x = tty_lowlevel_getch () - 32; 729 ev->y = tty_lowlevel_getch () - 32; 730 } else { 731 /* SGR 1006 extension (e.g. "\e[<0;12;300M"): 732 - Numbers are encoded in decimal to make it ASCII-safe 733 and to overcome the limit of 223 columns/rows. 734 - Mouse release is encoded by trailing 'm' rather than 'M' 735 so that the released button can be reported. 736 - Numbers are no longer offset by 32. */ 737 char c; 738 btn = ev->x = ev->y = 0; 739 ev->type = 0; /* In case we return on an invalid sequence */ 740 while ((c = tty_lowlevel_getch()) != ';') { 741 if (c < '0' || c > '9') return; 742 btn = 10 * btn + (c - '0'); 743 } 744 while ((c = tty_lowlevel_getch()) != ';') { 745 if (c < '0' || c > '9') return; 746 ev->x = 10 * ev->x + (c - '0'); 747 } 748 while ((c = tty_lowlevel_getch()) != 'M' && c != 'm') { 749 if (c < '0' || c > '9') return; 750 ev->y = 10 * ev->y + (c - '0'); 751 } 752 /* Legacy mouse protocol doesn't tell which button was released, 753 conveniently all of mc's widgets are written not to rely on this 754 information. With the SGR extension the released button becomes 755 known, but for the sake of simplicity we just ignore it. */ 756 if (c == 'm') btn = 3; 757 } 758 724 759 /* There seems to be no way of knowing which button was released */ 725 760 /* So we assume all the buttons were released */ 726 761 … … 798 833 } 799 834 last_btn = ev->buttons; 800 835 } 801 ev->x = mouse_x;802 ev->y = mouse_y;803 836 } 804 837 805 838 /* --------------------------------------------------------------------------------------------- */ … … 934 967 } 935 968 936 969 /* --------------------------------------------------------------------------------------------- */ 937 /* Parse extended mouse coordinates.938 Returns -1 if pending_keys (up to seq_append) cannot be a prefix of extended mouse coordinates.939 Returns 0 if pending_keys (up to seq_append) is a valid (but still incomplete) prefix for940 extended mouse coordinates, e.g. "^[[32;4".941 Returns 1 and fills the mouse_btn, mouse_x, mouse_y values if pending_keys (up to seq_append) is942 a complete extended mouse sequence, e.g. "^[[32;42;5M"943 */944 945 /* Technical info (Egmont Koblinger <egmont@gmail.com>):946 947 The ancient way of reporting mouse coordinates only supports coordinates up to 223,948 so if your terminal is wider (or taller, but that's unlikely), you cannot use your mouse949 in the rightmost columns.950 951 * The old way of reporting mouse coordinates is the following:952 + Output DECSET 1000 to enable mouse953 + Expect escape sequences in the format \e[M<action+32><x+32><y+32> whereas <action+32>,954 <x+32> and <y+32> are single bytes. (Action is 0 for left click, 1 for middle click,955 2 for right click, 3 for release, or something like this.)956 + Disadvantages of this format:957 + x and y can only go up to 223.958 + Coordinates above 95 are not ascii-compatible, so any character set converting959 layer (e.g. luit) messes them up.960 + The stream is not valid UTF-8, even if everything else is.961 962 * The first new extension, introduced by xterm-262, is the following:963 + Output DECSET 1000 to enable mouse, followed by DECSET 1005 to activate extended mode.964 + Expect escape sequences in the format \e[M<action+32><<x+32>><<y+32>> whereas <<x+32>>965 and <<y+32>> each can be up to two bytes long: coordinate+32 is encoded in UTF-8.966 + Disadvantates of this format:967 + There's still a limit of 2015 rows/columns (okay, it's not a real life problem).968 + Doesn't solve the luit issue.969 + It is "horribly broken" (quoting urxvt's changelog) in terms of compatibility970 with the previous standard. There is no way for an application to tell whether971 the underlying terminal supports this new mode (whether DECSET 1005 did actually change972 the behavior or not), but depending on this a completely different user action might973 generate the same input. Example:974 + If the terminal doesn't support this extension, then clicking at (162, 129)975 generates \e[M<32><194><161>.976 + If the terminal supports this extension, then clicking at (129, 1) [bit of math:977 129+32 = 161, U+0161 in UTF-8 is 194 161] generates \e[M<32><194><161><33>.978 + so there's no way to tell whether the terminal ignored the 1005 escape sequence,979 the user clicked on (162, 129) and then typed an exclamation mark; or whether980 the terminal recognized the escape, and the user clicked on (129, 1).981 + Due to this horrible brokenness, there's no way to implement support it without982 explicitly asking the user (via a setting) if the terminal can speak this extension.983 984 * The second new extension, introduced by rxvt-unicode-9.10, is the following:985 + Output DECSET 1000 to enable mouse, followed by DECSET 1015 to activate this extended mode.986 + Expect escape sequences in the format \e[{action+32};{x};{y}M where this time I used987 the braces to denote spelling out the numbers in decimal, rather than using raw bytes.988 + The only thing I don't understand is why they kept the offset of 32 at action, but other989 than that, this format is totally okay, and solves all the weaknesses of the previous ones.990 991 Currently, at least the following terminal emulators have support for these:992 * xterm supports the xterm extension993 * rxvt-unicode >= 9.10 supports both extensions994 * iterm2 supports both extensions995 * vte >= 0.31 supports the urxvt extension996 */997 998 static int999 parse_extended_mouse_coordinates (void)1000 {1001 int c, btn = 0, x = 0, y = 0;1002 const int *p = pending_keys;1003 const int *endp = seq_append;1004 1005 if (p == endp)1006 return 0;1007 c = *p++;1008 if (c != ESC_CHAR)1009 return -1;1010 1011 if (p == endp)1012 return 0;1013 c = *p++;1014 if (c != '[')1015 return -1;1016 1017 while (TRUE)1018 {1019 if (p == endp)1020 return 0;1021 c = *p++;1022 if (c == ';')1023 break;1024 if (c < '0' || c > '9')1025 return -1;1026 btn = 10 * btn + c - '0';1027 }1028 if (btn < 32)1029 return -1;1030 btn -= 32;1031 1032 while (TRUE)1033 {1034 if (p == endp)1035 return 0;1036 c = *p++;1037 if (c == ';')1038 break;1039 if (c < '0' || c > '9')1040 return -1;1041 x = 10 * x + c - '0';1042 }1043 if (x < 1)1044 return -1;1045 1046 while (TRUE)1047 {1048 if (p == endp)1049 return 0;1050 c = *p++;1051 if (c == 'M')1052 break;1053 if (c < '0' || c > '9')1054 return -1;1055 y = 10 * y + c - '0';1056 }1057 if (y < 1)1058 return -1;1059 1060 mouse_btn = btn;1061 mouse_x = x;1062 mouse_y = y;1063 return 1;1064 }1065 1066 /* --------------------------------------------------------------------------------------------- */1067 970 /* Apply corrections for the keycode generated in get_key_code() */ 1068 971 1069 972 static int … … 1831 1734 pend_send: 1832 1735 if (pending_keys != NULL) 1833 1736 { 1834 int m; 1835 1836 m = parse_extended_mouse_coordinates (); 1837 if (m == 1) 1737 int d = *pending_keys++; 1738 check_pend: 1739 if (*pending_keys == 0) 1838 1740 { 1839 pending_keys = seq_append = NULL; 1840 this = NULL; 1841 return MCKEY_EXTENDED_MOUSE; 1741 pending_keys = NULL; 1742 seq_append = NULL; 1842 1743 } 1843 if ( m == -1)1744 if ((d == ESC_CHAR) && (pending_keys != NULL)) 1844 1745 { 1845 int d = *pending_keys++; 1846 check_pend: 1847 if (*pending_keys == 0) 1848 { 1849 pending_keys = NULL; 1850 seq_append = NULL; 1851 } 1852 if ((d == ESC_CHAR) && (pending_keys != NULL)) 1853 { 1854 d = ALT (*pending_keys++); 1855 goto check_pend; 1856 } 1857 if ((d > 127 && d < 256) && use_8th_bit_as_meta) 1858 d = ALT (d & 0x7f); 1859 this = NULL; 1860 return correct_key_code (d); 1746 d = ALT (*pending_keys++); 1747 goto check_pend; 1861 1748 } 1862 /* else if (m == 0), just let it continue */ 1749 if ((d > 127 && d < 256) && use_8th_bit_as_meta) 1750 d = ALT (d & 0x7f); 1751 this = NULL; 1752 return correct_key_code (d); 1863 1753 } 1864 1754 1865 1755 nodelay_try_again: … … 2169 2059 || c == MCKEY_EXTENDED_MOUSE)) 2170 2060 { 2171 2061 /* Mouse event */ 2172 /* In case of extended coordinates, mouse_btn, mouse_x and mouse_y are already filled in. */ 2173 if (c != MCKEY_EXTENDED_MOUSE) 2174 { 2175 /* Variable btn has following meaning: */ 2176 /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */ 2177 mouse_btn = tty_lowlevel_getch () - 32; 2178 /* Coordinates are 33-based */ 2179 /* Transform them to 1-based */ 2180 mouse_x = tty_lowlevel_getch () - 32; 2181 mouse_y = tty_lowlevel_getch () - 32; 2182 } 2183 xmouse_get_event (event); 2062 xmouse_get_event (event, c == MCKEY_EXTENDED_MOUSE); 2184 2063 return (event->type != 0) ? EV_MOUSE : EV_NONE; 2185 2064 } 2186 2065 -
lib/tty/mouse.c
diff -ur mc-4.8.7.orig/lib/tty/mouse.c mc-4.8.7/lib/tty/mouse.c
old new 48 48 Mouse_Type use_mouse_p = MOUSE_NONE; 49 49 gboolean mouse_enabled = FALSE; 50 50 const char *xmouse_seq; 51 const char *xmouse_extended_seq; 51 52 52 53 /*** file scope macro definitions ****************************************************************/ 53 54 … … 90 91 case MOUSE_XTERM_NORMAL_TRACKING: 91 92 case MOUSE_XTERM_BUTTON_EVENT_TRACKING: 92 93 define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION); 94 define_sequence (MCKEY_EXTENDED_MOUSE, xmouse_extended_seq, MCKEY_NOACTION); 93 95 break; 94 96 95 97 default: … … 138 140 /* enable mouse tracking */ 139 141 printf (ESC_STR "[?1000h"); 140 142 141 /* enable urxvt extended mouse coordinate reporting */142 printf (ESC_STR "[?10 15h");143 /* enable SGR extended mouse reporting */ 144 printf (ESC_STR "[?1006h"); 143 145 144 146 fflush (stdout); 145 147 mouse_enabled = TRUE; … … 152 154 /* enable mouse tracking */ 153 155 printf (ESC_STR "[?1002h"); 154 156 155 /* enable urxvt extended mouse coordinate reporting */156 printf (ESC_STR "[?10 15h");157 /* enable SGR extended mouse reporting */ 158 printf (ESC_STR "[?1006h"); 157 159 158 160 fflush (stdout); 159 161 mouse_enabled = TRUE; … … 182 184 break; 183 185 #endif 184 186 case MOUSE_XTERM_NORMAL_TRACKING: 185 /* disable urxvt extended mouse coordinate reporting */186 printf (ESC_STR "[?10 15l");187 /* disable SGR extended mouse reporting */ 188 printf (ESC_STR "[?1006l"); 187 189 188 190 /* disable mouse tracking */ 189 191 printf (ESC_STR "[?1000l"); … … 194 196 fflush (stdout); 195 197 break; 196 198 case MOUSE_XTERM_BUTTON_EVENT_TRACKING: 197 /* disable urxvt extended mouse coordinate reporting */198 printf (ESC_STR "[?10 15l");199 /* disable SGR extended mouse reporting */ 200 printf (ESC_STR "[?1006l"); 199 201 200 202 /* disable mouse tracking */ 201 203 printf (ESC_STR "[?1002l"); -
lib/tty/mouse.h
diff -ur mc-4.8.7.orig/lib/tty/mouse.h mc-4.8.7/lib/tty/mouse.h
old new 97 97 /* Type of the currently used mouse */ 98 98 extern Mouse_Type use_mouse_p; 99 99 100 /* String indicating that a mouse event has occur ed, usually "\E[M" */100 /* String indicating that a mouse event has occurred, usually "\E[M" */ 101 101 extern const char *xmouse_seq; 102 102 103 /* String indicating that an SGR extended mouse event has occurred, namely "\E[<" */ 104 extern const char *xmouse_extended_seq; 105 103 106 /*** declarations of public functions ************************************************************/ 104 107 105 108 /* General (i.e. both for xterm and gpm) mouse support definitions */ -
lib/tty/tty.c
diff -ur mc-4.8.7.orig/lib/tty/tty.c mc-4.8.7/lib/tty/tty.c
old new 303 303 } 304 304 } 305 305 } 306 307 /* No termcap for SGR extended mouse (yet), hardcode it for now */ 308 if (xmouse_seq != NULL) 309 xmouse_extended_seq = ESC_STR "[<"; 306 310 } 307 311 308 312 /* --------------------------------------------------------------------------------------------- */