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>
24 #if defined(HAVE_CURSES_H)
26 #elif defined(HAVE_NCURSES_H)
34 * - default colours (under AX or op capabilities);
35 * - AIX colours (under colors >= 16);
36 * - alternate escape (if terminal is VT100-like).
39 * - DECFRA uses a flag instead of capabilities;
40 * - UTF-8 is a separate flag on the client; needed for unattached clients.
43 /* A named terminal feature. */
46 const char *const *capabilities
;
50 /* Terminal has xterm(1) title setting. */
51 static const char *const tty_feature_title_capabilities
[] = {
52 "tsl=\\E]0;", /* should be using TS really */
56 static const struct tty_feature tty_feature_title
= {
58 tty_feature_title_capabilities
,
62 /* Terminal has OSC 7 working directory. */
63 static const char *const tty_feature_osc7_capabilities
[] = {
68 static const struct tty_feature tty_feature_osc7
= {
70 tty_feature_osc7_capabilities
,
74 /* Terminal has mouse support. */
75 static const char *const tty_feature_mouse_capabilities
[] = {
79 static const struct tty_feature tty_feature_mouse
= {
81 tty_feature_mouse_capabilities
,
85 /* Terminal can set the clipboard with OSC 52. */
86 static const char *const tty_feature_clipboard_capabilities
[] = {
87 "Ms=\\E]52;%p1%s;%p2%s\\a",
90 static const struct tty_feature tty_feature_clipboard
= {
92 tty_feature_clipboard_capabilities
,
96 /* Terminal supports OSC 8 hyperlinks. */
97 static const char *tty_feature_hyperlinks_capabilities
[] = {
98 #if defined (__OpenBSD__) || (defined(NCURSES_VERSION_MAJOR) && \
99 (NCURSES_VERSION_MAJOR > 5 || \
100 (NCURSES_VERSION_MAJOR == 5 && NCURSES_VERSION_MINOR > 8)))
101 "*:Hls=\\E]8;%?%p1%l%tid=%p1%s%;;%p2%s\\E\\\\",
105 static const struct tty_feature tty_feature_hyperlinks
= {
107 tty_feature_hyperlinks_capabilities
,
112 * Terminal supports RGB colour. This replaces setab and setaf also since
113 * terminals with RGB have versions that do not allow setting colours from the
116 static const char *const tty_feature_rgb_capabilities
[] = {
118 "setrgbf=\\E[38;2;%p1%d;%p2%d;%p3%dm",
119 "setrgbb=\\E[48;2;%p1%d;%p2%d;%p3%dm",
120 "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
121 "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
124 static const struct tty_feature tty_feature_rgb
= {
126 tty_feature_rgb_capabilities
,
127 TERM_256COLOURS
|TERM_RGBCOLOURS
130 /* Terminal supports 256 colours. */
131 static const char *const tty_feature_256_capabilities
[] = {
133 "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
134 "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
137 static const struct tty_feature tty_feature_256
= {
139 tty_feature_256_capabilities
,
143 /* Terminal supports overline. */
144 static const char *const tty_feature_overline_capabilities
[] = {
148 static const struct tty_feature tty_feature_overline
= {
150 tty_feature_overline_capabilities
,
154 /* Terminal supports underscore styles. */
155 static const char *const tty_feature_usstyle_capabilities
[] = {
156 "Smulx=\\E[4::%p1%dm",
157 "Setulc=\\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m",
161 static const struct tty_feature tty_feature_usstyle
= {
163 tty_feature_usstyle_capabilities
,
167 /* Terminal supports bracketed paste. */
168 static const char *const tty_feature_bpaste_capabilities
[] = {
173 static const struct tty_feature tty_feature_bpaste
= {
175 tty_feature_bpaste_capabilities
,
179 /* Terminal supports focus reporting. */
180 static const char *const tty_feature_focus_capabilities
[] = {
185 static const struct tty_feature tty_feature_focus
= {
187 tty_feature_focus_capabilities
,
191 /* Terminal supports cursor styles. */
192 static const char *const tty_feature_cstyle_capabilities
[] = {
197 static const struct tty_feature tty_feature_cstyle
= {
199 tty_feature_cstyle_capabilities
,
203 /* Terminal supports cursor colours. */
204 static const char *const tty_feature_ccolour_capabilities
[] = {
205 "Cs=\\E]12;%p1%s\\a",
209 static const struct tty_feature tty_feature_ccolour
= {
211 tty_feature_ccolour_capabilities
,
215 /* Terminal supports strikethrough. */
216 static const char *const tty_feature_strikethrough_capabilities
[] = {
220 static const struct tty_feature tty_feature_strikethrough
= {
222 tty_feature_strikethrough_capabilities
,
226 /* Terminal supports synchronized updates. */
227 static const char *const tty_feature_sync_capabilities
[] = {
228 "Sync=\\EP=%p1%ds\\E\\\\",
231 static const struct tty_feature tty_feature_sync
= {
233 tty_feature_sync_capabilities
,
237 /* Terminal supports extended keys. */
238 static const char *const tty_feature_extkeys_capabilities
[] = {
243 static const struct tty_feature tty_feature_extkeys
= {
245 tty_feature_extkeys_capabilities
,
249 /* Terminal supports DECSLRM margins. */
250 static const char *const tty_feature_margins_capabilities
[] = {
254 "Cmg=\\E[%i%p1%d;%p2%ds",
257 static const struct tty_feature tty_feature_margins
= {
259 tty_feature_margins_capabilities
,
263 /* Terminal supports DECFRA rectangle fill. */
264 static const char *const tty_feature_rectfill_capabilities
[] = {
268 static const struct tty_feature tty_feature_rectfill
= {
270 tty_feature_rectfill_capabilities
,
274 /* Use builtin function keys only. */
275 static const char *const tty_feature_ignorefkeys_capabilities
[] = {
342 static const struct tty_feature tty_feature_ignorefkeys
= {
344 tty_feature_ignorefkeys_capabilities
,
348 /* Terminal has sixel capability. */
349 static const char *const tty_feature_sixel_capabilities
[] = {
353 static const struct tty_feature tty_feature_sixel
= {
355 tty_feature_sixel_capabilities
,
359 /* Available terminal features. */
360 static const struct tty_feature
*const tty_features
[] = {
363 &tty_feature_ccolour
,
364 &tty_feature_clipboard
,
365 &tty_feature_hyperlinks
,
367 &tty_feature_extkeys
,
369 &tty_feature_ignorefkeys
,
370 &tty_feature_margins
,
373 &tty_feature_overline
,
374 &tty_feature_rectfill
,
377 &tty_feature_strikethrough
,
384 tty_add_features(int *feat
, const char *s
, const char *separators
)
386 const struct tty_feature
*tf
;
387 char *next
, *loop
, *copy
;
390 log_debug("adding terminal features %s", s
);
392 loop
= copy
= xstrdup(s
);
393 while ((next
= strsep(&loop
, separators
)) != NULL
) {
394 for (i
= 0; i
< nitems(tty_features
); i
++) {
395 tf
= tty_features
[i
];
396 if (strcasecmp(tf
->name
, next
) == 0)
399 if (i
== nitems(tty_features
)) {
400 log_debug("unknown terminal feature: %s", next
);
403 if (~(*feat
) & (1 << i
)) {
404 log_debug("adding terminal feature: %s", tf
->name
);
412 tty_get_features(int feat
)
414 const struct tty_feature
*tf
;
419 for (i
= 0; i
< nitems(tty_features
); i
++) {
420 if (~feat
& (1 << i
))
422 tf
= tty_features
[i
];
424 strlcat(s
, tf
->name
, sizeof s
);
425 strlcat(s
, ",", sizeof s
);
428 s
[strlen(s
) - 1] = '\0';
433 tty_apply_features(struct tty_term
*term
, int feat
)
435 const struct tty_feature
*tf
;
436 const char *const *capability
;
441 log_debug("applying terminal features: %s", tty_get_features(feat
));
443 for (i
= 0; i
< nitems(tty_features
); i
++) {
444 if ((term
->features
& (1 << i
)) || (~feat
& (1 << i
)))
446 tf
= tty_features
[i
];
448 log_debug("applying terminal feature: %s", tf
->name
);
449 if (tf
->capabilities
!= NULL
) {
450 capability
= tf
->capabilities
;
451 while (*capability
!= NULL
) {
452 log_debug("adding capability: %s", *capability
);
453 tty_term_apply(term
, *capability
, 1);
457 term
->flags
|= tf
->flags
;
459 if ((term
->features
| feat
) == term
->features
)
461 term
->features
|= feat
;
466 tty_default_features(int *feat
, const char *name
, u_int version
)
468 static const struct {
471 const char *features
;
473 #define TTY_FEATURES_BASE_MODERN_XTERM \
474 "256,RGB,bpaste,clipboard,mouse,strikethrough,title"
476 .features
= TTY_FEATURES_BASE_MODERN_XTERM
477 ",ccolour,cstyle,extkeys,margins,overline,usstyle"
480 .features
= TTY_FEATURES_BASE_MODERN_XTERM
481 ",ccolour,cstyle,focus,overline,usstyle,hyperlinks"
483 { .name
= "rxvt-unicode",
484 .features
= "256,bpaste,ccolour,cstyle,mouse,title,ignorefkeys"
487 .features
= TTY_FEATURES_BASE_MODERN_XTERM
488 ",cstyle,extkeys,margins,usstyle,sync,osc7,hyperlinks"
492 * xterm also supports DECSLRM and DECFRA, but they can be
493 * disabled so not set it here - they will be added if
494 * secondary DA shows VT420.
496 .features
= TTY_FEATURES_BASE_MODERN_XTERM
497 ",ccolour,cstyle,extkeys,focus"
502 for (i
= 0; i
< nitems(table
); i
++) {
503 if (strcmp(table
[i
].name
, name
) != 0)
505 if (version
!= 0 && version
< table
[i
].version
)
507 tty_add_features(feat
, table
[i
].features
, ",");