Codebergdnkl

foot

master
ply5 git calls514 ms40 KiB3 cachedprotocol v2
1722 lines48 KiBRaw
1#include "osc.h"
3#include <stdlib.h>
4#include <string.h>
5#include <ctype.h>
6#include <errno.h>
8#include <sys/epoll.h>
10#define LOG_MODULE "osc"
11#define LOG_ENABLE_DBG 0
12#include "log.h"
13#include "base64.h"
14#include "config.h"
15#include "macros.h"
16#include "notify.h"
17#include "selection.h"
18#include "terminal.h"
19#include "uri.h"
20#include "util.h"
21#include "xmalloc.h"
22#include "xsnprintf.h"
24#define UNHANDLED() LOG_DBG("unhandled: OSC: %.*s", (int)term->vt.osc.idx, term->vt.osc.data)
26static void
27osc_to_clipboard(struct terminal *term, const char *target,
28 const char *base64_data)
30 bool to_clipboard = false;
31 bool to_primary = false;
33 if (target[0] == '\0')
34 to_clipboard = true;
36 for (const char *t = target; *t != '\0'; t++) {
37 switch (*t) {
38 case 'c':
39 to_clipboard = true;
40 break;
42 case 's':
43 case 'p':
44 to_primary = true;
45 break;
47 default:
48 LOG_WARN("unimplemented: clipboard target '%c'", *t);
49 break;
50 }
51 }
53 /* Find a seat in which the terminal has focus */
54 struct seat *seat = NULL;
55 tll_foreach(term->wl->seats, it) {
56 if (it->item.kbd_focus == term) {
57 seat = &it->item;
58 break;
59 }
60 }
62 if (seat == NULL) {
63 LOG_WARN("OSC52: client tried to write to clipboard data while window was unfocused");
64 return;
65 }
67 const bool copy_allowed = term->conf->security.osc52 == OSC52_ENABLED
68 || term->conf->security.osc52 == OSC52_COPY_ENABLED;
70 if (!copy_allowed) {
71 LOG_DBG("ignoring copy request: disabled in configuration");
72 return;
73 }
75 char *decoded = base64_decode(base64_data, NULL);
76 if (decoded == NULL || decoded[0] == '\0') {
77 if (decoded == NULL) {
78 if (errno == EINVAL)
79 LOG_WARN("OSC: invalid clipboard data: %s", base64_data);
80 else
81 LOG_ERRNO("base64_decode() failed");
82 }
84 if (to_clipboard)
85 selection_clipboard_unset(seat);
86 if (to_primary)
87 selection_primary_unset(seat);
88 free(decoded);
89 return;
90 }
92 LOG_DBG("decoded: %s", decoded);
94 if (to_clipboard) {
95 char *copy = xstrdup(decoded);
96 if (!text_to_clipboard(seat, term, copy, seat->kbd.serial))
97 free(copy);
98 }
100 if (to_primary) {
101 char *copy = xstrdup(decoded);
102 if (!text_to_primary(seat, term, copy, seat->kbd.serial))
103 free(copy);
104 }
106 free(decoded);
109struct clip_context {
110 struct seat *seat;
111 struct terminal *term;
112 uint8_t buf[3];
113 int idx;
114};
116static void
117from_clipboard_cb(char *text, size_t size, void *user)
119 struct clip_context *ctx = user;
120 struct terminal *term = ctx->term;
122 xassert(ctx->idx >= 0 && ctx->idx <= 2);
124 const char *t = text;
125 size_t left = size;
127 if (ctx->idx > 0) {
128 for (size_t i = ctx->idx; i < 3 && left > 0; i++, t++, left--)
129 ctx->buf[ctx->idx++] = *t;
131 xassert(ctx->idx <= 3);
132 if (ctx->idx == 3) {
133 char *chunk = base64_encode(ctx->buf, 3);
134 xassert(chunk != NULL);
135 xassert(strlen(chunk) == 4);
137 term_paste_data_to_slave(term, chunk, 4);
138 free(chunk);
140 ctx->idx = 0;
141 }
142 }
144 if (left == 0)
145 return;
147 xassert(ctx->idx == 0);
149 int remaining = left % 3;
150 for (int i = remaining; i > 0; i--)
151 ctx->buf[ctx->idx++] = text[size - i];
152 xassert(ctx->idx == remaining);
154 char *chunk = base64_encode((const uint8_t *)t, left / 3 * 3);
155 xassert(chunk != NULL);
156 xassert(strlen(chunk) % 4 == 0);
157 term_paste_data_to_slave(term, chunk, strlen(chunk));
158 free(chunk);
161static void
162from_clipboard_done(void *user)
164 struct clip_context *ctx = user;
165 struct terminal *term = ctx->term;
167 if (ctx->idx > 0) {
168 char res[4];
169 base64_encode_final(ctx->buf, ctx->idx, res);
170 term_paste_data_to_slave(term, res, 4);
171 }
173 if (term->vt.osc.bel)
174 term_paste_data_to_slave(term, "\a", 1);
175 else
176 term_paste_data_to_slave(term, "\033\\", 2);
178 term->is_sending_paste_data = false;
180 /* Make sure we send any queued up non-paste data */
181 if (tll_length(term->ptmx_buffers) > 0)
182 fdm_event_add(term->fdm, term->ptmx, EPOLLOUT);
184 free(ctx);
187static void
188osc_from_clipboard(struct terminal *term, const char *source)
190 /* Find a seat in which the terminal has focus */
191 struct seat *seat = NULL;
192 tll_foreach(term->wl->seats, it) {
193 if (it->item.kbd_focus == term) {
194 seat = &it->item;
195 break;
196 }
197 }
199 if (seat == NULL) {
200 LOG_WARN("OSC52: client tried to read clipboard data while window was unfocused");
201 return;
202 }
204 const bool paste_allowed = term->conf->security.osc52 == OSC52_ENABLED
205 || term->conf->security.osc52 == OSC52_PASTE_ENABLED;
206 if (!paste_allowed) {
207 LOG_DBG("ignoring paste request: disabled in configuration");
208 return;
209 }
211 /* Use clipboard if no source has been specified */
212 char src = 'c';
213 for (char const *s = source; *s != '\0'; ++s) {
214 src = *s;
216 if (src == 'c') {
217 if (selection_clipboard_has_data(seat))
218 break;
219 } else if (src == 'p' || src == 's') {
220 if (selection_primary_has_data(seat))
221 break;
222 } else
223 LOG_WARN("unimplemented: clipboard source '%c'", src);
224 }
226 if (term->is_sending_paste_data) {
227 /* FIXME: we should wait for the paste to end, then continue
228 with the OSC-52 reply */
229 term_to_slave(term, "\033]52;", 5);
230 term_to_slave(term, &src, 1);
231 term_to_slave(term, ";", 1);
232 if (term->vt.osc.bel)
233 term_to_slave(term, "\a", 1);
234 else
235 term_to_slave(term, "\033\\", 2);
236 return;
237 }
239 term->is_sending_paste_data = true;
241 term_paste_data_to_slave(term, "\033]52;", 5);
242 term_paste_data_to_slave(term, &src, 1);
243 term_paste_data_to_slave(term, ";", 1);
245 struct clip_context *ctx = xmalloc(sizeof(*ctx));
246 *ctx = (struct clip_context) {.seat = seat, .term = term};
248 if (src == 'c') {
249 text_from_clipboard(
250 seat, term, true, &from_clipboard_cb, &from_clipboard_done, ctx);
251 } else if (src == 'p' || src == 's') {
252 text_from_primary(
253 seat, term, true, &from_clipboard_cb, &from_clipboard_done, ctx);
254 } else {
255 /* Send empty reply for unsupported source */
256 from_clipboard_done(ctx);
257 }
260static void
261osc_selection(struct terminal *term, char *string)
263 char *p = string;
264 bool clipboard_done = false;
266 /* The first parameter is a string of clipbard sources/targets */
267 while (*p != '\0' && !clipboard_done) {
268 switch (*p) {
269 case ';':
270 clipboard_done = true;
271 *p = '\0';
272 break;
273 }
275 p++;
276 }
278 LOG_DBG("clipboard: target = %s data = %s", string, p);
280 if (p[0] == '?' && p[1] == '\0')
281 osc_from_clipboard(term, string);
282 else
283 osc_to_clipboard(term, string, p);
286static void
287osc_flash(struct terminal *term)
289 /* Our own private - flash */
290 term_flash(term, 50);
293static bool
294parse_legacy_color(const char *string, uint32_t *color, bool *_have_alpha,
295 uint16_t *_alpha)
297 bool have_alpha = false;
298 uint16_t alpha = 0xffff;
300 if (string[0] == '[') {
301 /* e.g. \E]11;[50]#00ff00 */
302 const char *start = &string[1];
304 errno = 0;
305 char *end;
306 unsigned long percent = strtoul(start, &end, 10);
308 if (errno != 0 || *end != ']')
309 return false;
311 have_alpha = true;
312 alpha = (0xffff * min(percent, 100) + 50) / 100;
314 string = end + 1;
315 }
317 if (string[0] != '#')
318 return false;
320 string++;
321 const size_t len = strlen(string);
323 if (len % 3 != 0)
324 return false;
326 const int digits = len / 3;
328 int rgb[3];
329 for (size_t i = 0; i < 3; i++) {
330 rgb[i] = 0;
331 for (size_t j = 0; j < digits; j++) {
332 size_t idx = i * digits + j;
333 char c = string[idx];
334 rgb[i] <<= 4;
336 if (!isxdigit(c))
337 rgb[i] |= 0;
338 else
339 rgb[i] |= c >= '0' && c <= '9' ? c - '0' :
340 c >= 'a' && c <= 'f' ? c - 'a' + 10 : c - 'A' + 10;
341 }
343 /* Values with less than 16 bits represent the *most
344 * significant bits*. I.e. the values are *not* scaled */
345 rgb[i] <<= 16 - (4 * digits);
346 }
348 /* Re-scale to 8-bit */
349 uint8_t r = 256 * (rgb[0] / 65536.);
350 uint8_t g = 256 * (rgb[1] / 65536.);
351 uint8_t b = 256 * (rgb[2] / 65536.);
353 LOG_DBG("legacy: %02x%02x%02x (alpha=%04x)", r, g, b,
354 have_alpha ? alpha : 0xffff);
356 *color = r << 16 | g << 8 | b;
358 if (_have_alpha != NULL)
359 *_have_alpha = have_alpha;
360 if (_alpha != NULL)
361 *_alpha = alpha;
362 return true;
365static bool
366parse_rgb(const char *string, uint32_t *color, bool *_have_alpha,
367 uint16_t *_alpha)
369 size_t len = strlen(string);
370 bool have_alpha = len >= 4 && strncmp(string, "rgba", 4) == 0;
372 /* Verify we have the minimum required length (for "") */
373 if (have_alpha) {
374 if (len < STRLEN("rgba:x/x/x/x"))
375 return false;
376 } else {
377 if (len < STRLEN("rgb:x/x/x"))
378 return false;
379 }
381 /* Verify prefix is "rgb:" or "rgba:" */
382 if (have_alpha) {
383 if (strncmp(string, "rgba:", 5) != 0)
384 return false;
385 string += 5;
386 len -= 5;
387 } else {
388 if (strncmp(string, "rgb:", 4) != 0)
389 return false;
390 string += 4;
391 len -= 4;
392 }
394 int rgb[4];
395 int digits[4];
397 for (size_t i = 0; i < (have_alpha ? 4 : 3); i++) {
398 for (rgb[i] = 0, digits[i] = 0;
399 len > 0 && *string != '/';
400 len--, string++, digits[i]++)
401 {
402 char c = *string;
403 rgb[i] <<= 4;
405 if (!isxdigit(c))
406 rgb[i] |= 0;
407 else
408 rgb[i] |= c >= '0' && c <= '9' ? c - '0' :
409 c >= 'a' && c <= 'f' ? c - 'a' + 10 : c - 'A' + 10;
410 }
412 if (i >= (have_alpha ? 3 : 2))
413 break;
415 if (len == 0 || *string != '/')
416 return false;
417 string++; len--;
418 }
420 /* Re-scale to 8-bit */
421 uint8_t r = 256 * (rgb[0] / (double)(1 << (4 * digits[0])));
422 uint8_t g = 256 * (rgb[1] / (double)(1 << (4 * digits[1])));
423 uint8_t b = 256 * (rgb[2] / (double)(1 << (4 * digits[2])));
425 uint16_t alpha = 0xffff;
426 if (have_alpha)
427 alpha = 65536 * (rgb[3] / (double)(1 << (4 * digits[3])));
429 if (have_alpha)
430 LOG_DBG("rgba: %02x%02x%02x (alpha=%04x)", r, g, b, alpha);
431 else
432 LOG_DBG("rgb: %02x%02x%02x", r, g, b);
434 if (_have_alpha != NULL)
435 *_have_alpha = have_alpha;
436 if (_alpha != NULL)
437 *_alpha = alpha;
439 *color = r << 16 | g << 8 | b;
440 return true;
443static void
444osc_set_pwd(struct terminal *term, char *string)
446 LOG_DBG("PWD: URI: %s", string);
448 char *scheme, *host, *path;
449 if (!uri_parse(string, strlen(string), &scheme, NULL, NULL, &host, NULL, &path, NULL, NULL)) {
450 LOG_ERR("OSC7: invalid URI: %s", string);
451 return;
452 }
454 if (streq(scheme, "file") && hostname_is_localhost(host)) {
455 LOG_DBG("OSC7: pwd: %s", path);
456 free(term->cwd);
457 term->cwd = path;
458 } else
459 free(path);
461 free(scheme);
462 free(host);
465static void
466osc_uri(struct terminal *term, char *string)
468 /*
469 * \E]8;<params>;URI\e\\
470 *
471 * Params are key=value pairs, separated by ':'.
472 *
473 * The only defined key (as of 2020-05-31) is 'id', which is used
474 * to group split-up URIs:
475 *
476 * ╔═ file1 ════╗
477 * ║ ╔═ file2 ═══╗
478 * ║http://exa║Lorem ipsum║
479 * ║le.com ║ dolor sit ║
480 * ║ ║amet, conse║
481 * ╚══════════║ctetur adip║
482 * ╚═══════════╝
483 *
484 * This lets a terminal emulator highlight both parts at the same
485 * time (e.g. when hovering over one of the parts with the mouse).
486 */
488 char *params = string;
489 char *params_end = strchr(params, ';');
490 if (params_end == NULL)
491 return;
493 *params_end = '\0';
494 const char *uri = params_end + 1;
495 uint64_t id = (uint64_t)rand() << 32 | rand();
497 char *ctx = NULL;
498 for (const char *key_value = strtok_r(params, ":", &ctx);
499 key_value != NULL;
500 key_value = strtok_r(NULL, ":", &ctx))
501 {
502 const char *key = key_value;
503 char *operator = (char *)strchr(key_value, '=');
505 if (operator == NULL)
506 continue;
507 *operator = '\0';
509 const char *value = operator + 1;
511 if (streq(key, "id"))
512 id = sdbm_hash(value);
513 }
516 if (uri[0] == '\0') {
517 LOG_DBG("OSC-8: close");
518 term_osc8_close(term);
519 } else {
520 LOG_DBG("OSC-8: URL=%s, id=%" PRIu64, uri, id);
521 term_osc8_open(term, id, uri);
522 }
525static void
526osc_notify(struct terminal *term, char *string)
528 /*
529 * The 'notify' perl extension
530 * (https://pub.phyks.me/scripts/urxvt/notify) is very simple:
531 *
532 * #!/usr/bin/perl
533 *
534 * sub on_osc_seq_perl {
535 * my ($term, $osc, $resp) = @_;
536 * if ($osc =~ /^notify;(\S+);(.*)$/) {
537 * system("notify-send '$1' '$2'");
538 * }
539 * }
540 *
541 * As can be seen, the notification text is not encoded in any
542 * way. The regex does a greedy match of the ';' separator. Thus,
543 * any extra ';' will end up being part of the title. There's no
544 * way to have a ';' in the message body.
545 *
546 * I've changed that behavior slightly in; we split the title from
547 * body on the *first* ';', allowing us to have semicolons in the
548 * message body, but *not* in the title.
549 */
550 char *ctx = NULL;
551 const char *title = strtok_r(string, ";", &ctx);
552 const char *msg = strtok_r(NULL, "\x00", &ctx);
554 if (title == NULL)
555 return;
557 if (mbsntoc32(NULL, title, strlen(title), 0) == (size_t)-1) {
558 LOG_WARN("%s: notification title is not valid UTF-8, ignoring", title);
559 return;
560 }
562 if (msg != NULL && mbsntoc32(NULL, msg, strlen(msg), 0) == (size_t)-1) {
563 LOG_WARN("%s: notification message is not valid UTF-8, ignoring", msg);
564 return;
565 }
567 char *msgdup = NULL;
568 if (msg != NULL)
569 msgdup = xstrdup(msg);
571 notify_notify(term, &(struct notification){
572 .title = xstrdup(title),
573 .body = msgdup,
574 .expire_time = -1,
575 .focus = true,
576 });
579IGNORE_WARNING("-Wpedantic")
580static bool
581verify_kitty_id_is_valid(const char *id)
583 const size_t len = strlen(id);
585 for (size_t i = 0; i < len; i++) {
586 switch (id[i]) {
587 case 'a' ... 'z':
588 case 'A' ... 'Z':
589 case '0' ... '9':
590 case '_':
591 case '-':
592 case '+':
593 case '.':
594 break;
596 default:
597 return false;
598 }
599 }
601 return true;
603UNIGNORE_WARNINGS
605static void
606kitty_notification(struct terminal *term, char *string)
608 /* https://sw.kovidgoyal.net/kitty/desktop-notifications */
610 char *payload_raw = strchr(string, ';');
611 if (payload_raw == NULL)
612 return;
614 char *parameters = string;
615 *payload_raw = '\0';
616 payload_raw++;
618 char *id = NULL; /* The 'i' parameter */
619 char *app_id = NULL; /* The 'f' parameter */
620 char *icon_cache_id = NULL; /* The 'g' parameter */
621 char *symbolic_icon = NULL; /* The 'n' parameter */
622 char *category = NULL; /* The 't' parameter */
623 char *sound_name = NULL; /* The 's' parameter */
624 char *payload = NULL;
626 bool focus = true; /* The 'a' parameter */
627 bool report_activated = false; /* The 'a' parameter */
628 bool report_closed = false; /* The 'c' parameter */
629 bool done = true; /* The 'd' parameter */
630 bool base64 = false; /* The 'e' parameter */
632 int32_t expire_time = -1; /* The 'w' parameter */
634 size_t payload_size;
635 enum {
636 PAYLOAD_TITLE,
637 PAYLOAD_BODY,
638 PAYLOAD_CLOSE,
639 PAYLOAD_ALIVE,
640 PAYLOAD_ICON,
641 PAYLOAD_BUTTON,
642 } payload_type = PAYLOAD_TITLE; /* The 'p' parameter */
644 enum notify_when when = NOTIFY_ALWAYS;
645 enum notify_urgency urgency = NOTIFY_URGENCY_NORMAL;
647 bool have_a = false;
648 bool have_c = false;
649 bool have_o = false;
650 bool have_u = false;
651 bool have_w = false;
653 char *ctx = NULL;
654 for (char *param = strtok_r(parameters, ":", &ctx);
655 param != NULL;
656 param = strtok_r(NULL, ":", &ctx))
657 {
658 /* All parameters are on the form X=value, where X is always
659 exactly one character */
660 if (param[0] == '\0' || param[1] != '=')
661 continue;
663 char *value = &param[2];
665 switch (param[0]) {
666 case 'a': {
667 /* notification activation action: focus|report|-focus|-report */
668 have_a = true;
669 char *a_ctx = NULL;
671 for (const char *v = strtok_r(value, ",", &a_ctx);
672 v != NULL;
673 v = strtok_r(NULL, ",", &a_ctx))
674 {
675 bool reverse = v[0] == '-';
676 if (reverse)
677 v++;
679 if (streq(v, "focus"))
680 focus = !reverse;
681 else if (streq(v, "report"))
682 report_activated = !reverse;
683 }
685 break;
686 }
688 case 'c':
689 if (value[0] == '1' && value[1] == '\0')
690 report_closed = true;
691 else if (value[0] == '0' && value[1] == '\0')
692 report_closed = false;
693 have_c = true;
694 break;
696 case 'd':
697 /* done: 0|1 */
698 if (value[0] == '0' && value[1] == '\0')
699 done = false;
700 else if (value[0] == '1' && value[1] == '\0')
701 done = true;
702 break;
704 case 'e':
705 /* base64 (payload encoding): 0=utf8, 1=base64(utf8) */
706 if (value[0] == '0' && value[1] == '\0')
707 base64 = false;
708 else if (value[0] == '1' && value[1] == '\0')
709 base64 = true;
710 break;
712 case 'i':
713 /* id */
714 if (verify_kitty_id_is_valid(value)) {
715 free(id);
716 id = xstrdup(value);
717 } else
718 LOG_WARN("OSC-99: ignoring invalid 'i' identifier");
719 break;
721 case 'p':
722 /* payload content: title|body */
723 if (streq(value, "title"))
724 payload_type = PAYLOAD_TITLE;
725 else if (streq(value, "body"))
726 payload_type = PAYLOAD_BODY;
727 else if (streq(value, "close"))
728 payload_type = PAYLOAD_CLOSE;
729 else if (streq(value, "alive"))
730 payload_type = PAYLOAD_ALIVE;
731 else if (streq(value, "icon"))
732 payload_type = PAYLOAD_ICON;
733 else if (streq(value, "buttons"))
734 payload_type = PAYLOAD_BUTTON;
735 else if (streq(value, "?")) {
736 /* Query capabilities */
738 const char *reply_id = id != NULL ? id : "0";
740 const char *p_caps = "title,body,?,close,alive,icon,buttons";
741 const char *a_caps = "focus,report";
742 const char *u_caps = "0,1,2";
744 char when_caps[64];
745 strcpy(when_caps, "unfocused");
746 if (!term->conf->desktop_notifications.inhibit_when_focused)
747 strcat(when_caps, ",always");
749 const char *terminator = term->vt.osc.bel ? "\a" : "\033\\";
751 char reply[512];
752 size_t n = xsnprintf(
753 reply, sizeof(reply),
754 "\033]99;i=%s:p=?;p=%s:a=%s:o=%s:u=%s:c=1:w=1:s=system,silent,error,warn,warning,info,question%s",
755 reply_id, p_caps, a_caps, when_caps, u_caps, terminator);
757 term_to_slave(term, reply, n);
758 goto out;
759 }
760 break;
762 case 'o':
763 /* honor when: always|unfocused|invisible */
764 have_o = true;
765 if (streq(value, "always"))
766 when = NOTIFY_ALWAYS;
767 else if (streq(value, "unfocused"))
768 when = NOTIFY_UNFOCUSED;
769 else if (streq(value, "invisible"))
770 when = NOTIFY_INVISIBLE;
771 break;
773 case 'u':
774 /* urgency: 0=low, 1=normal, 2=critical */
775 have_u = true;
776 if (value[0] == '0' && value[1] == '\0')
777 urgency = NOTIFY_URGENCY_LOW;
778 else if (value[0] == '1' && value[1] == '\0')
779 urgency = NOTIFY_URGENCY_NORMAL;
780 else if (value[0] == '2' && value[1] == '\0')
781 urgency = NOTIFY_URGENCY_CRITICAL;
782 break;
784 case 'w': {
785 /* Notification timeout */
786 errno = 0;
787 char *end = NULL;
788 long timeout = strtol(value, &end, 10);
790 if (errno == 0 && *end == '\0' && timeout <= INT32_MAX) {
791 expire_time = timeout;
792 have_w = true;
793 }
794 break;
795 }
797 case 'f': {
798 /* App-name */
799 char *decoded = base64_decode(value, NULL);
800 if (decoded != NULL) {
801 free(app_id);
802 app_id = decoded;
803 }
804 break;
805 }
807 case 't': {
808 /* Type (category) */
809 char *decoded = base64_decode(value, NULL);
810 if (decoded != NULL) {
811 if (category == NULL)
812 category = decoded;
813 else {
814 /* Append, comma separated */
815 char *old_category = category;
816 category = xstrjoin3(old_category, ",", decoded);
817 free(decoded);
818 free(old_category);
819 }
820 }
821 break;
822 }
824 case 's': {
825 /* Sound */
826 char *decoded = base64_decode(value, NULL);
827 if (decoded != NULL) {
828 free(sound_name);
829 sound_name = decoded;
831 const char *translated_name = NULL;
833 if (streq(decoded, "error"))
834 translated_name = "dialog-error";
835 else if (streq(decoded, "warn") || streq(decoded, "warning"))
836 translated_name = "dialog-warning";
837 else if (streq(decoded, "info"))
838 translated_name = "dialog-information";
839 else if (streq(decoded, "question"))
840 translated_name = "dialog-question";
842 if (translated_name != NULL) {
843 free(sound_name);
844 sound_name = xstrdup(translated_name);
845 }
846 }
847 break;
848 }
850 case 'g':
851 /* graphical ID (see 'n' and 'p=icon') */
852 free(icon_cache_id);
853 icon_cache_id = xstrdup(value);
854 break;
856 case 'n': {
857 /* Symbolic icon name, may used with 'g' */
859 /*
860 * Sigh, protocol says 'n' can be used multiple times, and
861 * that the terminal picks the first one that it can
862 * resolve.
863 *
864 * We can't resolve any icons at all. So, enter
865 * heuristics... let's pick the *shortest* symbolic
866 * name. The idea is that icon *names* are typically
867 * shorter than .desktop names, and macOS bundle
868 * identifiers.
869 */
870 char *maybe_new_symbolic_icon = base64_decode(value, NULL);
871 if (maybe_new_symbolic_icon == NULL)
872 break;
874 if (symbolic_icon == NULL ||
875 strlen(maybe_new_symbolic_icon) < strlen(symbolic_icon))
876 {
877 free(symbolic_icon);
878 symbolic_icon = maybe_new_symbolic_icon;
880 /* Translate OSC-99 "special" names */
881 if (symbolic_icon != NULL) {
882 const char *translated_name = NULL;
884 if (streq(symbolic_icon, "error"))
885 translated_name = "dialog-error";
886 else if (streq(symbolic_icon, "warn") ||
887 streq(symbolic_icon, "warning"))
888 translated_name = "dialog-warning";
889 else if (streq(symbolic_icon, "info"))
890 translated_name = "dialog-information";
891 else if (streq(symbolic_icon, "question"))
892 translated_name = "dialog-question";
893 else if (streq(symbolic_icon, "help"))
894 translated_name = "system-help";
895 else if (streq(symbolic_icon, "file-manager"))
896 translated_name = "system-file-manager";
897 else if (streq(symbolic_icon, "system-monitor"))
898 translated_name = "utilities-system-monitor";
899 else if (streq(symbolic_icon, "text-editor"))
900 translated_name = "text-editor";
902 if (translated_name != NULL) {
903 free(symbolic_icon);
904 symbolic_icon = xstrdup(translated_name);
905 }
906 }
907 } else {
908 free(maybe_new_symbolic_icon);
909 }
910 break;
911 }
912 }
913 }
915 if (base64) {
916 payload = base64_decode(payload_raw, &payload_size);
917 if (payload == NULL)
918 goto out;
919 } else {
920 payload = xstrdup(payload_raw);
921 payload_size = strlen(payload);
922 }
924 /* Append metadata to previous notification chunk */
925 struct notification *notif = &term->kitty_notification;
927 if (!((id == NULL && notif->id == NULL) ||
928 (id != NULL && notif->id != NULL && streq(id, notif->id))) ||
929 !notif->may_be_programatically_closed) /* Free:d notification has this as false... */
930 {
931 /* ID mismatch, ignore previous notification state */
932 notify_free(term, notif);
934 notif->id = id;
935 notif->when = when;
936 notif->urgency = urgency;
937 notif->expire_time = expire_time;
938 notif->focus = focus;
939 notif->may_be_programatically_closed = true;
940 notif->report_activated = report_activated;
941 notif->report_closed = report_closed;
943 id = NULL; /* Prevent double free */
944 }
946 if (have_a) {
947 notif->focus = focus;
948 notif->report_activated = report_activated;
949 }
951 if (have_c)
952 notif->report_closed = report_closed;
954 if (have_o)
955 notif->when = when;
956 if (have_u)
957 notif->urgency = urgency;
958 if (have_w)
959 notif->expire_time = expire_time;
961 if (icon_cache_id != NULL) {
962 free(notif->icon_cache_id);
963 notif->icon_cache_id = icon_cache_id;
964 icon_cache_id = NULL; /* Prevent double free */
965 }
967 if (symbolic_icon != NULL) {
968 free(notif->icon_symbolic_name);
969 notif->icon_symbolic_name = symbolic_icon;
970 symbolic_icon = NULL;
971 }
973 if (app_id != NULL) {
974 free(notif->app_id);
975 notif->app_id = app_id;
976 app_id = NULL; /* Prevent double free */
977 }
979 if (category != NULL) {
980 if (notif->category == NULL) {
981 notif->category = category;
982 category = NULL; /* Prevent double free */
983 } else {
984 /* Append, comma separated */
985 char *new_category = xstrjoin3(notif->category, ",", category);
986 free(notif->category);
987 notif->category = new_category;
988 }
989 }
991 if (sound_name != NULL) {
992 notif->muted = streq(sound_name, "silent");
994 if (notif->muted || streq(sound_name, "system")) {
995 free(notif->sound_name);
996 notif->sound_name = NULL;
997 } else {
998 free(notif->sound_name);
999 notif->sound_name = sound_name;
1000 sound_name = NULL; /* Prevent double free */
1004 /* Handled chunked payload - append to existing metadata */
1005 switch (payload_type) {
1006 case PAYLOAD_TITLE:
1007 case PAYLOAD_BODY: {
1008 char **ptr = payload_type == PAYLOAD_TITLE
1009 ? &notif->title
1010 : &notif->body;
1012 if (*ptr == NULL) {
1013 *ptr = payload;
1014 payload = NULL;
1015 } else {
1016 char *old = *ptr;
1017 *ptr = xstrjoin(old, payload);
1018 free(old);
1020 break;
1023 case PAYLOAD_CLOSE:
1024 case PAYLOAD_ALIVE:
1025 /* Ignore payload */
1026 break;
1028 case PAYLOAD_ICON:
1029 if (notif->icon_data == NULL) {
1030 notif->icon_data = (uint8_t *)payload;
1031 notif->icon_data_sz = payload_size;
1032 payload = NULL;
1033 } else {
1034 notif->icon_data = xrealloc(
1035 notif->icon_data, notif->icon_data_sz + payload_size);
1036 memcpy(&notif->icon_data[notif->icon_data_sz], payload, payload_size);
1037 notif->icon_data_sz += payload_size;
1039 break;
1041 case PAYLOAD_BUTTON: {
1042 char *ctx = NULL;
1043 for (const char *button = strtok_r(payload, "\u2028", &ctx);
1044 button != NULL;
1045 button = strtok_r(NULL, "\u2028", &ctx))
1047 if (button[0] != '\0') {
1048 tll_push_back(notif->actions, xstrdup(button));
1052 break;
1056 if (done) {
1057 /* Update icon cache, if necessary */
1058 if (notif->icon_cache_id != NULL &&
1059 (notif->icon_symbolic_name != NULL || notif->icon_data != NULL))
1061 notify_icon_del(term, notif->icon_cache_id);
1062 notify_icon_add(term, notif->icon_cache_id,
1063 notif->icon_symbolic_name,
1064 notif->icon_data, notif->icon_data_sz);
1066 /* Don't need this anymore */
1067 free(notif->icon_symbolic_name);
1068 free(notif->icon_data);
1069 notif->icon_symbolic_name = NULL;
1070 notif->icon_data = NULL;
1071 notif->icon_data_sz = 0;
1074 if (payload_type == PAYLOAD_CLOSE) {
1075 if (notif->id != NULL)
1076 notify_close(term, notif->id);
1077 } else if (payload_type == PAYLOAD_ALIVE) {
1078 char *alive_ids = NULL;
1080 tll_foreach(term->active_notifications, it) {
1081 /* TODO: check with kitty: use "0" for all
1082 notifications with no ID? */
1084 const char *item_id = it->item.id != NULL ? it->item.id : "0";
1086 if (alive_ids == NULL)
1087 alive_ids = xstrdup(item_id);
1088 else {
1089 char *old_alive_ids = alive_ids;
1090 alive_ids = xstrjoin3(old_alive_ids, ",", item_id);
1091 free(old_alive_ids);
1095 char *reply = xasprintf(
1096 "\033]99;i=%s:p=alive;%s\033\\",
1097 notif->id != NULL ? notif->id : "0",
1098 alive_ids != NULL ? alive_ids : "");
1100 term_to_slave(term, reply, strlen(reply));
1101 free(reply);
1102 free(alive_ids);
1103 } else {
1104 /*
1105 * Show notification.
1107 * The checks for title|body is to handle notifications that
1108 * only load icon data into the icon cache
1109 */
1110 if (notif->title != NULL || notif->body != NULL) {
1111 notify_notify(term, notif);
1115 notify_free(term, notif);
1118out:
1119 free(id);
1120 free(app_id);
1121 free(icon_cache_id);
1122 free(symbolic_icon);
1123 free(payload);
1124 free(category);
1125 free(sound_name);
1128static void
1129kitty_text_size(struct terminal *term, char *string)
1131 char *text = strchr(string, ';');
1132 if (text == NULL)
1133 return;
1135 char *parameters = string;
1136 *text = '\0';
1137 text++;
1139 if (text[0] == '\0')
1140 return;
1142 char32_t *wchars = ambstoc32(text);
1143 if (wchars == NULL)
1144 return;
1146 int forced_width = 0;
1148 char *ctx = NULL;
1149 for (char *param = strtok_r(parameters, ":", &ctx);
1150 param != NULL;
1151 param = strtok_r(NULL, ":", &ctx))
1153 /* All parameters are on the form X=value, where X is always
1154 exactly one character */
1155 if (param[0] == '\0' || param[1] != '=')
1156 continue;
1158 char *value = &param[2];
1160 switch (param[0]) {
1161 case 'w': {
1162 errno = 0;
1163 char *end = NULL;
1164 unsigned long w = strtoul(value, &end, 10);
1166 if (*end == '\0' && errno == 0 && w <= 7) {
1167 forced_width = (int)w;
1168 break;
1169 } else
1170 LOG_ERR("OSC-66: invalid 'w' value, ignoring");
1171 break;
1174 case 's':
1175 case 'n':
1176 case 'd':
1177 case 'v':
1178 LOG_WARN("OSC-66: unsupported: '%c' parameter, ignoring", param[0]);
1179 break;
1183 const size_t len = c32len(wchars);
1185 if (forced_width == 0) {
1186 /*
1187 * w=0 means we split the text up as we'd normally do... Since
1188 * we don't support any other parameters of the text-sizing
1189 * protocol, that means we just process the string as if it
1190 * has been printed without this OSC.
1191 */
1192 for (size_t i = 0; i < len; i++)
1193 term_process_and_print_non_ascii(term, wchars[i]);
1194 free(wchars);
1195 return;
1198 size_t max_cp_width = 0;
1199 size_t all_cp_width = 0;
1201 for (size_t i = 0; i < len; i++) {
1202 const size_t cp_width = c32width(wchars[i]);
1203 all_cp_width += cp_width;
1204 max_cp_width = max(max_cp_width, cp_width);
1207 size_t calculated_width = 0;
1208 switch (term->conf->tweak.grapheme_width_method) {
1209 case GRAPHEME_WIDTH_WCSWIDTH: calculated_width = all_cp_width; break;
1210 case GRAPHEME_WIDTH_MAX: calculated_width = max_cp_width; break;
1211 case GRAPHEME_WIDTH_DOUBLE: calculated_width = min(max_cp_width, 2); break;
1214 const size_t width = forced_width == 0 ? calculated_width : forced_width;
1216 LOG_DBG("len=%zu, forced=%d, calculated=%zu, using=%zu",
1217 len, forced_width, calculated_width, width);
1219#if 0
1220 if (len == 1 && calculated_width == forced_width) {
1221 /*
1222 * Optimization: if there's a single codepoint, and either
1223 * w=0, or the 'w' matches the calculated width, print
1224 * codepoint directly instead of creating a combining
1225 * character.
1226 */
1227 term_print(term, wchars[0], width);
1228 free(wchars);
1229 return;
1231#endif
1233 uint32_t key = composed_key_from_chars(wchars, len);
1235 const struct composed *composed = composed_lookup_without_collision(
1236 term->composed, &key, wchars, len - 1, wchars[len - 1], forced_width);
1238 if (composed == NULL) {
1239 struct composed *new_cc = xmalloc(sizeof(*new_cc));
1240 new_cc->chars = wchars;
1241 new_cc->count = len;
1242 new_cc->key = key;
1243 new_cc->width = width;
1244 new_cc->forced_width = forced_width;
1246 term->composed_count++;
1247 composed_insert(&term->composed, new_cc);
1248 composed = new_cc;
1249 } else if (composed->width == width) {
1250 free(wchars);
1253 term_print(
1254 term, CELL_COMB_CHARS_LO + composed->key,
1255 composed->forced_width > 0 ? composed->forced_width : composed->width,
1256 false);
1259void
1260osc_dispatch(struct terminal *term)
1262 unsigned param = 0;
1263 int data_ofs = 0;
1265 for (size_t i = 0; i < term->vt.osc.idx; i++, data_ofs++) {
1266 char c = term->vt.osc.data[i];
1268 if (c == ';') {
1269 data_ofs++;
1270 break;
1273 if (!isdigit(c)) {
1274 UNHANDLED();
1275 return;
1278 param *= 10;
1279 param += c - '0';
1282 LOG_DBG("OSC: %.*s (param = %d)",
1283 (int)term->vt.osc.idx, term->vt.osc.data, param);
1285 char *string = (char *)&term->vt.osc.data[data_ofs];
1287 switch (param) {
1288 case 0: /* icon + title */
1289 term_set_window_title(term, string);
1290 break;
1292 case 1: /* icon */
1293 break;
1295 case 2: /* title */
1296 term_set_window_title(term, string);
1297 break;
1299 case 4: {
1300 /* Set color<idx> */
1302 string--;
1303 if (*string != ';')
1304 break;
1306 xassert(*string == ';');
1308 for (const char *s_idx = strtok(string, ";"), *s_color = strtok(NULL, ";");
1309 s_idx != NULL && s_color != NULL;
1310 s_idx = strtok(NULL, ";"), s_color = strtok(NULL, ";"))
1312 /* Parse <idx> parameter */
1313 unsigned idx = 0;
1314 for (; *s_idx != '\0'; s_idx++) {
1315 char c = *s_idx;
1316 idx *= 10;
1317 idx += c - '0';
1320 if (idx >= ALEN(term->colors.table)) {
1321 LOG_WARN("invalid OSC 4 color index: %u", idx);
1322 break;
1325 /* Client queried for current value */
1326 if (s_color[0] == '?' && s_color[1] == '\0') {
1327 uint32_t color = term->colors.table[idx];
1328 uint8_t r = (color >> 16) & 0xff;
1329 uint8_t g = (color >> 8) & 0xff;
1330 uint8_t b = (color >> 0) & 0xff;
1331 const char *terminator = term->vt.osc.bel ? "\a" : "\033\\";
1333 char reply[32];
1334 size_t n = xsnprintf(
1335 reply, sizeof(reply),
1336 "\033]4;%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
1337 idx, r, r, g, g, b, b, terminator);
1338 term_to_slave(term, reply, n);
1341 else {
1342 uint32_t color;
1343 bool color_is_valid = s_color[0] == '#' || s_color[0] == '['
1344 ? parse_legacy_color(s_color, &color, NULL, NULL)
1345 : parse_rgb(s_color, &color, NULL, NULL);
1347 if (!color_is_valid)
1348 continue;
1350 LOG_DBG("change color definition for #%u from %06x to %06x",
1351 idx, term->colors.table[idx], color);
1353 term->colors.table[idx] = color;
1354 term_damage_color(term, COLOR_BASE256, idx);
1358 break;
1361 case 7:
1362 /* Update terminal's understanding of PWD */
1363 osc_set_pwd(term, string);
1364 break;
1366 case 8:
1367 osc_uri(term, string);
1368 break;
1370 case 9: {
1371 /* iTerm2 Growl notifications */
1372 const char *sep = strchr(string, ';');
1373 if (sep != NULL) {
1374 errno = 0;
1375 char *end = NULL;
1376 strtoul(string, &end, 10);
1377 if (end == sep && errno == 0) {
1378 /* Ignore ConEmu/Windows Terminal escape */
1379 break;
1383 osc_notify(term, string);
1384 break;
1387 case 10: /* fg */
1388 case 11: /* bg */
1389 case 12: /* cursor */
1390 case 17: /* highlight (selection) fg */
1391 case 19: { /* highlight (selection) bg */
1392 /* Set default foreground/background/highlight-bg/highlight-fg color */
1394 /* Client queried for current value */
1395 if (string[0] == '?' && string[1] == '\0') {
1396 uint32_t color = param == 10
1397 ? term->colors.fg
1398 : param == 11
1399 ? term->colors.bg
1400 : param == 12
1401 ? term->colors.cursor_bg
1402 : param == 17
1403 ? term->colors.selection_bg
1404 : term->colors.selection_fg;
1406 uint8_t r = (color >> 16) & 0xff;
1407 uint8_t g = (color >> 8) & 0xff;
1408 uint8_t b = (color >> 0) & 0xff;
1409 const char *terminator = term->vt.osc.bel ? "\a" : "\033\\";
1411 /*
1412 * Reply in XParseColor format
1413 * E.g. for color 0xdcdccc we reply "\033]10;rgb:dc/dc/cc\033\\"
1414 */
1415 char reply[32];
1416 size_t n = xsnprintf(
1417 reply, sizeof(reply),
1418 "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
1419 param, r, r, g, g, b, b, terminator);
1421 term_to_slave(term, reply, n);
1422 break;
1425 uint32_t color;
1426 bool have_alpha = false;
1427 uint16_t alpha = 0xffff;
1429 if (string[0] == '#' || string[0] == '['
1430 ? !parse_legacy_color(string, &color, &have_alpha, &alpha)
1431 : !parse_rgb(string, &color, &have_alpha, &alpha))
1433 break;
1436 LOG_DBG("change color definition for %s to %06x",
1437 param == 10 ? "foreground" :
1438 param == 11 ? "background" :
1439 param == 12 ? "cursor" :
1440 param == 17 ? "selection background" :
1441 "selection foreground",
1442 color);
1444 switch (param) {
1445 case 10:
1446 term->colors.fg = color;
1447 term_damage_color(term, COLOR_DEFAULT, 0);
1448 break;
1450 case 11:
1451 term->colors.bg = color;
1452 if (!have_alpha)
1453 alpha = term_theme_get(term)->alpha;
1455 const bool changed = term->colors.alpha != alpha;
1456 term->colors.alpha = alpha;
1458 if (changed) {
1459 wayl_win_alpha_changed(term->window);
1460 term_font_subpixel_changed(term);
1463 term_damage_color(term, COLOR_DEFAULT, 0);
1464 term_damage_margins(term);
1465 break;
1467 case 12:
1468 term->colors.cursor_bg = 1u << 31 | color;
1469 term_damage_cursor(term);
1470 break;
1472 case 17:
1473 term->colors.selection_bg = color;
1474 break;
1476 case 19:
1477 term->colors.selection_fg = color;
1478 break;
1481 break;
1484 case 22: /* Set mouse cursor */
1485 term_set_user_mouse_cursor(term, string);
1486 break;
1488 case 30: /* Set tab title */
1489 break;
1491 case 52: /* Copy to/from clipboard/primary */
1492 osc_selection(term, string);
1493 break;
1495 case 66: /* text-size protocol (kitty) */
1496 kitty_text_size(term, string);
1497 break;
1499 case 99: /* Kitty notifications */
1500 kitty_notification(term, string);
1501 break;
1503 case 104: {
1504 /* Reset Color Number 'c' (whole table if no parameter) */
1506 const struct color_theme *theme = term_theme_get(term);
1508 if (string[0] == '\0') {
1509 LOG_DBG("resetting all colors");
1510 memcpy(term->colors.table, theme->table, sizeof(term->colors.table));
1511 term_damage_view(term);
1514 else {
1515 for (const char *s_idx = strtok(string, ";");
1516 s_idx != NULL;
1517 s_idx = strtok(NULL, ";"))
1519 unsigned idx = 0;
1520 for (; *s_idx != '\0'; s_idx++) {
1521 char c = *s_idx;
1522 idx *= 10;
1523 idx += c - '0';
1526 if (idx >= ALEN(term->colors.table)) {
1527 LOG_WARN("invalid OSC 104 color index: %u", idx);
1528 continue;
1531 LOG_DBG("resetting color #%u", idx);
1532 term->colors.table[idx] = theme->table[idx];
1533 term_damage_color(term, COLOR_BASE256, idx);
1537 break;
1540 case 105: /* Reset Special Color Number 'c' */
1541 break;
1543 case 110: /* Reset default text foreground color */
1544 LOG_DBG("resetting foreground color");
1546 const struct color_theme *theme = term_theme_get(term);
1547 term->colors.fg = theme->fg;
1548 term_damage_color(term, COLOR_DEFAULT, 0);
1549 break;
1551 case 111: { /* Reset default text background color */
1552 LOG_DBG("resetting background color");
1554 const struct color_theme *theme = term_theme_get(term);
1555 bool alpha_changed = term->colors.alpha != theme->alpha;
1557 term->colors.bg = theme->bg;
1558 term->colors.alpha = theme->alpha;
1560 if (alpha_changed) {
1561 wayl_win_alpha_changed(term->window);
1562 term_font_subpixel_changed(term);
1565 term_damage_color(term, COLOR_DEFAULT, 0);
1566 term_damage_margins(term);
1567 break;
1570 case 112: {
1571 LOG_DBG("resetting cursor color");
1573 const struct color_theme *theme = term_theme_get(term);
1574 term->colors.cursor_fg = theme->cursor.text;
1575 term->colors.cursor_bg = theme->cursor.cursor;
1577 if (term->conf->colors_dark.use_custom.cursor) {
1578 term->colors.cursor_fg |= 1u << 31;
1579 term->colors.cursor_bg |= 1u << 31;
1582 term_damage_cursor(term);
1583 break;
1586 case 117: {
1587 LOG_DBG("resetting selection background color");
1589 const struct color_theme *theme = term_theme_get(term);
1590 term->colors.selection_bg = theme->selection_bg;
1591 break;
1594 case 119: {
1595 LOG_DBG("resetting selection foreground color");
1597 const struct color_theme *theme = term_theme_get(term);
1598 term->colors.selection_fg = theme->selection_fg;
1599 break;
1602 case 133:
1603 /*
1604 * Shell integration; see
1605 * https://iterm2.com/documentation-escape-codes.html (Shell
1606 * Integration/FinalTerm)
1608 * [PROMPT]prompt% [COMMAND_START] ls -l
1609 * [COMMAND_EXECUTED]
1610 * -rw-r--r-- 1 user group 127 May 1 2016 filename
1611 * [COMMAND_FINISHED]
1612 */
1613 switch (string[0]) {
1614 case 'A':
1615 LOG_DBG("FTCS_PROMPT: %dx%d",
1616 term->grid->cursor.point.row,
1617 term->grid->cursor.point.col);
1619 term->grid->cur_row->shell_integration.prompt_marker = true;
1620 break;
1622 case 'B':
1623 LOG_DBG("FTCS_COMMAND_START");
1624 break;
1626 case 'C':
1627 LOG_DBG("FTCS_COMMAND_EXECUTED: %dx%d",
1628 term->grid->cursor.point.row,
1629 term->grid->cursor.point.col);
1630 term->grid->cur_row->shell_integration.cmd_start = term->grid->cursor.point.col;
1631 break;
1633 case 'D':
1634 LOG_DBG("FTCS_COMMAND_FINISHED: %dx%d",
1635 term->grid->cursor.point.row,
1636 term->grid->cursor.point.col);
1637 term->grid->cur_row->shell_integration.cmd_end = term->grid->cursor.point.col;
1638 break;
1640 break;
1642 case 176:
1643 if (string[0] == '?' && string[1] == '\0') {
1644#if 0 /* Disabled for now, see #1894 */
1645 const char *terminator = term->vt.osc.bel ? "\a" : "\033\\";
1646 char *reply = xasprintf(
1647 "\033]176;%s%s",
1648 term->app_id != NULL ? term->app_id : term->conf->app_id,
1649 terminator);
1651 term_to_slave(term, reply, strlen(reply));
1652 free(reply);
1653#else
1654 LOG_WARN("OSC-176 app-id query ignored");
1655#endif
1656 break;
1659 term_set_app_id(term, string);
1660 break;
1662 case 555:
1663 osc_flash(term);
1664 break;
1666 case 777: {
1667 /*
1668 * OSC 777 is an URxvt generic escape used to send commands to
1669 * perl extensions. The generic syntax is: \E]777;<command>;<string>ST
1671 * We only recognize the 'notify' command, which is, if not
1672 * well established, at least fairly well known.
1673 */
1675 char *param_brk = strchr(string, ';');
1676 if (param_brk == NULL) {
1677 UNHANDLED();
1678 return;
1681 if (strncmp(string, "notify", param_brk - string) == 0)
1682 osc_notify(term, param_brk + 1);
1683 else
1684 UNHANDLED();
1685 break;
1688 default:
1689 UNHANDLED();
1690 break;
1694bool
1695osc_ensure_size(struct terminal *term, size_t required_size)
1697 if (likely(required_size <= term->vt.osc.size))
1698 return true;
1700 const size_t pow2_max = ~(SIZE_MAX >> 1);
1701 if (unlikely(required_size > pow2_max)) {
1702 LOG_ERR("required OSC buffer size (%zu) exceeds limit (%zu)",
1703 required_size, pow2_max);
1704 return false;
1707 size_t new_size = max(term->vt.osc.size, 4096);
1708 while (new_size < required_size) {
1709 new_size <<= 1;
1712 uint8_t *new_data = realloc(term->vt.osc.data, new_size);
1713 if (new_data == NULL) {
1714 LOG_ERRNO("failed to increase size of OSC buffer");
1715 return false;
1718 LOG_DBG("resized OSC buffer: %zu", new_size);
1719 term->vt.osc.data = new_data;
1720 term->vt.osc.size = new_size;
1721 return true;
Diagnostics5 git calls · 514 ms
CallStatusSizeTime0514 ms
discover200178 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=178
git.server.agent=git/2.56.1-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=178
git.pkt.payload.max=35
ls-refs20010 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=10322
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=108
git.pkt.data=107
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=10322
git.pkt.payload.max=106
git.ls_refs.count=107
git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=8830
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=8830
git.pkt.payload.max=8730
git.sideband.band1.bytes=8730
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=14
git.pack.bytes=8730
git.fetch.wide.objects=14
git.fetch.wide.extra_bytes=9909
git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=509
git.response.bytes=9489
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=9489
git.pkt.payload.max=9389
git.sideband.band1.bytes=9389
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=9389
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a
git.pack.full=10
Open pack map
fetch20012 KiB509 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=11799
git.response.cache=MISS
git.response.duration_ms=509
git.fetch.wants=e04428aa02c62f3029098eaa30b68c1422212315
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=11799
git.pkt.payload.max=11772
git.sideband.band1.bytes=11772
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://codeberg.org/dnkl/foot
Ref
refs/heads/master
Commit
c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
cf-ray
a5e190132efd5b65
Request
1e753008…
Protocol
v2 · sha1
Agent
git/2.56.1-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format