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>
28 * - default colours (under AX or op capabilities);
29 * - AIX colours (under colors >= 16);
30 * - alternate escape (if terminal is VT100-like).
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. */
40 const char **capabilities
;
44 /* Terminal has xterm(1) title setting. */
45 static const char *tty_feature_title_capabilities
[] = {
46 "tsl=\\E]0;", /* should be using TS really */
50 static const struct tty_feature tty_feature_title
= {
52 tty_feature_title_capabilities
,
56 /* Terminal has mouse support. */
57 static const char *tty_feature_mouse_capabilities
[] = {
61 static const struct tty_feature tty_feature_mouse
= {
63 tty_feature_mouse_capabilities
,
67 /* Terminal can set the clipboard with OSC 52. */
68 static const char *tty_feature_clipboard_capabilities
[] = {
69 "Ms=\\E]52;%p1%s;%p2%s\\a",
72 static const struct tty_feature tty_feature_clipboard
= {
74 tty_feature_clipboard_capabilities
,
79 * Terminal supports RGB colour. This replaces setab and setaf also since
80 * terminals with RGB have versions that do not allow setting colours from the
83 static const char *tty_feature_rgb_capabilities
[] = {
85 "setrgbf=\\E[38;2;%p1%d;%p2%d;%p3%dm",
86 "setrgbb=\\E[48;2;%p1%d;%p2%d;%p3%dm",
87 "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
88 "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
91 static const struct tty_feature tty_feature_rgb
= {
93 tty_feature_rgb_capabilities
,
94 TERM_256COLOURS
|TERM_RGBCOLOURS
97 /* Terminal supports 256 colours. */
98 static const char *tty_feature_256_capabilities
[] = {
100 "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
101 "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
104 static const struct tty_feature tty_feature_256
= {
106 tty_feature_256_capabilities
,
110 /* Terminal supports overline. */
111 static const char *tty_feature_overline_capabilities
[] = {
115 static const struct tty_feature tty_feature_overline
= {
117 tty_feature_overline_capabilities
,
121 /* Terminal supports underscore styles. */
122 static const char *tty_feature_usstyle_capabilities
[] = {
123 "Smulx=\\E[4::%p1%dm",
124 "Setulc=\\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m",
128 static const struct tty_feature tty_feature_usstyle
= {
130 tty_feature_usstyle_capabilities
,
134 /* Terminal supports bracketed paste. */
135 static const char *tty_feature_bpaste_capabilities
[] = {
140 static const struct tty_feature tty_feature_bpaste
= {
142 tty_feature_bpaste_capabilities
,
146 /* Terminal supports focus reporting. */
147 static const char *tty_feature_focus_capabilities
[] = {
152 static const struct tty_feature tty_feature_focus
= {
154 tty_feature_focus_capabilities
,
158 /* Terminal supports cursor styles. */
159 static const char *tty_feature_cstyle_capabilities
[] = {
164 static const struct tty_feature tty_feature_cstyle
= {
166 tty_feature_cstyle_capabilities
,
170 /* Terminal supports cursor colours. */
171 static const char *tty_feature_ccolour_capabilities
[] = {
172 "Cs=\\E]12;%p1%s\\a",
176 static const struct tty_feature tty_feature_ccolour
= {
178 tty_feature_ccolour_capabilities
,
182 /* Terminal supports strikethrough. */
183 static const char *tty_feature_strikethrough_capabilities
[] = {
187 static const struct tty_feature tty_feature_strikethrough
= {
189 tty_feature_strikethrough_capabilities
,
193 /* Terminal supports synchronized updates. */
194 static const char *tty_feature_sync_capabilities
[] = {
195 "Sync=\\EP=%p1%ds\\E\\\\",
198 static const struct tty_feature tty_feature_sync
= {
200 tty_feature_sync_capabilities
,
204 /* Terminal supports extended keys. */
205 static const char *tty_feature_extkeys_capabilities
[] = {
210 static const struct tty_feature tty_feature_extkeys
= {
212 tty_feature_extkeys_capabilities
,
216 /* Terminal supports DECSLRM margins. */
217 static const char *tty_feature_margins_capabilities
[] = {
221 "Cmg=\\E[%i%p1%d;%p2%ds",
224 static const struct tty_feature tty_feature_margins
= {
226 tty_feature_margins_capabilities
,
230 /* Terminal supports DECFRA rectangle fill. */
231 static const char *tty_feature_rectfill_capabilities
[] = {
235 static const struct tty_feature tty_feature_rectfill
= {
237 tty_feature_rectfill_capabilities
,
241 /* Available terminal features. */
242 static const struct tty_feature
*tty_features
[] = {
245 &tty_feature_ccolour
,
246 &tty_feature_clipboard
,
248 &tty_feature_extkeys
,
250 &tty_feature_margins
,
252 &tty_feature_overline
,
253 &tty_feature_rectfill
,
255 &tty_feature_strikethrough
,
262 tty_add_features(int *feat
, const char *s
, const char *separators
)
264 const struct tty_feature
*tf
;
265 char *next
, *loop
, *copy
;
268 log_debug("adding terminal features %s", s
);
270 loop
= copy
= xstrdup(s
);
271 while ((next
= strsep(&loop
, separators
)) != NULL
) {
272 for (i
= 0; i
< nitems(tty_features
); i
++) {
273 tf
= tty_features
[i
];
274 if (strcasecmp(tf
->name
, next
) == 0)
277 if (i
== nitems(tty_features
)) {
278 log_debug("unknown terminal feature: %s", next
);
281 if (~(*feat
) & (1 << i
)) {
282 log_debug("adding terminal feature: %s", tf
->name
);
290 tty_get_features(int feat
)
292 const struct tty_feature
*tf
;
297 for (i
= 0; i
< nitems(tty_features
); i
++) {
298 if (~feat
& (1 << i
))
300 tf
= tty_features
[i
];
302 strlcat(s
, tf
->name
, sizeof s
);
303 strlcat(s
, ",", sizeof s
);
306 s
[strlen(s
) - 1] = '\0';
311 tty_apply_features(struct tty_term
*term
, int feat
)
313 const struct tty_feature
*tf
;
314 const char **capability
;
319 log_debug("applying terminal features: %s", tty_get_features(feat
));
321 for (i
= 0; i
< nitems(tty_features
); i
++) {
322 if ((term
->features
& (1 << i
)) || (~feat
& (1 << i
)))
324 tf
= tty_features
[i
];
326 log_debug("applying terminal feature: %s", tf
->name
);
327 if (tf
->capabilities
!= NULL
) {
328 capability
= tf
->capabilities
;
329 while (*capability
!= NULL
) {
330 log_debug("adding capability: %s", *capability
);
331 tty_term_apply(term
, *capability
, 1);
335 term
->flags
|= tf
->flags
;
337 if ((term
->features
| feat
) == term
->features
)
339 term
->features
|= feat
;
344 tty_default_features(int *feat
, const char *name
, u_int version
)
349 const char *features
;
351 #define TTY_FEATURES_BASE_MODERN_XTERM \
352 "256,RGB,bpaste,clipboard,mouse,strikethrough,title"
354 .features
= TTY_FEATURES_BASE_MODERN_XTERM
355 ",ccolour,cstyle,extkeys,margins,overline,usstyle"
358 .features
= TTY_FEATURES_BASE_MODERN_XTERM
359 ",ccolour,cstyle,focus,overline,usstyle"
361 { .name
= "rxvt-unicode",
362 .features
= "256,bpaste,ccolour,cstyle,mouse,title"
365 .features
= TTY_FEATURES_BASE_MODERN_XTERM
366 ",cstyle,extkeys,margins,usstyle,sync"
370 * xterm also supports DECSLRM and DECFRA, but they can be
371 * disabled so not set it here - they will be added if
372 * secondary DA shows VT420.
374 .features
= TTY_FEATURES_BASE_MODERN_XTERM
375 ",ccolour,cstyle,extkeys,focus"
380 for (i
= 0; i
< nitems(table
); i
++) {
381 if (strcmp(table
[i
].name
, name
) != 0)
383 if (version
!= 0 && version
< table
[i
].version
)
385 tty_add_features(feat
, table
[i
].features
, ",");