terminal: support SGR-style mouse for extended coordinates

This commit is contained in:
K. Lange 2019-12-04 13:16:03 +09:00
parent 88363fa19e
commit 0d162680ff
4 changed files with 36 additions and 14 deletions

View File

@ -1127,9 +1127,15 @@ static int old_x = 0;
static int old_y = 0;
static void mouse_event(int button, int x, int y) {
char buf[7];
sprintf(buf, "\033[M%c%c%c", button + 32, x + 33, y + 33);
handle_input_s(buf);
if (ansi_state->mouse_on & TERMEMU_MOUSE_SGR) {
char buf[100];
sprintf(buf,"\033[<%d;%d;%d%c", button == 3 ? 0 : button, x+1, y+1, button == 3 ? 'm' : 'M');
handle_input_s(buf);
} else {
char buf[7];
sprintf(buf, "\033[M%c%c%c", button + 32, x + 33, y + 33);
handle_input_s(buf);
}
}
static void redraw_mouse(void) {
@ -1142,7 +1148,7 @@ static void redraw_mouse(void) {
static unsigned int button_state = 0;
void handle_mouse_event(mouse_device_packet_t * packet) {
if (ansi_state->mouse_on) {
if (ansi_state->mouse_on & TERMEMU_MOUSE_ENABLE) {
/* TODO: Handle shift */
if (packet->buttons & MOUSE_SCROLL_UP) {
mouse_event(32+32, mouse_x, mouse_y);
@ -1158,7 +1164,7 @@ void handle_mouse_event(mouse_device_packet_t * packet) {
if (!(packet->buttons & MIDDLE_CLICK) && (button_state & MIDDLE_CLICK)) mouse_event(3, mouse_x, mouse_y);
if (!(packet->buttons & RIGHT_CLICK) && (button_state & MIDDLE_CLICK)) mouse_event(3, mouse_x, mouse_y);
button_state = packet->buttons;
} else if (ansi_state->mouse_on == 2) {
} else if (ansi_state->mouse_on & TERMEMU_MOUSE_DRAG) {
if (old_x != mouse_x || old_y != mouse_y) {
if (button_state & LEFT_CLICK) mouse_event(32, mouse_x, mouse_y);
if (button_state & MIDDLE_CLICK) mouse_event(33, mouse_x, mouse_y);

View File

@ -2022,9 +2022,15 @@ static void resize_finish(int width, int height) {
/* Insert a mouse event sequence into the PTY */
static void mouse_event(int button, int x, int y) {
char buf[7];
sprintf(buf, "\033[M%c%c%c", button + 32, x + 33, y + 33);
handle_input_s(buf);
if (ansi_state->mouse_on & TERMEMU_MOUSE_SGR) {
char buf[100];
sprintf(buf,"\033[<%d;%d;%d%c", button == 3 ? 0 : button, x+1, y+1, button == 3 ? 'm' : 'M');
handle_input_s(buf);
} else {
char buf[7];
sprintf(buf, "\033[M%c%c%c", button + 32, x + 33, y + 33);
handle_input_s(buf);
}
}
/* Handle Yutani messages */
@ -2144,7 +2150,7 @@ static void * handle_incoming(void) {
if (new_x >= term_width || new_y >= term_height) break;
/* Map Cursor Action */
if (ansi_state->mouse_on && !(me->modifiers & YUTANI_KEY_MODIFIER_SHIFT)) {
if ((ansi_state->mouse_on & TERMEMU_MOUSE_ENABLE) && !(me->modifiers & YUTANI_KEY_MODIFIER_SHIFT)) {
if (me->buttons & YUTANI_MOUSE_SCROLL_UP) {
mouse_event(32+32, new_x, new_y);
@ -2175,7 +2181,7 @@ static void * handle_incoming(void) {
last_mouse_x = new_x;
last_mouse_y = new_y;
button_state = me->buttons;
} else if (ansi_state->mouse_on == 2) {
} else if (ansi_state->mouse_on & TERMEMU_MOUSE_DRAG) {
/* Report motion for pressed buttons */
if (last_mouse_x == new_x && last_mouse_y == new_y) break;
if (button_state & YUTANI_MOUSE_BUTTON_LEFT) mouse_event(32, new_x, new_y);

View File

@ -109,6 +109,12 @@ typedef struct {
#define TERM_DEFAULT_FLAGS 0x00 /* Default flags for a cell */
#define TERM_DEFAULT_OPAC 0xF2 /* For background, default transparency */
#define TERMEMU_MOUSE_ENABLE 0x01
#define TERMEMU_MOUSE_DRAG 0x02
#define TERMEMU_MOUSE_SGR 0x04
/* TODO: _MOUSE_UTF8 0x08 */
/* TODO: _MOUSE_URXVT 0x10 */
extern term_state_t * ansi_init(term_state_t * s, int w, int y, term_callbacks_t * callbacks_in);
extern void ansi_put(term_state_t * s, char c);

View File

@ -322,9 +322,11 @@ static void _ansi_put(term_state_t * s, char c) {
if (!strcmp(argv[0], "?1049")) {
if (callbacks->switch_buffer) callbacks->switch_buffer(1);
} else if (!strcmp(argv[0], "?1000")) {
s->mouse_on = 1;
s->mouse_on |= TERMEMU_MOUSE_ENABLE;
} else if (!strcmp(argv[0], "?1002")) {
s->mouse_on = 2;
s->mouse_on |= TERMEMU_MOUSE_DRAG;
} else if (!strcmp(argv[0], "?1006")) {
s->mouse_on |= TERMEMU_MOUSE_SGR;
} else if (!strcmp(argv[0], "?25")) {
callbacks->set_csr_on(1);
}
@ -335,9 +337,11 @@ static void _ansi_put(term_state_t * s, char c) {
if (!strcmp(argv[0], "?1049")) {
if (callbacks->switch_buffer) callbacks->switch_buffer(0);
} else if (!strcmp(argv[0], "?1000")) {
s->mouse_on = 0;
s->mouse_on &= ~TERMEMU_MOUSE_ENABLE;
} else if (!strcmp(argv[0], "?1002")) {
s->mouse_on = 0;
s->mouse_on &= ~TERMEMU_MOUSE_DRAG;
} else if (!strcmp(argv[0],"?1006")) {
s->mouse_on &= ~TERMEMU_MOUSE_SGR;
} else if (!strcmp(argv[0], "?25")) {
callbacks->set_csr_on(0);
}