Debian Codegraph @

xbmc/windowing/wayland/Seat.cpp

raw
1 /*
2 * Copyright (C) 2017-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "Seat.h"
10
11 #include "utils/log.h"
12
13 #include "platform/posix/utils/FileHandle.h"
14 #include "platform/posix/utils/Mmap.h"
15
16 #include <cassert>
17 #include <utility>
18
19 #include <unistd.h>
20
21 using namespace KODI::WINDOWING::WAYLAND;
22 using namespace std::placeholders;
23
24 namespace
25 {
26
27 /**
28 * Handle change of availability of a wl_seat input capability
29 *
30 * This checks whether the capability is currently available with the wl_seat
31 * and whether it was bound to a protocol object. If there is a mismatch between
32 * these two, the protocol proxy is released if a capability was removed or bound
33 * if a capability was added.
34 *
35 * \param caps new capabilities
36 * \param cap capability to check for
37 * \param seatName human-readable name of the seat for log messages
38 * \param capName human-readable name of the capability for log messages
39 * \param proxy proxy object that should be filled with a new instance or reset
40 * \param instanceProvider function that functions as factory for the Wayland
41 * protocol instance if the capability has been added
42 */
43 template<typename T, typename InstanceProviderT>
44 bool HandleCapabilityChange(const wayland::seat_capability& caps,
45 const wayland::seat_capability& cap,
46 std::string const& seatName,
47 std::string const& capName,
48 T& proxy,
49 InstanceProviderT instanceProvider)
50 {
51 bool hasCapability = caps & cap;
52
53 if ((!!proxy) != hasCapability)
54 {
55 // Capability changed
56
57 if (hasCapability)
58 {
59 // The capability was added
60 CLog::Log(LOGDEBUG, "Wayland seat {} gained capability {}", seatName, capName);
61 proxy = instanceProvider();
62 return true;
63 }
64 else
65 {
66 // The capability was removed
67 CLog::Log(LOGDEBUG, "Wayland seat {} lost capability {}", seatName, capName);
68 proxy.proxy_release();
69 }
70 }
71
72 return false;
73 }
74
75 }
76
77 CSeat::CSeat(std::uint32_t globalName, wayland::seat_t const& seat, CConnection& connection)
78 : m_globalName{globalName}, m_seat{seat}, m_selection{connection, seat}
79 {
80 m_seat.on_name() = [this](std::string name) { m_name = std::move(name); };
81 m_seat.on_capabilities() = std::bind(&CSeat::HandleOnCapabilities, this, std::placeholders::_1);
82 }
83
84 CSeat::~CSeat() noexcept = default;
85
86 void CSeat::AddRawInputHandlerKeyboard(KODI::WINDOWING::WAYLAND::IRawInputHandlerKeyboard *rawKeyboardHandler)
87 {
88 assert(rawKeyboardHandler);
89 m_rawKeyboardHandlers.emplace(rawKeyboardHandler);
90 }
91
92 void CSeat::RemoveRawInputHandlerKeyboard(KODI::WINDOWING::WAYLAND::IRawInputHandlerKeyboard *rawKeyboardHandler)
93 {
94 m_rawKeyboardHandlers.erase(rawKeyboardHandler);
95 }
96
97 void CSeat::AddRawInputHandlerPointer(IRawInputHandlerPointer* rawPointerHandler)
98 {
99 assert(rawPointerHandler);
100 m_rawPointerHandlers.emplace(rawPointerHandler);
101 }
102
103 void CSeat::RemoveRawInputHandlerPointer(KODI::WINDOWING::WAYLAND::IRawInputHandlerPointer *rawPointerHandler)
104 {
105 m_rawPointerHandlers.erase(rawPointerHandler);
106 }
107
108 void CSeat::AddRawInputHandlerTouch(IRawInputHandlerTouch* rawTouchHandler)
109 {
110 assert(rawTouchHandler);
111 m_rawTouchHandlers.emplace(rawTouchHandler);
112 }
113
114 void CSeat::RemoveRawInputHandlerTouch(KODI::WINDOWING::WAYLAND::IRawInputHandlerTouch *rawTouchHandler)
115 {
116 m_rawTouchHandlers.erase(rawTouchHandler);
117 }
118
119 void CSeat::HandleOnCapabilities(const wayland::seat_capability& caps)
120 {
121 if (HandleCapabilityChange(caps, wayland::seat_capability::keyboard, GetName(), "keyboard", m_keyboard, std::bind(&wayland::seat_t::get_keyboard, m_seat)))
122 {
123 HandleKeyboardCapability();
124 }
125 if (HandleCapabilityChange(caps, wayland::seat_capability::pointer, GetName(), "pointer", m_pointer, std::bind(&wayland::seat_t::get_pointer, m_seat)))
126 {
127 HandlePointerCapability();
128 }
129 if (HandleCapabilityChange(caps, wayland::seat_capability::touch, GetName(), "touch", m_touch, std::bind(&wayland::seat_t::get_touch, m_seat)))
130 {
131 HandleTouchCapability();
132 }
133 }
134
135 void CSeat::SetCursor(std::uint32_t serial, wayland::surface_t const &surface, std::int32_t hotspotX, std::int32_t hotspotY)
136 {
137 if (m_pointer)
138 {
139 // set_cursor on webOS completely breaks pointer input
140 #ifndef TARGET_WEBOS
141 m_pointer.set_cursor(serial, surface, hotspotX, hotspotY);
142 #endif
143 }
144 }
145
146 void CSeat::HandleKeyboardCapability()
147 {
148 m_keyboard.on_keymap() = [this](wayland::keyboard_keymap_format format, int fd, std::uint32_t size)
149 {
150 KODI::UTILS::POSIX::CFileHandle fdGuard{fd};
151 KODI::UTILS::POSIX::CMmap mmap{nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0};
152 std::string keymap{static_cast<const char*> (mmap.Data()), size};
153 for (auto handler : m_rawKeyboardHandlers)
154 {
155 handler->OnKeyboardKeymap(this, format, keymap);
156 }
157 };
158 m_keyboard.on_enter() = [this](std::uint32_t serial, const wayland::surface_t& surface,
159 const wayland::array_t& keys) {
160 for (auto handler : m_rawKeyboardHandlers)
161 {
162 handler->OnKeyboardEnter(this, serial, surface, keys);
163 }
164 };
165 m_keyboard.on_leave() = [this](std::uint32_t serial, const wayland::surface_t& surface) {
166 for (auto handler : m_rawKeyboardHandlers)
167 {
168 handler->OnKeyboardLeave(this, serial, surface);
169 }
170 };
171 m_keyboard.on_key() = [this](std::uint32_t serial, std::uint32_t time, std::uint32_t key, wayland::keyboard_key_state state)
172 {
173 for (auto handler : m_rawKeyboardHandlers)
174 {
175 handler->OnKeyboardKey(this, serial, time, key, state);
176 }
177 };
178 m_keyboard.on_modifiers() = [this](std::uint32_t serial, std::uint32_t modsDepressed, std::uint32_t modsLatched, std::uint32_t modsLocked, std::uint32_t group)
179 {
180 for (auto handler : m_rawKeyboardHandlers)
181 {
182 handler->OnKeyboardModifiers(this, serial, modsDepressed, modsLatched, modsLocked, group);
183 }
184 };
185 m_keyboard.on_repeat_info() = [this](std::int32_t rate, std::int32_t delay)
186 {
187 for (auto handler : m_rawKeyboardHandlers)
188 {
189 handler->OnKeyboardRepeatInfo(this, rate, delay);
190 }
191 };
192 }
193
194
195 void CSeat::HandlePointerCapability()
196 {
197 m_pointer.on_enter() = [this](std::uint32_t serial, const wayland::surface_t& surface,
198 double surfaceX, double surfaceY) {
199 for (auto handler : m_rawPointerHandlers)
200 {
201 handler->OnPointerEnter(this, serial, surface, surfaceX, surfaceY);
202 }
203 };
204 m_pointer.on_leave() = [this](std::uint32_t serial, const wayland::surface_t& surface) {
205 for (auto handler : m_rawPointerHandlers)
206 {
207 handler->OnPointerLeave(this, serial, surface);
208 }
209 };
210 m_pointer.on_motion() = [this](std::uint32_t time, double surfaceX, double surfaceY)
211 {
212 for (auto handler : m_rawPointerHandlers)
213 {
214 handler->OnPointerMotion(this, time, surfaceX, surfaceY);
215 }
216 };
217 m_pointer.on_button() = [this](std::uint32_t serial, std::uint32_t time, std::uint32_t button, wayland::pointer_button_state state)
218 {
219 for (auto handler : m_rawPointerHandlers)
220 {
221 handler->OnPointerButton(this, serial, time, button, state);
222 }
223 };
224 m_pointer.on_axis() = [this](std::uint32_t time, wayland::pointer_axis axis, double value)
225 {
226 for (auto handler : m_rawPointerHandlers)
227 {
228 handler->OnPointerAxis(this, time, axis, value);
229 }
230 };
231 // Wayland groups pointer events, but right now there is no benefit in
232 // treating them in groups. The main use case for doing so seems to be
233 // multi-axis (i.e. diagonal) scrolling, but we do not support this anyway.
234 /*m_pointer.on_frame() = [this]()
235 {
236
237 };*/
238 }
239
240 void CSeat::HandleTouchCapability()
241 {
242 m_touch.on_down() = [this](std::uint32_t serial, std::uint32_t time,
243 const wayland::surface_t& surface, std::int32_t id, double x,
244 double y) {
245 for (auto handler : m_rawTouchHandlers)
246 {
247 handler->OnTouchDown(this, serial, time, surface, id, x, y);
248 }
249 };
250 m_touch.on_up() = [this](std::uint32_t serial, std::uint32_t time, std::int32_t id)
251 {
252 for (auto handler : m_rawTouchHandlers)
253 {
254 handler->OnTouchUp(this, serial, time, id);
255 }
256 };
257 m_touch.on_motion() = [this](std::uint32_t time, std::int32_t id, double x, double y)
258 {
259 for (auto handler : m_rawTouchHandlers)
260 {
261 handler->OnTouchMotion(this, time, id, x, y);
262 }
263 };
264 m_touch.on_cancel() = [this]()
265 {
266 for (auto handler : m_rawTouchHandlers)
267 {
268 handler->OnTouchCancel(this);
269 }
270 };
271 m_touch.on_shape() = [this](std::int32_t id, double major, double minor)
272 {
273 for (auto handler : m_rawTouchHandlers)
274 {
275 handler->OnTouchShape(this, id, major, minor);
276 }
277 };
278 }