diff options
Diffstat (limited to '')
-rw-r--r-- | Makefile | 13 | ||||
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | libterminput.c | 45 | ||||
-rw-r--r-- | test.c | 262 |
4 files changed, 297 insertions, 24 deletions
@@ -24,10 +24,11 @@ HDR =\ OBJ =\ interactive-test.o\ + test.o\ $(LOBJ:.lo=.o) -all: libterminput.a libterminput.$(LIBEXT) interactive-test +all: libterminput.a libterminput.$(LIBEXT) interactive-test test $(OBJ): $(@:.o=.c) $(HDR) $(LOBJ): $(@:.lo=.c) $(HDR) @@ -48,6 +49,12 @@ $(LOBJ): $(@:.lo=.c) $(HDR) interactive-test: interactive-test.o libterminput.a $(CC) -o $@ interactive-test.o libterminput.a $(LDFLAGS) +test: test.o libterminput.a + $(CC) -o $@ test.o libterminput.a $(LDFLAGS) + +check: test + ./test + install: libterminput.a libterminput.$(LIBEXT) mkdir -p -- "$(DESTDIR)$(PREFIX)/lib" mkdir -p -- "$(DESTDIR)$(PREFIX)/include" @@ -75,9 +82,9 @@ uninstall: -rm -f -- "$(DESTDIR)$(MANPREFIX)/man7/libterminput.7" clean: - -rm -f -- *.o *.a *.lo *.so *.su *.dll *.dylib interactive-test + -rm -f -- *.o *.a *.lo *.so *.su *.dll *.dylib interactive-test test .SUFFIXES: .SUFFIXES: .a .o .lo .c .$(LIBEXT) -.PHONY: all install uninstall clean +.PHONY: all check install uninstall clean @@ -1 +0,0 @@ -Add test diff --git a/libterminput.c b/libterminput.c index ed2415f..54016fe 100644 --- a/libterminput.c +++ b/libterminput.c @@ -333,7 +333,7 @@ parse_sequence(union libterminput_input *input, struct libterminput_state *ctx) case 'M': if (ctx->flags & LIBTERMINPUT_MACRO_ON_CSI_M) { input->keypress.key = LIBTERMINPUT_MACRO; - } else if (nnums >= 3) { + } else if (nnums >= 3) { /* TODO test */ /* Parsing for \e[?1000;1015h output. */ nums[0] -= 32ULL; decimal_mouse_tracking_set_press: @@ -352,7 +352,7 @@ parse_sequence(union libterminput_input *input, struct libterminput_state *ctx) input->mouseevent.event = LIBTERMINPUT_RELEASE; } input->mouseevent.button = (enum libterminput_button)nums[0]; - } else if (!nnums & !(ctx->flags & LIBTERMINPUT_DECSET_1005)) { + } else if (!nnums & !(ctx->flags & LIBTERMINPUT_DECSET_1005)) { /* TODO test */ /* Parsing output for legacy mouse tracking output. */ ctx->mouse_tracking = 0; nums = numsbuf; @@ -365,7 +365,7 @@ parse_sequence(union libterminput_input *input, struct libterminput_state *ctx) if (ctx->stored_head == ctx->stored_tail) ctx->stored_head = ctx->stored_tail = 0; goto decimal_mouse_tracking_set_press; - } else if (!nnums) { + } else if (!nnums) { /* TODO test */ /* Parsing for semi-legacy \e[?1000;1005h output. */ ctx->mouse_tracking = 0; nums = numsbuf; @@ -406,7 +406,7 @@ parse_sequence(union libterminput_input *input, struct libterminput_state *ctx) input->keypress.key = LIBTERMINPUT_F2; break; case 'R': - if ((ctx->flags & LIBTERMINPUT_AWAITING_CURSOR_POSITION) && nnums == 2) { + if ((ctx->flags & LIBTERMINPUT_AWAITING_CURSOR_POSITION) && nnums >= 2) { input->position.type = LIBTERMINPUT_CURSOR_POSITION; input->position.y = (size_t)nums[0] + (size_t)!nums[0]; input->position.x = (size_t)nums[1] + (size_t)!nums[1]; @@ -417,7 +417,7 @@ parse_sequence(union libterminput_input *input, struct libterminput_state *ctx) case 'S': input->keypress.key = LIBTERMINPUT_F4; break; - case 'T': + case 'T': /* TODO test */ /* Parsing output for legacy mouse highlight tracking output. (\e[?1001h) */ ctx->mouse_tracking = 0; nums = numsbuf; @@ -473,15 +473,15 @@ parse_sequence(union libterminput_input *input, struct libterminput_state *ctx) input->keypress.mods |= LIBTERMINPUT_SHIFT; break; case 'n': - if (nnums == 1 && nums[0] == '0') { + if (nnums == 1 && nums[0] == 0) { input->type = LIBTERMINPUT_TERMINAL_IS_OK; - } else if (nnums == 1 && nums[0] == '3') { + } else if (nnums == 1 && nums[0] == 3) { input->type = LIBTERMINPUT_TERMINAL_IS_NOT_OK; } else { goto suppress; } break; - case 't': + case 't': /* TODO test */ /* Parsing output for legacy mouse highlight tracking output (\e[?1001h). */ ctx->mouse_tracking = 0; nums = numsbuf; @@ -582,7 +582,7 @@ parse_sequence(union libterminput_input *input, struct libterminput_state *ctx) case 'D': input->keypress.key = LIBTERMINPUT_F4; break; case 'E': input->keypress.key = LIBTERMINPUT_F5; break; default: - if (ctx->key[1] == '<' && (ctx->key[2] == 'M' || ctx->key[2] == 'm') && nnums >= 3) { + if (ctx->key[1] == '<' && (ctx->key[2] == 'M' || ctx->key[2] == 'm') && nnums >= 3) { /* TODO test */ /* Parsing for \e[?1003;1006h output. */ input->mouseevent.event = LIBTERMINPUT_PRESS; if (ctx->key[2] == 'm') @@ -604,8 +604,10 @@ parse_sequence(union libterminput_input *input, struct libterminput_state *ctx) case 'B': input->keypress.key = LIBTERMINPUT_DOWN; break; case 'C': input->keypress.key = LIBTERMINPUT_RIGHT; break; case 'D': input->keypress.key = LIBTERMINPUT_LEFT; break; - case 'H': input->keypress.key = LIBTERMINPUT_HOME; break; + case 'E': input->keypress.key = LIBTERMINPUT_BEGIN; break; /* not attested */ case 'F': input->keypress.key = LIBTERMINPUT_END; break; + case 'G': input->keypress.key = LIBTERMINPUT_BEGIN; break; /* not attested */ + case 'H': input->keypress.key = LIBTERMINPUT_HOME; break; case 'P': input->keypress.key = LIBTERMINPUT_F1; break; case 'Q': input->keypress.key = LIBTERMINPUT_F2; break; case 'R': input->keypress.key = LIBTERMINPUT_F3; break; @@ -654,7 +656,7 @@ read_bracketed_paste(int fd, union libterminput_input *input, struct libterminpu * would stop the paste at the ~ in ESC [201~, send ~ as normal, and * then continue the brackated paste mode. */ - if (ctx->stored_head - ctx->stored_tail) { + if (ctx->stored_head - ctx->stored_tail) { /* TODO test */ for (n = ctx->stored_tail; n + 6 < ctx->stored_head; n++) { if (ctx->stored[n + 0] == '\033' && ctx->stored[n + 1] == '[' && ctx->stored[n + 2] == '2' && ctx->stored[n + 3] == '0' && ctx->stored[n + 4] == '0' && ctx->stored[n + 5] == '~') @@ -664,6 +666,7 @@ read_bracketed_paste(int fd, union libterminput_input *input, struct libterminpu ctx->stored_tail += 6; if (ctx->stored_tail == ctx->stored_head) ctx->stored_tail = ctx->stored_head = 0; + ctx->bracketed_paste = 0; input->type = LIBTERMINPUT_BRACKETED_PASTE_END; return 0; } @@ -692,9 +695,11 @@ read_bracketed_paste(int fd, union libterminput_input *input, struct libterminpu memcpy(ctx->stored, &input->text.bytes[6], ctx->stored_head); if (ctx->stored_tail == ctx->stored_head) ctx->stored_tail = ctx->stored_head = 0; + ctx->bracketed_paste = 0; input->type = LIBTERMINPUT_BRACKETED_PASTE_END; return 0; } + /* TODO test */ ctx->stored_tail = 0; ctx->stored_head = input->text.nbytes - n; input->text.nbytes = n; @@ -753,7 +758,7 @@ libterminput_read(int fd, union libterminput_input *input, struct libterminput_s } again: - if (!*ret.symbol) { + if (!*ret.symbol) { /* TODO test */ /* Incomplete input */ if (ctx->meta < 3) { /* Up to two Meta/ESC, wait until a third or something else is read */ @@ -785,15 +790,15 @@ again: p = stpcpy(&ctx->key[n], ret.symbol); /* Check if sequence is complete */ continue_incomplete: - if (!isalpha(p[-1]) && p[-1] != '~') { + if (!isalpha(p[-1]) && p[-1] != '~' && p[-1] != '@' && p[-1] != '^' && p[-1] != '$') { /* TODO test */ input->type = LIBTERMINPUT_NONE; return 1; - } else if (ctx->key[0] == '[' && ctx->key[1] == '<' && p == &ctx->key[2]) { + } else if (ctx->key[0] == '[' && ctx->key[1] == '<' && p == &ctx->key[2]) { /* TODO test */ input->type = LIBTERMINPUT_NONE; return 1; - } else if (ctx->key[0] == '[' && ctx->key[1] == 'M' && (ctx->flags & LIBTERMINPUT_MACRO_ON_CSI_M)) { + } else if (ctx->key[0] == '[' && ctx->key[1] == 'M' && (ctx->flags & LIBTERMINPUT_MACRO_ON_CSI_M)) { /* TODO test */ /* complete */ - } else if (ctx->key[0] == '[' && ctx->key[1] == 'M' && (ctx->flags & LIBTERMINPUT_DECSET_1005)) { + } else if (ctx->key[0] == '[' && ctx->key[1] == 'M' && (ctx->flags & LIBTERMINPUT_DECSET_1005)) { /* TODO test */ ctx->mouse_tracking = 1; if (ctx->stored_head == ctx->stored_tail) { input->type = LIBTERMINPUT_NONE; @@ -833,15 +838,15 @@ again: ctx->stored_tail += n; return 1; } - } else if (ctx->key[0] == '[' && ctx->key[1] == 'M' && ctx->stored_head - ctx->stored_tail < 3) { + } else if (ctx->key[0] == '[' && ctx->key[1] == 'M' && ctx->stored_head - ctx->stored_tail < 3) { /* TODO test */ ctx->mouse_tracking = 3; input->type = LIBTERMINPUT_NONE; return 1; - } else if (ctx->key[0] == '[' && ctx->key[1] == 't' && ctx->stored_head - ctx->stored_tail < 2) { + } else if (ctx->key[0] == '[' && ctx->key[1] == 't' && ctx->stored_head - ctx->stored_tail < 2) { /* TODO test */ ctx->mouse_tracking = 2; input->type = LIBTERMINPUT_NONE; return 1; - } else if (ctx->key[0] == '[' && ctx->key[1] == 'T' && ctx->stored_head - ctx->stored_tail < 6) { + } else if (ctx->key[0] == '[' && ctx->key[1] == 'T' && ctx->stored_head - ctx->stored_tail < 6) { /* TODO test */ ctx->mouse_tracking = 6; input->type = LIBTERMINPUT_NONE; return 1; @@ -855,7 +860,7 @@ again: /* ESC [ or ESC 0 is used as the beginning of most special keys */ strcpy(ctx->key, ret.symbol); input->type = LIBTERMINPUT_NONE; - } else { + } else { /* TODO test */ /* Character input and single-byte special keys */ input->type = LIBTERMINPUT_KEYPRESS; input->keypress.mods = ret.mods; @@ -0,0 +1,262 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "libterminput.h" + + +static const struct keypress { + const char *part1; + const char *part2; + enum libterminput_key key; + enum libterminput_mod mods; + enum libterminput_flags flags; +} keypresses[] = { + {"\033[[", "A", LIBTERMINPUT_F1, 0, 0}, + {"\033[[", "B", LIBTERMINPUT_F2, 0, 0}, + {"\033[[", "C", LIBTERMINPUT_F3, 0, 0}, + {"\033[[", "D", LIBTERMINPUT_F4, 0, 0}, + {"\033[[", "E", LIBTERMINPUT_F5, 0, 0}, + {"\033O", "A", LIBTERMINPUT_UP, 0, 0}, + {"\033O", "B", LIBTERMINPUT_DOWN, 0, 0}, + {"\033O", "C", LIBTERMINPUT_RIGHT, 0, 0}, + {"\033O", "D", LIBTERMINPUT_LEFT, 0, 0}, + {"\033O", "E", LIBTERMINPUT_BEGIN, 0, 0}, /* not attested */ + {"\033O", "F", LIBTERMINPUT_END, 0, 0}, + {"\033O", "G", LIBTERMINPUT_BEGIN, 0, 0}, /* not attested */ + {"\033O", "H", LIBTERMINPUT_HOME, 0, 0}, + {"\033O", "M", LIBTERMINPUT_KEYPAD_ENTER, 0, 0}, + {"\033O", "P", LIBTERMINPUT_F1, 0, 0}, + {"\033O", "Q", LIBTERMINPUT_F2, 0, 0}, + {"\033O", "R", LIBTERMINPUT_F3, 0, 0}, + {"\033O", "S", LIBTERMINPUT_F4, 0, 0}, + {"\033O", "p", LIBTERMINPUT_KEYPAD_0, 0, 0}, + {"\033O", "q", LIBTERMINPUT_KEYPAD_1, 0, 0}, + {"\033O", "r", LIBTERMINPUT_KEYPAD_2, 0, 0}, + {"\033O", "s", LIBTERMINPUT_KEYPAD_3, 0, 0}, + {"\033O", "t", LIBTERMINPUT_KEYPAD_4, 0, 0}, + {"\033O", "u", LIBTERMINPUT_KEYPAD_5, 0, 0}, + {"\033O", "v", LIBTERMINPUT_KEYPAD_6, 0, 0}, + {"\033O", "w", LIBTERMINPUT_KEYPAD_7, 0, 0}, + {"\033O", "x", LIBTERMINPUT_KEYPAD_8, 0, 0}, + {"\033O", "y", LIBTERMINPUT_KEYPAD_9, 0, 0}, + {"\033O", "k", LIBTERMINPUT_KEYPAD_PLUS, 0, 0}, + {"\033O", "m", LIBTERMINPUT_KEYPAD_MINUS, 0, 0}, + {"\033O", "j", LIBTERMINPUT_KEYPAD_TIMES, 0, 0}, + {"\033O", "o", LIBTERMINPUT_KEYPAD_DIVISION, 0, 0}, + {"\033O", "n", LIBTERMINPUT_KEYPAD_DECIMAL, 0, 0}, + {"\033O", "l", LIBTERMINPUT_KEYPAD_COMMA, 0, 0}, + {"\033O", "b", LIBTERMINPUT_KEYPAD_POINT, 0, 0}, + {"\033[", "A", LIBTERMINPUT_UP, 0, 0}, + {"\033[", "B", LIBTERMINPUT_DOWN, 0, 0}, + {"\033[", "C", LIBTERMINPUT_RIGHT, 0, 0}, + {"\033[", "D", LIBTERMINPUT_LEFT, 0, 0}, + {"\033[", "E", LIBTERMINPUT_BEGIN, 0, 0}, + {"\033[", "F", LIBTERMINPUT_END, 0, 0}, + {"\033[", "G", LIBTERMINPUT_BEGIN, 0, 0}, + {"\033[", "H", LIBTERMINPUT_HOME, 0, 0}, + {"\033[", "M", LIBTERMINPUT_MACRO, 0, LIBTERMINPUT_MACRO_ON_CSI_M}, + {"\033[", "P", LIBTERMINPUT_F1, 0, 0}, + {"\033[", "P", LIBTERMINPUT_PAUSE, 0, LIBTERMINPUT_PAUSE_ON_CSI_P}, + {"\033[", "Q", LIBTERMINPUT_F2, 0, 0}, + {"\033[", "R", LIBTERMINPUT_F3, 0, 0}, + {"\033[", "S", LIBTERMINPUT_F4, 0, 0}, + {"\033[", "U", LIBTERMINPUT_NEXT, 0, 0}, + {"\033[", "V", LIBTERMINPUT_PRIOR, 0, 0}, + {"\033[", "Z", LIBTERMINPUT_TAB, LIBTERMINPUT_SHIFT, 0}, + {"\033[", "Z", LIBTERMINPUT_BACKTAB, 0, LIBTERMINPUT_SEPARATE_BACKTAB}, + {"\033[", "a", LIBTERMINPUT_UP, LIBTERMINPUT_SHIFT, 0}, + {"\033[", "b", LIBTERMINPUT_DOWN, LIBTERMINPUT_SHIFT, 0}, + {"\033[", "c", LIBTERMINPUT_RIGHT, LIBTERMINPUT_SHIFT, 0}, + {"\033[", "d", LIBTERMINPUT_LEFT, LIBTERMINPUT_SHIFT, 0}, + {"\033[", "@", LIBTERMINPUT_INS, 0, LIBTERMINPUT_INS_ON_CSI_AT}, + {NULL, NULL, 0, 0, 0} +}; + +static const struct keynum { + int number; + enum libterminput_key key; + enum libterminput_mod mods; + enum libterminput_flags flags; +} keynums[] = { + {1, LIBTERMINPUT_HOME, 0, 0}, + {2, LIBTERMINPUT_INS, 0, 0}, + {3, LIBTERMINPUT_DEL, 0, 0}, + {4, LIBTERMINPUT_END, 0, 0}, + {5, LIBTERMINPUT_PRIOR, 0, 0}, + {6, LIBTERMINPUT_NEXT, 0, 0}, + {7, LIBTERMINPUT_HOME, 0, 0}, + {8, LIBTERMINPUT_END, 0, 0}, + {9, LIBTERMINPUT_ESC, 0, 0}, /* just made this one up */ + {11, LIBTERMINPUT_F1, 0, 0}, + {12, LIBTERMINPUT_F2, 0, 0}, + {13, LIBTERMINPUT_F3, 0, 0}, + {14, LIBTERMINPUT_F4, 0, 0}, + {15, LIBTERMINPUT_F5, 0, 0}, + {17, LIBTERMINPUT_F6, 0, 0}, + {18, LIBTERMINPUT_F7, 0, 0}, + {19, LIBTERMINPUT_F8, 0, 0}, + {20, LIBTERMINPUT_F9, 0, 0}, + {21, LIBTERMINPUT_F10, 0, 0}, + {23, LIBTERMINPUT_F11, 0, 0}, + {24, LIBTERMINPUT_F12, 0, 0}, + {25, LIBTERMINPUT_F1, LIBTERMINPUT_SHIFT, 0}, + {26, LIBTERMINPUT_F2, LIBTERMINPUT_SHIFT, 0}, + {28, LIBTERMINPUT_F3, LIBTERMINPUT_SHIFT, 0}, + {29, LIBTERMINPUT_F4, LIBTERMINPUT_SHIFT, 0}, + {31, LIBTERMINPUT_F5, LIBTERMINPUT_SHIFT, 0}, + {32, LIBTERMINPUT_F6, LIBTERMINPUT_SHIFT, 0}, + {33, LIBTERMINPUT_F7, LIBTERMINPUT_SHIFT, 0}, + {34, LIBTERMINPUT_F8, LIBTERMINPUT_SHIFT, 0}, + {0, 0, 0, 0} +}; + + +int +main(void) +{ +#define TEST(EXPR)\ + do {\ + if (EXPR)\ + break;\ + fprintf(stderr, "Failure at line %i, with errno = %i (%s): %s\n", __LINE__, errno, strerror(errno), #EXPR);\ + exit(1);\ + } while (0) + +#define TYPE(STR, T)\ + do {\ + TEST(write(fds[1], STR, strlen(STR)) == (ssize_t)strlen(STR));\ + do {\ + TEST(libterminput_read(fds[0], &input, &ctx) == 1);\ + } while (input.type == LIBTERMINPUT_NONE && libterminput_is_ready(&input, &ctx));\ + TEST(input.type == (T));\ + } while (0) + +#define KEYPRESS_(STR1, STR2, STR3, STR4, KEY, MODS, TIMES)\ + do {\ + int times__ = (TIMES);\ + stpcpy(stpcpy(stpcpy(stpcpy(buffer, STR1), STR2), STR3), STR4);\ + TEST(write(fds[1], buffer, strlen(buffer)) == (ssize_t)strlen(buffer));\ + for (; times__; times__--) {\ + do {\ + TEST(libterminput_read(fds[0], &input, &ctx) == 1);\ + } while (input.type == LIBTERMINPUT_NONE && libterminput_is_ready(&input, &ctx));\ + TEST(input.type == LIBTERMINPUT_KEYPRESS);\ + TEST(input.keypress.key == (KEY));\ + TEST(input.keypress.mods == (MODS));\ + TEST(input.keypress.times == times__);\ + }\ + } while (0) + +#define KEYPRESS(A, B, KEY, MODS)\ + do {\ + KEYPRESS_("", A, "", B, (KEY), (MODS), 1);\ + KEYPRESS_("", A, "4", B, (KEY), (MODS), 4);\ + KEYPRESS_("", A, "1;1", B, (KEY), (MODS), 1);\ + KEYPRESS_("", A, "1;2", B, (KEY), (MODS) | LIBTERMINPUT_SHIFT, 1);\ + KEYPRESS_("", A, "1;3", B, (KEY), (MODS) | LIBTERMINPUT_META, 1);\ + KEYPRESS_("", A, "1;4", B, (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META, 1);\ + KEYPRESS_("", A, "1;5", B, (KEY), (MODS) | LIBTERMINPUT_CTRL, 1);\ + KEYPRESS_("", A, "1;6", B, (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_CTRL, 1);\ + KEYPRESS_("", A, "1;7", B, (KEY), (MODS) | LIBTERMINPUT_META | LIBTERMINPUT_CTRL, 1);\ + KEYPRESS_("", A, "1;8", B, (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META | LIBTERMINPUT_CTRL, 1);\ + KEYPRESS_("", A, "2;5", B, (KEY), (MODS) | LIBTERMINPUT_CTRL, 2);\ + KEYPRESS_("\033", A, "", B, (KEY), (MODS) | LIBTERMINPUT_META, 1);\ + KEYPRESS_("\033", A, "4", B, (KEY), (MODS) | LIBTERMINPUT_META, 4);\ + KEYPRESS_("\033", A, "1;1", B, (KEY), (MODS) | LIBTERMINPUT_META, 1);\ + KEYPRESS_("\033", A, "1;2", B, (KEY), (MODS) | LIBTERMINPUT_META | LIBTERMINPUT_SHIFT, 1);\ + KEYPRESS_("\033", A, "1;3", B, (KEY), (MODS) | LIBTERMINPUT_META, 1);\ + KEYPRESS_("\033", A, "1;4", B, (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META, 1);\ + KEYPRESS_("\033", A, "1;5", B, (KEY), (MODS) | LIBTERMINPUT_META | LIBTERMINPUT_CTRL, 1);\ + KEYPRESS_("\033", A, "1;6", B, (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META | LIBTERMINPUT_CTRL, 1);\ + KEYPRESS_("\033", A, "1;7", B, (KEY), (MODS) | LIBTERMINPUT_META | LIBTERMINPUT_CTRL, 1);\ + KEYPRESS_("\033", A, "1;8", B, (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META | LIBTERMINPUT_CTRL, 1);\ + KEYPRESS_("\033", A, "2;5", B, (KEY), (MODS) | LIBTERMINPUT_META | LIBTERMINPUT_CTRL, 2);\ + } while (0) + +#define KEYNUM_(A, B, C, KEY, MODS)\ + do {\ + sprintf(numbuf, "%i", B);\ + KEYPRESS_(A, numbuf, C, "~", (KEY), (MODS), 1);\ + KEYPRESS_(A, numbuf, C, "^", (KEY), (MODS) | LIBTERMINPUT_CTRL, 1);\ + KEYPRESS_(A, numbuf, C, "$", (KEY), (MODS) | LIBTERMINPUT_SHIFT, 1);\ + KEYPRESS_(A, numbuf, C, "@", (KEY), (MODS) | LIBTERMINPUT_CTRL | LIBTERMINPUT_SHIFT, 1);\ + } while (0) + +#define KEYNUM(NUM, KEY, MODS)\ + do {\ + KEYNUM_("\033[", NUM, ";1", (KEY), (MODS));\ + KEYNUM_("\033[", NUM, ";2", (KEY), (MODS) | LIBTERMINPUT_SHIFT);\ + KEYNUM_("\033[", NUM, ";3", (KEY), (MODS) | LIBTERMINPUT_META);\ + KEYNUM_("\033[", NUM, ";4", (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META);\ + KEYNUM_("\033[", NUM, ";5", (KEY), (MODS) | LIBTERMINPUT_CTRL);\ + KEYNUM_("\033[", NUM, ";6", (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_CTRL);\ + KEYNUM_("\033[", NUM, ";7", (KEY), (MODS) | LIBTERMINPUT_META | LIBTERMINPUT_CTRL);\ + KEYNUM_("\033[", NUM, ";8", (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META | LIBTERMINPUT_CTRL);\ + KEYNUM_("\033\033[", NUM, ";1", (KEY), (MODS) | LIBTERMINPUT_META);\ + KEYNUM_("\033\033[", NUM, ";2", (KEY), (MODS) | LIBTERMINPUT_META | LIBTERMINPUT_SHIFT);\ + KEYNUM_("\033\033[", NUM, ";3", (KEY), (MODS) | LIBTERMINPUT_META);\ + KEYNUM_("\033\033[", NUM, ";4", (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META);\ + KEYNUM_("\033\033[", NUM, ";5", (KEY), (MODS) | LIBTERMINPUT_META | LIBTERMINPUT_CTRL);\ + KEYNUM_("\033\033[", NUM, ";6", (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META | LIBTERMINPUT_CTRL);\ + KEYNUM_("\033\033[", NUM, ";7", (KEY), (MODS) | LIBTERMINPUT_META | LIBTERMINPUT_CTRL);\ + KEYNUM_("\033\033[", NUM, ";8", (KEY), (MODS) | LIBTERMINPUT_SHIFT | LIBTERMINPUT_META | LIBTERMINPUT_CTRL);\ + } while (0) + + char buffer[512], numbuf[3 * sizeof(int) + 2]; + struct libterminput_state ctx; + union libterminput_input input; + int fds[2]; + size_t i; + + memset(&ctx, 0, sizeof(ctx)); + TEST(!pipe(fds)); + + for (i = 0; keypresses[i].part1; i++) { + libterminput_set_flags(&ctx, keypresses[i].flags); + KEYPRESS(keypresses[i].part1, keypresses[i].part2, keypresses[i].key, keypresses[i].mods); + libterminput_clear_flags(&ctx, keypresses[i].flags); + } + + for (i = 0; keynums[i].number; i++) { + libterminput_set_flags(&ctx, keynums[i].flags); + KEYNUM(keynums[i].number, keynums[i].key, keynums[i].mods); + libterminput_clear_flags(&ctx, keynums[i].flags); + } + + TYPE("\033[201~", LIBTERMINPUT_BRACKETED_PASTE_END); + TYPE("x", LIBTERMINPUT_KEYPRESS); +#ifdef TODO + TYPE("\033[200~", LIBTERMINPUT_BRACKETED_PASTE_START); + TYPE("x", LIBTERMINPUT_TEXT); +#endif + TYPE("\033[201~", LIBTERMINPUT_BRACKETED_PASTE_END); + TYPE("\033[200^", LIBTERMINPUT_NONE); + TYPE("\033[200$", LIBTERMINPUT_NONE); + TYPE("\033[200@", LIBTERMINPUT_NONE); + TYPE("\033[201^", LIBTERMINPUT_NONE); + TYPE("\033[201$", LIBTERMINPUT_NONE); + TYPE("\033[201@", LIBTERMINPUT_NONE); + TYPE("\033[n", LIBTERMINPUT_NONE); + TYPE("\033[0n", LIBTERMINPUT_TERMINAL_IS_OK); + TYPE("\033[3n", LIBTERMINPUT_TERMINAL_IS_NOT_OK); + libterminput_set_flags(&ctx, LIBTERMINPUT_AWAITING_CURSOR_POSITION); + KEYPRESS_("\033[R", "", "", "", LIBTERMINPUT_F3, 0, 1); + KEYPRESS_("\033[1R", "", "", "", LIBTERMINPUT_F3, 0, 1); + TYPE("\033[1;1R", LIBTERMINPUT_CURSOR_POSITION); + TEST(input.position.y == 1); + TEST(input.position.x == 1); + TYPE("\033[25;93R", LIBTERMINPUT_CURSOR_POSITION); + TEST(input.position.y == 25); + TEST(input.position.x == 93); + libterminput_clear_flags(&ctx, LIBTERMINPUT_AWAITING_CURSOR_POSITION); + + close(fds[1]); + TEST(libterminput_read(fds[0], &input, &ctx) == 0); + close(fds[0]); + TEST(libterminput_read(fds[0], &input, &ctx) == -1 && errno == EBADF); + return 0; +} |