xref: /openbsd-src/usr.bin/tmux/tty-features.c (revision d89bc9f8837a69f2e5273e4a839ba106b53b1b41)
1 /* $OpenBSD: tty-features.c,v 1.2 2020/04/20 13:38:48 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  * - bracket paste (sent if application asks for it);
29  * - mouse (under kmous capability);
30  * - focus events (under focus-events option);
31  * - default colours (under AX or op capabilities);
32  * - AIX colours (under colors >= 16);
33  * - alternate escape (under XT).
34  *
35  * Also:
36  * - XT is used to decide whether to send DA and DSR,
37  * - DECSLRM and DECFRA use a flag instead of capabilities.
38  * - UTF-8 is a separate flag on the client; needed for unattached clients.
39  */
40 
41 /* A named terminal feature. */
42 struct tty_feature {
43 	const char	 *name;
44 	const char	**capabilities;
45 	int		  flags;
46 };
47 
48 /* Terminal has xterm(1) title setting. */
49 static const char *tty_feature_title_capabilities[] = {
50 	"tsl=\\E]0;", /* should be using TS really */
51 	"fsl=\\a",
52 	NULL
53 };
54 static struct tty_feature tty_feature_title = {
55 	"title",
56 	tty_feature_title_capabilities,
57 	0
58 };
59 
60 /* Terminal can set the clipboard with OSC 52. */
61 static const char *tty_feature_clipboard_capabilities[] = {
62 	"Ms=\\E]52;%p1%s;%p2%s\\a",
63 	NULL
64 };
65 static struct tty_feature tty_feature_clipboard = {
66 	"clipboard",
67 	tty_feature_clipboard_capabilities,
68 	0
69 };
70 
71 /*
72  * Terminal supports RGB colour. This replaces setab and setaf also since
73  * terminals with RGB have versions that do not allow setting colours from the
74  * 256 palette.
75  */
76 static const char *tty_feature_rgb_capabilities[] = {
77 	"setrgbf=\\E[38;2;%p1%d;%p2%d;%p3%dm",
78 	"setrgbb=\\E[48;2;%p1%d;%p2%d;%p3%dm",
79 	"setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
80 	"setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
81 	NULL
82 };
83 static struct tty_feature tty_feature_rgb = {
84 	"RGB",
85 	tty_feature_rgb_capabilities,
86 	(TERM_256COLOURS|TERM_RGBCOLOURS)
87 };
88 
89 /* Terminal supports 256 colours. */
90 static const char *tty_feature_256_capabilities[] = {
91 	"setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
92 	"setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
93 	NULL
94 };
95 static struct tty_feature tty_feature_256 = {
96 	"256",
97 	tty_feature_256_capabilities,
98 	TERM_256COLOURS
99 };
100 
101 /* Terminal supports overline. */
102 static const char *tty_feature_overline_capabilities[] = {
103 	"Smol=\\E[53m",
104 	NULL
105 };
106 static struct tty_feature tty_feature_overline = {
107 	"overline",
108 	tty_feature_overline_capabilities,
109 	0
110 };
111 
112 /* Terminal supports underscore styles. */
113 static const char *tty_feature_usstyle_capabilities[] = {
114 	"Smulx=\E[4::%p1%dm",
115 	"Setulc=\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m",
116 	NULL
117 };
118 static struct tty_feature tty_feature_usstyle = {
119 	"usstyle",
120 	tty_feature_usstyle_capabilities,
121 	0
122 };
123 
124 /* Terminal supports cursor styles. */
125 static const char *tty_feature_cstyle_capabilities[] = {
126 	"Ss=\\E[%p1%d q",
127 	"Se=\\E[2 q",
128 	NULL
129 };
130 static struct tty_feature tty_feature_cstyle = {
131 	"cstyle",
132 	tty_feature_cstyle_capabilities,
133 	0
134 };
135 
136 /* Terminal supports cursor colours. */
137 static const char *tty_feature_ccolour_capabilities[] = {
138 	"Cs=\\E]12;%p1%s\\a",
139 	"Cr=\\E]112\\a",
140 	NULL
141 };
142 static struct tty_feature tty_feature_ccolour = {
143 	"ccolour",
144 	tty_feature_ccolour_capabilities,
145 	0
146 };
147 
148 /* Terminal supports synchronized updates. */
149 static const char *tty_feature_sync_capabilities[] = {
150 	"Sync=\\EP=%p1%ds\\E\\\\",
151 	NULL
152 };
153 static struct tty_feature tty_feature_sync = {
154 	"sync",
155 	tty_feature_sync_capabilities,
156 	0
157 };
158 
159 /* Terminal supports DECSLRM margins. */
160 static struct tty_feature tty_feature_margins = {
161 	"margins",
162 	NULL,
163 	TERM_DECSLRM
164 };
165 
166 /* Terminal supports DECFRA rectangle fill. */
167 static struct tty_feature tty_feature_rectfill = {
168 	"rectfill",
169 	NULL,
170 	TERM_DECFRA
171 };
172 
173 /* Available terminal features. */
174 static const struct tty_feature *tty_features[] = {
175 	&tty_feature_256,
176 	&tty_feature_clipboard,
177 	&tty_feature_ccolour,
178 	&tty_feature_cstyle,
179 	&tty_feature_margins,
180 	&tty_feature_overline,
181 	&tty_feature_rectfill,
182 	&tty_feature_rgb,
183 	&tty_feature_sync,
184 	&tty_feature_title,
185 	&tty_feature_usstyle
186 };
187 
188 void
189 tty_add_features(int *feat, const char *s, const char *separators)
190 {
191 	const struct tty_feature	 *tf;
192 	char				 *next, *loop, *copy;
193 	u_int				  i;
194 
195 	loop = copy = xstrdup(s);
196 	while ((next = strsep(&loop, separators)) != NULL) {
197 		for (i = 0; i < nitems(tty_features); i++) {
198 			tf = tty_features[i];
199 			if (strcasecmp(tf->name, next) == 0)
200 				break;
201 		}
202 		if (i == nitems(tty_features)) {
203 			log_debug("unknown terminal feature: %s", next);
204 			break;
205 		}
206 		if (~(*feat) & (1 << i)) {
207 			log_debug("adding terminal feature: %s", tf->name);
208 			(*feat) |= (1 << i);
209 		}
210 	}
211 	free(copy);
212 }
213 
214 const char *
215 tty_get_features(int feat)
216 {
217 	const struct tty_feature	*tf;
218 	static char			 s[512];
219 	u_int				 i;
220 
221 	*s = '\0';
222 	for (i = 0; i < nitems(tty_features); i++) {
223 		if (~feat & (1 << i))
224 			continue;
225 		tf = tty_features[i];
226 
227 		strlcat(s, tf->name, sizeof s);
228 		strlcat(s, ",", sizeof s);
229 	}
230 	if (*s != '\0')
231 		s[strlen(s) - 1] = '\0';
232 	return (s);
233 }
234 
235 void
236 tty_apply_features(struct tty_term *term, int feat)
237 {
238 	const struct tty_feature	 *tf;
239 	const char			**capability;
240 	u_int				  i;
241 
242 	if (feat == 0)
243 		return;
244 	log_debug("applying terminal features: %s", tty_get_features(feat));
245 
246 	for (i = 0; i < nitems(tty_features); i++) {
247 		if ((term->features & (1 << i)) || (~feat & (1 << i)))
248 			continue;
249 		tf = tty_features[i];
250 
251 		log_debug("applying terminal feature: %s", tf->name);
252 		if (tf->capabilities != NULL) {
253 			capability = tf->capabilities;
254 			while (*capability != NULL) {
255 				log_debug("adding capability: %s", *capability);
256 				tty_term_apply(term, *capability, 1);
257 				capability++;
258 			}
259 		}
260 		term->flags |= tf->flags;
261 	}
262 	term->features |= feat;
263 }
264