tmux  3.2a
About: tmux is a terminal multiplexer that lets you switch easily between several programs in one terminal.
  Fossies Dox: tmux-3.2a.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

tty-features.c
Go to the documentation of this file.
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2020 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Still hardcoded:
28  * - default colours (under AX or op capabilities);
29  * - AIX colours (under colors >= 16);
30  * - alternate escape (if terminal is VT100-like).
31  *
32  * Also:
33  * - DECFRA uses a flag instead of capabilities;
34  * - UTF-8 is a separate flag on the client; needed for unattached clients.
35  */
36 
37 /* A named terminal feature. */
38 struct tty_feature {
39  const char *name;
40  const char **capabilities;
41  int flags;
42 };
43 
44 /* Terminal has xterm(1) title setting. */
45 static const char *tty_feature_title_capabilities[] = {
46  "tsl=\\E]0;", /* should be using TS really */
47  "fsl=\\a",
48  NULL
49 };
50 static const struct tty_feature tty_feature_title = {
51  "title",
53  0
54 };
55 
56 /* Terminal has mouse support. */
57 static const char *tty_feature_mouse_capabilities[] = {
58  "kmous=\\E[M",
59  NULL
60 };
61 static const struct tty_feature tty_feature_mouse = {
62  "mouse",
64  0
65 };
66 
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",
70  NULL
71 };
72 static const struct tty_feature tty_feature_clipboard = {
73  "clipboard",
75  0
76 };
77 
78 /*
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
81  * 256 palette.
82  */
83 static const char *tty_feature_rgb_capabilities[] = {
84  "AX",
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",
89  NULL
90 };
91 static const struct tty_feature tty_feature_rgb = {
92  "RGB",
95 };
96 
97 /* Terminal supports 256 colours. */
98 static const char *tty_feature_256_capabilities[] = {
99  "AX",
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",
102  NULL
103 };
104 static const struct tty_feature tty_feature_256 = {
105  "256",
108 };
109 
110 /* Terminal supports overline. */
111 static const char *tty_feature_overline_capabilities[] = {
112  "Smol=\\E[53m",
113  NULL
114 };
115 static const struct tty_feature tty_feature_overline = {
116  "overline",
118  0
119 };
120 
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",
125  "ol=\\E[59m",
126  NULL
127 };
128 static const struct tty_feature tty_feature_usstyle = {
129  "usstyle",
131  0
132 };
133 
134 /* Terminal supports bracketed paste. */
135 static const char *tty_feature_bpaste_capabilities[] = {
136  "Enbp=\\E[?2004h",
137  "Dsbp=\\E[?2004l",
138  NULL
139 };
140 static const struct tty_feature tty_feature_bpaste = {
141  "bpaste",
143  0
144 };
145 
146 /* Terminal supports focus reporting. */
147 static const char *tty_feature_focus_capabilities[] = {
148  "Enfcs=\\E[?1004h",
149  "Dsfcs=\\E[?1004l",
150  NULL
151 };
152 static const struct tty_feature tty_feature_focus = {
153  "focus",
155  0
156 };
157 
158 /* Terminal supports cursor styles. */
159 static const char *tty_feature_cstyle_capabilities[] = {
160  "Ss=\\E[%p1%d q",
161  "Se=\\E[2 q",
162  NULL
163 };
164 static const struct tty_feature tty_feature_cstyle = {
165  "cstyle",
167  0
168 };
169 
170 /* Terminal supports cursor colours. */
171 static const char *tty_feature_ccolour_capabilities[] = {
172  "Cs=\\E]12;%p1%s\\a",
173  "Cr=\\E]112\\a",
174  NULL
175 };
176 static const struct tty_feature tty_feature_ccolour = {
177  "ccolour",
179  0
180 };
181 
182 /* Terminal supports strikethrough. */
184  "smxx=\\E[9m",
185  NULL
186 };
187 static const struct tty_feature tty_feature_strikethrough = {
188  "strikethrough",
190  0
191 };
192 
193 /* Terminal supports synchronized updates. */
194 static const char *tty_feature_sync_capabilities[] = {
195  "Sync=\\EP=%p1%ds\\E\\\\",
196  NULL
197 };
198 static const struct tty_feature tty_feature_sync = {
199  "sync",
201  0
202 };
203 
204 /* Terminal supports extended keys. */
205 static const char *tty_feature_extkeys_capabilities[] = {
206  "Eneks=\\E[>4;1m",
207  "Dseks=\\E[>4m",
208  NULL
209 };
210 static const struct tty_feature tty_feature_extkeys = {
211  "extkeys",
213  0
214 };
215 
216 /* Terminal supports DECSLRM margins. */
217 static const char *tty_feature_margins_capabilities[] = {
218  "Enmg=\\E[?69h",
219  "Dsmg=\\E[?69l",
220  "Clmg=\\E[s",
221  "Cmg=\\E[%i%p1%d;%p2%ds",
222  NULL
223 };
224 static const struct tty_feature tty_feature_margins = {
225  "margins",
228 };
229 
230 /* Terminal supports DECFRA rectangle fill. */
231 static const char *tty_feature_rectfill_capabilities[] = {
232  "Rect",
233  NULL
234 };
235 static const struct tty_feature tty_feature_rectfill = {
236  "rectfill",
239 };
240 
241 /* Available terminal features. */
242 static const struct tty_feature *tty_features[] = {
259 };
260 
261 void
262 tty_add_features(int *feat, const char *s, const char *separators)
263 {
264  const struct tty_feature *tf;
265  char *next, *loop, *copy;
266  u_int i;
267 
268  log_debug("adding terminal features %s", s);
269 
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)
275  break;
276  }
277  if (i == nitems(tty_features)) {
278  log_debug("unknown terminal feature: %s", next);
279  break;
280  }
281  if (~(*feat) & (1 << i)) {
282  log_debug("adding terminal feature: %s", tf->name);
283  (*feat) |= (1 << i);
284  }
285  }
286  free(copy);
287 }
288 
289 const char *
291 {
292  const struct tty_feature *tf;
293  static char s[512];
294  u_int i;
295 
296  *s = '\0';
297  for (i = 0; i < nitems(tty_features); i++) {
298  if (~feat & (1 << i))
299  continue;
300  tf = tty_features[i];
301 
302  strlcat(s, tf->name, sizeof s);
303  strlcat(s, ",", sizeof s);
304  }
305  if (*s != '\0')
306  s[strlen(s) - 1] = '\0';
307  return (s);
308 }
309 
310 int
311 tty_apply_features(struct tty_term *term, int feat)
312 {
313  const struct tty_feature *tf;
314  const char **capability;
315  u_int i;
316 
317  if (feat == 0)
318  return (0);
319  log_debug("applying terminal features: %s", tty_get_features(feat));
320 
321  for (i = 0; i < nitems(tty_features); i++) {
322  if ((term->features & (1 << i)) || (~~feat & (1 << i)))
323  continue;
324  tf = tty_features[i];
325 
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);
332  capability++;
333  }
334  }
335  term->flags |= tf->flags;
336  }
337  if ((term->features | feat) == term->features)
338  return (0);
339  term->features |= feat;
340  return (1);
341 }
342 
343 void
344 tty_default_features(int *feat, const char *name, u_int version)
345 {
346  static struct {
347  const char *name;
348  u_int version;
349  const char *features;
350  } table[] = {
351 #define TTY_FEATURES_BASE_MODERN_XTERM \
352  "256,RGB,bpaste,clipboard,mouse,strikethrough,title"
353  { .name = "mintty",
355  ",ccolour,cstyle,extkeys,margins,overline,usstyle"
356  },
357  { .name = "tmux",
359  ",ccolour,cstyle,focus,overline,usstyle"
360  },
361  { .name = "rxvt-unicode",
362  .features = "256,bpaste,ccolour,cstyle,mouse,title"
363  },
364  { .name = "iTerm2",
366  ",cstyle,extkeys,margins,sync"
367  },
368  { .name = "XTerm",
369  /*
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.
373  */
375  ",ccolour,cstyle,extkeys,focus"
376  }
377  };
378  u_int i;
379 
380  for (i = 0; i < nitems(table); i++) {
381  if (strcmp(table[i].name, name) != 0)
382  continue;
383  if (version != 0 && version < table[i].version)
384  continue;
385  tty_add_features(feat, table[i].features, ",");
386  }
387 }
char * strsep(char **, const char *)
size_t strlcat(char *, const char *, size_t)
const char * name
Definition: layout-set.c:38
void log_debug(const char *msg,...)
Definition: log.c:130
#define nitems(_a)
const char ** capabilities
Definition: tty-features.c:40
const char * name
Definition: tty-features.c:39
int features
Definition: tmux.h:1286
int flags
Definition: tmux.h:1298
#define TERM_RGBCOLOURS
Definition: tmux.h:1296
void tty_term_apply(struct tty_term *, const char *, int)
Definition: tty-term.c:357
#define TERM_DECSLRM
Definition: tmux.h:1294
#define TERM_DECFRA
Definition: tmux.h:1295
#define TERM_256COLOURS
Definition: tmux.h:1292
static const char * tty_feature_mouse_capabilities[]
Definition: tty-features.c:57
static const char * tty_feature_256_capabilities[]
Definition: tty-features.c:98
int tty_apply_features(struct tty_term *term, int feat)
Definition: tty-features.c:311
static const struct tty_feature tty_feature_cstyle
Definition: tty-features.c:164
static const char * tty_feature_margins_capabilities[]
Definition: tty-features.c:217
static const char * tty_feature_extkeys_capabilities[]
Definition: tty-features.c:205
static const char * tty_feature_overline_capabilities[]
Definition: tty-features.c:111
static const struct tty_feature tty_feature_clipboard
Definition: tty-features.c:72
static const char * tty_feature_bpaste_capabilities[]
Definition: tty-features.c:135
static const char * tty_feature_rectfill_capabilities[]
Definition: tty-features.c:231
static const char * tty_feature_cstyle_capabilities[]
Definition: tty-features.c:159
static const struct tty_feature tty_feature_margins
Definition: tty-features.c:224
void tty_add_features(int *feat, const char *s, const char *separators)
Definition: tty-features.c:262
static const struct tty_feature tty_feature_title
Definition: tty-features.c:50
static const struct tty_feature tty_feature_overline
Definition: tty-features.c:115
static const struct tty_feature tty_feature_256
Definition: tty-features.c:104
static const char * tty_feature_ccolour_capabilities[]
Definition: tty-features.c:171
const char * tty_get_features(int feat)
Definition: tty-features.c:290
static const struct tty_feature tty_feature_bpaste
Definition: tty-features.c:140
static const struct tty_feature tty_feature_extkeys
Definition: tty-features.c:210
static const struct tty_feature tty_feature_strikethrough
Definition: tty-features.c:187
#define TTY_FEATURES_BASE_MODERN_XTERM
static const char * tty_feature_focus_capabilities[]
Definition: tty-features.c:147
static const char * tty_feature_usstyle_capabilities[]
Definition: tty-features.c:122
static const struct tty_feature tty_feature_mouse
Definition: tty-features.c:61
static const char * tty_feature_sync_capabilities[]
Definition: tty-features.c:194
static const struct tty_feature tty_feature_focus
Definition: tty-features.c:152
static const struct tty_feature tty_feature_ccolour
Definition: tty-features.c:176
static const struct tty_feature tty_feature_sync
Definition: tty-features.c:198
static const struct tty_feature tty_feature_usstyle
Definition: tty-features.c:128
static const char * tty_feature_rgb_capabilities[]
Definition: tty-features.c:83
static const struct tty_feature * tty_features[]
Definition: tty-features.c:242
static const char * tty_feature_strikethrough_capabilities[]
Definition: tty-features.c:183
void tty_default_features(int *feat, const char *name, u_int version)
Definition: tty-features.c:344
static const struct tty_feature tty_feature_rgb
Definition: tty-features.c:91
static const char * tty_feature_clipboard_capabilities[]
Definition: tty-features.c:68
static const char * tty_feature_title_capabilities[]
Definition: tty-features.c:45
static const struct tty_feature tty_feature_rectfill
Definition: tty-features.c:235
char * xstrdup(const char *str)
Definition: xmalloc.c:89