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 /* Terminal has sixel capability. */
339 static const char *const tty_feature_sixel_capabilities
[] = {
343 static const struct tty_feature tty_feature_sixel
= {
345 tty_feature_sixel_capabilities
,
349 /* Available terminal features. */
350 static const struct tty_feature
*const tty_features
[] = {
353 &tty_feature_ccolour
,
354 &tty_feature_clipboard
,
355 &tty_feature_hyperlinks
,
357 &tty_feature_extkeys
,
359 &tty_feature_ignorefkeys
,
360 &tty_feature_margins
,
363 &tty_feature_overline
,
364 &tty_feature_rectfill
,
367 &tty_feature_strikethrough
,
374 tty_add_features(int *feat
, const char *s
, const char *separators
)
376 const struct tty_feature
*tf
;
377 char *next
, *loop
, *copy
;
380 log_debug("adding terminal features %s", s
);
382 loop
= copy
= xstrdup(s
);
383 while ((next
= strsep(&loop
, separators
)) != NULL
) {
384 for (i
= 0; i
< nitems(tty_features
); i
++) {
385 tf
= tty_features
[i
];
386 if (strcasecmp(tf
->name
, next
) == 0)
389 if (i
== nitems(tty_features
)) {
390 log_debug("unknown terminal feature: %s", next
);
393 if (~(*feat
) & (1 << i
)) {
394 log_debug("adding terminal feature: %s", tf
->name
);
402 tty_get_features(int feat
)
404 const struct tty_feature
*tf
;
409 for (i
= 0; i
< nitems(tty_features
); i
++) {
410 if (~feat
& (1 << i
))
412 tf
= tty_features
[i
];
414 strlcat(s
, tf
->name
, sizeof s
);
415 strlcat(s
, ",", sizeof s
);
418 s
[strlen(s
) - 1] = '\0';
423 tty_apply_features(struct tty_term
*term
, int feat
)
425 const struct tty_feature
*tf
;
426 const char *const *capability
;
431 log_debug("applying terminal features: %s", tty_get_features(feat
));
433 for (i
= 0; i
< nitems(tty_features
); i
++) {
434 if ((term
->features
& (1 << i
)) || (~feat
& (1 << i
)))
436 tf
= tty_features
[i
];
438 log_debug("applying terminal feature: %s", tf
->name
);
439 if (tf
->capabilities
!= NULL
) {
440 capability
= tf
->capabilities
;
441 while (*capability
!= NULL
) {
442 log_debug("adding capability: %s", *capability
);
443 tty_term_apply(term
, *capability
, 1);
447 term
->flags
|= tf
->flags
;
449 if ((term
->features
| feat
) == term
->features
)
451 term
->features
|= feat
;
456 tty_default_features(int *feat
, const char *name
, u_int version
)
458 static const struct {
461 const char *features
;
463 #define TTY_FEATURES_BASE_MODERN_XTERM \
464 "256,RGB,bpaste,clipboard,mouse,strikethrough,title"
466 .features
= TTY_FEATURES_BASE_MODERN_XTERM
467 ",ccolour,cstyle,extkeys,margins,overline,usstyle"
470 .features
= TTY_FEATURES_BASE_MODERN_XTERM
471 ",ccolour,cstyle,focus,overline,usstyle,hyperlinks"
473 { .name
= "rxvt-unicode",
474 .features
= "256,bpaste,ccolour,cstyle,mouse,title,ignorefkeys"
477 .features
= TTY_FEATURES_BASE_MODERN_XTERM
478 ",cstyle,extkeys,margins,usstyle,sync,osc7,hyperlinks"
482 * xterm also supports DECSLRM and DECFRA, but they can be
483 * disabled so not set it here - they will be added if
484 * secondary DA shows VT420.
486 .features
= TTY_FEATURES_BASE_MODERN_XTERM
487 ",ccolour,cstyle,extkeys,focus"
492 for (i
= 0; i
< nitems(table
); i
++) {
493 if (strcmp(table
[i
].name
, name
) != 0)
495 if (version
!= 0 && version
< table
[i
].version
)
497 tty_add_features(feat
, table
[i
].features
, ",");