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

KDE's Doxygen guidelines are available online.