KWayland

keyboard.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 "keyboard.h"
7 #include "surface.h"
8 #include "wayland_pointer_p.h"
9 #include <QPointer>
10 // wayland
11 #include <wayland-client-protocol.h>
12 
13 namespace KWayland
14 {
15 namespace Client
16 {
17 
18 class Q_DECL_HIDDEN Keyboard::Private
19 {
20 public:
21  Private(Keyboard *q);
22  void setup(wl_keyboard *k);
23 
24  WaylandPointer<wl_keyboard, wl_keyboard_release> keyboard;
25  QPointer<Surface> enteredSurface;
26 
27  struct {
28  qint32 charactersPerSecond = 0;
29  qint32 delay = 0;
30  } repeatInfo;
31 private:
32  void enter(uint32_t serial, wl_surface *surface, wl_array *keys);
33  void leave(uint32_t serial);
34  static void keymapCallback(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size);
35  static void enterCallback(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys);
36  static void leaveCallback(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface);
37  static void keyCallback(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
38  static void modifiersCallback(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t modsDepressed,
39  uint32_t modsLatched, uint32_t modsLocked, uint32_t group);
40  static void repeatInfoCallback(void *data, wl_keyboard *keyboard, int32_t charactersPerSecond, int32_t delay);
41  Keyboard *q;
42  static const wl_keyboard_listener s_listener;
43 };
44 
45 Keyboard::Private::Private(Keyboard *q)
46  : q(q)
47 {
48 }
49 
50 void Keyboard::Private::setup(wl_keyboard *k)
51 {
52  Q_ASSERT(k);
53  Q_ASSERT(!keyboard);
54  keyboard.setup(k);
55  wl_keyboard_add_listener(keyboard, &s_listener, this);
56 }
57 
58 const wl_keyboard_listener Keyboard::Private::s_listener = {
59  keymapCallback,
60  enterCallback,
61  leaveCallback,
62  keyCallback,
63  modifiersCallback,
64  repeatInfoCallback
65 };
66 
67 Keyboard::Keyboard(QObject *parent)
68  : QObject(parent)
69  , d(new Private(this))
70 {
71 }
72 
73 Keyboard::~Keyboard()
74 {
75  release();
76 }
77 
78 void Keyboard::release()
79 {
80  d->keyboard.release();
81 }
82 
83 void Keyboard::destroy()
84 {
85  d->keyboard.destroy();
86 }
87 
88 void Keyboard::setup(wl_keyboard *keyboard)
89 {
90  d->setup(keyboard);
91 }
92 
93 void Keyboard::Private::enterCallback(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys)
94 {
95  auto k = reinterpret_cast<Private*>(data);
96  Q_ASSERT(k->keyboard == keyboard);
97  k->enter(serial, surface, keys);
98 }
99 
100 void Keyboard::Private::enter(uint32_t serial, wl_surface *surface, wl_array *keys)
101 {
102  Q_UNUSED(keys)
103  enteredSurface = Surface::get(surface);
104  emit q->entered(serial);
105 }
106 
107 void Keyboard::Private::leaveCallback(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface)
108 {
109  Q_UNUSED(surface)
110  auto k = reinterpret_cast<Private*>(data);
111  Q_ASSERT(k->keyboard == keyboard);
112  k->leave(serial);
113 }
114 
115 void Keyboard::Private::leave(uint32_t serial)
116 {
117  enteredSurface.clear();
118  emit q->left(serial);
119 }
120 
121 void Keyboard::Private::keyCallback(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
122 {
123  Q_UNUSED(serial)
124  auto k = reinterpret_cast<Keyboard::Private*>(data);
125  Q_ASSERT(k->keyboard == keyboard);
126  auto toState = [state] {
127  if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
128  return KeyState::Released;
129  } else {
130  return KeyState::Pressed;
131  }
132  };
133  emit k->q->keyChanged(key, toState(), time);
134 }
135 
136 void Keyboard::Private::keymapCallback(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size)
137 {
138  auto k = reinterpret_cast<Keyboard::Private*>(data);
139  Q_ASSERT(k->keyboard == keyboard);
140  if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
141  return;
142  }
143  emit k->q->keymapChanged(fd, size);
144 }
145 
146 void Keyboard::Private::modifiersCallback(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t modsDepressed,
147  uint32_t modsLatched, uint32_t modsLocked, uint32_t group)
148 {
149  Q_UNUSED(serial)
150  auto k = reinterpret_cast<Keyboard::Private*>(data);
151  Q_ASSERT(k->keyboard == keyboard);
152  emit k->q->modifiersChanged(modsDepressed, modsLatched, modsLocked, group);
153 }
154 
155 void Keyboard::Private::repeatInfoCallback(void *data, wl_keyboard *keyboard, int32_t charactersPerSecond, int32_t delay)
156 {
157  auto k = reinterpret_cast<Keyboard::Private*>(data);
158  Q_ASSERT(k->keyboard == keyboard);
159  k->repeatInfo.charactersPerSecond = qMax(charactersPerSecond, 0);
160  k->repeatInfo.delay = qMax(delay, 0);
161  emit k->q->keyRepeatChanged();
162 }
163 
164 Surface *Keyboard::enteredSurface()
165 {
166  return d->enteredSurface.data();
167 }
168 
169 Surface *Keyboard::enteredSurface() const
170 {
171  return d->enteredSurface.data();
172 }
173 
174 bool Keyboard::isValid() const
175 {
176  return d->keyboard.isValid();
177 }
178 
179 bool Keyboard::isKeyRepeatEnabled() const
180 {
181  return d->repeatInfo.charactersPerSecond > 0;
182 }
183 
184 qint32 Keyboard::keyRepeatDelay() const
185 {
186  return d->repeatInfo.delay;
187 }
188 
189 qint32 Keyboard::keyRepeatRate() const
190 {
191  return d->repeatInfo.charactersPerSecond;
192 }
193 
194 Keyboard::operator wl_keyboard*()
195 {
196  return d->keyboard;
197 }
198 
199 Keyboard::operator wl_keyboard*() const
200 {
201  return d->keyboard;
202 }
203 
204 }
205 }
Wrapper for the wl_surface interface.
Definition: surface.h:44
T * data() const const
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 12 2020 22:50:22 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.