Annotation of src/usr.bin/tmux/input-keys.c, Revision 1.75
1.75 ! nicm 1: /* $OpenBSD: input-keys.c,v 1.74 2020/05/16 16:35:13 nicm Exp $ */
1.1 nicm 2:
3: /*
1.53 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <stdint.h>
22: #include <stdlib.h>
23: #include <string.h>
24:
25: #include "tmux.h"
26:
1.8 nicm 27: /*
28: * This file is rather misleadingly named, it contains the code which takes a
29: * key code and translates it into something suitable to be sent to the
30: * application running in a pane (similar to input.c does in the other
31: * direction with output).
32: */
33:
1.56 nicm 34: static void input_key_mouse(struct window_pane *, struct mouse_event *);
1.42 nicm 35:
1.72 nicm 36: /* Entry in the key tree. */
37: struct input_key_entry {
38: key_code key;
39: const char *data;
40:
41: RB_ENTRY(input_key_entry) entry;
1.1 nicm 42: };
1.72 nicm 43: RB_HEAD(input_key_tree, input_key_entry);
44:
45: /* Tree of input keys. */
46: static int input_key_cmp(struct input_key_entry *,
47: struct input_key_entry *);
48: RB_GENERATE_STATIC(input_key_tree, input_key_entry, entry, input_key_cmp);
49: struct input_key_tree input_key_tree = RB_INITIALIZER(&input_key_tree);
1.1 nicm 50:
1.72 nicm 51: /* List of default keys, the tree is built from this. */
52: static struct input_key_entry input_key_defaults[] = {
1.61 nicm 53: /* Paste keys. */
1.72 nicm 54: { .key = KEYC_PASTE_START,
55: .data = "\033[200~"
56: },
57: { .key = KEYC_PASTE_END,
58: .data = "\033[201~"
59: },
1.3 nicm 60:
1.1 nicm 61: /* Function keys. */
1.72 nicm 62: { .key = KEYC_F1,
63: .data = "\033OP"
64: },
65: { .key = KEYC_F2,
66: .data = "\033OQ"
67: },
68: { .key = KEYC_F3,
69: .data = "\033OR"
70: },
71: { .key = KEYC_F4,
72: .data = "\033OS"
73: },
74: { .key = KEYC_F5,
75: .data = "\033[15~"
76: },
77: { .key = KEYC_F6,
78: .data = "\033[17~"
79: },
80: { .key = KEYC_F7,
81: .data = "\033[18~"
82: },
83: { .key = KEYC_F8,
84: .data = "\033[19~"
85: },
86: { .key = KEYC_F9,
87: .data = "\033[20~"
88: },
89: { .key = KEYC_F10,
90: .data = "\033[21~"
91: },
92: { .key = KEYC_F11,
93: .data = "\033[23~"
94: },
95: { .key = KEYC_F12,
96: .data = "\033[24~"
97: },
98: { .key = KEYC_F1|KEYC_SHIFT,
99: .data = "\033[25~"
100: },
101: { .key = KEYC_F2|KEYC_SHIFT,
102: .data = "\033[26~"
103: },
104: { .key = KEYC_F3|KEYC_SHIFT,
105: .data = "\033[28~"
106: },
107: { .key = KEYC_F4|KEYC_SHIFT,
108: .data = "\033[29~"
109: },
110: { .key = KEYC_F5|KEYC_SHIFT,
111: .data = "\033[31~"
112: },
113: { .key = KEYC_F6|KEYC_SHIFT,
114: .data = "\033[32~"
115: },
116: { .key = KEYC_F7|KEYC_SHIFT,
117: .data = "\033[33~"
118: },
119: { .key = KEYC_F8|KEYC_SHIFT,
120: .data = "\033[34~"
121: },
122: { .key = KEYC_IC,
123: .data = "\033[2~"
124: },
125: { .key = KEYC_DC,
126: .data = "\033[3~"
127: },
128: { .key = KEYC_HOME,
129: .data = "\033[1~"
130: },
131: { .key = KEYC_END,
132: .data = "\033[4~"
133: },
134: { .key = KEYC_NPAGE,
135: .data = "\033[6~"
136: },
137: { .key = KEYC_PPAGE,
138: .data = "\033[5~"
139: },
140: { .key = KEYC_BTAB,
141: .data = "\033[Z"
142: },
143:
144: /* Arrow keys. */
145: { .key = KEYC_UP|KEYC_CURSOR,
146: .data = "\033OA"
147: },
148: { .key = KEYC_DOWN|KEYC_CURSOR,
149: .data = "\033OB"
150: },
151: { .key = KEYC_RIGHT|KEYC_CURSOR,
152: .data = "\033OC"
153: },
154: { .key = KEYC_LEFT|KEYC_CURSOR,
155: .data = "\033OD"
156: },
157: { .key = KEYC_UP,
158: .data = "\033[A"
159: },
160: { .key = KEYC_DOWN,
161: .data = "\033[B"
162: },
163: { .key = KEYC_RIGHT,
164: .data = "\033[C"
165: },
166: { .key = KEYC_LEFT,
167: .data = "\033[D"
168: },
169:
170: /* Keypad keys. */
171: { .key = KEYC_KP_SLASH|KEYC_KEYPAD,
172: .data = "\033Oo"
173: },
174: { .key = KEYC_KP_STAR|KEYC_KEYPAD,
175: .data = "\033Oj"
176: },
177: { .key = KEYC_KP_MINUS|KEYC_KEYPAD,
178: .data = "\033Om"
179: },
180: { .key = KEYC_KP_SEVEN|KEYC_KEYPAD,
181: .data = "\033Ow"
182: },
183: { .key = KEYC_KP_EIGHT|KEYC_KEYPAD,
184: .data = "\033Ox"
185: },
186: { .key = KEYC_KP_NINE|KEYC_KEYPAD,
187: .data = "\033Oy"
188: },
189: { .key = KEYC_KP_PLUS|KEYC_KEYPAD,
190: .data = "\033Ok"
191: },
192: { .key = KEYC_KP_FOUR|KEYC_KEYPAD,
193: .data = "\033Ot"
194: },
195: { .key = KEYC_KP_FIVE|KEYC_KEYPAD,
196: .data = "\033Ou"
197: },
198: { .key = KEYC_KP_SIX|KEYC_KEYPAD,
199: .data = "\033Ov"
200: },
201: { .key = KEYC_KP_ONE|KEYC_KEYPAD,
202: .data = "\033Oq"
203: },
204: { .key = KEYC_KP_TWO|KEYC_KEYPAD,
205: .data = "\033Or"
206: },
207: { .key = KEYC_KP_THREE|KEYC_KEYPAD,
208: .data = "\033Os"
209: },
210: { .key = KEYC_KP_ENTER|KEYC_KEYPAD,
211: .data = "\033OM"
212: },
213: { .key = KEYC_KP_ZERO|KEYC_KEYPAD,
214: .data = "\033Op"
215: },
216: { .key = KEYC_KP_PERIOD|KEYC_KEYPAD,
217: .data = "\033On"
218: },
219: { .key = KEYC_KP_SLASH,
220: .data = "/"
221: },
222: { .key = KEYC_KP_STAR,
223: .data = "*"
224: },
225: { .key = KEYC_KP_MINUS,
226: .data = "-"
227: },
228: { .key = KEYC_KP_SEVEN,
229: .data = "7"
230: },
231: { .key = KEYC_KP_EIGHT,
232: .data = "8"
233: },
234: { .key = KEYC_KP_NINE,
235: .data = "9"
236: },
237: { .key = KEYC_KP_PLUS,
238: .data = "+"
239: },
240: { .key = KEYC_KP_FOUR,
241: .data = "4"
242: },
243: { .key = KEYC_KP_FIVE,
244: .data = "5"
245: },
246: { .key = KEYC_KP_SIX,
247: .data = "6"
248: },
249: { .key = KEYC_KP_ONE,
250: .data = "1"
251: },
252: { .key = KEYC_KP_TWO,
253: .data = "2"
254: },
255: { .key = KEYC_KP_THREE,
256: .data = "3"
257: },
258: { .key = KEYC_KP_ENTER,
259: .data = "\n"
260: },
261: { .key = KEYC_KP_ZERO,
262: .data = "0"
263: },
264: { .key = KEYC_KP_PERIOD,
265: .data = "."
266: },
267:
268: /* Keys with an embedded modifier. */
1.74 nicm 269: { .key = KEYC_F1|KEYC_BUILD_MODIFIERS,
1.72 nicm 270: .data = "\033[1;_P"
271: },
1.74 nicm 272: { .key = KEYC_F2|KEYC_BUILD_MODIFIERS,
1.72 nicm 273: .data = "\033[1;_Q"
274: },
1.74 nicm 275: { .key = KEYC_F3|KEYC_BUILD_MODIFIERS,
1.72 nicm 276: .data = "\033[1;_R"
277: },
1.74 nicm 278: { .key = KEYC_F4|KEYC_BUILD_MODIFIERS,
1.72 nicm 279: .data = "\033[1;_S"
280: },
1.74 nicm 281: { .key = KEYC_F5|KEYC_BUILD_MODIFIERS,
1.72 nicm 282: .data = "\033[15;_~"
283: },
1.74 nicm 284: { .key = KEYC_F6|KEYC_BUILD_MODIFIERS,
1.72 nicm 285: .data = "\033[17;_~"
286: },
1.74 nicm 287: { .key = KEYC_F7|KEYC_BUILD_MODIFIERS,
1.72 nicm 288: .data = "\033[18;_~"
289: },
1.74 nicm 290: { .key = KEYC_F8|KEYC_BUILD_MODIFIERS,
1.72 nicm 291: .data = "\033[19;_~"
292: },
1.74 nicm 293: { .key = KEYC_F9|KEYC_BUILD_MODIFIERS,
1.72 nicm 294: .data = "\033[20;_~"
295: },
1.74 nicm 296: { .key = KEYC_F10|KEYC_BUILD_MODIFIERS,
1.72 nicm 297: .data = "\033[21;_~"
298: },
1.74 nicm 299: { .key = KEYC_F11|KEYC_BUILD_MODIFIERS,
1.72 nicm 300: .data = "\033[23;_~"
301: },
1.74 nicm 302: { .key = KEYC_F12|KEYC_BUILD_MODIFIERS,
1.72 nicm 303: .data = "\033[24;_~"
304: },
1.74 nicm 305: { .key = KEYC_UP|KEYC_BUILD_MODIFIERS,
1.72 nicm 306: .data = "\033[1;_A"
307: },
1.74 nicm 308: { .key = KEYC_DOWN|KEYC_BUILD_MODIFIERS,
1.72 nicm 309: .data = "\033[1;_B"
310: },
1.74 nicm 311: { .key = KEYC_RIGHT|KEYC_BUILD_MODIFIERS,
1.72 nicm 312: .data = "\033[1;_C"
313: },
1.74 nicm 314: { .key = KEYC_LEFT|KEYC_BUILD_MODIFIERS,
1.72 nicm 315: .data = "\033[1;_D"
316: },
1.74 nicm 317: { .key = KEYC_HOME|KEYC_BUILD_MODIFIERS,
1.72 nicm 318: .data = "\033[1;_H"
319: },
1.74 nicm 320: { .key = KEYC_END|KEYC_BUILD_MODIFIERS,
1.72 nicm 321: .data = "\033[1;_F"
322: },
1.74 nicm 323: { .key = KEYC_PPAGE|KEYC_BUILD_MODIFIERS,
1.72 nicm 324: .data = "\033[5;_~"
325: },
1.74 nicm 326: { .key = KEYC_NPAGE|KEYC_BUILD_MODIFIERS,
1.72 nicm 327: .data = "\033[6;_~"
328: },
1.74 nicm 329: { .key = KEYC_IC|KEYC_BUILD_MODIFIERS,
1.72 nicm 330: .data = "\033[2;_~"
331: },
1.74 nicm 332: { .key = KEYC_DC|KEYC_BUILD_MODIFIERS,
1.72 nicm 333: .data = "\033[3;_~" }
334: };
335: static const key_code input_key_modifiers[] = {
336: 0,
337: 0,
1.74 nicm 338: KEYC_SHIFT,
339: KEYC_META|KEYC_IMPLIED_META,
340: KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META,
341: KEYC_CTRL,
342: KEYC_SHIFT|KEYC_CTRL,
343: KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
344: KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL
1.72 nicm 345: };
1.7 nicm 346:
1.72 nicm 347: /* Input key comparison function. */
348: static int
349: input_key_cmp(struct input_key_entry *ike1, struct input_key_entry *ike2)
350: {
351: if (ike1->key < ike2->key)
352: return (-1);
353: if (ike1->key > ike2->key)
354: return (1);
355: return (0);
356: }
1.1 nicm 357:
1.74 nicm 358: /* Look for key in tree. */
359: static struct input_key_entry *
360: input_key_get (key_code key)
361: {
362: struct input_key_entry entry = { .key = key };
363:
364: return (RB_FIND(input_key_tree, &input_key_tree, &entry));
365: }
366:
1.54 nicm 367: /* Split a character into two UTF-8 bytes. */
368: static size_t
1.72 nicm 369: input_key_split2(u_int c, u_char *dst)
1.54 nicm 370: {
371: if (c > 0x7f) {
372: dst[0] = (c >> 6) | 0xc0;
373: dst[1] = (c & 0x3f) | 0x80;
374: return (2);
375: }
376: dst[0] = c;
377: return (1);
378: }
379:
1.72 nicm 380: /* Build input key tree. */
381: void
382: input_key_build(void)
383: {
384: struct input_key_entry *ike, *new;
385: u_int i, j;
386: char *data;
1.74 nicm 387: key_code key;
1.72 nicm 388:
389: for (i = 0; i < nitems(input_key_defaults); i++) {
390: ike = &input_key_defaults[i];
1.74 nicm 391: if (~ike->key & KEYC_BUILD_MODIFIERS) {
1.72 nicm 392: RB_INSERT(input_key_tree, &input_key_tree, ike);
393: continue;
394: }
395:
396: for (j = 2; j < nitems(input_key_modifiers); j++) {
1.74 nicm 397: key = (ike->key & ~KEYC_BUILD_MODIFIERS);
1.72 nicm 398: data = xstrdup(ike->data);
399: data[strcspn(data, "_")] = '0' + j;
400:
401: new = xcalloc(1, sizeof *new);
1.74 nicm 402: new->key = key|input_key_modifiers[j];
1.72 nicm 403: new->data = data;
404: RB_INSERT(input_key_tree, &input_key_tree, new);
405: }
406: }
407:
408: RB_FOREACH(ike, input_key_tree, &input_key_tree) {
409: log_debug("%s: 0x%llx (%s) is %s", __func__, ike->key,
1.74 nicm 410: key_string_lookup_key(ike->key, 1), ike->data);
1.72 nicm 411: }
412: }
413:
1.69 nicm 414: /* Translate a key code into an output key sequence for a pane. */
415: int
416: input_key_pane(struct window_pane *wp, key_code key, struct mouse_event *m)
417: {
1.72 nicm 418: if (log_get_level() != 0) {
419: log_debug("writing key 0x%llx (%s) to %%%u", key,
1.74 nicm 420: key_string_lookup_key(key, 1), wp->id);
1.72 nicm 421: }
1.69 nicm 422:
423: if (KEYC_IS_MOUSE(key)) {
424: if (m != NULL && m->wp != -1 && (u_int)m->wp == wp->id)
425: input_key_mouse(wp, m);
426: return (0);
427: }
1.72 nicm 428: return (input_key(wp->screen, wp->event, key));
1.69 nicm 429: }
430:
1.8 nicm 431: /* Translate a key code into an output key sequence. */
1.67 nicm 432: int
1.72 nicm 433: input_key(struct screen *s, struct bufferevent *bev, key_code key)
1.1 nicm 434: {
1.74 nicm 435: struct input_key_entry *ike;
1.75 ! nicm 436: key_code justkey, newkey, outkey;
1.72 nicm 437: struct utf8_data ud;
1.75 ! nicm 438: char tmp[64], modifier;
1.1 nicm 439:
1.69 nicm 440: /* Mouse keys need a pane. */
441: if (KEYC_IS_MOUSE(key))
1.67 nicm 442: return (0);
1.64 nicm 443:
444: /* Literal keys go as themselves (can't be more than eight bits). */
445: if (key & KEYC_LITERAL) {
446: ud.data[0] = (u_char)key;
1.69 nicm 447: bufferevent_write(bev, &ud.data[0], 1);
1.67 nicm 448: return (0);
1.65 nicm 449: }
450:
451: /* Is this backspace? */
452: if ((key & KEYC_MASK_KEY) == KEYC_BSPACE) {
1.66 nicm 453: newkey = options_get_number(global_options, "backspace");
454: if (newkey >= 0x7f)
455: newkey = '\177';
1.74 nicm 456: key = newkey|(key & (KEYC_MASK_MODIFIERS|KEYC_MASK_FLAGS));
1.42 nicm 457: }
1.1 nicm 458:
1.8 nicm 459: /*
460: * If this is a normal 7-bit key, just send it, with a leading escape
1.46 nicm 461: * if necessary. If it is a UTF-8 key, split it and send it.
1.8 nicm 462: */
1.75 ! nicm 463: justkey = (key & ~(KEYC_META|KEYC_IMPLIED_META));
1.52 nicm 464: if (justkey <= 0x7f) {
1.73 nicm 465: if (key & KEYC_META)
1.69 nicm 466: bufferevent_write(bev, "\033", 1);
1.47 nicm 467: ud.data[0] = justkey;
1.69 nicm 468: bufferevent_write(bev, &ud.data[0], 1);
1.67 nicm 469: return (0);
1.46 nicm 470: }
1.52 nicm 471: if (justkey > 0x7f && justkey < KEYC_BASE) {
1.48 nicm 472: if (utf8_split(justkey, &ud) != UTF8_DONE)
1.67 nicm 473: return (-1);
1.73 nicm 474: if (key & KEYC_META)
1.69 nicm 475: bufferevent_write(bev, "\033", 1);
476: bufferevent_write(bev, ud.data, ud.size);
1.67 nicm 477: return (0);
1.12 nicm 478: }
479:
1.18 nicm 480: /*
1.72 nicm 481: * Look up in the tree. If not in application keypad or cursor mode,
482: * remove the flags from the key.
1.12 nicm 483: */
1.72 nicm 484: if (~s->mode & MODE_KKEYPAD)
485: key &= ~KEYC_KEYPAD;
486: if (~s->mode & MODE_KCURSOR)
487: key &= ~KEYC_CURSOR;
1.74 nicm 488: ike = input_key_get(key);
489: if (ike == NULL && (key & KEYC_META) && (~key & KEYC_IMPLIED_META))
490: ike = input_key_get(key & ~KEYC_META);
1.75 ! nicm 491: if (ike != NULL) {
! 492: log_debug("found key 0x%llx: \"%s\"", key, ike->data);
! 493: if (key & KEYC_META && (~key & KEYC_IMPLIED_META))
! 494: bufferevent_write(bev, "\033", 1);
! 495: bufferevent_write(bev, ike->data, strlen(ike->data));
! 496: return (0);
1.1 nicm 497: }
498:
1.75 ! nicm 499: /* No builtin key sequence; construct an extended key sequence. */
! 500: outkey = (key & KEYC_MASK_KEY);
! 501: if (outkey >= KEYC_BASE) {
! 502: switch (outkey) {
! 503: case KEYC_IC:
! 504: outkey = 2;
! 505: break;
! 506: case KEYC_DC:
! 507: outkey = 3;
! 508: break;
! 509: case KEYC_PPAGE:
! 510: outkey = 5;
! 511: break;
! 512: case KEYC_NPAGE:
! 513: outkey = 6;
! 514: break;
! 515: case KEYC_HOME:
! 516: outkey = 7;
! 517: break;
! 518: case KEYC_END:
! 519: outkey = 8;
! 520: break;
! 521: case KEYC_F1:
! 522: outkey = 11;
! 523: break;
! 524: case KEYC_F2:
! 525: outkey = 12;
! 526: break;
! 527: case KEYC_F3:
! 528: outkey = 13;
! 529: break;
! 530: case KEYC_F4:
! 531: outkey = 14;
! 532: break;
! 533: case KEYC_F5:
! 534: outkey = 15;
! 535: break;
! 536: case KEYC_F6:
! 537: outkey = 17;
! 538: break;
! 539: case KEYC_F7:
! 540: outkey = 18;
! 541: break;
! 542: case KEYC_F8:
! 543: outkey = 19;
! 544: break;
! 545: case KEYC_F9:
! 546: outkey = 20;
! 547: break;
! 548: case KEYC_F10:
! 549: outkey = 21;
! 550: break;
! 551: case KEYC_F11:
! 552: outkey = 23;
! 553: break;
! 554: case KEYC_F12:
! 555: outkey = 24;
! 556: break;
! 557: default:
! 558: goto missing;
! 559: }
! 560: }
! 561: switch (key & KEYC_MASK_MODIFIERS) {
! 562: case KEYC_SHIFT:
! 563: modifier = '2';
! 564: break;
! 565: case KEYC_META:
! 566: modifier = '3';
! 567: break;
! 568: case KEYC_SHIFT|KEYC_META:
! 569: modifier = '4';
! 570: break;
! 571: case KEYC_CTRL:
! 572: modifier = '5';
! 573: break;
! 574: case KEYC_SHIFT|KEYC_CTRL:
! 575: modifier = '6';
! 576: break;
! 577: case KEYC_META|KEYC_CTRL:
! 578: modifier = '7';
! 579: break;
! 580: case KEYC_SHIFT|KEYC_META|KEYC_CTRL:
! 581: modifier = '8';
! 582: break;
! 583: }
! 584: xsnprintf(tmp, sizeof tmp, "\033[%llu;%cu", outkey, modifier);
! 585: bufferevent_write(bev, tmp, strlen(tmp));
1.67 nicm 586: return (0);
1.75 ! nicm 587:
! 588: missing:
! 589: log_debug("key 0x%llx missing", key);
! 590: return (-1);
1.1 nicm 591: }
592:
1.70 nicm 593: /* Get mouse event string. */
594: int
595: input_key_get_mouse(struct screen *s, struct mouse_event *m, u_int x, u_int y,
596: const char **rbuf, size_t *rlen)
1.1 nicm 597: {
1.70 nicm 598: static char buf[40];
1.59 nicm 599: size_t len;
1.42 nicm 600:
1.70 nicm 601: *rbuf = NULL;
602: *rlen = 0;
1.24 nicm 603:
1.59 nicm 604: /* If this pane is not in button or all mode, discard motion events. */
1.68 nicm 605: if (MOUSE_DRAG(m->b) && (s->mode & MOTION_MOUSE_MODES) == 0)
1.71 nicm 606: return (0);
607: if ((s->mode & ALL_MOUSE_MODES) == 0)
1.70 nicm 608: return (0);
1.59 nicm 609:
610: /*
611: * If this event is a release event and not in all mode, discard it.
612: * In SGR mode we can tell absolutely because a release is normally
613: * shown by the last character. Without SGR, we check if the last
614: * buttons was also a release.
615: */
616: if (m->sgr_type != ' ') {
617: if (MOUSE_DRAG(m->sgr_b) &&
618: MOUSE_BUTTONS(m->sgr_b) == 3 &&
1.68 nicm 619: (~s->mode & MODE_MOUSE_ALL))
1.70 nicm 620: return (0);
1.59 nicm 621: } else {
622: if (MOUSE_DRAG(m->b) &&
623: MOUSE_BUTTONS(m->b) == 3 &&
624: MOUSE_BUTTONS(m->lb) == 3 &&
1.68 nicm 625: (~s->mode & MODE_MOUSE_ALL))
1.70 nicm 626: return (0);
1.59 nicm 627: }
1.42 nicm 628:
629: /*
630: * Use the SGR (1006) extension only if the application requested it
631: * and the underlying terminal also sent the event in this format (this
632: * is because an old style mouse release event cannot be converted into
633: * the new SGR format, since the released button is unknown). Otherwise
634: * pretend that tmux doesn't speak this extension, and fall back to the
1.51 nicm 635: * UTF-8 (1005) extension if the application requested, or to the
1.42 nicm 636: * legacy format.
637: */
1.59 nicm 638: if (m->sgr_type != ' ' && (s->mode & MODE_MOUSE_SGR)) {
1.42 nicm 639: len = xsnprintf(buf, sizeof buf, "\033[<%u;%u;%u%c",
640: m->sgr_b, x + 1, y + 1, m->sgr_type);
1.59 nicm 641: } else if (s->mode & MODE_MOUSE_UTF8) {
1.55 nicm 642: if (m->b > 0x7ff - 32 || x > 0x7ff - 33 || y > 0x7ff - 33)
1.70 nicm 643: return (0);
1.51 nicm 644: len = xsnprintf(buf, sizeof buf, "\033[M");
1.72 nicm 645: len += input_key_split2(m->b + 32, &buf[len]);
646: len += input_key_split2(x + 33, &buf[len]);
647: len += input_key_split2(y + 33, &buf[len]);
1.42 nicm 648: } else {
649: if (m->b > 223)
1.70 nicm 650: return (0);
1.42 nicm 651: len = xsnprintf(buf, sizeof buf, "\033[M");
652: buf[len++] = m->b + 32;
653: buf[len++] = x + 33;
654: buf[len++] = y + 33;
1.1 nicm 655: }
1.70 nicm 656:
657: *rbuf = buf;
658: *rlen = len;
659: return (1);
660: }
661:
662: /* Translate mouse and output. */
663: static void
664: input_key_mouse(struct window_pane *wp, struct mouse_event *m)
665: {
666: struct screen *s = wp->screen;
667: u_int x, y;
668: const char *buf;
669: size_t len;
670:
671: /* Ignore events if no mouse mode or the pane is not visible. */
672: if (m->ignore || (s->mode & ALL_MOUSE_MODES) == 0)
673: return;
674: if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
675: return;
676: if (!window_pane_visible(wp))
677: return;
678: if (!input_key_get_mouse(s, m, x, y, &buf, &len))
679: return;
1.44 nicm 680: log_debug("writing mouse %.*s to %%%u", (int)len, buf, wp->id);
1.42 nicm 681: bufferevent_write(wp->event, buf, len);
1.1 nicm 682: }