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 +}