Package: rxvt-unicode
Version: 9.31-1
Severity: normal
Tags: patch upstream
X-Debbugs-Cc: tobespam...@web.de

Hi,

I was recently experimenting with a terminal UI library and had some contact
with terminal mouse reporting [0]. Specifically, SGR pixel mode (enabled via
ESC[?1006h) and the any mouse event mode (ESC[?1003h).

In this combination of modes, moving the mouse around in urxvt's window
generates input for the TTY indicating these mouse moves. See [1] for the urxvt
documentation about these features. For example, ESC[<33;67;40M is reported when
moving the mouse to x=67, y=40. 33 indicates the button state. It is a bit mask.
Since bit 5 is set (32), this is a mouse move without a click. The last button
that was pressed is button 2 (32 + 1).

I noticed that right after starting a new urxvt instance without ever pressing a
mouse button in its window, urxvt reports 31 as the first value of the event.
According to the docs [1], this would be a press of button 3 with Shift, Meta,
and Control modifiers active, even though this was just a simple mouse move
without any modifiers.

To reproduced, I used a shell script so that I can run this in a fresh urxvt
window without having to type the exact commands or using the middle mouse
button for paste. It only later occurred to me that I could have used
Shift+Insert instead. The shell script is (yes, this enables more than just mode
1006 and 1003; I was experimenting):

#!/bin/bash
printf "\x1b[?1000h\x1b[?1003h\x1b[?1015h\x1b[?1006h"
xxd
printf "\x1b[?1000l\x1b[?1003l\x1b[?1015l\x1b[?1006l" 

Open a new urxvt window, run this script without clicking in the window, press
Ctrl+D twice to make it exit and clean up again.

Next, I looked at the source code of urxvt.

During startup, MEvent.button is initialised to AnyButton (which has value 0) in
rxvt_term::init_vars(). The code that does the actual mouse event reporting is
rxvt_term::mouse_report(). It calculates the button number as "MEvent.button -
Button1". Since "Button1" has the value 1, this ends up with 0 - 1 = -1 if no
button was ever pressed.

I would propose to e.g. pick button 1 instead if no mouse button ever happened
before:

diff --git a/src/command.C b/src/command.C
index 14e6c24..0b16b9b 100644
--- a/src/command.C
+++ b/src/command.C
@@ -1297,6 +1297,10 @@ rxvt_term::mouse_report (XButtonEvent &ev)
     }

   button_number = MEvent.button - Button1;
+  /* Make sure we do not end up with button_number = -1 */
+  if (MEvent.button == AnyButton)
+    button_number = 0;
+
   /* add 0x3D for wheel events, like xterm does */
   if (button_number >= 3)
     button_number += 64 - 3; 

However, I have to admit that I only read the source code and did the above
mentioned experiment. I never actually build urxvt with my patch.

I hope this information suffices to reproduce the problem and helps fixing it.

Now, why am I reporting this to the Debian BTS? Because I failed at contacting
upstream. The IRC channel had no activity since I wrote there and my mail to
Marc Lehmann didn't produce any reaction yet (but the mail is only four days
old, so that might still happen). Besides, there doesn't seem to be any bug
tracker available upstream. I hope that rak, the current Debian urxvt
maintainer, has better information about how to deal with upstream. Sorry for
dumping this task upon you.

Thanks & Cheers,
Uli

[0]: https://github.com/ArthurSonzogni/FTXUI/issues/791
[1]: 
http://pod.tst.eu/http://cvs.schmorp.de/rxvt-unicode/doc/rxvt.7.pod#Mouse_Reporting

-- System Information:
Debian Release: trixie/sid
  APT prefers testing
  APT policy: (990, 'testing'), (500, 'testing-debug'), (50, 'experimental'), 
(50, 'unstable')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 6.5.0-5-amd64 (SMP w/8 CPU threads; PREEMPT)
Locale: LANG=de_DE.UTF-8, LC_CTYPE=de_DE.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages rxvt-unicode depends on:
ii  base-passwd               3.6.3
ii  libc6                     2.37-12
ii  libfontconfig1            2.14.2-6
ii  libgcc-s1                 13.2.0-7
ii  libgdk-pixbuf-2.0-0       2.42.10+dfsg-3
ii  libglib2.0-0              2.78.3-1
ii  libperl5.36               5.36.0-10
ii  libptytty0                2.0-1+b1
ii  libstartup-notification0  0.12-6+b1
ii  libx11-6                  2:1.8.7-1
ii  libxext6                  2:1.3.4-1+b1
ii  libxft2                   2.3.6-1
ii  libxmu6                   2:1.1.3-3
ii  libxrender1               1:0.9.10-1.1
ii  ncurses-base              6.4+20231209-1
ii  ncurses-term              6.4+20231209-1

Versions of packages rxvt-unicode recommends:
ii  fonts-dejavu                            2.37-8
pn  fonts-vlgothic | fonts-japanese-gothic  <none>

Versions of packages rxvt-unicode suggests:
ii  sensible-utils  0.0.20

-- no debconf information

Reply via email to