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 7a49f26..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() @@ -68,7 +85,14 @@ 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 { + 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)) + } } func (backend *Backend) handleButtonRelease ( @@ -76,6 +100,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)) } @@ -101,7 +126,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 +157,47 @@ 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 +} + +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 +} 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