2023-01-08 23:03:19 -07:00
|
|
|
package x
|
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements"
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
import "github.com/jezek/xgbutil"
|
|
|
|
import "github.com/jezek/xgb/xproto"
|
|
|
|
import "github.com/jezek/xgbutil/xevent"
|
|
|
|
|
|
|
|
type scrollSum struct {
|
|
|
|
x, y int
|
|
|
|
}
|
|
|
|
|
2023-01-20 14:44:07 -07:00
|
|
|
const scrollDistance = 16
|
|
|
|
|
|
|
|
func (sum *scrollSum) add (button xproto.Button, window *Window, state uint16) {
|
|
|
|
shift :=
|
|
|
|
(state & xproto.ModMaskShift) > 0 ||
|
|
|
|
(state & window.backend.modifierMasks.shiftLock) > 0
|
|
|
|
if shift {
|
|
|
|
switch button {
|
|
|
|
case 4:
|
|
|
|
sum.x -= scrollDistance
|
|
|
|
case 5:
|
|
|
|
sum.x += scrollDistance
|
|
|
|
case 6:
|
|
|
|
sum.y -= scrollDistance
|
|
|
|
case 7:
|
|
|
|
sum.y += scrollDistance
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch button {
|
|
|
|
case 4:
|
|
|
|
sum.y -= scrollDistance
|
|
|
|
case 5:
|
|
|
|
sum.y += scrollDistance
|
|
|
|
case 6:
|
|
|
|
sum.x -= scrollDistance
|
|
|
|
case 7:
|
|
|
|
sum.x += scrollDistance
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
2023-01-20 14:44:07 -07:00
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-26 00:08:07 -07:00
|
|
|
func (window *Window) handleExpose (
|
|
|
|
connection *xgbutil.XUtil,
|
|
|
|
event xevent.ExposeEvent,
|
|
|
|
) {
|
|
|
|
_ = window.compressExpose(*event.ExposeEvent)
|
|
|
|
window.redrawChildEntirely()
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
func (window *Window) handleConfigureNotify (
|
|
|
|
connection *xgbutil.XUtil,
|
|
|
|
event xevent.ConfigureNotifyEvent,
|
|
|
|
) {
|
2023-01-15 10:23:13 -07:00
|
|
|
if window.child == nil { return }
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
configureEvent := *event.ConfigureNotifyEvent
|
|
|
|
|
|
|
|
newWidth := int(configureEvent.Width)
|
|
|
|
newHeight := int(configureEvent.Height)
|
|
|
|
sizeChanged :=
|
|
|
|
window.metrics.width != newWidth ||
|
|
|
|
window.metrics.height != newHeight
|
|
|
|
window.metrics.width = newWidth
|
|
|
|
window.metrics.height = newHeight
|
|
|
|
|
|
|
|
if sizeChanged {
|
|
|
|
configureEvent = window.compressConfigureNotify(configureEvent)
|
|
|
|
window.metrics.width = int(configureEvent.Width)
|
|
|
|
window.metrics.height = int(configureEvent.Height)
|
|
|
|
window.reallocateCanvas()
|
|
|
|
window.resizeChildToFit()
|
2023-01-26 00:08:07 -07:00
|
|
|
|
|
|
|
if !window.exposeEventFollows(configureEvent) {
|
|
|
|
window.redrawChildEntirely()
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 00:08:07 -07:00
|
|
|
func (window *Window) exposeEventFollows (event xproto.ConfigureNotifyEvent) (found bool) {
|
|
|
|
nextEvents := xevent.Peek(window.backend.connection)
|
|
|
|
if len(nextEvents) > 0 {
|
|
|
|
untypedEvent := nextEvents[0]
|
|
|
|
if untypedEvent.Err == nil {
|
|
|
|
typedEvent, ok :=
|
|
|
|
untypedEvent.Event.(xproto.ConfigureNotifyEvent)
|
|
|
|
|
|
|
|
if ok && typedEvent.Window == event.Window {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-20 15:40:28 -07:00
|
|
|
func (window *Window) modifiersFromState (
|
|
|
|
state uint16,
|
|
|
|
) (
|
2023-02-01 23:47:55 -07:00
|
|
|
modifiers input.Modifiers,
|
2023-01-20 15:40:28 -07:00
|
|
|
) {
|
2023-02-01 23:47:55 -07:00
|
|
|
return input.Modifiers {
|
2023-01-20 15:40:28 -07:00
|
|
|
Shift:
|
|
|
|
(state & xproto.ModMaskShift) > 0 ||
|
|
|
|
(state & window.backend.modifierMasks.shiftLock) > 0,
|
|
|
|
Control: (state & xproto.ModMaskControl) > 0,
|
|
|
|
Alt: (state & window.backend.modifierMasks.alt) > 0,
|
|
|
|
Meta: (state & window.backend.modifierMasks.meta) > 0,
|
|
|
|
Super: (state & window.backend.modifierMasks.super) > 0,
|
|
|
|
Hyper: (state & window.backend.modifierMasks.hyper) > 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
func (window *Window) handleKeyPress (
|
|
|
|
connection *xgbutil.XUtil,
|
|
|
|
event xevent.KeyPressEvent,
|
|
|
|
) {
|
2023-01-20 14:44:07 -07:00
|
|
|
if window.child == nil { return }
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
keyEvent := *event.KeyPressEvent
|
|
|
|
key, numberPad := window.backend.keycodeToKey(keyEvent.Detail, keyEvent.State)
|
2023-01-20 15:40:28 -07:00
|
|
|
modifiers := window.modifiersFromState(keyEvent.State)
|
|
|
|
modifiers.NumberPad = numberPad
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
if key == input.KeyTab && modifiers.Alt {
|
|
|
|
if child, ok := window.child.(elements.Focusable); ok {
|
|
|
|
direction := input.KeynavDirectionForward
|
2023-01-15 10:23:13 -07:00
|
|
|
if modifiers.Shift {
|
2023-02-01 23:47:55 -07:00
|
|
|
direction = input.KeynavDirectionBackward
|
2023-01-11 16:32:02 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
if !child.HandleFocus(direction) {
|
|
|
|
child.HandleUnfocus()
|
2023-01-16 10:21:47 -07:00
|
|
|
}
|
2023-01-11 16:32:02 -07:00
|
|
|
}
|
2023-02-01 23:47:55 -07:00
|
|
|
} else if child, ok := window.child.(elements.KeyboardTarget); ok {
|
2023-01-20 15:40:28 -07:00
|
|
|
child.HandleKeyDown(key, modifiers)
|
2023-01-11 16:32:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
func (window *Window) handleKeyRelease (
|
|
|
|
connection *xgbutil.XUtil,
|
|
|
|
event xevent.KeyReleaseEvent,
|
|
|
|
) {
|
|
|
|
if window.child == nil { return }
|
|
|
|
|
|
|
|
keyEvent := *event.KeyReleaseEvent
|
2023-01-20 15:40:28 -07:00
|
|
|
|
|
|
|
// do not process this event if it was generated from a key repeat
|
|
|
|
nextEvents := xevent.Peek(window.backend.connection)
|
|
|
|
if len(nextEvents) > 0 {
|
|
|
|
untypedEvent := nextEvents[0]
|
|
|
|
if untypedEvent.Err == nil {
|
|
|
|
typedEvent, ok :=
|
|
|
|
untypedEvent.Event.(xproto.KeyReleaseEvent)
|
|
|
|
|
|
|
|
if ok && typedEvent.Detail == keyEvent.Detail &&
|
|
|
|
typedEvent.Event == keyEvent.Event &&
|
|
|
|
typedEvent.State == keyEvent.State {
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
2023-01-15 10:23:13 -07:00
|
|
|
|
2023-01-20 15:40:28 -07:00
|
|
|
key, numberPad := window.backend.keycodeToKey(keyEvent.Detail, keyEvent.State)
|
|
|
|
modifiers := window.modifiersFromState(keyEvent.State)
|
|
|
|
modifiers.NumberPad = numberPad
|
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
if child, ok := window.child.(elements.KeyboardTarget); ok {
|
2023-01-15 10:23:13 -07:00
|
|
|
child.HandleKeyUp(key, modifiers)
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) handleButtonPress (
|
|
|
|
connection *xgbutil.XUtil,
|
|
|
|
event xevent.ButtonPressEvent,
|
|
|
|
) {
|
|
|
|
if window.child == nil { return }
|
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
if child, ok := window.child.(elements.MouseTarget); ok {
|
2023-01-15 10:23:13 -07:00
|
|
|
buttonEvent := *event.ButtonPressEvent
|
|
|
|
if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 {
|
|
|
|
sum := scrollSum { }
|
2023-01-20 14:44:07 -07:00
|
|
|
sum.add(buttonEvent.Detail, window, buttonEvent.State)
|
2023-01-15 10:23:13 -07:00
|
|
|
window.compressScrollSum(buttonEvent, &sum)
|
2023-01-19 13:05:13 -07:00
|
|
|
child.HandleMouseScroll (
|
2023-01-15 10:23:13 -07:00
|
|
|
int(buttonEvent.EventX),
|
|
|
|
int(buttonEvent.EventY),
|
|
|
|
float64(sum.x), float64(sum.y))
|
|
|
|
} else {
|
|
|
|
child.HandleMouseDown (
|
|
|
|
int(buttonEvent.EventX),
|
|
|
|
int(buttonEvent.EventY),
|
2023-02-01 23:47:55 -07:00
|
|
|
input.Button(buttonEvent.Detail))
|
2023-01-15 10:23:13 -07:00
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
2023-01-15 10:23:13 -07:00
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) handleButtonRelease (
|
|
|
|
connection *xgbutil.XUtil,
|
|
|
|
event xevent.ButtonReleaseEvent,
|
|
|
|
) {
|
2023-01-15 10:23:13 -07:00
|
|
|
if window.child == nil { return }
|
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
if child, ok := window.child.(elements.MouseTarget); ok {
|
2023-01-15 10:23:13 -07:00
|
|
|
buttonEvent := *event.ButtonReleaseEvent
|
|
|
|
if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { return }
|
|
|
|
child.HandleMouseUp (
|
|
|
|
int(buttonEvent.EventX),
|
|
|
|
int(buttonEvent.EventY),
|
2023-02-01 23:47:55 -07:00
|
|
|
input.Button(buttonEvent.Detail))
|
2023-01-15 10:23:13 -07:00
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) handleMotionNotify (
|
|
|
|
connection *xgbutil.XUtil,
|
|
|
|
event xevent.MotionNotifyEvent,
|
|
|
|
) {
|
2023-01-15 10:23:13 -07:00
|
|
|
if window.child == nil { return }
|
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
if child, ok := window.child.(elements.MouseTarget); ok {
|
2023-01-15 10:23:13 -07:00
|
|
|
motionEvent := window.compressMotionNotify(*event.MotionNotifyEvent)
|
|
|
|
child.HandleMouseMove (
|
|
|
|
int(motionEvent.EventX),
|
|
|
|
int(motionEvent.EventY))
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-26 00:08:07 -07:00
|
|
|
func (window *Window) compressExpose (
|
|
|
|
firstEvent xproto.ExposeEvent,
|
|
|
|
) (
|
|
|
|
lastEvent xproto.ExposeEvent,
|
|
|
|
) {
|
|
|
|
window.backend.connection.Sync()
|
|
|
|
xevent.Read(window.backend.connection, false)
|
|
|
|
lastEvent = firstEvent
|
|
|
|
|
|
|
|
for index, untypedEvent := range xevent.Peek(window.backend.connection) {
|
|
|
|
if untypedEvent.Err != nil { continue }
|
|
|
|
|
|
|
|
typedEvent, ok := untypedEvent.Event.(xproto.ExposeEvent)
|
|
|
|
if !ok { continue }
|
|
|
|
|
|
|
|
// FIXME: union all areas into the last event
|
|
|
|
if firstEvent.Window == typedEvent.Window {
|
|
|
|
lastEvent = typedEvent
|
|
|
|
defer func (index int) {
|
|
|
|
xevent.DequeueAt(window.backend.connection, index)
|
|
|
|
} (index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
func (window *Window) compressConfigureNotify (
|
|
|
|
firstEvent xproto.ConfigureNotifyEvent,
|
|
|
|
) (
|
|
|
|
lastEvent xproto.ConfigureNotifyEvent,
|
|
|
|
) {
|
|
|
|
window.backend.connection.Sync()
|
|
|
|
xevent.Read(window.backend.connection, false)
|
|
|
|
lastEvent = firstEvent
|
|
|
|
|
|
|
|
for index, untypedEvent := range xevent.Peek(window.backend.connection) {
|
|
|
|
if untypedEvent.Err != nil { continue }
|
|
|
|
|
|
|
|
typedEvent, ok := untypedEvent.Event.(xproto.ConfigureNotifyEvent)
|
|
|
|
if !ok { continue }
|
|
|
|
|
|
|
|
if firstEvent.Event == typedEvent.Event &&
|
|
|
|
firstEvent.Window == typedEvent.Window {
|
|
|
|
|
|
|
|
lastEvent = typedEvent
|
|
|
|
defer func (index int) {
|
|
|
|
xevent.DequeueAt(window.backend.connection, index)
|
|
|
|
} (index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) compressScrollSum (
|
|
|
|
firstEvent xproto.ButtonPressEvent,
|
|
|
|
sum *scrollSum,
|
|
|
|
) {
|
|
|
|
window.backend.connection.Sync()
|
|
|
|
xevent.Read(window.backend.connection, false)
|
|
|
|
|
|
|
|
for index, untypedEvent := range xevent.Peek(window.backend.connection) {
|
|
|
|
if untypedEvent.Err != nil { continue }
|
|
|
|
|
|
|
|
typedEvent, ok := untypedEvent.Event.(xproto.ButtonPressEvent)
|
|
|
|
if !ok { continue }
|
|
|
|
|
|
|
|
if firstEvent.Event == typedEvent.Event &&
|
|
|
|
typedEvent.Detail >= 4 &&
|
|
|
|
typedEvent.Detail <= 7 {
|
|
|
|
|
2023-01-20 14:44:07 -07:00
|
|
|
sum.add(typedEvent.Detail, window, typedEvent.State)
|
2023-01-08 23:03:19 -07:00
|
|
|
defer func (index int) {
|
|
|
|
xevent.DequeueAt(window.backend.connection, index)
|
|
|
|
} (index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) compressMotionNotify (
|
|
|
|
firstEvent xproto.MotionNotifyEvent,
|
|
|
|
) (
|
|
|
|
lastEvent xproto.MotionNotifyEvent,
|
|
|
|
) {
|
|
|
|
window.backend.connection.Sync()
|
|
|
|
xevent.Read(window.backend.connection, false)
|
|
|
|
lastEvent = firstEvent
|
|
|
|
|
|
|
|
for index, untypedEvent := range xevent.Peek(window.backend.connection) {
|
|
|
|
if untypedEvent.Err != nil { continue }
|
|
|
|
|
|
|
|
typedEvent, ok := untypedEvent.Event.(xproto.MotionNotifyEvent)
|
|
|
|
if !ok { continue }
|
|
|
|
|
2023-01-20 23:24:24 -07:00
|
|
|
if firstEvent.Event == typedEvent.Event {
|
2023-01-08 23:03:19 -07:00
|
|
|
lastEvent = typedEvent
|
|
|
|
defer func (index int) {
|
|
|
|
xevent.DequeueAt(window.backend.connection, index)
|
|
|
|
} (index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|