KWayland

seat.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 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 "seat.h"
7 #include "event_queue.h"
8 #include "keyboard.h"
9 #include "pointer.h"
10 #include "touch.h"
11 #include "wayland_pointer_p.h"
12 // Wayland
13 #include <wayland-client-protocol.h>
14 
15 namespace KWayland
16 {
17 namespace Client
18 {
19 
20 class Q_DECL_HIDDEN Seat::Private
21 {
22 public:
23  Private(Seat *q);
24  void resetSeat();
25  void setup(wl_seat *seat);
26 
27  WaylandPointer<wl_seat, wl_seat_destroy> seat;
28  EventQueue *queue = nullptr;
29  bool capabilityKeyboard = false;
30  bool capabilityPointer = false;
31  bool capabilityTouch = false;
32  QString name;
33 
34 private:
35  void setHasKeyboard(bool has);
36  void setHasPointer(bool has);
37  void setHasTouch(bool has);
38  void capabilitiesChanged(uint32_t capabilities);
39  void setName(const QString &name);
40  static void capabilitiesCallback(void *data, wl_seat *seat, uint32_t capabilities);
41  static void nameCallback(void *data, wl_seat *wl_seat, const char *name);
42 
43  Seat *q;
44  static const wl_seat_listener s_listener;
45 };
46 
47 Seat::Private::Private(Seat *q)
48  : q(q)
49 {
50 }
51 
52 void Seat::Private::setup(wl_seat *s)
53 {
54  Q_ASSERT(s);
55  Q_ASSERT(!seat);
56  seat.setup(s);
57  wl_seat_add_listener(seat, &s_listener, this);
58 }
59 
60 const wl_seat_listener Seat::Private::s_listener = {
61  capabilitiesCallback,
62  nameCallback
63 };
64 
65 Seat::Seat(QObject *parent)
66  : QObject(parent)
67  , d(new Private(this))
68 {
69 }
70 
71 Seat::~Seat()
72 {
73  release();
74 }
75 
76 void Seat::release()
77 {
78  if (!d->seat) {
79  return;
80  }
81  emit interfaceAboutToBeReleased();
82  d->seat.release();
83  d->resetSeat();
84 }
85 
86 void Seat::destroy()
87 {
88  if (!d->seat) {
89  return;
90  }
91  emit interfaceAboutToBeDestroyed();
92  d->seat.destroy();
93  d->resetSeat();
94 }
95 
96 void Seat::setEventQueue(EventQueue *queue)
97 {
98  d->queue = queue;
99 }
100 
101 EventQueue *Seat::eventQueue()
102 {
103  return d->queue;
104 }
105 
106 void Seat::Private::resetSeat()
107 {
108  setHasKeyboard(false);
109  setHasPointer(false);
110  setHasTouch(false);
111  setName(QString());
112 }
113 
114 void Seat::Private::setHasKeyboard(bool has)
115 {
116  if (capabilityKeyboard == has) {
117  return;
118  }
119  capabilityKeyboard = has;
120  emit q->hasKeyboardChanged(capabilityKeyboard);
121 }
122 
123 void Seat::Private::setHasPointer(bool has)
124 {
125  if (capabilityPointer == has) {
126  return;
127  }
128  capabilityPointer = has;
129  emit q->hasPointerChanged(capabilityPointer);
130 }
131 
132 void Seat::Private::setHasTouch(bool has)
133 {
134  if (capabilityTouch == has) {
135  return;
136  }
137  capabilityTouch = has;
138  emit q->hasTouchChanged(capabilityTouch);
139 }
140 
141 void Seat::setup(wl_seat *seat)
142 {
143  d->setup(seat);
144 }
145 
146 void Seat::Private::capabilitiesCallback(void *data, wl_seat *seat, uint32_t capabilities)
147 {
148  auto s = reinterpret_cast<Seat::Private*>(data);
149  Q_ASSERT(s->seat == seat);
150  s->capabilitiesChanged(capabilities);
151 }
152 
153 void Seat::Private::nameCallback(void *data, wl_seat *seat, const char *name)
154 {
155  auto s = reinterpret_cast<Seat::Private*>(data);
156  Q_ASSERT(s->seat == seat);
157  s->setName(QString::fromUtf8(name));
158 }
159 
160 void Seat::Private::capabilitiesChanged(uint32_t capabilities)
161 {
162  setHasKeyboard(capabilities & WL_SEAT_CAPABILITY_KEYBOARD);
163  setHasPointer(capabilities & WL_SEAT_CAPABILITY_POINTER);
164  setHasTouch(capabilities & WL_SEAT_CAPABILITY_TOUCH);
165 }
166 
167 Keyboard *Seat::createKeyboard(QObject *parent)
168 {
169  Q_ASSERT(isValid());
170  Q_ASSERT(d->capabilityKeyboard);
171  Keyboard *k = new Keyboard(parent);
172  connect(this, &Seat::interfaceAboutToBeReleased, k, &Keyboard::release);
173  connect(this, &Seat::interfaceAboutToBeDestroyed, k, &Keyboard::destroy);
174  auto w = wl_seat_get_keyboard(d->seat);
175  if (d->queue) {
176  d->queue->addProxy(w);
177  }
178  k->setup(w);
179  return k;
180 }
181 
182 Pointer *Seat::createPointer(QObject *parent)
183 {
184  Q_ASSERT(isValid());
185  Q_ASSERT(d->capabilityPointer);
186  Pointer *p = new Pointer(parent);
187  connect(this, &Seat::interfaceAboutToBeReleased, p, &Pointer::release);
188  connect(this, &Seat::interfaceAboutToBeDestroyed, p, &Pointer::destroy);
189  auto w = wl_seat_get_pointer(d->seat);
190  if (d->queue) {
191  d->queue->addProxy(w);
192  }
193  p->setup(w);
194  return p;
195 }
196 
197 Touch *Seat::createTouch(QObject *parent)
198 {
199  Q_ASSERT(isValid());
200  Q_ASSERT(d->capabilityTouch);
201  Touch *t = new Touch(parent);
202  connect(this, &Seat::interfaceAboutToBeReleased, t, &Touch::release);
203  connect(this, &Seat::interfaceAboutToBeDestroyed, t, &Touch::destroy);
204  auto w = wl_seat_get_touch(d->seat);
205  if (d->queue) {
206  d->queue->addProxy(w);
207  }
208  t->setup(w);
209  return t;
210 }
211 
212 void Seat::Private::setName(const QString &n)
213 {
214  if (name == n) {
215  return;
216  }
217  name = n;
218  emit q->nameChanged(name);
219 }
220 
221 bool Seat::isValid() const
222 {
223  return d->seat.isValid();
224 }
225 
226 bool Seat::hasKeyboard() const
227 {
228  return d->capabilityKeyboard;
229 }
230 
231 bool Seat::hasPointer() const
232 {
233  return d->capabilityPointer;
234 }
235 
236 bool Seat::hasTouch() const
237 {
238  return d->capabilityTouch;
239 }
240 
241 QString Seat::name() const
242 {
243  return d->name;
244 }
245 
246 Seat::operator wl_seat*()
247 {
248  return d->seat;
249 }
250 
251 Seat::operator wl_seat*() const
252 {
253  return d->seat;
254 }
255 
256 }
257 }
void setup(wl_keyboard *keyboard)
Setup this Keyboard to manage the keyboard.
Definition: keyboard.cpp:88
void setup(wl_touch *touch)
Setup this Touch to manage the touch.
Definition: touch.cpp:258
Wrapper class for wl_event_queue interface.
Definition: event_queue.h:55
Wrapper for the wl_touch interface.
Definition: touch.h:88
void setup(wl_pointer *pointer)
Setup this Pointer to manage the pointer.
Definition: pointer.cpp:106
QString fromUtf8(const char *str, int size)
Wrapper for the wl_keyboard interface.
Definition: keyboard.h:31
Wrapper for the wl_pointer interface.
Definition: pointer.h:32
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
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.