1package core
2
3import (
4 "context"
5
6 "github.com/bnema/neferwl/internal/ports"
7)
8
9// clientEvent applies one client event. publish asks for a new scene;
10// stop ends Run (a command could not be delivered).
11func (c *Core) clientEvent(ctx context.Context, ev ports.ClientEvent) (publish, stop bool) {
12 switch v := ev.(type) {
13 case ports.SessionLockChanged:
14 if !c.applyLockChanged(v) {
15 return false, false
16 }
17 case ports.CaptureSessionOpen:
18 c.captureOpen(v)
19 case ports.CaptureSessionClose:
20 c.captureClose(v.ID)
21 case ports.CaptureFrameTaken:
22 // Nothing new to show (the target flashes already): no scene.
23 if !c.captureFrame(v) {
24 return false, false
25 }
26 case ports.CaptureExclusionBegin:
27 c.captureExclusionBegin(v)
28 case ports.CaptureExclusionLayer:
29 c.captureExclusionLayer(v)
30 case ports.CaptureExclusionEnd:
31 c.captureExclusionEnd(v.Session)
32 case ports.LayerChanged:
33 c.layerChanged = true
34 c.setLayers(v.Layers)
35 case ports.InputRegionChanged:
36 c.windows.setRegion(v)
37 case ports.WindowMapped:
38 c.mapWindow(v)
39 case ports.WindowResized:
40 c.resizeFloating(v)
41 case ports.PopupRequest:
42 if err := c.placePopup(ctx, v); err != nil {
43 return false, true
44 }
45 case ports.PopupMapped:
46 if p := c.popups[v.ID]; p != nil {
47 p.mapped = true
48 }
49 case ports.ShortcutsInhibit:
50 c.windows.setInhibitShortcuts(v.Window, v.Active)
51 if err := c.updateInhibit(ctx); err != nil {
52 return false, true
53 }
54 return false, false
55 case ports.OutputPower:
56 if i := c.screenIndex(v.Output); i >= 0 && c.screens[i].off == v.On {
57 c.screens[i].off = !v.On
58 }
59 case ports.IdleInhibit:
60 c.windows.setIdleInhibit(v.Window, v.Active)
61 c.publishState()
62 return false, false
63 case ports.WindowAppID:
64 c.windows.setAppID(v.ID, v.AppID)
65 case ports.WindowParent:
66 if _, w := c.screenOf(v.ID); w != nil {
67 w.SetDialogParent(v.ID, v.Parent)
68 }
69 case ports.WindowModal:
70 if _, w := c.screenOf(v.ID); w != nil {
71 w.SetModal(v.ID, v.Modal)
72 }
73 case ports.WindowUnmapped:
74 if c.unmapClient(ctx, v) != nil {
75 return false, true
76 }
77 case ports.PointerConstrained:
78 c.constrained = v
79 case ports.PointerWarp:
80 if c.warpPointer(ctx, v) != nil {
81 return false, true
82 }
83 case ports.WindowMoveRequest:
84 if c.clientDrag(ctx, v) != nil {
85 return false, true
86 }
87 case ports.DataDragStarted:
88 if c.startDataDrag(ctx, v) != nil {
89 return false, true
90 }
91 return false, false
92 case ports.DataDragIcon:
93 if !c.setDataDragIcon(v) {
94 return false, false
95 }
96 case ports.DataDragEnded:
97 if c.dataDrag == nil {
98 return false, false
99 }
100 c.abortDataDrag()
101 if c.finishDataDrag(ctx) != nil {
102 return false, true
103 }
104 return false, false
105 case ports.WindowFullscreenRequest:
106 if c.fullscreenRequest(ctx, v) != nil {
107 return false, true
108 }
109 case ports.WorkspaceActivate:
110 if c.activateWorkspaces(ctx, v) != nil {
111 return false, true
112 }
113 case ports.WindowActivate:
114 if c.activate(ctx, v.ID) != nil {
115 return false, true
116 }
117 }
118 return true, false
119}
120
121// unmapClient drops a window or popup the client unmapped.
122func (c *Core) unmapClient(ctx context.Context, v ports.WindowUnmapped) error {
123 if c.popups[v.ID] != nil {
124 if err := c.dropPopup(ctx, v.ID); err != nil {
125 return err
126 }
127 }
128 if err := c.closePopupsOf(ctx, v.ID); err != nil {
129 return err
130 }
131 c.windows.drop(v.ID)
132 if c.drag != nil && c.drag.id == v.ID {
133 c.abortDrag()
134 }
135 if err := c.dataDragGone(ctx, v.ID); err != nil {
136 return err
137 }
138 c.unmapWindow(v)
139 if c.pgrab != nil && !c.pgrab.valid() {
140 if err := c.abortPointerGrab(ctx); err != nil {
141 return err
142 }
143 }
144 c.releaseSlots()
145 if c.pointer == v.ID {
146 c.pointer = 0
147 if err := c.pointerFocus(ctx, ports.PointerFocus{}); err != nil {
148 return err
149 }
150 }
151 return nil
152}
153
154// fullscreenRequest answers xdg_toplevel set/unset_fullscreen, from the
155// window itself or a taskbar (External).
156func (c *Core) fullscreenRequest(ctx context.Context, v ports.WindowFullscreenRequest) error {
157 c.configures.answer[v.ID] = true
158 if v.Fullscreen && !v.External && c.now().Sub(c.windows.lookup(v.ID).mappedAt) < fullscreenGrace {
159 return nil
160 }
161 // A taskbar request is a user action on that window, as an
162 // activation: it comes on screen with the focus, leaving
163 // another window's fullscreen.
164 if v.External && v.Fullscreen {
165 if err := c.activate(ctx, v.ID); err != nil {
166 return err
167 }
168 }
169 if s, _ := c.screenOf(v.ID); s != nil {
170 s.mon.SetFullscreen(v.ID, v.Fullscreen)
171 if w := s.mon.Current(); v.Fullscreen && w.fullscreen == v.ID {
172 // A taskbar or late fullscreen request that took
173 // effect: the window does not fade in, it keeps the
174 // direct scanout path. One that was ignored (the
175 // grace after the map) leaves the entrance running.
176 delete(s.rects, v.ID)
177 }
178 }
179 return nil
180}
181
182// activateWorkspaces shows the workspaces of v by stable ID, in order.
183func (c *Core) activateWorkspaces(ctx context.Context, v ports.WorkspaceActivate) error {
184 before := c.cur().mon.Current()
185 for _, id := range v.IDs {
186 for i, sc := range c.screens {
187 if sc.name() == "" {
188 continue
189 }
190 for w := range sc.mon.all() {
191 if w.ID == id {
192 c.focusScreen = i
193 sc.mon.show(w)
194 break
195 }
196 }
197 }
198 }
199 return c.workspaceVisible(ctx, c.cur().mon.Current() != before)
200}