KWaylandServer

fakeinput_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Martin Gräßlin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "fakeinput_interface.h"
7 #include "display.h"
8 
9 #include <QPointF>
10 #include <QSizeF>
11 
12 #include <qwayland-server-fake-input.h>
13 #include <wayland-server.h>
14 
15 namespace KWaylandServer
16 {
17 static const quint32 s_version = 4;
18 
19 class FakeInputInterfacePrivate : public QtWaylandServer::org_kde_kwin_fake_input
20 {
21 public:
22  FakeInputInterfacePrivate(FakeInputInterface *_q, Display *display);
24 
25 private:
26  FakeInputDevice *device(wl_resource *r);
27 
28  FakeInputInterface *q;
29  static QList<quint32> touchIds;
30 
31 protected:
32  void org_kde_kwin_fake_input_bind_resource(Resource *resource) override;
33  void org_kde_kwin_fake_input_destroy_resource(Resource *resource) override;
34  void org_kde_kwin_fake_input_authenticate(Resource *resource, const QString &application, const QString &reason) override;
35  void org_kde_kwin_fake_input_pointer_motion(Resource *resource, wl_fixed_t delta_x, wl_fixed_t delta_y) override;
36  void org_kde_kwin_fake_input_button(Resource *resource, uint32_t button, uint32_t state) override;
37  void org_kde_kwin_fake_input_axis(Resource *resource, uint32_t axis, wl_fixed_t value) override;
38  void org_kde_kwin_fake_input_touch_down(Resource *resource, uint32_t id, wl_fixed_t x, wl_fixed_t y) override;
39  void org_kde_kwin_fake_input_touch_motion(Resource *resource, uint32_t id, wl_fixed_t x, wl_fixed_t y) override;
40  void org_kde_kwin_fake_input_touch_up(Resource *resource, uint32_t id) override;
41  void org_kde_kwin_fake_input_touch_cancel(Resource *resource) override;
42  void org_kde_kwin_fake_input_touch_frame(Resource *resource) override;
43  void org_kde_kwin_fake_input_pointer_motion_absolute(Resource *resource, wl_fixed_t x, wl_fixed_t y) override;
44  void org_kde_kwin_fake_input_keyboard_key(Resource *resource, uint32_t button, uint32_t state) override;
45 };
46 
47 QList<quint32> FakeInputInterfacePrivate::touchIds = QList<quint32>();
48 
49 FakeInputInterfacePrivate::FakeInputInterfacePrivate(FakeInputInterface *_q, Display *display)
50  : QtWaylandServer::org_kde_kwin_fake_input(*display, s_version)
51  , q(_q)
52 {
53 }
54 
55 FakeInputInterface::FakeInputInterface(Display *display, QObject *parent)
56  : QObject(parent)
57  , d(new FakeInputInterfacePrivate(this, display))
58 {
59 }
60 
61 FakeInputInterface::~FakeInputInterface() = default;
62 
63 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_bind_resource(Resource *resource)
64 {
65  FakeInputDevice *device = new FakeInputDevice(q, resource->handle);
66  devices << device;
67  QObject::connect(device, &FakeInputDevice::destroyed, q, [device, this] {
68  devices.removeAll(device);
69  });
70  Q_EMIT q->deviceCreated(device);
71 }
72 
73 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_destroy_resource(Resource *resource)
74 {
75  if (FakeInputDevice *d = device(resource->handle)) {
76  d->deleteLater();
77  }
78 }
79 
80 FakeInputDevice *FakeInputInterfacePrivate::device(wl_resource *r)
81 {
82  auto it = std::find_if(devices.constBegin(), devices.constEnd(), [r](FakeInputDevice *device) {
83  return device->resource() == r;
84  });
85  if (it != devices.constEnd()) {
86  return *it;
87  }
88  return nullptr;
89 }
90 
91 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_authenticate(Resource *resource, const QString &application, const QString &reason)
92 {
93  FakeInputDevice *d = device(resource->handle);
94  if (!d) {
95  return;
96  }
97  Q_EMIT d->authenticationRequested(application, reason);
98 }
99 
100 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_pointer_motion(Resource *resource, wl_fixed_t delta_x, wl_fixed_t delta_y)
101 {
102  FakeInputDevice *d = device(resource->handle);
103  if (!d || !d->isAuthenticated()) {
104  return;
105  }
106  Q_EMIT d->pointerMotionRequested(QSizeF(wl_fixed_to_double(delta_x), wl_fixed_to_double(delta_y)));
107 }
108 
109 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_button(Resource *resource, uint32_t button, uint32_t state)
110 {
111  FakeInputDevice *d = device(resource->handle);
112  if (!d || !d->isAuthenticated()) {
113  return;
114  }
115  switch (state) {
116  case WL_POINTER_BUTTON_STATE_PRESSED:
117  Q_EMIT d->pointerButtonPressRequested(button);
118  break;
119  case WL_POINTER_BUTTON_STATE_RELEASED:
120  Q_EMIT d->pointerButtonReleaseRequested(button);
121  break;
122  default:
123  // nothing
124  break;
125  }
126 }
127 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_axis(Resource *resource, uint32_t axis, wl_fixed_t value)
128 {
129  FakeInputDevice *d = device(resource->handle);
130  if (!d || !d->isAuthenticated()) {
131  return;
132  }
133  Qt::Orientation orientation;
134  switch (axis) {
135  case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
136  orientation = Qt::Horizontal;
137  break;
138  case WL_POINTER_AXIS_VERTICAL_SCROLL:
139  orientation = Qt::Vertical;
140  break;
141  default:
142  // invalid
143  return;
144  }
145  Q_EMIT d->pointerAxisRequested(orientation, wl_fixed_to_double(value));
146 }
147 
148 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_touch_down(Resource *resource, uint32_t id, wl_fixed_t x, wl_fixed_t y)
149 {
150  FakeInputDevice *d = device(resource->handle);
151  if (!d || !d->isAuthenticated()) {
152  return;
153  }
154  if (touchIds.contains(id)) {
155  return;
156  }
157  touchIds << id;
158  Q_EMIT d->touchDownRequested(id, QPointF(wl_fixed_to_double(x), wl_fixed_to_double(y)));
159 }
160 
161 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_touch_motion(Resource *resource, uint32_t id, wl_fixed_t x, wl_fixed_t y)
162 {
163  FakeInputDevice *d = device(resource->handle);
164  if (!d || !d->isAuthenticated()) {
165  return;
166  }
167  if (!touchIds.contains(id)) {
168  return;
169  }
170  Q_EMIT d->touchMotionRequested(id, QPointF(wl_fixed_to_double(x), wl_fixed_to_double(y)));
171 }
172 
173 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_touch_up(Resource *resource, uint32_t id)
174 {
175  FakeInputDevice *d = device(resource->handle);
176  if (!d || !d->isAuthenticated()) {
177  return;
178  }
179  if (!touchIds.contains(id)) {
180  return;
181  }
182  touchIds.removeOne(id);
183  Q_EMIT d->touchUpRequested(id);
184 }
185 
186 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_touch_cancel(Resource *resource)
187 {
188  FakeInputDevice *d = device(resource->handle);
189  if (!d || !d->isAuthenticated()) {
190  return;
191  }
192  touchIds.clear();
193  Q_EMIT d->touchCancelRequested();
194 }
195 
196 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_touch_frame(Resource *resource)
197 {
198  FakeInputDevice *d = device(resource->handle);
199  if (!d || !d->isAuthenticated()) {
200  return;
201  }
202  Q_EMIT d->touchFrameRequested();
203 }
204 
205 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_pointer_motion_absolute(Resource *resource, wl_fixed_t x, wl_fixed_t y)
206 {
207  FakeInputDevice *d = device(resource->handle);
208  if (!d || !d->isAuthenticated()) {
209  return;
210  }
211  Q_EMIT d->pointerMotionAbsoluteRequested(QPointF(wl_fixed_to_double(x), wl_fixed_to_double(y)));
212 }
213 
214 void FakeInputInterfacePrivate::org_kde_kwin_fake_input_keyboard_key(Resource *resource, uint32_t button, uint32_t state)
215 {
216  FakeInputDevice *d = device(resource->handle);
217  if (!d || !d->isAuthenticated()) {
218  return;
219  }
220  switch (state) {
221  case WL_KEYBOARD_KEY_STATE_PRESSED:
222  Q_EMIT d->keyboardKeyPressRequested(button);
223  break;
224  case WL_KEYBOARD_KEY_STATE_RELEASED:
225  Q_EMIT d->keyboardKeyReleaseRequested(button);
226  break;
227  default:
228  // nothing
229  break;
230  }
231 }
232 
233 class FakeInputDevicePrivate
234 {
235 public:
236  FakeInputDevicePrivate(FakeInputInterface *interface, wl_resource *resource);
237  wl_resource *resource;
238  FakeInputInterface *interface;
239  bool authenticated = false;
240 };
241 
242 FakeInputDevicePrivate::FakeInputDevicePrivate(FakeInputInterface *interface, wl_resource *resource)
243  : resource(resource)
244  , interface(interface)
245 {
246 }
247 
248 FakeInputDevice::FakeInputDevice(FakeInputInterface *parent, wl_resource *resource)
249  : QObject(parent)
250  , d(new FakeInputDevicePrivate(parent, resource))
251 {
252 }
253 
254 FakeInputDevice::~FakeInputDevice() = default;
255 
256 void FakeInputDevice::setAuthentication(bool authenticated)
257 {
258  d->authenticated = authenticated;
259 }
260 
262 {
263  return d->resource;
264 }
265 
267 {
268  return d->authenticated;
269 }
270 
271 }
void setAuthentication(bool authenticated)
Authenticate this device to send events.
Orientation
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void destroyed(QObject *obj)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:08:27 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.