Skip to content

Comments

Fix mouse drag tracking in terminal applications#214

Merged
kdj0c merged 2 commits intokmscon:mainfrom
jtollet:fix-mouse-drag-upstream
Jan 13, 2026
Merged

Fix mouse drag tracking in terminal applications#214
kdj0c merged 2 commits intokmscon:mainfrom
jtollet:fix-mouse-drag-upstream

Conversation

@jtollet
Copy link
Contributor

@jtollet jtollet commented Jan 13, 2026

Summary

This PR fixes mouse drag tracking in kmscon, allowing applications like Vim to properly display visual selection in real-time during mouse drag operations.

Problem

When applications activate mouse tracking (e.g., Vim with set mouse=a ttymouse=sgr), visual selection would only update after releasing the mouse button, not during the drag itself. This worked fine in X terminal emulators but not in kmscon.

Root Cause

kmscon was not tracking or transmitting button state during mouse motion events. When the user dragged with a button pressed:

  1. Motion events were sent but without button state information
  2. Applications couldn't distinguish between hover (no button) and drag (button pressed)

Solution

Commit 1: Track button state

  • Add pressed_button field to uterm_input_pointer structure
  • Track which button is currently pressed (255=none, 0-2=button number)
  • Include button state in UTERM_MOVED events

Commit 2: Encode drags correctly

  • Add +32 offset to button number during drags (per X10/SGR protocol)
  • This signals to libtsm that it's a drag, not a hover

Testing

Tested with Vim visual selection under kmscon:

  • Before: Selection updates only on button release
  • After: Selection updates in real-time during drag

Works with all modern mouse tracking modes (SGR, PIXEL, modes 1002/1003).

Related

This requires corresponding fixes in libtsm: kmscon/libtsm#11

@kdj0c
Copy link
Contributor

kdj0c commented Jan 13, 2026

Thanks for your contribution.

It looks good to me, the only comment I have would be to add a define in uterm_input_internal.h:
#define BUTTON_NONE 255

And use that when no button is pressed.

@jtollet jtollet force-pushed the fix-mouse-drag-upstream branch from a2673fc to a90bd0d Compare January 13, 2026 13:01
@jtollet
Copy link
Contributor Author

jtollet commented Jan 13, 2026

Thank you for the review! I have updated the commits to use BUTTON_NONE constant instead of the magic number 255.

Changes:

  • Added #define BUTTON_NONE 255 in uterm_input_internal.h
  • Replaced all occurrences of 255 with BUTTON_NONE throughout the code
  • Updated commit message to document the constant

The code is now more readable and maintainable.

When an application like Vim activates mouse tracking (SGR mode with
any-event tracking), motion events during drag (button held + movement)
were not including button state information. This caused visual selection
in Vim to only update on button release instead of showing real-time
feedback during drag.

Changes:
- Add BUTTON_NONE constant (255) to represent no button pressed
- Add pressed_button field (uint8_t) to uterm_input_pointer struct to
  track which button is currently pressed (BUTTON_NONE, 0=left, 1=right, 2=middle)
- Initialize pressed_button to BUTTON_NONE in uterm_input_dev initialization
- Update pressed_button state in pointer_dev_button() on press/release
- Include button state in UTERM_MOVED events sent by pointer_dev_send_move()
- Now motion events during drag properly indicate which button is pressed

This fixes the lag/delay in visual selection for applications using mouse
tracking under kmscon, bringing behavior in line with X terminal emulators.
The previous patch tracked button state correctly but used the wrong
encoding when forwarding motion events to libtsm. In X10/SGR mouse
tracking protocol:
- Button press/release events use button numbers: 0 (left), 1 (right), 2 (middle)
- Motion with button pressed (drag) uses: button + 32

This commit adds the +32 offset in forward_pointer_event() for UTERM_MOVED
events when a button is pressed, so libtsm correctly interprets them as
drag events rather than hover motion.

Now visual selection in Vim should update in real-time during mouse drag.
@jtollet jtollet force-pushed the fix-mouse-drag-upstream branch from a90bd0d to ec0f6e5 Compare January 13, 2026 13:03
@kdj0c kdj0c merged commit 40db4a6 into kmscon:main Jan 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants