summaryrefslogtreecommitdiffstats
path: root/xbmc/windowing/wayland/Seat.cpp
blob: 56709b322711023b75fcafc6b873ddf9b525ecf1 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 *  Copyright (C) 2017-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "Seat.h"

#include "utils/log.h"

#include "platform/posix/utils/FileHandle.h"
#include "platform/posix/utils/Mmap.h"

#include <cassert>
#include <utility>

#include <unistd.h>

using namespace KODI::WINDOWING::WAYLAND;
using namespace std::placeholders;

namespace
{

/**
 * Handle change of availability of a wl_seat input capability
 *
 * This checks whether the capability is currently available with the wl_seat
 * and whether it was bound to a protocol object. If there is a mismatch between
 * these two, the protocol proxy is released if a capability was removed or bound
 * if a capability was added.
 *
 * \param caps new capabilities
 * \param cap capability to check for
 * \param seatName human-readable name of the seat for log messages
 * \param capName human-readable name of the capability for log messages
 * \param proxy proxy object that should be filled with a new instance or reset
 * \param instanceProvider function that functions as factory for the Wayland
 *                         protocol instance if the capability has been added
 */
template<typename T, typename InstanceProviderT>
bool HandleCapabilityChange(const wayland::seat_capability& caps,
                            const wayland::seat_capability& cap,
                            std::string const& seatName,
                            std::string const& capName,
                            T& proxy,
                            InstanceProviderT instanceProvider)
{
  bool hasCapability = caps & cap;

  if ((!!proxy) != hasCapability)
  {
    // Capability changed

    if (hasCapability)
    {
      // The capability was added
      CLog::Log(LOGDEBUG, "Wayland seat {} gained capability {}", seatName, capName);
      proxy = instanceProvider();
      return true;
    }
    else
    {
      // The capability was removed
      CLog::Log(LOGDEBUG, "Wayland seat {} lost capability {}", seatName, capName);
      proxy.proxy_release();
    }
  }

  return false;
}

}

CSeat::CSeat(std::uint32_t globalName, wayland::seat_t const& seat, CConnection& connection)
: m_globalName{globalName}, m_seat{seat}, m_selection{connection, seat}
{
  m_seat.on_name() = [this](std::string name) { m_name = std::move(name); };
  m_seat.on_capabilities() = std::bind(&CSeat::HandleOnCapabilities, this, std::placeholders::_1);
}

CSeat::~CSeat() noexcept = default;

void CSeat::AddRawInputHandlerKeyboard(KODI::WINDOWING::WAYLAND::IRawInputHandlerKeyboard *rawKeyboardHandler)
{
  assert(rawKeyboardHandler);
  m_rawKeyboardHandlers.emplace(rawKeyboardHandler);
}

void CSeat::RemoveRawInputHandlerKeyboard(KODI::WINDOWING::WAYLAND::IRawInputHandlerKeyboard *rawKeyboardHandler)
{
  m_rawKeyboardHandlers.erase(rawKeyboardHandler);
}

void CSeat::AddRawInputHandlerPointer(IRawInputHandlerPointer* rawPointerHandler)
{
  assert(rawPointerHandler);
  m_rawPointerHandlers.emplace(rawPointerHandler);
}

void CSeat::RemoveRawInputHandlerPointer(KODI::WINDOWING::WAYLAND::IRawInputHandlerPointer *rawPointerHandler)
{
  m_rawPointerHandlers.erase(rawPointerHandler);
}

void CSeat::AddRawInputHandlerTouch(IRawInputHandlerTouch* rawTouchHandler)
{
  assert(rawTouchHandler);
  m_rawTouchHandlers.emplace(rawTouchHandler);
}

void CSeat::RemoveRawInputHandlerTouch(KODI::WINDOWING::WAYLAND::IRawInputHandlerTouch *rawTouchHandler)
{
  m_rawTouchHandlers.erase(rawTouchHandler);
}

void CSeat::HandleOnCapabilities(const wayland::seat_capability& caps)
{
  if (HandleCapabilityChange(caps, wayland::seat_capability::keyboard, GetName(), "keyboard", m_keyboard, std::bind(&wayland::seat_t::get_keyboard, m_seat)))
  {
    HandleKeyboardCapability();
  }
  if (HandleCapabilityChange(caps, wayland::seat_capability::pointer, GetName(), "pointer", m_pointer, std::bind(&wayland::seat_t::get_pointer, m_seat)))
  {
    HandlePointerCapability();
  }
  if (HandleCapabilityChange(caps, wayland::seat_capability::touch, GetName(), "touch", m_touch, std::bind(&wayland::seat_t::get_touch, m_seat)))
  {
    HandleTouchCapability();
  }
}

void CSeat::SetCursor(std::uint32_t serial, wayland::surface_t const &surface, std::int32_t hotspotX, std::int32_t hotspotY)
{
  if (m_pointer)
  {
    m_pointer.set_cursor(serial, surface, hotspotX, hotspotY);
  }
}

void CSeat::HandleKeyboardCapability()
{
  m_keyboard.on_keymap() = [this](wayland::keyboard_keymap_format format, int fd, std::uint32_t size)
  {
    KODI::UTILS::POSIX::CFileHandle fdGuard{fd};
    KODI::UTILS::POSIX::CMmap mmap{nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0};
    std::string keymap{static_cast<const char*> (mmap.Data()), size};
    for (auto handler : m_rawKeyboardHandlers)
    {
      handler->OnKeyboardKeymap(this, format, keymap);
    }
  };
  m_keyboard.on_enter() = [this](std::uint32_t serial, const wayland::surface_t& surface,
                                 const wayland::array_t& keys) {
    for (auto handler : m_rawKeyboardHandlers)
    {
      handler->OnKeyboardEnter(this, serial, surface, keys);
    }
  };
  m_keyboard.on_leave() = [this](std::uint32_t serial, const wayland::surface_t& surface) {
    for (auto handler : m_rawKeyboardHandlers)
    {
      handler->OnKeyboardLeave(this, serial, surface);
    }
  };
  m_keyboard.on_key() = [this](std::uint32_t serial, std::uint32_t time, std::uint32_t key, wayland::keyboard_key_state state)
  {
    for (auto handler : m_rawKeyboardHandlers)
    {
      handler->OnKeyboardKey(this, serial, time, key, state);
    }
  };
  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)
  {
    for (auto handler : m_rawKeyboardHandlers)
    {
      handler->OnKeyboardModifiers(this, serial, modsDepressed, modsLatched, modsLocked, group);
    }
  };
  m_keyboard.on_repeat_info() = [this](std::int32_t rate, std::int32_t delay)
  {
    for (auto handler : m_rawKeyboardHandlers)
    {
      handler->OnKeyboardRepeatInfo(this, rate, delay);
    }
  };
}


void CSeat::HandlePointerCapability()
{
  m_pointer.on_enter() = [this](std::uint32_t serial, const wayland::surface_t& surface,
                                double surfaceX, double surfaceY) {
    for (auto handler : m_rawPointerHandlers)
    {
      handler->OnPointerEnter(this, serial, surface, surfaceX, surfaceY);
    }
  };
  m_pointer.on_leave() = [this](std::uint32_t serial, const wayland::surface_t& surface) {
    for (auto handler : m_rawPointerHandlers)
    {
      handler->OnPointerLeave(this, serial, surface);
    }
  };
  m_pointer.on_motion() = [this](std::uint32_t time, double surfaceX, double surfaceY)
  {
    for (auto handler : m_rawPointerHandlers)
    {
      handler->OnPointerMotion(this, time, surfaceX, surfaceY);
    }
  };
  m_pointer.on_button() = [this](std::uint32_t serial, std::uint32_t time, std::uint32_t button, wayland::pointer_button_state state)
  {
    for (auto handler : m_rawPointerHandlers)
    {
      handler->OnPointerButton(this, serial, time, button, state);
    }
  };
  m_pointer.on_axis() = [this](std::uint32_t time, wayland::pointer_axis axis, double value)
  {
    for (auto handler : m_rawPointerHandlers)
    {
      handler->OnPointerAxis(this, time, axis, value);
    }
  };
  // Wayland groups pointer events, but right now there is no benefit in
  // treating them in groups. The main use case for doing so seems to be
  // multi-axis (i.e. diagonal) scrolling, but we do not support this anyway.
  /*m_pointer.on_frame() = [this]()
  {

  };*/
}

void CSeat::HandleTouchCapability()
{
  m_touch.on_down() = [this](std::uint32_t serial, std::uint32_t time,
                             const wayland::surface_t& surface, std::int32_t id, double x,
                             double y) {
    for (auto handler : m_rawTouchHandlers)
    {
      handler->OnTouchDown(this, serial, time, surface, id, x, y);
    }
  };
  m_touch.on_up() = [this](std::uint32_t serial, std::uint32_t time, std::int32_t id)
  {
    for (auto handler : m_rawTouchHandlers)
    {
      handler->OnTouchUp(this, serial, time, id);
    }
  };
  m_touch.on_motion() = [this](std::uint32_t time, std::int32_t id, double x, double y)
  {
    for (auto handler : m_rawTouchHandlers)
    {
      handler->OnTouchMotion(this, time, id, x, y);
    }
  };
  m_touch.on_cancel() = [this]()
  {
    for (auto handler : m_rawTouchHandlers)
    {
      handler->OnTouchCancel(this);
    }
  };
  m_touch.on_shape() = [this](std::int32_t id, double major, double minor)
  {
    for (auto handler : m_rawTouchHandlers)
    {
      handler->OnTouchShape(this, id, major, minor);
    }
  };
}