From 43696543d84276139215db7c7617936d8f3b6ab7 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 17 Nov 2022 19:01:16 -0500 Subject: [PATCH 1/3] Compress motion notify events --- backends/x/event.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/backends/x/event.go b/backends/x/event.go index 7a49f26..3631ca6 100644 --- a/backends/x/event.go +++ b/backends/x/event.go @@ -101,7 +101,7 @@ func (backend *Backend) handleMotionNotify ( connection *xgbutil.XUtil, event xevent.MotionNotifyEvent, ) { - motionEvent := *event.MotionNotifyEvent + motionEvent := backend.compressMotionNotify(*event.MotionNotifyEvent) x, y := backend.cellAt (image.Point { X: int(motionEvent.EventX), Y: int(motionEvent.EventY), @@ -132,3 +132,27 @@ func (backend *Backend) compressConfigureNotify ( return } + +func (backend *Backend) compressMotionNotify ( + firstEvent xproto.MotionNotifyEvent, +) ( + lastEvent xproto.MotionNotifyEvent, +) { + backend.connection.Sync() + xevent.Read(backend.connection, false) + lastEvent = firstEvent + + for index, untypedEvent := range xevent.Peek(backend.connection) { + if untypedEvent.Err != nil { continue } + + typedEvent, ok := untypedEvent.Event.(xproto.MotionNotifyEvent) + if !ok { continue } + + lastEvent = typedEvent + defer func (index int) { + xevent.DequeueAt(backend.connection, index) + } (index) + } + + return +} -- 2.46.1 From c57c8acba77560c389e0bafa512170340b2da1f9 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 17 Nov 2022 19:11:49 -0500 Subject: [PATCH 2/3] Changed the API for scrolling --- application.go | 6 ++++++ backends/x/event.go | 16 +++++++++++++++- event.go | 6 ++++++ input.go | 4 ---- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/application.go b/application.go index a97ccd3..4929cc7 100644 --- a/application.go +++ b/application.go @@ -63,6 +63,12 @@ func (application *Application) OnMouseMove ( application.callbackManager.onMouseMove = onMouseMove } +func (application *Application) OnScroll ( + onScroll func (x, y int), +) { + application.callbackManager.onScroll = onScroll +} + func (application *Application) OnStart ( onStart func (), ) { diff --git a/backends/x/event.go b/backends/x/event.go index 3631ca6..17726f4 100644 --- a/backends/x/event.go +++ b/backends/x/event.go @@ -68,7 +68,20 @@ func (backend *Backend) handleButtonPress ( event xevent.ButtonPressEvent, ) { buttonEvent := *event.ButtonPressEvent - backend.callbackManager.RunPress(stone.Button(buttonEvent.Detail + 127)) + 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) + } + } else { + backend.callbackManager.RunPress(stone.Button(buttonEvent.Detail + 127)) + } } func (backend *Backend) handleButtonRelease ( @@ -76,6 +89,7 @@ func (backend *Backend) handleButtonRelease ( event xevent.ButtonReleaseEvent, ) { buttonEvent := *event.ButtonReleaseEvent + if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { return } backend.callbackManager.RunRelease(stone.Button(buttonEvent.Detail + 127)) } diff --git a/event.go b/event.go index 1032e32..edbaf2c 100644 --- a/event.go +++ b/event.go @@ -6,6 +6,7 @@ type CallbackManager struct { onRelease func (button Button) onResize func () onMouseMove func (x, y int) + onScroll func (x, y int) onStart func () } @@ -34,6 +35,11 @@ func (manager *CallbackManager) RunMouseMove (x, y int) { manager.onMouseMove(x, y) } +func (manager *CallbackManager) RunScroll (x, y int) { + if manager.onScroll == nil { return } + manager.onScroll(x, y) +} + func (manager *CallbackManager) RunStart () { if manager.onStart == nil { return } manager.onStart() diff --git a/input.go b/input.go index 570d35d..e3aba82 100644 --- a/input.go +++ b/input.go @@ -52,10 +52,6 @@ const ( MouseButtonLeft Button = MouseButton1 MouseButtonMiddle Button = MouseButton2 MouseButtonRight Button = MouseButton3 - MouseButtonScrollUp Button = MouseButton4 - MouseButtonScrollDown Button = MouseButton5 - MouseButtonScrollLeft Button = MouseButton6 - MouseButtonScrollRight Button = MouseButton7 MouseButtonBack Button = MouseButton8 MouseButtonForward Button = MouseButton9 -- 2.46.1 From 75200a73101b44d34c105d4a9d3ab88aca452e92 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 17 Nov 2022 19:23:39 -0500 Subject: [PATCH 3/3] 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 +} -- 2.46.1