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

Annotation of src/usr.bin/tmux/input-keys.c, Revision 1.71

1.71    ! nicm        1: /* $OpenBSD: input-keys.c,v 1.70 2020/04/01 09:05:27 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.53      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:
                     21: #include <stdint.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
1.8       nicm       27: /*
                     28:  * This file is rather misleadingly named, it contains the code which takes a
                     29:  * key code and translates it into something suitable to be sent to the
                     30:  * application running in a pane (similar to input.c does in the other
                     31:  * direction with output).
                     32:  */
                     33:
1.56      nicm       34: static void     input_key_mouse(struct window_pane *, struct mouse_event *);
1.42      nicm       35:
1.1       nicm       36: struct input_key_ent {
1.46      nicm       37:        key_code         key;
1.1       nicm       38:        const char      *data;
                     39:
                     40:        int              flags;
                     41: #define INPUTKEY_KEYPAD 0x1    /* keypad key */
                     42: #define INPUTKEY_CURSOR 0x2    /* cursor key */
                     43: };
                     44:
1.56      nicm       45: static const struct input_key_ent input_keys[] = {
1.61      nicm       46:        /* Paste keys. */
                     47:        { KEYC_PASTE_START,     "\033[200~",    0 },
                     48:        { KEYC_PASTE_END,       "\033[201~",    0 },
1.3       nicm       49:
1.1       nicm       50:        /* Function keys. */
1.9       nicm       51:        { KEYC_F1,              "\033OP",       0 },
                     52:        { KEYC_F2,              "\033OQ",       0 },
                     53:        { KEYC_F3,              "\033OR",       0 },
                     54:        { KEYC_F4,              "\033OS",       0 },
                     55:        { KEYC_F5,              "\033[15~",     0 },
                     56:        { KEYC_F6,              "\033[17~",     0 },
                     57:        { KEYC_F7,              "\033[18~",     0 },
                     58:        { KEYC_F8,              "\033[19~",     0 },
                     59:        { KEYC_F9,              "\033[20~",     0 },
                     60:        { KEYC_F10,             "\033[21~",     0 },
                     61:        { KEYC_F11,             "\033[23~",     0 },
                     62:        { KEYC_F12,             "\033[24~",     0 },
1.38      nicm       63:        { KEYC_F1|KEYC_SHIFT,   "\033[25~",     0 },
                     64:        { KEYC_F2|KEYC_SHIFT,   "\033[26~",     0 },
                     65:        { KEYC_F3|KEYC_SHIFT,   "\033[28~",     0 },
                     66:        { KEYC_F4|KEYC_SHIFT,   "\033[29~",     0 },
                     67:        { KEYC_F5|KEYC_SHIFT,   "\033[31~",     0 },
                     68:        { KEYC_F6|KEYC_SHIFT,   "\033[32~",     0 },
                     69:        { KEYC_F7|KEYC_SHIFT,   "\033[33~",     0 },
                     70:        { KEYC_F8|KEYC_SHIFT,   "\033[34~",     0 },
1.9       nicm       71:        { KEYC_IC,              "\033[2~",      0 },
                     72:        { KEYC_DC,              "\033[3~",      0 },
                     73:        { KEYC_HOME,            "\033[1~",      0 },
                     74:        { KEYC_END,             "\033[4~",      0 },
                     75:        { KEYC_NPAGE,           "\033[6~",      0 },
                     76:        { KEYC_PPAGE,           "\033[5~",      0 },
                     77:        { KEYC_BTAB,            "\033[Z",       0 },
1.7       nicm       78:
1.13      nicm       79:        /*
                     80:         * Arrow keys. Cursor versions must come first. The codes are toggled
                     81:         * between CSI and SS3 versions when ctrl is pressed.
                     82:         */
1.10      nicm       83:        { KEYC_UP|KEYC_CTRL,    "\033[A",       INPUTKEY_CURSOR },
                     84:        { KEYC_DOWN|KEYC_CTRL,  "\033[B",       INPUTKEY_CURSOR },
                     85:        { KEYC_RIGHT|KEYC_CTRL, "\033[C",       INPUTKEY_CURSOR },
                     86:        { KEYC_LEFT|KEYC_CTRL,  "\033[D",       INPUTKEY_CURSOR },
1.18      nicm       87:
1.7       nicm       88:        { KEYC_UP,              "\033OA",       INPUTKEY_CURSOR },
                     89:        { KEYC_DOWN,            "\033OB",       INPUTKEY_CURSOR },
                     90:        { KEYC_RIGHT,           "\033OC",       INPUTKEY_CURSOR },
                     91:        { KEYC_LEFT,            "\033OD",       INPUTKEY_CURSOR },
                     92:
1.9       nicm       93:        { KEYC_UP|KEYC_CTRL,    "\033OA",       0 },
                     94:        { KEYC_DOWN|KEYC_CTRL,  "\033OB",       0 },
                     95:        { KEYC_RIGHT|KEYC_CTRL, "\033OC",       0 },
                     96:        { KEYC_LEFT|KEYC_CTRL,  "\033OD",       0 },
                     97:
1.7       nicm       98:        { KEYC_UP,              "\033[A",       0 },
                     99:        { KEYC_DOWN,            "\033[B",       0 },
                    100:        { KEYC_RIGHT,           "\033[C",       0 },
                    101:        { KEYC_LEFT,            "\033[D",       0 },
                    102:
1.8       nicm      103:        /* Keypad keys. Keypad versions must come first. */
1.17      nicm      104:        { KEYC_KP_SLASH,        "\033Oo",       INPUTKEY_KEYPAD },
                    105:        { KEYC_KP_STAR,         "\033Oj",       INPUTKEY_KEYPAD },
                    106:        { KEYC_KP_MINUS,        "\033Om",       INPUTKEY_KEYPAD },
                    107:        { KEYC_KP_SEVEN,        "\033Ow",       INPUTKEY_KEYPAD },
                    108:        { KEYC_KP_EIGHT,        "\033Ox",       INPUTKEY_KEYPAD },
                    109:        { KEYC_KP_NINE,         "\033Oy",       INPUTKEY_KEYPAD },
                    110:        { KEYC_KP_PLUS,         "\033Ok",       INPUTKEY_KEYPAD },
                    111:        { KEYC_KP_FOUR,         "\033Ot",       INPUTKEY_KEYPAD },
                    112:        { KEYC_KP_FIVE,         "\033Ou",       INPUTKEY_KEYPAD },
                    113:        { KEYC_KP_SIX,          "\033Ov",       INPUTKEY_KEYPAD },
                    114:        { KEYC_KP_ONE,          "\033Oq",       INPUTKEY_KEYPAD },
                    115:        { KEYC_KP_TWO,          "\033Or",       INPUTKEY_KEYPAD },
                    116:        { KEYC_KP_THREE,        "\033Os",       INPUTKEY_KEYPAD },
                    117:        { KEYC_KP_ENTER,        "\033OM",       INPUTKEY_KEYPAD },
                    118:        { KEYC_KP_ZERO,         "\033Op",       INPUTKEY_KEYPAD },
                    119:        { KEYC_KP_PERIOD,       "\033On",       INPUTKEY_KEYPAD },
                    120:
                    121:        { KEYC_KP_SLASH,        "/",            0 },
                    122:        { KEYC_KP_STAR,         "*",            0 },
                    123:        { KEYC_KP_MINUS,        "-",            0 },
                    124:        { KEYC_KP_SEVEN,        "7",            0 },
                    125:        { KEYC_KP_EIGHT,        "8",            0 },
                    126:        { KEYC_KP_NINE,         "9",            0 },
                    127:        { KEYC_KP_PLUS,         "+",            0 },
1.18      nicm      128:        { KEYC_KP_FOUR,         "4",            0 },
1.17      nicm      129:        { KEYC_KP_FIVE,         "5",            0 },
                    130:        { KEYC_KP_SIX,          "6",            0 },
                    131:        { KEYC_KP_ONE,          "1",            0 },
                    132:        { KEYC_KP_TWO,          "2",            0 },
                    133:        { KEYC_KP_THREE,        "3",            0 },
                    134:        { KEYC_KP_ENTER,        "\n",           0 },
                    135:        { KEYC_KP_ZERO,         "0",            0 },
                    136:        { KEYC_KP_PERIOD,       ".",            0 },
1.1       nicm      137: };
                    138:
1.54      nicm      139: /* Split a character into two UTF-8 bytes. */
                    140: static size_t
                    141: input_split2(u_int c, u_char *dst)
                    142: {
                    143:        if (c > 0x7f) {
                    144:                dst[0] = (c >> 6) | 0xc0;
                    145:                dst[1] = (c & 0x3f) | 0x80;
                    146:                return (2);
                    147:        }
                    148:        dst[0] = c;
                    149:        return (1);
                    150: }
                    151:
1.69      nicm      152: /* Translate a key code into an output key sequence for a pane. */
                    153: int
                    154: input_key_pane(struct window_pane *wp, key_code key, struct mouse_event *m)
                    155: {
                    156:        log_debug("writing key 0x%llx (%s) to %%%u", key,
                    157:            key_string_lookup_key(key), wp->id);
                    158:
                    159:        if (KEYC_IS_MOUSE(key)) {
                    160:                if (m != NULL && m->wp != -1 && (u_int)m->wp == wp->id)
                    161:                        input_key_mouse(wp, m);
                    162:                return (0);
                    163:        }
                    164:        return (input_key(wp, wp->screen, wp->event, key));
                    165: }
                    166:
1.8       nicm      167: /* Translate a key code into an output key sequence. */
1.67      nicm      168: int
1.69      nicm      169: input_key(struct window_pane *wp, struct screen *s, struct bufferevent *bev,
                    170:     key_code key)
1.1       nicm      171: {
1.46      nicm      172:        const struct input_key_ent      *ike;
                    173:        u_int                            i;
                    174:        size_t                           dlen;
                    175:        char                            *out;
1.66      nicm      176:        key_code                         justkey, newkey;
1.47      nicm      177:        struct utf8_data                 ud;
1.1       nicm      178:
1.69      nicm      179:        /* Mouse keys need a pane. */
                    180:        if (KEYC_IS_MOUSE(key))
1.67      nicm      181:                return (0);
1.64      nicm      182:
                    183:        /* Literal keys go as themselves (can't be more than eight bits). */
                    184:        if (key & KEYC_LITERAL) {
                    185:                ud.data[0] = (u_char)key;
1.69      nicm      186:                bufferevent_write(bev, &ud.data[0], 1);
1.67      nicm      187:                return (0);
1.65      nicm      188:        }
                    189:
                    190:        /* Is this backspace? */
                    191:        if ((key & KEYC_MASK_KEY) == KEYC_BSPACE) {
1.66      nicm      192:                newkey = options_get_number(global_options, "backspace");
                    193:                if (newkey >= 0x7f)
                    194:                        newkey = '\177';
                    195:                key = newkey|(key & KEYC_MASK_MOD);
1.42      nicm      196:        }
1.1       nicm      197:
1.8       nicm      198:        /*
                    199:         * If this is a normal 7-bit key, just send it, with a leading escape
1.46      nicm      200:         * if necessary. If it is a UTF-8 key, split it and send it.
1.8       nicm      201:         */
1.62      nicm      202:        justkey = (key & ~(KEYC_XTERM|KEYC_ESCAPE));
1.52      nicm      203:        if (justkey <= 0x7f) {
1.2       nicm      204:                if (key & KEYC_ESCAPE)
1.69      nicm      205:                        bufferevent_write(bev, "\033", 1);
1.47      nicm      206:                ud.data[0] = justkey;
1.69      nicm      207:                bufferevent_write(bev, &ud.data[0], 1);
1.67      nicm      208:                return (0);
1.46      nicm      209:        }
1.52      nicm      210:        if (justkey > 0x7f && justkey < KEYC_BASE) {
1.48      nicm      211:                if (utf8_split(justkey, &ud) != UTF8_DONE)
1.67      nicm      212:                        return (-1);
1.46      nicm      213:                if (key & KEYC_ESCAPE)
1.69      nicm      214:                        bufferevent_write(bev, "\033", 1);
                    215:                bufferevent_write(bev, ud.data, ud.size);
1.67      nicm      216:                return (0);
1.12      nicm      217:        }
                    218:
1.18      nicm      219:        /*
1.12      nicm      220:         * Then try to look this up as an xterm key, if the flag to output them
                    221:         * is set.
                    222:         */
1.69      nicm      223:        if (wp == NULL || options_get_number(wp->window->options, "xterm-keys")) {
1.58      nicm      224:                if ((out = xterm_keys_lookup(key)) != NULL) {
1.69      nicm      225:                        bufferevent_write(bev, out, strlen(out));
1.26      nicm      226:                        free(out);
1.67      nicm      227:                        return (0);
1.12      nicm      228:                }
1.1       nicm      229:        }
1.60      nicm      230:        key &= ~KEYC_XTERM;
1.1       nicm      231:
1.8       nicm      232:        /* Otherwise look the key up in the table. */
1.1       nicm      233:        for (i = 0; i < nitems(input_keys); i++) {
                    234:                ike = &input_keys[i];
                    235:
1.69      nicm      236:                if ((ike->flags & INPUTKEY_KEYPAD) && (~s->mode & MODE_KKEYPAD))
1.1       nicm      237:                        continue;
1.69      nicm      238:                if ((ike->flags & INPUTKEY_CURSOR) && (~s->mode & MODE_KCURSOR))
1.1       nicm      239:                        continue;
                    240:
1.2       nicm      241:                if ((key & KEYC_ESCAPE) && (ike->key | KEYC_ESCAPE) == key)
1.1       nicm      242:                        break;
                    243:                if (ike->key == key)
                    244:                        break;
                    245:        }
                    246:        if (i == nitems(input_keys)) {
1.46      nicm      247:                log_debug("key 0x%llx missing", key);
1.67      nicm      248:                return (-1);
1.1       nicm      249:        }
                    250:        dlen = strlen(ike->data);
1.46      nicm      251:        log_debug("found key 0x%llx: \"%s\"", key, ike->data);
1.1       nicm      252:
1.9       nicm      253:        /* Prefix a \033 for escape. */
1.2       nicm      254:        if (key & KEYC_ESCAPE)
1.69      nicm      255:                bufferevent_write(bev, "\033", 1);
                    256:        bufferevent_write(bev, ike->data, dlen);
1.67      nicm      257:        return (0);
1.1       nicm      258: }
                    259:
1.70      nicm      260: /* Get mouse event string. */
                    261: int
                    262: input_key_get_mouse(struct screen *s, struct mouse_event *m, u_int x, u_int y,
                    263:     const char **rbuf, size_t *rlen)
1.1       nicm      264: {
1.70      nicm      265:        static char      buf[40];
1.59      nicm      266:        size_t           len;
1.42      nicm      267:
1.70      nicm      268:        *rbuf = NULL;
                    269:        *rlen = 0;
1.24      nicm      270:
1.59      nicm      271:        /* If this pane is not in button or all mode, discard motion events. */
1.68      nicm      272:        if (MOUSE_DRAG(m->b) && (s->mode & MOTION_MOUSE_MODES) == 0)
1.71    ! nicm      273:                return (0);
        !           274:        if ((s->mode & ALL_MOUSE_MODES) == 0)
1.70      nicm      275:                return (0);
1.59      nicm      276:
                    277:        /*
                    278:         * If this event is a release event and not in all mode, discard it.
                    279:         * In SGR mode we can tell absolutely because a release is normally
                    280:         * shown by the last character. Without SGR, we check if the last
                    281:         * buttons was also a release.
                    282:         */
                    283:        if (m->sgr_type != ' ') {
                    284:                if (MOUSE_DRAG(m->sgr_b) &&
                    285:                    MOUSE_BUTTONS(m->sgr_b) == 3 &&
1.68      nicm      286:                    (~s->mode & MODE_MOUSE_ALL))
1.70      nicm      287:                        return (0);
1.59      nicm      288:        } else {
                    289:                if (MOUSE_DRAG(m->b) &&
                    290:                    MOUSE_BUTTONS(m->b) == 3 &&
                    291:                    MOUSE_BUTTONS(m->lb) == 3 &&
1.68      nicm      292:                    (~s->mode & MODE_MOUSE_ALL))
1.70      nicm      293:                        return (0);
1.59      nicm      294:        }
1.42      nicm      295:
                    296:        /*
                    297:         * Use the SGR (1006) extension only if the application requested it
                    298:         * and the underlying terminal also sent the event in this format (this
                    299:         * is because an old style mouse release event cannot be converted into
                    300:         * the new SGR format, since the released button is unknown). Otherwise
                    301:         * pretend that tmux doesn't speak this extension, and fall back to the
1.51      nicm      302:         * UTF-8 (1005) extension if the application requested, or to the
1.42      nicm      303:         * legacy format.
                    304:         */
1.59      nicm      305:        if (m->sgr_type != ' ' && (s->mode & MODE_MOUSE_SGR)) {
1.42      nicm      306:                len = xsnprintf(buf, sizeof buf, "\033[<%u;%u;%u%c",
                    307:                    m->sgr_b, x + 1, y + 1, m->sgr_type);
1.59      nicm      308:        } else if (s->mode & MODE_MOUSE_UTF8) {
1.55      nicm      309:                if (m->b > 0x7ff - 32 || x > 0x7ff - 33 || y > 0x7ff - 33)
1.70      nicm      310:                        return (0);
1.51      nicm      311:                len = xsnprintf(buf, sizeof buf, "\033[M");
1.54      nicm      312:                len += input_split2(m->b + 32, &buf[len]);
                    313:                len += input_split2(x + 33, &buf[len]);
                    314:                len += input_split2(y + 33, &buf[len]);
1.42      nicm      315:        } else {
                    316:                if (m->b > 223)
1.70      nicm      317:                        return (0);
1.42      nicm      318:                len = xsnprintf(buf, sizeof buf, "\033[M");
                    319:                buf[len++] = m->b + 32;
                    320:                buf[len++] = x + 33;
                    321:                buf[len++] = y + 33;
1.1       nicm      322:        }
1.70      nicm      323:
                    324:        *rbuf = buf;
                    325:        *rlen = len;
                    326:        return (1);
                    327: }
                    328:
                    329: /* Translate mouse and output. */
                    330: static void
                    331: input_key_mouse(struct window_pane *wp, struct mouse_event *m)
                    332: {
                    333:        struct screen   *s = wp->screen;
                    334:        u_int            x, y;
                    335:        const char      *buf;
                    336:        size_t           len;
                    337:
                    338:        /* Ignore events if no mouse mode or the pane is not visible. */
                    339:        if (m->ignore || (s->mode & ALL_MOUSE_MODES) == 0)
                    340:                return;
                    341:        if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
                    342:                return;
                    343:        if (!window_pane_visible(wp))
                    344:                return;
                    345:        if (!input_key_get_mouse(s, m, x, y, &buf, &len))
                    346:                return;
1.44      nicm      347:        log_debug("writing mouse %.*s to %%%u", (int)len, buf, wp->id);
1.42      nicm      348:        bufferevent_write(wp->event, buf, len);
1.1       nicm      349: }