[BACK]Return to tty-features.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/tty-features.c, Revision 1.10

1.10    ! nicm        1: /* $OpenBSD: tty-features.c,v 1.9 2020/05/16 14:30:17 nicm Exp $ */
1.1       nicm        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:  * - bracket paste (sent if application asks for it);
                     29:  * - mouse (under kmous capability);
1.8       nicm       30:  * - focus events (under XT and focus-events option);
1.1       nicm       31:  * - default colours (under AX or op capabilities);
                     32:  * - AIX colours (under colors >= 16);
                     33:  * - alternate escape (under XT).
                     34:  *
                     35:  * Also:
1.6       nicm       36:  * - XT is used to decide whether to send DA and XDA;
1.7       nicm       37:  * - DECFRA uses a flag instead of capabilities;
1.1       nicm       38:  * - UTF-8 is a separate flag on the client; needed for unattached clients.
                     39:  */
                     40:
                     41: /* A named terminal feature. */
                     42: struct tty_feature {
                     43:        const char       *name;
                     44:        const char      **capabilities;
                     45:        int               flags;
                     46: };
                     47:
                     48: /* Terminal has xterm(1) title setting. */
                     49: static const char *tty_feature_title_capabilities[] = {
                     50:        "tsl=\\E]0;", /* should be using TS really */
                     51:        "fsl=\\a",
                     52:        NULL
                     53: };
                     54: static struct tty_feature tty_feature_title = {
                     55:        "title",
                     56:        tty_feature_title_capabilities,
                     57:        0
                     58: };
                     59:
                     60: /* Terminal can set the clipboard with OSC 52. */
                     61: static const char *tty_feature_clipboard_capabilities[] = {
                     62:        "Ms=\\E]52;%p1%s;%p2%s\\a",
                     63:        NULL
                     64: };
                     65: static struct tty_feature tty_feature_clipboard = {
                     66:        "clipboard",
                     67:        tty_feature_clipboard_capabilities,
                     68:        0
                     69: };
                     70:
                     71: /*
                     72:  * Terminal supports RGB colour. This replaces setab and setaf also since
                     73:  * terminals with RGB have versions that do not allow setting colours from the
                     74:  * 256 palette.
                     75:  */
                     76: static const char *tty_feature_rgb_capabilities[] = {
1.4       nicm       77:        "AX",
1.1       nicm       78:        "setrgbf=\\E[38;2;%p1%d;%p2%d;%p3%dm",
                     79:        "setrgbb=\\E[48;2;%p1%d;%p2%d;%p3%dm",
                     80:        "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
                     81:        "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
                     82:        NULL
                     83: };
                     84: static struct tty_feature tty_feature_rgb = {
                     85:        "RGB",
                     86:        tty_feature_rgb_capabilities,
1.7       nicm       87:        TERM_256COLOURS|TERM_RGBCOLOURS
1.1       nicm       88: };
                     89:
                     90: /* Terminal supports 256 colours. */
                     91: static const char *tty_feature_256_capabilities[] = {
1.4       nicm       92:        "AX",
1.1       nicm       93:        "setab=\\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
                     94:        "setaf=\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
                     95:        NULL
                     96: };
                     97: static struct tty_feature tty_feature_256 = {
                     98:        "256",
                     99:        tty_feature_256_capabilities,
                    100:        TERM_256COLOURS
                    101: };
                    102:
                    103: /* Terminal supports overline. */
                    104: static const char *tty_feature_overline_capabilities[] = {
                    105:        "Smol=\\E[53m",
                    106:        NULL
                    107: };
                    108: static struct tty_feature tty_feature_overline = {
                    109:        "overline",
                    110:        tty_feature_overline_capabilities,
                    111:        0
                    112: };
                    113:
                    114: /* Terminal supports underscore styles. */
                    115: static const char *tty_feature_usstyle_capabilities[] = {
                    116:        "Smulx=\E[4::%p1%dm",
                    117:        "Setulc=\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m",
                    118:        NULL
                    119: };
                    120: static struct tty_feature tty_feature_usstyle = {
                    121:        "usstyle",
                    122:        tty_feature_usstyle_capabilities,
                    123:        0
                    124: };
                    125:
                    126: /* Terminal supports cursor styles. */
                    127: static const char *tty_feature_cstyle_capabilities[] = {
                    128:        "Ss=\\E[%p1%d q",
                    129:        "Se=\\E[2 q",
                    130:        NULL
                    131: };
                    132: static struct tty_feature tty_feature_cstyle = {
                    133:        "cstyle",
                    134:        tty_feature_cstyle_capabilities,
                    135:        0
                    136: };
                    137:
                    138: /* Terminal supports cursor colours. */
                    139: static const char *tty_feature_ccolour_capabilities[] = {
                    140:        "Cs=\\E]12;%p1%s\\a",
                    141:        "Cr=\\E]112\\a",
                    142:        NULL
                    143: };
                    144: static struct tty_feature tty_feature_ccolour = {
                    145:        "ccolour",
                    146:        tty_feature_ccolour_capabilities,
                    147:        0
                    148: };
                    149:
1.10    ! nicm      150: /* Terminal supports strikethrough. */
        !           151: static const char *tty_feature_strikethrough_capabilities[] = {
        !           152:        "smxx=\\E[9m",
        !           153:        NULL
        !           154: };
        !           155: static struct tty_feature tty_feature_strikethrough = {
        !           156:        "strikethrough",
        !           157:        tty_feature_strikethrough_capabilities,
        !           158:        0
        !           159: };
        !           160:
1.1       nicm      161: /* Terminal supports synchronized updates. */
                    162: static const char *tty_feature_sync_capabilities[] = {
1.2       nicm      163:        "Sync=\\EP=%p1%ds\\E\\\\",
1.1       nicm      164:        NULL
                    165: };
                    166: static struct tty_feature tty_feature_sync = {
                    167:        "sync",
                    168:        tty_feature_sync_capabilities,
                    169:        0
                    170: };
                    171:
                    172: /* Terminal supports DECSLRM margins. */
1.7       nicm      173: static const char *tty_feature_margins_capabilities[] = {
                    174:        "Enmg=\\E[?69h",
                    175:        "Dsmg=\\E[?69l",
                    176:        "Clmg=\\E[s",
                    177:        "Cmg=\\E[%i%p1%d;%p2%ds",
                    178:        NULL
                    179: };
1.1       nicm      180: static struct tty_feature tty_feature_margins = {
                    181:        "margins",
1.7       nicm      182:        tty_feature_margins_capabilities,
1.1       nicm      183:        TERM_DECSLRM
                    184: };
                    185:
                    186: /* Terminal supports DECFRA rectangle fill. */
                    187: static struct tty_feature tty_feature_rectfill = {
                    188:        "rectfill",
                    189:        NULL,
                    190:        TERM_DECFRA
                    191: };
                    192:
                    193: /* Available terminal features. */
                    194: static const struct tty_feature *tty_features[] = {
                    195:        &tty_feature_256,
                    196:        &tty_feature_clipboard,
                    197:        &tty_feature_ccolour,
                    198:        &tty_feature_cstyle,
                    199:        &tty_feature_margins,
                    200:        &tty_feature_overline,
                    201:        &tty_feature_rectfill,
                    202:        &tty_feature_rgb,
1.10    ! nicm      203:        &tty_feature_strikethrough,
1.1       nicm      204:        &tty_feature_sync,
                    205:        &tty_feature_title,
                    206:        &tty_feature_usstyle
                    207: };
                    208:
                    209: void
                    210: tty_add_features(int *feat, const char *s, const char *separators)
                    211: {
                    212:        const struct tty_feature         *tf;
                    213:        char                             *next, *loop, *copy;
                    214:        u_int                             i;
1.7       nicm      215:
1.9       nicm      216:        log_debug("adding terminal features %s", s);
1.1       nicm      217:
                    218:        loop = copy = xstrdup(s);
                    219:        while ((next = strsep(&loop, separators)) != NULL) {
                    220:                for (i = 0; i < nitems(tty_features); i++) {
                    221:                        tf = tty_features[i];
                    222:                        if (strcasecmp(tf->name, next) == 0)
                    223:                                break;
                    224:                }
                    225:                if (i == nitems(tty_features)) {
                    226:                        log_debug("unknown terminal feature: %s", next);
                    227:                        break;
                    228:                }
                    229:                if (~(*feat) & (1 << i)) {
                    230:                        log_debug("adding terminal feature: %s", tf->name);
                    231:                        (*feat) |= (1 << i);
                    232:                }
                    233:        }
                    234:        free(copy);
                    235: }
                    236:
                    237: const char *
                    238: tty_get_features(int feat)
                    239: {
                    240:        const struct tty_feature        *tf;
                    241:        static char                      s[512];
                    242:        u_int                            i;
                    243:
                    244:        *s = '\0';
                    245:        for (i = 0; i < nitems(tty_features); i++) {
                    246:                if (~feat & (1 << i))
                    247:                        continue;
                    248:                tf = tty_features[i];
                    249:
                    250:                strlcat(s, tf->name, sizeof s);
                    251:                strlcat(s, ",", sizeof s);
                    252:        }
                    253:        if (*s != '\0')
                    254:                s[strlen(s) - 1] = '\0';
                    255:        return (s);
                    256: }
                    257:
1.3       nicm      258: int
1.1       nicm      259: tty_apply_features(struct tty_term *term, int feat)
                    260: {
                    261:        const struct tty_feature         *tf;
                    262:        const char                      **capability;
                    263:        u_int                             i;
                    264:
                    265:        if (feat == 0)
1.3       nicm      266:                return (0);
1.1       nicm      267:        log_debug("applying terminal features: %s", tty_get_features(feat));
                    268:
                    269:        for (i = 0; i < nitems(tty_features); i++) {
                    270:                if ((term->features & (1 << i)) || (~feat & (1 << i)))
                    271:                        continue;
                    272:                tf = tty_features[i];
                    273:
                    274:                log_debug("applying terminal feature: %s", tf->name);
                    275:                if (tf->capabilities != NULL) {
                    276:                        capability = tf->capabilities;
                    277:                        while (*capability != NULL) {
                    278:                                log_debug("adding capability: %s", *capability);
                    279:                                tty_term_apply(term, *capability, 1);
                    280:                                capability++;
                    281:                        }
                    282:                }
                    283:                term->flags |= tf->flags;
                    284:        }
1.3       nicm      285:        if ((term->features | feat) == term->features)
                    286:                return (0);
1.1       nicm      287:        term->features |= feat;
1.3       nicm      288:        return (1);
1.9       nicm      289: }
                    290:
                    291: void
                    292: tty_default_features(int *feat, const char *name, u_int version)
                    293: {
                    294:        static struct {
                    295:                const char      *name;
                    296:                u_int            version;
                    297:                const char      *features;
                    298:        } table[] = {
                    299:                { .name = "mintty",
1.10    ! nicm      300:                  .features = "256,RGB,ccolour,clipboard,cstyle,margins,strikethrough,overline,title"
1.9       nicm      301:                },
                    302:                { .name = "tmux",
1.10    ! nicm      303:                  .features = "256,RGB,ccolour,clipboard,cstyle,overline,strikethough,title,usstyle"
1.9       nicm      304:                },
                    305:                { .name = "rxvt-unicode",
                    306:                  .features = "256,title"
                    307:                },
                    308:                { .name = "iTerm2",
1.10    ! nicm      309:                  .features = "256,RGB,clipboard,cstyle,margins,strikethrough,sync,title"
1.9       nicm      310:                },
                    311:                { .name = "XTerm",
1.10    ! nicm      312:                  .features = "256,RGB,ccolour,clipboard,cstyle,margins,rectfill,strikethrough,title"
1.9       nicm      313:                }
                    314:        };
                    315:        u_int   i;
                    316:
                    317:        for (i = 0; i < nitems(table); i++) {
                    318:                if (strcmp(table[i].name, name) != 0)
                    319:                        continue;
                    320:                if (version != 0 && version < table[i].version)
                    321:                        continue;
                    322:                tty_add_features(feat, table[i].features, ",");
                    323:        }
                    324:
1.1       nicm      325: }