-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput.go
186 lines (149 loc) · 4.86 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"os"
"time"
"deedles.dev/wlr"
"deedles.dev/wlr/xkb"
"deedles.dev/ximage/geom"
)
type CursorMover interface {
CursorMoved(*Server, time.Time)
}
type CursorButtonPresser interface {
CursorButtonPressed(*Server, wlr.Pointer, wlr.CursorButton, time.Time)
}
type CursorButtonReleaser interface {
CursorButtonReleased(*Server, wlr.Pointer, wlr.CursorButton, time.Time)
}
type CursorRequester interface {
RequestCursor(*Server, wlr.Surface, int, int)
}
type Keyboard struct {
Device wlr.Keyboard
onModifiersListener wlr.Listener
onKeyListener wlr.Listener
}
func (server *Server) onNewInput(device wlr.InputDevice) {
switch device.Type() {
case wlr.InputDeviceTypeKeyboard:
server.addKeyboard(device.Keyboard())
case wlr.InputDeviceTypePointer:
server.addPointer(device.Pointer())
}
}
func (server *Server) onKeyboardModifiers(kb *Keyboard) {
server.seat.SetKeyboard(kb.Device)
server.seat.KeyboardNotifyModifiers(kb.Device.Modifiers())
}
func (server *Server) onKeyboardKey(kb *Keyboard, code uint32, update bool, state wlr.KeyState, t time.Time) {
switch state {
case wlr.KeyStatePressed:
server.onKeyboardKeyPressed(kb, code, update, t)
case wlr.KeyStateReleased:
server.onKeyboardKeyReleased(kb, code, update, t)
}
}
func (server *Server) onKeyboardKeyPressed(kb *Keyboard, code uint32, update bool, t time.Time) {
if server.handleKeyboardShortcut(kb, code, t) {
return
}
server.seat.SetKeyboard(kb.Device)
server.seat.KeyboardNotifyKey(t, code, wlr.KeyStatePressed)
}
func (server *Server) onKeyboardKeyReleased(kb *Keyboard, code uint32, update bool, t time.Time) {
server.seat.SetKeyboard(kb.Device)
server.seat.KeyboardNotifyKey(t, code, wlr.KeyStateReleased)
}
func (server *Server) onCursorMotion(dev wlr.Pointer, t time.Time, dx, dy float64) {
server.cursor.Move(dev.Base(), dx, dy)
m, ok := server.inputMode.(CursorMover)
if ok {
m.CursorMoved(server, t)
}
}
func (server *Server) onCursorMotionAbsolute(dev wlr.Pointer, t time.Time, x, y float64) {
server.cursor.WarpAbsolute(dev.Base(), x, y)
m, ok := server.inputMode.(CursorMover)
if ok {
m.CursorMoved(server, t)
}
}
func (server *Server) onCursorButton(dev wlr.Pointer, t time.Time, b wlr.CursorButton, state wlr.ButtonState) {
switch state {
case wlr.ButtonPressed:
m, ok := server.inputMode.(CursorButtonPresser)
if ok {
m.CursorButtonPressed(server, dev, b, t)
}
case wlr.ButtonReleased:
m, ok := server.inputMode.(CursorButtonReleaser)
if ok {
m.CursorButtonReleased(server, dev, b, t)
}
}
}
func (server *Server) onCursorAxis(dev wlr.Pointer, t time.Time, source wlr.AxisSource, orient wlr.AxisOrientation, delta float64, deltaDiscrete int32) {
server.seat.PointerNotifyAxis(t, orient, delta, deltaDiscrete, source)
}
func (server *Server) onCursorFrame() {
server.seat.PointerNotifyFrame()
}
func (server *Server) onRequestCursor(client wlr.SeatClient, surface wlr.Surface, serial uint32, hotspotX, hotspotY int32) {
m, ok := server.inputMode.(CursorRequester)
if !ok {
return
}
focused := server.seat.PointerState().FocusedClient()
if focused == client {
m.RequestCursor(server, surface, int(hotspotX), int(hotspotY))
}
}
func (server *Server) addKeyboard(dev wlr.Keyboard) {
kb := Keyboard{
Device: dev,
}
rules := xkb.RuleNames{
Rules: os.Getenv("XKB_DEFAULT_RULES"),
Model: os.Getenv("XKB_DEFAULT_MODEL"),
Layout: os.Getenv("XKB_DEFAULT_LAYOUT"),
Variant: os.Getenv("XKB_DEFAULT_VARIANT"),
Options: os.Getenv("XKB_DEFAULT_OPTIONS"),
}
ctx := xkb.NewContext(xkb.ContextNoFlags)
defer ctx.Unref()
keymap := xkb.NewKeymapFromNames(ctx, &rules, xkb.KeymapCompileNoFlags)
defer keymap.Unref()
kb.Device.SetKeymap(keymap)
kb.Device.SetRepeatInfo(25, 600)
kb.onModifiersListener = kb.Device.OnModifiers(func(k wlr.Keyboard) {
server.onKeyboardModifiers(&kb)
})
kb.onKeyListener = kb.Device.OnKey(func(k wlr.Keyboard, t time.Time, code uint32, update bool, state wlr.KeyState) {
server.onKeyboardKey(&kb, code, update, state, t)
})
server.seat.SetKeyboard(dev)
server.keyboards = append(server.keyboards, &kb)
server.seat.SetCapabilities(server.seat.Capabilities() | wlr.SeatCapabilityKeyboard)
}
func (server *Server) addPointer(dev wlr.Pointer) {
server.cursor.AttachInputDevice(dev.Base())
server.seat.SetCapabilities(server.seat.Capabilities() | wlr.SeatCapabilityPointer)
server.setCursor("left_ptr")
server.pointers = append(server.pointers, dev)
}
func (server *Server) setCursor(name string) {
if name == "" {
return
}
if server.xwayland.Valid() {
server.xwayland.SetCursor(server.cursorMgr.GetXCursor(name, 1).Image(0))
}
server.cursor.SetXCursor(server.cursorMgr, name)
}
func (server *Server) handleKeyboardShortcut(kb *Keyboard, code uint32, t time.Time) bool {
// TODO
return false
}
func (server *Server) cursorCoords() geom.Point[float64] {
return geom.Pt(server.cursor.X(), server.cursor.Y())
}