summaryrefslogtreecommitdiffstats
path: root/tty-keys.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2009-11-26 15:14:03 +0000
committerNicholas Marriott <nicm@openbsd.org>2009-11-26 15:14:03 +0000
commit9c0147915e3724af59f718ef11936ad319c54c54 (patch)
treea2ce13fb58718d20d3148feea99a8e7b06ee23f7 /tty-keys.c
parentd31d4c05cff725db571d4463da4106638304e29b (diff)
Handle the possibility of partial mouse reads, and fix a comment while here.
Diffstat (limited to 'tty-keys.c')
-rw-r--r--tty-keys.c45
1 files changed, 36 insertions, 9 deletions
diff --git a/tty-keys.c b/tty-keys.c
index 03797994..d44583c4 100644
--- a/tty-keys.c
+++ b/tty-keys.c
@@ -418,7 +418,7 @@ tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
/*
* Process at least one key in the buffer and invoke tty->key_callback. Return
- * 1 if there are no further keys, or 0 if there is more in the buffer.
+ * 0 if there are no further keys, or 1 if there could be more in the buffer.
*/
int
tty_keys_next(struct tty *tty)
@@ -461,12 +461,19 @@ tty_keys_next(struct tty *tty)
}
/* Not found. Is this a mouse key press? */
- key = tty_keys_mouse(buf, len, &size, &mouse);
- if (key != KEYC_NONE) {
+ switch (tty_keys_mouse(buf, len, &size, &mouse)) {
+ case 0: /* yes */
evbuffer_drain(tty->event->input, size);
+ key = KEYC_MOUSE;
goto handle_key;
+ case -1: /* no, or not valid */
+ evbuffer_drain(tty->event->input, size);
+ return (1);
+ case 1: /* partial */
+ goto partial_key;
}
+
/* Not found. Try to parse a key with an xterm-style modifier. */
key = xterm_keys_find(buf, len, &size);
if (key != KEYC_NONE) {
@@ -560,7 +567,10 @@ tty_keys_callback(unused int fd, unused short events, void *data)
;
}
-/* Handle mouse key input. */
+/*
+ * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
+ * (probably a mouse sequence but need more data).
+ */
int
tty_keys_mouse(const char *buf, size_t len, size_t *size, struct mouse_event *m)
{
@@ -569,19 +579,36 @@ tty_keys_mouse(const char *buf, size_t len, size_t *size, struct mouse_event *m)
* buttons, X and Y, all based at 32 with 1,1 top-left.
*/
- if (len != 6 || memcmp(buf, "\033[M", 3) != 0)
- return (KEYC_NONE);
+ *size = 0;
+
+ if (buf[0] != '\033')
+ return (-1);
+ if (len == 1)
+ return (1);
+
+ if (buf[1] != '[')
+ return (-1);
+ if (len == 2)
+ return (1);
+
+ if (buf[2] != 'M')
+ return (-1);
+ if (len == 3)
+ return (1);
+
+ if (len < 6)
+ return (1);
*size = 6;
- log_debug("mouse input is: %.*s", (int) len, buf);
+ log_debug("mouse input is: %.6s", buf);
m->b = buf[3];
m->x = buf[4];
m->y = buf[5];
if (m->b < 32 || m->x < 33 || m->y < 33)
- return (KEYC_NONE);
+ return (-1);
m->b -= 32;
m->x -= 33;
m->y -= 33;
- return (KEYC_MOUSE);
+ return (0);
}