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

1.31    ! nicm        1: /* $OpenBSD: input-keys.c,v 1.30 2012/11/27 20:22:12 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      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.1       nicm       34: struct input_key_ent {
                     35:        int              key;
                     36:        const char      *data;
                     37:
                     38:        int              flags;
                     39: #define INPUTKEY_KEYPAD 0x1    /* keypad key */
                     40: #define INPUTKEY_CURSOR 0x2    /* cursor key */
                     41: };
                     42:
1.20      nicm       43: const struct input_key_ent input_keys[] = {
1.3       nicm       44:        /* Backspace key. */
1.7       nicm       45:        { KEYC_BSPACE,          "\177",         0 },
1.3       nicm       46:
1.1       nicm       47:        /* Function keys. */
1.9       nicm       48:        { KEYC_F1,              "\033OP",       0 },
                     49:        { KEYC_F2,              "\033OQ",       0 },
                     50:        { KEYC_F3,              "\033OR",       0 },
                     51:        { KEYC_F4,              "\033OS",       0 },
                     52:        { KEYC_F5,              "\033[15~",     0 },
                     53:        { KEYC_F6,              "\033[17~",     0 },
                     54:        { KEYC_F7,              "\033[18~",     0 },
                     55:        { KEYC_F8,              "\033[19~",     0 },
                     56:        { KEYC_F9,              "\033[20~",     0 },
                     57:        { KEYC_F10,             "\033[21~",     0 },
                     58:        { KEYC_F11,             "\033[23~",     0 },
                     59:        { KEYC_F12,             "\033[24~",     0 },
                     60:        { KEYC_F13,             "\033[25~",     0 },
                     61:        { KEYC_F14,             "\033[26~",     0 },
                     62:        { KEYC_F15,             "\033[28~",     0 },
                     63:        { KEYC_F16,             "\033[29~",     0 },
                     64:        { KEYC_F17,             "\033[31~",     0 },
                     65:        { KEYC_F18,             "\033[32~",     0 },
                     66:        { KEYC_F19,             "\033[33~",     0 },
                     67:        { KEYC_F20,             "\033[34~",     0 },
                     68:        { KEYC_IC,              "\033[2~",      0 },
                     69:        { KEYC_DC,              "\033[3~",      0 },
                     70:        { KEYC_HOME,            "\033[1~",      0 },
                     71:        { KEYC_END,             "\033[4~",      0 },
                     72:        { KEYC_NPAGE,           "\033[6~",      0 },
                     73:        { KEYC_PPAGE,           "\033[5~",      0 },
                     74:        { KEYC_BTAB,            "\033[Z",       0 },
1.7       nicm       75:
1.13      nicm       76:        /*
                     77:         * Arrow keys. Cursor versions must come first. The codes are toggled
                     78:         * between CSI and SS3 versions when ctrl is pressed.
                     79:         */
1.10      nicm       80:        { KEYC_UP|KEYC_CTRL,    "\033[A",       INPUTKEY_CURSOR },
                     81:        { KEYC_DOWN|KEYC_CTRL,  "\033[B",       INPUTKEY_CURSOR },
                     82:        { KEYC_RIGHT|KEYC_CTRL, "\033[C",       INPUTKEY_CURSOR },
                     83:        { KEYC_LEFT|KEYC_CTRL,  "\033[D",       INPUTKEY_CURSOR },
1.18      nicm       84:
1.7       nicm       85:        { KEYC_UP,              "\033OA",       INPUTKEY_CURSOR },
                     86:        { KEYC_DOWN,            "\033OB",       INPUTKEY_CURSOR },
                     87:        { KEYC_RIGHT,           "\033OC",       INPUTKEY_CURSOR },
                     88:        { KEYC_LEFT,            "\033OD",       INPUTKEY_CURSOR },
                     89:
1.9       nicm       90:        { KEYC_UP|KEYC_CTRL,    "\033OA",       0 },
                     91:        { KEYC_DOWN|KEYC_CTRL,  "\033OB",       0 },
                     92:        { KEYC_RIGHT|KEYC_CTRL, "\033OC",       0 },
                     93:        { KEYC_LEFT|KEYC_CTRL,  "\033OD",       0 },
                     94:
1.7       nicm       95:        { KEYC_UP,              "\033[A",       0 },
                     96:        { KEYC_DOWN,            "\033[B",       0 },
                     97:        { KEYC_RIGHT,           "\033[C",       0 },
                     98:        { KEYC_LEFT,            "\033[D",       0 },
                     99:
1.8       nicm      100:        /* Keypad keys. Keypad versions must come first. */
1.17      nicm      101:        { KEYC_KP_SLASH,        "\033Oo",       INPUTKEY_KEYPAD },
                    102:        { KEYC_KP_STAR,         "\033Oj",       INPUTKEY_KEYPAD },
                    103:        { KEYC_KP_MINUS,        "\033Om",       INPUTKEY_KEYPAD },
                    104:        { KEYC_KP_SEVEN,        "\033Ow",       INPUTKEY_KEYPAD },
                    105:        { KEYC_KP_EIGHT,        "\033Ox",       INPUTKEY_KEYPAD },
                    106:        { KEYC_KP_NINE,         "\033Oy",       INPUTKEY_KEYPAD },
                    107:        { KEYC_KP_PLUS,         "\033Ok",       INPUTKEY_KEYPAD },
                    108:        { KEYC_KP_FOUR,         "\033Ot",       INPUTKEY_KEYPAD },
                    109:        { KEYC_KP_FIVE,         "\033Ou",       INPUTKEY_KEYPAD },
                    110:        { KEYC_KP_SIX,          "\033Ov",       INPUTKEY_KEYPAD },
                    111:        { KEYC_KP_ONE,          "\033Oq",       INPUTKEY_KEYPAD },
                    112:        { KEYC_KP_TWO,          "\033Or",       INPUTKEY_KEYPAD },
                    113:        { KEYC_KP_THREE,        "\033Os",       INPUTKEY_KEYPAD },
                    114:        { KEYC_KP_ENTER,        "\033OM",       INPUTKEY_KEYPAD },
                    115:        { KEYC_KP_ZERO,         "\033Op",       INPUTKEY_KEYPAD },
                    116:        { KEYC_KP_PERIOD,       "\033On",       INPUTKEY_KEYPAD },
                    117:
                    118:        { KEYC_KP_SLASH,        "/",            0 },
                    119:        { KEYC_KP_STAR,         "*",            0 },
                    120:        { KEYC_KP_MINUS,        "-",            0 },
                    121:        { KEYC_KP_SEVEN,        "7",            0 },
                    122:        { KEYC_KP_EIGHT,        "8",            0 },
                    123:        { KEYC_KP_NINE,         "9",            0 },
                    124:        { KEYC_KP_PLUS,         "+",            0 },
1.18      nicm      125:        { KEYC_KP_FOUR,         "4",            0 },
1.17      nicm      126:        { KEYC_KP_FIVE,         "5",            0 },
                    127:        { KEYC_KP_SIX,          "6",            0 },
                    128:        { KEYC_KP_ONE,          "1",            0 },
                    129:        { KEYC_KP_TWO,          "2",            0 },
                    130:        { KEYC_KP_THREE,        "3",            0 },
                    131:        { KEYC_KP_ENTER,        "\n",           0 },
                    132:        { KEYC_KP_ZERO,         "0",            0 },
                    133:        { KEYC_KP_PERIOD,       ".",            0 },
1.1       nicm      134: };
                    135:
1.8       nicm      136: /* Translate a key code into an output key sequence. */
1.1       nicm      137: void
                    138: input_key(struct window_pane *wp, int key)
                    139: {
1.20      nicm      140:        const struct input_key_ent     *ike;
                    141:        u_int                           i;
                    142:        size_t                          dlen;
                    143:        char                           *out;
                    144:        u_char                          ch;
1.1       nicm      145:
                    146:        log_debug2("writing key 0x%x", key);
                    147:
1.8       nicm      148:        /*
                    149:         * If this is a normal 7-bit key, just send it, with a leading escape
                    150:         * if necessary.
                    151:         */
1.2       nicm      152:        if (key != KEYC_NONE && (key & ~KEYC_ESCAPE) < 0x100) {
                    153:                if (key & KEYC_ESCAPE)
1.15      nicm      154:                        bufferevent_write(wp->event, "\033", 1);
                    155:                ch = key & ~KEYC_ESCAPE;
1.14      nicm      156:                bufferevent_write(wp->event, &ch, 1);
1.1       nicm      157:                return;
1.12      nicm      158:        }
                    159:
1.18      nicm      160:        /*
1.12      nicm      161:         * Then try to look this up as an xterm key, if the flag to output them
                    162:         * is set.
                    163:         */
                    164:        if (options_get_number(&wp->window->options, "xterm-keys")) {
                    165:                if ((out = xterm_keys_lookup(key)) != NULL) {
1.14      nicm      166:                        bufferevent_write(wp->event, out, strlen(out));
1.26      nicm      167:                        free(out);
1.12      nicm      168:                        return;
                    169:                }
1.1       nicm      170:        }
                    171:
1.8       nicm      172:        /* Otherwise look the key up in the table. */
1.1       nicm      173:        for (i = 0; i < nitems(input_keys); i++) {
                    174:                ike = &input_keys[i];
                    175:
                    176:                if ((ike->flags & INPUTKEY_KEYPAD) &&
                    177:                    !(wp->screen->mode & MODE_KKEYPAD))
                    178:                        continue;
                    179:                if ((ike->flags & INPUTKEY_CURSOR) &&
                    180:                    !(wp->screen->mode & MODE_KCURSOR))
                    181:                        continue;
                    182:
1.2       nicm      183:                if ((key & KEYC_ESCAPE) && (ike->key | KEYC_ESCAPE) == key)
1.1       nicm      184:                        break;
                    185:                if (ike->key == key)
                    186:                        break;
                    187:        }
                    188:        if (i == nitems(input_keys)) {
                    189:                log_debug2("key 0x%x missing", key);
                    190:                return;
                    191:        }
                    192:        dlen = strlen(ike->data);
                    193:        log_debug2("found key 0x%x: \"%s\"", key, ike->data);
                    194:
1.9       nicm      195:        /* Prefix a \033 for escape. */
1.2       nicm      196:        if (key & KEYC_ESCAPE)
1.14      nicm      197:                bufferevent_write(wp->event, "\033", 1);
                    198:        bufferevent_write(wp->event, ike->data, dlen);
1.1       nicm      199: }
                    200:
1.8       nicm      201: /* Translate mouse and output. */
1.1       nicm      202: void
1.27      nicm      203: input_mouse(struct window_pane *wp, struct session *s, struct mouse_event *m)
1.1       nicm      204: {
1.31    ! nicm      205:        char                     buf[40];
1.30      nicm      206:        size_t                   len;
                    207:        struct paste_buffer     *pb;
1.14      nicm      208:
1.19      nicm      209:        if (wp->screen->mode & ALL_MOUSE_MODES) {
1.31    ! nicm      210:                /*
        !           211:                 * Use the SGR (1006) extension only if the application
        !           212:                 * requested it and the underlying terminal also sent the event
        !           213:                 * in this format (this is because an old style mouse release
        !           214:                 * event cannot be converted into the new SGR format, since the
        !           215:                 * released button is unknown). Otherwise pretend that tmux
        !           216:                 * doesn't speak this extension, and fall back to the UTF-8
        !           217:                 * (1005) extension if the application requested, or to the
        !           218:                 * legacy format.
        !           219:                 */
        !           220:                if (m->sgr && (wp->screen->mode & MODE_MOUSE_SGR)) {
        !           221:                        len = xsnprintf(buf, sizeof buf, "\033[<%d;%d;%d%c",
        !           222:                            m->sgr_xb, m->x + 1, m->y + 1, m->sgr_rel ? 'm' : 'M');
        !           223:                } else if (wp->screen->mode & MODE_MOUSE_UTF8) {
1.21      nicm      224:                        len = xsnprintf(buf, sizeof buf, "\033[M");
1.27      nicm      225:                        len += utf8_split2(m->xb + 32, &buf[len]);
1.21      nicm      226:                        len += utf8_split2(m->x + 33, &buf[len]);
                    227:                        len += utf8_split2(m->y + 33, &buf[len]);
                    228:                } else {
1.27      nicm      229:                        if (m->xb > 223 || m->x >= 222 || m->y > 222)
1.21      nicm      230:                                return;
                    231:                        len = xsnprintf(buf, sizeof buf, "\033[M");
1.27      nicm      232:                        buf[len++] = m->xb + 32;
1.21      nicm      233:                        buf[len++] = m->x + 33;
                    234:                        buf[len++] = m->y + 33;
                    235:                }
                    236:                bufferevent_write(wp->event, buf, len);
1.24      nicm      237:                return;
                    238:        }
                    239:
1.30      nicm      240:        if (m->button == 1 && (m->event & MOUSE_EVENT_CLICK) &&
                    241:            options_get_number(&wp->window->options, "mode-mouse") == 1) {
                    242:                pb = paste_get_top(&global_buffers);
                    243:                if (pb != NULL) {
                    244:                        paste_send_pane(pb, wp, "\r",
                    245:                            wp->screen->mode & MODE_BRACKETPASTE);
                    246:                }
                    247:        } else if ((m->xb & 3) != 1 &&
1.29      nicm      248:            options_get_number(&wp->window->options, "mode-mouse") == 1) {
1.24      nicm      249:                if (window_pane_set_mode(wp, &window_copy_mode) == 0) {
1.22      nicm      250:                        window_copy_init_from_pane(wp);
                    251:                        if (wp->mode->mouse != NULL)
1.27      nicm      252:                                wp->mode->mouse(wp, s, m);
1.22      nicm      253:                }
1.1       nicm      254:        }
                    255: }