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 "display.h"
9 #include "global_p.h"
10 
11 #include <QDebug>
12 #include <QVector>
13 #include <wayland-keystate-server-protocol.h>
14 #include <wayland-server.h>
15 
16 namespace KWayland
17 {
18 namespace Server
19 {
20 class KeyStateInterface::Private : public Global::Private
21 {
22 public:
23  Private(Display *d)
24  : Global::Private(d, &org_kde_kwin_keystate_interface, s_version)
25  {
26  }
27 
28  void bind(wl_client *client, uint32_t version, uint32_t id) override
29  {
30  auto c = display->getConnection(client);
31  wl_resource *resource = c->createResource(&org_kde_kwin_keystate_interface, qMin(version, s_version), id);
32  if (!resource) {
33  wl_client_post_no_memory(client);
34  return;
35  }
36  wl_resource_set_implementation(resource, &s_interface, this, unbind);
37  m_resources << resource;
38  }
39 
40  static void unbind(wl_resource *resource)
41  {
42  auto *d = reinterpret_cast<Private *>(wl_resource_get_user_data(resource));
43  d->m_resources.removeAll(resource);
44  }
45 
46  static void fetchStatesCallback(struct wl_client * /*client*/, struct wl_resource *resource)
47  {
48  auto s = reinterpret_cast<KeyStateInterface::Private *>(wl_resource_get_user_data(resource));
49 
50  for (int i = 0; i < s->m_keyStates.count(); ++i) {
51  org_kde_kwin_keystate_send_stateChanged(resource, i, s->m_keyStates[i]);
52  }
53  }
54 
55  static const quint32 s_version;
56  QVector<wl_resource *> m_resources;
57  QVector<State> m_keyStates = QVector<State>(3, Unlocked);
58  static const struct org_kde_kwin_keystate_interface s_interface;
59 };
60 
61 const quint32 KeyStateInterface::Private::s_version = 1;
62 
63 KeyStateInterface::KeyStateInterface(Display *d, QObject *parent)
64  : Global(new Private(d), parent)
65 {
66 }
67 
68 KeyStateInterface::~KeyStateInterface() = default;
69 
70 const struct org_kde_kwin_keystate_interface KeyStateInterface::Private::s_interface = {fetchStatesCallback};
71 
72 void KeyStateInterface::setState(KeyStateInterface::Key key, KeyStateInterface::State state)
73 {
74  auto dptr = static_cast<KeyStateInterface::Private *>(d.data());
75  dptr->m_keyStates[int(key)] = state;
76 
77  for (auto r : std::as_const(dptr->m_resources)) {
78  org_kde_kwin_keystate_send_stateChanged(r, int(key), int(state));
79  }
80 }
81 
82 }
83 
84 }
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.