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

Annotation of src/usr.bin/tmux/tty-keys.c, Revision 1.169

1.169   ! nicm        1: /* $OpenBSD: tty-keys.c,v 1.168 2023/09/02 20:03:10 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.84      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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: #include <sys/time.h>
                     21:
1.104     nicm       22: #include <netinet/in.h>
                     23:
1.133     nicm       24: #include <ctype.h>
1.35      nicm       25: #include <limits.h>
1.104     nicm       26: #include <resolv.h>
1.35      nicm       27: #include <stdlib.h>
1.1       nicm       28: #include <string.h>
1.3       nicm       29: #include <termios.h>
                     30: #include <unistd.h>
1.1       nicm       31:
                     32: #include "tmux.h"
                     33:
1.9       nicm       34: /*
1.50      nicm       35:  * Handle keys input from the outside terminal. tty_default_*_keys[] are a base
                     36:  * table of supported keys which are looked up in terminfo(5) and translated
                     37:  * into a ternary tree.
1.9       nicm       38:  */
                     39:
1.86      nicm       40: static void    tty_keys_add1(struct tty_key **, const char *, key_code);
                     41: static void    tty_keys_add(struct tty *, const char *, key_code);
                     42: static void    tty_keys_free1(struct tty_key *);
                     43: static struct tty_key *tty_keys_find1(struct tty_key *, const char *, size_t,
1.76      nicm       44:                    size_t *);
1.86      nicm       45: static struct tty_key *tty_keys_find(struct tty *, const char *, size_t,
                     46:                    size_t *);
                     47: static int     tty_keys_next1(struct tty *, const char *, size_t, key_code *,
1.87      nicm       48:                    size_t *, int);
1.86      nicm       49: static void    tty_keys_callback(int, short, void *);
1.133     nicm       50: static int     tty_keys_extended_key(struct tty *, const char *, size_t,
                     51:                    size_t *, key_code *);
1.111     nicm       52: static int     tty_keys_mouse(struct tty *, const char *, size_t, size_t *,
                     53:                    struct mouse_event *);
1.104     nicm       54: static int     tty_keys_clipboard(struct tty *, const char *, size_t,
                     55:                    size_t *);
1.91      nicm       56: static int     tty_keys_device_attributes(struct tty *, const char *, size_t,
                     57:                    size_t *);
1.161     nicm       58: static int     tty_keys_device_attributes2(struct tty *, const char *, size_t,
                     59:                    size_t *);
1.128     nicm       60: static int     tty_keys_extended_device_attributes(struct tty *, const char *,
1.119     nicm       61:                    size_t, size_t *);
1.163     nicm       62: static int     tty_keys_colours(struct tty *, const char *, size_t, size_t *);
1.150     nicm       63:
                     64: /* A key tree entry. */
                     65: struct tty_key {
                     66:        char             ch;
                     67:        key_code         key;
                     68:
                     69:        struct tty_key  *left;
                     70:        struct tty_key  *right;
                     71:
                     72:        struct tty_key  *next;
                     73: };
1.1       nicm       74:
1.48      nicm       75: /* Default raw keys. */
                     76: struct tty_default_key_raw {
1.1       nicm       77:        const char             *string;
1.143     nicm       78:        key_code                key;
1.1       nicm       79: };
1.90      nicm       80: static const struct tty_default_key_raw tty_default_raw_keys[] = {
1.125     nicm       81:        /* Application escape. */
                     82:        { "\033O[", '\033' },
                     83:
1.1       nicm       84:        /*
1.8       nicm       85:         * Numeric keypad. Just use the vt100 escape sequences here and always
                     86:         * put the terminal into keypad_xmit mode. Translation of numbers
                     87:         * mode/applications mode is done in input-keys.c.
1.1       nicm       88:         */
1.132     nicm       89:        { "\033Oo", KEYC_KP_SLASH|KEYC_KEYPAD },
                     90:        { "\033Oj", KEYC_KP_STAR|KEYC_KEYPAD },
                     91:        { "\033Om", KEYC_KP_MINUS|KEYC_KEYPAD },
                     92:        { "\033Ow", KEYC_KP_SEVEN|KEYC_KEYPAD },
                     93:        { "\033Ox", KEYC_KP_EIGHT|KEYC_KEYPAD },
                     94:        { "\033Oy", KEYC_KP_NINE|KEYC_KEYPAD },
                     95:        { "\033Ok", KEYC_KP_PLUS|KEYC_KEYPAD },
                     96:        { "\033Ot", KEYC_KP_FOUR|KEYC_KEYPAD },
                     97:        { "\033Ou", KEYC_KP_FIVE|KEYC_KEYPAD },
                     98:        { "\033Ov", KEYC_KP_SIX|KEYC_KEYPAD },
                     99:        { "\033Oq", KEYC_KP_ONE|KEYC_KEYPAD },
                    100:        { "\033Or", KEYC_KP_TWO|KEYC_KEYPAD },
                    101:        { "\033Os", KEYC_KP_THREE|KEYC_KEYPAD },
                    102:        { "\033OM", KEYC_KP_ENTER|KEYC_KEYPAD },
                    103:        { "\033Op", KEYC_KP_ZERO|KEYC_KEYPAD },
                    104:        { "\033On", KEYC_KP_PERIOD|KEYC_KEYPAD },
1.10      nicm      105:
1.29      nicm      106:        /* Arrow keys. */
1.132     nicm      107:        { "\033OA", KEYC_UP|KEYC_CURSOR },
                    108:        { "\033OB", KEYC_DOWN|KEYC_CURSOR },
                    109:        { "\033OC", KEYC_RIGHT|KEYC_CURSOR },
                    110:        { "\033OD", KEYC_LEFT|KEYC_CURSOR },
                    111:
                    112:        { "\033[A", KEYC_UP|KEYC_CURSOR },
                    113:        { "\033[B", KEYC_DOWN|KEYC_CURSOR },
                    114:        { "\033[C", KEYC_RIGHT|KEYC_CURSOR },
                    115:        { "\033[D", KEYC_LEFT|KEYC_CURSOR },
1.57      nicm      116:
1.142     nicm      117:        /*
                    118:         * Meta arrow keys. These do not get the IMPLIED_META flag so they
                    119:         * don't match the xterm-style meta keys in the output tree - Escape+Up
                    120:         * should stay as Escape+Up and not become M-Up.
                    121:         */
                    122:        { "\033\033OA", KEYC_UP|KEYC_CURSOR|KEYC_META },
                    123:        { "\033\033OB", KEYC_DOWN|KEYC_CURSOR|KEYC_META },
                    124:        { "\033\033OC", KEYC_RIGHT|KEYC_CURSOR|KEYC_META },
                    125:        { "\033\033OD", KEYC_LEFT|KEYC_CURSOR|KEYC_META },
                    126:
                    127:        { "\033\033[A", KEYC_UP|KEYC_CURSOR|KEYC_META },
                    128:        { "\033\033[B", KEYC_DOWN|KEYC_CURSOR|KEYC_META },
                    129:        { "\033\033[C", KEYC_RIGHT|KEYC_CURSOR|KEYC_META },
                    130:        { "\033\033[D", KEYC_LEFT|KEYC_CURSOR|KEYC_META },
1.140     nicm      131:
1.157     nicm      132:        /* Other xterm keys. */
1.57      nicm      133:        { "\033OH", KEYC_HOME },
                    134:        { "\033OF", KEYC_END },
                    135:
1.140     nicm      136:        { "\033\033OH", KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
                    137:        { "\033\033OF", KEYC_END|KEYC_META|KEYC_IMPLIED_META },
                    138:
1.57      nicm      139:        { "\033[H", KEYC_HOME },
                    140:        { "\033[F", KEYC_END },
1.140     nicm      141:
                    142:        { "\033\033[H", KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
                    143:        { "\033\033[F", KEYC_END|KEYC_META|KEYC_IMPLIED_META },
1.21      nicm      144:
1.157     nicm      145:        /* rxvt arrow keys. */
1.48      nicm      146:        { "\033Oa", KEYC_UP|KEYC_CTRL },
                    147:        { "\033Ob", KEYC_DOWN|KEYC_CTRL },
                    148:        { "\033Oc", KEYC_RIGHT|KEYC_CTRL },
                    149:        { "\033Od", KEYC_LEFT|KEYC_CTRL },
                    150:
                    151:        { "\033[a", KEYC_UP|KEYC_SHIFT },
                    152:        { "\033[b", KEYC_DOWN|KEYC_SHIFT },
                    153:        { "\033[c", KEYC_RIGHT|KEYC_SHIFT },
                    154:        { "\033[d", KEYC_LEFT|KEYC_SHIFT },
                    155:
1.157     nicm      156:        /* rxvt function keys. */
                    157:        { "\033[11~", KEYC_F1 },
                    158:        { "\033[12~", KEYC_F2 },
                    159:        { "\033[13~", KEYC_F3 },
                    160:        { "\033[14~", KEYC_F4 },
                    161:        { "\033[15~", KEYC_F5 },
                    162:        { "\033[17~", KEYC_F6 },
                    163:        { "\033[18~", KEYC_F7 },
                    164:        { "\033[19~", KEYC_F8 },
                    165:        { "\033[20~", KEYC_F9 },
                    166:        { "\033[21~", KEYC_F10 },
                    167:
                    168:        { "\033[23~", KEYC_F1|KEYC_SHIFT },
                    169:        { "\033[24~", KEYC_F2|KEYC_SHIFT },
                    170:        { "\033[25~", KEYC_F3|KEYC_SHIFT },
                    171:        { "\033[26~", KEYC_F4|KEYC_SHIFT },
                    172:        { "\033[28~", KEYC_F5|KEYC_SHIFT },
                    173:        { "\033[29~", KEYC_F6|KEYC_SHIFT },
                    174:        { "\033[31~", KEYC_F7|KEYC_SHIFT },
                    175:        { "\033[32~", KEYC_F8|KEYC_SHIFT },
                    176:        { "\033[33~", KEYC_F9|KEYC_SHIFT },
                    177:        { "\033[34~", KEYC_F10|KEYC_SHIFT },
                    178:        { "\033[23$", KEYC_F11|KEYC_SHIFT },
                    179:        { "\033[24$", KEYC_F12|KEYC_SHIFT },
                    180:
1.48      nicm      181:        { "\033[11^", KEYC_F1|KEYC_CTRL },
                    182:        { "\033[12^", KEYC_F2|KEYC_CTRL },
                    183:        { "\033[13^", KEYC_F3|KEYC_CTRL },
                    184:        { "\033[14^", KEYC_F4|KEYC_CTRL },
                    185:        { "\033[15^", KEYC_F5|KEYC_CTRL },
                    186:        { "\033[17^", KEYC_F6|KEYC_CTRL },
                    187:        { "\033[18^", KEYC_F7|KEYC_CTRL },
                    188:        { "\033[19^", KEYC_F8|KEYC_CTRL },
                    189:        { "\033[20^", KEYC_F9|KEYC_CTRL },
                    190:        { "\033[21^", KEYC_F10|KEYC_CTRL },
                    191:        { "\033[23^", KEYC_F11|KEYC_CTRL },
                    192:        { "\033[24^", KEYC_F12|KEYC_CTRL },
                    193:
                    194:        { "\033[11@", KEYC_F1|KEYC_CTRL|KEYC_SHIFT },
                    195:        { "\033[12@", KEYC_F2|KEYC_CTRL|KEYC_SHIFT },
                    196:        { "\033[13@", KEYC_F3|KEYC_CTRL|KEYC_SHIFT },
                    197:        { "\033[14@", KEYC_F4|KEYC_CTRL|KEYC_SHIFT },
                    198:        { "\033[15@", KEYC_F5|KEYC_CTRL|KEYC_SHIFT },
                    199:        { "\033[17@", KEYC_F6|KEYC_CTRL|KEYC_SHIFT },
                    200:        { "\033[18@", KEYC_F7|KEYC_CTRL|KEYC_SHIFT },
                    201:        { "\033[19@", KEYC_F8|KEYC_CTRL|KEYC_SHIFT },
                    202:        { "\033[20@", KEYC_F9|KEYC_CTRL|KEYC_SHIFT },
                    203:        { "\033[21@", KEYC_F10|KEYC_CTRL|KEYC_SHIFT },
                    204:        { "\033[23@", KEYC_F11|KEYC_CTRL|KEYC_SHIFT },
                    205:        { "\033[24@", KEYC_F12|KEYC_CTRL|KEYC_SHIFT },
1.56      nicm      206:
                    207:        /* Focus tracking. */
                    208:        { "\033[I", KEYC_FOCUS_IN },
                    209:        { "\033[O", KEYC_FOCUS_OUT },
1.98      nicm      210:
                    211:        /* Paste keys. */
                    212:        { "\033[200~", KEYC_PASTE_START },
                    213:        { "\033[201~", KEYC_PASTE_END },
1.159     nicm      214:
                    215:        /* Extended keys. */
1.160     nicm      216:        { "\033[1;5Z", '\011'|KEYC_CTRL|KEYC_SHIFT },
1.48      nicm      217: };
1.29      nicm      218:
1.132     nicm      219: /* Default xterm keys. */
                    220: struct tty_default_key_xterm {
                    221:        const char      *template;
                    222:        key_code         key;
                    223: };
                    224: static const struct tty_default_key_xterm tty_default_xterm_keys[] = {
                    225:        { "\033[1;_P", KEYC_F1 },
                    226:        { "\033O1;_P", KEYC_F1 },
                    227:        { "\033O_P", KEYC_F1 },
                    228:        { "\033[1;_Q", KEYC_F2 },
                    229:        { "\033O1;_Q", KEYC_F2 },
                    230:        { "\033O_Q", KEYC_F2 },
                    231:        { "\033[1;_R", KEYC_F3 },
                    232:        { "\033O1;_R", KEYC_F3 },
                    233:        { "\033O_R", KEYC_F3 },
                    234:        { "\033[1;_S", KEYC_F4 },
                    235:        { "\033O1;_S", KEYC_F4 },
                    236:        { "\033O_S", KEYC_F4 },
                    237:        { "\033[15;_~", KEYC_F5 },
                    238:        { "\033[17;_~", KEYC_F6 },
                    239:        { "\033[18;_~", KEYC_F7 },
                    240:        { "\033[19;_~", KEYC_F8 },
                    241:        { "\033[20;_~", KEYC_F9 },
                    242:        { "\033[21;_~", KEYC_F10 },
                    243:        { "\033[23;_~", KEYC_F11 },
                    244:        { "\033[24;_~", KEYC_F12 },
                    245:        { "\033[1;_A", KEYC_UP },
                    246:        { "\033[1;_B", KEYC_DOWN },
                    247:        { "\033[1;_C", KEYC_RIGHT },
                    248:        { "\033[1;_D", KEYC_LEFT },
                    249:        { "\033[1;_H", KEYC_HOME },
                    250:        { "\033[1;_F", KEYC_END },
                    251:        { "\033[5;_~", KEYC_PPAGE },
                    252:        { "\033[6;_~", KEYC_NPAGE },
                    253:        { "\033[2;_~", KEYC_IC },
                    254:        { "\033[3;_~", KEYC_DC },
                    255: };
                    256: static const key_code tty_default_xterm_modifiers[] = {
                    257:        0,
                    258:        0,
1.135     nicm      259:        KEYC_SHIFT,
                    260:        KEYC_META|KEYC_IMPLIED_META,
                    261:        KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META,
                    262:        KEYC_CTRL,
                    263:        KEYC_SHIFT|KEYC_CTRL,
                    264:        KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
1.147     nicm      265:        KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
                    266:        KEYC_META|KEYC_IMPLIED_META
1.132     nicm      267: };
                    268:
1.103     nicm      269: /*
1.132     nicm      270:  * Default terminfo(5) keys. Any keys that have builtin modifiers (that is,
                    271:  * where the key itself contains the modifiers) has the KEYC_XTERM flag set so
                    272:  * a leading escape is not treated as meta (and probably removed).
1.103     nicm      273:  */
1.48      nicm      274: struct tty_default_key_code {
                    275:        enum tty_code_code      code;
1.143     nicm      276:        key_code                key;
1.48      nicm      277: };
1.90      nicm      278: static const struct tty_default_key_code tty_default_code_keys[] = {
1.29      nicm      279:        /* Function keys. */
1.48      nicm      280:        { TTYC_KF1, KEYC_F1 },
                    281:        { TTYC_KF2, KEYC_F2 },
                    282:        { TTYC_KF3, KEYC_F3 },
                    283:        { TTYC_KF4, KEYC_F4 },
                    284:        { TTYC_KF5, KEYC_F5 },
                    285:        { TTYC_KF6, KEYC_F6 },
                    286:        { TTYC_KF7, KEYC_F7 },
                    287:        { TTYC_KF8, KEYC_F8 },
                    288:        { TTYC_KF9, KEYC_F9 },
                    289:        { TTYC_KF10, KEYC_F10 },
                    290:        { TTYC_KF11, KEYC_F11 },
                    291:        { TTYC_KF12, KEYC_F12 },
1.70      nicm      292:
1.135     nicm      293:        { TTYC_KF13, KEYC_F1|KEYC_SHIFT },
                    294:        { TTYC_KF14, KEYC_F2|KEYC_SHIFT },
                    295:        { TTYC_KF15, KEYC_F3|KEYC_SHIFT },
                    296:        { TTYC_KF16, KEYC_F4|KEYC_SHIFT },
                    297:        { TTYC_KF17, KEYC_F5|KEYC_SHIFT },
                    298:        { TTYC_KF18, KEYC_F6|KEYC_SHIFT },
                    299:        { TTYC_KF19, KEYC_F7|KEYC_SHIFT },
                    300:        { TTYC_KF20, KEYC_F8|KEYC_SHIFT },
                    301:        { TTYC_KF21, KEYC_F9|KEYC_SHIFT },
                    302:        { TTYC_KF22, KEYC_F10|KEYC_SHIFT },
                    303:        { TTYC_KF23, KEYC_F11|KEYC_SHIFT },
                    304:        { TTYC_KF24, KEYC_F12|KEYC_SHIFT },
                    305:
                    306:        { TTYC_KF25, KEYC_F1|KEYC_CTRL },
                    307:        { TTYC_KF26, KEYC_F2|KEYC_CTRL },
                    308:        { TTYC_KF27, KEYC_F3|KEYC_CTRL },
                    309:        { TTYC_KF28, KEYC_F4|KEYC_CTRL },
                    310:        { TTYC_KF29, KEYC_F5|KEYC_CTRL },
                    311:        { TTYC_KF30, KEYC_F6|KEYC_CTRL },
                    312:        { TTYC_KF31, KEYC_F7|KEYC_CTRL },
                    313:        { TTYC_KF32, KEYC_F8|KEYC_CTRL },
                    314:        { TTYC_KF33, KEYC_F9|KEYC_CTRL },
                    315:        { TTYC_KF34, KEYC_F10|KEYC_CTRL },
                    316:        { TTYC_KF35, KEYC_F11|KEYC_CTRL },
                    317:        { TTYC_KF36, KEYC_F12|KEYC_CTRL },
                    318:
                    319:        { TTYC_KF37, KEYC_F1|KEYC_SHIFT|KEYC_CTRL },
                    320:        { TTYC_KF38, KEYC_F2|KEYC_SHIFT|KEYC_CTRL },
                    321:        { TTYC_KF39, KEYC_F3|KEYC_SHIFT|KEYC_CTRL },
                    322:        { TTYC_KF40, KEYC_F4|KEYC_SHIFT|KEYC_CTRL },
                    323:        { TTYC_KF41, KEYC_F5|KEYC_SHIFT|KEYC_CTRL },
                    324:        { TTYC_KF42, KEYC_F6|KEYC_SHIFT|KEYC_CTRL },
                    325:        { TTYC_KF43, KEYC_F7|KEYC_SHIFT|KEYC_CTRL },
                    326:        { TTYC_KF44, KEYC_F8|KEYC_SHIFT|KEYC_CTRL },
                    327:        { TTYC_KF45, KEYC_F9|KEYC_SHIFT|KEYC_CTRL },
                    328:        { TTYC_KF46, KEYC_F10|KEYC_SHIFT|KEYC_CTRL },
                    329:        { TTYC_KF47, KEYC_F11|KEYC_SHIFT|KEYC_CTRL },
                    330:        { TTYC_KF48, KEYC_F12|KEYC_SHIFT|KEYC_CTRL },
                    331:
                    332:        { TTYC_KF49, KEYC_F1|KEYC_META|KEYC_IMPLIED_META },
                    333:        { TTYC_KF50, KEYC_F2|KEYC_META|KEYC_IMPLIED_META },
                    334:        { TTYC_KF51, KEYC_F3|KEYC_META|KEYC_IMPLIED_META },
                    335:        { TTYC_KF52, KEYC_F4|KEYC_META|KEYC_IMPLIED_META },
                    336:        { TTYC_KF53, KEYC_F5|KEYC_META|KEYC_IMPLIED_META },
                    337:        { TTYC_KF54, KEYC_F6|KEYC_META|KEYC_IMPLIED_META },
                    338:        { TTYC_KF55, KEYC_F7|KEYC_META|KEYC_IMPLIED_META },
                    339:        { TTYC_KF56, KEYC_F8|KEYC_META|KEYC_IMPLIED_META },
                    340:        { TTYC_KF57, KEYC_F9|KEYC_META|KEYC_IMPLIED_META },
                    341:        { TTYC_KF58, KEYC_F10|KEYC_META|KEYC_IMPLIED_META },
                    342:        { TTYC_KF59, KEYC_F11|KEYC_META|KEYC_IMPLIED_META },
                    343:        { TTYC_KF60, KEYC_F12|KEYC_META|KEYC_IMPLIED_META },
                    344:
                    345:        { TTYC_KF61, KEYC_F1|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
                    346:        { TTYC_KF62, KEYC_F2|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
                    347:        { TTYC_KF63, KEYC_F3|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
1.70      nicm      348:
1.48      nicm      349:        { TTYC_KICH1, KEYC_IC },
                    350:        { TTYC_KDCH1, KEYC_DC },
                    351:        { TTYC_KHOME, KEYC_HOME },
                    352:        { TTYC_KEND, KEYC_END },
                    353:        { TTYC_KNP, KEYC_NPAGE },
                    354:        { TTYC_KPP, KEYC_PPAGE },
                    355:        { TTYC_KCBT, KEYC_BTAB },
1.29      nicm      356:
                    357:        /* Arrow keys from terminfo. */
1.132     nicm      358:        { TTYC_KCUU1, KEYC_UP|KEYC_CURSOR },
                    359:        { TTYC_KCUD1, KEYC_DOWN|KEYC_CURSOR },
                    360:        { TTYC_KCUB1, KEYC_LEFT|KEYC_CURSOR },
                    361:        { TTYC_KCUF1, KEYC_RIGHT|KEYC_CURSOR },
1.29      nicm      362:
1.103     nicm      363:        /* Key and modifier capabilities. */
1.135     nicm      364:        { TTYC_KDC2, KEYC_DC|KEYC_SHIFT },
                    365:        { TTYC_KDC3, KEYC_DC|KEYC_META|KEYC_IMPLIED_META },
                    366:        { TTYC_KDC4, KEYC_DC|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    367:        { TTYC_KDC5, KEYC_DC|KEYC_CTRL },
                    368:        { TTYC_KDC6, KEYC_DC|KEYC_SHIFT|KEYC_CTRL },
                    369:        { TTYC_KDC7, KEYC_DC|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
                    370:        { TTYC_KIND, KEYC_DOWN|KEYC_SHIFT },
                    371:        { TTYC_KDN2, KEYC_DOWN|KEYC_SHIFT },
                    372:        { TTYC_KDN3, KEYC_DOWN|KEYC_META|KEYC_IMPLIED_META },
                    373:        { TTYC_KDN4, KEYC_DOWN|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    374:        { TTYC_KDN5, KEYC_DOWN|KEYC_CTRL },
                    375:        { TTYC_KDN6, KEYC_DOWN|KEYC_SHIFT|KEYC_CTRL },
                    376:        { TTYC_KDN7, KEYC_DOWN|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
                    377:        { TTYC_KEND2, KEYC_END|KEYC_SHIFT },
                    378:        { TTYC_KEND3, KEYC_END|KEYC_META|KEYC_IMPLIED_META },
                    379:        { TTYC_KEND4, KEYC_END|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    380:        { TTYC_KEND5, KEYC_END|KEYC_CTRL },
                    381:        { TTYC_KEND6, KEYC_END|KEYC_SHIFT|KEYC_CTRL },
                    382:        { TTYC_KEND7, KEYC_END|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
                    383:        { TTYC_KHOM2, KEYC_HOME|KEYC_SHIFT },
                    384:        { TTYC_KHOM3, KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
                    385:        { TTYC_KHOM4, KEYC_HOME|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    386:        { TTYC_KHOM5, KEYC_HOME|KEYC_CTRL },
                    387:        { TTYC_KHOM6, KEYC_HOME|KEYC_SHIFT|KEYC_CTRL },
                    388:        { TTYC_KHOM7, KEYC_HOME|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
                    389:        { TTYC_KIC2, KEYC_IC|KEYC_SHIFT },
                    390:        { TTYC_KIC3, KEYC_IC|KEYC_META|KEYC_IMPLIED_META },
                    391:        { TTYC_KIC4, KEYC_IC|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    392:        { TTYC_KIC5, KEYC_IC|KEYC_CTRL },
                    393:        { TTYC_KIC6, KEYC_IC|KEYC_SHIFT|KEYC_CTRL },
                    394:        { TTYC_KIC7, KEYC_IC|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
                    395:        { TTYC_KLFT2, KEYC_LEFT|KEYC_SHIFT },
                    396:        { TTYC_KLFT3, KEYC_LEFT|KEYC_META|KEYC_IMPLIED_META },
                    397:        { TTYC_KLFT4, KEYC_LEFT|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    398:        { TTYC_KLFT5, KEYC_LEFT|KEYC_CTRL },
                    399:        { TTYC_KLFT6, KEYC_LEFT|KEYC_SHIFT|KEYC_CTRL },
                    400:        { TTYC_KLFT7, KEYC_LEFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
                    401:        { TTYC_KNXT2, KEYC_NPAGE|KEYC_SHIFT },
                    402:        { TTYC_KNXT3, KEYC_NPAGE|KEYC_META|KEYC_IMPLIED_META },
                    403:        { TTYC_KNXT4, KEYC_NPAGE|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    404:        { TTYC_KNXT5, KEYC_NPAGE|KEYC_CTRL },
                    405:        { TTYC_KNXT6, KEYC_NPAGE|KEYC_SHIFT|KEYC_CTRL },
                    406:        { TTYC_KNXT7, KEYC_NPAGE|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
                    407:        { TTYC_KPRV2, KEYC_PPAGE|KEYC_SHIFT },
                    408:        { TTYC_KPRV3, KEYC_PPAGE|KEYC_META|KEYC_IMPLIED_META },
                    409:        { TTYC_KPRV4, KEYC_PPAGE|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    410:        { TTYC_KPRV5, KEYC_PPAGE|KEYC_CTRL },
                    411:        { TTYC_KPRV6, KEYC_PPAGE|KEYC_SHIFT|KEYC_CTRL },
                    412:        { TTYC_KPRV7, KEYC_PPAGE|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
                    413:        { TTYC_KRIT2, KEYC_RIGHT|KEYC_SHIFT },
                    414:        { TTYC_KRIT3, KEYC_RIGHT|KEYC_META|KEYC_IMPLIED_META },
                    415:        { TTYC_KRIT4, KEYC_RIGHT|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    416:        { TTYC_KRIT5, KEYC_RIGHT|KEYC_CTRL },
                    417:        { TTYC_KRIT6, KEYC_RIGHT|KEYC_SHIFT|KEYC_CTRL },
                    418:        { TTYC_KRIT7, KEYC_RIGHT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
                    419:        { TTYC_KRI, KEYC_UP|KEYC_SHIFT },
                    420:        { TTYC_KUP2, KEYC_UP|KEYC_SHIFT },
                    421:        { TTYC_KUP3, KEYC_UP|KEYC_META|KEYC_IMPLIED_META },
                    422:        { TTYC_KUP4, KEYC_UP|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
                    423:        { TTYC_KUP5, KEYC_UP|KEYC_CTRL },
                    424:        { TTYC_KUP6, KEYC_UP|KEYC_SHIFT|KEYC_CTRL },
                    425:        { TTYC_KUP7, KEYC_UP|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
1.1       nicm      426: };
                    427:
1.48      nicm      428: /* Add key to tree. */
1.86      nicm      429: static void
1.76      nicm      430: tty_keys_add(struct tty *tty, const char *s, key_code key)
1.16      nicm      431: {
1.29      nicm      432:        struct tty_key  *tk;
                    433:        size_t           size;
1.143     nicm      434:        const char      *keystr;
1.1       nicm      435:
1.135     nicm      436:        keystr = key_string_lookup_key(key, 1);
1.29      nicm      437:        if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) {
1.76      nicm      438:                log_debug("new key %s: 0x%llx (%s)", s, key, keystr);
1.16      nicm      439:                tty_keys_add1(&tty->key_tree, s, key);
1.29      nicm      440:        } else {
1.76      nicm      441:                log_debug("replacing key %s: 0x%llx (%s)", s, key, keystr);
1.29      nicm      442:                tk->key = key;
1.16      nicm      443:        }
1.1       nicm      444: }
                    445:
1.16      nicm      446: /* Add next node to the tree. */
1.86      nicm      447: static void
1.76      nicm      448: tty_keys_add1(struct tty_key **tkp, const char *s, key_code key)
1.1       nicm      449: {
1.16      nicm      450:        struct tty_key  *tk;
1.1       nicm      451:
1.16      nicm      452:        /* Allocate a tree entry if there isn't one already. */
                    453:        tk = *tkp;
                    454:        if (tk == NULL) {
                    455:                tk = *tkp = xcalloc(1, sizeof *tk);
                    456:                tk->ch = *s;
1.83      nicm      457:                tk->key = KEYC_UNKNOWN;
1.16      nicm      458:        }
                    459:
                    460:        /* Find the next entry. */
                    461:        if (*s == tk->ch) {
                    462:                /* Move forward in string. */
                    463:                s++;
                    464:
                    465:                /* If this is the end of the string, no more is necessary. */
                    466:                if (*s == '\0') {
                    467:                        tk->key = key;
                    468:                        return;
                    469:                }
                    470:
                    471:                /* Use the child tree for the next character. */
                    472:                tkp = &tk->next;
1.27      nicm      473:        } else {
1.16      nicm      474:                if (*s < tk->ch)
                    475:                        tkp = &tk->left;
                    476:                else if (*s > tk->ch)
                    477:                        tkp = &tk->right;
                    478:        }
                    479:
                    480:        /* And recurse to add it. */
                    481:        tty_keys_add1(tkp, s, key);
1.1       nicm      482: }
                    483:
1.16      nicm      484: /* Initialise a key tree from the table. */
1.1       nicm      485: void
1.48      nicm      486: tty_keys_build(struct tty *tty)
1.1       nicm      487: {
1.48      nicm      488:        const struct tty_default_key_raw        *tdkr;
1.132     nicm      489:        const struct tty_default_key_xterm      *tdkx;
1.48      nicm      490:        const struct tty_default_key_code       *tdkc;
1.143     nicm      491:        u_int                                    i, j;
1.109     nicm      492:        const char                              *s;
1.99      nicm      493:        struct options_entry                    *o;
1.107     nicm      494:        struct options_array_item               *a;
1.109     nicm      495:        union options_value                     *ov;
1.132     nicm      496:        char                                     copy[16];
                    497:        key_code                                 key;
1.1       nicm      498:
1.48      nicm      499:        if (tty->key_tree != NULL)
1.71      nicm      500:                tty_keys_free(tty);
1.16      nicm      501:        tty->key_tree = NULL;
1.1       nicm      502:
1.132     nicm      503:        for (i = 0; i < nitems(tty_default_xterm_keys); i++) {
                    504:                tdkx = &tty_default_xterm_keys[i];
                    505:                for (j = 2; j < nitems(tty_default_xterm_modifiers); j++) {
                    506:                        strlcpy(copy, tdkx->template, sizeof copy);
                    507:                        copy[strcspn(copy, "_")] = '0' + j;
                    508:
                    509:                        key = tdkx->key|tty_default_xterm_modifiers[j];
                    510:                        tty_keys_add(tty, copy, key);
                    511:                }
                    512:        }
1.48      nicm      513:        for (i = 0; i < nitems(tty_default_raw_keys); i++) {
                    514:                tdkr = &tty_default_raw_keys[i];
                    515:
                    516:                s = tdkr->string;
1.51      nicm      517:                if (*s != '\0')
                    518:                        tty_keys_add(tty, s, tdkr->key);
1.48      nicm      519:        }
                    520:        for (i = 0; i < nitems(tty_default_code_keys); i++) {
                    521:                tdkc = &tty_default_code_keys[i];
                    522:
                    523:                s = tty_term_string(tty->term, tdkc->code);
1.51      nicm      524:                if (*s != '\0')
                    525:                        tty_keys_add(tty, s, tdkc->key);
1.1       nicm      526:
1.99      nicm      527:        }
                    528:
                    529:        o = options_get(global_options, "user-keys");
1.107     nicm      530:        if (o != NULL) {
                    531:                a = options_array_first(o);
                    532:                while (a != NULL) {
1.112     nicm      533:                        i = options_array_item_index(a);
1.109     nicm      534:                        ov = options_array_item_value(a);
1.110     nicm      535:                        tty_keys_add(tty, ov->string, KEYC_USER + i);
1.107     nicm      536:                        a = options_array_next(a);
1.99      nicm      537:                }
1.1       nicm      538:        }
                    539: }
                    540:
1.16      nicm      541: /* Free the entire key tree. */
1.1       nicm      542: void
                    543: tty_keys_free(struct tty *tty)
                    544: {
1.16      nicm      545:        tty_keys_free1(tty->key_tree);
                    546: }
1.1       nicm      547:
1.16      nicm      548: /* Free a single key. */
1.86      nicm      549: static void
1.16      nicm      550: tty_keys_free1(struct tty_key *tk)
                    551: {
                    552:        if (tk->next != NULL)
                    553:                tty_keys_free1(tk->next);
                    554:        if (tk->left != NULL)
                    555:                tty_keys_free1(tk->left);
                    556:        if (tk->right != NULL)
                    557:                tty_keys_free1(tk->right);
1.42      nicm      558:        free(tk);
1.1       nicm      559: }
                    560:
1.16      nicm      561: /* Lookup a key in the tree. */
1.86      nicm      562: static struct tty_key *
1.16      nicm      563: tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
1.1       nicm      564: {
1.16      nicm      565:        *size = 0;
                    566:        return (tty_keys_find1(tty->key_tree, buf, len, size));
                    567: }
1.1       nicm      568:
1.16      nicm      569: /* Find the next node. */
1.86      nicm      570: static struct tty_key *
1.16      nicm      571: tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
                    572: {
1.106     nicm      573:        /* If no data, no match. */
                    574:        if (len == 0)
                    575:                return (NULL);
                    576:
1.16      nicm      577:        /* If the node is NULL, this is the end of the tree. No match. */
                    578:        if (tk == NULL)
1.1       nicm      579:                return (NULL);
                    580:
1.16      nicm      581:        /* Pick the next in the sequence. */
                    582:        if (tk->ch == *buf) {
                    583:                /* Move forward in the string. */
                    584:                buf++; len--;
                    585:                (*size)++;
1.1       nicm      586:
1.16      nicm      587:                /* At the end of the string, return the current node. */
1.83      nicm      588:                if (len == 0 || (tk->next == NULL && tk->key != KEYC_UNKNOWN))
1.16      nicm      589:                        return (tk);
1.1       nicm      590:
1.16      nicm      591:                /* Move into the next tree for the following character. */
                    592:                tk = tk->next;
                    593:        } else {
                    594:                if (*buf < tk->ch)
                    595:                        tk = tk->left;
                    596:                else if (*buf > tk->ch)
                    597:                        tk = tk->right;
1.1       nicm      598:        }
                    599:
1.16      nicm      600:        /* Move to the next in the tree. */
                    601:        return (tty_keys_find1(tk, buf, len, size));
1.1       nicm      602: }
                    603:
1.86      nicm      604: /* Look up part of the next key. */
                    605: static int
                    606: tty_keys_next1(struct tty *tty, const char *buf, size_t len, key_code *key,
1.87      nicm      607:     size_t *size, int expired)
1.86      nicm      608: {
1.94      nicm      609:        struct client           *c = tty->client;
1.86      nicm      610:        struct tty_key          *tk, *tk1;
                    611:        struct utf8_data         ud;
                    612:        enum utf8_state          more;
1.138     nicm      613:        utf8_char                uc;
1.86      nicm      614:        u_int                    i;
                    615:
1.94      nicm      616:        log_debug("%s: next key is %zu (%.*s) (expired=%d)", c->name, len,
                    617:            (int)len, buf, expired);
1.86      nicm      618:
                    619:        /* Is this a known key? */
                    620:        tk = tty_keys_find(tty, buf, len, size);
1.87      nicm      621:        if (tk != NULL && tk->key != KEYC_UNKNOWN) {
1.86      nicm      622:                tk1 = tk;
                    623:                do
1.94      nicm      624:                        log_debug("%s: keys in list: %#llx", c->name, tk1->key);
1.86      nicm      625:                while ((tk1 = tk1->next) != NULL);
1.87      nicm      626:                if (tk->next != NULL && !expired)
                    627:                        return (1);
1.86      nicm      628:                *key = tk->key;
1.87      nicm      629:                return (0);
1.86      nicm      630:        }
1.97      nicm      631:
1.86      nicm      632:        /* Is this valid UTF-8? */
                    633:        more = utf8_open(&ud, (u_char)*buf);
                    634:        if (more == UTF8_MORE) {
                    635:                *size = ud.size;
1.87      nicm      636:                if (len < ud.size) {
                    637:                        if (!expired)
                    638:                                return (1);
                    639:                        return (-1);
                    640:                }
1.86      nicm      641:                for (i = 1; i < ud.size; i++)
                    642:                        more = utf8_append(&ud, (u_char)buf[i]);
                    643:                if (more != UTF8_DONE)
1.87      nicm      644:                        return (-1);
1.86      nicm      645:
1.138     nicm      646:                if (utf8_from_data(&ud, &uc) != UTF8_DONE)
1.87      nicm      647:                        return (-1);
1.138     nicm      648:                *key = uc;
1.86      nicm      649:
1.94      nicm      650:                log_debug("%s: UTF-8 key %.*s %#llx", c->name, (int)ud.size,
1.138     nicm      651:                    ud.data, *key);
1.86      nicm      652:                return (0);
                    653:        }
                    654:
                    655:        return (-1);
                    656: }
                    657:
1.111     nicm      658: /* Process at least one key in the buffer. Return 0 if no keys present. */
                    659: int
1.14      nicm      660: tty_keys_next(struct tty *tty)
1.1       nicm      661: {
1.111     nicm      662:        struct client           *c = tty->client;
                    663:        struct timeval           tv;
                    664:        const char              *buf;
                    665:        size_t                   len, size;
                    666:        cc_t                     bspace;
                    667:        int                      delay, expired = 0, n;
                    668:        key_code                 key;
                    669:        struct mouse_event       m = { 0 };
                    670:        struct key_event        *event;
1.1       nicm      671:
1.51      nicm      672:        /* Get key buffer. */
1.93      nicm      673:        buf = EVBUFFER_DATA(tty->in);
                    674:        len = EVBUFFER_LENGTH(tty->in);
1.1       nicm      675:        if (len == 0)
1.14      nicm      676:                return (0);
1.94      nicm      677:        log_debug("%s: keys are %zu (%.*s)", c->name, len, (int)len, buf);
1.1       nicm      678:
1.104     nicm      679:        /* Is this a clipboard response? */
                    680:        switch (tty_keys_clipboard(tty, buf, len, &size)) {
                    681:        case 0:         /* yes */
                    682:                key = KEYC_UNKNOWN;
                    683:                goto complete_key;
                    684:        case -1:        /* no, or not valid */
                    685:                break;
                    686:        case 1:         /* partial */
                    687:                goto partial_key;
                    688:        }
                    689:
1.161     nicm      690:        /* Is this a primary device attributes response? */
1.91      nicm      691:        switch (tty_keys_device_attributes(tty, buf, len, &size)) {
                    692:        case 0:         /* yes */
                    693:                key = KEYC_UNKNOWN;
                    694:                goto complete_key;
                    695:        case -1:        /* no, or not valid */
                    696:                break;
                    697:        case 1:         /* partial */
                    698:                goto partial_key;
                    699:        }
                    700:
1.161     nicm      701:        /* Is this a secondary device attributes response? */
                    702:        switch (tty_keys_device_attributes2(tty, buf, len, &size)) {
                    703:        case 0:         /* yes */
                    704:                key = KEYC_UNKNOWN;
                    705:                goto complete_key;
                    706:        case -1:        /* no, or not valid */
                    707:                break;
                    708:        case 1:         /* partial */
                    709:                goto partial_key;
                    710:        }
                    711:
1.128     nicm      712:        /* Is this an extended device attributes response? */
                    713:        switch (tty_keys_extended_device_attributes(tty, buf, len, &size)) {
1.119     nicm      714:        case 0:         /* yes */
                    715:                key = KEYC_UNKNOWN;
                    716:                goto complete_key;
                    717:        case -1:        /* no, or not valid */
                    718:                break;
                    719:        case 1:         /* partial */
                    720:                goto partial_key;
                    721:        }
                    722:
1.163     nicm      723:        /* Is this a colours response? */
                    724:        switch (tty_keys_colours(tty, buf, len, &size)) {
                    725:        case 0:         /* yes */
                    726:                key = KEYC_UNKNOWN;
                    727:                goto complete_key;
                    728:        case -1:        /* no, or not valid */
                    729:                break;
                    730:        case 1:         /* partial */
                    731:                goto partial_key;
                    732:        }
                    733:
1.26      nicm      734:        /* Is this a mouse key press? */
1.111     nicm      735:        switch (tty_keys_mouse(tty, buf, len, &size, &m)) {
1.22      nicm      736:        case 0:         /* yes */
                    737:                key = KEYC_MOUSE;
1.51      nicm      738:                goto complete_key;
1.22      nicm      739:        case -1:        /* no, or not valid */
1.24      nicm      740:                break;
1.67      nicm      741:        case -2:        /* yes, but we don't care. */
1.73      nicm      742:                key = KEYC_MOUSE;
1.67      nicm      743:                goto discard_key;
1.22      nicm      744:        case 1:         /* partial */
                    745:                goto partial_key;
1.11      nicm      746:        }
                    747:
1.133     nicm      748:        /* Is this an extended key press? */
                    749:        switch (tty_keys_extended_key(tty, buf, len, &size, &key)) {
                    750:        case 0:         /* yes */
                    751:                goto complete_key;
                    752:        case -1:        /* no, or not valid */
                    753:                break;
                    754:        case 1:         /* partial */
                    755:                goto partial_key;
                    756:        }
                    757:
1.86      nicm      758: first_key:
1.101     nicm      759:        /* Try to lookup complete key. */
                    760:        n = tty_keys_next1(tty, buf, len, &key, &size, expired);
                    761:        if (n == 0)     /* found */
                    762:                goto complete_key;
                    763:        if (n == 1)
                    764:                goto partial_key;
                    765:
                    766:        /*
                    767:         * If not a complete key, look for key with an escape prefix (meta
                    768:         * modifier).
                    769:         */
1.105     nicm      770:        if (*buf == '\033' && len > 1) {
1.87      nicm      771:                /* Look for a key without the escape. */
                    772:                n = tty_keys_next1(tty, buf + 1, len - 1, &key, &size, expired);
                    773:                if (n == 0) {   /* found */
1.135     nicm      774:                        if (key & KEYC_IMPLIED_META) {
1.97      nicm      775:                                /*
                    776:                                 * We want the escape key as well as the xterm
                    777:                                 * key, because the xterm sequence implicitly
                    778:                                 * includes the escape (so if we see
                    779:                                 * \033\033[1;3D we know it is an Escape
                    780:                                 * followed by M-Left, not just M-Left).
                    781:                                 */
                    782:                                key = '\033';
                    783:                                size = 1;
                    784:                                goto complete_key;
                    785:                        }
1.134     nicm      786:                        key |= KEYC_META;
1.87      nicm      787:                        size++;
                    788:                        goto complete_key;
                    789:                }
                    790:                if (n == 1)     /* partial */
1.62      nicm      791:                        goto partial_key;
1.89      nicm      792:        }
1.1       nicm      793:
1.89      nicm      794:        /*
                    795:         * At this point, we know the key is not partial (with or without
                    796:         * escape). So pass it through even if the timer has not expired.
                    797:         */
                    798:        if (*buf == '\033' && len >= 2) {
1.134     nicm      799:                key = (u_char)buf[1] | KEYC_META;
1.89      nicm      800:                size = 2;
                    801:        } else {
                    802:                key = (u_char)buf[0];
                    803:                size = 1;
1.76      nicm      804:        }
1.51      nicm      805:        goto complete_key;
                    806:
                    807: partial_key:
1.94      nicm      808:        log_debug("%s: partial key %.*s", c->name, (int)len, buf);
1.14      nicm      809:
1.51      nicm      810:        /* If timer is going, check for expiration. */
                    811:        if (tty->flags & TTY_TIMER) {
                    812:                if (evtimer_initialized(&tty->key_timer) &&
1.59      nicm      813:                    !evtimer_pending(&tty->key_timer, NULL)) {
                    814:                        expired = 1;
1.51      nicm      815:                        goto first_key;
1.59      nicm      816:                }
1.39      nicm      817:                return (0);
1.51      nicm      818:        }
1.39      nicm      819:
1.51      nicm      820:        /* Get the time period. */
1.75      nicm      821:        delay = options_get_number(global_options, "escape-time");
1.156     nicm      822:        if (delay == 0)
                    823:                delay = 1;
1.28      nicm      824:        tv.tv_sec = delay / 1000;
                    825:        tv.tv_usec = (delay % 1000) * 1000L;
1.27      nicm      826:
1.51      nicm      827:        /* Start the timer. */
1.36      nicm      828:        if (event_initialized(&tty->key_timer))
                    829:                evtimer_del(&tty->key_timer);
1.14      nicm      830:        evtimer_set(&tty->key_timer, tty_keys_callback, tty);
                    831:        evtimer_add(&tty->key_timer, &tv);
1.27      nicm      832:
1.51      nicm      833:        tty->flags |= TTY_TIMER;
1.14      nicm      834:        return (0);
1.1       nicm      835:
1.51      nicm      836: complete_key:
1.94      nicm      837:        log_debug("%s: complete key %.*s %#llx", c->name, (int)size, buf, key);
1.88      nicm      838:
                    839:        /*
                    840:         * Check for backspace key using termios VERASE - the terminfo
                    841:         * kbs entry is extremely unreliable, so cannot be safely
                    842:         * used. termios should have a better idea.
                    843:         */
                    844:        bspace = tty->tio.c_cc[VERASE];
                    845:        if (bspace != _POSIX_VDISABLE && (key & KEYC_MASK_KEY) == bspace)
1.135     nicm      846:                key = (key & KEYC_MASK_MODIFIERS)|KEYC_BSPACE;
1.16      nicm      847:
1.51      nicm      848:        /* Remove data from buffer. */
1.93      nicm      849:        evbuffer_drain(tty->in, size);
1.16      nicm      850:
1.51      nicm      851:        /* Remove key timer. */
1.36      nicm      852:        if (event_initialized(&tty->key_timer))
                    853:                evtimer_del(&tty->key_timer);
1.51      nicm      854:        tty->flags &= ~TTY_TIMER;
1.56      nicm      855:
                    856:        /* Check for focus events. */
1.148     nicm      857:        if (key == KEYC_FOCUS_OUT) {
1.149     nicm      858:                c->flags &= ~CLIENT_FOCUSED;
                    859:                window_update_focus(c->session->curw->window);
1.148     nicm      860:                notify_client("client-focus-out", c);
                    861:        } else if (key == KEYC_FOCUS_IN) {
1.149     nicm      862:                c->flags |= CLIENT_FOCUSED;
1.148     nicm      863:                notify_client("client-focus-in", c);
1.149     nicm      864:                window_update_focus(c->session->curw->window);
1.148     nicm      865:        }
1.16      nicm      866:
1.51      nicm      867:        /* Fire the key. */
1.111     nicm      868:        if (key != KEYC_UNKNOWN) {
                    869:                event = xmalloc(sizeof *event);
                    870:                event->key = key;
                    871:                memcpy(&event->m, &m, sizeof event->m);
1.113     nicm      872:                if (!server_client_handle_key(c, event))
                    873:                        free(event);
1.111     nicm      874:        }
1.16      nicm      875:
1.14      nicm      876:        return (1);
1.67      nicm      877:
                    878: discard_key:
1.94      nicm      879:        log_debug("%s: discard key %.*s %#llx", c->name, (int)size, buf, key);
1.67      nicm      880:
                    881:        /* Remove data from buffer. */
1.93      nicm      882:        evbuffer_drain(tty->in, size);
1.67      nicm      883:
                    884:        return (1);
1.14      nicm      885: }
                    886:
1.16      nicm      887: /* Key timer callback. */
1.86      nicm      888: static void
1.81      nicm      889: tty_keys_callback(__unused int fd, __unused short events, void *data)
1.14      nicm      890: {
                    891:        struct tty      *tty = data;
                    892:
1.51      nicm      893:        if (tty->flags & TTY_TIMER) {
                    894:                while (tty_keys_next(tty))
                    895:                        ;
                    896:        }
1.133     nicm      897: }
                    898:
                    899: /*
                    900:  * Handle extended key input. This has two forms: \033[27;m;k~ and \033[k;mu,
                    901:  * where k is key as a number and m is a modifier. Returns 0 for success, -1
                    902:  * for failure, 1 for partial;
                    903:  */
                    904: static int
                    905: tty_keys_extended_key(struct tty *tty, const char *buf, size_t len,
                    906:     size_t *size, key_code *key)
                    907: {
                    908:        struct client   *c = tty->client;
                    909:        size_t           end;
                    910:        u_int            number, modifiers;
                    911:        char             tmp[64];
1.143     nicm      912:        cc_t             bspace;
                    913:        key_code         nkey;
1.144     nicm      914:        key_code         onlykey;
1.133     nicm      915:
                    916:        *size = 0;
                    917:
                    918:        /* First two bytes are always \033[. */
                    919:        if (buf[0] != '\033')
                    920:                return (-1);
                    921:        if (len == 1)
                    922:                return (1);
                    923:        if (buf[1] != '[')
                    924:                return (-1);
                    925:        if (len == 2)
                    926:                return (1);
                    927:
                    928:        /*
                    929:         * Look for a terminator. Stop at either '~' or anything that isn't a
                    930:         * number or ';'.
                    931:         */
                    932:        for (end = 2; end < len && end != sizeof tmp; end++) {
                    933:                if (buf[end] == '~')
                    934:                        break;
                    935:                if (!isdigit((u_char)buf[end]) && buf[end] != ';')
                    936:                        break;
                    937:        }
                    938:        if (end == len)
                    939:                return (1);
                    940:        if (end == sizeof tmp || (buf[end] != '~' && buf[end] != 'u'))
                    941:                return (-1);
                    942:
                    943:        /* Copy to the buffer. */
                    944:        memcpy(tmp, buf + 2, end);
                    945:        tmp[end] = '\0';
                    946:
                    947:        /* Try to parse either form of key. */
                    948:        if (buf[end] == '~') {
                    949:                if (sscanf(tmp, "27;%u;%u", &modifiers, &number) != 2)
                    950:                        return (-1);
                    951:        } else {
                    952:                if (sscanf(tmp ,"%u;%u", &number, &modifiers) != 2)
                    953:                        return (-1);
                    954:        }
                    955:        *size = end + 1;
                    956:
1.143     nicm      957:        /* Store the key. */
                    958:        bspace = tty->tio.c_cc[VERASE];
                    959:        if (bspace != _POSIX_VDISABLE && number == bspace)
                    960:                nkey = KEYC_BSPACE;
                    961:        else
                    962:                nkey = number;
                    963:
                    964:        /* Update the modifiers. */
1.158     nicm      965:        if (modifiers > 0) {
                    966:                modifiers--;
                    967:                if (modifiers & 1)
                    968:                        nkey |= KEYC_SHIFT;
                    969:                if (modifiers & 2)
                    970:                        nkey |= (KEYC_META|KEYC_IMPLIED_META); /* Alt */
                    971:                if (modifiers & 4)
                    972:                        nkey |= KEYC_CTRL;
                    973:                if (modifiers & 8)
                    974:                        nkey |= (KEYC_META|KEYC_IMPLIED_META); /* Meta */
1.133     nicm      975:        }
1.143     nicm      976:
1.144     nicm      977:        /*
                    978:         * Don't allow both KEYC_CTRL and as an implied modifier. Also convert
                    979:         * C-X into C-x and so on.
                    980:         */
1.145     nicm      981:        if (nkey & KEYC_CTRL) {
1.144     nicm      982:                onlykey = (nkey & KEYC_MASK_KEY);
1.147     nicm      983:                if (onlykey < 32 &&
                    984:                    onlykey != 9 &&
                    985:                    onlykey != 13 &&
                    986:                    onlykey != 27)
1.146     nicm      987:                        /* nothing */;
                    988:                else if (onlykey >= 97 && onlykey <= 122)
                    989:                        onlykey -= 96;
                    990:                else if (onlykey >= 64 && onlykey <= 95)
                    991:                        onlykey -= 64;
                    992:                else if (onlykey == 32)
                    993:                        onlykey = 0;
                    994:                else if (onlykey == 63)
                    995:                        onlykey = 127;
                    996:                else
                    997:                        onlykey |= KEYC_CTRL;
                    998:                nkey = onlykey|((nkey & KEYC_MASK_MODIFIERS) & ~KEYC_CTRL);
1.143     nicm      999:        }
                   1000:
1.133     nicm     1001:        if (log_get_level() != 0) {
                   1002:                log_debug("%s: extended key %.*s is %llx (%s)", c->name,
1.143     nicm     1003:                    (int)*size, buf, nkey, key_string_lookup_key(nkey, 1));
1.133     nicm     1004:        }
1.160     nicm     1005:        *key = nkey;
1.133     nicm     1006:        return (0);
1.1       nicm     1007: }
                   1008:
1.27      nicm     1009: /*
1.22      nicm     1010:  * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
1.166     nicm     1011:  * (probably a mouse sequence but need more data), -2 if an invalid mouse
                   1012:  * sequence.
1.22      nicm     1013:  */
1.86      nicm     1014: static int
1.111     nicm     1015: tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size,
                   1016:     struct mouse_event *m)
1.1       nicm     1017: {
1.111     nicm     1018:        struct client   *c = tty->client;
                   1019:        u_int            i, x, y, b, sgr_b;
                   1020:        u_char           sgr_type, ch;
1.33      nicm     1021:
1.1       nicm     1022:        /*
1.33      nicm     1023:         * Standard mouse sequences are \033[M followed by three characters
1.55      nicm     1024:         * indicating button, X and Y, all based at 32 with 1,1 top-left.
1.33      nicm     1025:         *
                   1026:         * UTF-8 mouse sequences are similar but the three are expressed as
                   1027:         * UTF-8 characters.
1.55      nicm     1028:         *
                   1029:         * SGR extended mouse sequences are \033[< followed by three numbers in
                   1030:         * decimal and separated by semicolons indicating button, X and Y. A
                   1031:         * trailing 'M' is click or scroll and trailing 'm' release. All are
                   1032:         * based at 0 with 1,1 top-left.
1.1       nicm     1033:         */
                   1034:
1.22      nicm     1035:        *size = 0;
1.72      nicm     1036:        x = y = b = sgr_b = 0;
                   1037:        sgr_type = ' ';
1.22      nicm     1038:
1.55      nicm     1039:        /* First two bytes are always \033[. */
1.22      nicm     1040:        if (buf[0] != '\033')
                   1041:                return (-1);
                   1042:        if (len == 1)
                   1043:                return (1);
                   1044:        if (buf[1] != '[')
                   1045:                return (-1);
                   1046:        if (len == 2)
                   1047:                return (1);
                   1048:
1.55      nicm     1049:        /*
1.82      nicm     1050:         * Third byte is M in old standard (and UTF-8 extension which we do not
                   1051:         * support), < in SGR extension.
1.55      nicm     1052:         */
                   1053:        if (buf[2] == 'M') {
                   1054:                /* Read the three inputs. */
                   1055:                *size = 3;
                   1056:                for (i = 0; i < 3; i++) {
                   1057:                        if (len <= *size)
                   1058:                                return (1);
1.94      nicm     1059:                        ch = (u_char)buf[(*size)++];
1.55      nicm     1060:                        if (i == 0)
1.94      nicm     1061:                                b = ch;
1.55      nicm     1062:                        else if (i == 1)
1.94      nicm     1063:                                x = ch;
1.55      nicm     1064:                        else
1.94      nicm     1065:                                y = ch;
1.33      nicm     1066:                }
1.94      nicm     1067:                log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf);
1.16      nicm     1068:
1.55      nicm     1069:                /* Check and return the mouse input. */
1.155     nicm     1070:                if (b < MOUSE_PARAM_BTN_OFF ||
                   1071:                    x < MOUSE_PARAM_POS_OFF ||
                   1072:                    y < MOUSE_PARAM_POS_OFF)
1.166     nicm     1073:                        return (-2);
1.155     nicm     1074:                b -= MOUSE_PARAM_BTN_OFF;
                   1075:                x -= MOUSE_PARAM_POS_OFF;
                   1076:                y -= MOUSE_PARAM_POS_OFF;
1.55      nicm     1077:        } else if (buf[2] == '<') {
                   1078:                /* Read the three inputs. */
                   1079:                *size = 3;
                   1080:                while (1) {
                   1081:                        if (len <= *size)
                   1082:                                return (1);
1.94      nicm     1083:                        ch = (u_char)buf[(*size)++];
                   1084:                        if (ch == ';')
1.55      nicm     1085:                                break;
1.94      nicm     1086:                        if (ch < '0' || ch > '9')
1.55      nicm     1087:                                return (-1);
1.94      nicm     1088:                        sgr_b = 10 * sgr_b + (ch - '0');
1.55      nicm     1089:                }
                   1090:                while (1) {
                   1091:                        if (len <= *size)
                   1092:                                return (1);
1.94      nicm     1093:                        ch = (u_char)buf[(*size)++];
                   1094:                        if (ch == ';')
1.55      nicm     1095:                                break;
1.94      nicm     1096:                        if (ch < '0' || ch > '9')
1.55      nicm     1097:                                return (-1);
1.94      nicm     1098:                        x = 10 * x + (ch - '0');
1.55      nicm     1099:                }
                   1100:                while (1) {
                   1101:                        if (len <= *size)
                   1102:                                return (1);
1.94      nicm     1103:                        ch = (u_char)buf[(*size)++];
                   1104:                        if (ch == 'M' || ch == 'm')
1.55      nicm     1105:                                break;
1.94      nicm     1106:                        if (ch < '0' || ch > '9')
1.55      nicm     1107:                                return (-1);
1.94      nicm     1108:                        y = 10 * y + (ch - '0');
1.55      nicm     1109:                }
1.94      nicm     1110:                log_debug("%s: mouse input (SGR): %.*s", c->name, (int)*size,
                   1111:                    buf);
1.1       nicm     1112:
1.55      nicm     1113:                /* Check and return the mouse input. */
                   1114:                if (x < 1 || y < 1)
1.166     nicm     1115:                        return (-2);
1.55      nicm     1116:                x--;
                   1117:                y--;
1.72      nicm     1118:                b = sgr_b;
                   1119:
                   1120:                /* Type is M for press, m for release. */
1.94      nicm     1121:                sgr_type = ch;
1.72      nicm     1122:                if (sgr_type == 'm')
1.153     nicm     1123:                        b = 3;
1.67      nicm     1124:
                   1125:                /*
                   1126:                 * Some terminals (like PuTTY 0.63) mistakenly send
                   1127:                 * button-release events for scroll-wheel button-press event.
                   1128:                 * Discard it before it reaches any program running inside
                   1129:                 * tmux.
                   1130:                 */
1.153     nicm     1131:                if (sgr_type == 'm' && MOUSE_WHEEL(sgr_b))
1.67      nicm     1132:                    return (-2);
1.55      nicm     1133:        } else
1.22      nicm     1134:                return (-1);
1.45      nicm     1135:
1.72      nicm     1136:        /* Fill mouse event. */
1.111     nicm     1137:        m->lx = tty->mouse_last_x;
1.61      nicm     1138:        m->x = x;
1.111     nicm     1139:        m->ly = tty->mouse_last_y;
1.61      nicm     1140:        m->y = y;
1.114     nicm     1141:        m->lb = tty->mouse_last_b;
1.72      nicm     1142:        m->b = b;
                   1143:        m->sgr_type = sgr_type;
                   1144:        m->sgr_b = sgr_b;
1.111     nicm     1145:
                   1146:        /* Update last mouse state. */
                   1147:        tty->mouse_last_x = x;
                   1148:        tty->mouse_last_y = y;
1.114     nicm     1149:        tty->mouse_last_b = b;
1.104     nicm     1150:
                   1151:        return (0);
                   1152: }
                   1153:
                   1154: /*
                   1155:  * Handle OSC 52 clipboard input. Returns 0 for success, -1 for failure, 1 for
                   1156:  * partial.
                   1157:  */
                   1158: static int
1.154     nicm     1159: tty_keys_clipboard(struct tty *tty, const char *buf, size_t len, size_t *size)
1.104     nicm     1160: {
1.154     nicm     1161:        struct client           *c = tty->client;
                   1162:        struct window_pane      *wp;
1.167     nicm     1163:        size_t                   end, terminator = 0, needed;
1.154     nicm     1164:        char                    *copy, *out;
                   1165:        int                      outlen;
                   1166:        u_int                    i;
1.104     nicm     1167:
                   1168:        *size = 0;
                   1169:
1.128     nicm     1170:        /* First five bytes are always \033]52;. */
1.104     nicm     1171:        if (buf[0] != '\033')
                   1172:                return (-1);
                   1173:        if (len == 1)
                   1174:                return (1);
                   1175:        if (buf[1] != ']')
                   1176:                return (-1);
                   1177:        if (len == 2)
                   1178:                return (1);
                   1179:        if (buf[2] != '5')
                   1180:                return (-1);
                   1181:        if (len == 3)
                   1182:                return (1);
                   1183:        if (buf[3] != '2')
                   1184:                return (-1);
                   1185:        if (len == 4)
                   1186:                return (1);
                   1187:        if (buf[4] != ';')
                   1188:                return (-1);
                   1189:        if (len == 5)
                   1190:                return (1);
                   1191:
                   1192:        /* Find the terminator if any. */
                   1193:        for (end = 5; end < len; end++) {
                   1194:                if (buf[end] == '\007') {
                   1195:                        terminator = 1;
                   1196:                        break;
                   1197:                }
                   1198:                if (end > 5 && buf[end - 1] == '\033' && buf[end] == '\\') {
                   1199:                        terminator = 2;
                   1200:                        break;
                   1201:                }
                   1202:        }
                   1203:        if (end == len)
                   1204:                return (1);
                   1205:        *size = end + terminator;
                   1206:
                   1207:        /* Skip the initial part. */
                   1208:        buf += 5;
                   1209:        end -= 5;
1.151     nicm     1210:
                   1211:        /* Adjust end so that it points to the start of the terminator. */
                   1212:        end -= terminator - 1;
1.104     nicm     1213:
                   1214:        /* Get the second argument. */
                   1215:        while (end != 0 && *buf != ';') {
                   1216:                buf++;
                   1217:                end--;
                   1218:        }
                   1219:        if (end == 0 || end == 1)
                   1220:                return (0);
                   1221:        buf++;
                   1222:        end--;
1.152     nicm     1223:
                   1224:        /* If we did not request this, ignore it. */
                   1225:        if (~tty->flags & TTY_OSC52QUERY)
                   1226:                return (0);
                   1227:        tty->flags &= ~TTY_OSC52QUERY;
1.154     nicm     1228:        evtimer_del(&tty->clipboard_timer);
1.104     nicm     1229:
                   1230:        /* It has to be a string so copy it. */
                   1231:        copy = xmalloc(end + 1);
                   1232:        memcpy(copy, buf, end);
                   1233:        copy[end] = '\0';
                   1234:
                   1235:        /* Convert from base64. */
                   1236:        needed = (end / 4) * 3;
                   1237:        out = xmalloc(needed);
                   1238:        if ((outlen = b64_pton(copy, out, len)) == -1) {
                   1239:                free(out);
                   1240:                free(copy);
                   1241:                return (0);
                   1242:        }
                   1243:        free(copy);
                   1244:
1.154     nicm     1245:        /* Create a new paste buffer and forward to panes. */
1.104     nicm     1246:        log_debug("%s: %.*s", __func__, outlen, out);
1.154     nicm     1247:        if (c->flags & CLIENT_CLIPBOARDBUFFER) {
                   1248:                paste_add(NULL, out, outlen);
                   1249:                c->flags &= ~CLIENT_CLIPBOARDBUFFER;
                   1250:        }
                   1251:        for (i = 0; i < c->clipboard_npanes; i++) {
                   1252:                wp = window_pane_find_by_id(c->clipboard_panes[i]);
                   1253:                if (wp != NULL)
                   1254:                        input_reply_clipboard(wp->event, out, outlen, "\033\\");
                   1255:        }
                   1256:        free(c->clipboard_panes);
                   1257:        c->clipboard_panes = NULL;
                   1258:        c->clipboard_npanes = 0;
1.91      nicm     1259:
                   1260:        return (0);
                   1261: }
                   1262:
                   1263: /*
1.161     nicm     1264:  * Handle primary device attributes input. Returns 0 for success, -1 for
1.124     nicm     1265:  * failure, 1 for partial.
1.91      nicm     1266:  */
                   1267: static int
                   1268: tty_keys_device_attributes(struct tty *tty, const char *buf, size_t len,
                   1269:     size_t *size)
                   1270: {
1.118     nicm     1271:        struct client   *c = tty->client;
1.162     nicm     1272:        int             *features = &c->term_features;
1.118     nicm     1273:        u_int            i, n = 0;
1.161     nicm     1274:        char             tmp[128], *endptr, p[32] = { 0 }, *cp, *next;
1.91      nicm     1275:
                   1276:        *size = 0;
1.120     nicm     1277:        if (tty->flags & TTY_HAVEDA)
                   1278:                return (-1);
1.91      nicm     1279:
1.161     nicm     1280:        /* First three bytes are always \033[?. */
1.91      nicm     1281:        if (buf[0] != '\033')
                   1282:                return (-1);
                   1283:        if (len == 1)
                   1284:                return (1);
                   1285:        if (buf[1] != '[')
                   1286:                return (-1);
                   1287:        if (len == 2)
                   1288:                return (1);
1.161     nicm     1289:        if (buf[2] != '?')
1.91      nicm     1290:                return (-1);
                   1291:        if (len == 3)
                   1292:                return (1);
                   1293:
1.163     nicm     1294:        /* Copy the rest up to a c. */
1.161     nicm     1295:        for (i = 0; i < (sizeof tmp); i++) {
1.91      nicm     1296:                if (3 + i == len)
                   1297:                        return (1);
1.128     nicm     1298:                if (buf[3 + i] == 'c')
                   1299:                        break;
1.91      nicm     1300:                tmp[i] = buf[3 + i];
                   1301:        }
1.161     nicm     1302:        if (i == (sizeof tmp))
1.91      nicm     1303:                return (-1);
                   1304:        tmp[i] = '\0';
                   1305:        *size = 4 + i;
1.141     nicm     1306:
1.161     nicm     1307:        /* Convert all arguments to numbers. */
                   1308:        cp = tmp;
                   1309:        while ((next = strsep(&cp, ";")) != NULL) {
                   1310:                p[n] = strtoul(next, &endptr, 10);
                   1311:                if (*endptr != '\0')
                   1312:                        p[n] = 0;
                   1313:                if (++n == nitems(p))
                   1314:                        break;
                   1315:        }
                   1316:
1.169   ! nicm     1317:        /*
        !          1318:         * Add terminal features. Technically, VT420 and VT525 do not support
        !          1319:         * SIXEL, but some modern terminals report it anyway so we accept it
        !          1320:         * here too.
        !          1321:         */
1.161     nicm     1322:        switch (p[0]) {
                   1323:        case 62: /* VT220 */
                   1324:        case 63: /* VT320 */
                   1325:        case 64: /* VT420 */
1.169   ! nicm     1326:        case 65: /* VT525 */
1.162     nicm     1327:                for (i = 1; i < n; i++) {
                   1328:                        log_debug("%s: DA feature: %d", c->name, p[i]);
                   1329:                        if (p[i] == 4)
                   1330:                                tty_add_features(features, "sixel", ",");
                   1331:                }
1.161     nicm     1332:                break;
                   1333:        }
                   1334:        log_debug("%s: received primary DA %.*s", c->name, (int)*size, buf);
                   1335:
                   1336:        tty_update_features(tty);
                   1337:        tty->flags |= TTY_HAVEDA;
                   1338:
                   1339:        return (0);
                   1340: }
                   1341:
                   1342: /*
                   1343:  * Handle secondary device attributes input. Returns 0 for success, -1 for
                   1344:  * failure, 1 for partial.
                   1345:  */
                   1346: static int
                   1347: tty_keys_device_attributes2(struct tty *tty, const char *buf, size_t len,
                   1348:     size_t *size)
                   1349: {
                   1350:        struct client   *c = tty->client;
1.162     nicm     1351:        int             *features = &c->term_features;
1.161     nicm     1352:        u_int            i, n = 0;
                   1353:        char             tmp[128], *endptr, p[32] = { 0 }, *cp, *next;
                   1354:
                   1355:        *size = 0;
                   1356:        if (tty->flags & TTY_HAVEDA2)
                   1357:                return (-1);
                   1358:
                   1359:        /* First three bytes are always \033[>. */
                   1360:        if (buf[0] != '\033')
                   1361:                return (-1);
                   1362:        if (len == 1)
                   1363:                return (1);
                   1364:        if (buf[1] != '[')
                   1365:                return (-1);
                   1366:        if (len == 2)
                   1367:                return (1);
                   1368:        if (buf[2] != '>')
                   1369:                return (-1);
                   1370:        if (len == 3)
                   1371:                return (1);
                   1372:
1.163     nicm     1373:        /* Copy the rest up to a c. */
1.161     nicm     1374:        for (i = 0; i < (sizeof tmp); i++) {
                   1375:                if (3 + i == len)
                   1376:                        return (1);
                   1377:                if (buf[3 + i] == 'c')
                   1378:                        break;
                   1379:                tmp[i] = buf[3 + i];
                   1380:        }
                   1381:        if (i == (sizeof tmp))
                   1382:                return (-1);
                   1383:        tmp[i] = '\0';
                   1384:        *size = 4 + i;
1.91      nicm     1385:
1.124     nicm     1386:        /* Convert all arguments to numbers. */
1.116     nicm     1387:        cp = tmp;
                   1388:        while ((next = strsep(&cp, ";")) != NULL) {
                   1389:                p[n] = strtoul(next, &endptr, 10);
1.117     nicm     1390:                if (*endptr != '\0')
1.116     nicm     1391:                        p[n] = 0;
1.161     nicm     1392:                if (++n == nitems(p))
                   1393:                        break;
1.116     nicm     1394:        }
1.91      nicm     1395:
1.126     nicm     1396:        /* Add terminal features. */
1.116     nicm     1397:        switch (p[0]) {
1.124     nicm     1398:        case 41: /* VT420 */
1.162     nicm     1399:                tty_add_features(features, "margins,rectfill", ",");
1.91      nicm     1400:                break;
1.124     nicm     1401:        case 'M': /* mintty */
1.162     nicm     1402:                tty_default_features(features, "mintty", 0);
1.124     nicm     1403:                break;
1.126     nicm     1404:        case 'T': /* tmux */
1.162     nicm     1405:                tty_default_features(features, "tmux", 0);
1.124     nicm     1406:                break;
                   1407:        case 'U': /* rxvt-unicode */
1.162     nicm     1408:                tty_default_features(features, "rxvt-unicode", 0);
1.124     nicm     1409:                break;
1.91      nicm     1410:        }
1.124     nicm     1411:        log_debug("%s: received secondary DA %.*s", c->name, (int)*size, buf);
1.120     nicm     1412:
1.126     nicm     1413:        tty_update_features(tty);
1.161     nicm     1414:        tty->flags |= TTY_HAVEDA2;
1.120     nicm     1415:
1.119     nicm     1416:        return (0);
                   1417: }
                   1418:
                   1419: /*
1.128     nicm     1420:  * Handle extended device attributes input. Returns 0 for success, -1 for
                   1421:  * failure, 1 for partial.
1.119     nicm     1422:  */
                   1423: static int
1.128     nicm     1424: tty_keys_extended_device_attributes(struct tty *tty, const char *buf,
                   1425:     size_t len, size_t *size)
1.119     nicm     1426: {
                   1427:        struct client   *c = tty->client;
1.162     nicm     1428:        int             *features = &c->term_features;
1.119     nicm     1429:        u_int            i;
1.130     nicm     1430:        char             tmp[128];
1.119     nicm     1431:
                   1432:        *size = 0;
1.128     nicm     1433:        if (tty->flags & TTY_HAVEXDA)
1.120     nicm     1434:                return (-1);
1.119     nicm     1435:
1.128     nicm     1436:        /* First four bytes are always \033P>|. */
1.119     nicm     1437:        if (buf[0] != '\033')
                   1438:                return (-1);
                   1439:        if (len == 1)
                   1440:                return (1);
1.128     nicm     1441:        if (buf[1] != 'P')
1.119     nicm     1442:                return (-1);
                   1443:        if (len == 2)
                   1444:                return (1);
1.128     nicm     1445:        if (buf[2] != '>')
1.120     nicm     1446:                return (-1);
                   1447:        if (len == 3)
                   1448:                return (1);
1.128     nicm     1449:        if (buf[3] != '|')
                   1450:                return (-1);
                   1451:        if (len == 4)
                   1452:                return (1);
1.119     nicm     1453:
1.163     nicm     1454:        /* Copy the rest up to \033\. */
1.128     nicm     1455:        for (i = 0; i < (sizeof tmp) - 1; i++) {
                   1456:                if (4 + i == len)
1.119     nicm     1457:                        return (1);
1.128     nicm     1458:                if (buf[4 + i - 1] == '\033' && buf[4 + i] == '\\')
                   1459:                        break;
                   1460:                tmp[i] = buf[4 + i];
1.119     nicm     1461:        }
                   1462:        if (i == (sizeof tmp) - 1)
                   1463:                return (-1);
1.128     nicm     1464:        tmp[i - 1] = '\0';
                   1465:        *size = 5 + i;
1.119     nicm     1466:
1.126     nicm     1467:        /* Add terminal features. */
1.130     nicm     1468:        if (strncmp(tmp, "iTerm2 ", 7) == 0)
1.162     nicm     1469:                tty_default_features(features, "iTerm2", 0);
1.130     nicm     1470:        else if (strncmp(tmp, "tmux ", 5) == 0)
1.162     nicm     1471:                tty_default_features(features, "tmux", 0);
1.130     nicm     1472:        else if (strncmp(tmp, "XTerm(", 6) == 0)
1.162     nicm     1473:                tty_default_features(features, "XTerm", 0);
1.130     nicm     1474:        else if (strncmp(tmp, "mintty ", 7) == 0)
1.162     nicm     1475:                tty_default_features(features, "mintty", 0);
1.128     nicm     1476:        log_debug("%s: received extended DA %.*s", c->name, (int)*size, buf);
1.130     nicm     1477:
                   1478:        free(c->term_type);
                   1479:        c->term_type = xstrdup(tmp);
1.120     nicm     1480:
1.126     nicm     1481:        tty_update_features(tty);
1.128     nicm     1482:        tty->flags |= TTY_HAVEXDA;
1.163     nicm     1483:
                   1484:        return (0);
                   1485: }
                   1486:
                   1487: /*
                   1488:  * Handle foreground or background input. Returns 0 for success, -1 for
                   1489:  * failure, 1 for partial.
                   1490:  */
                   1491: static int
                   1492: tty_keys_colours(struct tty *tty, const char *buf, size_t len, size_t *size)
                   1493: {
                   1494:        struct client   *c = tty->client;
                   1495:        u_int            i;
                   1496:        char             tmp[128];
                   1497:        int              n;
                   1498:
                   1499:        *size = 0;
                   1500:
                   1501:        /* First four bytes are always \033]1 and 0 or 1 and ;. */
                   1502:        if (buf[0] != '\033')
                   1503:                return (-1);
                   1504:        if (len == 1)
                   1505:                return (1);
                   1506:        if (buf[1] != ']')
                   1507:                return (-1);
                   1508:        if (len == 2)
                   1509:                return (1);
                   1510:        if (buf[2] != '1')
                   1511:                return (-1);
                   1512:        if (len == 3)
                   1513:                return (1);
                   1514:        if (buf[3] != '0' && buf[3] != '1')
                   1515:                return (-1);
                   1516:        if (len == 4)
                   1517:                return (1);
                   1518:        if (buf[4] != ';')
                   1519:                return (-1);
                   1520:        if (len == 5)
                   1521:                return (1);
                   1522:
1.164     nicm     1523:        /* Copy the rest up to \033\ or \007. */
1.163     nicm     1524:        for (i = 0; i < (sizeof tmp) - 1; i++) {
                   1525:                if (5 + i == len)
                   1526:                        return (1);
                   1527:                if (buf[5 + i - 1] == '\033' && buf[5 + i] == '\\')
                   1528:                        break;
1.164     nicm     1529:                if (buf[5 + i] == '\007')
                   1530:                        break;
1.163     nicm     1531:                tmp[i] = buf[5 + i];
                   1532:        }
                   1533:        if (i == (sizeof tmp) - 1)
                   1534:                return (-1);
1.165     nicm     1535:        if (tmp[i - 1] == '\033')
                   1536:                tmp[i - 1] = '\0';
                   1537:        else
1.164     nicm     1538:                tmp[i] = '\0';
1.165     nicm     1539:        *size = 6 + i;
1.163     nicm     1540:
                   1541:        n = colour_parseX11(tmp);
                   1542:        if (n != -1 && buf[3] == '0') {
                   1543:                log_debug("%s: foreground is %s", c->name, colour_tostring(n));
                   1544:                tty->fg = n;
                   1545:        } else if (n != -1) {
                   1546:                log_debug("%s: background is %s", c->name, colour_tostring(n));
                   1547:                tty->bg = n;
                   1548:        }
1.120     nicm     1549:
1.22      nicm     1550:        return (0);
1.1       nicm     1551: }