KWayland

pointerconstraints_interface_v1.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 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 "display.h"
7 #include "pointer_interface.h"
8 #include "pointerconstraints_interface_p.h"
9 #include "region_interface.h"
10 #include "surface_interface_p.h"
11 
12 #include <wayland-pointer-constraints-unstable-v1-server-protocol.h>
13 
14 namespace KWayland
15 {
16 namespace Server
17 {
18 class PointerConstraintsUnstableV1Interface::Private : public PointerConstraintsInterface::Private
19 {
20 public:
21  Private(PointerConstraintsUnstableV1Interface *q, Display *d);
22 
23 private:
24  void bind(wl_client *client, uint32_t version, uint32_t id) override;
25 
26  template<class T>
27  void
28  createConstraint(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface, wl_resource *pointer, wl_resource *region, uint32_t lifetime);
29 
30  static void unbind(wl_resource *resource);
31  static Private *cast(wl_resource *r)
32  {
33  return reinterpret_cast<Private *>(wl_resource_get_user_data(r));
34  }
35 
36  static void destroyCallback(wl_client *client, wl_resource *resource);
37  static void lockPointerCallback(wl_client *client,
38  wl_resource *resource,
39  uint32_t id,
40  wl_resource *surface,
41  wl_resource *pointer,
42  wl_resource *region,
43  uint32_t lifetime);
44  static void confinePointerCallback(wl_client *client,
45  wl_resource *resource,
46  uint32_t id,
47  wl_resource *surface,
48  wl_resource *pointer,
49  wl_resource *region,
50  uint32_t lifetime);
51 
52  PointerConstraintsUnstableV1Interface *q;
53  static const struct zwp_pointer_constraints_v1_interface s_interface;
54  static const quint32 s_version;
55 };
56 
57 class LockedPointerUnstableV1Interface::Private : public LockedPointerInterface::Private
58 {
59 public:
60  Private(LockedPointerUnstableV1Interface *q, PointerConstraintsUnstableV1Interface *c, wl_resource *parentResource);
61  ~Private() override;
62 
63  void updateLocked() override;
64 
65 private:
66  static void setCursorPositionHintCallback(wl_client *client, wl_resource *resource, wl_fixed_t surface_x, wl_fixed_t surface_y);
67  static void setRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region);
68 
69  LockedPointerUnstableV1Interface *q_func()
70  {
71  return reinterpret_cast<LockedPointerUnstableV1Interface *>(q);
72  }
73 
74  static const struct zwp_locked_pointer_v1_interface s_interface;
75 };
76 
77 const quint32 PointerConstraintsUnstableV1Interface::Private::s_version = 1;
78 
79 #ifndef K_DOXYGEN
80 const struct zwp_pointer_constraints_v1_interface PointerConstraintsUnstableV1Interface::Private::s_interface = {destroyCallback,
81  lockPointerCallback,
82  confinePointerCallback};
83 #endif
84 
85 void PointerConstraintsUnstableV1Interface::Private::destroyCallback(wl_client *client, wl_resource *resource)
86 {
87  Q_UNUSED(client)
88  wl_resource_destroy(resource);
89 }
90 
91 template<class T>
92 void PointerConstraintsUnstableV1Interface::Private::createConstraint(wl_client *client,
93  wl_resource *resource,
94  uint32_t id,
95  wl_resource *surface,
96  wl_resource *pointer,
97  wl_resource *region,
98  uint32_t lifetime)
99 {
100  auto s = SurfaceInterface::get(surface);
101  auto p = PointerInterface::get(pointer);
102  if (!s || !p) {
103  // send error?
104  return;
105  }
106  if (!s->lockedPointer().isNull() || !s->confinedPointer().isNull()) {
107  wl_resource_post_error(s->resource(), ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED, "Surface already constrained");
108  return;
109  }
110  auto constraint = new T(q, resource);
111  switch (lifetime) {
112  case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT:
113  constraint->d_func()->lifeTime = T::LifeTime::Persistent;
114  break;
115  case ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT: // fall through
116  default:
117  constraint->d_func()->lifeTime = T::LifeTime::OneShot;
118  break;
119  }
120  auto r = RegionInterface::get(region);
121  constraint->d_func()->region = r ? r->region() : QRegion();
122  constraint->d_func()->create(display->getConnection(client), version, id);
123  s->d_func()->installPointerConstraint(constraint);
124 }
125 
126 void PointerConstraintsUnstableV1Interface::Private::lockPointerCallback(wl_client *client,
127  wl_resource *resource,
128  uint32_t id,
129  wl_resource *surface,
130  wl_resource *pointer,
131  wl_resource *region,
132  uint32_t lifetime)
133 {
134  cast(resource)->createConstraint<LockedPointerUnstableV1Interface>(client, resource, id, surface, pointer, region, lifetime);
135 }
136 
137 void PointerConstraintsUnstableV1Interface::Private::confinePointerCallback(wl_client *client,
138  wl_resource *resource,
139  uint32_t id,
140  wl_resource *surface,
141  wl_resource *pointer,
142  wl_resource *region,
143  uint32_t lifetime)
144 {
145  cast(resource)->createConstraint<ConfinedPointerUnstableV1Interface>(client, resource, id, surface, pointer, region, lifetime);
146 }
147 
148 PointerConstraintsUnstableV1Interface::Private::Private(PointerConstraintsUnstableV1Interface *q, Display *d)
149  : PointerConstraintsInterface::Private(PointerConstraintsInterfaceVersion::UnstableV1, q, d, &zwp_pointer_constraints_v1_interface, s_version)
150  , q(q)
151 {
152 }
153 
154 void PointerConstraintsUnstableV1Interface::Private::bind(wl_client *client, uint32_t version, uint32_t id)
155 {
156  auto c = display->getConnection(client);
157  wl_resource *resource = c->createResource(&zwp_pointer_constraints_v1_interface, qMin(version, s_version), id);
158  if (!resource) {
159  wl_client_post_no_memory(client);
160  return;
161  }
162  wl_resource_set_implementation(resource, &s_interface, this, unbind);
163  // TODO: should we track?
164 }
165 
166 void PointerConstraintsUnstableV1Interface::Private::unbind(wl_resource *resource)
167 {
168  Q_UNUSED(resource)
169  // TODO: implement?
170 }
171 
172 PointerConstraintsUnstableV1Interface::PointerConstraintsUnstableV1Interface(Display *display, QObject *parent)
173  : PointerConstraintsInterface(new Private(this, display), parent)
174 {
175 }
176 
177 PointerConstraintsUnstableV1Interface::~PointerConstraintsUnstableV1Interface() = default;
178 
179 #ifndef K_DOXYGEN
180 const struct zwp_locked_pointer_v1_interface LockedPointerUnstableV1Interface::Private::s_interface = {resourceDestroyedCallback,
181  setCursorPositionHintCallback,
182  setRegionCallback};
183 #endif
184 
185 void LockedPointerUnstableV1Interface::Private::setCursorPositionHintCallback(wl_client *client,
186  wl_resource *resource,
187  wl_fixed_t surface_x,
188  wl_fixed_t surface_y)
189 {
190  Q_UNUSED(client)
191  auto p = cast<Private>(resource);
192  p->pendingHint = QPointF(wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y));
193  p->hintIsSet = true;
194 }
195 
196 void LockedPointerUnstableV1Interface::Private::setRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region)
197 {
198  Q_UNUSED(client)
199  auto p = cast<Private>(resource);
200  auto r = RegionInterface::get(region);
201  p->pendingRegion = r ? r->region() : QRegion();
202  p->regionIsSet = true;
203 }
204 
205 void LockedPointerUnstableV1Interface::Private::updateLocked()
206 {
207  if (!resource) {
208  return;
209  }
210  if (locked) {
211  zwp_locked_pointer_v1_send_locked(resource);
212  } else {
213  zwp_locked_pointer_v1_send_unlocked(resource);
214  }
215 }
216 
217 LockedPointerUnstableV1Interface::Private::Private(LockedPointerUnstableV1Interface *q, PointerConstraintsUnstableV1Interface *c, wl_resource *parentResource)
218  : LockedPointerInterface::Private(PointerConstraintsInterfaceVersion::UnstableV1, q, c, parentResource, &zwp_locked_pointer_v1_interface, &s_interface)
219 {
220 }
221 
222 LockedPointerUnstableV1Interface::LockedPointerUnstableV1Interface(PointerConstraintsUnstableV1Interface *parent, wl_resource *parentResource)
223  : LockedPointerInterface(new Private(this, parent, parentResource))
224 {
225 }
226 
227 LockedPointerUnstableV1Interface::Private::~Private() = default;
228 
229 LockedPointerUnstableV1Interface::~LockedPointerUnstableV1Interface() = default;
230 
231 LockedPointerUnstableV1Interface::Private *LockedPointerUnstableV1Interface::d_func() const
232 {
233  return reinterpret_cast<Private *>(d.data());
234 }
235 
236 class ConfinedPointerUnstableV1Interface::Private : public ConfinedPointerInterface::Private
237 {
238 public:
239  Private(ConfinedPointerUnstableV1Interface *q, PointerConstraintsUnstableV1Interface *c, wl_resource *parentResource);
240  ~Private() override;
241 
242  void updateConfined() override;
243 
244 private:
245  static void setRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region);
246 
247  ConfinedPointerUnstableV1Interface *q_func()
248  {
249  return reinterpret_cast<ConfinedPointerUnstableV1Interface *>(q);
250  }
251 
252  static const struct zwp_confined_pointer_v1_interface s_interface;
253 };
254 
255 #ifndef K_DOXYGEN
256 const struct zwp_confined_pointer_v1_interface ConfinedPointerUnstableV1Interface::Private::s_interface = {resourceDestroyedCallback, setRegionCallback};
257 #endif
258 
259 void ConfinedPointerUnstableV1Interface::Private::setRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region)
260 {
261  Q_UNUSED(client)
262  auto p = cast<Private>(resource);
263  auto r = RegionInterface::get(region);
264  p->pendingRegion = r ? r->region() : QRegion();
265  p->regionIsSet = true;
266 }
267 
268 ConfinedPointerUnstableV1Interface::Private::Private(ConfinedPointerUnstableV1Interface *q,
269  PointerConstraintsUnstableV1Interface *c,
270  wl_resource *parentResource)
271  : ConfinedPointerInterface::Private(PointerConstraintsInterfaceVersion::UnstableV1, q, c, parentResource, &zwp_confined_pointer_v1_interface, &s_interface)
272 {
273 }
274 
275 ConfinedPointerUnstableV1Interface::ConfinedPointerUnstableV1Interface(PointerConstraintsUnstableV1Interface *parent, wl_resource *parentResource)
276  : ConfinedPointerInterface(new Private(this, parent, parentResource))
277 {
278 }
279 
280 ConfinedPointerUnstableV1Interface::Private::~Private() = default;
281 
282 ConfinedPointerUnstableV1Interface::~ConfinedPointerUnstableV1Interface() = default;
283 
284 void ConfinedPointerUnstableV1Interface::Private::updateConfined()
285 {
286  if (!resource) {
287  return;
288  }
289  if (confined) {
290  zwp_confined_pointer_v1_send_confined(resource);
291  } else {
292  zwp_confined_pointer_v1_send_unconfined(resource);
293  }
294 }
295 
296 ConfinedPointerUnstableV1Interface::Private *ConfinedPointerUnstableV1Interface::d_func() const
297 {
298  return reinterpret_cast<Private *>(d.data());
299 }
300 
301 }
302 }
static PointerInterface * get(wl_resource *native)
static SurfaceInterface * get(wl_resource *native)
static RegionInterface * get(wl_resource *native)
PointerConstraintsInterfaceVersion
Enum describing the interface versions the PointerConstraintsInterface can support.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Feb 8 2023 03:59:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.