Compress mouse scroll and motion events #4

Merged
sashakoshka merged 3 commits from compress-scroll into main 2022-11-17 18:32:35 -07:00
4 changed files with 83 additions and 6 deletions

View File

@ -63,6 +63,12 @@ func (application *Application) OnMouseMove (
application.callbackManager.onMouseMove = onMouseMove application.callbackManager.onMouseMove = onMouseMove
} }
func (application *Application) OnScroll (
onScroll func (x, y int),
) {
application.callbackManager.onScroll = onScroll
}
func (application *Application) OnStart ( func (application *Application) OnStart (
onStart func (), onStart func (),
) { ) {

View File

@ -8,6 +8,23 @@ import "github.com/jezek/xgbutil/xevent"
import "git.tebibyte.media/sashakoshka/stone" 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 () { func (backend *Backend) Run () {
backend.callbackManager.RunStart() backend.callbackManager.RunStart()
backend.Draw() backend.Draw()
@ -68,7 +85,14 @@ func (backend *Backend) handleButtonPress (
event xevent.ButtonPressEvent, event xevent.ButtonPressEvent,
) { ) {
buttonEvent := *event.ButtonPressEvent buttonEvent := *event.ButtonPressEvent
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)) backend.callbackManager.RunPress(stone.Button(buttonEvent.Detail + 127))
}
} }
func (backend *Backend) handleButtonRelease ( func (backend *Backend) handleButtonRelease (
@ -76,6 +100,7 @@ func (backend *Backend) handleButtonRelease (
event xevent.ButtonReleaseEvent, event xevent.ButtonReleaseEvent,
) { ) {
buttonEvent := *event.ButtonReleaseEvent buttonEvent := *event.ButtonReleaseEvent
if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { return }
backend.callbackManager.RunRelease(stone.Button(buttonEvent.Detail + 127)) backend.callbackManager.RunRelease(stone.Button(buttonEvent.Detail + 127))
} }
@ -101,7 +126,7 @@ func (backend *Backend) handleMotionNotify (
connection *xgbutil.XUtil, connection *xgbutil.XUtil,
event xevent.MotionNotifyEvent, event xevent.MotionNotifyEvent,
) { ) {
motionEvent := *event.MotionNotifyEvent motionEvent := backend.compressMotionNotify(*event.MotionNotifyEvent)
x, y := backend.cellAt (image.Point { x, y := backend.cellAt (image.Point {
X: int(motionEvent.EventX), X: int(motionEvent.EventX),
Y: int(motionEvent.EventY), Y: int(motionEvent.EventY),
@ -132,3 +157,47 @@ func (backend *Backend) compressConfigureNotify (
return 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
}

View File

@ -6,6 +6,7 @@ type CallbackManager struct {
onRelease func (button Button) onRelease func (button Button)
onResize func () onResize func ()
onMouseMove func (x, y int) onMouseMove func (x, y int)
onScroll func (x, y int)
onStart func () onStart func ()
} }
@ -34,6 +35,11 @@ func (manager *CallbackManager) RunMouseMove (x, y int) {
manager.onMouseMove(x, y) manager.onMouseMove(x, y)
} }
func (manager *CallbackManager) RunScroll (x, y int) {
if manager.onScroll == nil { return }
manager.onScroll(x, y)
}
func (manager *CallbackManager) RunStart () { func (manager *CallbackManager) RunStart () {
if manager.onStart == nil { return } if manager.onStart == nil { return }
manager.onStart() manager.onStart()

View File

@ -52,10 +52,6 @@ const (
MouseButtonLeft Button = MouseButton1 MouseButtonLeft Button = MouseButton1
MouseButtonMiddle Button = MouseButton2 MouseButtonMiddle Button = MouseButton2
MouseButtonRight Button = MouseButton3 MouseButtonRight Button = MouseButton3
MouseButtonScrollUp Button = MouseButton4
MouseButtonScrollDown Button = MouseButton5
MouseButtonScrollLeft Button = MouseButton6
MouseButtonScrollRight Button = MouseButton7
MouseButtonBack Button = MouseButton8 MouseButtonBack Button = MouseButton8
MouseButtonForward Button = MouseButton9 MouseButtonForward Button = MouseButton9