Fix: Make the Go web console wheel scroll correctly

The Go web server's mouse handler read deltaY as an int, so json.Unmarshal
rejected the entire mouse message whenever the browser reported a
fractional delta (Chrome's smooth scrolling splits one 100px notch into
33.33 + 33.33 + 33.34), and the wheel was dead while clicks and moves
kept working. Treating every event as one notch was wrong the other way:
it scrolled three to four times too far, and sub-pixel deltas from a
touchpad truncated to zero.

The server now accumulates the delta in notch units per client and
injects WM_MOUSEWHEEL only once a whole notch is reached. The remainder
is rounded, not truncated, so a float sum of 99.99px still lands on
exactly one notch and the first notch fires halfway through a physical
one, like native scrolling. The page forwards e.deltaMode so line and
page based deltas convert correctly, and touchpad flings are capped at
20 notches per event.

Verified against a live Go server with a connected client: 100px sends
one packet, three 33.33px events send one, 53.33px sends one, sub-pixel
events accumulate to none, and the wParam is -120 down / +120 up with
SendToScreen returning no error. The MFC web console is untouched.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-26 17:10:09 +01:00
1 parent c271911fcb
commit 6236ed1eb4
4 files changed
+156 -17

No files matched your search

+5
View File
@@ -63,6 +63,11 @@ type wsClient struct {
user string // mirrors session username after login/resume
termWatching string // device ID for an open web terminal session, "" otherwise
// wheelResidue carries the sub-notch remainder between wheel events so one
// physical notch becomes exactly one WM_MOUSEWHEEL (see handleMouse). Only
// the read loop that owns this client touches it.
wheelResidue float64
// watchIDs is the set of devices this browser is streaming (the screen
// wall watches several at once). Empty while on the device list.
// Guarded by wsHub.mu.
+54 -15
View File
@@ -3,6 +3,7 @@ package web
import (
"encoding/json"
"errors"
"math"
"sort"
"strings"
"time"
@@ -803,11 +804,12 @@ func (h *wsHub) handleMouse(c *wsClient, raw []byte) {
return
}
var in struct {
Type string `json:"type"`
X int32 `json:"x"`
Y int32 `json:"y"`
Button int `json:"button"`
Delta int `json:"delta"`
Type string `json:"type"`
X int32 `json:"x"`
Y int32 `json:"y"`
Button int `json:"button"`
Delta float64 `json:"delta"`
DeltaMode int `json:"deltaMode"`
}
if err := json.Unmarshal(raw, &in); err != nil {
return
@@ -845,17 +847,13 @@ func (h *wsHub) handleMouse(c *wsClient, raw []byte) {
message = protocol.WMMouseMove
case "wheel":
message = protocol.WMMouseWheel
// Windows expects ±120 per notch in HIWORD(wParam). Browsers report
// `deltaY` in pixels with sign flipped relative to Win32 conventions
// (positive deltaY = scroll down). Normalize to one notch per call,
// signed so up scrolls "up" on the remote.
var wheel int16
switch {
case in.Delta > 0:
wheel = -120
case in.Delta < 0:
wheel = 120
// Windows expects ±120 per notch in HIWORD(wParam); wheelNotches turns the
// browser's distance into whole notches (see its doc comment).
notches := wheelNotches(&c.wheelResidue, in.Delta, in.DeltaMode)
if notches == 0 {
return // less than a notch so far: keep it for the next event
}
wheel := int16(-notches * 120) // positive deltaY = scroll down
wParam = uint64(uint16(wheel)) << 16
case "dblclick":
// Windows synthesizes double-click from rapid down/up sequences, so
@@ -872,6 +870,47 @@ func (h *wsHub) handleMouse(c *wsClient, raw []byte) {
_ = h.devices.SendToScreen(deviceID, pkt)
}
// maxWheelNotches caps how many wheel notches one browser event may inject.
// A touchpad fling or a scrollbar drag can report a huge delta; without the cap
// the remote jumps pages at a time.
const maxWheelNotches = 20
// wheelNotches converts one browser wheel event into whole Windows wheel
// notches, carrying the sub-notch remainder in *residue.
//
// The browser reports a *distance*, not a notch count: deltaMode 0 = pixels
// (Chrome's notch is about 100px), 1 = lines (about 3 per notch), 2 = pages.
// Chrome's smooth scrolling splits one physical notch into several events with
// fractional deltas (33.33 + 33.33 + 33.34) and touchpads go well below one
// pixel per event, so events have to be accumulated: reading the delta as an
// int dropped every fractional event (the wheel did nothing at all), and
// treating every event as one notch scrolled three to four times too far.
//
// Rounding, not truncation: the split events do not sum to exactly 1.0 in
// float (33.33*3 = 99.99), so an integer cast would sit at 0.999 forever. With
// rounding the first notch fires halfway through a physical one, which is how
// native scrolling feels, and *residue always stays within ±0.5 — it is a
// positional carry, not a bank of credit — so a change of direction needs no
// special case.
func wheelNotches(residue *float64, delta float64, deltaMode int) int {
unit := 100.0
switch deltaMode {
case 1:
unit = 3
case 2:
unit = 1
}
*residue += delta / unit
notches := int(math.Round(*residue))
*residue -= float64(notches)
if notches > maxWheelNotches {
notches = maxWheelNotches
} else if notches < -maxWheelNotches {
notches = -maxWheelNotches
}
return notches
}
// handleKey forwards one keyboard event to the device. lParam is built to
// match the Windows convention so applications relying on TranslateMessage
// (e.g. text input fields) behave correctly. Mirrors
+93
View File
@@ -0,0 +1,93 @@
package web
import "testing"
// Chrome's smooth scrolling splits one physical notch (100px) into a few
// fractional wheel events. Each event must not become a notch on its own, and
// the whole physical notch must inject exactly one.
func TestWheelNotchesSmoothScrollSequence(t *testing.T) {
var residue float64
seq := []float64{33.33, 33.33, 33.34}
total := 0
for _, d := range seq {
total += abs(wheelNotches(&residue, d, 0))
}
if total != 1 {
t.Fatalf("one 100px notch in %d events gave %d notches, want 1", len(seq), total)
}
if residue > 0.5 || residue < -0.5 {
t.Fatalf("residue must stay within half a notch, got %v", residue)
}
}
// Touchpads report sub-pixel deltas: they used to truncate to 0 and the wheel
// did nothing at all. They must accumulate into one notch.
func TestWheelNotchesSubPixelDeltas(t *testing.T) {
var residue float64
got := 0
for i := 0; i < 167; i++ { // 167 events x 0.6px = 100.2px
got += wheelNotches(&residue, 0.6, 0)
}
if got != 1 {
t.Fatalf("167 x 0.6px gave %d notches, want 1", got)
}
}
// The same split in three equal parts must also land on exactly one notch:
// 33.33*3 is 99.99 in float, and truncating would never reach a whole notch.
func TestWheelNotchesEqualThirds(t *testing.T) {
var residue float64
got := 0
for i := 0; i < 3; i++ {
got += wheelNotches(&residue, 33.33, 0)
}
if got != 1 {
t.Fatalf("3 x 33.33px gave %d notches, want 1", got)
}
}
func TestWheelNotchesDirection(t *testing.T) {
var residue float64
if n := wheelNotches(&residue, 100, 0); n != 1 {
t.Fatalf("down: got %d notches, want 1", n)
}
if n := wheelNotches(&residue, -100, 0); n != -1 {
t.Fatalf("up: got %d notches, want -1", n)
}
// A half-scrolled notch does not block a reversal: the carry is a position,
// so 40px down then a full notch up is one notch up.
residue = 0.4
if n := wheelNotches(&residue, -100, 0); n != -1 {
t.Fatalf("flip: got %d notches, want -1", n)
}
}
func TestWheelNotchesDeltaModes(t *testing.T) {
var pixel, line, page float64
if n := wheelNotches(&pixel, 100, 0); n != 1 {
t.Fatalf("pixel mode: got %d, want 1", n)
}
if n := wheelNotches(&line, 3, 1); n != 1 {
t.Fatalf("line mode: got %d, want 1", n)
}
if n := wheelNotches(&page, 1, 2); n != 1 {
t.Fatalf("page mode: got %d, want 1", n)
}
}
func TestWheelNotchesClamp(t *testing.T) {
var residue float64
if n := wheelNotches(&residue, 100000, 0); n != maxWheelNotches {
t.Fatalf("huge delta gave %d notches, want %d", n, maxWheelNotches)
}
if n := wheelNotches(&residue, -100000, 0); n != -maxWheelNotches {
t.Fatalf("huge negative delta gave %d notches, want %d", n, -maxWheelNotches)
}
}
func abs(n int) int {
if n < 0 {
return -n
}
return n
}
+4 -2
View File
@@ -5854,13 +5854,15 @@
};
}
function sendMouse(type, x, y, button, delta) {
function sendMouse(type, x, y, button, delta, deltaMode) {
if (!controlEnabled) return; // Control mode required
// Update cursor overlay position
updateCursorOverlay(x, y);
if (ws && ws.readyState === WebSocket.OPEN && token) {
const msg = { cmd: 'mouse', token, id: currentDeviceId(), type, x, y, button: button || 0 };
if (delta !== undefined) msg.delta = delta;
// deltaMode: 0=像素 1=行 2=页。服务端按它把 delta 换算成滚轮刻度。
if (deltaMode !== undefined) msg.deltaMode = deltaMode;
ws.send(JSON.stringify(msg));
}
}
@@ -6495,7 +6497,7 @@
canvas.addEventListener('wheel', function(e) {
e.preventDefault();
const pos = getMousePos(e);
sendMouse('wheel', pos.x, pos.y, 0, e.deltaY);
sendMouse('wheel', pos.x, pos.y, 0, e.deltaY, e.deltaMode);
}, { passive: false });
canvas.addEventListener('contextmenu', e => e.preventDefault());