xref: /openbsd-src/usr.bin/tmux/tty-features.c (revision f84b1df5a16cdd762c93854218de246e79975d3b)
1 /* $OpenBSD: tty-features.c,v 1.22 2022/03/24 09:05:57 nicm Exp $ */
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);
30  * - alternate escape (if terminal is VT100-like).
31  *
32  * Also:
33  * - DECFRA uses a flag instead of capabilities;
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 {
39 	const char	 *name;
40 	const char	**capabilities;
41 	int		  flags;
42 };
43 
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
49 };
50 static const struct tty_feature tty_feature_title = {
51 	"title",
52 	tty_feature_title_capabilities,
53 	0
54 };
55 
56 /* Terminal has OSC 7 working directory. */
57 static const char *tty_feature_osc7_capabilities[] = {
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 
68 /* Terminal has mouse support. */
69 static const char *tty_feature_mouse_capabilities[] = {
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 
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
83 };
84 static const struct tty_feature tty_feature_clipboard = {
85 	"clipboard",
86 	tty_feature_clipboard_capabilities,
87 	0
88 };
89 
90 /*
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.
94  */
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
102 };
103 static const struct tty_feature tty_feature_rgb = {
104 	"RGB",
105 	tty_feature_rgb_capabilities,
106 	TERM_256COLOURS|TERM_RGBCOLOURS
107 };
108 
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
115 };
116 static const struct tty_feature tty_feature_256 = {
117 	"256",
118 	tty_feature_256_capabilities,
119 	TERM_256COLOURS
120 };
121 
122 /* Terminal supports overline. */
123 static const char *tty_feature_overline_capabilities[] = {
124 	"Smol=\\E[53m",
125 	NULL
126 };
127 static const struct tty_feature tty_feature_overline = {
128 	"overline",
129 	tty_feature_overline_capabilities,
130 	0
131 };
132 
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
139 };
140 static const struct tty_feature tty_feature_usstyle = {
141 	"usstyle",
142 	tty_feature_usstyle_capabilities,
143 	0
144 };
145 
146 /* Terminal supports bracketed paste. */
147 static const char *tty_feature_bpaste_capabilities[] = {
148 	"Enbp=\\E[?2004h",
149 	"Dsbp=\\E[?2004l",
150 	NULL
151 };
152 static const struct tty_feature tty_feature_bpaste = {
153 	"bpaste",
154 	tty_feature_bpaste_capabilities,
155 	0
156 };
157 
158 /* Terminal supports focus reporting. */
159 static const char *tty_feature_focus_capabilities[] = {
160 	"Enfcs=\\E[?1004h",
161 	"Dsfcs=\\E[?1004l",
162 	NULL
163 };
164 static const struct tty_feature tty_feature_focus = {
165 	"focus",
166 	tty_feature_focus_capabilities,
167 	0
168 };
169 
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
175 };
176 static const struct tty_feature tty_feature_cstyle = {
177 	"cstyle",
178 	tty_feature_cstyle_capabilities,
179 	0
180 };
181 
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
187 };
188 static const struct tty_feature tty_feature_ccolour = {
189 	"ccolour",
190 	tty_feature_ccolour_capabilities,
191 	0
192 };
193 
194 /* Terminal supports strikethrough. */
195 static const char *tty_feature_strikethrough_capabilities[] = {
196 	"smxx=\\E[9m",
197 	NULL
198 };
199 static const struct tty_feature tty_feature_strikethrough = {
200 	"strikethrough",
201 	tty_feature_strikethrough_capabilities,
202 	0
203 };
204 
205 /* Terminal supports synchronized updates. */
206 static const char *tty_feature_sync_capabilities[] = {
207 	"Sync=\\EP=%p1%ds\\E\\\\",
208 	NULL
209 };
210 static const struct tty_feature tty_feature_sync = {
211 	"sync",
212 	tty_feature_sync_capabilities,
213 	0
214 };
215 
216 /* Terminal supports extended keys. */
217 static const char *tty_feature_extkeys_capabilities[] = {
218 	"Eneks=\\E[>4;1m",
219 	"Dseks=\\E[>4m",
220 	NULL
221 };
222 static const struct tty_feature tty_feature_extkeys = {
223 	"extkeys",
224 	tty_feature_extkeys_capabilities,
225 	0
226 };
227 
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
235 };
236 static const struct tty_feature tty_feature_margins = {
237 	"margins",
238 	tty_feature_margins_capabilities,
239 	TERM_DECSLRM
240 };
241 
242 /* Terminal supports DECFRA rectangle fill. */
243 static const char *tty_feature_rectfill_capabilities[] = {
244 	"Rect",
245 	NULL
246 };
247 static const struct tty_feature tty_feature_rectfill = {
248 	"rectfill",
249 	tty_feature_rectfill_capabilities,
250 	TERM_DECFRA
251 };
252 
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
272 };
273 
274 void
275 tty_add_features(int *feat, const char *s, const char *separators)
276 {
277 	const struct tty_feature	 *tf;
278 	char				 *next, *loop, *copy;
279 	u_int				  i;
280 
281 	log_debug("adding terminal features %s", s);
282 
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;
289 		}
290 		if (i == nitems(tty_features)) {
291 			log_debug("unknown terminal feature: %s", next);
292 			break;
293 		}
294 		if (~(*feat) & (1 << i)) {
295 			log_debug("adding terminal feature: %s", tf->name);
296 			(*feat) |= (1 << i);
297 		}
298 	}
299 	free(copy);
300 }
301 
302 const char *
303 tty_get_features(int feat)
304 {
305 	const struct tty_feature	*tf;
306 	static char			 s[512];
307 	u_int				 i;
308 
309 	*s = '\0';
310 	for (i = 0; i < nitems(tty_features); i++) {
311 		if (~feat & (1 << i))
312 			continue;
313 		tf = tty_features[i];
314 
315 		strlcat(s, tf->name, sizeof s);
316 		strlcat(s, ",", sizeof s);
317 	}
318 	if (*s != '\0')
319 		s[strlen(s) - 1] = '\0';
320 	return (s);
321 }
322 
323 int
324 tty_apply_features(struct tty_term *term, int feat)
325 {
326 	const struct tty_feature	 *tf;
327 	const char			**capability;
328 	u_int				  i;
329 
330 	if (feat == 0)
331 		return (0);
332 	log_debug("applying terminal features: %s", tty_get_features(feat));
333 
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];
338 
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++;
346 			}
347 		}
348 		term->flags |= tf->flags;
349 	}
350 	if ((term->features | feat) == term->features)
351 		return (0);
352 	term->features |= feat;
353 	return (1);
354 }
355 
356 void
357 tty_default_features(int *feat, const char *name, u_int version)
358 {
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"
369 		},
370 		{ .name = "tmux",
371 		  .features = TTY_FEATURES_BASE_MODERN_XTERM
372 			      ",ccolour,cstyle,focus,overline,usstyle"
373 		},
374 		{ .name = "rxvt-unicode",
375 		  .features = "256,bpaste,ccolour,cstyle,mouse,title"
376 		},
377 		{ .name = "iTerm2",
378 		  .features = TTY_FEATURES_BASE_MODERN_XTERM
379 			      ",cstyle,extkeys,margins,usstyle,sync"
380 		},
381 		{ .name = "XTerm",
382 		  /*
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.
386 		   */
387 		  .features = TTY_FEATURES_BASE_MODERN_XTERM
388 			      ",ccolour,cstyle,extkeys,focus"
389 		}
390 	};
391 	u_int	i;
392 
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, ",");
399 	}
400 }
401