2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2018-10-18 08:04:14 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2020-05-16 16:44:54 +00:00
|
|
|
#include <ctype.h>
|
2012-03-15 10:36:00 +00:00
|
|
|
#include <limits.h>
|
2018-10-18 08:04:14 +00:00
|
|
|
#include <resolv.h>
|
2012-03-15 10:36:00 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
2009-07-26 21:42:08 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-10-26 13:58:02 +00:00
|
|
|
/*
|
2013-03-21 16:52:02 +00:00
|
|
|
* Handle keys input from the outside terminal. tty_default_*_keys[] are a base
|
|
|
|
* table of supported keys which are looked up in terminfo(5) and translated
|
|
|
|
* into a ternary tree.
|
2009-10-26 13:58:02 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-26 22:19:22 +00:00
|
|
|
static void tty_keys_add1(struct tty_key **, const char *, key_code);
|
|
|
|
static void tty_keys_add(struct tty *, const char *, key_code);
|
|
|
|
static void tty_keys_free1(struct tty_key *);
|
|
|
|
static struct tty_key *tty_keys_find1(struct tty_key *, const char *, size_t,
|
2015-11-12 11:05:34 +00:00
|
|
|
size_t *);
|
2016-04-26 22:19:22 +00:00
|
|
|
static struct tty_key *tty_keys_find(struct tty *, const char *, size_t,
|
|
|
|
size_t *);
|
|
|
|
static int tty_keys_next1(struct tty *, const char *, size_t, key_code *,
|
2016-04-27 16:46:21 +00:00
|
|
|
size_t *, int);
|
2016-04-26 22:19:22 +00:00
|
|
|
static void tty_keys_callback(int, short, void *);
|
2020-05-16 16:44:54 +00:00
|
|
|
static int tty_keys_extended_key(struct tty *, const char *, size_t,
|
|
|
|
size_t *, key_code *);
|
2019-05-03 14:51:30 +00:00
|
|
|
static int tty_keys_mouse(struct tty *, const char *, size_t, size_t *,
|
|
|
|
struct mouse_event *);
|
2018-10-18 08:04:14 +00:00
|
|
|
static int tty_keys_clipboard(struct tty *, const char *, size_t,
|
|
|
|
size_t *);
|
2016-11-15 14:02:32 +00:00
|
|
|
static int tty_keys_device_attributes(struct tty *, const char *, size_t,
|
|
|
|
size_t *);
|
2022-11-11 08:37:55 +00:00
|
|
|
static int tty_keys_device_attributes2(struct tty *, const char *, size_t,
|
|
|
|
size_t *);
|
2020-05-16 14:16:25 +00:00
|
|
|
static int tty_keys_extended_device_attributes(struct tty *, const char *,
|
2020-01-12 22:00:20 +00:00
|
|
|
size_t, size_t *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2021-08-13 07:37:58 +00:00
|
|
|
/* A key tree entry. */
|
|
|
|
struct tty_key {
|
|
|
|
char ch;
|
|
|
|
key_code key;
|
|
|
|
|
|
|
|
struct tty_key *left;
|
|
|
|
struct tty_key *right;
|
|
|
|
|
|
|
|
struct tty_key *next;
|
|
|
|
};
|
|
|
|
|
2013-03-21 16:52:02 +00:00
|
|
|
/* Default raw keys. */
|
|
|
|
struct tty_default_key_raw {
|
2009-06-01 22:58:49 +00:00
|
|
|
const char *string;
|
2021-04-07 07:30:02 +00:00
|
|
|
key_code key;
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
2016-10-10 21:29:23 +00:00
|
|
|
static const struct tty_default_key_raw tty_default_raw_keys[] = {
|
2020-04-17 09:06:10 +00:00
|
|
|
/* Application escape. */
|
|
|
|
{ "\033O[", '\033' },
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
/*
|
2009-10-26 13:41:46 +00:00
|
|
|
* Numeric keypad. Just use the vt100 escape sequences here and always
|
|
|
|
* put the terminal into keypad_xmit mode. Translation of numbers
|
|
|
|
* mode/applications mode is done in input-keys.c.
|
2009-06-01 22:58:49 +00:00
|
|
|
*/
|
2020-05-16 16:44:54 +00:00
|
|
|
{ "\033Oo", KEYC_KP_SLASH|KEYC_KEYPAD },
|
|
|
|
{ "\033Oj", KEYC_KP_STAR|KEYC_KEYPAD },
|
|
|
|
{ "\033Om", KEYC_KP_MINUS|KEYC_KEYPAD },
|
|
|
|
{ "\033Ow", KEYC_KP_SEVEN|KEYC_KEYPAD },
|
|
|
|
{ "\033Ox", KEYC_KP_EIGHT|KEYC_KEYPAD },
|
|
|
|
{ "\033Oy", KEYC_KP_NINE|KEYC_KEYPAD },
|
|
|
|
{ "\033Ok", KEYC_KP_PLUS|KEYC_KEYPAD },
|
|
|
|
{ "\033Ot", KEYC_KP_FOUR|KEYC_KEYPAD },
|
|
|
|
{ "\033Ou", KEYC_KP_FIVE|KEYC_KEYPAD },
|
|
|
|
{ "\033Ov", KEYC_KP_SIX|KEYC_KEYPAD },
|
|
|
|
{ "\033Oq", KEYC_KP_ONE|KEYC_KEYPAD },
|
|
|
|
{ "\033Or", KEYC_KP_TWO|KEYC_KEYPAD },
|
|
|
|
{ "\033Os", KEYC_KP_THREE|KEYC_KEYPAD },
|
|
|
|
{ "\033OM", KEYC_KP_ENTER|KEYC_KEYPAD },
|
|
|
|
{ "\033Op", KEYC_KP_ZERO|KEYC_KEYPAD },
|
|
|
|
{ "\033On", KEYC_KP_PERIOD|KEYC_KEYPAD },
|
2009-10-26 16:00:51 +00:00
|
|
|
|
2009-12-17 17:39:56 +00:00
|
|
|
/* Arrow keys. */
|
2020-05-16 16:44:54 +00:00
|
|
|
{ "\033OA", KEYC_UP|KEYC_CURSOR },
|
|
|
|
{ "\033OB", KEYC_DOWN|KEYC_CURSOR },
|
|
|
|
{ "\033OC", KEYC_RIGHT|KEYC_CURSOR },
|
|
|
|
{ "\033OD", KEYC_LEFT|KEYC_CURSOR },
|
2009-12-17 17:39:56 +00:00
|
|
|
|
2020-05-16 16:44:54 +00:00
|
|
|
{ "\033[A", KEYC_UP|KEYC_CURSOR },
|
|
|
|
{ "\033[B", KEYC_DOWN|KEYC_CURSOR },
|
|
|
|
{ "\033[C", KEYC_RIGHT|KEYC_CURSOR },
|
|
|
|
{ "\033[D", KEYC_LEFT|KEYC_CURSOR },
|
2009-11-12 08:01:02 +00:00
|
|
|
|
2020-09-23 14:57:33 +00:00
|
|
|
/*
|
|
|
|
* Meta arrow keys. These do not get the IMPLIED_META flag so they
|
|
|
|
* don't match the xterm-style meta keys in the output tree - Escape+Up
|
|
|
|
* should stay as Escape+Up and not become M-Up.
|
|
|
|
*/
|
|
|
|
{ "\033\033OA", KEYC_UP|KEYC_CURSOR|KEYC_META },
|
|
|
|
{ "\033\033OB", KEYC_DOWN|KEYC_CURSOR|KEYC_META },
|
|
|
|
{ "\033\033OC", KEYC_RIGHT|KEYC_CURSOR|KEYC_META },
|
|
|
|
{ "\033\033OD", KEYC_LEFT|KEYC_CURSOR|KEYC_META },
|
|
|
|
|
|
|
|
{ "\033\033[A", KEYC_UP|KEYC_CURSOR|KEYC_META },
|
|
|
|
{ "\033\033[B", KEYC_DOWN|KEYC_CURSOR|KEYC_META },
|
|
|
|
{ "\033\033[C", KEYC_RIGHT|KEYC_CURSOR|KEYC_META },
|
|
|
|
{ "\033\033[D", KEYC_LEFT|KEYC_CURSOR|KEYC_META },
|
2020-07-06 07:27:39 +00:00
|
|
|
|
2022-06-14 07:29:00 +00:00
|
|
|
/* Other xterm keys. */
|
2013-03-25 11:44:16 +00:00
|
|
|
{ "\033OH", KEYC_HOME },
|
|
|
|
{ "\033OF", KEYC_END },
|
|
|
|
|
2020-07-06 07:27:39 +00:00
|
|
|
{ "\033\033OH", KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ "\033\033OF", KEYC_END|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
|
2013-03-25 11:44:16 +00:00
|
|
|
{ "\033[H", KEYC_HOME },
|
|
|
|
{ "\033[F", KEYC_END },
|
|
|
|
|
2020-07-06 07:27:39 +00:00
|
|
|
{ "\033\033[H", KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ "\033\033[F", KEYC_END|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
|
2022-06-14 07:29:00 +00:00
|
|
|
/* rxvt arrow keys. */
|
2013-03-21 16:52:02 +00:00
|
|
|
{ "\033Oa", KEYC_UP|KEYC_CTRL },
|
|
|
|
{ "\033Ob", KEYC_DOWN|KEYC_CTRL },
|
|
|
|
{ "\033Oc", KEYC_RIGHT|KEYC_CTRL },
|
|
|
|
{ "\033Od", KEYC_LEFT|KEYC_CTRL },
|
|
|
|
|
|
|
|
{ "\033[a", KEYC_UP|KEYC_SHIFT },
|
|
|
|
{ "\033[b", KEYC_DOWN|KEYC_SHIFT },
|
|
|
|
{ "\033[c", KEYC_RIGHT|KEYC_SHIFT },
|
|
|
|
{ "\033[d", KEYC_LEFT|KEYC_SHIFT },
|
|
|
|
|
2022-06-14 07:29:00 +00:00
|
|
|
/* rxvt function keys. */
|
|
|
|
{ "\033[11~", KEYC_F1 },
|
|
|
|
{ "\033[12~", KEYC_F2 },
|
|
|
|
{ "\033[13~", KEYC_F3 },
|
|
|
|
{ "\033[14~", KEYC_F4 },
|
|
|
|
{ "\033[15~", KEYC_F5 },
|
|
|
|
{ "\033[17~", KEYC_F6 },
|
|
|
|
{ "\033[18~", KEYC_F7 },
|
|
|
|
{ "\033[19~", KEYC_F8 },
|
|
|
|
{ "\033[20~", KEYC_F9 },
|
|
|
|
{ "\033[21~", KEYC_F10 },
|
|
|
|
|
|
|
|
{ "\033[23~", KEYC_F1|KEYC_SHIFT },
|
|
|
|
{ "\033[24~", KEYC_F2|KEYC_SHIFT },
|
|
|
|
{ "\033[25~", KEYC_F3|KEYC_SHIFT },
|
|
|
|
{ "\033[26~", KEYC_F4|KEYC_SHIFT },
|
|
|
|
{ "\033[28~", KEYC_F5|KEYC_SHIFT },
|
|
|
|
{ "\033[29~", KEYC_F6|KEYC_SHIFT },
|
|
|
|
{ "\033[31~", KEYC_F7|KEYC_SHIFT },
|
|
|
|
{ "\033[32~", KEYC_F8|KEYC_SHIFT },
|
|
|
|
{ "\033[33~", KEYC_F9|KEYC_SHIFT },
|
|
|
|
{ "\033[34~", KEYC_F10|KEYC_SHIFT },
|
|
|
|
{ "\033[23$", KEYC_F11|KEYC_SHIFT },
|
|
|
|
{ "\033[24$", KEYC_F12|KEYC_SHIFT },
|
|
|
|
|
2013-03-21 16:52:02 +00:00
|
|
|
{ "\033[11^", KEYC_F1|KEYC_CTRL },
|
|
|
|
{ "\033[12^", KEYC_F2|KEYC_CTRL },
|
|
|
|
{ "\033[13^", KEYC_F3|KEYC_CTRL },
|
|
|
|
{ "\033[14^", KEYC_F4|KEYC_CTRL },
|
|
|
|
{ "\033[15^", KEYC_F5|KEYC_CTRL },
|
|
|
|
{ "\033[17^", KEYC_F6|KEYC_CTRL },
|
|
|
|
{ "\033[18^", KEYC_F7|KEYC_CTRL },
|
|
|
|
{ "\033[19^", KEYC_F8|KEYC_CTRL },
|
|
|
|
{ "\033[20^", KEYC_F9|KEYC_CTRL },
|
|
|
|
{ "\033[21^", KEYC_F10|KEYC_CTRL },
|
|
|
|
{ "\033[23^", KEYC_F11|KEYC_CTRL },
|
|
|
|
{ "\033[24^", KEYC_F12|KEYC_CTRL },
|
|
|
|
|
|
|
|
{ "\033[11@", KEYC_F1|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[12@", KEYC_F2|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[13@", KEYC_F3|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[14@", KEYC_F4|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[15@", KEYC_F5|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[17@", KEYC_F6|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[18@", KEYC_F7|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[19@", KEYC_F8|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[20@", KEYC_F9|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[21@", KEYC_F10|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[23@", KEYC_F11|KEYC_CTRL|KEYC_SHIFT },
|
|
|
|
{ "\033[24@", KEYC_F12|KEYC_CTRL|KEYC_SHIFT },
|
2013-03-24 09:28:59 +00:00
|
|
|
|
|
|
|
/* Focus tracking. */
|
|
|
|
{ "\033[I", KEYC_FOCUS_IN },
|
|
|
|
{ "\033[O", KEYC_FOCUS_OUT },
|
2017-06-12 07:04:24 +00:00
|
|
|
|
|
|
|
/* Paste keys. */
|
|
|
|
{ "\033[200~", KEYC_PASTE_START },
|
|
|
|
{ "\033[201~", KEYC_PASTE_END },
|
2022-11-01 09:54:13 +00:00
|
|
|
|
|
|
|
/* Extended keys. */
|
2022-11-02 07:36:07 +00:00
|
|
|
{ "\033[1;5Z", '\011'|KEYC_CTRL|KEYC_SHIFT },
|
2013-03-21 16:52:02 +00:00
|
|
|
};
|
2009-12-17 17:39:56 +00:00
|
|
|
|
2020-05-16 16:44:54 +00:00
|
|
|
/* Default xterm keys. */
|
|
|
|
struct tty_default_key_xterm {
|
|
|
|
const char *template;
|
|
|
|
key_code key;
|
|
|
|
};
|
|
|
|
static const struct tty_default_key_xterm tty_default_xterm_keys[] = {
|
|
|
|
{ "\033[1;_P", KEYC_F1 },
|
|
|
|
{ "\033O1;_P", KEYC_F1 },
|
|
|
|
{ "\033O_P", KEYC_F1 },
|
|
|
|
{ "\033[1;_Q", KEYC_F2 },
|
|
|
|
{ "\033O1;_Q", KEYC_F2 },
|
|
|
|
{ "\033O_Q", KEYC_F2 },
|
|
|
|
{ "\033[1;_R", KEYC_F3 },
|
|
|
|
{ "\033O1;_R", KEYC_F3 },
|
|
|
|
{ "\033O_R", KEYC_F3 },
|
|
|
|
{ "\033[1;_S", KEYC_F4 },
|
|
|
|
{ "\033O1;_S", KEYC_F4 },
|
|
|
|
{ "\033O_S", KEYC_F4 },
|
|
|
|
{ "\033[15;_~", KEYC_F5 },
|
|
|
|
{ "\033[17;_~", KEYC_F6 },
|
|
|
|
{ "\033[18;_~", KEYC_F7 },
|
|
|
|
{ "\033[19;_~", KEYC_F8 },
|
|
|
|
{ "\033[20;_~", KEYC_F9 },
|
|
|
|
{ "\033[21;_~", KEYC_F10 },
|
|
|
|
{ "\033[23;_~", KEYC_F11 },
|
|
|
|
{ "\033[24;_~", KEYC_F12 },
|
|
|
|
{ "\033[1;_A", KEYC_UP },
|
|
|
|
{ "\033[1;_B", KEYC_DOWN },
|
|
|
|
{ "\033[1;_C", KEYC_RIGHT },
|
|
|
|
{ "\033[1;_D", KEYC_LEFT },
|
|
|
|
{ "\033[1;_H", KEYC_HOME },
|
|
|
|
{ "\033[1;_F", KEYC_END },
|
|
|
|
{ "\033[5;_~", KEYC_PPAGE },
|
|
|
|
{ "\033[6;_~", KEYC_NPAGE },
|
|
|
|
{ "\033[2;_~", KEYC_IC },
|
|
|
|
{ "\033[3;_~", KEYC_DC },
|
|
|
|
};
|
|
|
|
static const key_code tty_default_xterm_modifiers[] = {
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
KEYC_SHIFT,
|
|
|
|
KEYC_META|KEYC_IMPLIED_META,
|
|
|
|
KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META,
|
|
|
|
KEYC_CTRL,
|
|
|
|
KEYC_SHIFT|KEYC_CTRL,
|
|
|
|
KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
|
2021-06-10 07:21:09 +00:00
|
|
|
KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
|
|
|
|
KEYC_META|KEYC_IMPLIED_META
|
2020-05-16 16:44:54 +00:00
|
|
|
};
|
|
|
|
|
2018-08-16 14:04:03 +00:00
|
|
|
/*
|
2020-05-16 16:44:54 +00:00
|
|
|
* Default terminfo(5) keys. Any keys that have builtin modifiers (that is,
|
|
|
|
* where the key itself contains the modifiers) has the KEYC_XTERM flag set so
|
|
|
|
* a leading escape is not treated as meta (and probably removed).
|
2018-08-16 14:04:03 +00:00
|
|
|
*/
|
2013-03-21 16:52:02 +00:00
|
|
|
struct tty_default_key_code {
|
|
|
|
enum tty_code_code code;
|
2021-04-07 07:30:02 +00:00
|
|
|
key_code key;
|
2013-03-21 16:52:02 +00:00
|
|
|
};
|
2016-10-10 21:29:23 +00:00
|
|
|
static const struct tty_default_key_code tty_default_code_keys[] = {
|
2009-12-17 17:39:56 +00:00
|
|
|
/* Function keys. */
|
2013-03-21 16:52:02 +00:00
|
|
|
{ TTYC_KF1, KEYC_F1 },
|
|
|
|
{ TTYC_KF2, KEYC_F2 },
|
|
|
|
{ TTYC_KF3, KEYC_F3 },
|
|
|
|
{ TTYC_KF4, KEYC_F4 },
|
|
|
|
{ TTYC_KF5, KEYC_F5 },
|
|
|
|
{ TTYC_KF6, KEYC_F6 },
|
|
|
|
{ TTYC_KF7, KEYC_F7 },
|
|
|
|
{ TTYC_KF8, KEYC_F8 },
|
|
|
|
{ TTYC_KF9, KEYC_F9 },
|
|
|
|
{ TTYC_KF10, KEYC_F10 },
|
|
|
|
{ TTYC_KF11, KEYC_F11 },
|
|
|
|
{ TTYC_KF12, KEYC_F12 },
|
2014-07-21 10:25:48 +00:00
|
|
|
|
2020-05-16 16:44:54 +00:00
|
|
|
{ TTYC_KF13, KEYC_F1|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF14, KEYC_F2|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF15, KEYC_F3|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF16, KEYC_F4|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF17, KEYC_F5|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF18, KEYC_F6|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF19, KEYC_F7|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF20, KEYC_F8|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF21, KEYC_F9|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF22, KEYC_F10|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF23, KEYC_F11|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF24, KEYC_F12|KEYC_SHIFT },
|
|
|
|
|
|
|
|
{ TTYC_KF25, KEYC_F1|KEYC_CTRL },
|
|
|
|
{ TTYC_KF26, KEYC_F2|KEYC_CTRL },
|
|
|
|
{ TTYC_KF27, KEYC_F3|KEYC_CTRL },
|
|
|
|
{ TTYC_KF28, KEYC_F4|KEYC_CTRL },
|
|
|
|
{ TTYC_KF29, KEYC_F5|KEYC_CTRL },
|
|
|
|
{ TTYC_KF30, KEYC_F6|KEYC_CTRL },
|
|
|
|
{ TTYC_KF31, KEYC_F7|KEYC_CTRL },
|
|
|
|
{ TTYC_KF32, KEYC_F8|KEYC_CTRL },
|
|
|
|
{ TTYC_KF33, KEYC_F9|KEYC_CTRL },
|
|
|
|
{ TTYC_KF34, KEYC_F10|KEYC_CTRL },
|
|
|
|
{ TTYC_KF35, KEYC_F11|KEYC_CTRL },
|
|
|
|
{ TTYC_KF36, KEYC_F12|KEYC_CTRL },
|
|
|
|
|
|
|
|
{ TTYC_KF37, KEYC_F1|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF38, KEYC_F2|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF39, KEYC_F3|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF40, KEYC_F4|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF41, KEYC_F5|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF42, KEYC_F6|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF43, KEYC_F7|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF44, KEYC_F8|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF45, KEYC_F9|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF46, KEYC_F10|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF47, KEYC_F11|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KF48, KEYC_F12|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
|
|
|
|
{ TTYC_KF49, KEYC_F1|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF50, KEYC_F2|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF51, KEYC_F3|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF52, KEYC_F4|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF53, KEYC_F5|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF54, KEYC_F6|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF55, KEYC_F7|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF56, KEYC_F8|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF57, KEYC_F9|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF58, KEYC_F10|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF59, KEYC_F11|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KF60, KEYC_F12|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
|
|
|
|
{ TTYC_KF61, KEYC_F1|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF62, KEYC_F2|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
|
|
|
|
{ TTYC_KF63, KEYC_F3|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
|
2014-07-21 10:25:48 +00:00
|
|
|
|
2013-03-21 16:52:02 +00:00
|
|
|
{ TTYC_KICH1, KEYC_IC },
|
|
|
|
{ TTYC_KDCH1, KEYC_DC },
|
|
|
|
{ TTYC_KHOME, KEYC_HOME },
|
|
|
|
{ TTYC_KEND, KEYC_END },
|
|
|
|
{ TTYC_KNP, KEYC_NPAGE },
|
|
|
|
{ TTYC_KPP, KEYC_PPAGE },
|
|
|
|
{ TTYC_KCBT, KEYC_BTAB },
|
2009-12-17 17:39:56 +00:00
|
|
|
|
|
|
|
/* Arrow keys from terminfo. */
|
2020-05-16 16:44:54 +00:00
|
|
|
{ TTYC_KCUU1, KEYC_UP|KEYC_CURSOR },
|
|
|
|
{ TTYC_KCUD1, KEYC_DOWN|KEYC_CURSOR },
|
|
|
|
{ TTYC_KCUB1, KEYC_LEFT|KEYC_CURSOR },
|
|
|
|
{ TTYC_KCUF1, KEYC_RIGHT|KEYC_CURSOR },
|
2009-12-17 17:39:56 +00:00
|
|
|
|
2018-08-16 14:04:03 +00:00
|
|
|
/* Key and modifier capabilities. */
|
2020-05-16 16:44:54 +00:00
|
|
|
{ TTYC_KDC2, KEYC_DC|KEYC_SHIFT },
|
|
|
|
{ TTYC_KDC3, KEYC_DC|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KDC4, KEYC_DC|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KDC5, KEYC_DC|KEYC_CTRL },
|
|
|
|
{ TTYC_KDC6, KEYC_DC|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KDC7, KEYC_DC|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
|
|
|
{ TTYC_KIND, KEYC_DOWN|KEYC_SHIFT },
|
|
|
|
{ TTYC_KDN2, KEYC_DOWN|KEYC_SHIFT },
|
|
|
|
{ TTYC_KDN3, KEYC_DOWN|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KDN4, KEYC_DOWN|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KDN5, KEYC_DOWN|KEYC_CTRL },
|
|
|
|
{ TTYC_KDN6, KEYC_DOWN|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KDN7, KEYC_DOWN|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
|
|
|
{ TTYC_KEND2, KEYC_END|KEYC_SHIFT },
|
|
|
|
{ TTYC_KEND3, KEYC_END|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KEND4, KEYC_END|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KEND5, KEYC_END|KEYC_CTRL },
|
|
|
|
{ TTYC_KEND6, KEYC_END|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KEND7, KEYC_END|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
|
|
|
{ TTYC_KHOM2, KEYC_HOME|KEYC_SHIFT },
|
|
|
|
{ TTYC_KHOM3, KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KHOM4, KEYC_HOME|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KHOM5, KEYC_HOME|KEYC_CTRL },
|
|
|
|
{ TTYC_KHOM6, KEYC_HOME|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KHOM7, KEYC_HOME|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
|
|
|
{ TTYC_KIC2, KEYC_IC|KEYC_SHIFT },
|
|
|
|
{ TTYC_KIC3, KEYC_IC|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KIC4, KEYC_IC|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KIC5, KEYC_IC|KEYC_CTRL },
|
|
|
|
{ TTYC_KIC6, KEYC_IC|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KIC7, KEYC_IC|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
|
|
|
{ TTYC_KLFT2, KEYC_LEFT|KEYC_SHIFT },
|
|
|
|
{ TTYC_KLFT3, KEYC_LEFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KLFT4, KEYC_LEFT|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KLFT5, KEYC_LEFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KLFT6, KEYC_LEFT|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KLFT7, KEYC_LEFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
|
|
|
{ TTYC_KNXT2, KEYC_NPAGE|KEYC_SHIFT },
|
|
|
|
{ TTYC_KNXT3, KEYC_NPAGE|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KNXT4, KEYC_NPAGE|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KNXT5, KEYC_NPAGE|KEYC_CTRL },
|
|
|
|
{ TTYC_KNXT6, KEYC_NPAGE|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KNXT7, KEYC_NPAGE|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
|
|
|
{ TTYC_KPRV2, KEYC_PPAGE|KEYC_SHIFT },
|
|
|
|
{ TTYC_KPRV3, KEYC_PPAGE|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KPRV4, KEYC_PPAGE|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KPRV5, KEYC_PPAGE|KEYC_CTRL },
|
|
|
|
{ TTYC_KPRV6, KEYC_PPAGE|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KPRV7, KEYC_PPAGE|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
|
|
|
{ TTYC_KRIT2, KEYC_RIGHT|KEYC_SHIFT },
|
|
|
|
{ TTYC_KRIT3, KEYC_RIGHT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KRIT4, KEYC_RIGHT|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KRIT5, KEYC_RIGHT|KEYC_CTRL },
|
|
|
|
{ TTYC_KRIT6, KEYC_RIGHT|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KRIT7, KEYC_RIGHT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
|
|
|
{ TTYC_KRI, KEYC_UP|KEYC_SHIFT },
|
|
|
|
{ TTYC_KUP2, KEYC_UP|KEYC_SHIFT },
|
|
|
|
{ TTYC_KUP3, KEYC_UP|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KUP4, KEYC_UP|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
|
|
|
|
{ TTYC_KUP5, KEYC_UP|KEYC_CTRL },
|
|
|
|
{ TTYC_KUP6, KEYC_UP|KEYC_SHIFT|KEYC_CTRL },
|
|
|
|
{ TTYC_KUP7, KEYC_UP|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2013-03-21 16:52:02 +00:00
|
|
|
/* Add key to tree. */
|
2016-04-26 22:19:22 +00:00
|
|
|
static void
|
2015-11-12 11:05:34 +00:00
|
|
|
tty_keys_add(struct tty *tty, const char *s, key_code key)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-12-17 17:39:56 +00:00
|
|
|
struct tty_key *tk;
|
|
|
|
size_t size;
|
2021-04-07 07:30:02 +00:00
|
|
|
const char *keystr;
|
2009-11-05 19:29:41 +00:00
|
|
|
|
2020-05-16 16:44:54 +00:00
|
|
|
keystr = key_string_lookup_key(key, 1);
|
2009-12-17 17:39:56 +00:00
|
|
|
if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) {
|
2015-11-12 11:05:34 +00:00
|
|
|
log_debug("new key %s: 0x%llx (%s)", s, key, keystr);
|
2009-11-05 19:29:41 +00:00
|
|
|
tty_keys_add1(&tty->key_tree, s, key);
|
2009-12-17 17:39:56 +00:00
|
|
|
} else {
|
2015-11-12 11:05:34 +00:00
|
|
|
log_debug("replacing key %s: 0x%llx (%s)", s, key, keystr);
|
2009-12-17 17:39:56 +00:00
|
|
|
tk->key = key;
|
2009-11-05 19:29:41 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Add next node to the tree. */
|
2016-04-26 22:19:22 +00:00
|
|
|
static void
|
2015-11-12 11:05:34 +00:00
|
|
|
tty_keys_add1(struct tty_key **tkp, const char *s, key_code key)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-11-05 19:29:41 +00:00
|
|
|
struct tty_key *tk;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Allocate a tree entry if there isn't one already. */
|
|
|
|
tk = *tkp;
|
|
|
|
if (tk == NULL) {
|
|
|
|
tk = *tkp = xcalloc(1, sizeof *tk);
|
|
|
|
tk->ch = *s;
|
2015-12-12 18:19:00 +00:00
|
|
|
tk->key = KEYC_UNKNOWN;
|
2009-11-05 19:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the next entry. */
|
|
|
|
if (*s == tk->ch) {
|
|
|
|
/* Move forward in string. */
|
|
|
|
s++;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* If this is the end of the string, no more is necessary. */
|
|
|
|
if (*s == '\0') {
|
|
|
|
tk->key = key;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use the child tree for the next character. */
|
|
|
|
tkp = &tk->next;
|
2009-12-03 22:50:09 +00:00
|
|
|
} else {
|
2009-11-05 19:29:41 +00:00
|
|
|
if (*s < tk->ch)
|
|
|
|
tkp = &tk->left;
|
|
|
|
else if (*s > tk->ch)
|
|
|
|
tkp = &tk->right;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* And recurse to add it. */
|
|
|
|
tty_keys_add1(tkp, s, key);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Initialise a key tree from the table. */
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2013-03-21 16:52:02 +00:00
|
|
|
tty_keys_build(struct tty *tty)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2013-03-21 16:52:02 +00:00
|
|
|
const struct tty_default_key_raw *tdkr;
|
2020-05-16 16:44:54 +00:00
|
|
|
const struct tty_default_key_xterm *tdkx;
|
2013-03-21 16:52:02 +00:00
|
|
|
const struct tty_default_key_code *tdkc;
|
2021-04-07 07:30:02 +00:00
|
|
|
u_int i, j;
|
2019-04-23 20:40:03 +00:00
|
|
|
const char *s;
|
2017-06-23 15:36:52 +00:00
|
|
|
struct options_entry *o;
|
2019-03-18 11:58:40 +00:00
|
|
|
struct options_array_item *a;
|
2019-04-23 20:40:03 +00:00
|
|
|
union options_value *ov;
|
2020-05-16 16:44:54 +00:00
|
|
|
char copy[16];
|
|
|
|
key_code key;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2013-03-21 16:52:02 +00:00
|
|
|
if (tty->key_tree != NULL)
|
2014-10-22 23:18:53 +00:00
|
|
|
tty_keys_free(tty);
|
2009-11-05 19:29:41 +00:00
|
|
|
tty->key_tree = NULL;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2020-05-16 16:44:54 +00:00
|
|
|
for (i = 0; i < nitems(tty_default_xterm_keys); i++) {
|
|
|
|
tdkx = &tty_default_xterm_keys[i];
|
|
|
|
for (j = 2; j < nitems(tty_default_xterm_modifiers); j++) {
|
|
|
|
strlcpy(copy, tdkx->template, sizeof copy);
|
|
|
|
copy[strcspn(copy, "_")] = '0' + j;
|
|
|
|
|
|
|
|
key = tdkx->key|tty_default_xterm_modifiers[j];
|
|
|
|
tty_keys_add(tty, copy, key);
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 16:52:02 +00:00
|
|
|
for (i = 0; i < nitems(tty_default_raw_keys); i++) {
|
|
|
|
tdkr = &tty_default_raw_keys[i];
|
|
|
|
|
|
|
|
s = tdkr->string;
|
2013-03-21 18:45:38 +00:00
|
|
|
if (*s != '\0')
|
|
|
|
tty_keys_add(tty, s, tdkr->key);
|
2013-03-21 16:52:02 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < nitems(tty_default_code_keys); i++) {
|
|
|
|
tdkc = &tty_default_code_keys[i];
|
|
|
|
|
|
|
|
s = tty_term_string(tty->term, tdkc->code);
|
2013-03-21 18:45:38 +00:00
|
|
|
if (*s != '\0')
|
|
|
|
tty_keys_add(tty, s, tdkc->key);
|
2013-03-21 16:52:02 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2017-06-23 15:36:52 +00:00
|
|
|
|
|
|
|
o = options_get(global_options, "user-keys");
|
2019-03-18 11:58:40 +00:00
|
|
|
if (o != NULL) {
|
|
|
|
a = options_array_first(o);
|
|
|
|
while (a != NULL) {
|
2019-05-03 18:00:19 +00:00
|
|
|
i = options_array_item_index(a);
|
2019-04-23 20:40:03 +00:00
|
|
|
ov = options_array_item_value(a);
|
2019-04-25 19:36:59 +00:00
|
|
|
tty_keys_add(tty, ov->string, KEYC_USER + i);
|
2019-03-18 11:58:40 +00:00
|
|
|
a = options_array_next(a);
|
2017-06-23 15:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Free the entire key tree. */
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
|
|
|
tty_keys_free(struct tty *tty)
|
|
|
|
{
|
2009-11-05 19:29:41 +00:00
|
|
|
tty_keys_free1(tty->key_tree);
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Free a single key. */
|
2016-04-26 22:19:22 +00:00
|
|
|
static void
|
2009-11-05 19:29:41 +00:00
|
|
|
tty_keys_free1(struct tty_key *tk)
|
|
|
|
{
|
|
|
|
if (tk->next != NULL)
|
|
|
|
tty_keys_free1(tk->next);
|
|
|
|
if (tk->left != NULL)
|
|
|
|
tty_keys_free1(tk->left);
|
|
|
|
if (tk->right != NULL)
|
|
|
|
tty_keys_free1(tk->right);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(tk);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Lookup a key in the tree. */
|
2016-04-26 22:19:22 +00:00
|
|
|
static struct tty_key *
|
2009-11-05 19:29:41 +00:00
|
|
|
tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-11-05 19:29:41 +00:00
|
|
|
*size = 0;
|
|
|
|
return (tty_keys_find1(tty->key_tree, buf, len, size));
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Find the next node. */
|
2016-04-26 22:19:22 +00:00
|
|
|
static struct tty_key *
|
2009-11-05 19:29:41 +00:00
|
|
|
tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
|
|
|
|
{
|
2019-02-16 19:04:34 +00:00
|
|
|
/* If no data, no match. */
|
|
|
|
if (len == 0)
|
|
|
|
return (NULL);
|
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* If the node is NULL, this is the end of the tree. No match. */
|
|
|
|
if (tk == NULL)
|
2009-06-01 22:58:49 +00:00
|
|
|
return (NULL);
|
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Pick the next in the sequence. */
|
|
|
|
if (tk->ch == *buf) {
|
|
|
|
/* Move forward in the string. */
|
|
|
|
buf++; len--;
|
|
|
|
(*size)++;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* At the end of the string, return the current node. */
|
2015-12-12 18:19:00 +00:00
|
|
|
if (len == 0 || (tk->next == NULL && tk->key != KEYC_UNKNOWN))
|
2009-06-01 22:58:49 +00:00
|
|
|
return (tk);
|
2009-11-05 19:29:41 +00:00
|
|
|
|
|
|
|
/* Move into the next tree for the following character. */
|
|
|
|
tk = tk->next;
|
|
|
|
} else {
|
|
|
|
if (*buf < tk->ch)
|
|
|
|
tk = tk->left;
|
|
|
|
else if (*buf > tk->ch)
|
|
|
|
tk = tk->right;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Move to the next in the tree. */
|
|
|
|
return (tty_keys_find1(tk, buf, len, size));
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 22:19:22 +00:00
|
|
|
/* Look up part of the next key. */
|
|
|
|
static int
|
|
|
|
tty_keys_next1(struct tty *tty, const char *buf, size_t len, key_code *key,
|
2016-04-27 16:46:21 +00:00
|
|
|
size_t *size, int expired)
|
2016-04-26 22:19:22 +00:00
|
|
|
{
|
2017-04-18 13:34:04 +00:00
|
|
|
struct client *c = tty->client;
|
2016-04-26 22:19:22 +00:00
|
|
|
struct tty_key *tk, *tk1;
|
|
|
|
struct utf8_data ud;
|
|
|
|
enum utf8_state more;
|
2020-05-25 18:57:24 +00:00
|
|
|
utf8_char uc;
|
2016-04-26 22:19:22 +00:00
|
|
|
u_int i;
|
|
|
|
|
2017-04-18 13:34:04 +00:00
|
|
|
log_debug("%s: next key is %zu (%.*s) (expired=%d)", c->name, len,
|
|
|
|
(int)len, buf, expired);
|
2016-04-26 22:19:22 +00:00
|
|
|
|
|
|
|
/* Is this a known key? */
|
|
|
|
tk = tty_keys_find(tty, buf, len, size);
|
2016-04-27 16:46:21 +00:00
|
|
|
if (tk != NULL && tk->key != KEYC_UNKNOWN) {
|
2016-04-26 22:19:22 +00:00
|
|
|
tk1 = tk;
|
|
|
|
do
|
2017-04-18 13:34:04 +00:00
|
|
|
log_debug("%s: keys in list: %#llx", c->name, tk1->key);
|
2016-04-26 22:19:22 +00:00
|
|
|
while ((tk1 = tk1->next) != NULL);
|
2016-04-27 16:46:21 +00:00
|
|
|
if (tk->next != NULL && !expired)
|
|
|
|
return (1);
|
2016-04-26 22:19:22 +00:00
|
|
|
*key = tk->key;
|
2016-04-27 16:46:21 +00:00
|
|
|
return (0);
|
2016-04-26 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this valid UTF-8? */
|
|
|
|
more = utf8_open(&ud, (u_char)*buf);
|
|
|
|
if (more == UTF8_MORE) {
|
|
|
|
*size = ud.size;
|
2016-04-27 16:46:21 +00:00
|
|
|
if (len < ud.size) {
|
|
|
|
if (!expired)
|
|
|
|
return (1);
|
|
|
|
return (-1);
|
|
|
|
}
|
2016-04-26 22:19:22 +00:00
|
|
|
for (i = 1; i < ud.size; i++)
|
|
|
|
more = utf8_append(&ud, (u_char)buf[i]);
|
|
|
|
if (more != UTF8_DONE)
|
2016-04-27 16:46:21 +00:00
|
|
|
return (-1);
|
2016-04-26 22:19:22 +00:00
|
|
|
|
2020-05-25 18:57:24 +00:00
|
|
|
if (utf8_from_data(&ud, &uc) != UTF8_DONE)
|
2016-04-27 16:46:21 +00:00
|
|
|
return (-1);
|
2020-05-25 18:57:24 +00:00
|
|
|
*key = uc;
|
2016-04-26 22:19:22 +00:00
|
|
|
|
2017-04-18 13:34:04 +00:00
|
|
|
log_debug("%s: UTF-8 key %.*s %#llx", c->name, (int)ud.size,
|
2020-05-25 18:57:24 +00:00
|
|
|
ud.data, *key);
|
2016-04-26 22:19:22 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2019-05-03 14:51:30 +00:00
|
|
|
/* Process at least one key in the buffer. Return 0 if no keys present. */
|
|
|
|
int
|
2009-11-05 08:45:08 +00:00
|
|
|
tty_keys_next(struct tty *tty)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2019-05-03 14:51:30 +00:00
|
|
|
struct client *c = tty->client;
|
|
|
|
struct timeval tv;
|
|
|
|
const char *buf;
|
|
|
|
size_t len, size;
|
|
|
|
cc_t bspace;
|
|
|
|
int delay, expired = 0, n;
|
|
|
|
key_code key;
|
|
|
|
struct mouse_event m = { 0 };
|
|
|
|
struct key_event *event;
|
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
/* Get key buffer. */
|
2017-02-10 12:59:18 +00:00
|
|
|
buf = EVBUFFER_DATA(tty->in);
|
|
|
|
len = EVBUFFER_LENGTH(tty->in);
|
2009-06-01 22:58:49 +00:00
|
|
|
if (len == 0)
|
2009-11-05 08:45:08 +00:00
|
|
|
return (0);
|
2017-04-18 13:34:04 +00:00
|
|
|
log_debug("%s: keys are %zu (%.*s)", c->name, len, (int)len, buf);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2018-10-18 08:04:14 +00:00
|
|
|
/* Is this a clipboard response? */
|
|
|
|
switch (tty_keys_clipboard(tty, buf, len, &size)) {
|
|
|
|
case 0: /* yes */
|
|
|
|
key = KEYC_UNKNOWN;
|
|
|
|
goto complete_key;
|
|
|
|
case -1: /* no, or not valid */
|
|
|
|
break;
|
|
|
|
case 1: /* partial */
|
|
|
|
goto partial_key;
|
|
|
|
}
|
|
|
|
|
2022-11-11 08:37:55 +00:00
|
|
|
/* Is this a primary device attributes response? */
|
2016-11-15 14:02:32 +00:00
|
|
|
switch (tty_keys_device_attributes(tty, buf, len, &size)) {
|
|
|
|
case 0: /* yes */
|
|
|
|
key = KEYC_UNKNOWN;
|
|
|
|
goto complete_key;
|
|
|
|
case -1: /* no, or not valid */
|
|
|
|
break;
|
|
|
|
case 1: /* partial */
|
|
|
|
goto partial_key;
|
|
|
|
}
|
|
|
|
|
2022-11-11 08:37:55 +00:00
|
|
|
/* Is this a secondary device attributes response? */
|
|
|
|
switch (tty_keys_device_attributes2(tty, buf, len, &size)) {
|
|
|
|
case 0: /* yes */
|
|
|
|
key = KEYC_UNKNOWN;
|
|
|
|
goto complete_key;
|
|
|
|
case -1: /* no, or not valid */
|
|
|
|
break;
|
|
|
|
case 1: /* partial */
|
|
|
|
goto partial_key;
|
|
|
|
}
|
|
|
|
|
2020-05-16 14:16:25 +00:00
|
|
|
/* Is this an extended device attributes response? */
|
|
|
|
switch (tty_keys_extended_device_attributes(tty, buf, len, &size)) {
|
2020-01-12 22:00:20 +00:00
|
|
|
case 0: /* yes */
|
|
|
|
key = KEYC_UNKNOWN;
|
|
|
|
goto complete_key;
|
|
|
|
case -1: /* no, or not valid */
|
|
|
|
break;
|
|
|
|
case 1: /* partial */
|
|
|
|
goto partial_key;
|
|
|
|
}
|
|
|
|
|
2023-01-03 11:43:24 +00:00
|
|
|
/* Is this a colours response? */
|
2024-06-24 08:30:50 +00:00
|
|
|
switch (tty_keys_colours(tty, buf, len, &size, &tty->fg, &tty->bg)) {
|
2023-01-03 11:43:24 +00:00
|
|
|
case 0: /* yes */
|
|
|
|
key = KEYC_UNKNOWN;
|
|
|
|
goto complete_key;
|
|
|
|
case -1: /* no, or not valid */
|
|
|
|
break;
|
|
|
|
case 1: /* partial */
|
|
|
|
goto partial_key;
|
|
|
|
}
|
|
|
|
|
2009-12-01 07:59:40 +00:00
|
|
|
/* Is this a mouse key press? */
|
2019-05-03 14:51:30 +00:00
|
|
|
switch (tty_keys_mouse(tty, buf, len, &size, &m)) {
|
2009-11-26 15:14:03 +00:00
|
|
|
case 0: /* yes */
|
|
|
|
key = KEYC_MOUSE;
|
2013-03-21 18:45:38 +00:00
|
|
|
goto complete_key;
|
2009-11-26 15:14:03 +00:00
|
|
|
case -1: /* no, or not valid */
|
2009-11-26 22:26:51 +00:00
|
|
|
break;
|
2014-06-19 07:26:43 +00:00
|
|
|
case -2: /* yes, but we don't care. */
|
2015-06-05 15:10:13 +00:00
|
|
|
key = KEYC_MOUSE;
|
2014-06-19 07:26:43 +00:00
|
|
|
goto discard_key;
|
2009-11-26 15:14:03 +00:00
|
|
|
case 1: /* partial */
|
|
|
|
goto partial_key;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:44:54 +00:00
|
|
|
/* Is this an extended key press? */
|
|
|
|
switch (tty_keys_extended_key(tty, buf, len, &size, &key)) {
|
|
|
|
case 0: /* yes */
|
|
|
|
goto complete_key;
|
|
|
|
case -1: /* no, or not valid */
|
|
|
|
break;
|
|
|
|
case 1: /* partial */
|
|
|
|
goto partial_key;
|
|
|
|
}
|
|
|
|
|
2016-04-26 22:19:22 +00:00
|
|
|
first_key:
|
2017-08-29 09:18:48 +00:00
|
|
|
/* Try to lookup complete key. */
|
|
|
|
n = tty_keys_next1(tty, buf, len, &key, &size, expired);
|
|
|
|
if (n == 0) /* found */
|
|
|
|
goto complete_key;
|
|
|
|
if (n == 1)
|
|
|
|
goto partial_key;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If not a complete key, look for key with an escape prefix (meta
|
|
|
|
* modifier).
|
|
|
|
*/
|
2018-10-28 15:34:27 +00:00
|
|
|
if (*buf == '\033' && len > 1) {
|
2016-04-27 16:46:21 +00:00
|
|
|
/* Look for a key without the escape. */
|
|
|
|
n = tty_keys_next1(tty, buf + 1, len - 1, &key, &size, expired);
|
|
|
|
if (n == 0) { /* found */
|
2020-05-16 16:44:54 +00:00
|
|
|
if (key & KEYC_IMPLIED_META) {
|
2017-05-07 21:25:59 +00:00
|
|
|
/*
|
|
|
|
* We want the escape key as well as the xterm
|
|
|
|
* key, because the xterm sequence implicitly
|
|
|
|
* includes the escape (so if we see
|
|
|
|
* \033\033[1;3D we know it is an Escape
|
|
|
|
* followed by M-Left, not just M-Left).
|
|
|
|
*/
|
|
|
|
key = '\033';
|
|
|
|
size = 1;
|
|
|
|
goto complete_key;
|
|
|
|
}
|
2020-05-16 16:44:54 +00:00
|
|
|
key |= KEYC_META;
|
2016-04-27 16:46:21 +00:00
|
|
|
size++;
|
2016-04-26 22:19:22 +00:00
|
|
|
goto complete_key;
|
2016-04-27 16:46:21 +00:00
|
|
|
}
|
|
|
|
if (n == 1) /* partial */
|
|
|
|
goto partial_key;
|
2016-04-29 10:42:16 +00:00
|
|
|
}
|
2014-02-10 11:20:41 +00:00
|
|
|
|
2016-04-29 10:42:16 +00:00
|
|
|
/*
|
|
|
|
* At this point, we know the key is not partial (with or without
|
|
|
|
* escape). So pass it through even if the timer has not expired.
|
|
|
|
*/
|
|
|
|
if (*buf == '\033' && len >= 2) {
|
2020-05-16 16:44:54 +00:00
|
|
|
key = (u_char)buf[1] | KEYC_META;
|
2016-04-29 10:42:16 +00:00
|
|
|
size = 2;
|
|
|
|
} else {
|
|
|
|
key = (u_char)buf[0];
|
|
|
|
size = 1;
|
2015-11-12 11:05:34 +00:00
|
|
|
}
|
2013-03-21 18:45:38 +00:00
|
|
|
goto complete_key;
|
2009-11-05 19:29:41 +00:00
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
partial_key:
|
2017-04-18 13:34:04 +00:00
|
|
|
log_debug("%s: partial key %.*s", c->name, (int)len, buf);
|
2013-03-21 18:45:38 +00:00
|
|
|
|
|
|
|
/* If timer is going, check for expiration. */
|
|
|
|
if (tty->flags & TTY_TIMER) {
|
|
|
|
if (evtimer_initialized(&tty->key_timer) &&
|
2013-03-26 14:14:08 +00:00
|
|
|
!evtimer_pending(&tty->key_timer, NULL)) {
|
|
|
|
expired = 1;
|
2013-03-21 18:45:38 +00:00
|
|
|
goto first_key;
|
2013-03-26 14:14:08 +00:00
|
|
|
}
|
2012-04-22 05:21:40 +00:00
|
|
|
return (0);
|
2013-03-21 18:45:38 +00:00
|
|
|
}
|
2012-04-22 05:21:40 +00:00
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
/* Get the time period. */
|
2015-10-27 15:58:42 +00:00
|
|
|
delay = options_get_number(global_options, "escape-time");
|
2022-06-01 15:43:22 +00:00
|
|
|
if (delay == 0)
|
|
|
|
delay = 1;
|
2009-12-14 10:43:41 +00:00
|
|
|
tv.tv_sec = delay / 1000;
|
|
|
|
tv.tv_usec = (delay % 1000) * 1000L;
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
/* Start the timer. */
|
2012-03-17 18:24:07 +00:00
|
|
|
if (event_initialized(&tty->key_timer))
|
|
|
|
evtimer_del(&tty->key_timer);
|
2009-11-05 08:45:08 +00:00
|
|
|
evtimer_set(&tty->key_timer, tty_keys_callback, tty);
|
|
|
|
evtimer_add(&tty->key_timer, &tv);
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
tty->flags |= TTY_TIMER;
|
2009-11-05 08:45:08 +00:00
|
|
|
return (0);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
complete_key:
|
2017-04-18 13:34:04 +00:00
|
|
|
log_debug("%s: complete key %.*s %#llx", c->name, (int)size, buf, key);
|
2009-11-05 19:29:41 +00:00
|
|
|
|
2016-04-27 16:46:21 +00:00
|
|
|
/*
|
|
|
|
* Check for backspace key using termios VERASE - the terminfo
|
|
|
|
* kbs entry is extremely unreliable, so cannot be safely
|
|
|
|
* used. termios should have a better idea.
|
|
|
|
*/
|
|
|
|
bspace = tty->tio.c_cc[VERASE];
|
|
|
|
if (bspace != _POSIX_VDISABLE && (key & KEYC_MASK_KEY) == bspace)
|
2020-05-16 16:44:54 +00:00
|
|
|
key = (key & KEYC_MASK_MODIFIERS)|KEYC_BSPACE;
|
2016-04-27 16:46:21 +00:00
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
/* Remove data from buffer. */
|
2017-02-10 12:59:18 +00:00
|
|
|
evbuffer_drain(tty->in, size);
|
2009-11-05 19:29:41 +00:00
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
/* Remove key timer. */
|
2012-03-17 18:24:07 +00:00
|
|
|
if (event_initialized(&tty->key_timer))
|
|
|
|
evtimer_del(&tty->key_timer);
|
2013-03-21 18:45:38 +00:00
|
|
|
tty->flags &= ~TTY_TIMER;
|
2009-11-05 19:29:41 +00:00
|
|
|
|
2013-03-24 09:28:59 +00:00
|
|
|
/* Check for focus events. */
|
2021-08-06 09:19:02 +00:00
|
|
|
if (key == KEYC_FOCUS_OUT) {
|
2021-08-13 06:52:51 +00:00
|
|
|
c->flags &= ~CLIENT_FOCUSED;
|
|
|
|
window_update_focus(c->session->curw->window);
|
2021-08-06 09:19:02 +00:00
|
|
|
notify_client("client-focus-out", c);
|
|
|
|
} else if (key == KEYC_FOCUS_IN) {
|
2021-08-13 06:52:51 +00:00
|
|
|
c->flags |= CLIENT_FOCUSED;
|
2021-08-06 09:19:02 +00:00
|
|
|
notify_client("client-focus-in", c);
|
2021-08-13 06:52:51 +00:00
|
|
|
window_update_focus(c->session->curw->window);
|
2021-08-06 09:19:02 +00:00
|
|
|
}
|
2013-03-24 09:28:59 +00:00
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
/* Fire the key. */
|
2019-05-03 14:51:30 +00:00
|
|
|
if (key != KEYC_UNKNOWN) {
|
|
|
|
event = xmalloc(sizeof *event);
|
|
|
|
event->key = key;
|
|
|
|
memcpy(&event->m, &m, sizeof event->m);
|
2019-05-07 11:24:03 +00:00
|
|
|
if (!server_client_handle_key(c, event))
|
|
|
|
free(event);
|
2019-05-03 14:51:30 +00:00
|
|
|
}
|
2009-11-05 19:29:41 +00:00
|
|
|
|
2009-11-05 08:45:08 +00:00
|
|
|
return (1);
|
2014-06-19 07:26:43 +00:00
|
|
|
|
|
|
|
discard_key:
|
2017-04-18 13:34:04 +00:00
|
|
|
log_debug("%s: discard key %.*s %#llx", c->name, (int)size, buf, key);
|
2014-06-19 07:26:43 +00:00
|
|
|
|
|
|
|
/* Remove data from buffer. */
|
2017-02-10 12:59:18 +00:00
|
|
|
evbuffer_drain(tty->in, size);
|
2014-06-19 07:26:43 +00:00
|
|
|
|
|
|
|
return (1);
|
2009-11-05 08:45:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-05 19:29:41 +00:00
|
|
|
/* Key timer callback. */
|
2016-04-26 22:19:22 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
tty_keys_callback(__unused int fd, __unused short events, void *data)
|
2009-11-05 08:45:08 +00:00
|
|
|
{
|
|
|
|
struct tty *tty = data;
|
|
|
|
|
2013-03-21 18:45:38 +00:00
|
|
|
if (tty->flags & TTY_TIMER) {
|
|
|
|
while (tty_keys_next(tty))
|
|
|
|
;
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:44:54 +00:00
|
|
|
/*
|
|
|
|
* Handle extended key input. This has two forms: \033[27;m;k~ and \033[k;mu,
|
|
|
|
* where k is key as a number and m is a modifier. Returns 0 for success, -1
|
|
|
|
* for failure, 1 for partial;
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
tty_keys_extended_key(struct tty *tty, const char *buf, size_t len,
|
|
|
|
size_t *size, key_code *key)
|
|
|
|
{
|
|
|
|
struct client *c = tty->client;
|
|
|
|
size_t end;
|
|
|
|
u_int number, modifiers;
|
|
|
|
char tmp[64];
|
2021-04-07 07:30:02 +00:00
|
|
|
cc_t bspace;
|
|
|
|
key_code nkey;
|
2021-04-07 15:46:12 +00:00
|
|
|
key_code onlykey;
|
2020-05-16 16:44:54 +00:00
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
/* First two bytes are always \033[. */
|
|
|
|
if (buf[0] != '\033')
|
|
|
|
return (-1);
|
|
|
|
if (len == 1)
|
|
|
|
return (1);
|
|
|
|
if (buf[1] != '[')
|
|
|
|
return (-1);
|
|
|
|
if (len == 2)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for a terminator. Stop at either '~' or anything that isn't a
|
|
|
|
* number or ';'.
|
|
|
|
*/
|
|
|
|
for (end = 2; end < len && end != sizeof tmp; end++) {
|
|
|
|
if (buf[end] == '~')
|
|
|
|
break;
|
|
|
|
if (!isdigit((u_char)buf[end]) && buf[end] != ';')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (end == len)
|
|
|
|
return (1);
|
|
|
|
if (end == sizeof tmp || (buf[end] != '~' && buf[end] != 'u'))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* Copy to the buffer. */
|
|
|
|
memcpy(tmp, buf + 2, end);
|
|
|
|
tmp[end] = '\0';
|
|
|
|
|
|
|
|
/* Try to parse either form of key. */
|
|
|
|
if (buf[end] == '~') {
|
|
|
|
if (sscanf(tmp, "27;%u;%u", &modifiers, &number) != 2)
|
|
|
|
return (-1);
|
|
|
|
} else {
|
|
|
|
if (sscanf(tmp ,"%u;%u", &number, &modifiers) != 2)
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
*size = end + 1;
|
|
|
|
|
2021-04-07 07:30:02 +00:00
|
|
|
/* Store the key. */
|
|
|
|
bspace = tty->tio.c_cc[VERASE];
|
|
|
|
if (bspace != _POSIX_VDISABLE && number == bspace)
|
|
|
|
nkey = KEYC_BSPACE;
|
|
|
|
else
|
|
|
|
nkey = number;
|
|
|
|
|
|
|
|
/* Update the modifiers. */
|
2022-07-19 06:51:31 +00:00
|
|
|
if (modifiers > 0) {
|
|
|
|
modifiers--;
|
|
|
|
if (modifiers & 1)
|
|
|
|
nkey |= KEYC_SHIFT;
|
|
|
|
if (modifiers & 2)
|
|
|
|
nkey |= (KEYC_META|KEYC_IMPLIED_META); /* Alt */
|
|
|
|
if (modifiers & 4)
|
|
|
|
nkey |= KEYC_CTRL;
|
|
|
|
if (modifiers & 8)
|
|
|
|
nkey |= (KEYC_META|KEYC_IMPLIED_META); /* Meta */
|
2020-05-16 16:44:54 +00:00
|
|
|
}
|
2021-04-07 07:30:02 +00:00
|
|
|
|
2021-04-07 15:46:12 +00:00
|
|
|
/*
|
|
|
|
* Don't allow both KEYC_CTRL and as an implied modifier. Also convert
|
|
|
|
* C-X into C-x and so on.
|
|
|
|
*/
|
2021-04-13 05:23:34 +00:00
|
|
|
if (nkey & KEYC_CTRL) {
|
2021-04-07 15:46:12 +00:00
|
|
|
onlykey = (nkey & KEYC_MASK_KEY);
|
2021-06-10 07:21:09 +00:00
|
|
|
if (onlykey < 32 &&
|
|
|
|
onlykey != 9 &&
|
|
|
|
onlykey != 13 &&
|
|
|
|
onlykey != 27)
|
2021-04-13 12:26:34 +00:00
|
|
|
/* nothing */;
|
|
|
|
else if (onlykey >= 97 && onlykey <= 122)
|
|
|
|
onlykey -= 96;
|
|
|
|
else if (onlykey >= 64 && onlykey <= 95)
|
|
|
|
onlykey -= 64;
|
|
|
|
else if (onlykey == 32)
|
|
|
|
onlykey = 0;
|
|
|
|
else if (onlykey == 63)
|
|
|
|
onlykey = 127;
|
|
|
|
else
|
|
|
|
onlykey |= KEYC_CTRL;
|
|
|
|
nkey = onlykey|((nkey & KEYC_MASK_MODIFIERS) & ~KEYC_CTRL);
|
2021-04-07 07:30:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:44:54 +00:00
|
|
|
if (log_get_level() != 0) {
|
|
|
|
log_debug("%s: extended key %.*s is %llx (%s)", c->name,
|
2021-04-07 07:30:02 +00:00
|
|
|
(int)*size, buf, nkey, key_string_lookup_key(nkey, 1));
|
2020-05-16 16:44:54 +00:00
|
|
|
}
|
2022-11-02 07:36:07 +00:00
|
|
|
*key = nkey;
|
2020-05-16 16:44:54 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-12-03 22:50:09 +00:00
|
|
|
/*
|
2009-11-26 15:14:03 +00:00
|
|
|
* Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
|
2023-04-17 17:57:35 +00:00
|
|
|
* (probably a mouse sequence but need more data), -2 if an invalid mouse
|
|
|
|
* sequence.
|
2009-11-26 15:14:03 +00:00
|
|
|
*/
|
2016-04-26 22:19:22 +00:00
|
|
|
static int
|
2019-05-03 14:51:30 +00:00
|
|
|
tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size,
|
|
|
|
struct mouse_event *m)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2019-05-03 14:51:30 +00:00
|
|
|
struct client *c = tty->client;
|
|
|
|
u_int i, x, y, b, sgr_b;
|
|
|
|
u_char sgr_type, ch;
|
2011-01-03 23:35:21 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
/*
|
2011-01-03 23:35:21 +00:00
|
|
|
* Standard mouse sequences are \033[M followed by three characters
|
2013-03-22 10:33:50 +00:00
|
|
|
* indicating button, X and Y, all based at 32 with 1,1 top-left.
|
2011-01-03 23:35:21 +00:00
|
|
|
*
|
|
|
|
* UTF-8 mouse sequences are similar but the three are expressed as
|
|
|
|
* UTF-8 characters.
|
2013-03-22 10:33:50 +00:00
|
|
|
*
|
|
|
|
* SGR extended mouse sequences are \033[< followed by three numbers in
|
|
|
|
* decimal and separated by semicolons indicating button, X and Y. A
|
|
|
|
* trailing 'M' is click or scroll and trailing 'm' release. All are
|
|
|
|
* based at 0 with 1,1 top-left.
|
2009-06-01 22:58:49 +00:00
|
|
|
*/
|
|
|
|
|
2009-11-26 15:14:03 +00:00
|
|
|
*size = 0;
|
2015-04-19 21:34:21 +00:00
|
|
|
x = y = b = sgr_b = 0;
|
|
|
|
sgr_type = ' ';
|
2009-11-26 15:14:03 +00:00
|
|
|
|
2013-03-22 10:33:50 +00:00
|
|
|
/* First two bytes are always \033[. */
|
2009-11-26 15:14:03 +00:00
|
|
|
if (buf[0] != '\033')
|
|
|
|
return (-1);
|
|
|
|
if (len == 1)
|
|
|
|
return (1);
|
|
|
|
if (buf[1] != '[')
|
|
|
|
return (-1);
|
|
|
|
if (len == 2)
|
|
|
|
return (1);
|
|
|
|
|
2013-03-22 10:33:50 +00:00
|
|
|
/*
|
2015-11-23 20:53:09 +00:00
|
|
|
* Third byte is M in old standard (and UTF-8 extension which we do not
|
|
|
|
* support), < in SGR extension.
|
2013-03-22 10:33:50 +00:00
|
|
|
*/
|
|
|
|
if (buf[2] == 'M') {
|
|
|
|
/* Read the three inputs. */
|
|
|
|
*size = 3;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (len <= *size)
|
|
|
|
return (1);
|
2017-04-18 13:34:04 +00:00
|
|
|
ch = (u_char)buf[(*size)++];
|
2013-03-22 10:33:50 +00:00
|
|
|
if (i == 0)
|
2017-04-18 13:34:04 +00:00
|
|
|
b = ch;
|
2013-03-22 10:33:50 +00:00
|
|
|
else if (i == 1)
|
2017-04-18 13:34:04 +00:00
|
|
|
x = ch;
|
2013-03-22 10:33:50 +00:00
|
|
|
else
|
2017-04-18 13:34:04 +00:00
|
|
|
y = ch;
|
2013-03-22 10:33:50 +00:00
|
|
|
}
|
2017-04-18 13:34:04 +00:00
|
|
|
log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf);
|
2013-03-22 10:33:50 +00:00
|
|
|
|
|
|
|
/* Check and return the mouse input. */
|
2022-05-30 13:02:55 +00:00
|
|
|
if (b < MOUSE_PARAM_BTN_OFF ||
|
|
|
|
x < MOUSE_PARAM_POS_OFF ||
|
|
|
|
y < MOUSE_PARAM_POS_OFF)
|
2023-04-17 17:57:35 +00:00
|
|
|
return (-2);
|
2022-05-30 13:02:55 +00:00
|
|
|
b -= MOUSE_PARAM_BTN_OFF;
|
|
|
|
x -= MOUSE_PARAM_POS_OFF;
|
|
|
|
y -= MOUSE_PARAM_POS_OFF;
|
2013-03-22 10:33:50 +00:00
|
|
|
} else if (buf[2] == '<') {
|
|
|
|
/* Read the three inputs. */
|
|
|
|
*size = 3;
|
|
|
|
while (1) {
|
|
|
|
if (len <= *size)
|
|
|
|
return (1);
|
2017-04-18 13:34:04 +00:00
|
|
|
ch = (u_char)buf[(*size)++];
|
|
|
|
if (ch == ';')
|
2013-03-22 10:33:50 +00:00
|
|
|
break;
|
2017-04-18 13:34:04 +00:00
|
|
|
if (ch < '0' || ch > '9')
|
2013-03-22 10:33:50 +00:00
|
|
|
return (-1);
|
2017-04-18 13:34:04 +00:00
|
|
|
sgr_b = 10 * sgr_b + (ch - '0');
|
2013-03-22 10:33:50 +00:00
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
if (len <= *size)
|
|
|
|
return (1);
|
2017-04-18 13:34:04 +00:00
|
|
|
ch = (u_char)buf[(*size)++];
|
|
|
|
if (ch == ';')
|
2013-03-22 10:33:50 +00:00
|
|
|
break;
|
2017-04-18 13:34:04 +00:00
|
|
|
if (ch < '0' || ch > '9')
|
2013-03-22 10:33:50 +00:00
|
|
|
return (-1);
|
2017-04-18 13:34:04 +00:00
|
|
|
x = 10 * x + (ch - '0');
|
2013-03-22 10:33:50 +00:00
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
if (len <= *size)
|
|
|
|
return (1);
|
2017-04-18 13:34:04 +00:00
|
|
|
ch = (u_char)buf[(*size)++];
|
|
|
|
if (ch == 'M' || ch == 'm')
|
2013-03-22 10:33:50 +00:00
|
|
|
break;
|
2017-04-18 13:34:04 +00:00
|
|
|
if (ch < '0' || ch > '9')
|
2013-03-22 10:33:50 +00:00
|
|
|
return (-1);
|
2017-04-18 13:34:04 +00:00
|
|
|
y = 10 * y + (ch - '0');
|
2013-03-22 10:33:50 +00:00
|
|
|
}
|
2017-04-18 13:34:04 +00:00
|
|
|
log_debug("%s: mouse input (SGR): %.*s", c->name, (int)*size,
|
|
|
|
buf);
|
2013-03-22 10:33:50 +00:00
|
|
|
|
|
|
|
/* Check and return the mouse input. */
|
|
|
|
if (x < 1 || y < 1)
|
2023-04-17 17:57:35 +00:00
|
|
|
return (-2);
|
2013-03-22 10:33:50 +00:00
|
|
|
x--;
|
|
|
|
y--;
|
2015-04-19 21:34:21 +00:00
|
|
|
b = sgr_b;
|
|
|
|
|
|
|
|
/* Type is M for press, m for release. */
|
2017-04-18 13:34:04 +00:00
|
|
|
sgr_type = ch;
|
2015-04-19 21:34:21 +00:00
|
|
|
if (sgr_type == 'm')
|
2022-02-16 18:55:05 +00:00
|
|
|
b = 3;
|
2013-03-22 10:33:50 +00:00
|
|
|
|
2014-06-19 07:26:43 +00:00
|
|
|
/*
|
|
|
|
* Some terminals (like PuTTY 0.63) mistakenly send
|
|
|
|
* button-release events for scroll-wheel button-press event.
|
|
|
|
* Discard it before it reaches any program running inside
|
|
|
|
* tmux.
|
|
|
|
*/
|
2022-02-16 18:55:05 +00:00
|
|
|
if (sgr_type == 'm' && MOUSE_WHEEL(sgr_b))
|
2014-06-19 07:26:43 +00:00
|
|
|
return (-2);
|
2013-03-22 10:33:50 +00:00
|
|
|
} else
|
2009-11-26 15:14:03 +00:00
|
|
|
return (-1);
|
2012-10-26 14:35:42 +00:00
|
|
|
|
2015-04-19 21:34:21 +00:00
|
|
|
/* Fill mouse event. */
|
2019-05-03 14:51:30 +00:00
|
|
|
m->lx = tty->mouse_last_x;
|
2013-10-10 11:57:52 +00:00
|
|
|
m->x = x;
|
2019-05-03 14:51:30 +00:00
|
|
|
m->ly = tty->mouse_last_y;
|
2013-10-10 11:57:52 +00:00
|
|
|
m->y = y;
|
2019-05-09 14:09:32 +00:00
|
|
|
m->lb = tty->mouse_last_b;
|
2015-04-19 21:34:21 +00:00
|
|
|
m->b = b;
|
|
|
|
m->sgr_type = sgr_type;
|
|
|
|
m->sgr_b = sgr_b;
|
2012-10-26 14:35:42 +00:00
|
|
|
|
2019-05-03 14:51:30 +00:00
|
|
|
/* Update last mouse state. */
|
|
|
|
tty->mouse_last_x = x;
|
|
|
|
tty->mouse_last_y = y;
|
2019-05-09 14:09:32 +00:00
|
|
|
tty->mouse_last_b = b;
|
2019-05-03 14:51:30 +00:00
|
|
|
|
2009-11-26 15:14:03 +00:00
|
|
|
return (0);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2016-11-15 14:02:32 +00:00
|
|
|
|
2018-10-18 08:04:14 +00:00
|
|
|
/*
|
|
|
|
* Handle OSC 52 clipboard input. Returns 0 for success, -1 for failure, 1 for
|
|
|
|
* partial.
|
|
|
|
*/
|
|
|
|
static int
|
2022-03-08 12:01:19 +00:00
|
|
|
tty_keys_clipboard(struct tty *tty, const char *buf, size_t len, size_t *size)
|
2018-10-18 08:04:14 +00:00
|
|
|
{
|
2022-03-08 12:01:19 +00:00
|
|
|
struct client *c = tty->client;
|
|
|
|
struct window_pane *wp;
|
2023-06-30 13:19:32 +00:00
|
|
|
size_t end, terminator = 0, needed;
|
2022-03-08 12:01:19 +00:00
|
|
|
char *copy, *out;
|
|
|
|
int outlen;
|
|
|
|
u_int i;
|
2018-10-18 08:04:14 +00:00
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
|
2020-05-16 14:16:25 +00:00
|
|
|
/* First five bytes are always \033]52;. */
|
2018-10-18 08:04:14 +00:00
|
|
|
if (buf[0] != '\033')
|
|
|
|
return (-1);
|
|
|
|
if (len == 1)
|
|
|
|
return (1);
|
|
|
|
if (buf[1] != ']')
|
|
|
|
return (-1);
|
|
|
|
if (len == 2)
|
|
|
|
return (1);
|
|
|
|
if (buf[2] != '5')
|
|
|
|
return (-1);
|
|
|
|
if (len == 3)
|
|
|
|
return (1);
|
|
|
|
if (buf[3] != '2')
|
|
|
|
return (-1);
|
|
|
|
if (len == 4)
|
|
|
|
return (1);
|
|
|
|
if (buf[4] != ';')
|
|
|
|
return (-1);
|
|
|
|
if (len == 5)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
/* Find the terminator if any. */
|
|
|
|
for (end = 5; end < len; end++) {
|
|
|
|
if (buf[end] == '\007') {
|
|
|
|
terminator = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (end > 5 && buf[end - 1] == '\033' && buf[end] == '\\') {
|
|
|
|
terminator = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (end == len)
|
|
|
|
return (1);
|
|
|
|
*size = end + terminator;
|
|
|
|
|
|
|
|
/* Skip the initial part. */
|
|
|
|
buf += 5;
|
|
|
|
end -= 5;
|
|
|
|
|
2021-10-21 08:36:51 +00:00
|
|
|
/* Adjust end so that it points to the start of the terminator. */
|
|
|
|
end -= terminator - 1;
|
|
|
|
|
2018-10-18 08:04:14 +00:00
|
|
|
/* Get the second argument. */
|
|
|
|
while (end != 0 && *buf != ';') {
|
|
|
|
buf++;
|
|
|
|
end--;
|
|
|
|
}
|
|
|
|
if (end == 0 || end == 1)
|
|
|
|
return (0);
|
|
|
|
buf++;
|
|
|
|
end--;
|
|
|
|
|
2022-02-15 13:03:02 +00:00
|
|
|
/* If we did not request this, ignore it. */
|
|
|
|
if (~tty->flags & TTY_OSC52QUERY)
|
|
|
|
return (0);
|
|
|
|
tty->flags &= ~TTY_OSC52QUERY;
|
2022-03-08 12:01:19 +00:00
|
|
|
evtimer_del(&tty->clipboard_timer);
|
2022-02-15 13:03:02 +00:00
|
|
|
|
2018-10-18 08:04:14 +00:00
|
|
|
/* It has to be a string so copy it. */
|
|
|
|
copy = xmalloc(end + 1);
|
|
|
|
memcpy(copy, buf, end);
|
|
|
|
copy[end] = '\0';
|
|
|
|
|
|
|
|
/* Convert from base64. */
|
|
|
|
needed = (end / 4) * 3;
|
|
|
|
out = xmalloc(needed);
|
|
|
|
if ((outlen = b64_pton(copy, out, len)) == -1) {
|
|
|
|
free(out);
|
|
|
|
free(copy);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
free(copy);
|
|
|
|
|
2022-03-08 12:01:19 +00:00
|
|
|
/* Create a new paste buffer and forward to panes. */
|
2018-10-18 08:04:14 +00:00
|
|
|
log_debug("%s: %.*s", __func__, outlen, out);
|
2022-03-08 12:01:19 +00:00
|
|
|
if (c->flags & CLIENT_CLIPBOARDBUFFER) {
|
|
|
|
paste_add(NULL, out, outlen);
|
|
|
|
c->flags &= ~CLIENT_CLIPBOARDBUFFER;
|
|
|
|
}
|
|
|
|
for (i = 0; i < c->clipboard_npanes; i++) {
|
|
|
|
wp = window_pane_find_by_id(c->clipboard_panes[i]);
|
|
|
|
if (wp != NULL)
|
|
|
|
input_reply_clipboard(wp->event, out, outlen, "\033\\");
|
|
|
|
}
|
|
|
|
free(c->clipboard_panes);
|
|
|
|
c->clipboard_panes = NULL;
|
|
|
|
c->clipboard_npanes = 0;
|
2018-10-18 08:04:14 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2016-11-15 14:02:32 +00:00
|
|
|
/*
|
2022-11-11 08:37:55 +00:00
|
|
|
* Handle primary device attributes input. Returns 0 for success, -1 for
|
2020-04-16 15:14:25 +00:00
|
|
|
* failure, 1 for partial.
|
2016-11-15 14:02:32 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
tty_keys_device_attributes(struct tty *tty, const char *buf, size_t len,
|
|
|
|
size_t *size)
|
|
|
|
{
|
2020-01-12 21:07:07 +00:00
|
|
|
struct client *c = tty->client;
|
2022-11-11 08:44:11 +00:00
|
|
|
int *features = &c->term_features;
|
2020-01-12 21:07:07 +00:00
|
|
|
u_int i, n = 0;
|
2022-11-11 08:37:55 +00:00
|
|
|
char tmp[128], *endptr, p[32] = { 0 }, *cp, *next;
|
2016-11-15 14:02:32 +00:00
|
|
|
|
|
|
|
*size = 0;
|
2020-01-13 08:12:53 +00:00
|
|
|
if (tty->flags & TTY_HAVEDA)
|
|
|
|
return (-1);
|
2016-11-15 14:02:32 +00:00
|
|
|
|
2022-11-11 08:37:55 +00:00
|
|
|
/* First three bytes are always \033[?. */
|
2016-11-15 14:02:32 +00:00
|
|
|
if (buf[0] != '\033')
|
|
|
|
return (-1);
|
|
|
|
if (len == 1)
|
|
|
|
return (1);
|
|
|
|
if (buf[1] != '[')
|
|
|
|
return (-1);
|
|
|
|
if (len == 2)
|
|
|
|
return (1);
|
2022-11-11 08:37:55 +00:00
|
|
|
if (buf[2] != '?')
|
2016-11-15 14:02:32 +00:00
|
|
|
return (-1);
|
|
|
|
if (len == 3)
|
|
|
|
return (1);
|
|
|
|
|
2023-01-03 11:43:24 +00:00
|
|
|
/* Copy the rest up to a c. */
|
2022-11-11 08:37:55 +00:00
|
|
|
for (i = 0; i < (sizeof tmp); i++) {
|
2016-11-15 14:02:32 +00:00
|
|
|
if (3 + i == len)
|
|
|
|
return (1);
|
2020-05-16 14:16:25 +00:00
|
|
|
if (buf[3 + i] == 'c')
|
|
|
|
break;
|
2016-11-15 14:02:32 +00:00
|
|
|
tmp[i] = buf[3 + i];
|
|
|
|
}
|
2022-11-11 08:37:55 +00:00
|
|
|
if (i == (sizeof tmp))
|
2016-11-15 14:02:32 +00:00
|
|
|
return (-1);
|
|
|
|
tmp[i] = '\0';
|
|
|
|
*size = 4 + i;
|
|
|
|
|
2022-11-11 08:37:55 +00:00
|
|
|
/* Convert all arguments to numbers. */
|
|
|
|
cp = tmp;
|
|
|
|
while ((next = strsep(&cp, ";")) != NULL) {
|
|
|
|
p[n] = strtoul(next, &endptr, 10);
|
|
|
|
if (*endptr != '\0')
|
|
|
|
p[n] = 0;
|
|
|
|
if (++n == nitems(p))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-03-21 11:47:55 +00:00
|
|
|
/* Add terminal features. */
|
2022-11-11 08:37:55 +00:00
|
|
|
switch (p[0]) {
|
2024-03-21 11:47:55 +00:00
|
|
|
case 61: /* level 1 */
|
2023-09-08 06:52:31 +00:00
|
|
|
case 62: /* level 2 */
|
|
|
|
case 63: /* level 3 */
|
2024-03-21 11:47:55 +00:00
|
|
|
case 64: /* level 4 */
|
2023-09-08 06:52:31 +00:00
|
|
|
case 65: /* level 5 */
|
2022-11-11 08:44:11 +00:00
|
|
|
for (i = 1; i < n; i++) {
|
|
|
|
log_debug("%s: DA feature: %d", c->name, p[i]);
|
|
|
|
if (p[i] == 4)
|
|
|
|
tty_add_features(features, "sixel", ",");
|
2024-03-21 11:47:55 +00:00
|
|
|
if (p[i] == 21)
|
|
|
|
tty_add_features(features, "margins", ",");
|
|
|
|
if (p[i] == 28)
|
|
|
|
tty_add_features(features, "rectfill", ",");
|
2022-11-11 08:44:11 +00:00
|
|
|
}
|
2022-11-11 08:37:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
log_debug("%s: received primary DA %.*s", c->name, (int)*size, buf);
|
|
|
|
|
|
|
|
tty_update_features(tty);
|
|
|
|
tty->flags |= TTY_HAVEDA;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle secondary device attributes input. Returns 0 for success, -1 for
|
|
|
|
* failure, 1 for partial.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
tty_keys_device_attributes2(struct tty *tty, const char *buf, size_t len,
|
|
|
|
size_t *size)
|
|
|
|
{
|
|
|
|
struct client *c = tty->client;
|
2022-11-11 08:44:11 +00:00
|
|
|
int *features = &c->term_features;
|
2022-11-11 08:37:55 +00:00
|
|
|
u_int i, n = 0;
|
|
|
|
char tmp[128], *endptr, p[32] = { 0 }, *cp, *next;
|
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
if (tty->flags & TTY_HAVEDA2)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* First three bytes are always \033[>. */
|
|
|
|
if (buf[0] != '\033')
|
|
|
|
return (-1);
|
|
|
|
if (len == 1)
|
|
|
|
return (1);
|
|
|
|
if (buf[1] != '[')
|
|
|
|
return (-1);
|
|
|
|
if (len == 2)
|
|
|
|
return (1);
|
|
|
|
if (buf[2] != '>')
|
|
|
|
return (-1);
|
|
|
|
if (len == 3)
|
|
|
|
return (1);
|
|
|
|
|
2023-01-03 11:43:24 +00:00
|
|
|
/* Copy the rest up to a c. */
|
2022-11-11 08:37:55 +00:00
|
|
|
for (i = 0; i < (sizeof tmp); i++) {
|
|
|
|
if (3 + i == len)
|
|
|
|
return (1);
|
|
|
|
if (buf[3 + i] == 'c')
|
|
|
|
break;
|
|
|
|
tmp[i] = buf[3 + i];
|
|
|
|
}
|
|
|
|
if (i == (sizeof tmp))
|
|
|
|
return (-1);
|
|
|
|
tmp[i] = '\0';
|
|
|
|
*size = 4 + i;
|
2020-08-24 05:23:30 +00:00
|
|
|
|
2020-04-16 15:14:25 +00:00
|
|
|
/* Convert all arguments to numbers. */
|
2019-11-28 10:17:22 +00:00
|
|
|
cp = tmp;
|
|
|
|
while ((next = strsep(&cp, ";")) != NULL) {
|
|
|
|
p[n] = strtoul(next, &endptr, 10);
|
2020-01-12 20:20:20 +00:00
|
|
|
if (*endptr != '\0')
|
2019-11-28 10:17:22 +00:00
|
|
|
p[n] = 0;
|
2022-11-11 08:37:55 +00:00
|
|
|
if (++n == nitems(p))
|
|
|
|
break;
|
2019-11-28 10:17:22 +00:00
|
|
|
}
|
2016-11-15 14:02:32 +00:00
|
|
|
|
2023-09-08 07:05:06 +00:00
|
|
|
/*
|
|
|
|
* Add terminal features. We add DECSLRM and DECFRA for some
|
|
|
|
* identification codes here, notably 64 will catch VT520, even though
|
|
|
|
* we can't use level 5 from DA because of VTE.
|
|
|
|
*/
|
2019-11-28 10:17:22 +00:00
|
|
|
switch (p[0]) {
|
2020-04-16 15:14:25 +00:00
|
|
|
case 'M': /* mintty */
|
2022-11-11 08:44:11 +00:00
|
|
|
tty_default_features(features, "mintty", 0);
|
2020-04-16 15:14:25 +00:00
|
|
|
break;
|
2020-04-20 13:25:36 +00:00
|
|
|
case 'T': /* tmux */
|
2022-11-11 08:44:11 +00:00
|
|
|
tty_default_features(features, "tmux", 0);
|
2020-04-16 15:14:25 +00:00
|
|
|
break;
|
|
|
|
case 'U': /* rxvt-unicode */
|
2022-11-11 08:44:11 +00:00
|
|
|
tty_default_features(features, "rxvt-unicode", 0);
|
2020-04-16 15:14:25 +00:00
|
|
|
break;
|
2016-11-15 14:02:32 +00:00
|
|
|
}
|
2020-04-16 15:14:25 +00:00
|
|
|
log_debug("%s: received secondary DA %.*s", c->name, (int)*size, buf);
|
2020-01-13 08:12:53 +00:00
|
|
|
|
2020-04-20 13:25:36 +00:00
|
|
|
tty_update_features(tty);
|
2022-11-11 08:37:55 +00:00
|
|
|
tty->flags |= TTY_HAVEDA2;
|
2020-01-13 08:12:53 +00:00
|
|
|
|
2016-11-15 14:02:32 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2020-01-12 22:00:20 +00:00
|
|
|
|
|
|
|
/*
|
2020-05-16 14:16:25 +00:00
|
|
|
* Handle extended device attributes input. Returns 0 for success, -1 for
|
|
|
|
* failure, 1 for partial.
|
2020-01-12 22:00:20 +00:00
|
|
|
*/
|
|
|
|
static int
|
2020-05-16 14:16:25 +00:00
|
|
|
tty_keys_extended_device_attributes(struct tty *tty, const char *buf,
|
|
|
|
size_t len, size_t *size)
|
2020-01-12 22:00:20 +00:00
|
|
|
{
|
|
|
|
struct client *c = tty->client;
|
2022-11-11 08:44:11 +00:00
|
|
|
int *features = &c->term_features;
|
2020-01-12 22:00:20 +00:00
|
|
|
u_int i;
|
2020-05-16 14:30:17 +00:00
|
|
|
char tmp[128];
|
2020-01-12 22:00:20 +00:00
|
|
|
|
|
|
|
*size = 0;
|
2020-05-16 14:16:25 +00:00
|
|
|
if (tty->flags & TTY_HAVEXDA)
|
2020-01-13 08:12:53 +00:00
|
|
|
return (-1);
|
2020-01-12 22:00:20 +00:00
|
|
|
|
2020-05-16 14:16:25 +00:00
|
|
|
/* First four bytes are always \033P>|. */
|
2020-01-12 22:00:20 +00:00
|
|
|
if (buf[0] != '\033')
|
|
|
|
return (-1);
|
|
|
|
if (len == 1)
|
|
|
|
return (1);
|
2020-05-16 14:16:25 +00:00
|
|
|
if (buf[1] != 'P')
|
2020-01-12 22:00:20 +00:00
|
|
|
return (-1);
|
|
|
|
if (len == 2)
|
|
|
|
return (1);
|
2020-05-16 14:16:25 +00:00
|
|
|
if (buf[2] != '>')
|
2020-01-13 08:12:53 +00:00
|
|
|
return (-1);
|
|
|
|
if (len == 3)
|
|
|
|
return (1);
|
2020-05-16 14:16:25 +00:00
|
|
|
if (buf[3] != '|')
|
|
|
|
return (-1);
|
|
|
|
if (len == 4)
|
|
|
|
return (1);
|
2020-01-12 22:00:20 +00:00
|
|
|
|
2023-01-03 11:43:24 +00:00
|
|
|
/* Copy the rest up to \033\. */
|
2020-05-16 14:16:25 +00:00
|
|
|
for (i = 0; i < (sizeof tmp) - 1; i++) {
|
|
|
|
if (4 + i == len)
|
2020-01-12 22:00:20 +00:00
|
|
|
return (1);
|
2020-05-16 14:16:25 +00:00
|
|
|
if (buf[4 + i - 1] == '\033' && buf[4 + i] == '\\')
|
|
|
|
break;
|
|
|
|
tmp[i] = buf[4 + i];
|
2020-01-12 22:00:20 +00:00
|
|
|
}
|
|
|
|
if (i == (sizeof tmp) - 1)
|
|
|
|
return (-1);
|
2020-05-16 14:16:25 +00:00
|
|
|
tmp[i - 1] = '\0';
|
|
|
|
*size = 5 + i;
|
2020-01-12 22:00:20 +00:00
|
|
|
|
2020-04-20 13:25:36 +00:00
|
|
|
/* Add terminal features. */
|
2020-05-16 14:30:17 +00:00
|
|
|
if (strncmp(tmp, "iTerm2 ", 7) == 0)
|
2022-11-11 08:44:11 +00:00
|
|
|
tty_default_features(features, "iTerm2", 0);
|
2020-05-16 14:30:17 +00:00
|
|
|
else if (strncmp(tmp, "tmux ", 5) == 0)
|
2022-11-11 08:44:11 +00:00
|
|
|
tty_default_features(features, "tmux", 0);
|
2020-05-16 14:30:17 +00:00
|
|
|
else if (strncmp(tmp, "XTerm(", 6) == 0)
|
2022-11-11 08:44:11 +00:00
|
|
|
tty_default_features(features, "XTerm", 0);
|
2020-05-16 14:30:17 +00:00
|
|
|
else if (strncmp(tmp, "mintty ", 7) == 0)
|
2022-11-11 08:44:11 +00:00
|
|
|
tty_default_features(features, "mintty", 0);
|
2020-05-16 14:16:25 +00:00
|
|
|
log_debug("%s: received extended DA %.*s", c->name, (int)*size, buf);
|
2020-01-13 08:12:53 +00:00
|
|
|
|
2020-05-16 14:30:17 +00:00
|
|
|
free(c->term_type);
|
|
|
|
c->term_type = xstrdup(tmp);
|
|
|
|
|
2020-04-20 13:25:36 +00:00
|
|
|
tty_update_features(tty);
|
2020-05-16 14:16:25 +00:00
|
|
|
tty->flags |= TTY_HAVEXDA;
|
2020-01-13 08:12:53 +00:00
|
|
|
|
2020-01-12 22:00:20 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2023-01-03 11:43:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle foreground or background input. Returns 0 for success, -1 for
|
|
|
|
* failure, 1 for partial.
|
|
|
|
*/
|
2024-06-24 08:30:50 +00:00
|
|
|
int
|
|
|
|
tty_keys_colours(struct tty *tty, const char *buf, size_t len, size_t *size,
|
|
|
|
int *fg, int *bg)
|
2023-01-03 11:43:24 +00:00
|
|
|
{
|
|
|
|
struct client *c = tty->client;
|
|
|
|
u_int i;
|
|
|
|
char tmp[128];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
/* First four bytes are always \033]1 and 0 or 1 and ;. */
|
|
|
|
if (buf[0] != '\033')
|
|
|
|
return (-1);
|
|
|
|
if (len == 1)
|
|
|
|
return (1);
|
|
|
|
if (buf[1] != ']')
|
|
|
|
return (-1);
|
|
|
|
if (len == 2)
|
|
|
|
return (1);
|
|
|
|
if (buf[2] != '1')
|
|
|
|
return (-1);
|
|
|
|
if (len == 3)
|
|
|
|
return (1);
|
|
|
|
if (buf[3] != '0' && buf[3] != '1')
|
|
|
|
return (-1);
|
|
|
|
if (len == 4)
|
|
|
|
return (1);
|
|
|
|
if (buf[4] != ';')
|
|
|
|
return (-1);
|
|
|
|
if (len == 5)
|
|
|
|
return (1);
|
|
|
|
|
2023-01-09 07:57:14 +00:00
|
|
|
/* Copy the rest up to \033\ or \007. */
|
2023-01-03 11:43:24 +00:00
|
|
|
for (i = 0; i < (sizeof tmp) - 1; i++) {
|
|
|
|
if (5 + i == len)
|
|
|
|
return (1);
|
|
|
|
if (buf[5 + i - 1] == '\033' && buf[5 + i] == '\\')
|
|
|
|
break;
|
2023-01-09 07:57:14 +00:00
|
|
|
if (buf[5 + i] == '\007')
|
|
|
|
break;
|
2023-01-03 11:43:24 +00:00
|
|
|
tmp[i] = buf[5 + i];
|
|
|
|
}
|
|
|
|
if (i == (sizeof tmp) - 1)
|
|
|
|
return (-1);
|
2023-01-09 14:12:41 +00:00
|
|
|
if (tmp[i - 1] == '\033')
|
2023-01-09 07:57:14 +00:00
|
|
|
tmp[i - 1] = '\0';
|
2023-01-09 14:12:41 +00:00
|
|
|
else
|
|
|
|
tmp[i] = '\0';
|
|
|
|
*size = 6 + i;
|
2023-01-03 11:43:24 +00:00
|
|
|
|
|
|
|
n = colour_parseX11(tmp);
|
|
|
|
if (n != -1 && buf[3] == '0') {
|
2024-06-24 08:30:50 +00:00
|
|
|
if (c != NULL)
|
|
|
|
log_debug("%s fg is %s", c->name, colour_tostring(n));
|
|
|
|
else
|
|
|
|
log_debug("fg is %s", colour_tostring(n));
|
|
|
|
*fg = n;
|
2023-01-03 11:43:24 +00:00
|
|
|
} else if (n != -1) {
|
2024-06-24 08:30:50 +00:00
|
|
|
if (c != NULL)
|
|
|
|
log_debug("%s bg is %s", c->name, colour_tostring(n));
|
|
|
|
else
|
|
|
|
log_debug("bg is %s", colour_tostring(n));
|
|
|
|
*bg = n;
|
2023-01-03 11:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|