xterm-keys.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 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 <string.h>
  19. #include "tmux.h"
  20. /*
  21. * xterm-style function keys append one of the following values before the last
  22. * character:
  23. *
  24. * 2 Shift
  25. * 3 Alt
  26. * 4 Shift + Alt
  27. * 5 Ctrl
  28. * 6 Shift + Ctrl
  29. * 7 Alt + Ctrl
  30. * 8 Shift + Alt + Ctrl
  31. *
  32. * Rather than parsing them, just match against a table.
  33. *
  34. * There are three forms for F1-F4 (\\033O_P and \\033O1;_P and \\033[1;_P).
  35. * We accept any but always output the latter (it comes first in the table).
  36. */
  37. int xterm_keys_match(const char *, const char *, size_t, size_t *,
  38. key_code *);
  39. int xterm_keys_modifiers(const char *, size_t, size_t *, key_code *);
  40. struct xterm_keys_entry {
  41. key_code key;
  42. const char *template;
  43. };
  44. const struct xterm_keys_entry xterm_keys_table[] = {
  45. { KEYC_F1, "\033[1;_P" },
  46. { KEYC_F1, "\033O1;_P" },
  47. { KEYC_F1, "\033O_P" },
  48. { KEYC_F2, "\033[1;_Q" },
  49. { KEYC_F2, "\033O1;_Q" },
  50. { KEYC_F2, "\033O_Q" },
  51. { KEYC_F3, "\033[1;_R" },
  52. { KEYC_F3, "\033O1;_R" },
  53. { KEYC_F3, "\033O_R" },
  54. { KEYC_F4, "\033[1;_S" },
  55. { KEYC_F4, "\033O1;_S" },
  56. { KEYC_F4, "\033O_S" },
  57. { KEYC_F5, "\033[15;_~" },
  58. { KEYC_F6, "\033[17;_~" },
  59. { KEYC_F7, "\033[18;_~" },
  60. { KEYC_F8, "\033[19;_~" },
  61. { KEYC_F9, "\033[20;_~" },
  62. { KEYC_F10, "\033[21;_~" },
  63. { KEYC_F11, "\033[23;_~" },
  64. { KEYC_F12, "\033[24;_~" },
  65. { KEYC_UP, "\033[1;_A" },
  66. { KEYC_DOWN, "\033[1;_B" },
  67. { KEYC_RIGHT, "\033[1;_C" },
  68. { KEYC_LEFT, "\033[1;_D" },
  69. { KEYC_HOME, "\033[1;_H" },
  70. { KEYC_END, "\033[1;_F" },
  71. { KEYC_PPAGE, "\033[5;_~" },
  72. { KEYC_NPAGE, "\033[6;_~" },
  73. { KEYC_IC, "\033[2;_~" },
  74. { KEYC_DC, "\033[3;_~" },
  75. { '!', "\033[27;_;33~" },
  76. { '#', "\033[27;_;35~" },
  77. { '(', "\033[27;_;40~" },
  78. { ')', "\033[27;_;41~" },
  79. { '+', "\033[27;_;43~" },
  80. { ',', "\033[27;_;44~" },
  81. { '-', "\033[27;_;45~" },
  82. { '.', "\033[27;_;46~" },
  83. { '0', "\033[27;_;48~" },
  84. { '1', "\033[27;_;49~" },
  85. { '2', "\033[27;_;50~" },
  86. { '3', "\033[27;_;51~" },
  87. { '4', "\033[27;_;52~" },
  88. { '5', "\033[27;_;53~" },
  89. { '6', "\033[27;_;54~" },
  90. { '7', "\033[27;_;55~" },
  91. { '8', "\033[27;_;56~" },
  92. { '9', "\033[27;_;57~" },
  93. { ':', "\033[27;_;58~" },
  94. { ';', "\033[27;_;59~" },
  95. { '<', "\033[27;_;60~" },
  96. { '=', "\033[27;_;61~" },
  97. { '>', "\033[27;_;62~" },
  98. { '?', "\033[27;_;63~" },
  99. { '\'', "\033[27;_;39~" },
  100. { '\r', "\033[27;_;13~" },
  101. { '\t', "\033[27;_;9~" },
  102. };
  103. /*
  104. * Match key against buffer, treating _ as a wildcard. Return -1 for no match,
  105. * 0 for match, 1 if the end of the buffer is reached (need more data).
  106. */
  107. int
  108. xterm_keys_match(const char *template, const char *buf, size_t len,
  109. size_t *size, key_code *modifiers)
  110. {
  111. size_t pos;
  112. int retval;
  113. *modifiers = 0;
  114. if (len == 0)
  115. return (0);
  116. pos = 0;
  117. do {
  118. if (*template == '_') {
  119. retval = xterm_keys_modifiers(buf, len, &pos,
  120. modifiers);
  121. if (retval != 0)
  122. return (retval);
  123. continue;
  124. }
  125. if (buf[pos] != *template)
  126. return (-1);
  127. pos++;
  128. } while (*++template != '\0' && pos != len);
  129. if (*template != '\0') /* partial */
  130. return (1);
  131. *size = pos;
  132. return (0);
  133. }
  134. /* Find modifiers from buffer. */
  135. int
  136. xterm_keys_modifiers(const char *buf, size_t len, size_t *pos,
  137. key_code *modifiers)
  138. {
  139. u_int flags;
  140. if (len - *pos < 2)
  141. return (1);
  142. if (buf[*pos] < '0' || buf[*pos] > '9')
  143. return (-1);
  144. flags = buf[(*pos)++] - '0';
  145. if (buf[*pos] >= '0' && buf[*pos] <= '9')
  146. flags = (flags * 10) + (buf[(*pos)++] - '0');
  147. flags -= 1;
  148. *modifiers = 0;
  149. if (flags & 1)
  150. *modifiers |= KEYC_SHIFT;
  151. if (flags & 2)
  152. *modifiers |= KEYC_ESCAPE;
  153. if (flags & 4)
  154. *modifiers |= KEYC_CTRL;
  155. if (flags & 8)
  156. *modifiers |= KEYC_ESCAPE;
  157. return (0);
  158. }
  159. /*
  160. * Lookup key from a buffer against the table. Returns 0 for found (and the
  161. * key), -1 for not found, 1 for partial match.
  162. */
  163. int
  164. xterm_keys_find(const char *buf, size_t len, size_t *size, key_code *key)
  165. {
  166. const struct xterm_keys_entry *entry;
  167. u_int i;
  168. int matched;
  169. key_code modifiers;
  170. for (i = 0; i < nitems(xterm_keys_table); i++) {
  171. entry = &xterm_keys_table[i];
  172. matched = xterm_keys_match(entry->template, buf, len, size,
  173. &modifiers);
  174. if (matched == -1)
  175. continue;
  176. if (matched == 0)
  177. *key = entry->key | modifiers;
  178. return (matched);
  179. }
  180. return (-1);
  181. }
  182. /* Lookup a key number from the table. */
  183. char *
  184. xterm_keys_lookup(key_code key)
  185. {
  186. const struct xterm_keys_entry *entry;
  187. u_int i;
  188. key_code modifiers;
  189. char *out;
  190. modifiers = 1;
  191. if (key & KEYC_SHIFT)
  192. modifiers += 1;
  193. if (key & KEYC_ESCAPE)
  194. modifiers += 2;
  195. if (key & KEYC_CTRL)
  196. modifiers += 4;
  197. /*
  198. * If the key has no modifiers, return NULL and let it fall through to
  199. * the normal lookup.
  200. */
  201. if (modifiers == 1)
  202. return (NULL);
  203. /* Otherwise, find the key in the table. */
  204. key &= ~(KEYC_SHIFT|KEYC_ESCAPE|KEYC_CTRL);
  205. for (i = 0; i < nitems(xterm_keys_table); i++) {
  206. entry = &xterm_keys_table[i];
  207. if (key == entry->key)
  208. break;
  209. }
  210. if (i == nitems(xterm_keys_table))
  211. return (NULL);
  212. /* Copy the template and replace the modifier. */
  213. out = xstrdup(entry->template);
  214. out[strcspn(out, "_")] = '0' + modifiers;
  215. return (out);
  216. }