[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.129

1.129   ! nicm        1: /* $OpenBSD: tty-keys.c,v 1.128 2020/05/16 14:16:25 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.35      nicm       24: #include <limits.h>
1.104     nicm       25: #include <resolv.h>
1.35      nicm       26: #include <stdlib.h>
1.1       nicm       27: #include <string.h>
1.3       nicm       28: #include <termios.h>
                     29: #include <unistd.h>
1.1       nicm       30:
                     31: #include "tmux.h"
                     32:
1.9       nicm       33: /*
1.50      nicm       34:  * Handle keys input from the outside terminal. tty_default_*_keys[] are a base
                     35:  * table of supported keys which are looked up in terminfo(5) and translated
                     36:  * into a ternary tree.
1.9       nicm       37:  */
                     38:
1.86      nicm       39: static void    tty_keys_add1(struct tty_key **, const char *, key_code);
                     40: static void    tty_keys_add(struct tty *, const char *, key_code);
                     41: static void    tty_keys_free1(struct tty_key *);
                     42: static struct tty_key *tty_keys_find1(struct tty_key *, const char *, size_t,
1.76      nicm       43:                    size_t *);
1.86      nicm       44: static struct tty_key *tty_keys_find(struct tty *, const char *, size_t,
                     45:                    size_t *);
                     46: static int     tty_keys_next1(struct tty *, const char *, size_t, key_code *,
1.87      nicm       47:                    size_t *, int);
1.86      nicm       48: static void    tty_keys_callback(int, short, void *);
1.111     nicm       49: static int     tty_keys_mouse(struct tty *, const char *, size_t, size_t *,
                     50:                    struct mouse_event *);
1.104     nicm       51: static int     tty_keys_clipboard(struct tty *, const char *, size_t,
                     52:                    size_t *);
1.91      nicm       53: static int     tty_keys_device_attributes(struct tty *, const char *, size_t,
                     54:                    size_t *);
1.128     nicm       55: static int     tty_keys_extended_device_attributes(struct tty *, const char *,
1.119     nicm       56:                    size_t, size_t *);
1.1       nicm       57:
1.48      nicm       58: /* Default raw keys. */
                     59: struct tty_default_key_raw {
1.1       nicm       60:        const char             *string;
1.76      nicm       61:        key_code                key;
1.1       nicm       62: };
1.90      nicm       63: static const struct tty_default_key_raw tty_default_raw_keys[] = {
1.125     nicm       64:        /* Application escape. */
                     65:        { "\033O[", '\033' },
                     66:
1.1       nicm       67:        /*
1.8       nicm       68:         * Numeric keypad. Just use the vt100 escape sequences here and always
                     69:         * put the terminal into keypad_xmit mode. Translation of numbers
                     70:         * mode/applications mode is done in input-keys.c.
1.1       nicm       71:         */
1.48      nicm       72:        { "\033Oo", KEYC_KP_SLASH },
                     73:        { "\033Oj", KEYC_KP_STAR },
                     74:        { "\033Om", KEYC_KP_MINUS },
                     75:        { "\033Ow", KEYC_KP_SEVEN },
                     76:        { "\033Ox", KEYC_KP_EIGHT },
                     77:        { "\033Oy", KEYC_KP_NINE },
                     78:        { "\033Ok", KEYC_KP_PLUS },
                     79:        { "\033Ot", KEYC_KP_FOUR },
                     80:        { "\033Ou", KEYC_KP_FIVE },
                     81:        { "\033Ov", KEYC_KP_SIX },
                     82:        { "\033Oq", KEYC_KP_ONE },
                     83:        { "\033Or", KEYC_KP_TWO },
                     84:        { "\033Os", KEYC_KP_THREE },
                     85:        { "\033OM", KEYC_KP_ENTER },
                     86:        { "\033Op", KEYC_KP_ZERO },
                     87:        { "\033On", KEYC_KP_PERIOD },
1.10      nicm       88:
1.29      nicm       89:        /* Arrow keys. */
1.48      nicm       90:        { "\033OA", KEYC_UP },
                     91:        { "\033OB", KEYC_DOWN },
                     92:        { "\033OC", KEYC_RIGHT },
                     93:        { "\033OD", KEYC_LEFT },
                     94:
                     95:        { "\033[A", KEYC_UP },
                     96:        { "\033[B", KEYC_DOWN },
                     97:        { "\033[C", KEYC_RIGHT },
                     98:        { "\033[D", KEYC_LEFT },
1.57      nicm       99:
                    100:        /* Other (xterm) "cursor" keys. */
                    101:        { "\033OH", KEYC_HOME },
                    102:        { "\033OF", KEYC_END },
                    103:
                    104:        { "\033[H", KEYC_HOME },
                    105:        { "\033[F", KEYC_END },
1.21      nicm      106:
                    107:        /* rxvt-style arrow + modifier keys. */
1.48      nicm      108:        { "\033Oa", KEYC_UP|KEYC_CTRL },
                    109:        { "\033Ob", KEYC_DOWN|KEYC_CTRL },
                    110:        { "\033Oc", KEYC_RIGHT|KEYC_CTRL },
                    111:        { "\033Od", KEYC_LEFT|KEYC_CTRL },
                    112:
                    113:        { "\033[a", KEYC_UP|KEYC_SHIFT },
                    114:        { "\033[b", KEYC_DOWN|KEYC_SHIFT },
                    115:        { "\033[c", KEYC_RIGHT|KEYC_SHIFT },
                    116:        { "\033[d", KEYC_LEFT|KEYC_SHIFT },
                    117:
                    118:        /* rxvt-style function + modifier keys (C = ^, S = $, C-S = @). */
                    119:        { "\033[11^", KEYC_F1|KEYC_CTRL },
                    120:        { "\033[12^", KEYC_F2|KEYC_CTRL },
                    121:        { "\033[13^", KEYC_F3|KEYC_CTRL },
                    122:        { "\033[14^", KEYC_F4|KEYC_CTRL },
                    123:        { "\033[15^", KEYC_F5|KEYC_CTRL },
                    124:        { "\033[17^", KEYC_F6|KEYC_CTRL },
                    125:        { "\033[18^", KEYC_F7|KEYC_CTRL },
                    126:        { "\033[19^", KEYC_F8|KEYC_CTRL },
                    127:        { "\033[20^", KEYC_F9|KEYC_CTRL },
                    128:        { "\033[21^", KEYC_F10|KEYC_CTRL },
                    129:        { "\033[23^", KEYC_F11|KEYC_CTRL },
                    130:        { "\033[24^", KEYC_F12|KEYC_CTRL },
                    131:        { "\033[2^", KEYC_IC|KEYC_CTRL },
                    132:        { "\033[3^", KEYC_DC|KEYC_CTRL },
                    133:        { "\033[7^", KEYC_HOME|KEYC_CTRL },
                    134:        { "\033[8^", KEYC_END|KEYC_CTRL },
                    135:        { "\033[6^", KEYC_NPAGE|KEYC_CTRL },
                    136:        { "\033[5^", KEYC_PPAGE|KEYC_CTRL },
                    137:
                    138:        { "\033[11$", KEYC_F1|KEYC_SHIFT },
                    139:        { "\033[12$", KEYC_F2|KEYC_SHIFT },
                    140:        { "\033[13$", KEYC_F3|KEYC_SHIFT },
                    141:        { "\033[14$", KEYC_F4|KEYC_SHIFT },
                    142:        { "\033[15$", KEYC_F5|KEYC_SHIFT },
                    143:        { "\033[17$", KEYC_F6|KEYC_SHIFT },
                    144:        { "\033[18$", KEYC_F7|KEYC_SHIFT },
                    145:        { "\033[19$", KEYC_F8|KEYC_SHIFT },
                    146:        { "\033[20$", KEYC_F9|KEYC_SHIFT },
                    147:        { "\033[21$", KEYC_F10|KEYC_SHIFT },
                    148:        { "\033[23$", KEYC_F11|KEYC_SHIFT },
                    149:        { "\033[24$", KEYC_F12|KEYC_SHIFT },
                    150:        { "\033[2$", KEYC_IC|KEYC_SHIFT },
                    151:        { "\033[3$", KEYC_DC|KEYC_SHIFT },
                    152:        { "\033[7$", KEYC_HOME|KEYC_SHIFT },
                    153:        { "\033[8$", KEYC_END|KEYC_SHIFT },
                    154:        { "\033[6$", KEYC_NPAGE|KEYC_SHIFT },
                    155:        { "\033[5$", KEYC_PPAGE|KEYC_SHIFT },
                    156:
                    157:        { "\033[11@", KEYC_F1|KEYC_CTRL|KEYC_SHIFT },
                    158:        { "\033[12@", KEYC_F2|KEYC_CTRL|KEYC_SHIFT },
                    159:        { "\033[13@", KEYC_F3|KEYC_CTRL|KEYC_SHIFT },
                    160:        { "\033[14@", KEYC_F4|KEYC_CTRL|KEYC_SHIFT },
                    161:        { "\033[15@", KEYC_F5|KEYC_CTRL|KEYC_SHIFT },
                    162:        { "\033[17@", KEYC_F6|KEYC_CTRL|KEYC_SHIFT },
                    163:        { "\033[18@", KEYC_F7|KEYC_CTRL|KEYC_SHIFT },
                    164:        { "\033[19@", KEYC_F8|KEYC_CTRL|KEYC_SHIFT },
                    165:        { "\033[20@", KEYC_F9|KEYC_CTRL|KEYC_SHIFT },
                    166:        { "\033[21@", KEYC_F10|KEYC_CTRL|KEYC_SHIFT },
                    167:        { "\033[23@", KEYC_F11|KEYC_CTRL|KEYC_SHIFT },
                    168:        { "\033[24@", KEYC_F12|KEYC_CTRL|KEYC_SHIFT },
                    169:        { "\033[2@", KEYC_IC|KEYC_CTRL|KEYC_SHIFT },
                    170:        { "\033[3@", KEYC_DC|KEYC_CTRL|KEYC_SHIFT },
                    171:        { "\033[7@", KEYC_HOME|KEYC_CTRL|KEYC_SHIFT },
                    172:        { "\033[8@", KEYC_END|KEYC_CTRL|KEYC_SHIFT },
                    173:        { "\033[6@", KEYC_NPAGE|KEYC_CTRL|KEYC_SHIFT },
                    174:        { "\033[5@", KEYC_PPAGE|KEYC_CTRL|KEYC_SHIFT },
1.56      nicm      175:
                    176:        /* Focus tracking. */
                    177:        { "\033[I", KEYC_FOCUS_IN },
                    178:        { "\033[O", KEYC_FOCUS_OUT },
1.98      nicm      179:
                    180:        /* Paste keys. */
                    181:        { "\033[200~", KEYC_PASTE_START },
                    182:        { "\033[201~", KEYC_PASTE_END },
1.48      nicm      183: };
1.29      nicm      184:
1.103     nicm      185: /*
                    186:  * Default terminfo(5) keys. Any keys that have builtin modifiers
                    187:  * (that is, where the key itself contains the modifiers) has the
                    188:  * KEYC_XTERM flag set so a leading escape is not treated as meta (and
                    189:  * probably removed).
                    190:  */
1.48      nicm      191: struct tty_default_key_code {
                    192:        enum tty_code_code      code;
1.76      nicm      193:        key_code                key;
1.48      nicm      194: };
1.90      nicm      195: static const struct tty_default_key_code tty_default_code_keys[] = {
1.29      nicm      196:        /* Function keys. */
1.48      nicm      197:        { TTYC_KF1, KEYC_F1 },
                    198:        { TTYC_KF2, KEYC_F2 },
                    199:        { TTYC_KF3, KEYC_F3 },
                    200:        { TTYC_KF4, KEYC_F4 },
                    201:        { TTYC_KF5, KEYC_F5 },
                    202:        { TTYC_KF6, KEYC_F6 },
                    203:        { TTYC_KF7, KEYC_F7 },
                    204:        { TTYC_KF8, KEYC_F8 },
                    205:        { TTYC_KF9, KEYC_F9 },
                    206:        { TTYC_KF10, KEYC_F10 },
                    207:        { TTYC_KF11, KEYC_F11 },
                    208:        { TTYC_KF12, KEYC_F12 },
1.70      nicm      209:
1.103     nicm      210:        { TTYC_KF13, KEYC_F1|KEYC_SHIFT|KEYC_XTERM },
                    211:        { TTYC_KF14, KEYC_F2|KEYC_SHIFT|KEYC_XTERM },
                    212:        { TTYC_KF15, KEYC_F3|KEYC_SHIFT|KEYC_XTERM },
                    213:        { TTYC_KF16, KEYC_F4|KEYC_SHIFT|KEYC_XTERM },
                    214:        { TTYC_KF17, KEYC_F5|KEYC_SHIFT|KEYC_XTERM },
                    215:        { TTYC_KF18, KEYC_F6|KEYC_SHIFT|KEYC_XTERM },
                    216:        { TTYC_KF19, KEYC_F7|KEYC_SHIFT|KEYC_XTERM },
                    217:        { TTYC_KF20, KEYC_F8|KEYC_SHIFT|KEYC_XTERM },
                    218:        { TTYC_KF21, KEYC_F9|KEYC_SHIFT|KEYC_XTERM },
                    219:        { TTYC_KF22, KEYC_F10|KEYC_SHIFT|KEYC_XTERM },
                    220:        { TTYC_KF23, KEYC_F11|KEYC_SHIFT|KEYC_XTERM },
                    221:        { TTYC_KF24, KEYC_F12|KEYC_SHIFT|KEYC_XTERM },
                    222:
                    223:        { TTYC_KF25, KEYC_F1|KEYC_CTRL|KEYC_XTERM },
                    224:        { TTYC_KF26, KEYC_F2|KEYC_CTRL|KEYC_XTERM },
                    225:        { TTYC_KF27, KEYC_F3|KEYC_CTRL|KEYC_XTERM },
                    226:        { TTYC_KF28, KEYC_F4|KEYC_CTRL|KEYC_XTERM },
                    227:        { TTYC_KF29, KEYC_F5|KEYC_CTRL|KEYC_XTERM },
                    228:        { TTYC_KF30, KEYC_F6|KEYC_CTRL|KEYC_XTERM },
                    229:        { TTYC_KF31, KEYC_F7|KEYC_CTRL|KEYC_XTERM },
                    230:        { TTYC_KF32, KEYC_F8|KEYC_CTRL|KEYC_XTERM },
                    231:        { TTYC_KF33, KEYC_F9|KEYC_CTRL|KEYC_XTERM },
                    232:        { TTYC_KF34, KEYC_F10|KEYC_CTRL|KEYC_XTERM },
                    233:        { TTYC_KF35, KEYC_F11|KEYC_CTRL|KEYC_XTERM },
                    234:        { TTYC_KF36, KEYC_F12|KEYC_CTRL|KEYC_XTERM },
                    235:
                    236:        { TTYC_KF37, KEYC_F1|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    237:        { TTYC_KF38, KEYC_F2|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    238:        { TTYC_KF39, KEYC_F3|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    239:        { TTYC_KF40, KEYC_F4|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    240:        { TTYC_KF41, KEYC_F5|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    241:        { TTYC_KF42, KEYC_F6|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    242:        { TTYC_KF43, KEYC_F7|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    243:        { TTYC_KF44, KEYC_F8|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    244:        { TTYC_KF45, KEYC_F9|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    245:        { TTYC_KF46, KEYC_F10|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    246:        { TTYC_KF47, KEYC_F11|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    247:        { TTYC_KF48, KEYC_F12|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    248:
                    249:        { TTYC_KF49, KEYC_F1|KEYC_ESCAPE|KEYC_XTERM },
                    250:        { TTYC_KF50, KEYC_F2|KEYC_ESCAPE|KEYC_XTERM },
                    251:        { TTYC_KF51, KEYC_F3|KEYC_ESCAPE|KEYC_XTERM },
                    252:        { TTYC_KF52, KEYC_F4|KEYC_ESCAPE|KEYC_XTERM },
                    253:        { TTYC_KF53, KEYC_F5|KEYC_ESCAPE|KEYC_XTERM },
                    254:        { TTYC_KF54, KEYC_F6|KEYC_ESCAPE|KEYC_XTERM },
                    255:        { TTYC_KF55, KEYC_F7|KEYC_ESCAPE|KEYC_XTERM },
                    256:        { TTYC_KF56, KEYC_F8|KEYC_ESCAPE|KEYC_XTERM },
                    257:        { TTYC_KF57, KEYC_F9|KEYC_ESCAPE|KEYC_XTERM },
                    258:        { TTYC_KF58, KEYC_F10|KEYC_ESCAPE|KEYC_XTERM },
                    259:        { TTYC_KF59, KEYC_F11|KEYC_ESCAPE|KEYC_XTERM },
                    260:        { TTYC_KF60, KEYC_F12|KEYC_ESCAPE|KEYC_XTERM },
                    261:
                    262:        { TTYC_KF61, KEYC_F1|KEYC_ESCAPE|KEYC_SHIFT|KEYC_XTERM },
                    263:        { TTYC_KF62, KEYC_F2|KEYC_ESCAPE|KEYC_SHIFT|KEYC_XTERM },
                    264:        { TTYC_KF63, KEYC_F3|KEYC_ESCAPE|KEYC_SHIFT|KEYC_XTERM },
1.70      nicm      265:
1.48      nicm      266:        { TTYC_KICH1, KEYC_IC },
                    267:        { TTYC_KDCH1, KEYC_DC },
                    268:        { TTYC_KHOME, KEYC_HOME },
                    269:        { TTYC_KEND, KEYC_END },
                    270:        { TTYC_KNP, KEYC_NPAGE },
                    271:        { TTYC_KPP, KEYC_PPAGE },
                    272:        { TTYC_KCBT, KEYC_BTAB },
1.29      nicm      273:
                    274:        /* Arrow keys from terminfo. */
1.48      nicm      275:        { TTYC_KCUU1, KEYC_UP },
                    276:        { TTYC_KCUD1, KEYC_DOWN },
                    277:        { TTYC_KCUB1, KEYC_LEFT },
                    278:        { TTYC_KCUF1, KEYC_RIGHT },
1.29      nicm      279:
1.103     nicm      280:        /* Key and modifier capabilities. */
1.97      nicm      281:        { TTYC_KDC2, KEYC_DC|KEYC_SHIFT|KEYC_XTERM },
                    282:        { TTYC_KDC3, KEYC_DC|KEYC_ESCAPE|KEYC_XTERM },
                    283:        { TTYC_KDC4, KEYC_DC|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    284:        { TTYC_KDC5, KEYC_DC|KEYC_CTRL|KEYC_XTERM },
                    285:        { TTYC_KDC6, KEYC_DC|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    286:        { TTYC_KDC7, KEYC_DC|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
1.102     nicm      287:        { TTYC_KIND, KEYC_DOWN|KEYC_SHIFT|KEYC_XTERM },
1.97      nicm      288:        { TTYC_KDN2, KEYC_DOWN|KEYC_SHIFT|KEYC_XTERM },
                    289:        { TTYC_KDN3, KEYC_DOWN|KEYC_ESCAPE|KEYC_XTERM },
                    290:        { TTYC_KDN4, KEYC_DOWN|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    291:        { TTYC_KDN5, KEYC_DOWN|KEYC_CTRL|KEYC_XTERM },
                    292:        { TTYC_KDN6, KEYC_DOWN|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    293:        { TTYC_KDN7, KEYC_DOWN|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
                    294:        { TTYC_KEND2, KEYC_END|KEYC_SHIFT|KEYC_XTERM },
                    295:        { TTYC_KEND3, KEYC_END|KEYC_ESCAPE|KEYC_XTERM },
                    296:        { TTYC_KEND4, KEYC_END|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    297:        { TTYC_KEND5, KEYC_END|KEYC_CTRL|KEYC_XTERM },
                    298:        { TTYC_KEND6, KEYC_END|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    299:        { TTYC_KEND7, KEYC_END|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
                    300:        { TTYC_KHOM2, KEYC_HOME|KEYC_SHIFT|KEYC_XTERM },
                    301:        { TTYC_KHOM3, KEYC_HOME|KEYC_ESCAPE|KEYC_XTERM },
                    302:        { TTYC_KHOM4, KEYC_HOME|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    303:        { TTYC_KHOM5, KEYC_HOME|KEYC_CTRL|KEYC_XTERM },
                    304:        { TTYC_KHOM6, KEYC_HOME|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    305:        { TTYC_KHOM7, KEYC_HOME|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
                    306:        { TTYC_KIC2, KEYC_IC|KEYC_SHIFT|KEYC_XTERM },
                    307:        { TTYC_KIC3, KEYC_IC|KEYC_ESCAPE|KEYC_XTERM },
                    308:        { TTYC_KIC4, KEYC_IC|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    309:        { TTYC_KIC5, KEYC_IC|KEYC_CTRL|KEYC_XTERM },
                    310:        { TTYC_KIC6, KEYC_IC|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    311:        { TTYC_KIC7, KEYC_IC|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
                    312:        { TTYC_KLFT2, KEYC_LEFT|KEYC_SHIFT|KEYC_XTERM },
                    313:        { TTYC_KLFT3, KEYC_LEFT|KEYC_ESCAPE|KEYC_XTERM },
                    314:        { TTYC_KLFT4, KEYC_LEFT|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    315:        { TTYC_KLFT5, KEYC_LEFT|KEYC_CTRL|KEYC_XTERM },
                    316:        { TTYC_KLFT6, KEYC_LEFT|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    317:        { TTYC_KLFT7, KEYC_LEFT|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
                    318:        { TTYC_KNXT2, KEYC_NPAGE|KEYC_SHIFT|KEYC_XTERM },
                    319:        { TTYC_KNXT3, KEYC_NPAGE|KEYC_ESCAPE|KEYC_XTERM },
                    320:        { TTYC_KNXT4, KEYC_NPAGE|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    321:        { TTYC_KNXT5, KEYC_NPAGE|KEYC_CTRL|KEYC_XTERM },
                    322:        { TTYC_KNXT6, KEYC_NPAGE|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    323:        { TTYC_KNXT7, KEYC_NPAGE|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
                    324:        { TTYC_KPRV2, KEYC_PPAGE|KEYC_SHIFT|KEYC_XTERM },
                    325:        { TTYC_KPRV3, KEYC_PPAGE|KEYC_ESCAPE|KEYC_XTERM },
                    326:        { TTYC_KPRV4, KEYC_PPAGE|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    327:        { TTYC_KPRV5, KEYC_PPAGE|KEYC_CTRL|KEYC_XTERM },
                    328:        { TTYC_KPRV6, KEYC_PPAGE|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    329:        { TTYC_KPRV7, KEYC_PPAGE|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
                    330:        { TTYC_KRIT2, KEYC_RIGHT|KEYC_SHIFT|KEYC_XTERM },
                    331:        { TTYC_KRIT3, KEYC_RIGHT|KEYC_ESCAPE|KEYC_XTERM },
                    332:        { TTYC_KRIT4, KEYC_RIGHT|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    333:        { TTYC_KRIT5, KEYC_RIGHT|KEYC_CTRL|KEYC_XTERM },
                    334:        { TTYC_KRIT6, KEYC_RIGHT|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    335:        { TTYC_KRIT7, KEYC_RIGHT|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
1.100     nicm      336:        { TTYC_KRI, KEYC_UP|KEYC_SHIFT|KEYC_XTERM },
1.97      nicm      337:        { TTYC_KUP2, KEYC_UP|KEYC_SHIFT|KEYC_XTERM },
                    338:        { TTYC_KUP3, KEYC_UP|KEYC_ESCAPE|KEYC_XTERM },
                    339:        { TTYC_KUP4, KEYC_UP|KEYC_SHIFT|KEYC_ESCAPE|KEYC_XTERM },
                    340:        { TTYC_KUP5, KEYC_UP|KEYC_CTRL|KEYC_XTERM },
                    341:        { TTYC_KUP6, KEYC_UP|KEYC_SHIFT|KEYC_CTRL|KEYC_XTERM },
                    342:        { TTYC_KUP7, KEYC_UP|KEYC_ESCAPE|KEYC_CTRL|KEYC_XTERM },
1.1       nicm      343: };
                    344:
1.48      nicm      345: /* Add key to tree. */
1.86      nicm      346: static void
1.76      nicm      347: tty_keys_add(struct tty *tty, const char *s, key_code key)
1.16      nicm      348: {
1.29      nicm      349:        struct tty_key  *tk;
                    350:        size_t           size;
                    351:        const char      *keystr;
1.1       nicm      352:
1.29      nicm      353:        keystr = key_string_lookup_key(key);
                    354:        if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) {
1.76      nicm      355:                log_debug("new key %s: 0x%llx (%s)", s, key, keystr);
1.16      nicm      356:                tty_keys_add1(&tty->key_tree, s, key);
1.29      nicm      357:        } else {
1.76      nicm      358:                log_debug("replacing key %s: 0x%llx (%s)", s, key, keystr);
1.29      nicm      359:                tk->key = key;
1.16      nicm      360:        }
1.1       nicm      361: }
                    362:
1.16      nicm      363: /* Add next node to the tree. */
1.86      nicm      364: static void
1.76      nicm      365: tty_keys_add1(struct tty_key **tkp, const char *s, key_code key)
1.1       nicm      366: {
1.16      nicm      367:        struct tty_key  *tk;
1.1       nicm      368:
1.16      nicm      369:        /* Allocate a tree entry if there isn't one already. */
                    370:        tk = *tkp;
                    371:        if (tk == NULL) {
                    372:                tk = *tkp = xcalloc(1, sizeof *tk);
                    373:                tk->ch = *s;
1.83      nicm      374:                tk->key = KEYC_UNKNOWN;
1.16      nicm      375:        }
                    376:
                    377:        /* Find the next entry. */
                    378:        if (*s == tk->ch) {
                    379:                /* Move forward in string. */
                    380:                s++;
                    381:
                    382:                /* If this is the end of the string, no more is necessary. */
                    383:                if (*s == '\0') {
                    384:                        tk->key = key;
                    385:                        return;
                    386:                }
                    387:
                    388:                /* Use the child tree for the next character. */
                    389:                tkp = &tk->next;
1.27      nicm      390:        } else {
1.16      nicm      391:                if (*s < tk->ch)
                    392:                        tkp = &tk->left;
                    393:                else if (*s > tk->ch)
                    394:                        tkp = &tk->right;
                    395:        }
                    396:
                    397:        /* And recurse to add it. */
                    398:        tty_keys_add1(tkp, s, key);
1.1       nicm      399: }
                    400:
1.16      nicm      401: /* Initialise a key tree from the table. */
1.1       nicm      402: void
1.48      nicm      403: tty_keys_build(struct tty *tty)
1.1       nicm      404: {
1.48      nicm      405:        const struct tty_default_key_raw        *tdkr;
                    406:        const struct tty_default_key_code       *tdkc;
1.107     nicm      407:        u_int                                    i;
1.109     nicm      408:        const char                              *s;
1.99      nicm      409:        struct options_entry                    *o;
1.107     nicm      410:        struct options_array_item               *a;
1.109     nicm      411:        union options_value                     *ov;
1.1       nicm      412:
1.48      nicm      413:        if (tty->key_tree != NULL)
1.71      nicm      414:                tty_keys_free(tty);
1.16      nicm      415:        tty->key_tree = NULL;
1.1       nicm      416:
1.48      nicm      417:        for (i = 0; i < nitems(tty_default_raw_keys); i++) {
                    418:                tdkr = &tty_default_raw_keys[i];
                    419:
                    420:                s = tdkr->string;
1.51      nicm      421:                if (*s != '\0')
                    422:                        tty_keys_add(tty, s, tdkr->key);
1.48      nicm      423:        }
                    424:        for (i = 0; i < nitems(tty_default_code_keys); i++) {
                    425:                tdkc = &tty_default_code_keys[i];
                    426:
                    427:                s = tty_term_string(tty->term, tdkc->code);
1.51      nicm      428:                if (*s != '\0')
                    429:                        tty_keys_add(tty, s, tdkc->key);
1.1       nicm      430:
1.99      nicm      431:        }
                    432:
                    433:        o = options_get(global_options, "user-keys");
1.107     nicm      434:        if (o != NULL) {
                    435:                a = options_array_first(o);
                    436:                while (a != NULL) {
1.112     nicm      437:                        i = options_array_item_index(a);
1.109     nicm      438:                        ov = options_array_item_value(a);
1.110     nicm      439:                        tty_keys_add(tty, ov->string, KEYC_USER + i);
1.107     nicm      440:                        a = options_array_next(a);
1.99      nicm      441:                }
1.1       nicm      442:        }
                    443: }
                    444:
1.16      nicm      445: /* Free the entire key tree. */
1.1       nicm      446: void
                    447: tty_keys_free(struct tty *tty)
                    448: {
1.16      nicm      449:        tty_keys_free1(tty->key_tree);
                    450: }
1.1       nicm      451:
1.16      nicm      452: /* Free a single key. */
1.86      nicm      453: static void
1.16      nicm      454: tty_keys_free1(struct tty_key *tk)
                    455: {
                    456:        if (tk->next != NULL)
                    457:                tty_keys_free1(tk->next);
                    458:        if (tk->left != NULL)
                    459:                tty_keys_free1(tk->left);
                    460:        if (tk->right != NULL)
                    461:                tty_keys_free1(tk->right);
1.42      nicm      462:        free(tk);
1.1       nicm      463: }
                    464:
1.16      nicm      465: /* Lookup a key in the tree. */
1.86      nicm      466: static struct tty_key *
1.16      nicm      467: tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
1.1       nicm      468: {
1.16      nicm      469:        *size = 0;
                    470:        return (tty_keys_find1(tty->key_tree, buf, len, size));
                    471: }
1.1       nicm      472:
1.16      nicm      473: /* Find the next node. */
1.86      nicm      474: static struct tty_key *
1.16      nicm      475: tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
                    476: {
1.106     nicm      477:        /* If no data, no match. */
                    478:        if (len == 0)
                    479:                return (NULL);
                    480:
1.16      nicm      481:        /* If the node is NULL, this is the end of the tree. No match. */
                    482:        if (tk == NULL)
1.1       nicm      483:                return (NULL);
                    484:
1.16      nicm      485:        /* Pick the next in the sequence. */
                    486:        if (tk->ch == *buf) {
                    487:                /* Move forward in the string. */
                    488:                buf++; len--;
                    489:                (*size)++;
1.1       nicm      490:
1.16      nicm      491:                /* At the end of the string, return the current node. */
1.83      nicm      492:                if (len == 0 || (tk->next == NULL && tk->key != KEYC_UNKNOWN))
1.16      nicm      493:                        return (tk);
1.1       nicm      494:
1.16      nicm      495:                /* Move into the next tree for the following character. */
                    496:                tk = tk->next;
                    497:        } else {
                    498:                if (*buf < tk->ch)
                    499:                        tk = tk->left;
                    500:                else if (*buf > tk->ch)
                    501:                        tk = tk->right;
1.1       nicm      502:        }
                    503:
1.16      nicm      504:        /* Move to the next in the tree. */
                    505:        return (tty_keys_find1(tk, buf, len, size));
1.1       nicm      506: }
                    507:
1.86      nicm      508: /* Look up part of the next key. */
                    509: static int
                    510: tty_keys_next1(struct tty *tty, const char *buf, size_t len, key_code *key,
1.87      nicm      511:     size_t *size, int expired)
1.86      nicm      512: {
1.94      nicm      513:        struct client           *c = tty->client;
1.86      nicm      514:        struct tty_key          *tk, *tk1;
                    515:        struct utf8_data         ud;
                    516:        enum utf8_state          more;
                    517:        u_int                    i;
                    518:        wchar_t                  wc;
1.97      nicm      519:        int                      n;
1.86      nicm      520:
1.94      nicm      521:        log_debug("%s: next key is %zu (%.*s) (expired=%d)", c->name, len,
                    522:            (int)len, buf, expired);
1.86      nicm      523:
                    524:        /* Is this a known key? */
                    525:        tk = tty_keys_find(tty, buf, len, size);
1.87      nicm      526:        if (tk != NULL && tk->key != KEYC_UNKNOWN) {
1.86      nicm      527:                tk1 = tk;
                    528:                do
1.94      nicm      529:                        log_debug("%s: keys in list: %#llx", c->name, tk1->key);
1.86      nicm      530:                while ((tk1 = tk1->next) != NULL);
1.87      nicm      531:                if (tk->next != NULL && !expired)
                    532:                        return (1);
1.86      nicm      533:                *key = tk->key;
1.87      nicm      534:                return (0);
1.86      nicm      535:        }
                    536:
1.97      nicm      537:        /* Is this an an xterm(1) key? */
                    538:        n = xterm_keys_find(buf, len, size, key);
                    539:        if (n == 0)
                    540:                return (0);
                    541:        if (n == 1 && !expired)
                    542:                return (1);
                    543:
1.86      nicm      544:        /* Is this valid UTF-8? */
                    545:        more = utf8_open(&ud, (u_char)*buf);
                    546:        if (more == UTF8_MORE) {
                    547:                *size = ud.size;
1.87      nicm      548:                if (len < ud.size) {
                    549:                        if (!expired)
                    550:                                return (1);
                    551:                        return (-1);
                    552:                }
1.86      nicm      553:                for (i = 1; i < ud.size; i++)
                    554:                        more = utf8_append(&ud, (u_char)buf[i]);
                    555:                if (more != UTF8_DONE)
1.87      nicm      556:                        return (-1);
1.86      nicm      557:
                    558:                if (utf8_combine(&ud, &wc) != UTF8_DONE)
1.87      nicm      559:                        return (-1);
1.86      nicm      560:                *key = wc;
                    561:
1.94      nicm      562:                log_debug("%s: UTF-8 key %.*s %#llx", c->name, (int)ud.size,
                    563:                    buf, *key);
1.86      nicm      564:                return (0);
                    565:        }
                    566:
                    567:        return (-1);
                    568: }
                    569:
1.111     nicm      570: /* Process at least one key in the buffer. Return 0 if no keys present. */
                    571: int
1.14      nicm      572: tty_keys_next(struct tty *tty)
1.1       nicm      573: {
1.111     nicm      574:        struct client           *c = tty->client;
                    575:        struct timeval           tv;
                    576:        const char              *buf;
                    577:        size_t                   len, size;
                    578:        cc_t                     bspace;
                    579:        int                      delay, expired = 0, n;
                    580:        key_code                 key;
                    581:        struct mouse_event       m = { 0 };
                    582:        struct key_event        *event;
                    583:
                    584:        gettimeofday(&tv, NULL);
1.1       nicm      585:
1.51      nicm      586:        /* Get key buffer. */
1.93      nicm      587:        buf = EVBUFFER_DATA(tty->in);
                    588:        len = EVBUFFER_LENGTH(tty->in);
1.1       nicm      589:        if (len == 0)
1.14      nicm      590:                return (0);
1.94      nicm      591:        log_debug("%s: keys are %zu (%.*s)", c->name, len, (int)len, buf);
1.1       nicm      592:
1.104     nicm      593:        /* Is this a clipboard response? */
                    594:        switch (tty_keys_clipboard(tty, buf, len, &size)) {
                    595:        case 0:         /* yes */
                    596:                key = KEYC_UNKNOWN;
                    597:                goto complete_key;
                    598:        case -1:        /* no, or not valid */
                    599:                break;
                    600:        case 1:         /* partial */
                    601:                goto partial_key;
                    602:        }
                    603:
1.91      nicm      604:        /* Is this a device attributes response? */
                    605:        switch (tty_keys_device_attributes(tty, buf, len, &size)) {
                    606:        case 0:         /* yes */
                    607:                key = KEYC_UNKNOWN;
                    608:                goto complete_key;
                    609:        case -1:        /* no, or not valid */
                    610:                break;
                    611:        case 1:         /* partial */
                    612:                goto partial_key;
                    613:        }
                    614:
1.128     nicm      615:        /* Is this an extended device attributes response? */
                    616:        switch (tty_keys_extended_device_attributes(tty, buf, len, &size)) {
1.119     nicm      617:        case 0:         /* yes */
                    618:                key = KEYC_UNKNOWN;
                    619:                goto complete_key;
                    620:        case -1:        /* no, or not valid */
                    621:                break;
                    622:        case 1:         /* partial */
                    623:                goto partial_key;
                    624:        }
                    625:
1.26      nicm      626:        /* Is this a mouse key press? */
1.111     nicm      627:        switch (tty_keys_mouse(tty, buf, len, &size, &m)) {
1.22      nicm      628:        case 0:         /* yes */
                    629:                key = KEYC_MOUSE;
1.51      nicm      630:                goto complete_key;
1.22      nicm      631:        case -1:        /* no, or not valid */
1.24      nicm      632:                break;
1.67      nicm      633:        case -2:        /* yes, but we don't care. */
1.73      nicm      634:                key = KEYC_MOUSE;
1.67      nicm      635:                goto discard_key;
1.22      nicm      636:        case 1:         /* partial */
                    637:                goto partial_key;
1.11      nicm      638:        }
                    639:
1.86      nicm      640: first_key:
1.101     nicm      641:        /* Try to lookup complete key. */
                    642:        n = tty_keys_next1(tty, buf, len, &key, &size, expired);
                    643:        if (n == 0)     /* found */
                    644:                goto complete_key;
                    645:        if (n == 1)
                    646:                goto partial_key;
                    647:
                    648:        /*
                    649:         * If not a complete key, look for key with an escape prefix (meta
                    650:         * modifier).
                    651:         */
1.105     nicm      652:        if (*buf == '\033' && len > 1) {
1.87      nicm      653:                /* Look for a key without the escape. */
                    654:                n = tty_keys_next1(tty, buf + 1, len - 1, &key, &size, expired);
                    655:                if (n == 0) {   /* found */
1.97      nicm      656:                        if (key & KEYC_XTERM) {
                    657:                                /*
                    658:                                 * We want the escape key as well as the xterm
                    659:                                 * key, because the xterm sequence implicitly
                    660:                                 * includes the escape (so if we see
                    661:                                 * \033\033[1;3D we know it is an Escape
                    662:                                 * followed by M-Left, not just M-Left).
                    663:                                 */
                    664:                                key = '\033';
                    665:                                size = 1;
                    666:                                goto complete_key;
                    667:                        }
1.87      nicm      668:                        key |= KEYC_ESCAPE;
                    669:                        size++;
                    670:                        goto complete_key;
                    671:                }
                    672:                if (n == 1)     /* partial */
1.62      nicm      673:                        goto partial_key;
1.89      nicm      674:        }
1.1       nicm      675:
1.89      nicm      676:        /*
                    677:         * At this point, we know the key is not partial (with or without
                    678:         * escape). So pass it through even if the timer has not expired.
                    679:         */
                    680:        if (*buf == '\033' && len >= 2) {
                    681:                key = (u_char)buf[1] | KEYC_ESCAPE;
                    682:                size = 2;
                    683:        } else {
                    684:                key = (u_char)buf[0];
                    685:                size = 1;
1.76      nicm      686:        }
1.51      nicm      687:        goto complete_key;
                    688:
                    689: partial_key:
1.94      nicm      690:        log_debug("%s: partial key %.*s", c->name, (int)len, buf);
1.14      nicm      691:
1.51      nicm      692:        /* If timer is going, check for expiration. */
                    693:        if (tty->flags & TTY_TIMER) {
                    694:                if (evtimer_initialized(&tty->key_timer) &&
1.59      nicm      695:                    !evtimer_pending(&tty->key_timer, NULL)) {
                    696:                        expired = 1;
1.51      nicm      697:                        goto first_key;
1.59      nicm      698:                }
1.39      nicm      699:                return (0);
1.51      nicm      700:        }
1.39      nicm      701:
1.51      nicm      702:        /* Get the time period. */
1.75      nicm      703:        delay = options_get_number(global_options, "escape-time");
1.28      nicm      704:        tv.tv_sec = delay / 1000;
                    705:        tv.tv_usec = (delay % 1000) * 1000L;
1.27      nicm      706:
1.51      nicm      707:        /* Start the timer. */
1.36      nicm      708:        if (event_initialized(&tty->key_timer))
                    709:                evtimer_del(&tty->key_timer);
1.14      nicm      710:        evtimer_set(&tty->key_timer, tty_keys_callback, tty);
                    711:        evtimer_add(&tty->key_timer, &tv);
1.27      nicm      712:
1.51      nicm      713:        tty->flags |= TTY_TIMER;
1.14      nicm      714:        return (0);
1.1       nicm      715:
1.51      nicm      716: complete_key:
1.94      nicm      717:        log_debug("%s: complete key %.*s %#llx", c->name, (int)size, buf, key);
1.88      nicm      718:
                    719:        /*
                    720:         * Check for backspace key using termios VERASE - the terminfo
                    721:         * kbs entry is extremely unreliable, so cannot be safely
                    722:         * used. termios should have a better idea.
                    723:         */
                    724:        bspace = tty->tio.c_cc[VERASE];
                    725:        if (bspace != _POSIX_VDISABLE && (key & KEYC_MASK_KEY) == bspace)
                    726:                key = (key & KEYC_MASK_MOD) | KEYC_BSPACE;
1.16      nicm      727:
1.51      nicm      728:        /* Remove data from buffer. */
1.93      nicm      729:        evbuffer_drain(tty->in, size);
1.16      nicm      730:
1.51      nicm      731:        /* Remove key timer. */
1.36      nicm      732:        if (event_initialized(&tty->key_timer))
                    733:                evtimer_del(&tty->key_timer);
1.51      nicm      734:        tty->flags &= ~TTY_TIMER;
1.56      nicm      735:
                    736:        /* Check for focus events. */
                    737:        if (key == KEYC_FOCUS_OUT) {
                    738:                tty->client->flags &= ~CLIENT_FOCUSED;
                    739:                return (1);
                    740:        } else if (key == KEYC_FOCUS_IN) {
                    741:                tty->client->flags |= CLIENT_FOCUSED;
                    742:                return (1);
                    743:        }
1.16      nicm      744:
1.51      nicm      745:        /* Fire the key. */
1.111     nicm      746:        if (key != KEYC_UNKNOWN) {
                    747:                event = xmalloc(sizeof *event);
                    748:                event->key = key;
                    749:                memcpy(&event->m, &m, sizeof event->m);
1.113     nicm      750:                if (!server_client_handle_key(c, event))
                    751:                        free(event);
1.111     nicm      752:        }
1.16      nicm      753:
1.14      nicm      754:        return (1);
1.67      nicm      755:
                    756: discard_key:
1.94      nicm      757:        log_debug("%s: discard key %.*s %#llx", c->name, (int)size, buf, key);
1.67      nicm      758:
                    759:        /* Remove data from buffer. */
1.93      nicm      760:        evbuffer_drain(tty->in, size);
1.67      nicm      761:
                    762:        return (1);
1.14      nicm      763: }
                    764:
1.16      nicm      765: /* Key timer callback. */
1.86      nicm      766: static void
1.81      nicm      767: tty_keys_callback(__unused int fd, __unused short events, void *data)
1.14      nicm      768: {
                    769:        struct tty      *tty = data;
                    770:
1.51      nicm      771:        if (tty->flags & TTY_TIMER) {
                    772:                while (tty_keys_next(tty))
                    773:                        ;
                    774:        }
1.1       nicm      775: }
                    776:
1.27      nicm      777: /*
1.22      nicm      778:  * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
                    779:  * (probably a mouse sequence but need more data).
                    780:  */
1.86      nicm      781: static int
1.111     nicm      782: tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size,
                    783:     struct mouse_event *m)
1.1       nicm      784: {
1.111     nicm      785:        struct client   *c = tty->client;
                    786:        u_int            i, x, y, b, sgr_b;
                    787:        u_char           sgr_type, ch;
1.33      nicm      788:
1.1       nicm      789:        /*
1.33      nicm      790:         * Standard mouse sequences are \033[M followed by three characters
1.55      nicm      791:         * indicating button, X and Y, all based at 32 with 1,1 top-left.
1.33      nicm      792:         *
                    793:         * UTF-8 mouse sequences are similar but the three are expressed as
                    794:         * UTF-8 characters.
1.55      nicm      795:         *
                    796:         * SGR extended mouse sequences are \033[< followed by three numbers in
                    797:         * decimal and separated by semicolons indicating button, X and Y. A
                    798:         * trailing 'M' is click or scroll and trailing 'm' release. All are
                    799:         * based at 0 with 1,1 top-left.
1.1       nicm      800:         */
                    801:
1.22      nicm      802:        *size = 0;
1.72      nicm      803:        x = y = b = sgr_b = 0;
                    804:        sgr_type = ' ';
1.22      nicm      805:
1.55      nicm      806:        /* First two bytes are always \033[. */
1.22      nicm      807:        if (buf[0] != '\033')
                    808:                return (-1);
                    809:        if (len == 1)
                    810:                return (1);
                    811:        if (buf[1] != '[')
                    812:                return (-1);
                    813:        if (len == 2)
                    814:                return (1);
                    815:
1.55      nicm      816:        /*
1.82      nicm      817:         * Third byte is M in old standard (and UTF-8 extension which we do not
                    818:         * support), < in SGR extension.
1.55      nicm      819:         */
                    820:        if (buf[2] == 'M') {
                    821:                /* Read the three inputs. */
                    822:                *size = 3;
                    823:                for (i = 0; i < 3; i++) {
                    824:                        if (len <= *size)
                    825:                                return (1);
1.94      nicm      826:                        ch = (u_char)buf[(*size)++];
1.55      nicm      827:                        if (i == 0)
1.94      nicm      828:                                b = ch;
1.55      nicm      829:                        else if (i == 1)
1.94      nicm      830:                                x = ch;
1.55      nicm      831:                        else
1.94      nicm      832:                                y = ch;
1.33      nicm      833:                }
1.94      nicm      834:                log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf);
1.16      nicm      835:
1.55      nicm      836:                /* Check and return the mouse input. */
1.60      nicm      837:                if (b < 32)
1.55      nicm      838:                        return (-1);
                    839:                b -= 32;
1.60      nicm      840:                if (x >= 33)
                    841:                        x -= 33;
                    842:                else
                    843:                        x = 256 - x;
                    844:                if (y >= 33)
                    845:                        y -= 33;
                    846:                else
                    847:                        y = 256 - y;
1.55      nicm      848:        } else if (buf[2] == '<') {
                    849:                /* Read the three inputs. */
                    850:                *size = 3;
                    851:                while (1) {
                    852:                        if (len <= *size)
                    853:                                return (1);
1.94      nicm      854:                        ch = (u_char)buf[(*size)++];
                    855:                        if (ch == ';')
1.55      nicm      856:                                break;
1.94      nicm      857:                        if (ch < '0' || ch > '9')
1.55      nicm      858:                                return (-1);
1.94      nicm      859:                        sgr_b = 10 * sgr_b + (ch - '0');
1.55      nicm      860:                }
                    861:                while (1) {
                    862:                        if (len <= *size)
                    863:                                return (1);
1.94      nicm      864:                        ch = (u_char)buf[(*size)++];
                    865:                        if (ch == ';')
1.55      nicm      866:                                break;
1.94      nicm      867:                        if (ch < '0' || ch > '9')
1.55      nicm      868:                                return (-1);
1.94      nicm      869:                        x = 10 * x + (ch - '0');
1.55      nicm      870:                }
                    871:                while (1) {
                    872:                        if (len <= *size)
                    873:                                return (1);
1.94      nicm      874:                        ch = (u_char)buf[(*size)++];
                    875:                        if (ch == 'M' || ch == 'm')
1.55      nicm      876:                                break;
1.94      nicm      877:                        if (ch < '0' || ch > '9')
1.55      nicm      878:                                return (-1);
1.94      nicm      879:                        y = 10 * y + (ch - '0');
1.55      nicm      880:                }
1.94      nicm      881:                log_debug("%s: mouse input (SGR): %.*s", c->name, (int)*size,
                    882:                    buf);
1.1       nicm      883:
1.55      nicm      884:                /* Check and return the mouse input. */
                    885:                if (x < 1 || y < 1)
                    886:                        return (-1);
                    887:                x--;
                    888:                y--;
1.72      nicm      889:                b = sgr_b;
                    890:
                    891:                /* Type is M for press, m for release. */
1.94      nicm      892:                sgr_type = ch;
1.72      nicm      893:                if (sgr_type == 'm')
                    894:                        b |= 3;
1.67      nicm      895:
                    896:                /*
                    897:                 * Some terminals (like PuTTY 0.63) mistakenly send
                    898:                 * button-release events for scroll-wheel button-press event.
                    899:                 * Discard it before it reaches any program running inside
                    900:                 * tmux.
                    901:                 */
1.72      nicm      902:                if (sgr_type == 'm' && (sgr_b & 64))
1.67      nicm      903:                    return (-2);
1.55      nicm      904:        } else
1.22      nicm      905:                return (-1);
1.45      nicm      906:
1.72      nicm      907:        /* Fill mouse event. */
1.111     nicm      908:        m->lx = tty->mouse_last_x;
1.61      nicm      909:        m->x = x;
1.111     nicm      910:        m->ly = tty->mouse_last_y;
1.61      nicm      911:        m->y = y;
1.114     nicm      912:        m->lb = tty->mouse_last_b;
1.72      nicm      913:        m->b = b;
                    914:        m->sgr_type = sgr_type;
                    915:        m->sgr_b = sgr_b;
1.111     nicm      916:
                    917:        /* Update last mouse state. */
                    918:        tty->mouse_last_x = x;
                    919:        tty->mouse_last_y = y;
1.114     nicm      920:        tty->mouse_last_b = b;
1.104     nicm      921:
                    922:        return (0);
                    923: }
                    924:
                    925: /*
                    926:  * Handle OSC 52 clipboard input. Returns 0 for success, -1 for failure, 1 for
                    927:  * partial.
                    928:  */
                    929: static int
                    930: tty_keys_clipboard(__unused struct tty *tty, const char *buf, size_t len,
                    931:     size_t *size)
                    932: {
                    933:        size_t   end, terminator, needed;
                    934:        char    *copy, *out;
                    935:        int      outlen;
                    936:
                    937:        *size = 0;
                    938:
1.128     nicm      939:        /* First five bytes are always \033]52;. */
1.104     nicm      940:        if (buf[0] != '\033')
                    941:                return (-1);
                    942:        if (len == 1)
                    943:                return (1);
                    944:        if (buf[1] != ']')
                    945:                return (-1);
                    946:        if (len == 2)
                    947:                return (1);
                    948:        if (buf[2] != '5')
                    949:                return (-1);
                    950:        if (len == 3)
                    951:                return (1);
                    952:        if (buf[3] != '2')
                    953:                return (-1);
                    954:        if (len == 4)
                    955:                return (1);
                    956:        if (buf[4] != ';')
                    957:                return (-1);
                    958:        if (len == 5)
                    959:                return (1);
                    960:
                    961:        /* Find the terminator if any. */
                    962:        for (end = 5; end < len; end++) {
                    963:                if (buf[end] == '\007') {
                    964:                        terminator = 1;
                    965:                        break;
                    966:                }
                    967:                if (end > 5 && buf[end - 1] == '\033' && buf[end] == '\\') {
                    968:                        terminator = 2;
                    969:                        break;
                    970:                }
                    971:        }
                    972:        if (end == len)
                    973:                return (1);
                    974:        *size = end + terminator;
                    975:
                    976:        /* Skip the initial part. */
                    977:        buf += 5;
                    978:        end -= 5;
                    979:
                    980:        /* Get the second argument. */
                    981:        while (end != 0 && *buf != ';') {
                    982:                buf++;
                    983:                end--;
                    984:        }
                    985:        if (end == 0 || end == 1)
                    986:                return (0);
                    987:        buf++;
                    988:        end--;
                    989:
                    990:        /* It has to be a string so copy it. */
                    991:        copy = xmalloc(end + 1);
                    992:        memcpy(copy, buf, end);
                    993:        copy[end] = '\0';
                    994:
                    995:        /* Convert from base64. */
                    996:        needed = (end / 4) * 3;
                    997:        out = xmalloc(needed);
                    998:        if ((outlen = b64_pton(copy, out, len)) == -1) {
                    999:                free(out);
                   1000:                free(copy);
                   1001:                return (0);
                   1002:        }
                   1003:        free(copy);
                   1004:
                   1005:        /* Create a new paste buffer. */
                   1006:        log_debug("%s: %.*s", __func__, outlen, out);
1.108     nicm     1007:        paste_add(NULL, out, outlen);
1.91      nicm     1008:
                   1009:        return (0);
                   1010: }
                   1011:
                   1012: /*
1.124     nicm     1013:  * Handle secondary device attributes input. Returns 0 for success, -1 for
                   1014:  * failure, 1 for partial.
1.91      nicm     1015:  */
                   1016: static int
                   1017: tty_keys_device_attributes(struct tty *tty, const char *buf, size_t len,
                   1018:     size_t *size)
                   1019: {
1.118     nicm     1020:        struct client   *c = tty->client;
                   1021:        u_int            i, n = 0;
                   1022:        char             tmp[64], *endptr, p[32] = { 0 }, *cp, *next;
1.91      nicm     1023:
                   1024:        *size = 0;
1.120     nicm     1025:        if (tty->flags & TTY_HAVEDA)
                   1026:                return (-1);
1.91      nicm     1027:
                   1028:        /* First three bytes are always \033[?. */
                   1029:        if (buf[0] != '\033')
                   1030:                return (-1);
                   1031:        if (len == 1)
                   1032:                return (1);
                   1033:        if (buf[1] != '[')
                   1034:                return (-1);
                   1035:        if (len == 2)
                   1036:                return (1);
1.124     nicm     1037:        if (buf[2] != '>')
1.91      nicm     1038:                return (-1);
                   1039:        if (len == 3)
                   1040:                return (1);
                   1041:
                   1042:        /* Copy the rest up to a 'c'. */
1.128     nicm     1043:        for (i = 0; i < (sizeof tmp) - 1; i++) {
1.91      nicm     1044:                if (3 + i == len)
                   1045:                        return (1);
1.128     nicm     1046:                if (buf[3 + i] == 'c')
                   1047:                        break;
1.91      nicm     1048:                tmp[i] = buf[3 + i];
                   1049:        }
                   1050:        if (i == (sizeof tmp) - 1)
                   1051:                return (-1);
                   1052:        tmp[i] = '\0';
                   1053:        *size = 4 + i;
                   1054:
1.124     nicm     1055:        /* Convert all arguments to numbers. */
1.116     nicm     1056:        cp = tmp;
                   1057:        while ((next = strsep(&cp, ";")) != NULL) {
                   1058:                p[n] = strtoul(next, &endptr, 10);
1.117     nicm     1059:                if (*endptr != '\0')
1.116     nicm     1060:                        p[n] = 0;
                   1061:                n++;
                   1062:        }
1.91      nicm     1063:
1.126     nicm     1064:        /* Add terminal features. */
1.116     nicm     1065:        switch (p[0]) {
1.124     nicm     1066:        case 41: /* VT420 */
1.126     nicm     1067:                tty_add_features(&c->term_features,
                   1068:                    "margins,"
                   1069:                    "rectfill",
                   1070:                    ",");
1.91      nicm     1071:                break;
1.124     nicm     1072:        case 'M': /* mintty */
1.126     nicm     1073:                tty_add_features(&c->term_features,
                   1074:                    "256,"
                   1075:                    "RGB,"
                   1076:                    "title",
                   1077:                    ",");
1.124     nicm     1078:                break;
1.126     nicm     1079:        case 'T': /* tmux */
                   1080:                tty_add_features(&c->term_features,
                   1081:                    "256,"
                   1082:                    "RGB,"
                   1083:                    "ccolour,"
                   1084:                    "cstyle,"
                   1085:                    "overline,"
                   1086:                    "title,"
                   1087:                    "usstyle",
                   1088:                    ",");
1.124     nicm     1089:                break;
                   1090:        case 'U': /* rxvt-unicode */
1.126     nicm     1091:                tty_add_features(&c->term_features,
                   1092:                    "256,"
                   1093:                    "title",
                   1094:                    ",");
1.124     nicm     1095:                break;
1.91      nicm     1096:        }
1.124     nicm     1097:        log_debug("%s: received secondary DA %.*s", c->name, (int)*size, buf);
1.120     nicm     1098:
1.126     nicm     1099:        tty_update_features(tty);
1.120     nicm     1100:        tty->flags |= TTY_HAVEDA;
                   1101:
1.119     nicm     1102:        return (0);
                   1103: }
                   1104:
                   1105: /*
1.128     nicm     1106:  * Handle extended device attributes input. Returns 0 for success, -1 for
                   1107:  * failure, 1 for partial.
1.119     nicm     1108:  */
                   1109: static int
1.128     nicm     1110: tty_keys_extended_device_attributes(struct tty *tty, const char *buf,
                   1111:     size_t len, size_t *size)
1.119     nicm     1112: {
                   1113:        struct client   *c = tty->client;
                   1114:        u_int            i;
                   1115:        char             tmp[64];
                   1116:
                   1117:        *size = 0;
1.128     nicm     1118:        if (tty->flags & TTY_HAVEXDA)
1.120     nicm     1119:                return (-1);
1.119     nicm     1120:
1.128     nicm     1121:        /* First four bytes are always \033P>|. */
1.119     nicm     1122:        if (buf[0] != '\033')
                   1123:                return (-1);
                   1124:        if (len == 1)
                   1125:                return (1);
1.128     nicm     1126:        if (buf[1] != 'P')
1.119     nicm     1127:                return (-1);
                   1128:        if (len == 2)
                   1129:                return (1);
1.128     nicm     1130:        if (buf[2] != '>')
1.120     nicm     1131:                return (-1);
                   1132:        if (len == 3)
                   1133:                return (1);
1.128     nicm     1134:        if (buf[3] != '|')
                   1135:                return (-1);
                   1136:        if (len == 4)
                   1137:                return (1);
1.119     nicm     1138:
1.128     nicm     1139:        /* Copy the rest up to a '\033\\'. */
                   1140:        for (i = 0; i < (sizeof tmp) - 1; i++) {
                   1141:                if (4 + i == len)
1.119     nicm     1142:                        return (1);
1.128     nicm     1143:                if (buf[4 + i - 1] == '\033' && buf[4 + i] == '\\')
                   1144:                        break;
                   1145:                tmp[i] = buf[4 + i];
1.119     nicm     1146:        }
                   1147:        if (i == (sizeof tmp) - 1)
                   1148:                return (-1);
1.128     nicm     1149:        tmp[i - 1] = '\0';
                   1150:        *size = 5 + i;
1.119     nicm     1151:
1.126     nicm     1152:        /* Add terminal features. */
1.129   ! nicm     1153:        if (strncmp(tmp, "iTerm2 ", 7) == 0) {
1.126     nicm     1154:                tty_add_features(&c->term_features,
                   1155:                    "256,"
                   1156:                    "RGB,"
                   1157:                    "clipboard,"
                   1158:                    "cstyle,"
                   1159:                    "margins,"
                   1160:                    "sync,"
1.127     nicm     1161:                    "title",
1.126     nicm     1162:                    ",");
1.128     nicm     1163:        } else if (strncmp(tmp, "tmux ", 5) == 0) {
1.126     nicm     1164:                tty_add_features(&c->term_features,
                   1165:                    "256,"
                   1166:                    "RGB,"
                   1167:                    "ccolour,"
                   1168:                    "cstyle,"
                   1169:                    "overline,"
                   1170:                    "title,"
                   1171:                    "usstyle",
                   1172:                    ",");
                   1173:        }
1.128     nicm     1174:        log_debug("%s: received extended DA %.*s", c->name, (int)*size, buf);
1.120     nicm     1175:
1.126     nicm     1176:        tty_update_features(tty);
1.128     nicm     1177:        tty->flags |= TTY_HAVEXDA;
1.120     nicm     1178:
1.22      nicm     1179:        return (0);
1.1       nicm     1180: }