From 75200a73101b44d34c105d4a9d3ab88aca452e92 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 17 Nov 2022 19:23:39 -0500 Subject: [PATCH] Mouse scroll events are properly compressed and summed up --- backends/x/event.go | 51 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/backends/x/event.go b/backends/x/event.go index 17726f4..0508407 100644 --- a/backends/x/event.go +++ b/backends/x/event.go @@ -8,6 +8,23 @@ import "github.com/jezek/xgbutil/xevent" import "git.tebibyte.media/sashakoshka/stone" +type scrollSum struct { + x, y int +} + +func (sum *scrollSum) add (button xproto.Button) { + switch button { + case 4: + sum.y -- + case 5: + sum.y ++ + case 6: + sum.x -- + case 7: + sum.x ++ + } +} + func (backend *Backend) Run () { backend.callbackManager.RunStart() backend.Draw() @@ -69,16 +86,10 @@ func (backend *Backend) handleButtonPress ( ) { buttonEvent := *event.ButtonPressEvent if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { - switch buttonEvent.Detail { - case 4: - backend.callbackManager.RunScroll(0, -1) - case 5: - backend.callbackManager.RunScroll(0, 1) - case 6: - backend.callbackManager.RunScroll(-1, 0) - case 7: - backend.callbackManager.RunScroll(1, 0) - } + sum := scrollSum { } + sum.add(buttonEvent.Detail) + backend.compressScrollSum(&sum) + backend.callbackManager.RunScroll(sum.x, sum.y) } else { backend.callbackManager.RunPress(stone.Button(buttonEvent.Detail + 127)) } @@ -170,3 +181,23 @@ func (backend *Backend) compressMotionNotify ( return } + +func (backend *Backend) compressScrollSum (sum *scrollSum) { + backend.connection.Sync() + xevent.Read(backend.connection, false) + + + for index, untypedEvent := range xevent.Peek(backend.connection) { + if untypedEvent.Err != nil { continue } + + typedEvent, ok := untypedEvent.Event.(xproto.ButtonPressEvent) + if !ok { continue } + + sum.add(typedEvent.Detail) + defer func (index int) { + xevent.DequeueAt(backend.connection, index) + } (index) + } + + return +}