Merge branch 'obsd-master'
[tmux.git] / tty-features.c
blobdca40fadff7393c789394173a8cb4739756d196f
1 /* $OpenBSD$ */
3 /*
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>
21 #include <stdlib.h>
22 #include <string.h>
24 #if defined(HAVE_CURSES_H)
25 #include <curses.h>
26 #elif defined(HAVE_NCURSES_H)
27 #include <ncurses.h>
28 #endif
30 #include "tmux.h"
33 * Still hardcoded:
34 * - default colours (under AX or op capabilities);
35 * - AIX colours (under colors >= 16);
36 * - alternate escape (if terminal is VT100-like).
38 * Also:
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. */
44 struct tty_feature {
45 const char *name;
46 const char *const *capabilities;
47 int flags;
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 */
53 "fsl=\\a",
54 NULL
56 static const struct tty_feature tty_feature_title = {
57 "title",
58 tty_feature_title_capabilities,
62 /* Terminal has OSC 7 working directory. */
63 static const char *const tty_feature_osc7_capabilities[] = {
64 "Swd=\\E]7;",
65 "fsl=\\a",
66 NULL
68 static const struct tty_feature tty_feature_osc7 = {
69 "osc7",
70 tty_feature_osc7_capabilities,
74 /* Terminal has mouse support. */
75 static const char *const tty_feature_mouse_capabilities[] = {
76 "kmous=\\E[M",
77 NULL
79 static const struct tty_feature tty_feature_mouse = {
80 "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",
88 NULL
90 static const struct tty_feature tty_feature_clipboard = {
91 "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\\\\",
102 #endif
103 NULL
105 static const struct tty_feature tty_feature_hyperlinks = {
106 "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
114 * 256 palette.
116 static const char *const tty_feature_rgb_capabilities[] = {
117 "AX",
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",
122 NULL
124 static const struct tty_feature tty_feature_rgb = {
125 "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[] = {
132 "AX",
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",
135 NULL
137 static const struct tty_feature tty_feature_256 = {
138 "256",
139 tty_feature_256_capabilities,
140 TERM_256COLOURS
143 /* Terminal supports overline. */
144 static const char *const tty_feature_overline_capabilities[] = {
145 "Smol=\\E[53m",
146 NULL
148 static const struct tty_feature tty_feature_overline = {
149 "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",
158 "ol=\\E[59m",
159 NULL
161 static const struct tty_feature tty_feature_usstyle = {
162 "usstyle",
163 tty_feature_usstyle_capabilities,
167 /* Terminal supports bracketed paste. */
168 static const char *const tty_feature_bpaste_capabilities[] = {
169 "Enbp=\\E[?2004h",
170 "Dsbp=\\E[?2004l",
171 NULL
173 static const struct tty_feature tty_feature_bpaste = {
174 "bpaste",
175 tty_feature_bpaste_capabilities,
179 /* Terminal supports focus reporting. */
180 static const char *const tty_feature_focus_capabilities[] = {
181 "Enfcs=\\E[?1004h",
182 "Dsfcs=\\E[?1004l",
183 NULL
185 static const struct tty_feature tty_feature_focus = {
186 "focus",
187 tty_feature_focus_capabilities,
191 /* Terminal supports cursor styles. */
192 static const char *const tty_feature_cstyle_capabilities[] = {
193 "Ss=\\E[%p1%d q",
194 "Se=\\E[2 q",
195 NULL
197 static const struct tty_feature tty_feature_cstyle = {
198 "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",
206 "Cr=\\E]112\\a",
207 NULL
209 static const struct tty_feature tty_feature_ccolour = {
210 "ccolour",
211 tty_feature_ccolour_capabilities,
215 /* Terminal supports strikethrough. */
216 static const char *const tty_feature_strikethrough_capabilities[] = {
217 "smxx=\\E[9m",
218 NULL
220 static const struct tty_feature tty_feature_strikethrough = {
221 "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\\\\",
229 NULL
231 static const struct tty_feature tty_feature_sync = {
232 "sync",
233 tty_feature_sync_capabilities,
237 /* Terminal supports extended keys. */
238 static const char *const tty_feature_extkeys_capabilities[] = {
239 "Eneks=\\E[>4;1m",
240 "Dseks=\\E[>4m",
241 NULL
243 static const struct tty_feature tty_feature_extkeys = {
244 "extkeys",
245 tty_feature_extkeys_capabilities,
249 /* Terminal supports DECSLRM margins. */
250 static const char *const tty_feature_margins_capabilities[] = {
251 "Enmg=\\E[?69h",
252 "Dsmg=\\E[?69l",
253 "Clmg=\\E[s",
254 "Cmg=\\E[%i%p1%d;%p2%ds",
255 NULL
257 static const struct tty_feature tty_feature_margins = {
258 "margins",
259 tty_feature_margins_capabilities,
260 TERM_DECSLRM
263 /* Terminal supports DECFRA rectangle fill. */
264 static const char *const tty_feature_rectfill_capabilities[] = {
265 "Rect",
266 NULL
268 static const struct tty_feature tty_feature_rectfill = {
269 "rectfill",
270 tty_feature_rectfill_capabilities,
271 TERM_DECFRA
274 /* Use builtin function keys only. */
275 static const char *const tty_feature_ignorefkeys_capabilities[] = {
276 "kf0@",
277 "kf1@",
278 "kf2@",
279 "kf3@",
280 "kf4@",
281 "kf5@",
282 "kf6@",
283 "kf7@",
284 "kf8@",
285 "kf9@",
286 "kf10@",
287 "kf11@",
288 "kf12@",
289 "kf13@",
290 "kf14@",
291 "kf15@",
292 "kf16@",
293 "kf17@",
294 "kf18@",
295 "kf19@",
296 "kf20@",
297 "kf21@",
298 "kf22@",
299 "kf23@",
300 "kf24@",
301 "kf25@",
302 "kf26@",
303 "kf27@",
304 "kf28@",
305 "kf29@",
306 "kf30@",
307 "kf31@",
308 "kf32@",
309 "kf33@",
310 "kf34@",
311 "kf35@",
312 "kf36@",
313 "kf37@",
314 "kf38@",
315 "kf39@",
316 "kf40@",
317 "kf41@",
318 "kf42@",
319 "kf43@",
320 "kf44@",
321 "kf45@",
322 "kf46@",
323 "kf47@",
324 "kf48@",
325 "kf49@",
326 "kf50@",
327 "kf51@",
328 "kf52@",
329 "kf53@",
330 "kf54@",
331 "kf55@",
332 "kf56@",
333 "kf57@",
334 "kf58@",
335 "kf59@",
336 "kf60@",
337 "kf61@",
338 "kf62@",
339 "kf63@",
340 NULL
342 static const struct tty_feature tty_feature_ignorefkeys = {
343 "ignorefkeys",
344 tty_feature_ignorefkeys_capabilities,
348 /* Terminal has sixel capability. */
349 static const char *const tty_feature_sixel_capabilities[] = {
350 "Sxl",
351 NULL
353 static const struct tty_feature tty_feature_sixel = {
354 "sixel",
355 tty_feature_sixel_capabilities,
356 TERM_SIXEL
359 /* Available terminal features. */
360 static const struct tty_feature *const tty_features[] = {
361 &tty_feature_256,
362 &tty_feature_bpaste,
363 &tty_feature_ccolour,
364 &tty_feature_clipboard,
365 &tty_feature_hyperlinks,
366 &tty_feature_cstyle,
367 &tty_feature_extkeys,
368 &tty_feature_focus,
369 &tty_feature_ignorefkeys,
370 &tty_feature_margins,
371 &tty_feature_mouse,
372 &tty_feature_osc7,
373 &tty_feature_overline,
374 &tty_feature_rectfill,
375 &tty_feature_rgb,
376 &tty_feature_sixel,
377 &tty_feature_strikethrough,
378 &tty_feature_sync,
379 &tty_feature_title,
380 &tty_feature_usstyle
383 void
384 tty_add_features(int *feat, const char *s, const char *separators)
386 const struct tty_feature *tf;
387 char *next, *loop, *copy;
388 u_int i;
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)
397 break;
399 if (i == nitems(tty_features)) {
400 log_debug("unknown terminal feature: %s", next);
401 break;
403 if (~(*feat) & (1 << i)) {
404 log_debug("adding terminal feature: %s", tf->name);
405 (*feat) |= (1 << i);
408 free(copy);
411 const char *
412 tty_get_features(int feat)
414 const struct tty_feature *tf;
415 static char s[512];
416 u_int i;
418 *s = '\0';
419 for (i = 0; i < nitems(tty_features); i++) {
420 if (~feat & (1 << i))
421 continue;
422 tf = tty_features[i];
424 strlcat(s, tf->name, sizeof s);
425 strlcat(s, ",", sizeof s);
427 if (*s != '\0')
428 s[strlen(s) - 1] = '\0';
429 return (s);
433 tty_apply_features(struct tty_term *term, int feat)
435 const struct tty_feature *tf;
436 const char *const *capability;
437 u_int i;
439 if (feat == 0)
440 return (0);
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)))
445 continue;
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);
454 capability++;
457 term->flags |= tf->flags;
459 if ((term->features | feat) == term->features)
460 return (0);
461 term->features |= feat;
462 return (1);
465 void
466 tty_default_features(int *feat, const char *name, u_int version)
468 static const struct {
469 const char *name;
470 u_int version;
471 const char *features;
472 } table[] = {
473 #define TTY_FEATURES_BASE_MODERN_XTERM \
474 "256,RGB,bpaste,clipboard,mouse,strikethrough,title"
475 { .name = "mintty",
476 .features = TTY_FEATURES_BASE_MODERN_XTERM
477 ",ccolour,cstyle,extkeys,margins,overline,usstyle"
479 { .name = "tmux",
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"
486 { .name = "iTerm2",
487 .features = TTY_FEATURES_BASE_MODERN_XTERM
488 ",cstyle,extkeys,margins,usstyle,sync,osc7,hyperlinks"
490 { .name = "XTerm",
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"
500 u_int i;
502 for (i = 0; i < nitems(table); i++) {
503 if (strcmp(table[i].name, name) != 0)
504 continue;
505 if (version != 0 && version < table[i].version)
506 continue;
507 tty_add_features(feat, table[i].features, ",");