From 4d17c5ab27be6adb8437d7a685b1a9f8fee0ade5 Mon Sep 17 00:00:00 2001 From: Jared Finder Date: Sat, 4 Jan 2025 13:55:05 -0800 Subject: [PATCH] Support TTY child frames with GPM mouse * src/term.c (frame_at): New C function, wraps Lisp `frame-at'. (handle_one_term_event): Call frame_at to get frame under mouse, store it in last_mouse_frame. Alter event coordinates based on mouse frame. (term_mouse_position): Use last_mouse_frame when it is set. (syms_of_term): Add Qframe_at. * src/termhooks.h: Make Gpm_Event parameter const. --- src/term.c | 52 +++++++++++++++++++++++++++++++++++++------------ src/termhooks.h | 2 +- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/term.c b/src/term.c index 368e20803e1..bbf0bafb93f 100644 --- a/src/term.c +++ b/src/term.c @@ -141,6 +141,7 @@ #define OUTPUT1_IF(tty, a) \ struct tty_display_info *gpm_tty = NULL; /* Last recorded mouse coordinates. */ +static Lisp_Object last_mouse_frame; static int last_mouse_x, last_mouse_y; #endif /* HAVE_GPM */ @@ -2638,7 +2639,11 @@ term_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window, enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y, Time *timeptr) { - *fp = SELECTED_FRAME (); + /* This code runs even when `gpm-mouse-mode' was never active, like + inside an xterm. In such cases, last_mouse_frame will never be + set, so fallback to SELECTED_FRAME. */ + *fp = FRAMEP (last_mouse_frame) ? XFRAME (last_mouse_frame) + : SELECTED_FRAME (); (*fp)->mouse_moved = 0; *bar_window = Qnil; @@ -2712,10 +2717,28 @@ term_mouse_click (struct input_event *result, Gpm_Event *event, return Qnil; } +static Lisp_Object +frame_at(int x, int y) +{ + Lisp_Object frame = CALLN (Ffuncall, Qframe_at, + make_fixnum (x), make_fixnum (y)); + if (NILP (frame)) + { + frame = selected_frame; + } + + return frame; +} + int -handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event) +handle_one_term_event (struct tty_display_info *tty, const Gpm_Event *event_in) { - struct frame *f = XFRAME (tty->top_frame); + Lisp_Object frame = frame_at(event_in->x, event_in->y); + struct frame *f = XFRAME (frame); + Gpm_Event event = *event_in; + event.x -= f->left_pos; + event.y -= f->top_pos; + struct input_event ie; int count = 0; @@ -2723,30 +2746,34 @@ handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event) ie.kind = NO_EVENT; ie.arg = Qnil; - if (event->type & (GPM_MOVE | GPM_DRAG)) + if (event.type & (GPM_MOVE | GPM_DRAG)) { - Gpm_DrawPointer (event->x, event->y, fileno (tty->output)); + /* The pointer must be drawn using screen coordinates (x,y), not + frame coordinates. Use event_in which has an unmodified event + directly from GPM. */ + Gpm_DrawPointer (event_in->x, event_in->y, fileno (tty->output)); /* Has the mouse moved off the glyph it was on at the last sighting? */ - if (event->x != last_mouse_x || event->y != last_mouse_y) + if (event.x != last_mouse_x || event.y != last_mouse_y) { - /* FIXME: These three lines can not be moved into + /* FIXME: These four lines can not be moved into update_mouse_position unless xterm-mouse gets updated to generate mouse events via C code. See https://lists.gnu.org/archive/html/emacs-devel/2020-11/msg00163.html */ - last_mouse_x = event->x; - last_mouse_y = event->y; + last_mouse_frame = frame; + last_mouse_x = event.x; + last_mouse_y = event.y; f->mouse_moved = 1; - count += update_mouse_position (f, event->x, event->y); + count += update_mouse_position (f, event.x, event.y); } } else { f->mouse_moved = 0; - term_mouse_click (&ie, event, f); - ie.arg = tty_handle_tab_bar_click (f, event->x, event->y, + term_mouse_click (&ie, &event, f); + ie.arg = tty_handle_tab_bar_click (f, event.x, event.y, (ie.modifiers & down_modifier) != 0, &ie); kbd_buffer_store_event (&ie); count++; @@ -4970,6 +4997,7 @@ syms_of_term (void) #ifdef HAVE_GPM defsubr (&Sgpm_mouse_start); defsubr (&Sgpm_mouse_stop); + DEFSYM (Qframe_at, "frame-at"); #endif /* HAVE_GPM */ defsubr (&Stty_frame_geometry); diff --git a/src/termhooks.h b/src/termhooks.h index b32804a57b3..0795148f1af 100644 --- a/src/termhooks.h +++ b/src/termhooks.h @@ -458,7 +458,7 @@ #define EVENT_INIT(event) (memset (&(event), 0, sizeof (struct input_event)), \ #ifdef HAVE_GPM #include -extern int handle_one_term_event (struct tty_display_info *, Gpm_Event *); +extern int handle_one_term_event (struct tty_display_info *, const Gpm_Event *); extern void term_mouse_moveto (int, int); /* The device for which we have enabled gpm support. */ -- 2.39.5