From 8b56df17036f0852791547e767bb15a2a98ff5cb Mon Sep 17 00:00:00 2001 From: akocis Date: Sat, 18 Jan 2020 06:51:50 +0100 Subject: [PATCH 1/4] Proper support of XTerm function/small keypad keys --- 0.73_My_PuTTY/putty.h | 5 +-- 0.73_My_PuTTY/terminal.c | 79 ++++++++++++++++++++++++++++++++-- 0.73_My_PuTTY/windows/window.c | 5 +-- 3 files changed, 80 insertions(+), 9 deletions(-) diff --git a/0.73_My_PuTTY/putty.h b/0.73_My_PuTTY/putty.h index 1b258bc..94adc58 100644 --- a/0.73_My_PuTTY/putty.h +++ b/0.73_My_PuTTY/putty.h @@ -1810,9 +1810,8 @@ int format_arrow_key(char *buf, Terminal *term, int xkey, int modifier, bool alt int format_arrow_key(char *buf, Terminal *term, int xkey, bool ctrl); #endif -int format_function_key(char *buf, Terminal *term, int key_number, - bool shift, bool ctrl); -int format_small_keypad_key(char *buf, Terminal *term, SmallKeypadKey key); +int format_function_key(char *buf, Terminal *term, int key_number, int modifier, bool alt); +int format_small_keypad_key(char *buf, Terminal *term, SmallKeypadKey key, int modifier, bool alt); int format_numeric_keypad_key(char *buf, Terminal *term, char key, bool shift, bool ctrl); diff --git a/0.73_My_PuTTY/terminal.c b/0.73_My_PuTTY/terminal.c index 8c2c346..f3c2ad1 100644 --- a/0.73_My_PuTTY/terminal.c +++ b/0.73_My_PuTTY/terminal.c @@ -7137,6 +7137,18 @@ void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked, term_update(term); } +int get_xterm_modifier(bool shift, bool ctrl, bool alt) +{ + int xterm_modifier = 1; // no modifier key was pressed + if (shift) + xterm_modifier += 1; + if (alt) + xterm_modifier += 2; + if (ctrl) + xterm_modifier += 4; + return xterm_modifier; +} + #ifdef MOD_KEYMAPPING int format_arrow_key(char *buf, Terminal *term, int xkey, int modifier, bool alt) #else @@ -7210,8 +7222,7 @@ int format_arrow_key(char *buf, Terminal *term, int xkey, bool ctrl) return p - buf; } -int format_function_key(char *buf, Terminal *term, int key_number, - bool shift, bool ctrl) +int format_function_key(char *buf, Terminal *term, int key_number, int modifier, bool alt) { char *p = buf; @@ -7224,6 +7235,37 @@ int format_function_key(char *buf, Terminal *term, int key_number, assert(key_number > 0); assert(key_number < lenof(key_number_to_tilde_code)); + bool shift = modifier & 1; + bool ctrl = modifier & 2; + if (term->funky_type == FUNKY_XTERM && !term->vt52_mode) + { + // XTerm mode + char prefix[20]; + char suffix[20]; + int xterm_modifier = get_xterm_modifier(shift, ctrl, alt); + if (xterm_modifier > 1) + { + sprintf(prefix, "[1;%d", xterm_modifier); + sprintf(suffix, ";%d", xterm_modifier); + } + else + { + sprintf(prefix, "O"); + sprintf(suffix, ""); + } + + int code = key_number_to_tilde_code[key_number]; + if (code >= 11 && code <= 14) + { + p += sprintf(p, "\x1B%s%c", prefix, code + 'P' - 11); + } + else + { + p += sprintf(p, "\x1B[%d%s~", code, suffix); + } + return p - buf; + } + int index = (shift && key_number <= 10) ? key_number + 10 : key_number; int code = key_number_to_tilde_code[index]; @@ -7260,7 +7302,7 @@ int format_function_key(char *buf, Terminal *term, int key_number, return p - buf; } -int format_small_keypad_key(char *buf, Terminal *term, SmallKeypadKey key) +int format_small_keypad_key(char *buf, Terminal *term, SmallKeypadKey key, int modifier, bool alt) { char *p = buf; @@ -7275,6 +7317,37 @@ int format_small_keypad_key(char *buf, Terminal *term, SmallKeypadKey key) default: unreachable("bad small keypad key enum value"); } + bool shift = modifier & 1; + bool ctrl = modifier & 2; + if (term->funky_type == FUNKY_XTERM && !term->vt52_mode) + { + // XTerm mode + char prefix[20]; + char suffix[20]; + int xterm_modifier = get_xterm_modifier(shift, ctrl, alt); + if (xterm_modifier > 1) + { + sprintf(prefix, "[1;%d", xterm_modifier); + sprintf(suffix, ";%d", xterm_modifier); + } + else + { + sprintf(prefix, "["); + sprintf(suffix, ""); + } + + if (code == 1 || code == 4) + { + p += sprintf(p, "\x1B%s%c", prefix, code == 1 ? 'H' : 'F'); + } + else + { + p += sprintf(p, "\x1B[%d%s~", code, suffix); + } + + return p - buf; + } + /* Reorder edit keys to physical order */ if (term->funky_type == FUNKY_VT400 && code <= 6) code = "\0\2\1\4\5\3\6"[code]; diff --git a/0.73_My_PuTTY/windows/window.c b/0.73_My_PuTTY/windows/window.c index 9a0871c..8b7de99 100644 --- a/0.73_My_PuTTY/windows/window.c +++ b/0.73_My_PuTTY/windows/window.c @@ -6801,8 +6801,7 @@ if( !get_param("PUTTY") && conf_get_int(conf, CONF_disablealtgr) ) { case VK_F19: fkey_number = 19; goto numbered_function_key; case VK_F20: fkey_number = 20; goto numbered_function_key; numbered_function_key: - p += format_function_key((char *)p, term, fkey_number, - shift_state & 1, shift_state & 2); + p += format_function_key((char *)p, term, fkey_number, shift_state, left_alt); return p - output; SmallKeypadKey sk_key; @@ -6817,7 +6816,7 @@ if( !get_param("PUTTY") && conf_get_int(conf, CONF_disablealtgr) ) { if (shift_state & 2) break; - p += format_small_keypad_key((char *)p, term, sk_key); + p += format_small_keypad_key((char *)p, term, sk_key, shift_state, left_alt); return p - output; char xkey; From 82d45e9f09721cc2ea933102ba9cf9a809294b4d Mon Sep 17 00:00:00 2001 From: Adrian Kocis Date: Sat, 18 Jan 2020 07:08:43 +0000 Subject: [PATCH 2/4] Do not send extra escape when alt is pressed together with function or small keypad keys (i.e. Fx or ins/del/home/end/pgup/pgdown keys) --- 0.73_My_PuTTY/windows/window.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/0.73_My_PuTTY/windows/window.c b/0.73_My_PuTTY/windows/window.c index 8b7de99..4e9c193 100644 --- a/0.73_My_PuTTY/windows/window.c +++ b/0.73_My_PuTTY/windows/window.c @@ -6545,9 +6545,38 @@ if( !get_param("PUTTY") && conf_get_int(conf, CONF_disablealtgr) ) { /* Okay, prepare for most alts then ... */ #ifdef MOD_KEYMAPPING if( !GetPuttyFlag() ) { - if (left_alt && shift_state != 1 && !(wParam == VK_UP || wParam == VK_DOWN || wParam == VK_RIGHT || wParam == VK_LEFT)) - *p++ = '\033'; + if (left_alt && shift_state != 1 && !(wParam == VK_UP || wParam == VK_DOWN || wParam == VK_RIGHT || wParam == VK_LEFT)) { + int fkey = 0; + switch (wParam) { + case VK_F1: + case VK_F2: + case VK_F3: + case VK_F4: + case VK_F5: + case VK_F6: + case VK_F7: + case VK_F8: + case VK_F9: + case VK_F10: + case VK_F11: + case VK_F12: + case VK_INSERT: + case VK_DELETE: + case VK_HOME: + case VK_END: + case VK_PRIOR: + case VK_NEXT: + fkey = 1; + break; + default: + break; + } + + if (!(term->funky_type == FUNKY_XTERM && !term->vt52_mode) || fkey == 0) { + *p++ = '\033'; + } } + } else #endif if (left_alt) From 1db1dce45e998d8a6486866cad03c093b36c182a Mon Sep 17 00:00:00 2001 From: Adrian Kocis Date: Sat, 18 Jan 2020 07:20:09 +0000 Subject: [PATCH 3/4] Allow sending of ctrl+small keypad keys --- 0.73_My_PuTTY/windows/window.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/0.73_My_PuTTY/windows/window.c b/0.73_My_PuTTY/windows/window.c index 4e9c193..f0cad4f 100644 --- a/0.73_My_PuTTY/windows/window.c +++ b/0.73_My_PuTTY/windows/window.c @@ -6841,10 +6841,6 @@ if( !get_param("PUTTY") && conf_get_int(conf, CONF_disablealtgr) ) { case VK_PRIOR: sk_key = SKK_PGUP; goto small_keypad_key; case VK_NEXT: sk_key = SKK_PGDN; goto small_keypad_key; small_keypad_key: - /* These keys don't generate terminal input with Ctrl */ - if (shift_state & 2) - break; - p += format_small_keypad_key((char *)p, term, sk_key, shift_state, left_alt); return p - output; From 33cb109afacc4a0e1dab7c6053edf799507891f0 Mon Sep 17 00:00:00 2001 From: Adrian Kocis Date: Sat, 18 Jan 2020 07:23:21 +0000 Subject: [PATCH 4/4] Align backspace/ctrl+backspace toggling with XTerm (instead of backspace/shift+backspace) --- 0.73_My_PuTTY/windows/window.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/0.73_My_PuTTY/windows/window.c b/0.73_My_PuTTY/windows/window.c index f0cad4f..f5008eb 100644 --- a/0.73_My_PuTTY/windows/window.c +++ b/0.73_My_PuTTY/windows/window.c @@ -6683,12 +6683,12 @@ if( !get_param("PUTTY") && conf_get_int(conf, CONF_disablealtgr) ) { return 0; } - if (wParam == VK_BACK && shift_state == 0) { /* Backspace */ + if (wParam == VK_BACK && shift_state <= 1) { /* Backspace of Shift Backspace */ *p++ = (conf_get_bool(conf, CONF_bksp_is_delete) ? 0x7F : 0x08); *p++ = 0; return -2; } - if (wParam == VK_BACK && shift_state == 1) { /* Shift Backspace */ + if (wParam == VK_BACK) { /* Ctrl Backspace or Ctrl+Shift Backspace */ /* We do the opposite of what is configured */ *p++ = (conf_get_bool(conf, CONF_bksp_is_delete) ? 0x08 : 0x7F); *p++ = 0;