1package wayland
2
3import (
4 "github.com/bnema/go-wayland-bindings/server/idleinhibit"
5 "github.com/bnema/go-wayland-bindings/server/keyboardshortcutsinhibit"
6 "github.com/bnema/go-wayland-bindings/server/wayland"
7 "github.com/bnema/neferwl/internal/ports"
8 "github.com/bnema/purego-libwayland/server"
9)
10
11// Inhibitors. keyboard-shortcuts-inhibit: a window asks for every key,
12// compositor binds included, while it has keyboard focus; core decides
13// when it is in effect (ports.ShortcutsInhibitState) and the client hears
14// active/inactive. idle-inhibit: a window keeps the session awake while
15// it is mapped. Both are reported to core per window.
16
17// inhibitor is one inhibitor object on a surface.
18type inhibitor struct {
19 surf *surface
20 shortcut *keyboardshortcutsinhibit.ZwpKeyboardShortcutsInhibitorV1 // nil: idle
21 window ports.WindowID // reported to core (0: not yet)
22 active bool // shortcuts: last state sent
23}
24
25func registerInhibit(d *server.Display, s *Server) error {
26 if err := keyboardshortcutsinhibit.NewZwpKeyboardShortcutsInhibitManagerV1Global(d, 1, func(c server.Client, v, id uint32) {
27 _, _ = keyboardshortcutsinhibit.NewZwpKeyboardShortcutsInhibitManagerV1(c, int32(v), id, shortcutsManager{s})
28 }); err != nil {
29 return err
30 }
31 return idleinhibit.NewZwpIdleInhibitManagerV1Global(d, 1, func(c server.Client, v, id uint32) {
32 _, _ = idleinhibit.NewZwpIdleInhibitManagerV1(c, int32(v), id, idleManager{s})
33 })
34}
35
36type shortcutsManager struct{ server *Server }
37
38func (shortcutsManager) Destroy(*keyboardshortcutsinhibit.ZwpKeyboardShortcutsInhibitManagerV1) {}
39
40// InhibitShortcuts adds a surface's inhibitor; one per surface and seat
41// (there is one seat).
42func (m shortcutsManager) InhibitShortcuts(r *keyboardshortcutsinhibit.ZwpKeyboardShortcutsInhibitManagerV1, id uint32, surf *wayland.Surface, _ *wayland.Seat) {
43 state := m.server.surfaceOf(surf)
44 if state == nil {
45 return
46 }
47 for _, in := range m.server.inhibitors {
48 if in.surf == state && in.shortcut != nil {
49 r.PostError(uint32(keyboardshortcutsinhibit.ZwpKeyboardShortcutsInhibitManagerV1ErrorAlreadyInhibited), "surface already inhibits shortcuts")
50 return
51 }
52 }
53 res, err := keyboardshortcutsinhibit.NewZwpKeyboardShortcutsInhibitorV1(r.Client(), r.Version(), id, shortcutsInhibitor{})
54 if err != nil {
55 return
56 }
57 in := &inhibitor{surf: state, shortcut: res}
58 m.server.addInhibitor(in)
59 res.OnDestroy = func() { m.server.removeInhibitor(in) }
60}
61
62type shortcutsInhibitor struct{}
63
64func (shortcutsInhibitor) Destroy(*keyboardshortcutsinhibit.ZwpKeyboardShortcutsInhibitorV1) {}
65
66type idleManager struct{ server *Server }
67
68func (idleManager) Destroy(*idleinhibit.ZwpIdleInhibitManagerV1) {}
69
70// CreateInhibitor adds an idle inhibitor; a surface may have several.
71func (m idleManager) CreateInhibitor(r *idleinhibit.ZwpIdleInhibitManagerV1, id uint32, surf *wayland.Surface) {
72 state := m.server.surfaceOf(surf)
73 if state == nil {
74 return
75 }
76 res, err := idleinhibit.NewZwpIdleInhibitorV1(r.Client(), r.Version(), id, idleInhibitor{})
77 if err != nil {
78 return
79 }
80 in := &inhibitor{surf: state}
81 m.server.addInhibitor(in)
82 res.OnDestroy = func() { m.server.removeInhibitor(in) }
83}
84
85type idleInhibitor struct{}
86
87func (idleInhibitor) Destroy(*idleinhibit.ZwpIdleInhibitorV1) {}
88
89func (s *Server) addInhibitor(in *inhibitor) {
90 s.inhibitors = append(s.inhibitors, in)
91 s.syncInhibitors()
92}
93
94func (s *Server) removeInhibitor(in *inhibitor) {
95 for i, x := range s.inhibitors {
96 if x == in {
97 s.inhibitors = append(s.inhibitors[:i], s.inhibitors[i+1:]...)
98 break
99 }
100 }
101 in.surf = nil
102 s.syncInhibitors()
103}
104
105// syncInhibitors tells core, per window, whether it has inhibitors of
106// each kind, after any change: an inhibitor added or removed, a window
107// mapped or unmapped.
108func (s *Server) syncInhibitors() {
109 shortcuts, idle := map[ports.WindowID]bool{}, map[ports.WindowID]bool{}
110 for _, in := range s.inhibitors {
111 in.window = 0
112 if in.surf != nil && !in.surf.destroyed {
113 in.window = in.surf.root().windowID()
114 }
115 if in.window == 0 {
116 continue
117 }
118 if in.shortcut != nil {
119 shortcuts[in.window] = true
120 } else {
121 idle[in.window] = true
122 }
123 }
124 for id, on := range diffSets(s.shortcutWindows, shortcuts) {
125 s.emit(ports.ShortcutsInhibit{Window: id, Active: on})
126 }
127 for id, on := range diffSets(s.idleWindows, idle) {
128 s.emit(ports.IdleInhibit{Window: id, Active: on})
129 }
130 wasHeld := s.inhibited()
131 s.shortcutWindows, s.idleWindows = shortcuts, idle
132 s.syncIdle(wasHeld)
133}
134
135// diffSets is the membership changes from old to cur.
136func diffSets(old, cur map[ports.WindowID]bool) map[ports.WindowID]bool {
137 out := map[ports.WindowID]bool{}
138 for id := range cur {
139 if !old[id] {
140 out[id] = true
141 }
142 }
143 for id := range old {
144 if !cur[id] {
145 out[id] = false
146 }
147 }
148 return out
149}
150
151// setShortcutsInhibit applies core's decision to a window's shortcuts
152// inhibitor.
153func (s *Server) setShortcutsInhibit(c ports.ShortcutsInhibitState) {
154 for _, in := range s.inhibitors {
155 if in.shortcut == nil || in.window != c.Window || in.active == c.Active || !in.shortcut.Resource.Alive() {
156 continue
157 }
158 in.active = c.Active
159 if c.Active {
160 in.shortcut.SendActive()
161 } else {
162 in.shortcut.SendInactive()
163 }
164 }
165}