KWayland

keystate_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Aleix Pol Gonzalez <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #include "keystate_interface.h"
8 #include "global_p.h"
9 #include "display.h"
10 
11 #include <QDebug>
12 #include <QVector>
13 #include <wayland-server.h>
14 #include <wayland-keystate-server-protocol.h>
15 
16 namespace KWayland
17 {
18 namespace Server
19 {
20 
21 class KeyStateInterface::Private : public Global::Private
22 {
23 public:
24  Private(Display *d)
25  : Global::Private(d, &org_kde_kwin_keystate_interface, s_version)
26  {}
27 
28  void bind(wl_client * client, uint32_t version, uint32_t id) override {
29  auto c = display->getConnection(client);
30  wl_resource *resource = c->createResource(&org_kde_kwin_keystate_interface, qMin(version, s_version), id);
31  if (!resource) {
32  wl_client_post_no_memory(client);
33  return;
34  }
35  wl_resource_set_implementation(resource, &s_interface, this, unbind);
36  m_resources << resource;
37  }
38 
39  static void unbind(wl_resource *resource) {
40  auto *d = reinterpret_cast<Private*>(wl_resource_get_user_data(resource));
41  d->m_resources.removeAll(resource);
42  }
43 
44 
45  static void fetchStatesCallback(struct wl_client */*client*/, struct wl_resource *resource) {
46  auto s = reinterpret_cast<KeyStateInterface::Private*>(wl_resource_get_user_data(resource));
47 
48  for (int i=0; i<s->m_keyStates.count(); ++i)
49  org_kde_kwin_keystate_send_stateChanged(resource, i, s->m_keyStates[i]);
50  }
51 
52  static const quint32 s_version;
53  QVector<wl_resource*> m_resources;
54  QVector<State> m_keyStates = QVector<State>(3, Unlocked);
55  static const struct org_kde_kwin_keystate_interface s_interface;
56 };
57 
58 const quint32 KeyStateInterface::Private::s_version = 1;
59 
60 KeyStateInterface::KeyStateInterface(Display* d, QObject* parent)
61  : Global(new Private(d), parent)
62 {}
63 
64 KeyStateInterface::~KeyStateInterface() = default;
65 
66 const struct org_kde_kwin_keystate_interface KeyStateInterface::Private::s_interface = {
67  fetchStatesCallback
68 };
69 
70 void KeyStateInterface::setState(KeyStateInterface::Key key, KeyStateInterface::State state)
71 {
72  auto dptr = static_cast<KeyStateInterface::Private*>(d.data());
73  dptr->m_keyStates[int(key)] = state;
74 
75  for(auto r : qAsConst(dptr->m_resources)) {
76  org_kde_kwin_keystate_send_stateChanged(r, int(key), int(state));
77  }
78 }
79 
80 }
81 
82 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:48:18 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.