This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch tymux-10-quiet-head
in repository terminology.
View the commit online.
commit d9ff5f2d164f2ee1fb98edb16395b631d2b2243f
Author: Boris Faure <[email protected]>
AuthorDate: Sat Oct 31 16:30:57 2026 +0000
termptynotify: show OSC 99 notifications
Implement the core of kitty's desktop notification protocol
(https://sw.kovidgoyal.net/kitty/desktop-notifications/): title and
body, chunking with d=0, base64 with e=1, u= and w=. Unlike kitty,
pending chunks are kept per i= in a list capped at 8, so that two panes
behind a multiplexer can interleave them. The code lives in a new
termptynotify.c to keep termptyesc.c from growing.
Text is sanitized once, when shown, so that a UTF-8 character split
across chunks survives, and no control character or invalid UTF-8
(which libdbus rejects) reaches the server. \n is kept in the body
only, \t becomes a space. OSC 777 goes through the same helper.
---
DESIGN.md | 1 +
src/bin/meson.build | 4 +
src/bin/termpty.c | 30 +++
src/bin/termpty.h | 2 +
src/bin/termptyesc.c | 16 +-
src/bin/termptynotify.c | 635 ++++++++++++++++++++++++++++++++++++++++++++++++
src/bin/termptynotify.h | 23 ++
src/bin/tytest.c | 3 +
src/bin/unit_tests.h | 3 +
tests/osc-99-chunks.sh | 55 +++++
tests/osc-99-notify.sh | 29 +++
tests/tests.results | 2 +
12 files changed, 801 insertions(+), 2 deletions(-)
diff --git a/DESIGN.md b/DESIGN.md
index be57db4b..1da2378b 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -32,6 +32,7 @@ A simple documentation to navigate through the C files:
* `src/bin/termptydbl.c`: code to hande double-width characters
* `src/bin/termptyesc.c`: escape codes parsing
* `src/bin/termptyext.c`: extented terminology escape handling
+* `src/bin/termptynotify.c`: desktop notification escape codes (OSC 99)
* `src/bin/termptygfx.c`: charset translations
* `src/bin/termptyops.c`: handling history
* `src/bin/tyalpha.c`: the `tyalpha` tool
diff --git a/src/bin/meson.build b/src/bin/meson.build
index 3e0d0ba1..db9f0ad0 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -46,6 +46,7 @@ terminology_sources = ['private.h',
'termptyesc.c', 'termptyesc.h',
'termptyops.c', 'termptyops.h',
'termptygfx.c', 'termptygfx.h',
+ 'termptynotify.c', 'termptynotify.h',
'termptyext.c', 'termptyext.h',
'backlog.c', 'backlog.h',
'md5.c', 'md5.h',
@@ -72,6 +73,7 @@ tyfuzz_sources = [color_names,
'backlog.c', 'backlog.h',
'termptyops.c', 'termptyops.h',
'termptydbl.c', 'termptydbl.h',
+ 'termptynotify.c', 'termptynotify.h',
'termptytcap.h',
'termptyext.c', 'termptyext.h',
'termptygfx.c', 'termptygfx.h',
@@ -93,6 +95,7 @@ tytest_sources = [color_names,
'backlog.c', 'backlog.h',
'termptyops.c', 'termptyops.h',
'termptydbl.c', 'termptydbl.h',
+ 'termptynotify.c', 'termptynotify.h',
'termptytcap.h',
'termptyext.c', 'termptyext.h',
'termptygfx.c', 'termptygfx.h',
@@ -120,6 +123,7 @@ tybench_sources = [color_names,
'backlog.c', 'backlog.h',
'termptyops.c', 'termptyops.h',
'termptydbl.c', 'termptydbl.h',
+ 'termptynotify.c', 'termptynotify.h',
'termptytcap.h',
'termptyext.c', 'termptyext.h',
'termptygfx.c', 'termptygfx.h',
diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 81d6c6e3..1b41dfd8 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -5,6 +5,7 @@
#include <Ecore_IMF_Evas.h>
#include "termpty.h"
#include "termptyesc.h"
+#include "termptynotify.h"
#include "termptyops.h"
#include "backlog.h"
#include "utf8.h"
@@ -840,6 +841,7 @@ termpty_free(Termpty *ty)
Termexp *ex;
termpty_save_unregister(ty);
+ termpty_notify_free(ty);
free(ty->osc52_long.data);
EINA_LIST_FREE(ty->block.expecting, ex) free(ex);
if (ty->block.blocks) eina_hash_free(ty->block.blocks);
@@ -2059,6 +2061,7 @@ static void
_ty_test_shutdown(Termpty *ty)
{
termpty_sync_output_reset(ty);
+ termpty_notify_free(ty);
termpty_backlog_free(ty);
free(ty->screen);
free(ty->screen2);
@@ -2706,6 +2709,33 @@ tytest_notify_gated(void)
_ty_test_shutdown(&ty);
return 0;
}
+
+/* A flood of d=0 chunks with distinct ids only recycles entries: making
+ * a new pending notification past NOTIFY_PENDING_MAX evicts the oldest. */
+int
+tytest_notify_pending_cap(void)
+{
+ Termpty ty;
+ char seq[64];
+ int i;
+
+ _ty_test_init(&ty, 80, 24);
+
+ for (i = 0; i < NOTIFY_PENDING_MAX + 1; i++)
+ {
+ snprintf(seq, sizeof(seq), "\x1b]99;i=p%d:d=0;chunk %d\x1b\\", i, i);
+ _ty_feed(&ty, seq);
+ }
+ assert(eina_list_count(ty.notify_pending) == NOTIFY_PENDING_MAX);
+ /* the oldest, p0, was evicted: completing it now shows on its own */
+ tytest_notifications_clear();
+ _ty_feed(&ty, "\x1b]99;i=p0;done\x1b\\");
+ assert(strstr(tytest_notifications_get(), "chunk 0") == NULL);
+ assert(strstr(tytest_notifications_get(), "done") != NULL);
+
+ _ty_test_shutdown(&ty);
+ return 0;
+}
#endif
/* DECRQSS: an unsupported request is answered "invalid", DECSCL keeps its
diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index 56f02146..b1ac1994 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -278,6 +278,8 @@ struct tag_Termpty
uint32_t size;
} hl;
TitleIconElem *title_icon_stack;
+ /* OSC 99 notifications still waiting for their last chunk, oldest first */
+ Eina_List *notify_pending;
};
struct tag_Termcell
diff --git a/src/bin/termptyesc.c b/src/bin/termptyesc.c
index e7deaf0f..2994452f 100644
--- a/src/bin/termptyesc.c
+++ b/src/bin/termptyesc.c
@@ -10,6 +10,7 @@
#include "termptyesc.h"
#include "termptyops.h"
#include "termptyext.h"
+#include "termptynotify.h"
#include "termptytcap.h"
#include "simd/simd.h"
#include "theme.h"
@@ -4662,8 +4663,8 @@ _handle_xterm_777_command(Termpty *ty,
*title_end = '\0';
message = title_end + 1;
- termio_notification_send(ty->obj, title, message,
- ELM_SYS_NOTIFY_URGENCY_NORMAL, -1);
+ termpty_notify_show(ty, title, message,
+ ELM_SYS_NOTIFY_URGENCY_NORMAL, -1);
*cmd_end = ';';
*title_end = ';';
}
@@ -5526,6 +5527,17 @@ _handle_esc_osc(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
free(s);
}
break;
+ case 99:
+ DBG("kitty desktop notification");
+ if (!p)
+ goto err;
+ s = eina_unicode_unicode_to_utf8(p, &len);
+ if (s)
+ {
+ termpty_notify_osc99(ty, s, len, bel);
+ free(s);
+ }
+ break;
default:
// many others
WRN("unhandled xterm esc %d", arg);
diff --git a/src/bin/termptynotify.c b/src/bin/termptynotify.c
new file mode 100644
index 00000000..f49d8506
--- /dev/null
+++ b/src/bin/termptynotify.c
@@ -0,0 +1,635 @@
+#include "private.h"
+#include <Elementary.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "termio.h"
+#include "termpty.h"
+#include "termptynotify.h"
+#include "utf8.h"
+#include "utils.h"
+
+#if defined(BINARY_TYTEST)
+#include "tytest.h"
+#endif
+
+/* What one OSC 99 says about itself */
+typedef enum _Notify_Payload
+{
+ NOTIFY_PAYLOAD_TITLE,
+ NOTIFY_PAYLOAD_BODY,
+ NOTIFY_PAYLOAD_QUERY,
+ NOTIFY_PAYLOAD_CLOSE,
+ NOTIFY_PAYLOAD_ALIVE,
+ NOTIFY_PAYLOAD_UNKNOWN,
+} Notify_Payload;
+
+typedef enum _Notify_Occasion
+{
+ NOTIFY_OCCASION_UNSET = -1,
+ NOTIFY_OCCASION_ALWAYS,
+ NOTIFY_OCCASION_UNFOCUSED,
+ NOTIFY_OCCASION_INVISIBLE,
+} Notify_Occasion;
+
+/* What one OSC 99 said, parsed */
+typedef struct _Notify_Meta
+{
+ char id[NOTIFY_ID_MAX + 1];
+ Notify_Payload payload;
+ Notify_Occasion occasion;
+ int timeout_ms; /* -2 when unset, as kitty */
+ int urgency; /* -1 when unset */
+ Eina_Bool has_id;
+ Eina_Bool done;
+ Eina_Bool base64;
+} Notify_Meta;
+
+/* A notification whose chunks are still arriving */
+typedef struct _Notify_Pending
+{
+ char id[NOTIFY_ID_MAX + 1];
+ Eina_Strbuf *title, *body;
+ Ty_Base64_Stream b64;
+ Notify_Payload b64_payload;
+ Notify_Occasion occasion;
+ int timeout_ms;
+ int urgency;
+ Eina_Bool has_id;
+} Notify_Pending;
+
+static Eina_Bool
+_notify_id_char_valid(char c)
+{
+ return ((c >= 'a') && (c <= 'z')) ||
+ ((c >= 'A') && (c <= 'Z')) ||
+ ((c >= '0') && (c <= '9')) ||
+ (c == '_') || (c == '+') || (c == '.') ||
+ (c == '-') ;
+}
+
+static Eina_Bool
+_notify_id_is_valid(const char *id, size_t len)
+{
+ size_t i;
+
+ if ((len == 0) || (len > NOTIFY_ID_MAX))
+ return EINA_FALSE;
+ for (i = 0; i < len; i++)
+ {
+ if (!_notify_id_char_valid(id[i]))
+ return EINA_FALSE;
+ }
+ return EINA_TRUE;
+}
+
+static void
+_notify_meta_parse(const char *s, size_t len, Notify_Meta *meta)
+{
+ size_t pos = 0;
+
+ while (pos < len)
+ {
+ const char *part = s + pos;
+ const char *end = memchr(part, ':', len - pos);
+ size_t part_len = end ? (size_t)(end - part) : (len - pos);
+ const char *value;
+ size_t value_len;
+
+ pos += part_len + 1;
+
+ /* a part is used only if it is at least two bytes long and its
+ * second byte is '='; anything else is skipped, as in foot */
+ if ((part_len < 2) || (part[1] != '='))
+ continue;
+ value = part + 2;
+ value_len = part_len - 2;
+
+ switch (part[0])
+ {
+ case 'i':
+ meta->has_id = _notify_id_is_valid(value, value_len);
+ if (meta->has_id)
+ memcpy(meta->id, value, value_len), meta->id[value_len] = '\0';
+ break;
+ case 'p':
+ if ((value_len == 5) && !strncmp(value, "title", 5))
+ meta->payload = NOTIFY_PAYLOAD_TITLE;
+ else if ((value_len == 4) && !strncmp(value, "body", 4))
+ meta->payload = NOTIFY_PAYLOAD_BODY;
+ else if ((value_len == 1) && (value[0] == '?'))
+ meta->payload = NOTIFY_PAYLOAD_QUERY;
+ else if ((value_len == 5) && !strncmp(value, "close", 5))
+ meta->payload = NOTIFY_PAYLOAD_CLOSE;
+ else if ((value_len == 5) && !strncmp(value, "alive", 5))
+ meta->payload = NOTIFY_PAYLOAD_ALIVE;
+ else
+ meta->payload = NOTIFY_PAYLOAD_UNKNOWN;
+ break;
+ case 'd':
+ meta->done = !(value_len == 1 && value[0] == '0');
+ break;
+ case 'e':
+ meta->base64 = (value_len == 1 && value[0] == '1');
+ break;
+ case 'u':
+ meta->urgency = ((value_len == 1) && (value[0] >= '0') &&
+ (value[0] <= '2')) ? (value[0] - '0') : -1;
+ break;
+ case 'o':
+ if ((value_len == 6) && !strncmp(value, "always", 6))
+ meta->occasion = NOTIFY_OCCASION_ALWAYS;
+ else if ((value_len == 9) && !strncmp(value, "unfocused", 9))
+ meta->occasion = NOTIFY_OCCASION_UNFOCUSED;
+ else if ((value_len == 9) && !strncmp(value, "invisible", 9))
+ meta->occasion = NOTIFY_OCCASION_INVISIBLE;
+ else
+ meta->occasion = NOTIFY_OCCASION_UNSET;
+ break;
+ case 'w':
+ {
+ char buf[32];
+ char *endptr = NULL;
+ long long v;
+
+ if (value_len >= sizeof(buf))
+ break;
+ memcpy(buf, value, value_len);
+ buf[value_len] = '\0';
+ errno = 0;
+ v = strtoll(buf, &endptr, 10);
+ if (errno || !endptr || *endptr || (v > INT_MAX))
+ break;
+ if (v < -1)
+ v = -1;
+ meta->timeout_ms = (int)v;
+ break;
+ }
+ default:
+ /* a, c, f, g, n, s, t and unknown keys are ignored */
+ break;
+ }
+ }
+}
+
+/* Sanitize UTF-8 into dst, capped at NOTIFY_TEXT_MAX; \n kept if multiline */
+static void
+_notify_text_append(Eina_Strbuf *dst, const char *src, size_t len,
+ Eina_Bool multiline)
+{
+ int i = 0;
+
+ while (i < (int)len)
+ {
+ Eina_Unicode g;
+ char buf[8];
+ int char_len;
+
+ if (src[i])
+ g = eina_unicode_utf8_next_get(src, &i);
+ else
+ {
+ /* step over an embedded NUL by hand */
+ g = 0;
+ i++;
+ }
+ /* a dropped \t or \n joins words; termio renders a body \n <br/> */
+ if ((g == '\t') || ((g == '\n') && !multiline))
+ g = ' ';
+ else if (((g < 0x20) && (g != '\n')) || (g == 0x7f) ||
+ ((g >= 0x80) && (g <= 0x9f)) ||
+ ((g >= 0xd800) && (g <= 0xdfff)) ||
+ (g > 0x10ffff))
+ continue;
+ char_len = codepoint_to_utf8(g, buf);
+ if (eina_strbuf_length_get(dst) + char_len > NOTIFY_TEXT_MAX)
+ break;
+ eina_strbuf_append_length(dst, buf, char_len);
+ }
+}
+
+static Eina_Bool
+_notify_pending_matches(const Notify_Pending *pending, const Notify_Meta *meta)
+{
+ if (pending->has_id != meta->has_id)
+ return EINA_FALSE;
+ if (!meta->has_id)
+ return EINA_TRUE;
+ return !strcmp(pending->id, meta->id);
+}
+
+static Notify_Pending *
+_notify_pending_find(Termpty *ty, const Notify_Meta *meta)
+{
+ const Eina_List *l;
+ Notify_Pending *pending;
+
+ EINA_LIST_FOREACH(ty->notify_pending, l, pending)
+ {
+ if (_notify_pending_matches(pending, meta))
+ return pending;
+ }
+ return NULL;
+}
+
+static Notify_Pending *
+_notify_pending_new(Termpty *ty, const Notify_Meta *meta)
+{
+ Notify_Pending *pending;
+
+ pending = calloc(1, sizeof(Notify_Pending));
+ if (!pending)
+ return NULL;
+ pending->title = eina_strbuf_new();
+ pending->body = eina_strbuf_new();
+ if (!pending->title || !pending->body)
+ {
+ eina_strbuf_free(pending->title);
+ eina_strbuf_free(pending->body);
+ free(pending);
+ return NULL;
+ }
+ pending->has_id = meta->has_id;
+ if (meta->has_id)
+ strcpy(pending->id, meta->id);
+ pending->b64_payload = NOTIFY_PAYLOAD_UNKNOWN;
+ pending->occasion = NOTIFY_OCCASION_UNSET;
+ pending->timeout_ms = -2;
+ pending->urgency = -1;
+
+ if (eina_list_count(ty->notify_pending) >= NOTIFY_PENDING_MAX)
+ {
+ Notify_Pending *oldest = eina_list_data_get(ty->notify_pending);
+
+ ty->notify_pending = eina_list_remove(ty->notify_pending, oldest);
+ eina_strbuf_free(oldest->title);
+ eina_strbuf_free(oldest->body);
+ free(oldest);
+ }
+ ty->notify_pending = eina_list_append(ty->notify_pending, pending);
+ return pending;
+}
+
+static void
+_notify_pending_del(Termpty *ty, Notify_Pending *pending)
+{
+ ty->notify_pending = eina_list_remove(ty->notify_pending, pending);
+ eina_strbuf_free(pending->title);
+ eina_strbuf_free(pending->body);
+ free(pending);
+}
+
+/* Append raw payload bytes, dropping anything past NOTIFY_TEXT_MAX */
+static void
+_notify_raw_append(Eina_Strbuf *dst, const char *src, size_t len)
+{
+ size_t used = (size_t)eina_strbuf_length_get(dst);
+
+ if (used >= NOTIFY_TEXT_MAX)
+ return;
+ if (len > NOTIFY_TEXT_MAX - used)
+ len = NOTIFY_TEXT_MAX - used;
+ eina_strbuf_append_length(dst, src, len);
+}
+
+static void
+_notify_payload_append(Notify_Pending *pending, const Notify_Meta *meta,
+ const char *payload, size_t len)
+{
+ Eina_Strbuf *dst = (meta->payload == NOTIFY_PAYLOAD_TITLE) ?
+ pending->title : pending->body;
+
+ if (meta->base64)
+ {
+ Eina_Binbuf *decoded = eina_binbuf_new();
+ size_t len_decoded;
+ const char *decoded_s;
+
+ if (!decoded)
+ return;
+ if (pending->b64_payload != meta->payload)
+ memset(&pending->b64, 0, sizeof(pending->b64));
+ pending->b64_payload = meta->payload;
+ if (!ty_base64_stream_decode(&pending->b64, payload, len, decoded))
+ {
+ WRN("OSC 99: invalid base64 in payload, dropping it");
+ eina_binbuf_free(decoded);
+ return;
+ }
+ /* kept raw: a UTF-8 character may be split across two chunks */
+ decoded_s = (const char *)eina_binbuf_string_get(decoded);
+ len_decoded = eina_binbuf_length_get(decoded);
+ _notify_raw_append(dst, decoded_s, len_decoded);
+ eina_binbuf_free(decoded);
+ return;
+ }
+ _notify_raw_append(dst, payload, len);
+}
+
+static void
+_notify_pending_show(Termpty *ty, const Notify_Pending *pending)
+{
+ Eina_Strbuf *title, *body;
+ int urgency = pending->urgency;
+
+ title = eina_strbuf_new();
+ body = eina_strbuf_new();
+ if (!title || !body)
+ {
+ eina_strbuf_free(title);
+ eina_strbuf_free(body);
+ return;
+ }
+ /* the raw bytes of the chunks are sanitized once, here */
+ _notify_text_append(title, eina_strbuf_string_get(pending->title),
+ eina_strbuf_length_get(pending->title), EINA_FALSE);
+ _notify_text_append(body, eina_strbuf_string_get(pending->body),
+ eina_strbuf_length_get(pending->body), EINA_TRUE);
+ /* a notification with no title uses the body as title; one with no
+ * title and no body is ignored (kitty desktop-notifications.rst) */
+ if (eina_strbuf_length_get(title) == 0)
+ {
+ Eina_Strbuf *swap = title;
+
+ title = body;
+ body = swap;
+ }
+ if (eina_strbuf_length_get(title) == 0)
+ {
+ eina_strbuf_free(title);
+ eina_strbuf_free(body);
+ return;
+ }
+ if (urgency < 0)
+ urgency = ELM_SYS_NOTIFY_URGENCY_NORMAL;
+ termpty_notify_show(ty, eina_strbuf_string_get(title),
+ eina_strbuf_string_get(body), urgency,
+ pending->timeout_ms == -2 ? -1 : pending->timeout_ms);
+ eina_strbuf_free(title);
+ eina_strbuf_free(body);
+}
+
+void
+termpty_notify_show(Termpty *ty, const char *title, const char *body,
+ int urgency, int timeout_ms)
+{
+ Eina_Strbuf *title_sb, *body_sb;
+
+ if (!ty->config->notification_escapes)
+ return;
+
+ title_sb = eina_strbuf_new();
+ body_sb = eina_strbuf_new();
+ if (!title_sb || !body_sb)
+ {
+ eina_strbuf_free(title_sb);
+ eina_strbuf_free(body_sb);
+ return;
+ }
+ if (title)
+ _notify_text_append(title_sb, title, strlen(title), EINA_FALSE);
+ if (body)
+ _notify_text_append(body_sb, body, strlen(body), EINA_TRUE);
+ if (eina_strbuf_length_get(title_sb) == 0 &&
+ eina_strbuf_length_get(body_sb) == 0)
+ {
+ eina_strbuf_free(title_sb);
+ eina_strbuf_free(body_sb);
+ return;
+ }
+ termio_notification_send(ty->obj,
+ eina_strbuf_string_get(title_sb),
+ eina_strbuf_string_get(body_sb),
+ (Elm_Sys_Notify_Urgency)urgency, timeout_ms);
+ eina_strbuf_free(title_sb);
+ eina_strbuf_free(body_sb);
+}
+
+void
+termpty_notify_osc99(Termpty *ty, const char *s, size_t len,
+ Eina_Bool bel EINA_UNUSED)
+{
+ const char *sep;
+ Notify_Meta meta;
+ Notify_Pending *pending;
+
+ if (!ty->config->notification_escapes)
+ {
+ termpty_notify_free(ty);
+ return;
+ }
+ sep = memchr(s, ';', len);
+ if (!sep)
+ {
+ WRN("OSC 99: no payload separator");
+ ty->decoding_error = EINA_TRUE;
+ return;
+ }
+ memset(&meta, 0, sizeof(meta));
+ meta.payload = NOTIFY_PAYLOAD_TITLE;
+ meta.timeout_ms = -2;
+ meta.urgency = -1;
+ meta.done = EINA_TRUE;
+ meta.occasion = NOTIFY_OCCASION_UNSET;
+ _notify_meta_parse(s, sep - s, &meta);
+ sep++;
+ len -= (sep - s);
+
+ switch (meta.payload)
+ {
+ case NOTIFY_PAYLOAD_QUERY:
+ /* the capability query is answered in a later commit */
+ case NOTIFY_PAYLOAD_CLOSE:
+ case NOTIFY_PAYLOAD_ALIVE:
+ return;
+ default:
+ break;
+ }
+
+ pending = _notify_pending_find(ty, &meta);
+ if (!pending)
+ pending = _notify_pending_new(ty, &meta);
+ if (!pending)
+ return;
+ /* set keys override, unset keys keep the previous value */
+ if (meta.urgency >= 0)
+ pending->urgency = meta.urgency;
+ if (meta.occasion != NOTIFY_OCCASION_UNSET)
+ pending->occasion = meta.occasion;
+ if (meta.timeout_ms != -2)
+ pending->timeout_ms = meta.timeout_ms;
+ if ((meta.payload == NOTIFY_PAYLOAD_TITLE) ||
+ (meta.payload == NOTIFY_PAYLOAD_BODY))
+ _notify_payload_append(pending, &meta, sep, len);
+ if (meta.done)
+ {
+ _notify_pending_show(ty, pending);
+ _notify_pending_del(ty, pending);
+ }
+}
+
+void
+termpty_notify_free(Termpty *ty)
+{
+ Notify_Pending *pending;
+
+ EINA_LIST_FREE(ty->notify_pending, pending)
+ {
+ eina_strbuf_free(pending->title);
+ eina_strbuf_free(pending->body);
+ free(pending);
+ }
+}
+
+#if defined(BINARY_TYTEST)
+int
+tytest_notify_meta_parse(void)
+{
+ Notify_Meta meta;
+
+#define META_PARSE(s) memset(&meta, 0, sizeof(meta)); \
+ meta.payload = NOTIFY_PAYLOAD_TITLE; \
+ meta.timeout_ms = -2; \
+ meta.urgency = -1; \
+ meta.done = EINA_TRUE; \
+ meta.occasion = NOTIFY_OCCASION_UNSET; \
+ _notify_meta_parse(s, strlen(s), &meta)
+
+ /* defaults for an empty metadata */
+ META_PARSE("");
+ assert(meta.payload == NOTIFY_PAYLOAD_TITLE);
+ assert(meta.done);
+ assert(!meta.base64);
+ assert(meta.urgency == -1);
+ assert(meta.occasion == NOTIFY_OCCASION_UNSET);
+ assert(meta.timeout_ms == -2);
+ assert(!meta.has_id);
+
+ META_PARSE("i=abc:p=body");
+ assert(meta.has_id && !strcmp(meta.id, "abc"));
+ assert(meta.payload == NOTIFY_PAYLOAD_BODY);
+
+ META_PARSE("d=0");
+ assert(!meta.done);
+ META_PARSE("d=2");
+ assert(meta.done);
+ META_PARSE("e=1");
+ assert(meta.base64);
+ META_PARSE("e=2");
+ assert(!meta.base64);
+
+ META_PARSE("u=0");
+ assert(meta.urgency == 0);
+ META_PARSE("u=2");
+ assert(meta.urgency == 2);
+ META_PARSE("u=3");
+ assert(meta.urgency == -1);
+
+ META_PARSE("o=unfocused");
+ assert(meta.occasion == NOTIFY_OCCASION_UNFOCUSED);
+ META_PARSE("o=sometimes");
+ assert(meta.occasion == NOTIFY_OCCASION_UNSET);
+
+ META_PARSE("w=-5");
+ assert(meta.timeout_ms == -1);
+ META_PARSE("w=12x");
+ assert(meta.timeout_ms == -2);
+ META_PARSE("w=99999999999");
+ assert(meta.timeout_ms == -2);
+ META_PARSE("w=5000");
+ assert(meta.timeout_ms == 5000);
+
+ /* ids */
+ META_PARSE("i=");
+ assert(!meta.has_id);
+ META_PARSE("i=a/b");
+ assert(!meta.has_id);
+ {
+ char *long_id = malloc(NOTIFY_ID_MAX + 2);
+
+ memset(long_id, 'a', NOTIFY_ID_MAX + 1);
+ long_id[NOTIFY_ID_MAX + 1] = '\0';
+ META_PARSE("");
+ /* build "i=<long_id>" by hand */
+ {
+ char buf[NOTIFY_ID_MAX + 8];
+
+ snprintf(buf, sizeof(buf), "i=%s", long_id);
+ memset(&meta, 0, sizeof(meta));
+ _notify_meta_parse(buf, strlen(buf), &meta);
+ assert(!meta.has_id);
+ long_id[NOTIFY_ID_MAX] = '\0';
+ snprintf(buf, sizeof(buf), "i=%s", long_id);
+ memset(&meta, 0, sizeof(meta));
+ _notify_meta_parse(buf, strlen(buf), &meta);
+ assert(meta.has_id);
+ }
+ free(long_id);
+ }
+
+ META_PARSE("p=?");
+ assert(meta.payload == NOTIFY_PAYLOAD_QUERY);
+ META_PARSE("p=query");
+ assert(meta.payload == NOTIFY_PAYLOAD_UNKNOWN);
+ META_PARSE("p=close");
+ assert(meta.payload == NOTIFY_PAYLOAD_CLOSE);
+ META_PARSE("p=alive");
+ assert(meta.payload == NOTIFY_PAYLOAD_ALIVE);
+
+ /* parts that are not key=value are skipped */
+ META_PARSE("::x:=v:ab=c:i=ok");
+ assert(meta.has_id && !strcmp(meta.id, "ok"));
+
+ /* the last key wins */
+ META_PARSE("u=0:u=2");
+ assert(meta.urgency == 2);
+
+#undef META_PARSE
+ return 0;
+}
+
+int
+tytest_notify_text_append(void)
+{
+ Eina_Strbuf *sb = eina_strbuf_new();
+ size_t i;
+
+ /* C0, DEL and C1 are dropped */
+ eina_strbuf_reset(sb);
+ _notify_text_append(sb, "a\x01\x07\x7f\x1b\233b", 7, EINA_FALSE);
+ assert(!strcmp(eina_strbuf_string_get(sb), "ab"));
+
+ /* surrogates, which is how eina reports invalid bytes, are dropped */
+ eina_strbuf_reset(sb);
+ _notify_text_append(sb, "\x80\xff", 2, EINA_FALSE);
+ assert(eina_strbuf_length_get(sb) == 0);
+
+ /* a tab becomes a space: dropping it would join the words */
+ eina_strbuf_reset(sb);
+ _notify_text_append(sb, "tab\there", 8, EINA_TRUE);
+ assert(!strcmp(eina_strbuf_string_get(sb), "tab here"));
+
+ /* a newline is kept in the body: termio renders it as a line break */
+ eina_strbuf_reset(sb);
+ _notify_text_append(sb, "line1\nline2", 11, EINA_TRUE);
+ assert(!strcmp(eina_strbuf_string_get(sb), "line1\nline2"));
+
+ /* the title is a single line: a newline becomes a space, as a tab */
+ eina_strbuf_reset(sb);
+ _notify_text_append(sb, "line1\nline2", 11, EINA_FALSE);
+ assert(!strcmp(eina_strbuf_string_get(sb), "line1 line2"));
+
+ /* a 4-byte character that would cross NOTIFY_TEXT_MAX is not cut
+ * in half */
+ eina_strbuf_reset(sb);
+ for (i = 0; i < NOTIFY_TEXT_MAX / 3; i++)
+ _notify_text_append(sb, "\xe2\x9c\x93", 3, EINA_TRUE); /* U+2713 */
+ assert((size_t)eina_strbuf_length_get(sb) <= NOTIFY_TEXT_MAX);
+ assert((size_t)eina_strbuf_length_get(sb) % 3 == 0);
+
+ eina_strbuf_free(sb);
+ return 0;
+}
+#endif
diff --git a/src/bin/termptynotify.h b/src/bin/termptynotify.h
new file mode 100644
index 00000000..aef160d9
--- /dev/null
+++ b/src/bin/termptynotify.h
@@ -0,0 +1,23 @@
+#ifndef TERMINOLOGY_TERMPTY_NOTIFY_H_
+#define TERMINOLOGY_TERMPTY_NOTIFY_H_ 1
+
+/* Identifier and text limits; NOTIFY_PENDING_MAX is how many
+ * incomplete notifications a terminal buffers. */
+#define NOTIFY_ID_MAX 128
+#define NOTIFY_TEXT_MAX 1024
+#define NOTIFY_PENDING_MAX 8
+
+#include "termpty.h"
+
+/* Handle OSC 99;metadata;payload; bel is EINA_TRUE for a BEL terminator */
+void termpty_notify_osc99(Termpty *ty, const char *s, size_t len,
+ Eina_Bool bel);
+
+/* Sanitize (NOTIFY_TEXT_MAX) and show unless gated; -1 args mean unset */
+void termpty_notify_show(Termpty *ty, const char *title, const char *body,
+ int urgency, int timeout_ms);
+
+/* Drop the notifications still waiting for their last chunk */
+void termpty_notify_free(Termpty *ty);
+
+#endif
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index e726f3cf..f0510bac 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -74,6 +74,9 @@ static struct {
{ "xtgettcap", tytest_xtgettcap},
{ "tcap_table_sorted", tytest_tcap_table_sorted},
{ "notify_gated", tytest_notify_gated},
+ { "notify_meta_parse", tytest_notify_meta_parse},
+ { "notify_pending_cap", tytest_notify_pending_cap},
+ { "notify_text_append", tytest_notify_text_append},
{ "decrqss_invalid", tytest_decrqss_invalid},
{ "decrqss_sgr", tytest_decrqss_sgr},
{ "decrqss_settings", tytest_decrqss_settings},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 3de08c56..7c55ec44 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -48,6 +48,9 @@ int tytest_xmodkeys_reset(void);
int tytest_xtgettcap(void);
int tytest_tcap_table_sorted(void);
int tytest_notify_gated(void);
+int tytest_notify_meta_parse(void);
+int tytest_notify_pending_cap(void);
+int tytest_notify_text_append(void);
int tytest_decrqss_invalid(void);
int tytest_decrqss_sgr(void);
int tytest_decrqss_settings(void);
diff --git a/tests/osc-99-chunks.sh b/tests/osc-99-chunks.sh
new file mode 100755
index 00000000..ed4ea51a
--- /dev/null
+++ b/tests/osc-99-chunks.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+# fill space with E
+printf '\033#8'
+
+# two ids interleaved, as behind a multiplexer
+printf '\033]99;i=a:d=0;first \033\\'
+printf '\033]99;i=b:d=0;second \033\\'
+printf '\033]99;i=a:d=0;title\033\\'
+printf '\033]99;i=b;title\033\\'
+printf '\033]99;i=a;\033\\'
+# base64 cut after encoding, in the middle of a quantum
+printf '\033]99;i=c:e=1:d=0;SGVsbG\033\\'
+printf '\033]99;i=c:e=1;8gd29ybGQ=\033\\'
+# base64 cut before encoding, each chunk padded
+printf '\033]99;i=d:e=1:d=0;SGVs\033\\'
+printf '\033]99;i=d:e=1;bG8=\033\\'
+# unpadded last chunk
+printf '\033]99;e=1;SGVsbG8gd29ybGQ\033\\'
+# invalid base64: that chunk is dropped
+printf '\033]99;i=e:e=1:d=0;!!!!\033\\'
+printf '\033]99;i=e;fallback\033\\'
+# base64 of invalid UTF-8 and of a control character: dropped
+printf '\033]99;e=1;gP8=\033\\'
+printf '\033]99;e=1;YQdi\033\\'
+# a UTF-8 character split across two base64 chunks survives
+printf '\033]99;i=s:e=1:d=0;YcO\033\\'
+printf '\033]99;i=s:e=1;p\033\\'
+# a newline is kept in the body, becomes a space in the title
+printf '\033]99;i=n:p=title:e=1:d=0;bGluZTEKbGluZTI=\033\\'
+printf '\033]99;i=n:p=body:e=1;bGluZTEKbGluZTI=\033\\'
+# a tab in plain text becomes a space
+printf '\033]99;;tab\there\033\\'
+# a bad id is ignored: both chunks are id-less, and merge
+printf '\033]99;i=b$d:d=0;not\033\\'
+printf '\033]99;i=b$d;merged\033\\'
+# nine incomplete notifications: the first one is evicted
+for n in 1 2 3 4 5 6 7 8 9; do
+ printf '\033]99;i=p%s:d=0;pending %s\033\\' "$n" "$n"
+done
+printf '\033]99;i=p1;\033\\'
+printf '\033]99;i=p9;\033\\'
+# a title longer than the limit is cut at 1024 bytes, in three chunks
+printf '\033]99;i=t:d=0;'
+i=0
+while [ $i -lt 400 ]; do printf 'a'; i=$((i+1)); done
+printf '\033\\'
+printf '\033]99;i=t:d=0;'
+i=0
+while [ $i -lt 400 ]; do printf 'a'; i=$((i+1)); done
+printf '\033\\'
+printf '\033]99;i=t;'
+i=0
+while [ $i -lt 300 ]; do printf 'a'; i=$((i+1)); done
+printf '\033\\'
diff --git a/tests/osc-99-notify.sh b/tests/osc-99-notify.sh
new file mode 100755
index 00000000..cc2c6e07
--- /dev/null
+++ b/tests/osc-99-notify.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+# fill space with E
+printf '\033#8'
+
+# the simplest form: title only, no metadata
+printf '\033]99;;Hello world\033\\'
+# the spec's title then body
+printf '\033]99;i=1:d=0;Hello world\033\\'
+printf '\033]99;i=1:p=body;This is cool\033\\'
+# what opentui sends: base64 title held back, then the body
+printf '\033]99;i=opentui-1:p=title:e=1:d=0;T3BlbkNvZGU=\033\\'
+printf '\033]99;i=opentui-1:p=body:e=1:d=1;U2Vzc2lvbiBkb25l\033\\'
+# body only: becomes the title
+printf '\033]99;p=body;only a body\007'
+# urgency and expiry are forwarded
+printf '\033]99;u=2:w=5000;critical\033\\'
+# out of range urgency and timeout: defaults
+printf '\033]99;u=7:w=-9;defaults\033\\'
+# unknown key ignored, part without "=" skipped
+printf '\033]99;x=1:junk:i=2;still shown\033\\'
+# unknown payload type: payload ignored, nothing shown
+printf '\033]99;p=icon;ignored\033\\'
+# empty title and body: nothing shown
+printf '\033]99;;\033\\'
+# no second semicolon: malformed, nothing shown
+printf '\033]99;Hello\033\\'
+# C1 OSC and ST
+printf '\302\23599;;eight bit\302\234'
diff --git a/tests/tests.results b/tests/tests.results
index b37b32c9..cae12c46 100644
--- a/tests/tests.results
+++ b/tests/tests.results
@@ -144,6 +144,8 @@ osc-104.sh 773d4be48c4ef99cb5a34c59fe771e25
osc-4-reset.sh 76f6b1317c3ced32c16da58c2967efb5
osc-no-payload.sh 8aa7b12e298dfbeb5a48dcc48ea2f9a8
xterm-osc-777.sh 5fa189116a5358ed87aec7460f3785ad
+osc-99-chunks.sh 7b65134890f0cff0c1f06664a45135b2
+osc-99-notify.sh fd9e2e812e2a035c87971e141a0dd4db
osc-colors-ris.sh ae9b8008e2cb1fa2bae13a471c90733f
osc-110-112.sh 318a0f4da4b07567353e057bb2de5b2b
osc-colors-st.sh 84636d0d4589573e1e9b3c0fcceb2091
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.