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 *const *capabilities
;
44 /* Terminal has xterm(1) title setting. */
45 static const char *const 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 OSC 7 working directory. */
57 static const char *const tty_feature_osc7_capabilities
[] = {
62 static const struct tty_feature tty_feature_osc7
= {
64 tty_feature_osc7_capabilities
,
68 /* Terminal has mouse support. */
69 static const char *const tty_feature_mouse_capabilities
[] = {
73 static const struct tty_feature tty_feature_mouse
= {
75 tty_feature_mouse_capabilities
,
79 /* Terminal can set the clipboard with OSC 52. */
80 static const char *const tty_feature_clipboard_capabilities
[] = {
81 "Ms=\\E]52;%p1%s;%p2%s\\a",
84 static const struct tty_feature tty_feature_clipboard
= {
86 tty_feature_clipboard_capabilities
,
90 /* Terminal supports OSC 8 hyperlinks. */
91 static const char *const tty_feature_hyperlinks_capabilities
[] = {
92 "*:Hls=\\E]8;%?%p1%l%tid=%p1%s%;;%p2%s\\E\\\\",
95 static const struct tty_feature tty_feature_hyperlinks
= {
97 tty_feature_hyperlinks_capabilities
,
102 * Terminal supports RGB colour. This replaces setab and setaf also since
103 * terminals with RGB have versions that do not allow setting colours from the
106 static const char *const tty_feature_rgb_capabilities
[] = {
108 "setrgbf=\\E[38;2;%p1%d;%p2%d;%p3%dm",
109 "setrgbb=\\E[48;2;%p1%d;%p2%d;%p3%dm",
110 "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
111 "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
114 static const struct tty_feature tty_feature_rgb
= {
116 tty_feature_rgb_capabilities
,
117 TERM_256COLOURS
|TERM_RGBCOLOURS
120 /* Terminal supports 256 colours. */
121 static const char *const tty_feature_256_capabilities
[] = {
123 "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
124 "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
127 static const struct tty_feature tty_feature_256
= {
129 tty_feature_256_capabilities
,
133 /* Terminal supports overline. */
134 static const char *const tty_feature_overline_capabilities
[] = {
138 static const struct tty_feature tty_feature_overline
= {
140 tty_feature_overline_capabilities
,
144 /* Terminal supports underscore styles. */
145 static const char *const tty_feature_usstyle_capabilities
[] = {
146 "Smulx=\\E[4::%p1%dm",
147 "Setulc=\\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m",
151 static const struct tty_feature tty_feature_usstyle
= {
153 tty_feature_usstyle_capabilities
,
157 /* Terminal supports bracketed paste. */
158 static const char *const tty_feature_bpaste_capabilities
[] = {
163 static const struct tty_feature tty_feature_bpaste
= {
165 tty_feature_bpaste_capabilities
,
169 /* Terminal supports focus reporting. */
170 static const char *const tty_feature_focus_capabilities
[] = {
175 static const struct tty_feature tty_feature_focus
= {
177 tty_feature_focus_capabilities
,
181 /* Terminal supports cursor styles. */
182 static const char *const tty_feature_cstyle_capabilities
[] = {
187 static const struct tty_feature tty_feature_cstyle
= {
189 tty_feature_cstyle_capabilities
,
193 /* Terminal supports cursor colours. */
194 static const char *const tty_feature_ccolour_capabilities
[] = {
195 "Cs=\\E]12;%p1%s\\a",
199 static const struct tty_feature tty_feature_ccolour
= {
201 tty_feature_ccolour_capabilities
,
205 /* Terminal supports strikethrough. */
206 static const char *const tty_feature_strikethrough_capabilities
[] = {
210 static const struct tty_feature tty_feature_strikethrough
= {
212 tty_feature_strikethrough_capabilities
,
216 /* Terminal supports synchronized updates. */
217 static const char *const tty_feature_sync_capabilities
[] = {
218 "Sync=\\EP=%p1%ds\\E\\\\",
221 static const struct tty_feature tty_feature_sync
= {
223 tty_feature_sync_capabilities
,
227 /* Terminal supports extended keys. */
228 static const char *const tty_feature_extkeys_capabilities
[] = {
233 static const struct tty_feature tty_feature_extkeys
= {
235 tty_feature_extkeys_capabilities
,
239 /* Terminal supports DECSLRM margins. */
240 static const char *const tty_feature_margins_capabilities
[] = {
244 "Cmg=\\E[%i%p1%d;%p2%ds",
247 static const struct tty_feature tty_feature_margins
= {
249 tty_feature_margins_capabilities
,
253 /* Terminal supports DECFRA rectangle fill. */
254 static const char *const tty_feature_rectfill_capabilities
[] = {
258 static const struct tty_feature tty_feature_rectfill
= {
260 tty_feature_rectfill_capabilities
,
264 /* Use builtin function keys only. */
265 static const char *const tty_feature_ignorefkeys_capabilities
[] = {
332 static const struct tty_feature tty_feature_ignorefkeys
= {
334 tty_feature_ignorefkeys_capabilities
,
338 /* Available terminal features. */
339 static const struct tty_feature
*const tty_features
[] = {
342 &tty_feature_ccolour
,
343 &tty_feature_clipboard
,
344 &tty_feature_hyperlinks
,
346 &tty_feature_extkeys
,
348 &tty_feature_ignorefkeys
,
349 &tty_feature_margins
,
352 &tty_feature_overline
,
353 &tty_feature_rectfill
,
355 &tty_feature_strikethrough
,
362 tty_add_features(int *feat
, const char *s
, const char *separators
)
364 const struct tty_feature
*tf
;
365 char *next
, *loop
, *copy
;
368 log_debug("adding terminal features %s", s
);
370 loop
= copy
= xstrdup(s
);
371 while ((next
= strsep(&loop
, separators
)) != NULL
) {
372 for (i
= 0; i
< nitems(tty_features
); i
++) {
373 tf
= tty_features
[i
];
374 if (strcasecmp(tf
->name
, next
) == 0)
377 if (i
== nitems(tty_features
)) {
378 log_debug("unknown terminal feature: %s", next
);
381 if (~(*feat
) & (1 << i
)) {
382 log_debug("adding terminal feature: %s", tf
->name
);
390 tty_get_features(int feat
)
392 const struct tty_feature
*tf
;
397 for (i
= 0; i
< nitems(tty_features
); i
++) {
398 if (~feat
& (1 << i
))
400 tf
= tty_features
[i
];
402 strlcat(s
, tf
->name
, sizeof s
);
403 strlcat(s
, ",", sizeof s
);
406 s
[strlen(s
) - 1] = '\0';
411 tty_apply_features(struct tty_term
*term
, int feat
)
413 const struct tty_feature
*tf
;
414 const char *const *capability
;
419 log_debug("applying terminal features: %s", tty_get_features(feat
));
421 for (i
= 0; i
< nitems(tty_features
); i
++) {
422 if ((term
->features
& (1 << i
)) || (~feat
& (1 << i
)))
424 tf
= tty_features
[i
];
426 log_debug("applying terminal feature: %s", tf
->name
);
427 if (tf
->capabilities
!= NULL
) {
428 capability
= tf
->capabilities
;
429 while (*capability
!= NULL
) {
430 log_debug("adding capability: %s", *capability
);
431 tty_term_apply(term
, *capability
, 1);
435 term
->flags
|= tf
->flags
;
437 if ((term
->features
| feat
) == term
->features
)
439 term
->features
|= feat
;
444 tty_default_features(int *feat
, const char *name
, u_int version
)
446 static const struct {
449 const char *features
;
451 #define TTY_FEATURES_BASE_MODERN_XTERM \
452 "256,RGB,bpaste,clipboard,mouse,strikethrough,title"
454 .features
= TTY_FEATURES_BASE_MODERN_XTERM
455 ",ccolour,cstyle,extkeys,margins,overline,usstyle"
458 .features
= TTY_FEATURES_BASE_MODERN_XTERM
459 ",ccolour,cstyle,focus,overline,usstyle,hyperlinks"
461 { .name
= "rxvt-unicode",
462 .features
= "256,bpaste,ccolour,cstyle,mouse,title,ignorefkeys"
465 .features
= TTY_FEATURES_BASE_MODERN_XTERM
466 ",cstyle,extkeys,margins,usstyle,sync,osc7,hyperlinks"
470 * xterm also supports DECSLRM and DECFRA, but they can be
471 * disabled so not set it here - they will be added if
472 * secondary DA shows VT420.
474 .features
= TTY_FEATURES_BASE_MODERN_XTERM
475 ",ccolour,cstyle,extkeys,focus"
480 for (i
= 0; i
< nitems(table
); i
++) {
481 if (strcmp(table
[i
].name
, name
) != 0)
483 if (version
!= 0 && version
< table
[i
].version
)
485 tty_add_features(feat
, table
[i
].features
, ",");