input-keys.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "tmux.h"
  21. /*
  22. * This file is rather misleadingly named, it contains the code which takes a
  23. * key code and translates it into something suitable to be sent to the
  24. * application running in a pane (similar to input.c does in the other
  25. * direction with output).
  26. */
  27. void input_key_mouse(struct window_pane *, struct mouse_event *);
  28. struct input_key_ent {
  29. key_code key;
  30. const char *data;
  31. int flags;
  32. #define INPUTKEY_KEYPAD 0x1 /* keypad key */
  33. #define INPUTKEY_CURSOR 0x2 /* cursor key */
  34. };
  35. const struct input_key_ent input_keys[] = {
  36. /* Backspace key. */
  37. { KEYC_BSPACE, "\177", 0 },
  38. /* Function keys. */
  39. { KEYC_F1, "\033OP", 0 },
  40. { KEYC_F2, "\033OQ", 0 },
  41. { KEYC_F3, "\033OR", 0 },
  42. { KEYC_F4, "\033OS", 0 },
  43. { KEYC_F5, "\033[15~", 0 },
  44. { KEYC_F6, "\033[17~", 0 },
  45. { KEYC_F7, "\033[18~", 0 },
  46. { KEYC_F8, "\033[19~", 0 },
  47. { KEYC_F9, "\033[20~", 0 },
  48. { KEYC_F10, "\033[21~", 0 },
  49. { KEYC_F11, "\033[23~", 0 },
  50. { KEYC_F12, "\033[24~", 0 },
  51. { KEYC_F1|KEYC_SHIFT, "\033[25~", 0 },
  52. { KEYC_F2|KEYC_SHIFT, "\033[26~", 0 },
  53. { KEYC_F3|KEYC_SHIFT, "\033[28~", 0 },
  54. { KEYC_F4|KEYC_SHIFT, "\033[29~", 0 },
  55. { KEYC_F5|KEYC_SHIFT, "\033[31~", 0 },
  56. { KEYC_F6|KEYC_SHIFT, "\033[32~", 0 },
  57. { KEYC_F7|KEYC_SHIFT, "\033[33~", 0 },
  58. { KEYC_F8|KEYC_SHIFT, "\033[34~", 0 },
  59. { KEYC_IC, "\033[2~", 0 },
  60. { KEYC_DC, "\033[3~", 0 },
  61. { KEYC_HOME, "\033[1~", 0 },
  62. { KEYC_END, "\033[4~", 0 },
  63. { KEYC_NPAGE, "\033[6~", 0 },
  64. { KEYC_PPAGE, "\033[5~", 0 },
  65. { KEYC_BTAB, "\033[Z", 0 },
  66. /*
  67. * Arrow keys. Cursor versions must come first. The codes are toggled
  68. * between CSI and SS3 versions when ctrl is pressed.
  69. */
  70. { KEYC_UP|KEYC_CTRL, "\033[A", INPUTKEY_CURSOR },
  71. { KEYC_DOWN|KEYC_CTRL, "\033[B", INPUTKEY_CURSOR },
  72. { KEYC_RIGHT|KEYC_CTRL, "\033[C", INPUTKEY_CURSOR },
  73. { KEYC_LEFT|KEYC_CTRL, "\033[D", INPUTKEY_CURSOR },
  74. { KEYC_UP, "\033OA", INPUTKEY_CURSOR },
  75. { KEYC_DOWN, "\033OB", INPUTKEY_CURSOR },
  76. { KEYC_RIGHT, "\033OC", INPUTKEY_CURSOR },
  77. { KEYC_LEFT, "\033OD", INPUTKEY_CURSOR },
  78. { KEYC_UP|KEYC_CTRL, "\033OA", 0 },
  79. { KEYC_DOWN|KEYC_CTRL, "\033OB", 0 },
  80. { KEYC_RIGHT|KEYC_CTRL, "\033OC", 0 },
  81. { KEYC_LEFT|KEYC_CTRL, "\033OD", 0 },
  82. { KEYC_UP, "\033[A", 0 },
  83. { KEYC_DOWN, "\033[B", 0 },
  84. { KEYC_RIGHT, "\033[C", 0 },
  85. { KEYC_LEFT, "\033[D", 0 },
  86. /* Keypad keys. Keypad versions must come first. */
  87. { KEYC_KP_SLASH, "\033Oo", INPUTKEY_KEYPAD },
  88. { KEYC_KP_STAR, "\033Oj", INPUTKEY_KEYPAD },
  89. { KEYC_KP_MINUS, "\033Om", INPUTKEY_KEYPAD },
  90. { KEYC_KP_SEVEN, "\033Ow", INPUTKEY_KEYPAD },
  91. { KEYC_KP_EIGHT, "\033Ox", INPUTKEY_KEYPAD },
  92. { KEYC_KP_NINE, "\033Oy", INPUTKEY_KEYPAD },
  93. { KEYC_KP_PLUS, "\033Ok", INPUTKEY_KEYPAD },
  94. { KEYC_KP_FOUR, "\033Ot", INPUTKEY_KEYPAD },
  95. { KEYC_KP_FIVE, "\033Ou", INPUTKEY_KEYPAD },
  96. { KEYC_KP_SIX, "\033Ov", INPUTKEY_KEYPAD },
  97. { KEYC_KP_ONE, "\033Oq", INPUTKEY_KEYPAD },
  98. { KEYC_KP_TWO, "\033Or", INPUTKEY_KEYPAD },
  99. { KEYC_KP_THREE, "\033Os", INPUTKEY_KEYPAD },
  100. { KEYC_KP_ENTER, "\033OM", INPUTKEY_KEYPAD },
  101. { KEYC_KP_ZERO, "\033Op", INPUTKEY_KEYPAD },
  102. { KEYC_KP_PERIOD, "\033On", INPUTKEY_KEYPAD },
  103. { KEYC_KP_SLASH, "/", 0 },
  104. { KEYC_KP_STAR, "*", 0 },
  105. { KEYC_KP_MINUS, "-", 0 },
  106. { KEYC_KP_SEVEN, "7", 0 },
  107. { KEYC_KP_EIGHT, "8", 0 },
  108. { KEYC_KP_NINE, "9", 0 },
  109. { KEYC_KP_PLUS, "+", 0 },
  110. { KEYC_KP_FOUR, "4", 0 },
  111. { KEYC_KP_FIVE, "5", 0 },
  112. { KEYC_KP_SIX, "6", 0 },
  113. { KEYC_KP_ONE, "1", 0 },
  114. { KEYC_KP_TWO, "2", 0 },
  115. { KEYC_KP_THREE, "3", 0 },
  116. { KEYC_KP_ENTER, "\n", 0 },
  117. { KEYC_KP_ZERO, "0", 0 },
  118. { KEYC_KP_PERIOD, ".", 0 },
  119. };
  120. /* Split a character into two UTF-8 bytes. */
  121. static size_t
  122. input_split2(u_int c, u_char *dst)
  123. {
  124. if (c > 0x7f) {
  125. dst[0] = (c >> 6) | 0xc0;
  126. dst[1] = (c & 0x3f) | 0x80;
  127. return (2);
  128. }
  129. dst[0] = c;
  130. return (1);
  131. }
  132. /* Translate a key code into an output key sequence. */
  133. void
  134. input_key(struct window_pane *wp, key_code key, struct mouse_event *m)
  135. {
  136. const struct input_key_ent *ike;
  137. u_int i;
  138. size_t dlen;
  139. char *out;
  140. key_code justkey;
  141. struct utf8_data ud;
  142. log_debug("writing key 0x%llx (%s) to %%%u", key,
  143. key_string_lookup_key(key), wp->id);
  144. /* If this is a mouse key, pass off to mouse function. */
  145. if (KEYC_IS_MOUSE(key)) {
  146. if (m != NULL && m->wp != -1 && (u_int)m->wp == wp->id)
  147. input_key_mouse(wp, m);
  148. return;
  149. }
  150. /*
  151. * If this is a normal 7-bit key, just send it, with a leading escape
  152. * if necessary. If it is a UTF-8 key, split it and send it.
  153. */
  154. justkey = (key & ~KEYC_ESCAPE);
  155. if (justkey <= 0x7f) {
  156. if (key & KEYC_ESCAPE)
  157. bufferevent_write(wp->event, "\033", 1);
  158. ud.data[0] = justkey;
  159. bufferevent_write(wp->event, &ud.data[0], 1);
  160. return;
  161. }
  162. if (justkey > 0x7f && justkey < KEYC_BASE) {
  163. if (utf8_split(justkey, &ud) != UTF8_DONE)
  164. return;
  165. if (key & KEYC_ESCAPE)
  166. bufferevent_write(wp->event, "\033", 1);
  167. bufferevent_write(wp->event, ud.data, ud.size);
  168. return;
  169. }
  170. /*
  171. * Then try to look this up as an xterm key, if the flag to output them
  172. * is set.
  173. */
  174. if (options_get_number(wp->window->options, "xterm-keys")) {
  175. if ((out = xterm_keys_lookup(key)) != NULL) {
  176. bufferevent_write(wp->event, out, strlen(out));
  177. free(out);
  178. return;
  179. }
  180. }
  181. /* Otherwise look the key up in the table. */
  182. for (i = 0; i < nitems(input_keys); i++) {
  183. ike = &input_keys[i];
  184. if ((ike->flags & INPUTKEY_KEYPAD) &&
  185. !(wp->screen->mode & MODE_KKEYPAD))
  186. continue;
  187. if ((ike->flags & INPUTKEY_CURSOR) &&
  188. !(wp->screen->mode & MODE_KCURSOR))
  189. continue;
  190. if ((key & KEYC_ESCAPE) && (ike->key | KEYC_ESCAPE) == key)
  191. break;
  192. if (ike->key == key)
  193. break;
  194. }
  195. if (i == nitems(input_keys)) {
  196. log_debug("key 0x%llx missing", key);
  197. return;
  198. }
  199. dlen = strlen(ike->data);
  200. log_debug("found key 0x%llx: \"%s\"", key, ike->data);
  201. /* Prefix a \033 for escape. */
  202. if (key & KEYC_ESCAPE)
  203. bufferevent_write(wp->event, "\033", 1);
  204. bufferevent_write(wp->event, ike->data, dlen);
  205. }
  206. /* Translate mouse and output. */
  207. void
  208. input_key_mouse(struct window_pane *wp, struct mouse_event *m)
  209. {
  210. char buf[40];
  211. size_t len;
  212. u_int x, y;
  213. if ((wp->screen->mode & ALL_MOUSE_MODES) == 0)
  214. return;
  215. if (!window_pane_visible(wp))
  216. return;
  217. if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
  218. return;
  219. /* If this pane is not in button mode, discard motion events. */
  220. if (!(wp->screen->mode & MODE_MOUSE_BUTTON) && (m->b & MOUSE_MASK_DRAG))
  221. return;
  222. /*
  223. * Use the SGR (1006) extension only if the application requested it
  224. * and the underlying terminal also sent the event in this format (this
  225. * is because an old style mouse release event cannot be converted into
  226. * the new SGR format, since the released button is unknown). Otherwise
  227. * pretend that tmux doesn't speak this extension, and fall back to the
  228. * UTF-8 (1005) extension if the application requested, or to the
  229. * legacy format.
  230. */
  231. if (m->sgr_type != ' ' && (wp->screen->mode & MODE_MOUSE_SGR)) {
  232. len = xsnprintf(buf, sizeof buf, "\033[<%u;%u;%u%c",
  233. m->sgr_b, x + 1, y + 1, m->sgr_type);
  234. } else if (wp->screen->mode & MODE_MOUSE_UTF8) {
  235. if (m->b > 0x7ff - 32 || x > 0x7ff - 33 || y > 0x7ff - 33)
  236. return;
  237. len = xsnprintf(buf, sizeof buf, "\033[M");
  238. len += input_split2(m->b + 32, &buf[len]);
  239. len += input_split2(x + 33, &buf[len]);
  240. len += input_split2(y + 33, &buf[len]);
  241. } else {
  242. if (m->b > 223)
  243. return;
  244. len = xsnprintf(buf, sizeof buf, "\033[M");
  245. buf[len++] = m->b + 32;
  246. buf[len++] = x + 33;
  247. buf[len++] = y + 33;
  248. }
  249. log_debug("writing mouse %.*s to %%%u", (int)len, buf, wp->id);
  250. bufferevent_write(wp->event, buf, len);
  251. }