From b55f6925d360dcb8109b90b266c0e9c2fc625b0c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 13 Feb 2021 12:41:57 +0100 Subject: osc: parse OSC-8, by calling term_osc8_{open,close} as appropriate --- osc.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'osc.c') diff --git a/osc.c b/osc.c index 801b04e9..2f1cfe2b 100644 --- a/osc.c +++ b/osc.c @@ -376,6 +376,45 @@ osc_set_pwd(struct terminal *term, char *string) free(host); } +static void +osc_uri(struct terminal *term, char *string) +{ + /* + * \E]8;;URI\e\\ + * + * Params are key=value pairs, separated by ‘:’. + * + * The only defined key (as of 2020-05-31) is ‘id’, which is used + * to group split-up URIs: + * + * ╔═ file1 ════╗ + * ║ ╔═ file2 ═══╗ + * ║http://exa║Lorem ipsum║ + * ║le.com ║ dolor sit ║ + * ║ ║amet, conse║ + * ╚══════════║ctetur adip║ + * ╚═══════════╝ + * + * This lets a terminal emulator highlight both parts at the same + * time (e.g. when hovering over one of the parts with the mouse). + */ + + const char *params = string; + char *params_end = strchr(params, ';'); + if (params_end == NULL) + return; + + *params_end = '\0'; + const char *uri = params_end + 1; + + LOG_DBG("params=%s, URI=%s", params, uri); + + if (uri[0] == '\0') + term_osc8_close(term); + else + term_osc8_open(term, uri); +} + static void osc_notify(struct terminal *term, char *string) { @@ -553,6 +592,10 @@ osc_dispatch(struct terminal *term) osc_set_pwd(term, string); break; + case 8: + osc_uri(term, string); + break; + case 10: case 11: { /* Set default foreground/background color */ -- cgit v1.3 From ecbfc2bbe9b1964c8b2563b52618393d63dc40ce Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 13 Feb 2021 13:41:27 +0100 Subject: osc: 8: parse params, and the ‘id’ parameter specifically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Applications can assign an ID to the URL. This is used to associate the parts of a split up URL with each other: ╔═ file1 ════╗ ║ ╔═ file2 ═══╗ ║http://exa║Lorem ipsum║ ║le.com ║ dolor sit ║ ║ ║amet, conse║ ╚══════════║ctetur adip║ ╚═══════════╝ In the example above, ‘http://exa’ and ‘le.com’ are part of the same URL, but by necessity split up into two OSC-8 URLs. If the application sets the same ID for those two OSC-8 URLs, the terminal can ensure they are handled as one. --- osc.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'osc.c') diff --git a/osc.c b/osc.c index 2f1cfe2b..fb4f15d6 100644 --- a/osc.c +++ b/osc.c @@ -1,5 +1,6 @@ #include "osc.h" +#include #include #include #include @@ -399,16 +400,36 @@ osc_uri(struct terminal *term, char *string) * time (e.g. when hovering over one of the parts with the mouse). */ - const char *params = string; + char *params = string; char *params_end = strchr(params, ';'); if (params_end == NULL) return; *params_end = '\0'; const char *uri = params_end + 1; + uint64_t id = (uint64_t)rand() << 32 | rand(); LOG_DBG("params=%s, URI=%s", params, uri); + char *ctx = NULL; + for (const char *key_value = strtok_r(params, ":", &ctx); + key_value != NULL; + key_value = strtok_r(NULL, ":", &ctx)) + { + const char *key = key_value; + char *operator = strchr(key_value, '='); + + if (operator == NULL) + continue; + *operator = '\0'; + + const char *value = operator + 1; + LOG_DBG("param: %s=%s", key, value); + + if (strcmp(key, "id") == 0) + id = (uintptr_t)value; + } + if (uri[0] == '\0') term_osc8_close(term); else -- cgit v1.3 From b934969b858d8874e480cc9ee7f4ccf5657f6f2a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 13 Feb 2021 13:44:07 +0100 Subject: term: add ‘id’ parameter to term_osc8_open() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current OSC-8 URL’s ID is now tracked along with the URI itself, and its starting point. --- osc.c | 2 +- terminal.c | 4 +++- terminal.h | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) (limited to 'osc.c') diff --git a/osc.c b/osc.c index fb4f15d6..b57bdd6c 100644 --- a/osc.c +++ b/osc.c @@ -433,7 +433,7 @@ osc_uri(struct terminal *term, char *string) if (uri[0] == '\0') term_osc8_close(term); else - term_osc8_open(term, uri); + term_osc8_open(term, id, uri); } static void diff --git a/terminal.c b/terminal.c index c6afbef1..218882f0 100644 --- a/terminal.c +++ b/terminal.c @@ -3016,7 +3016,7 @@ term_ime_set_cursor_rect(struct terminal *term, int x, int y, int width, } void -term_osc8_open(struct terminal *term, const char *uri) +term_osc8_open(struct terminal *term, uint64_t id, const char *uri) { if (unlikely(term->vt.osc8.begin.row < 0)) { /* It’s valid to switch from one URI to another without @@ -3028,6 +3028,7 @@ term_osc8_open(struct terminal *term, const char *uri) .col = term->grid->cursor.point.col, .row = grid_row_absolute(term->grid, term->grid->cursor.point.row), }; + term->vt.osc8.id = id; term->vt.osc8.uri = xstrdup(uri); } @@ -3066,6 +3067,7 @@ term_osc8_close(struct terminal *term) done: free(term->vt.osc8.uri); + term->vt.osc8.id = 0; term->vt.osc8.uri = NULL; term->vt.osc8.begin = (struct coord){-1, -1}; } diff --git a/terminal.h b/terminal.h index 6dc211d9..733fc92d 100644 --- a/terminal.h +++ b/terminal.h @@ -169,8 +169,9 @@ struct vt { /* Start coordinate for current OSC-8 URI */ struct { - struct coord begin; + uint64_t id; char *uri; + struct coord begin; } osc8; struct { @@ -690,5 +691,5 @@ void term_ime_set_cursor_rect( void term_urls_reset(struct terminal *term); void term_collect_urls(struct terminal *term); -void term_osc8_open(struct terminal *term, const char *uri); +void term_osc8_open(struct terminal *term, uint64_t id, const char *uri); void term_osc8_close(struct terminal *term); -- cgit v1.3 From d44cda11bf435b7687fada29946d4a1cecbc728c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 13:28:42 +0100 Subject: osc-8: fix thinko: can’t use the ID parameters *address* as actual ID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Doing so means the next OSC-8 URL emitted by the client application, with the same ID, will get a *new* id internally. Instead, hash the string and use that as ID. --- osc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'osc.c') diff --git a/osc.c b/osc.c index b57bdd6c..bf97db52 100644 --- a/osc.c +++ b/osc.c @@ -16,6 +16,7 @@ #include "selection.h" #include "terminal.h" #include "uri.h" +#include "util.h" #include "vt.h" #include "xmalloc.h" #include "xsnprintf.h" @@ -409,8 +410,6 @@ osc_uri(struct terminal *term, char *string) const char *uri = params_end + 1; uint64_t id = (uint64_t)rand() << 32 | rand(); - LOG_DBG("params=%s, URI=%s", params, uri); - char *ctx = NULL; for (const char *key_value = strtok_r(params, ":", &ctx); key_value != NULL; @@ -424,12 +423,13 @@ osc_uri(struct terminal *term, char *string) *operator = '\0'; const char *value = operator + 1; - LOG_DBG("param: %s=%s", key, value); if (strcmp(key, "id") == 0) - id = (uintptr_t)value; + id = sdbm_hash(value); } + LOG_DBG("OSC-8: URL=%s, id=%" PRIu64, uri, id); + if (uri[0] == '\0') term_osc8_close(term); else -- cgit v1.3