refactor(tui): use termkey_interpret_modereport

The sign extension issue has been fixed upstream, so we no longer need
to use our own workaround.
This commit is contained in:
Gregory Anders
2023-11-17 12:15:16 -06:00
parent 89dd939c15
commit e80b83bf56
3 changed files with 26 additions and 24 deletions

View File

@@ -586,11 +586,13 @@ static void handle_term_response(TermInput *input, const TermKeyKey *key)
static void handle_modereport(TermInput *input, const TermKeyKey *key) static void handle_modereport(TermInput *input, const TermKeyKey *key)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
// termkey_interpret_modereport incorrectly sign extends the mode so we parse the response int initial;
// ourselves int mode;
int mode = (uint8_t)key->code.mouse[1] << 8 | (uint8_t)key->code.mouse[2]; int value;
TerminalModeState value = (uint8_t)key->code.mouse[3]; if (termkey_interpret_modereport(input->tk, key, &initial, &mode, &value) == TERMKEY_RES_KEY) {
tui_dec_report_mode(input->tui_data, (TerminalDecMode)mode, value); (void)initial; // Unused
tui_handle_term_mode(input->tui_data, (TermMode)mode, (TermModeState)value);
}
} }
/// Handle a CSI sequence from the terminal that is unrecognized by libtermkey. /// Handle a CSI sequence from the terminal that is unrecognized by libtermkey.

View File

@@ -211,10 +211,10 @@ static void tui_reset_key_encoding(TUIData *tui)
} }
} }
/// Request the terminal's DEC mode (DECRQM). /// Request the terminal's mode (DECRQM).
/// ///
/// @see handle_modereport /// @see handle_modereport
static void tui_dec_request_mode(TUIData *tui, TerminalDecMode mode) static void tui_request_term_mode(TUIData *tui, TermMode mode)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
// 5 bytes for \x1b[?$p, 1 byte for null terminator, 6 bytes for mode digits (more than enough) // 5 bytes for \x1b[?$p, 1 byte for null terminator, 6 bytes for mode digits (more than enough)
@@ -224,22 +224,22 @@ static void tui_dec_request_mode(TUIData *tui, TerminalDecMode mode)
out(tui, buf, (size_t)len); out(tui, buf, (size_t)len);
} }
/// Handle a DECRPM response from the terminal. /// Handle a mode report (DECRPM) from the terminal.
void tui_dec_report_mode(TUIData *tui, TerminalDecMode mode, TerminalModeState state) void tui_handle_term_mode(TUIData *tui, TermMode mode, TermModeState state)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
switch (state) { switch (state) {
case kTerminalModeNotRecognized: case kTermModeNotRecognized:
case kTerminalModePermanentlySet: case kTermModePermanentlySet:
case kTerminalModePermanentlyReset: case kTermModePermanentlyReset:
// If the mode is not recognized, or if the terminal emulator does not allow it to be changed, // If the mode is not recognized, or if the terminal emulator does not allow it to be changed,
// then there is nothing to do // then there is nothing to do
break; break;
case kTerminalModeSet: case kTermModeSet:
case kTerminalModeReset: case kTermModeReset:
// The terminal supports changing the given mode // The terminal supports changing the given mode
switch (mode) { switch (mode) {
case kDecModeSynchronizedOutput: case kTermModeSynchronizedOutput:
// Ref: https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036 // Ref: https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036
tui->unibi_ext.sync = (int)unibi_add_ext_str(tui->ut, "Sync", tui->unibi_ext.sync = (int)unibi_add_ext_str(tui->ut, "Sync",
"\x1b[?2026%?%p1%{1}%-%tl%eh%;"); "\x1b[?2026%?%p1%{1}%-%tl%eh%;");
@@ -371,7 +371,7 @@ static void terminfo_start(TUIData *tui)
// Query support for mode 2026 (Synchronized Output). Some terminals also // Query support for mode 2026 (Synchronized Output). Some terminals also
// support an older DCS sequence for synchronized output, but we will only use // support an older DCS sequence for synchronized output, but we will only use
// mode 2026 // mode 2026
tui_dec_request_mode(tui, kDecModeSynchronizedOutput); tui_request_term_mode(tui, kTermModeSynchronizedOutput);
// Query the terminal to see if it supports Kitty's keyboard protocol // Query the terminal to see if it supports Kitty's keyboard protocol
tui_query_kitty_keyboard(tui); tui_query_kitty_keyboard(tui);

View File

@@ -6,16 +6,16 @@
typedef struct TUIData TUIData; typedef struct TUIData TUIData;
typedef enum { typedef enum {
kDecModeSynchronizedOutput = 2026, kTermModeSynchronizedOutput = 2026,
} TerminalDecMode; } TermMode;
typedef enum { typedef enum {
kTerminalModeNotRecognized = 0, kTermModeNotRecognized = 0,
kTerminalModeSet = 1, kTermModeSet = 1,
kTerminalModeReset = 2, kTermModeReset = 2,
kTerminalModePermanentlySet = 3, kTermModePermanentlySet = 3,
kTerminalModePermanentlyReset = 4, kTermModePermanentlyReset = 4,
} TerminalModeState; } TermModeState;
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "tui/tui.h.generated.h" # include "tui/tui.h.generated.h"