From 3fa6bec91213525b031f76a67c6b685912dc6720 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 2 Jan 2022 18:28:40 +0100 Subject: dcs: implement XTGETTCAP We emit one DCS reply for each queried capability (like Kitty, but unlike XTerm), as this allows us to a) not skip any capabilities in the query, and b) reply with correct success/fail flag for each capability. We do not batch the entire reply - as soon as the reply for _one_ capability is done, we write it to the PTY. Closes #846 --- dcs.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-) (limited to 'dcs.c') diff --git a/dcs.c b/dcs.c index 79b04df9..6fed8010 100644 --- a/dcs.c +++ b/dcs.c @@ -1,10 +1,14 @@ #include "dcs.h" +#include #define LOG_MODULE "dcs" #define LOG_ENABLE_DBG 0 #include "log.h" +#include "foot-terminfo.h" #include "sixel.h" +#include "util.h" #include "vt.h" +#include "xmalloc.h" static void bsu(struct terminal *term) @@ -30,6 +34,139 @@ esu(struct terminal *term) term_disable_app_sync_updates(term); } +/* Decode hex-encoded string *inline*. NULL terminates */ +static char * +hex_decode(const char *s, size_t len) +{ + if (len % 2) + return NULL; + + char *hex = xmalloc(len / 2 + 1); + char *o = hex; + + /* TODO: error checking */ + for (size_t i = 0; i < len; i += 2) { + uint8_t nib1 = hex2nibble(*s); s++; + uint8_t nib2 = hex2nibble(*s); s++; + + if (nib1 == HEX_DIGIT_INVALID || nib2 == HEX_DIGIT_INVALID) + goto err; + + *o = nib1 << 4 | nib2; o++; + } + + *o = '\0'; + return hex; + +err: + free(hex); + return NULL; +} + +UNITTEST +{ + /* Verify table is sorted */ + for (size_t i = 1; i < ALEN(terminfo_capabilities); i++) { + xassert(strcmp(terminfo_capabilities[i - 1].name, + terminfo_capabilities[i].name ) <= 0); + } +} + +static int +terminfo_entry_compar(const void *_key, const void *_entry) +{ + const char *key = _key; + const struct foot_terminfo_entry *entry = _entry; + + return strcmp(key, entry->name); +} + +static void +xtgettcap_reply(struct terminal *term, const char *hex_cap_name, size_t len) +{ + char *name = hex_decode(hex_cap_name, len); + if (name == NULL) + goto err; + + const struct foot_terminfo_entry *entry = + bsearch(name, terminfo_capabilities, ALEN(terminfo_capabilities), + sizeof(*entry), &terminfo_entry_compar); + + LOG_DBG("XTGETTCAP: cap=%s (%.*s), value=%s", + name, (int)len, hex_cap_name, + entry != NULL ? entry->value : ""); + + if (entry == NULL) + goto err; + + /* + * Reply format: + * \EP 1 + r cap=value \E\\ + * Where ‘cap’ and ‘value are hex encoded ascii strings + */ + char *reply = xmalloc( + 5 + /* DCS 1 + r (\EP1+r) */ + len + /* capability name, hex encoded */ + 1 + /* ‘=’ */ + strlen(entry->value) * 2 + /* capability value, hex encoded */ + 2 + /* ST (\E\\) */ + 1); + + int idx = sprintf(reply, "\033P1+r%.*s=", (int)len, hex_cap_name); + + for (const char *c = entry->value; *c != '\0'; c++) { + uint8_t nib1 = (uint8_t)*c >> 4; + uint8_t nib2 = (uint8_t)*c & 0xf; + + reply[idx] = nib1 >= 0xa ? 'A' + nib1 - 0xa : '0' + nib1; idx++; + reply[idx] = nib2 >= 0xa ? 'A' + nib2 - 0xa : '0' + nib2; idx++; + } + + reply[idx] = '\033'; idx++; + reply[idx] = '\\'; idx++; + term_to_slave(term, reply, idx); + + free(reply); + goto out; + +err: + term_to_slave(term, "\033P0+r", 5); + term_to_slave(term, hex_cap_name, len); + term_to_slave(term, "\033\\", 2); + +out: + free(name); +} + +static void +xtgettcap_unhook(struct terminal *term) +{ + size_t left = term->vt.dcs.idx; + + const char *const end = (const char *)&term->vt.dcs.data[left]; + const char *p = (const char *)term->vt.dcs.data; + + while (true) { + const char *sep = memchr(p, ';', left); + size_t cap_len; + + if (sep == NULL) { + /* Last capability */ + cap_len = end - p; + } else { + cap_len = sep - p; + } + + xtgettcap_reply(term, p, cap_len); + + left -= cap_len + 1; + p += cap_len + 1; + + if (sep == NULL) + break; + } +} + void dcs_hook(struct terminal *term, uint8_t final) { @@ -67,6 +204,14 @@ dcs_hook(struct terminal *term, uint8_t final) break; } break; + + case '+': + switch (final) { + case 'q': /* XTGETTCAP */ + term->vt.dcs.unhook_handler = &xtgettcap_unhook; + break; + } + break; } } @@ -93,7 +238,8 @@ ensure_size(struct terminal *term, size_t required_size) void dcs_put(struct terminal *term, uint8_t c) { - LOG_DBG("PUT: %c", c); + /* LOG_DBG("PUT: %c", c); */ + if (term->vt.dcs.put_handler != NULL) term->vt.dcs.put_handler(term, c); else { -- cgit v1.3 From 44aad0941fff3e314ae42c3c578cfa6d61913198 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 4 Jan 2022 22:01:36 +0100 Subject: dcs: xtgettcap: handle boolean capabilities Reply with DCS 1 + r ST --- dcs.c | 8 ++++++++ scripts/generate-builtin-terminfo.py | 9 +++++---- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'dcs.c') diff --git a/dcs.c b/dcs.c index 6fed8010..07fec5c7 100644 --- a/dcs.c +++ b/dcs.c @@ -99,6 +99,14 @@ xtgettcap_reply(struct terminal *term, const char *hex_cap_name, size_t len) if (entry == NULL) goto err; + if (entry->value == NULL) { + /* Boolean */ + term_to_slave(term, "\033P1+r", 5); + term_to_slave(term, hex_cap_name, len); + term_to_slave(term, "\033\\", 2); + goto out; + } + /* * Reply format: * \EP 1 + r cap=value \E\\ diff --git a/scripts/generate-builtin-terminfo.py b/scripts/generate-builtin-terminfo.py index a58b7da3..9c8807bf 100755 --- a/scripts/generate-builtin-terminfo.py +++ b/scripts/generate-builtin-terminfo.py @@ -174,9 +174,6 @@ def main(): 'static const struct foot_terminfo_entry terminfo_capabilities[] = {\n') for cap in sorted(entry.caps.values()): - if isinstance(cap, BoolCapability): - continue - name = cap.name value = str(cap.value) @@ -194,7 +191,11 @@ def main(): # Do escape ‘“‘ name = name.replace('"', '\"') value = value.replace('"', '\"') - target.write(f' {{"{name}", "{value}"}},\n') + + if isinstance(cap, BoolCapability): + target.write(f' {{"{name}", NULL}},\n') + else: + target.write(f' {{"{name}", "{value}"}},\n') target.write('};\n') -- cgit v1.3 From 5a032c4c6f6517e664ac7b920c3cfae8d5a25b05 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 13 Jan 2022 13:37:44 +0100 Subject: xtgettcap: switch from a table based internal structure, to a single char array Having the builtin terminfo DB as a table, with one entry per capability/value pair was ineffective memory-wise - we ended up adding ~20K to the final binary size. This patch changes the internal representation from a table, to a single NULL-separated (and NULL-terminated) char array: cap1-name \0 cap1-value \0 cap2-name \0 cap2-value \0 The capabilities are still sorted; to lookup a capability we need to scan from the beginning until we either find a match, or until the capabilities from the DB sort higher (lexicographically) than the searched-for capability. The terminfo char array is 3.3K - more better than before. --- dcs.c | 64 ++++++++++++++++++++++++++++-------- scripts/generate-builtin-terminfo.py | 57 ++++++++++++++------------------ 2 files changed, 75 insertions(+), 46 deletions(-) (limited to 'dcs.c') diff --git a/dcs.c b/dcs.c index 07fec5c7..1a388863 100644 --- a/dcs.c +++ b/dcs.c @@ -66,19 +66,52 @@ err: UNITTEST { /* Verify table is sorted */ - for (size_t i = 1; i < ALEN(terminfo_capabilities); i++) { - xassert(strcmp(terminfo_capabilities[i - 1].name, - terminfo_capabilities[i].name ) <= 0); + const char *p = terminfo_capabilities; + size_t left = sizeof(terminfo_capabilities); + + const char *last_cap = NULL; + + while (left > 0) { + const char *cap = p; + const char *val = cap + strlen(cap) + 1; + + size_t size = strlen(cap) + 1 + strlen(val) + 1;; + xassert(size <= left); + p += size; + left -= size; + + if (last_cap != NULL) + xassert(strcmp(last_cap, cap) < 0); + + last_cap = cap; } } -static int -terminfo_entry_compar(const void *_key, const void *_entry) +static bool +lookup_capability(const char *name, const char **value) { - const char *key = _key; - const struct foot_terminfo_entry *entry = _entry; + const char *p = terminfo_capabilities; + size_t left = sizeof(terminfo_capabilities); + + while (left > 0) { + const char *cap = p; + const char *val = cap + strlen(cap) + 1; + + size_t size = strlen(cap) + 1 + strlen(val) + 1;; + xassert(size <= left); + p += size; + left -= size; + + int r = strcmp(cap, name); + if (r == 0) { + *value = val; + return true; + } else if (r > 0) + break; + } - return strcmp(key, entry->name); + *value = NULL; + return false; } static void @@ -88,18 +121,23 @@ xtgettcap_reply(struct terminal *term, const char *hex_cap_name, size_t len) if (name == NULL) goto err; +#if 0 const struct foot_terminfo_entry *entry = bsearch(name, terminfo_capabilities, ALEN(terminfo_capabilities), sizeof(*entry), &terminfo_entry_compar); +#endif + const char *value; + bool valid_capability = lookup_capability(name, &value); + xassert(!valid_capability || value != NULL); LOG_DBG("XTGETTCAP: cap=%s (%.*s), value=%s", name, (int)len, hex_cap_name, - entry != NULL ? entry->value : ""); + valid_capability ? value : ""); - if (entry == NULL) + if (!valid_capability) goto err; - if (entry->value == NULL) { + if (value[0] == '\0') { /* Boolean */ term_to_slave(term, "\033P1+r", 5); term_to_slave(term, hex_cap_name, len); @@ -116,13 +154,13 @@ xtgettcap_reply(struct terminal *term, const char *hex_cap_name, size_t len) 5 + /* DCS 1 + r (\EP1+r) */ len + /* capability name, hex encoded */ 1 + /* ‘=’ */ - strlen(entry->value) * 2 + /* capability value, hex encoded */ + strlen(value) * 2 + /* capability value, hex encoded */ 2 + /* ST (\E\\) */ 1); int idx = sprintf(reply, "\033P1+r%.*s=", (int)len, hex_cap_name); - for (const char *c = entry->value; *c != '\0'; c++) { + for (const char *c = value; *c != '\0'; c++) { uint8_t nib1 = (uint8_t)*c >> 4; uint8_t nib2 = (uint8_t)*c & 0xf; diff --git a/scripts/generate-builtin-terminfo.py b/scripts/generate-builtin-terminfo.py index 9c8807bf..c8d3be4b 100755 --- a/scripts/generate-builtin-terminfo.py +++ b/scripts/generate-builtin-terminfo.py @@ -49,7 +49,19 @@ class IntCapability(Capability): class StringCapability(Capability): - pass + def __init__(self, name: str, value: str): + # Expand \E to literal ESC in non-parameterized capabilities + if '%' not in value: + value = re.sub(r'\\E([0-7])', r'\\033" "\1', value) + value = re.sub(r'\\E', r'\\033', value) + else: + # Need to double-escape \E in C string literals + value = value.replace('\\E', '\\\\E') + + # Don’t escape ‘:’ + value = value.replace('\\:', ':') + + super().__init__(name, value) class Fragment: @@ -156,48 +168,27 @@ def main(): entry.add_capability(StringCapability('TN', target_entry_name)) entry.add_capability(IntCapability('RGB', 8)) # 8 bits per channel - target.write('#pragma once\n') - target.write('\n') - # target.write('enum terminfo_capability_type {\n') - # target.write(' TERMINFO_CAP_BOOL,\n') - # target.write(' TERMINFO_CAP_INT,\n') - # target.write(' TERMINFO_CAP_STRING,\n') - # target.write('};\n') - # target.write('\n') - target.write('struct foot_terminfo_entry {\n') - target.write(' const char *name;\n') - # target.write(' enum terminfo_capability_type type;\n') - target.write(' const char *value;\n') - target.write('};\n') - target.write('\n') - target.write( - 'static const struct foot_terminfo_entry terminfo_capabilities[] = {\n') - + terminfo_parts = [] for cap in sorted(entry.caps.values()): name = cap.name value = str(cap.value) - # Expand \E to literal ESC in non-parameterized capabilities - if '%' not in value: - value = re.sub(r'\\E([0-7])', r'\\033" "\1', value) - value = re.sub(r'\\E', r'\\033', value) - else: - # Need to double-escape \E in C string literals - value = value.replace('\\E', '\\\\E') - - # Don’t escape ‘:’ - value = value.replace('\\:', ':') - - # Do escape ‘“‘ + # Escape ‘“‘ name = name.replace('"', '\"') value = value.replace('"', '\"') + terminfo_parts.append(name) if isinstance(cap, BoolCapability): - target.write(f' {{"{name}", NULL}},\n') + terminfo_parts.append('') else: - target.write(f' {{"{name}", "{value}"}},\n') + terminfo_parts.append(value) + + terminfo = '\\0" "'.join(terminfo_parts) - target.write('};\n') + target.write('#pragma once\n') + target.write('\n') + target.write(f'static const char terminfo_capabilities[] = "{terminfo}";') + target.write('\n') if __name__ == '__main__': -- cgit v1.3