From f86c044e3e42669154e3aad79a3b0a1295128328 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 9 Apr 2021 20:02:37 +0200 Subject: Add LIBTERMINPUT_SEPARATE_BACKTAB and LIBTERMINPUT_ESC_ON_BLOCK MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- interactive-test.c | 9 +++++++++ libterminput.c | 21 ++++++++++++++++++--- libterminput.h | 23 +++++++++++++++++------ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/interactive-test.c b/interactive-test.c index e38b638..57bb20c 100644 --- a/interactive-test.c +++ b/interactive-test.c @@ -34,6 +34,14 @@ main(void) fprintf(stderr, "LIBTERMINPUT_INS_ON_CSI_AT set\n"); libterminput_set_flags(&ctx, LIBTERMINPUT_INS_ON_CSI_AT); } + if (getenv("TEST_LIBTERMINPUT_SEPARATE_BACKTAB")) { + fprintf(stderr, "LIBTERMINPUT_SEPARATE_BACKTAB set\n"); + libterminput_set_flags(&ctx, LIBTERMINPUT_SEPARATE_BACKTAB); + } + if (getenv("TEST_LIBTERMINPUT_ESC_ON_BLOCK")) { + fprintf(stderr, "LIBTERMINPUT_ESC_ON_BLOCK set\n"); + libterminput_set_flags(&ctx, LIBTERMINPUT_ESC_ON_BLOCK); + } if (tcgetattr(STDERR_FILENO, &stty)) { perror("tcgetattr STDERR_FILENO"); @@ -61,6 +69,7 @@ main(void) case LIBTERMINPUT_LEFT: printf("\t%s: %s\n", "key", "left"); break; case LIBTERMINPUT_BEGIN: printf("\t%s: %s\n", "key", "begin"); break; case LIBTERMINPUT_TAB: printf("\t%s: %s\n", "key", "tab"); break; + case LIBTERMINPUT_BACKTAB: printf("\t%s: %s\n", "key", "backtab"); break; case LIBTERMINPUT_F1: printf("\t%s: %s\n", "key", "f1"); break; case LIBTERMINPUT_F2: printf("\t%s: %s\n", "key", "f2"); break; case LIBTERMINPUT_F3: printf("\t%s: %s\n", "key", "f3"); break; diff --git a/libterminput.c b/libterminput.c index 95a4e03..7ca36de 100644 --- a/libterminput.c +++ b/libterminput.c @@ -63,7 +63,14 @@ again: } } } else if (c == 033 && !*ctx->key) { - /* ESC at the beginning, save as a Meta/ESC */ + /* ESC at the beginning, save as a Meta/ESC (for default behaviour) */ + if ((ctx->flags & LIBTERMINPUT_ESC_ON_BLOCK) && ctx->stored_tail == ctx->stored_head) { + input->symbol[0] = (char)c; + input->symbol[1] = '\0'; + input->mods = ctx->mods; + ctx->mods = 0; + return 1; + } ctx->meta += 1; } else if (c == 0) { /* CTRL on Space */ @@ -430,8 +437,12 @@ parse_sequence(union libterminput_input *input, struct libterminput_state *ctx) case 'U': input->keypress.key = LIBTERMINPUT_NEXT; break; case 'V': input->keypress.key = LIBTERMINPUT_PRIOR; break; case 'Z': - input->keypress.key = LIBTERMINPUT_TAB; - input->keypress.mods |= LIBTERMINPUT_SHIFT; + if (!(ctx->flags & LIBTERMINPUT_SEPARATE_BACKTAB)) { + input->keypress.key = LIBTERMINPUT_TAB; + input->keypress.mods |= LIBTERMINPUT_SHIFT; + } else { + input->keypress.key = LIBTERMINPUT_BACKTAB; + } break; case 'a': input->keypress.key = LIBTERMINPUT_UP; @@ -847,6 +858,10 @@ again: input->keypress.key = LIBTERMINPUT_ENTER; input->keypress.symbol[0] = '\0'; break; + case 033: + input->keypress.key = LIBTERMINPUT_ESC; + input->keypress.symbol[0] = '\0'; + break; default: input->keypress.key = LIBTERMINPUT_SYMBOL; strcpy(input->keypress.symbol, ret.symbol); diff --git a/libterminput.h b/libterminput.h index 7e94564..8318dff 100644 --- a/libterminput.h +++ b/libterminput.h @@ -12,10 +12,20 @@ * terminal support this flag if set. */ enum libterminput_flags { - LIBTERMINPUT_DECSET_1005 = 0x0001, - LIBTERMINPUT_MACRO_ON_CSI_M = 0x0002, - LIBTERMINPUT_PAUSE_ON_CSI_P = 0x0004, - LIBTERMINPUT_INS_ON_CSI_AT = 0x0008 + LIBTERMINPUT_DECSET_1005 = 0x0001, + LIBTERMINPUT_MACRO_ON_CSI_M = 0x0002, + LIBTERMINPUT_PAUSE_ON_CSI_P = 0x0004, + LIBTERMINPUT_INS_ON_CSI_AT = 0x0008, + LIBTERMINPUT_SEPARATE_BACKTAB = 0x0010, + + /** + * If an ESC is received without anything after it, + * return ESC keypress. This is not always desirable + * behaviour as the use may manually press ESC to + * simulate a keypress that terminal does not support + * (yes, this is a real world issue). + */ + LIBTERMINPUT_ESC_ON_BLOCK = 0x0020 }; enum libterminput_mod { @@ -30,8 +40,9 @@ enum libterminput_key { LIBTERMINPUT_DOWN, LIBTERMINPUT_RIGHT, LIBTERMINPUT_LEFT, - LIBTERMINPUT_BEGIN, /* keypad 5 without numlock */ - LIBTERMINPUT_TAB, /* backtab if with shift */ + LIBTERMINPUT_BEGIN, /* keypad 5 without numlock */ + LIBTERMINPUT_TAB, /* backtab is interpreted as shift+tab by default */ + LIBTERMINPUT_BACKTAB, /* requires LIBTERMINPUT_SEPARATE_BACKTAB */ LIBTERMINPUT_F1, LIBTERMINPUT_F2, LIBTERMINPUT_F3, -- cgit v1.2.3-70-g09d2