diff options
author | Mattias Andrée <m@maandree.se> | 2025-02-19 21:24:33 +0100 |
---|---|---|
committer | Mattias Andrée <m@maandree.se> | 2025-02-19 21:24:33 +0100 |
commit | a9498262cb523429fe0e7ce79e97d725d482b1a0 (patch) | |
tree | 8af04618ec478f691a6c58e04c0d2a754995e50b | |
parent | misc cleanup, fixes, and clarifications, and escape sequence (diff) | |
download | libterminput-a9498262cb523429fe0e7ce79e97d725d482b1a0.tar.gz libterminput-a9498262cb523429fe0e7ce79e97d725d482b1a0.tar.bz2 libterminput-a9498262cb523429fe0e7ce79e97d725d482b1a0.tar.xz |
Add LIBTERMINPUT_MACRO_ON_BLOCK
Signed-off-by: Mattias Andrée <m@maandree.se>
-rw-r--r-- | libterminput.h | 13 | ||||
-rw-r--r-- | libterminput_is_ready.3 | 6 | ||||
-rw-r--r-- | libterminput_read.c | 13 | ||||
-rw-r--r-- | libterminput_set_flags.3 | 9 | ||||
-rw-r--r-- | test.c | 10 |
5 files changed, 44 insertions, 7 deletions
diff --git a/libterminput.h b/libterminput.h index b0c55af..46d47e2 100644 --- a/libterminput.h +++ b/libterminput.h @@ -71,7 +71,18 @@ enum libterminput_flags { * This is required for distinguishing cursor position * reports from F3 key presses */ - LIBTERMINPUT_AWAITING_CURSOR_POSITION = 0x0040 + LIBTERMINPUT_AWAITING_CURSOR_POSITION = 0x0040, + + /** + * If CSI M is received without anything after it, + * return Macro keypress. Since the user probably + * does not have the Macro key, it seems having this + * as the default behaviour introduces an unncessary + * risk of misparsing input. However, if mouse tracking + * is not activated, it makes since to enable this + * flag. + */ + LIBTERMINPUT_MACRO_ON_BLOCK = 0x0080 }; /** diff --git a/libterminput_is_ready.3 b/libterminput_is_ready.3 index 27b98f6..c624008 100644 --- a/libterminput_is_ready.3 +++ b/libterminput_is_ready.3 @@ -30,9 +30,11 @@ The function should only be used if using blocking read operations (terminal file descriptor configured with .I O_NONBLOCK -cleared). The flag +cleared). The flags .I LIBTERMINPUT_ESC_ON_BLOCK -is only meaningful for non-blocking reads. With non-blocking +and +.I LIBTERMINPUT_MACRO_ON_BLOCK +are only meaningful for non-blocking reads. With non-blocking reads, the application should call .BR libterminput_read (3) and check for diff --git a/libterminput_read.c b/libterminput_read.c index 0ca3c36..42bb710 100644 --- a/libterminput_read.c +++ b/libterminput_read.c @@ -41,6 +41,7 @@ libterminput_read(int fd, union libterminput_input *input, struct libterminput_s input->type = LIBTERMINPUT_KEYPRESS; ctx->mods = 0; ctx->meta = 0; + ctx->key[0] = '\0'; return 1; } ctx->blocked = 0; @@ -73,7 +74,6 @@ again: if (ctx->meta > 1) input->keypress.mods |= LIBTERMINPUT_META; ctx->queued = 1; - goto none; } goto none; } @@ -108,7 +108,16 @@ again: goto none; } else if (ctx->key[0] == '[' && ctx->key[1] == 'M' && (ctx->flags & LIBTERMINPUT_MACRO_ON_CSI_M)) { /* complete */ - /* TODO optionally this should also be the case if blocked */ + } else if (ctx->key[0] == '[' && ctx->key[1] == 'M' && (ctx->flags & LIBTERMINPUT_MACRO_ON_BLOCK) && + ctx->stored_head - ctx->stored_tail == 0) { + input->keypress.key = LIBTERMINPUT_MACRO; + input->keypress.times = 1; + input->keypress.mods = ctx->mods; + input->keypress.symbol[0] = '\0'; + if (ctx->meta > 1) + input->keypress.mods |= LIBTERMINPUT_META; + ctx->queued = 1; + goto none; } else if (ctx->key[0] == '[' && ctx->key[1] == 'M' && (ctx->flags & LIBTERMINPUT_DECSET_1005)) { ctx->mouse_tracking = 1; n = ctx->stored_tail; diff --git a/libterminput_set_flags.3 b/libterminput_set_flags.3 index af1908a..b55c231 100644 --- a/libterminput_set_flags.3 +++ b/libterminput_set_flags.3 @@ -88,6 +88,15 @@ The sequence .BI "CSI " Ps " ; " Rs " R" shall be parsed as a cursor position report rather than as an F3 key press. +.TP +.B LIBTERMINPUT_MACRO_ON_BLOCK +If CSI M is received without anything after it, +return Macro keypress. Since the user probably +does not have the Macro key, it seems having this +as the default behaviour introduces an unncessary +risk of misparsing input. However, if mouse tracking +is not activated, it makes since to enable this +flag. .PP .I ctx must have been zero-initialised, e.g. with @@ -621,11 +621,17 @@ main(void) KEYPRESS_SPECIAL_CHAR('\b', LIBTERMINPUT_ERASE); KEYPRESS_SPECIAL_CHAR('\t', LIBTERMINPUT_TAB); KEYPRESS_SPECIAL_CHAR('\n', LIBTERMINPUT_ENTER); - libterminput_set_flags(&ctx, LIBTERMINPUT_ESC_ON_BLOCK); TEST(fcntl(fds[0], F_SETFL, flags | O_NONBLOCK) >= 0); + libterminput_set_flags(&ctx, LIBTERMINPUT_ESC_ON_BLOCK); KEYPRESS_SPECIAL_CHAR('\033', LIBTERMINPUT_ESC); - TEST(fcntl(fds[0], F_SETFL, flags) >= 0); libterminput_clear_flags(&ctx, LIBTERMINPUT_ESC_ON_BLOCK); + libterminput_set_flags(&ctx, LIBTERMINPUT_MACRO_ON_BLOCK); + TYPE("\033[M", LIBTERMINPUT_KEYPRESS); + TEST(input.keypress.key == LIBTERMINPUT_MACRO); + TEST(input.keypress.mods == 0); + TEST(input.keypress.times == 1); + libterminput_clear_flags(&ctx, LIBTERMINPUT_MACRO_ON_BLOCK); + TEST(fcntl(fds[0], F_SETFL, flags) >= 0); TYPE("text", LIBTERMINPUT_KEYPRESS); TEST(input.keypress.key == LIBTERMINPUT_SYMBOL); |