iTerm2 has OSC 7, from Gregory Anders.
[tmux-openbsd.git] / tty-features.c
blob2848b4d6fb99109479c086528f106835a7d9842f
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2020 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
27 * Still hardcoded:
28 * - default colours (under AX or op capabilities);
29 * - AIX colours (under colors >= 16);
30 * - alternate escape (if terminal is VT100-like).
32 * Also:
33 * - DECFRA uses a flag instead of capabilities;
34 * - UTF-8 is a separate flag on the client; needed for unattached clients.
37 /* A named terminal feature. */
38 struct tty_feature {
39 const char *name;
40 const char **capabilities;
41 int flags;
44 /* Terminal has xterm(1) title setting. */
45 static const char *tty_feature_title_capabilities[] = {
46 "tsl=\\E]0;", /* should be using TS really */
47 "fsl=\\a",
48 NULL
50 static const struct tty_feature tty_feature_title = {
51 "title",
52 tty_feature_title_capabilities,
56 /* Terminal has OSC 7 working directory. */
57 static const char *tty_feature_osc7_capabilities[] = {
58 "Swd=\\E]7;",
59 "fsl=\\a",
60 NULL
62 static const struct tty_feature tty_feature_osc7 = {
63 "osc7",
64 tty_feature_osc7_capabilities,
68 /* Terminal has mouse support. */
69 static const char *tty_feature_mouse_capabilities[] = {
70 "kmous=\\E[M",
71 NULL
73 static const struct tty_feature tty_feature_mouse = {
74 "mouse",
75 tty_feature_mouse_capabilities,
79 /* Terminal can set the clipboard with OSC 52. */
80 static const char *tty_feature_clipboard_capabilities[] = {
81 "Ms=\\E]52;%p1%s;%p2%s\\a",
82 NULL
84 static const struct tty_feature tty_feature_clipboard = {
85 "clipboard",
86 tty_feature_clipboard_capabilities,
91 * Terminal supports RGB colour. This replaces setab and setaf also since
92 * terminals with RGB have versions that do not allow setting colours from the
93 * 256 palette.
95 static const char *tty_feature_rgb_capabilities[] = {
96 "AX",
97 "setrgbf=\\E[38;2;%p1%d;%p2%d;%p3%dm",
98 "setrgbb=\\E[48;2;%p1%d;%p2%d;%p3%dm",
99 "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
100 "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
101 NULL
103 static const struct tty_feature tty_feature_rgb = {
104 "RGB",
105 tty_feature_rgb_capabilities,
106 TERM_256COLOURS|TERM_RGBCOLOURS
109 /* Terminal supports 256 colours. */
110 static const char *tty_feature_256_capabilities[] = {
111 "AX",
112 "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
113 "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
114 NULL
116 static const struct tty_feature tty_feature_256 = {
117 "256",
118 tty_feature_256_capabilities,
119 TERM_256COLOURS
122 /* Terminal supports overline. */
123 static const char *tty_feature_overline_capabilities[] = {
124 "Smol=\\E[53m",
125 NULL
127 static const struct tty_feature tty_feature_overline = {
128 "overline",
129 tty_feature_overline_capabilities,
133 /* Terminal supports underscore styles. */
134 static const char *tty_feature_usstyle_capabilities[] = {
135 "Smulx=\\E[4::%p1%dm",
136 "Setulc=\\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m",
137 "ol=\\E[59m",
138 NULL
140 static const struct tty_feature tty_feature_usstyle = {
141 "usstyle",
142 tty_feature_usstyle_capabilities,
146 /* Terminal supports bracketed paste. */
147 static const char *tty_feature_bpaste_capabilities[] = {
148 "Enbp=\\E[?2004h",
149 "Dsbp=\\E[?2004l",
150 NULL
152 static const struct tty_feature tty_feature_bpaste = {
153 "bpaste",
154 tty_feature_bpaste_capabilities,
158 /* Terminal supports focus reporting. */
159 static const char *tty_feature_focus_capabilities[] = {
160 "Enfcs=\\E[?1004h",
161 "Dsfcs=\\E[?1004l",
162 NULL
164 static const struct tty_feature tty_feature_focus = {
165 "focus",
166 tty_feature_focus_capabilities,
170 /* Terminal supports cursor styles. */
171 static const char *tty_feature_cstyle_capabilities[] = {
172 "Ss=\\E[%p1%d q",
173 "Se=\\E[2 q",
174 NULL
176 static const struct tty_feature tty_feature_cstyle = {
177 "cstyle",
178 tty_feature_cstyle_capabilities,
182 /* Terminal supports cursor colours. */
183 static const char *tty_feature_ccolour_capabilities[] = {
184 "Cs=\\E]12;%p1%s\\a",
185 "Cr=\\E]112\\a",
186 NULL
188 static const struct tty_feature tty_feature_ccolour = {
189 "ccolour",
190 tty_feature_ccolour_capabilities,
194 /* Terminal supports strikethrough. */
195 static const char *tty_feature_strikethrough_capabilities[] = {
196 "smxx=\\E[9m",
197 NULL
199 static const struct tty_feature tty_feature_strikethrough = {
200 "strikethrough",
201 tty_feature_strikethrough_capabilities,
205 /* Terminal supports synchronized updates. */
206 static const char *tty_feature_sync_capabilities[] = {
207 "Sync=\\EP=%p1%ds\\E\\\\",
208 NULL
210 static const struct tty_feature tty_feature_sync = {
211 "sync",
212 tty_feature_sync_capabilities,
216 /* Terminal supports extended keys. */
217 static const char *tty_feature_extkeys_capabilities[] = {
218 "Eneks=\\E[>4;1m",
219 "Dseks=\\E[>4m",
220 NULL
222 static const struct tty_feature tty_feature_extkeys = {
223 "extkeys",
224 tty_feature_extkeys_capabilities,
228 /* Terminal supports DECSLRM margins. */
229 static const char *tty_feature_margins_capabilities[] = {
230 "Enmg=\\E[?69h",
231 "Dsmg=\\E[?69l",
232 "Clmg=\\E[s",
233 "Cmg=\\E[%i%p1%d;%p2%ds",
234 NULL
236 static const struct tty_feature tty_feature_margins = {
237 "margins",
238 tty_feature_margins_capabilities,
239 TERM_DECSLRM
242 /* Terminal supports DECFRA rectangle fill. */
243 static const char *tty_feature_rectfill_capabilities[] = {
244 "Rect",
245 NULL
247 static const struct tty_feature tty_feature_rectfill = {
248 "rectfill",
249 tty_feature_rectfill_capabilities,
250 TERM_DECFRA
253 /* Available terminal features. */
254 static const struct tty_feature *tty_features[] = {
255 &tty_feature_256,
256 &tty_feature_bpaste,
257 &tty_feature_ccolour,
258 &tty_feature_clipboard,
259 &tty_feature_cstyle,
260 &tty_feature_extkeys,
261 &tty_feature_focus,
262 &tty_feature_margins,
263 &tty_feature_mouse,
264 &tty_feature_osc7,
265 &tty_feature_overline,
266 &tty_feature_rectfill,
267 &tty_feature_rgb,
268 &tty_feature_strikethrough,
269 &tty_feature_sync,
270 &tty_feature_title,
271 &tty_feature_usstyle
274 void
275 tty_add_features(int *feat, const char *s, const char *separators)
277 const struct tty_feature *tf;
278 char *next, *loop, *copy;
279 u_int i;
281 log_debug("adding terminal features %s", s);
283 loop = copy = xstrdup(s);
284 while ((next = strsep(&loop, separators)) != NULL) {
285 for (i = 0; i < nitems(tty_features); i++) {
286 tf = tty_features[i];
287 if (strcasecmp(tf->name, next) == 0)
288 break;
290 if (i == nitems(tty_features)) {
291 log_debug("unknown terminal feature: %s", next);
292 break;
294 if (~(*feat) & (1 << i)) {
295 log_debug("adding terminal feature: %s", tf->name);
296 (*feat) |= (1 << i);
299 free(copy);
302 const char *
303 tty_get_features(int feat)
305 const struct tty_feature *tf;
306 static char s[512];
307 u_int i;
309 *s = '\0';
310 for (i = 0; i < nitems(tty_features); i++) {
311 if (~feat & (1 << i))
312 continue;
313 tf = tty_features[i];
315 strlcat(s, tf->name, sizeof s);
316 strlcat(s, ",", sizeof s);
318 if (*s != '\0')
319 s[strlen(s) - 1] = '\0';
320 return (s);
324 tty_apply_features(struct tty_term *term, int feat)
326 const struct tty_feature *tf;
327 const char **capability;
328 u_int i;
330 if (feat == 0)
331 return (0);
332 log_debug("applying terminal features: %s", tty_get_features(feat));
334 for (i = 0; i < nitems(tty_features); i++) {
335 if ((term->features & (1 << i)) || (~feat & (1 << i)))
336 continue;
337 tf = tty_features[i];
339 log_debug("applying terminal feature: %s", tf->name);
340 if (tf->capabilities != NULL) {
341 capability = tf->capabilities;
342 while (*capability != NULL) {
343 log_debug("adding capability: %s", *capability);
344 tty_term_apply(term, *capability, 1);
345 capability++;
348 term->flags |= tf->flags;
350 if ((term->features | feat) == term->features)
351 return (0);
352 term->features |= feat;
353 return (1);
356 void
357 tty_default_features(int *feat, const char *name, u_int version)
359 static struct {
360 const char *name;
361 u_int version;
362 const char *features;
363 } table[] = {
364 #define TTY_FEATURES_BASE_MODERN_XTERM \
365 "256,RGB,bpaste,clipboard,mouse,strikethrough,title"
366 { .name = "mintty",
367 .features = TTY_FEATURES_BASE_MODERN_XTERM
368 ",ccolour,cstyle,extkeys,margins,overline,usstyle"
370 { .name = "tmux",
371 .features = TTY_FEATURES_BASE_MODERN_XTERM
372 ",ccolour,cstyle,focus,overline,usstyle"
374 { .name = "rxvt-unicode",
375 .features = "256,bpaste,ccolour,cstyle,mouse,title"
377 { .name = "iTerm2",
378 .features = TTY_FEATURES_BASE_MODERN_XTERM
379 ",cstyle,extkeys,margins,usstyle,sync,osc7"
381 { .name = "XTerm",
383 * xterm also supports DECSLRM and DECFRA, but they can be
384 * disabled so not set it here - they will be added if
385 * secondary DA shows VT420.
387 .features = TTY_FEATURES_BASE_MODERN_XTERM
388 ",ccolour,cstyle,extkeys,focus"
391 u_int i;
393 for (i = 0; i < nitems(table); i++) {
394 if (strcmp(table[i].name, name) != 0)
395 continue;
396 if (version != 0 && version < table[i].version)
397 continue;
398 tty_add_features(feat, table[i].features, ",");