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

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