Skip to content

daemon: debounce rubber and button flags to avoid spurious stylus lifts - #233

Open
06FallingLight90 wants to merge 2 commits into
linux-surface:masterfrom
06FallingLight90:debounce-stylus-flags
Open

06FallingLight90 wants to merge 2 commits into
linux-surface:masterfrom
06FallingLight90:debounce-stylus-flags

Conversation

@06FallingLight90

Copy link
Copy Markdown

Problem

Third-party MPP styli can produce constant "disconnects" while writing: the pen is repeatedly lifted and put back down several times per second. This is the symptom reported in #195.

Instrumenting an affected setup (Surface Pro 7+, iptsd 3.1.1) shows that the stylus data stream itself is healthy: the pen reports proximity = 1 continuously and DFT position windows arrive at a constant ~66 Hz. What actually happens is:

  • handle_button() derives the eraser / side button state from the sign of a phase correlation (val). With some pens val hovers near zero while writing, so data.rubber / data.button flip back and forth several times per second (measured: 424 flips within 10 minutes of normal writing).
  • StylusDevice::update() lifts the pen for one frame on every tool flip ("Switching tools within one frame causes issues, lift the stylus for one frame"). Every single-frame flicker is therefore emitted as a full BTN_TOOL_PEN 1 -> 0 -> 1 cycle — perceived as a disconnect (~3/s, with a stable 0.030 s lift-to-retouch interval = 2 frames at the DFT rate).

This also explains the wev observations in #195: each spurious lift makes libinput leave and re-enter tool proximity, which shows up downstream as duplicated coordinates followed by zero-timestamped motion events.

Fix

Debounce both flags in StylusDevice::update(): a flip only becomes visible after it has been seen in 2 consecutive updates (~30 ms at the typical DFT rate — imperceptible for real tool switches and button presses), while single-frame flickers are dropped entirely. BTN_STYLUS is debounced with the same scheme, since flapping it is interpreted as clicks by some applications.

Validation

On an affected Surface Pro 7+ with a third-party MPP 2.0 pen:

  • stylus disconnects (BTN_TOOL_PEN drops while writing): 123 per 40 s session → 0
  • real eraser usage still works (a real flip commits within ~30 ms)
  • side button presses still work; the flapping no longer reaches applications

Krita, Xournal, Okular and LibreOffice Draw all write continuously after the fix.

References #195 (assuming the reporter's pen flickers the same way; a quick discriminator is writing while holding the side button, which stops the disconnects completely on the pen tested here).

Some third-party MPP styli (see linux-surface#195) constantly flicker the eraser and
side button phase detection: the sign of the phase correlation computed
in handle_button() flips several times per second while writing. Since
StylusDevice::update() lifts the pen for one frame on every tool flip,
each flicker is emitted as a full BTN_TOOL_PEN 0 -> 1 cycle, which is
perceived as constant disconnects while inking (measured ~3 lift cycles
per second, with a stable 0.030s lift-to-retouch interval).

Debounce both flags: a flip only becomes visible once it has been seen
in 2 consecutive updates (~30ms at the typical DFT rate, imperceptible
for real tool switches and button presses, while single-frame flickers
are dropped entirely).

On an affected Surface Pro 7+ this reduces stylus disconnects from 123
per 40s writing session to 0, without affecting real eraser usage.
@tmarkov

tmarkov commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

This fix doesn't work on my machine. Interestingly, the diagnosis is correct - I verified the issue is caused by the eraser button triggering. I will investigate why the fix isn't working.

Edit: It looks like my issue is caused by the MPP2 based button detection mechanism instead of the phase sign one, so this PR wouldn't be expected to help.

Some third-party MPP pens constantly produce spurious eraser / button
events through the MPP v2 binary frame detection. Document the two
independent detection paths and how to disable the MPP v2 button
detection for affected pens, complementing the StylusDevice debounce
added for the phase based path (linux-surface#195).
@06FallingLight90

Copy link
Copy Markdown
Author

Thanks — this matches what we see and adds an important piece: there are two independent detection paths that can each produce spurious eraser / button events:

  1. MPP v2 binary frames (handle_dft_binary_mpp_2, magnitude comparison mag_4 < mag_5) — your case
  2. Button window phase sign (handle_button, sign of val = m_real * real + m_imag * imag) — the case measured in this PR

Both paths feed the same choke point: a flipped rubber / button flag makes StylusDevice::update() lift the pen for one frame, which is perceived as a disconnect. That's why the symptoms are identical while the working fixes differ.

I just ran the discrimination experiment on my setup (third-party MPP 2.0 pen, phase-sign case): setting Mpp2ButtonMinMag = 4294967295 to disable the MPP v2 path changes nothing here — the flicker continues at the same rate (~151 flips in a 30 s writing session), all absorbed by the 2-frame debounce in this PR. So on my pen the MPP v2 path is clean and the phase path is the noise source; on your pen it is the reverse. Complementary, not conflicting:

  • your Mpp2ButtonMinMag workaround removes the MPP v2 noise source at its origin,
  • the debounce in this PR absorbs single-frame flickers at the convergence point,
  • neither alone covers both mechanisms (a 2-frame debounce cannot absorb ≥2-frame MPP v2 bursts; source thresholds cannot fix a noisy phase path).

I've documented your workaround in the example config (second commit in this PR) so affected users can find it. Making the debounce depth configurable might be a sensible follow-up for pens that emit longer bursts.

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