Ticket #2956: mc-4.8.7-mouse-1006.patch

File mc-4.8.7-mouse-1006.patch, 13.5 KB (added by egmont, 11 years ago)

Replacing older 1015 extension with newer 1006

  • 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  
    521521 
    522522static int *pending_keys = NULL; 
    523523 
    524 static int mouse_btn, mouse_x, mouse_y; 
    525  
    526524#ifdef __QNXNTO__ 
    527525ph_dv_f ph_attach; 
    528526ph_ov_f ph_input_group; 
     
    711709/* --------------------------------------------------------------------------------------------- */ 
    712710 
    713711static void 
    714 xmouse_get_event (Gpm_Event * ev) 
     712xmouse_get_event (Gpm_Event * ev, gboolean extended) 
    715713{ 
    716714    static struct timeval tv1 = { 0, 0 };       /* Force first click as single */ 
    717715    static struct timeval tv2; 
    718716    static int clicks = 0; 
    719717    static int last_btn = 0; 
    720     int btn = mouse_btn; 
     718    int btn; 
    721719 
    722720    /* Decode Xterm mouse information to a GPM style event */ 
    723721 
     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 
    724759    /* There seems to be no way of knowing which button was released */ 
    725760    /* So we assume all the buttons were released */ 
    726761 
     
    798833        } 
    799834        last_btn = ev->buttons; 
    800835    } 
    801     ev->x = mouse_x; 
    802     ev->y = mouse_y; 
    803836} 
    804837 
    805838/* --------------------------------------------------------------------------------------------- */ 
     
    934967} 
    935968 
    936969/* --------------------------------------------------------------------------------------------- */ 
    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 for 
    940    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) is 
    942    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 mouse 
    949    in the rightmost columns. 
    950  
    951    * The old way of reporting mouse coordinates is the following: 
    952    + Output DECSET 1000 to enable mouse 
    953    + 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 converting 
    959    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 compatibility 
    970    with the previous standard. There is no way for an application to tell whether 
    971    the underlying terminal supports this new mode (whether DECSET 1005 did actually change 
    972    the behavior or not), but depending on this a completely different user action might 
    973    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 whether 
    980    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 without 
    982    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 used 
    987    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 other 
    989    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 extension 
    993    * rxvt-unicode >= 9.10 supports both extensions 
    994    * iterm2 supports both extensions 
    995    * vte >= 0.31 supports the urxvt extension 
    996  */ 
    997  
    998 static int 
    999 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 /* --------------------------------------------------------------------------------------------- */ 
    1067970/* Apply corrections for the keycode generated in get_key_code() */ 
    1068971 
    1069972static int 
     
    18311734  pend_send: 
    18321735    if (pending_keys != NULL) 
    18331736    { 
    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) 
    18381740        { 
    1839             pending_keys = seq_append = NULL; 
    1840             this = NULL; 
    1841             return MCKEY_EXTENDED_MOUSE; 
     1741            pending_keys = NULL; 
     1742            seq_append = NULL; 
    18421743        } 
    1843         if (m == -1) 
     1744        if ((d == ESC_CHAR) && (pending_keys != NULL)) 
    18441745        { 
    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; 
    18611748        } 
    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); 
    18631753    } 
    18641754 
    18651755  nodelay_try_again: 
     
    21692059                          || c == MCKEY_EXTENDED_MOUSE)) 
    21702060    { 
    21712061        /* 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); 
    21842063        return (event->type != 0) ? EV_MOUSE : EV_NONE; 
    21852064    } 
    21862065 
  • 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  
    4848Mouse_Type use_mouse_p = MOUSE_NONE; 
    4949gboolean mouse_enabled = FALSE; 
    5050const char *xmouse_seq; 
     51const char *xmouse_extended_seq; 
    5152 
    5253/*** file scope macro definitions ****************************************************************/ 
    5354 
     
    9091    case MOUSE_XTERM_NORMAL_TRACKING: 
    9192    case MOUSE_XTERM_BUTTON_EVENT_TRACKING: 
    9293        define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION); 
     94        define_sequence (MCKEY_EXTENDED_MOUSE, xmouse_extended_seq, MCKEY_NOACTION); 
    9395        break; 
    9496 
    9597    default: 
     
    138140        /* enable mouse tracking */ 
    139141        printf (ESC_STR "[?1000h"); 
    140142 
    141         /* enable urxvt extended mouse coordinate reporting */ 
    142         printf (ESC_STR "[?1015h"); 
     143        /* enable SGR extended mouse reporting */ 
     144        printf (ESC_STR "[?1006h"); 
    143145 
    144146        fflush (stdout); 
    145147        mouse_enabled = TRUE; 
     
    152154        /* enable mouse tracking */ 
    153155        printf (ESC_STR "[?1002h"); 
    154156 
    155         /* enable urxvt extended mouse coordinate reporting */ 
    156         printf (ESC_STR "[?1015h"); 
     157        /* enable SGR extended mouse reporting */ 
     158        printf (ESC_STR "[?1006h"); 
    157159 
    158160        fflush (stdout); 
    159161        mouse_enabled = TRUE; 
     
    182184        break; 
    183185#endif 
    184186    case MOUSE_XTERM_NORMAL_TRACKING: 
    185         /* disable urxvt extended mouse coordinate reporting */ 
    186         printf (ESC_STR "[?1015l"); 
     187        /* disable SGR extended mouse reporting */ 
     188        printf (ESC_STR "[?1006l"); 
    187189 
    188190        /* disable mouse tracking */ 
    189191        printf (ESC_STR "[?1000l"); 
     
    194196        fflush (stdout); 
    195197        break; 
    196198    case MOUSE_XTERM_BUTTON_EVENT_TRACKING: 
    197         /* disable urxvt extended mouse coordinate reporting */ 
    198         printf (ESC_STR "[?1015l"); 
     199        /* disable SGR extended mouse reporting */ 
     200        printf (ESC_STR "[?1006l"); 
    199201 
    200202        /* disable mouse tracking */ 
    201203        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  
    9797/* Type of the currently used mouse */ 
    9898extern Mouse_Type use_mouse_p; 
    9999 
    100 /* String indicating that a mouse event has occured, usually "\E[M" */ 
     100/* String indicating that a mouse event has occurred, usually "\E[M" */ 
    101101extern const char *xmouse_seq; 
    102102 
     103/* String indicating that an SGR extended mouse event has occurred, namely "\E[<" */ 
     104extern const char *xmouse_extended_seq; 
     105 
    103106/*** declarations of public functions ************************************************************/ 
    104107 
    105108/* 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  
    303303            } 
    304304        } 
    305305    } 
     306 
     307    /* No termcap for SGR extended mouse (yet), hardcode it for now */ 
     308    if (xmouse_seq != NULL) 
     309        xmouse_extended_seq = ESC_STR "[<"; 
    306310} 
    307311 
    308312/* --------------------------------------------------------------------------------------------- */