-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query terminal for
bce
, rep
and tsl
capabilities via XTGETTCAP
This is supported by xterm, foot, ghostty, iterm2 and perhaps a few other terminals. It serves a similar purpose to querying `terminfo(5)` capabilities from the filesystem, but with the following advantages: * Doesn't require a terminfo(5) entry (or database) to be installed * Doesn't require linking to libtinfo (or reimplementing parts of it) * Works reliably over SSH * Works reliably (in principle, at least) when spoofing `$TERM`, as is done automatically in some contexts (e.g. Docker) See also: * https://invisible-island.net/xterm/ctlseqs/ctlseqs.html * https://codeberg.org/dnkl/foot/src/commit/a1ac37e771edf9fe907ee09dbec0e90d447679e1/doc/foot-ctlseqs.7.scd#L756-L757 * moby/moby#42323 * https://docs.docker.com/engine/reference/run/#environment-variables
- Loading branch information
1 parent
8f6b3f4
commit 9723150
Showing
6 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "query.h" | ||
#include "cursor.h" | ||
#include "terminal.h" | ||
#include "util/log.h" | ||
#include "util/string-view.h" | ||
#include "util/strtonum.h" | ||
|
||
KeyCode parse_dcs_query_reply(const char *data, size_t len, bool truncated) | ||
{ | ||
const char *note = ""; | ||
if (unlikely(len == 0 || truncated)) { | ||
note = truncated ? " (truncated)" : " (empty)"; | ||
goto unhandled; | ||
} | ||
|
||
StringView seq = string_view(data, len); | ||
if (strview_has_prefix(&seq, "1+r")) { | ||
strview_remove_prefix(&seq, 3); | ||
if (strview_equal_cstring(&seq, "626365")) { // "bce" | ||
return KEYCODE_QUERY_REPLY_BIT | TFLAG_BACK_COLOR_ERASE; | ||
} | ||
if (strview_equal_cstring_icase(&seq, "74736C=1B5D323B")) { // "tsl=\e]2;" | ||
return KEYCODE_QUERY_REPLY_BIT | TFLAG_SET_WINDOW_TITLE; | ||
} | ||
if ( | ||
strview_has_prefix(&seq, "726570=") // "rep=" | ||
&& strview_has_suffix(&seq, "62") // "b" | ||
&& (seq.length & 1) // Odd length (2x even hex strings + "=") | ||
) { | ||
return KEYCODE_QUERY_REPLY_BIT | TFLAG_ECMA48_REPEAT; | ||
} | ||
LOG_DEBUG("unhandled XTGETTCAP reply: %.*s", (int)len, data); | ||
return KEY_IGNORE; | ||
} | ||
|
||
if (strview_has_prefix(&seq, "1$r")) { | ||
strview_remove_prefix(&seq, 3); | ||
if (strview_has_suffix(&seq, " q")) { | ||
size_t n = seq.length - 2; | ||
unsigned int x; | ||
if (n >= 1 && n == buf_parse_uint(seq.data, n, &x)) { | ||
const char *str = (x <= CURSOR_STEADY_BAR) ? cursor_type_to_str(x) : "??"; | ||
LOG_DEBUG("DECRQSS DECSCUSR (cursor style) reply: %u (%s)", x, str); | ||
return KEY_IGNORE; | ||
} | ||
} | ||
// TODO: Detect RGB and italic support from DECRQSS "1$r0;3;38:2::60:70:80m" reply | ||
// TODO: To facilitate the above, convert Terminal::color_type to TermFeatureFlags bits | ||
LOG_DEBUG("unhandled DECRQSS reply: %.*s", (int)len, data); | ||
return KEY_IGNORE; | ||
} | ||
|
||
if (strview_has_prefix(&seq, "0+r")) { | ||
note = " (XTGETTCAP; 'invalid request')"; | ||
goto unhandled; | ||
} | ||
|
||
if (strview_equal_cstring(&seq, "0$r")) { | ||
note = " (DECRQSS; 'invalid request')"; | ||
goto unhandled; | ||
} | ||
|
||
unhandled: | ||
LOG_DEBUG("unhandled DCS string%s: %.*s", note, (int)len, data); | ||
return KEY_IGNORE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef TERMINAL_QUERY_H | ||
#define TERMINAL_QUERY_H | ||
|
||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include "key.h" | ||
#include "util/macros.h" | ||
|
||
KeyCode parse_dcs_query_reply(const char *data, size_t len, bool truncated) NONNULL_ARGS WARN_UNUSED_RESULT; | ||
|
||
#endif |