[binutils-gdb] gdb/tui: add 'set tui mouse-events off' to restore mouse selection

Pedro Alves palves@sourceware.org
Wed Sep 20 15:38:38 GMT 2023


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0f6a69947891fc448c37e944d6c3e266621d35a8

commit 0f6a69947891fc448c37e944d6c3e266621d35a8
Author: Matthew "strager" Glazar <strager.nds@gmail.com>
Date:   Fri Jan 27 16:19:45 2023 -0800

    gdb/tui: add 'set tui mouse-events off' to restore mouse selection
    
    Rationale:
    I use the mouse with my terminal to select and copy text. In gdb, I use
    the mouse to select a function name to set a breakpoint, or a variable
    name to print, for example.
    
    When gdb is compiled with ncurses mouse support, gdb's TUI mode
    intercepts mouse events. Left-clicking and dragging, which would
    normally select text, seems to do nothing. This means I cannot select
    text using my mouse anymore. This makes it harder to set breakpoints,
    print variables, etc.
    
    Solution:
    I tried to fix this issue by editing the 'mousemask' call to only enable
    buttons 4 and 5. However, this still caused my terminal (gnome-terminal)
    to not allow text to be selected. The only way I could make it work is
    by calling 'mousemask (0, NULL);'. But doing so disables the mouse code
    entirely, which other people might want.
    
    I therefore decided to make a setting in gdb called 'tui mouse-events'.
    If enabled (the default), the behavior is as it is now: terminal mouse
    events are given to gdb, disabling the terminal's default behavior.
    If disabled (opt-in), the behavior is as it was before the year 2020:
    terminal mouse events are not given to gdb, therefore the mouse can be
    used to select and copy text.
    
    Notes:
    I am not attached to the setting name or its description. Feel free to
    suggest better wording.
    
    Testing:
    I tested this change in gnome-terminal by performing the following steps
    manually:
    
    1. Run: gdb --args ./myprogram
    2. Enable TUI: press ctrl-x ctrl-a
    3. Click and drag text with the mouse. Observe no selection.
    4. Input: set tui mouse-events off
    5. Click and drag text with the mouse. Observe that selection works now.
    6. Input: set tui mouse-events on.
    7. Click and drag text with the mouse. Observe no selection.

Diff:
---
 gdb/NEWS            |  6 ++++++
 gdb/doc/gdb.texinfo | 15 ++++++++++++++-
 gdb/doc/python.texi |  5 +++++
 gdb/tui/tui-io.c    |  3 ++-
 gdb/tui/tui-win.c   | 22 ++++++++++++++++++++++
 gdb/tui/tui-win.h   |  3 +++
 6 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index bb9d26abf6b..957b5a938b0 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -133,6 +133,12 @@ show always-read-ctf
 info main
   Get main symbol to identify entry point into program.
 
+set tui mouse-events [on|off]
+show tui mouse-events
+  When on (default), mouse clicks control the TUI and can be accessed by
+  Python extensions.  When off, mouse clicks are handled by the terminal,
+  enabling terminal-native text selection.
+
 * New convenience function "$_shell", to execute a shell command and
   return the result.  This lets you run shell commands in expressions.
   Some examples:
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 5db30bf4a71..f9c2f955ef6 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -30219,7 +30219,13 @@ the @key{SHIFT} key on your keyboard to temporarily bypass
 @value{GDBN}'s TUI and access the terminal's native mouse copy/paste
 functionality (commonly, click-drag-release or double-click to select
 text, middle-click to paste).  This copy/paste works with the
-terminal's selection buffer, as opposed to the TUI's buffer.
+terminal's selection buffer, as opposed to the TUI's buffer.  Alternatively, to
+disable mouse support in the TUI entirely and give the terminal control over
+mouse clicks, turn off the @code{tui mouse-events} setting
+(@pxref{tui-mouse-events,,set tui mouse-events}).
+
+Python extensions can react to mouse clicks
+(@pxref{python-window-click,,Window.click}).
 
 @node TUI Commands
 @section TUI-specific Commands
@@ -30500,6 +30506,13 @@ The default display uses more space for line numbers; the compact
 display uses only as much space as is needed for the line numbers in
 the current file.
 
+@anchor{tui-mouse-events}
+@item set tui mouse-events @r{[}on@r{|}off@r{]}
+@kindex set tui mouse-events
+When on (default), mouse clicks control the TUI (@pxref{TUI Mouse Support}).
+When off, mouse clicks are handled by the terminal, enabling terminal-native
+text selection.
+
 @kindex set debug tui
 @item set debug tui @r{[}on|off@r{]}
 Turn on or off display of @value{GDBN} internal debug messages relating
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 5b13958aeaf..82fa0e38e37 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -6974,11 +6974,16 @@ contents.  A positive argument should cause the viewport to move down,
 and so the content should appear to move up.
 @end defun
 
+@anchor{python-window-click}
 @defun Window.click (x, y, button)
 This is called on a mouse click in this window.  @var{x} and @var{y} are
 the mouse coordinates inside the window (0-based, from the top left
 corner), and @var{button} specifies which mouse button was used, whose
 values can be 1 (left), 2 (middle), or 3 (right).
+
+When TUI mouse events are disabled by turning off the @code{tui mouse-events}
+setting (@pxref{tui-mouse-events,,set tui mouse-events}), then @code{click} will
+not be called.
 @end defun
 
 @node Disassembly In Python
diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c
index 8cb68d12408..b8954af36e2 100644
--- a/gdb/tui/tui-io.c
+++ b/gdb/tui/tui-io.c
@@ -657,7 +657,8 @@ static void
 tui_prep_terminal (int notused1)
 {
 #ifdef NCURSES_MOUSE_VERSION
-  mousemask (ALL_MOUSE_EVENTS, NULL);
+  if (tui_enable_mouse)
+    mousemask (ALL_MOUSE_EVENTS, NULL);
 #endif
 }
 
diff --git a/gdb/tui/tui-win.c b/gdb/tui/tui-win.c
index 93cf45ed296..a0a123340e3 100644
--- a/gdb/tui/tui-win.c
+++ b/gdb/tui/tui-win.c
@@ -872,6 +872,17 @@ tui_show_compact_source (struct ui_file *file, int from_tty,
   gdb_printf (file, _("TUI source window compactness is %s.\n"), value);
 }
 
+bool tui_enable_mouse = true;
+
+/* Implement 'show tui mouse-events'.  */
+
+static void
+show_tui_mouse_events (struct ui_file *file, int from_tty,
+		       struct cmd_list_element *c, const char *value)
+{
+  gdb_printf (file, _("TUI mouse events are %s.\n"), value);
+}
+
 /* Set the tab width of the specified window.  */
 static void
 tui_set_tab_width_command (const char *arg, int from_tty)
@@ -1254,6 +1265,17 @@ in a compact form.  The compact form uses less horizontal space."),
 			   tui_set_compact_source, tui_show_compact_source,
 			   &tui_setlist, &tui_showlist);
 
+  add_setshow_boolean_cmd ("mouse-events", class_tui,
+			   &tui_enable_mouse, _("\
+Set whether TUI mode handles mouse clicks."), _("\
+Show whether TUI mode handles mouse clicks."), _("\
+When on (default), mouse clicks control the TUI and can be accessed by Python\n\
+extensions.  When off, mouse clicks are handled by the terminal, enabling\n\
+terminal-native text selection."),
+			   nullptr,
+			   show_tui_mouse_events,
+			   &tui_setlist, &tui_showlist);
+
   add_setshow_boolean_cmd ("tui-current-position", class_maintenance,
 			   &style_tui_current_position, _("\
 Set whether to style text highlighted by the TUI's current position indicator."),
diff --git a/gdb/tui/tui-win.h b/gdb/tui/tui-win.h
index 3d35f1dfb7f..c0a77324293 100644
--- a/gdb/tui/tui-win.h
+++ b/gdb/tui/tui-win.h
@@ -51,6 +51,9 @@ struct cmd_list_element **tui_get_cmd_list (void);
 /* Whether compact source display should be used.  */
 extern bool compact_source;
 
+/* Whether the TUI should intercept terminal mouse events.  */
+extern bool tui_enable_mouse;
+
 /* Whether to style the source and assembly code highlighted by the TUI's
    current position indicator.  */
 extern bool style_tui_current_position;


More information about the Gdb-cvs mailing list