1package core
2
3import (
4 "context"
5 "strconv"
6
7 "github.com/bnema/neferwl/internal/ports"
8)
9
10func (c *Core) handleInput(ctx context.Context, ev ports.InputEvent) error {
11 c.inputActive = true
12 defer func() { c.inputActive = false }()
13 if c.security.Protected {
14 if err := c.protectedInput(ctx, ev); err != nil {
15 return err
16 }
17 return nil
18 }
19 switch v := ev.(type) {
20 case ports.PointerMotion:
21 c.motionTime = v.Time
22 // A locked pointer stays still; relative motion still flows.
23 if c.constraint.Mode != ports.ConstraintLock {
24 c.cursorX, c.cursorY = c.constraint.Clamp(c.clampPointer(c.cursorX, c.cursorY, v.X, v.Y))
25 }
26 // The focused screen follows the pointer, so new windows
27 // and launchers open where the user is.
28 // Only a pointer entering another output switches: keyboard
29 // moves to another screen stick until then. Not mid-drag of a
30 // client; a window drag (drag.go) follows the pointer.
31 if i := c.screenAt(c.cursorX, c.cursorY); i >= 0 && c.screens[i].name() != c.pointerOutput && c.grab == 0 {
32 name := c.screens[i].name()
33 c.pointerOutput = name
34 if name != c.cur().name() {
35 c.focusScreen = i
36 if err := c.publish(ctx); err != nil {
37 return err
38 }
39 }
40 }
41 if c.drag != nil {
42 // The dragged window and the others get no pointer events.
43 return c.dragMotion(ctx, v.Time)
44 }
45 id, x, y := c.hit(c.cursorX, c.cursorY)
46 // Layout changes are intentionally re-hit-tested only on motion.
47 if id != c.pointer {
48 c.pointer = id
49 if err := c.command(ctx, ports.PointerFocus{ID: id, X: x, Y: y}); err != nil {
50 return err
51 }
52 }
53 c.pointerAt = [2]float64{x, y}
54 if id != 0 {
55 if err := c.command(ctx, ports.PointerMotionTo{ID: id, X: x, Y: y, DX: v.DX, DY: v.DY, UnaccelDX: v.UnaccelDX, UnaccelDY: v.UnaccelDY, Time: v.Time}); err != nil {
56 return err
57 }
58 }
59 return nil
60 case ports.PointerButton:
61 // Buttons a drag took stay in c.buttons until released, so the
62 // security gate admits their release; no client sees it.
63 if !v.Pressed && c.swallow[v.Button] {
64 delete(c.swallow, v.Button)
65 delete(c.buttons, v.Button)
66 return nil
67 }
68 if d := c.drag; d != nil {
69 if !v.Pressed && v.Button == d.button {
70 delete(c.buttons, v.Button)
71 return c.endDrag(ctx)
72 }
73 // Other buttons do nothing mid-drag; their releases neither.
74 if v.Pressed {
75 c.buttons[v.Button] = true
76 c.swallow[v.Button] = true
77 }
78 return nil
79 }
80 if started, err := c.startDrag(ctx, v); err != nil || started {
81 return err
82 }
83 if v.Pressed && c.pointer == 0 && c.grab == 0 && c.held() == 0 {
84 if picked, err := c.overviewClick(ctx); err != nil {
85 return err
86 } else if picked {
87 return nil
88 }
89 }
90 id := c.pointer
91 if c.grab != 0 {
92 id = c.grab
93 }
94 if v.Pressed && c.held() == 0 {
95 // A click outside an open menu closes it.
96 if err := c.dismissGrabs(ctx, id); err != nil {
97 return err
98 }
99 }
100 if v.Pressed {
101 if c.held() == 0 {
102 c.grab = id
103 }
104 c.buttons[v.Button] = true
105 c.lastButton = v.Button
106 } else {
107 delete(c.buttons, v.Button)
108 }
109 if id != 0 {
110 if err := c.command(ctx, ports.PointerButtonTo{ID: id, Button: v.Button, Pressed: v.Pressed, Time: v.Time}); err != nil {
111 return err
112 }
113 // A click on an on-demand layer gives it the keyboard.
114 if v.Pressed && c.onDemand(id) && c.keyboard.clickLayer(id, c.windowFocus()) {
115 if err := c.publish(ctx); err != nil {
116 return err
117 }
118 } else if l := c.keyboard.layer; v.Pressed && l != 0 && id != l && c.popupRoot(id) != l {
119 // A click anywhere else takes the keyboard back.
120 c.keyboard.takeBack()
121 if err := c.publish(ctx); err != nil {
122 return err
123 }
124 }
125 // A click focuses the window and its output.
126 s, w := c.screenOf(id)
127 if v.Pressed && s != nil && w == s.mon.Current() && (c.keyboard.sent != id || s != c.cur()) {
128 w.Click(id)
129 c.focusScreen = c.screenIndex(s.name())
130 if err := c.publish(ctx); err != nil {
131 return err
132 }
133 }
134 }
135 if c.held() == 0 {
136 c.grab = 0
137 }
138 return nil
139 case ports.PointerAxis:
140 c.scrollStop(v)
141 // In the overview, scrolling moves the selection.
142 if c.cur().mon.ov.open && !c.overviewKeyboardTaken() {
143 if c.cur().mon.overviewScroll(v) {
144 if err := c.workspaceVisible(ctx, true); err != nil {
145 return err
146 }
147 if err := c.publish(ctx); err != nil {
148 return err
149 }
150 }
151 return nil
152 }
153 // Scroll goes to the window under the pointer, which has the
154 // pointer focus even mid-drag. None during a window drag.
155 if c.pointer != 0 && c.drag == nil {
156 if err := c.command(ctx, ports.PointerAxisTo{ID: c.pointer, Axis: v}); err != nil {
157 return err
158 }
159 }
160 return nil
161 case ports.SwipeBegin:
162 if c.swipeBegin(v) {
163 return c.slid(ctx, false)
164 }
165 return nil
166 case ports.SwipeUpdate:
167 if c.swipeUpdate(v) {
168 return c.slid(ctx, false)
169 }
170 return nil
171 case ports.SwipeEnd:
172 return c.slid(ctx, c.swipeEnd(v))
173 }
174 key, ok := ev.(ports.KeyEvent)
175 if !ok {
176 return nil
177 }
178 c.mods = key.Mods
179 if c.drag != nil && key.Pressed && key.Keysym == "Escape" {
180 // Escape cancels the drag; its release goes nowhere either.
181 c.cancelDrag()
182 c.pressed[heldKey(key)] = true
183 if err := c.publish(ctx); err != nil {
184 return err
185 }
186 return c.rehit(ctx)
187 }
188 // The overview takes its keys before any window; others still
189 // run binds, and are not forwarded.
190 // A launcher or a menu holding the keyboard gets them first.
191 if mon := c.cur().mon; mon.ov.open && !c.overviewKeyboardTaken() {
192 if key.Pressed && mon.overviewKey(key) {
193 c.pressed[heldKey(key)] = true
194 if err := c.workspaceVisible(ctx, true); err != nil {
195 return err
196 }
197 if err := c.publish(ctx); err != nil {
198 return err
199 }
200 return nil
201 }
202 }
203 // A focused window inhibiting shortcuts gets every key:
204 // no bind runs (emergency quit and VT switch are handled by
205 // input before core).
206 if c.keyboard.inhibited(c.keyboardFocus()) {
207 if held := heldKey(key); !key.Pressed && c.pressed[held] {
208 // Its press ran a bind before inhibiting began: the
209 // window never saw it.
210 delete(c.pressed, held)
211 return nil
212 }
213 if err := c.command(ctx, ports.ForwardKey{ID: c.keyboard.inhibiting, Key: key}); err != nil {
214 return err
215 }
216 return nil
217 }
218 name := keyName(key.Keysym)
219 // A bind on the keysym wins; then the unshifted keysym (Cmd+Shift+1
220 // prints exclam on US); then the physical key (code:N).
221 action, bound := c.binds[binding{key: name, mods: key.Mods}]
222 if !bound && key.Base != "" {
223 action, bound = c.binds[binding{key: keyName(key.Base), mods: key.Mods}]
224 }
225 if !bound && key.Keycode != 0 {
226 action, bound = c.binds[binding{key: "code:" + strconv.FormatUint(uint64(key.Keycode), 10), mods: key.Mods}]
227 }
228 // Track presses by physical key: Shift may be released before the key.
229 held := heldKey(key)
230 if !key.Pressed {
231 consumed := c.pressed[held]
232 delete(c.pressed, held)
233 if consumed {
234 return nil
235 }
236 }
237 if key.Pressed {
238 if bound {
239 c.pressed[held] = true
240 if action == ActionScaleUp || action == ActionScaleDown {
241 dir := 1
242 if action == ActionScaleDown {
243 dir = -1
244 }
245 sc := c.cur()
246 prev := sc.scale
247 sc.setScale(StepScale(sc.info.Width, sc.info.Height, sc.scale, dir))
248 c.order()
249 if c.ch.Scales != nil && sc.scale != prev && sc.name() != "" {
250 // Never wait on persistence: a stalled save drops the change.
251 select {
252 case c.ch.Scales <- ports.ScaleChanged{Output: sc.name(), Scale: sc.scale}:
253 default:
254 }
255 }
256 if err := c.publish(ctx); err != nil {
257 return err
258 }
259 return nil
260 }
261 before := c.cur().mon.Current()
262 swiped := c.swipedWorkspace()
263 c.keyboard.takeBack() // a bind acts on the windows
264 effect := c.applyAction(action)
265 if effect.Quit {
266 return ErrQuit
267 }
268 if effect.Spawn {
269 argv := effect.Argv
270 if argv == nil {
271 argv = append([]string(nil), c.cfg.Terminal.Command...)
272 }
273 // Explicit actions are suppressed on a full launcher queue;
274 // never keep the input owner waiting across an epoch change.
275 c.trySpawn(ctx, ports.SpawnRequest{Argv: argv})
276 if c.inputEpochChanged() {
277 return errSecurityChanged
278 }
279 }
280 if c.swipedWorkspace() != swiped {
281 c.dropSwipe()
282 }
283 if err := c.workspaceVisible(ctx, c.cur().mon.Current() != before); err != nil {
284 return err
285 }
286 if effect.Close != 0 {
287 if err := c.command(ctx, ports.CloseWindow{ID: effect.Close}); err != nil {
288 return err
289 }
290 }
291 if err := c.publish(ctx); err != nil {
292 return err
293 }
294 return nil
295 }
296 c.pressed[held] = false
297 }
298 if id := c.keyboardFocus(); id != 0 && (!c.cur().mon.ov.open || c.overviewKeyboardTaken()) {
299 if err := c.command(ctx, ports.ForwardKey{ID: id, Key: key}); err != nil {
300 return err
301 }
302 }
303 return nil
304}