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