Annotation of src/usr.bin/tmux/tty-features.c, Revision 1.26
1.26 ! nicm 1: /* $OpenBSD: tty-features.c,v 1.25 2022/06/30 09:55:53 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2020 Nicholas Marriott <nicholas.marriott@gmail.com>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <stdlib.h>
22: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
27: * Still hardcoded:
28: * - default colours (under AX or op capabilities);
29: * - AIX colours (under colors >= 16);
1.13 nicm 30: * - alternate escape (if terminal is VT100-like).
1.1 nicm 31: *
32: * Also:
1.7 nicm 33: * - DECFRA uses a flag instead of capabilities;
1.1 nicm 34: * - UTF-8 is a separate flag on the client; needed for unattached clients.
35: */
36:
37: /* A named terminal feature. */
38: struct tty_feature {
1.26 ! nicm 39: const char *name;
! 40: const char *const *capabilities;
! 41: int flags;
1.1 nicm 42: };
43:
44: /* Terminal has xterm(1) title setting. */
1.26 ! nicm 45: static const char *const tty_feature_title_capabilities[] = {
1.1 nicm 46: "tsl=\\E]0;", /* should be using TS really */
47: "fsl=\\a",
48: NULL
49: };
1.12 nicm 50: static const struct tty_feature tty_feature_title = {
1.1 nicm 51: "title",
52: tty_feature_title_capabilities,
53: 0
54: };
55:
1.22 nicm 56: /* Terminal has OSC 7 working directory. */
1.26 ! nicm 57: static const char *const tty_feature_osc7_capabilities[] = {
1.22 nicm 58: "Swd=\\E]7;",
59: "fsl=\\a",
60: NULL
61: };
62: static const struct tty_feature tty_feature_osc7 = {
63: "osc7",
64: tty_feature_osc7_capabilities,
65: 0
66: };
67:
1.20 nicm 68: /* Terminal has mouse support. */
1.26 ! nicm 69: static const char *const tty_feature_mouse_capabilities[] = {
1.20 nicm 70: "kmous=\\E[M",
71: NULL
72: };
73: static const struct tty_feature tty_feature_mouse = {
74: "mouse",
75: tty_feature_mouse_capabilities,
76: 0
77: };
78:
1.1 nicm 79: /* Terminal can set the clipboard with OSC 52. */
1.26 ! nicm 80: static const char *const tty_feature_clipboard_capabilities[] = {
1.1 nicm 81: "Ms=\\E]52;%p1%s;%p2%s\\a",
82: NULL
83: };
1.12 nicm 84: static const struct tty_feature tty_feature_clipboard = {
1.1 nicm 85: "clipboard",
86: tty_feature_clipboard_capabilities,
87: 0
88: };
89:
1.25 nicm 90: /* Terminal supports OSC 8 hyperlinks. */
1.26 ! nicm 91: static const char *const tty_feature_hyperlinks_capabilities[] = {
1.25 nicm 92: "*:Hls=\\E]8;%?%p1%l%tid=%p1%s%;;%p2%s\\E\\\\",
93: NULL
94: };
95: static const struct tty_feature tty_feature_hyperlinks = {
96: "hyperlinks",
97: tty_feature_hyperlinks_capabilities,
98: 0
99: };
100:
1.1 nicm 101: /*
102: * Terminal supports RGB colour. This replaces setab and setaf also since
103: * terminals with RGB have versions that do not allow setting colours from the
104: * 256 palette.
105: */
1.26 ! nicm 106: static const char *const tty_feature_rgb_capabilities[] = {
1.4 nicm 107: "AX",
1.1 nicm 108: "setrgbf=\\E[38;2;%p1%d;%p2%d;%p3%dm",
109: "setrgbb=\\E[48;2;%p1%d;%p2%d;%p3%dm",
110: "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
111: "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
112: NULL
113: };
1.12 nicm 114: static const struct tty_feature tty_feature_rgb = {
1.1 nicm 115: "RGB",
116: tty_feature_rgb_capabilities,
1.7 nicm 117: TERM_256COLOURS|TERM_RGBCOLOURS
1.1 nicm 118: };
119:
120: /* Terminal supports 256 colours. */
1.26 ! nicm 121: static const char *const tty_feature_256_capabilities[] = {
1.4 nicm 122: "AX",
1.1 nicm 123: "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
124: "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
125: NULL
126: };
1.12 nicm 127: static const struct tty_feature tty_feature_256 = {
1.1 nicm 128: "256",
129: tty_feature_256_capabilities,
130: TERM_256COLOURS
131: };
132:
133: /* Terminal supports overline. */
1.26 ! nicm 134: static const char *const tty_feature_overline_capabilities[] = {
1.1 nicm 135: "Smol=\\E[53m",
136: NULL
137: };
1.12 nicm 138: static const struct tty_feature tty_feature_overline = {
1.1 nicm 139: "overline",
140: tty_feature_overline_capabilities,
141: 0
142: };
143:
144: /* Terminal supports underscore styles. */
1.26 ! nicm 145: static const char *const tty_feature_usstyle_capabilities[] = {
1.17 daniel 146: "Smulx=\\E[4::%p1%dm",
147: "Setulc=\\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m",
1.18 nicm 148: "ol=\\E[59m",
1.1 nicm 149: NULL
150: };
1.12 nicm 151: static const struct tty_feature tty_feature_usstyle = {
1.1 nicm 152: "usstyle",
153: tty_feature_usstyle_capabilities,
154: 0
155: };
156:
1.12 nicm 157: /* Terminal supports bracketed paste. */
1.26 ! nicm 158: static const char *const tty_feature_bpaste_capabilities[] = {
1.12 nicm 159: "Enbp=\\E[?2004h",
1.11 nicm 160: "Dsbp=\\E[?2004l",
161: NULL
162: };
1.12 nicm 163: static const struct tty_feature tty_feature_bpaste = {
1.11 nicm 164: "bpaste",
165: tty_feature_bpaste_capabilities,
166: 0
167: };
168:
1.12 nicm 169: /* Terminal supports focus reporting. */
1.26 ! nicm 170: static const char *const tty_feature_focus_capabilities[] = {
1.12 nicm 171: "Enfcs=\\E[?1004h",
172: "Dsfcs=\\E[?1004l",
173: NULL
174: };
175: static const struct tty_feature tty_feature_focus = {
176: "focus",
177: tty_feature_focus_capabilities,
178: 0
179: };
180:
1.1 nicm 181: /* Terminal supports cursor styles. */
1.26 ! nicm 182: static const char *const tty_feature_cstyle_capabilities[] = {
1.1 nicm 183: "Ss=\\E[%p1%d q",
184: "Se=\\E[2 q",
185: NULL
186: };
1.12 nicm 187: static const struct tty_feature tty_feature_cstyle = {
1.1 nicm 188: "cstyle",
189: tty_feature_cstyle_capabilities,
190: 0
191: };
192:
193: /* Terminal supports cursor colours. */
1.26 ! nicm 194: static const char *const tty_feature_ccolour_capabilities[] = {
1.1 nicm 195: "Cs=\\E]12;%p1%s\\a",
196: "Cr=\\E]112\\a",
197: NULL
198: };
1.12 nicm 199: static const struct tty_feature tty_feature_ccolour = {
1.1 nicm 200: "ccolour",
201: tty_feature_ccolour_capabilities,
202: 0
203: };
204:
1.10 nicm 205: /* Terminal supports strikethrough. */
1.26 ! nicm 206: static const char *const tty_feature_strikethrough_capabilities[] = {
1.10 nicm 207: "smxx=\\E[9m",
208: NULL
209: };
1.12 nicm 210: static const struct tty_feature tty_feature_strikethrough = {
1.10 nicm 211: "strikethrough",
212: tty_feature_strikethrough_capabilities,
213: 0
214: };
215:
1.1 nicm 216: /* Terminal supports synchronized updates. */
1.26 ! nicm 217: static const char *const tty_feature_sync_capabilities[] = {
1.2 nicm 218: "Sync=\\EP=%p1%ds\\E\\\\",
1.1 nicm 219: NULL
220: };
1.12 nicm 221: static const struct tty_feature tty_feature_sync = {
1.1 nicm 222: "sync",
223: tty_feature_sync_capabilities,
224: 0
225: };
226:
1.14 nicm 227: /* Terminal supports extended keys. */
1.26 ! nicm 228: static const char *const tty_feature_extkeys_capabilities[] = {
1.14 nicm 229: "Eneks=\\E[>4;1m",
230: "Dseks=\\E[>4m",
231: NULL
232: };
233: static const struct tty_feature tty_feature_extkeys = {
234: "extkeys",
235: tty_feature_extkeys_capabilities,
236: 0
237: };
238:
1.1 nicm 239: /* Terminal supports DECSLRM margins. */
1.26 ! nicm 240: static const char *const tty_feature_margins_capabilities[] = {
1.7 nicm 241: "Enmg=\\E[?69h",
242: "Dsmg=\\E[?69l",
243: "Clmg=\\E[s",
244: "Cmg=\\E[%i%p1%d;%p2%ds",
245: NULL
246: };
1.12 nicm 247: static const struct tty_feature tty_feature_margins = {
1.1 nicm 248: "margins",
1.7 nicm 249: tty_feature_margins_capabilities,
1.1 nicm 250: TERM_DECSLRM
251: };
252:
253: /* Terminal supports DECFRA rectangle fill. */
1.26 ! nicm 254: static const char *const tty_feature_rectfill_capabilities[] = {
1.19 nicm 255: "Rect",
256: NULL
257: };
1.12 nicm 258: static const struct tty_feature tty_feature_rectfill = {
1.1 nicm 259: "rectfill",
1.19 nicm 260: tty_feature_rectfill_capabilities,
1.1 nicm 261: TERM_DECFRA
262: };
263:
1.24 nicm 264: /* Use builtin function keys only. */
1.26 ! nicm 265: static const char *const tty_feature_ignorefkeys_capabilities[] = {
1.24 nicm 266: "kf0@",
267: "kf1@",
268: "kf2@",
269: "kf3@",
270: "kf4@",
271: "kf5@",
272: "kf6@",
273: "kf7@",
274: "kf8@",
275: "kf9@",
276: "kf10@",
277: "kf11@",
278: "kf12@",
279: "kf13@",
280: "kf14@",
281: "kf15@",
282: "kf16@",
283: "kf17@",
284: "kf18@",
285: "kf19@",
286: "kf20@",
287: "kf21@",
288: "kf22@",
289: "kf23@",
290: "kf24@",
291: "kf25@",
292: "kf26@",
293: "kf27@",
294: "kf28@",
295: "kf29@",
296: "kf30@",
297: "kf31@",
298: "kf32@",
299: "kf33@",
300: "kf34@",
301: "kf35@",
302: "kf36@",
303: "kf37@",
304: "kf38@",
305: "kf39@",
306: "kf40@",
307: "kf41@",
308: "kf42@",
309: "kf43@",
310: "kf44@",
311: "kf45@",
312: "kf46@",
313: "kf47@",
314: "kf48@",
315: "kf49@",
316: "kf50@",
317: "kf51@",
318: "kf52@",
319: "kf53@",
320: "kf54@",
321: "kf55@",
322: "kf56@",
323: "kf57@",
324: "kf58@",
325: "kf59@",
326: "kf60@",
327: "kf61@",
328: "kf62@",
329: "kf63@",
330: NULL
331: };
332: static const struct tty_feature tty_feature_ignorefkeys = {
333: "ignorefkeys",
334: tty_feature_ignorefkeys_capabilities,
335: 0
336: };
337:
1.1 nicm 338: /* Available terminal features. */
1.26 ! nicm 339: static const struct tty_feature *const tty_features[] = {
1.1 nicm 340: &tty_feature_256,
1.11 nicm 341: &tty_feature_bpaste,
1.12 nicm 342: &tty_feature_ccolour,
1.1 nicm 343: &tty_feature_clipboard,
1.25 nicm 344: &tty_feature_hyperlinks,
1.1 nicm 345: &tty_feature_cstyle,
1.14 nicm 346: &tty_feature_extkeys,
1.12 nicm 347: &tty_feature_focus,
1.24 nicm 348: &tty_feature_ignorefkeys,
1.1 nicm 349: &tty_feature_margins,
1.20 nicm 350: &tty_feature_mouse,
1.22 nicm 351: &tty_feature_osc7,
1.1 nicm 352: &tty_feature_overline,
353: &tty_feature_rectfill,
354: &tty_feature_rgb,
1.10 nicm 355: &tty_feature_strikethrough,
1.1 nicm 356: &tty_feature_sync,
357: &tty_feature_title,
358: &tty_feature_usstyle
359: };
360:
361: void
362: tty_add_features(int *feat, const char *s, const char *separators)
363: {
364: const struct tty_feature *tf;
365: char *next, *loop, *copy;
366: u_int i;
1.7 nicm 367:
1.9 nicm 368: log_debug("adding terminal features %s", s);
1.1 nicm 369:
370: loop = copy = xstrdup(s);
371: while ((next = strsep(&loop, separators)) != NULL) {
372: for (i = 0; i < nitems(tty_features); i++) {
373: tf = tty_features[i];
374: if (strcasecmp(tf->name, next) == 0)
375: break;
376: }
377: if (i == nitems(tty_features)) {
378: log_debug("unknown terminal feature: %s", next);
379: break;
380: }
381: if (~(*feat) & (1 << i)) {
382: log_debug("adding terminal feature: %s", tf->name);
383: (*feat) |= (1 << i);
384: }
385: }
386: free(copy);
387: }
388:
389: const char *
390: tty_get_features(int feat)
391: {
392: const struct tty_feature *tf;
393: static char s[512];
394: u_int i;
395:
396: *s = '\0';
397: for (i = 0; i < nitems(tty_features); i++) {
398: if (~feat & (1 << i))
399: continue;
400: tf = tty_features[i];
401:
402: strlcat(s, tf->name, sizeof s);
403: strlcat(s, ",", sizeof s);
404: }
405: if (*s != '\0')
406: s[strlen(s) - 1] = '\0';
407: return (s);
408: }
409:
1.3 nicm 410: int
1.1 nicm 411: tty_apply_features(struct tty_term *term, int feat)
412: {
1.26 ! nicm 413: const struct tty_feature *tf;
! 414: const char *const *capability;
! 415: u_int i;
1.1 nicm 416:
417: if (feat == 0)
1.3 nicm 418: return (0);
1.1 nicm 419: log_debug("applying terminal features: %s", tty_get_features(feat));
420:
421: for (i = 0; i < nitems(tty_features); i++) {
422: if ((term->features & (1 << i)) || (~feat & (1 << i)))
423: continue;
424: tf = tty_features[i];
425:
426: log_debug("applying terminal feature: %s", tf->name);
427: if (tf->capabilities != NULL) {
428: capability = tf->capabilities;
429: while (*capability != NULL) {
430: log_debug("adding capability: %s", *capability);
431: tty_term_apply(term, *capability, 1);
432: capability++;
433: }
434: }
435: term->flags |= tf->flags;
436: }
1.3 nicm 437: if ((term->features | feat) == term->features)
438: return (0);
1.1 nicm 439: term->features |= feat;
1.3 nicm 440: return (1);
1.9 nicm 441: }
442:
443: void
444: tty_default_features(int *feat, const char *name, u_int version)
445: {
1.26 ! nicm 446: static const struct {
1.9 nicm 447: const char *name;
448: u_int version;
449: const char *features;
450: } table[] = {
1.16 nicm 451: #define TTY_FEATURES_BASE_MODERN_XTERM \
1.20 nicm 452: "256,RGB,bpaste,clipboard,mouse,strikethrough,title"
1.9 nicm 453: { .name = "mintty",
1.16 nicm 454: .features = TTY_FEATURES_BASE_MODERN_XTERM
1.18 nicm 455: ",ccolour,cstyle,extkeys,margins,overline,usstyle"
1.9 nicm 456: },
457: { .name = "tmux",
1.16 nicm 458: .features = TTY_FEATURES_BASE_MODERN_XTERM
1.25 nicm 459: ",ccolour,cstyle,focus,overline,usstyle,hyperlinks"
1.9 nicm 460: },
461: { .name = "rxvt-unicode",
1.24 nicm 462: .features = "256,bpaste,ccolour,cstyle,mouse,title,ignorefkeys"
1.9 nicm 463: },
464: { .name = "iTerm2",
1.16 nicm 465: .features = TTY_FEATURES_BASE_MODERN_XTERM
1.25 nicm 466: ",cstyle,extkeys,margins,usstyle,sync,osc7,hyperlinks"
1.9 nicm 467: },
468: { .name = "XTerm",
1.19 nicm 469: /*
470: * xterm also supports DECSLRM and DECFRA, but they can be
471: * disabled so not set it here - they will be added if
472: * secondary DA shows VT420.
473: */
1.16 nicm 474: .features = TTY_FEATURES_BASE_MODERN_XTERM
1.19 nicm 475: ",ccolour,cstyle,extkeys,focus"
1.9 nicm 476: }
477: };
478: u_int i;
479:
480: for (i = 0; i < nitems(table); i++) {
481: if (strcmp(table[i].name, name) != 0)
482: continue;
483: if (version != 0 && version < table[i].version)
484: continue;
485: tty_add_features(feat, table[i].features, ",");
486: }
1.1 nicm 487: }