KWayland

contrast_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Martin Gräßlin <[email protected]>
3  SPDX-FileCopyrightText: 2015 Marco Martin <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7 #include "contrast_interface.h"
8 #include "display.h"
9 #include "global_p.h"
10 #include "region_interface.h"
11 #include "resource_p.h"
12 #include "surface_interface_p.h"
13 
14 #include <wayland-contrast-server-protocol.h>
15 #include <wayland-server.h>
16 
17 namespace KWayland
18 {
19 namespace Server
20 {
21 class ContrastManagerInterface::Private : public Global::Private
22 {
23 public:
24  Private(ContrastManagerInterface *q, Display *d);
25 
26 private:
27  void bind(wl_client *client, uint32_t version, uint32_t id) override;
28  void createContrast(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface);
29 
30  static void createCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface);
31  static void unsetCallback(wl_client *client, wl_resource *resource, wl_resource *surface);
32  static void unbind(wl_resource *resource);
33  static Private *cast(wl_resource *r)
34  {
35  auto contrastManager = reinterpret_cast<QPointer<ContrastManagerInterface> *>(wl_resource_get_user_data(r))->data();
36  if (contrastManager) {
37  return static_cast<Private *>(contrastManager->d.data());
38  }
39  return nullptr;
40  }
41 
42  ContrastManagerInterface *q;
43  static const struct org_kde_kwin_contrast_manager_interface s_interface;
44  static const quint32 s_version;
45 };
46 
47 const quint32 ContrastManagerInterface::Private::s_version = 1;
48 
49 #ifndef K_DOXYGEN
50 const struct org_kde_kwin_contrast_manager_interface ContrastManagerInterface::Private::s_interface = {createCallback, unsetCallback};
51 #endif
52 
53 ContrastManagerInterface::Private::Private(ContrastManagerInterface *q, Display *d)
54  : Global::Private(d, &org_kde_kwin_contrast_manager_interface, s_version)
55  , q(q)
56 {
57 }
58 
59 void ContrastManagerInterface::Private::bind(wl_client *client, uint32_t version, uint32_t id)
60 {
61  auto c = display->getConnection(client);
62  wl_resource *resource = c->createResource(&org_kde_kwin_contrast_manager_interface, qMin(version, s_version), id);
63  if (!resource) {
64  wl_client_post_no_memory(client);
65  return;
66  }
67  auto ref = new QPointer<ContrastManagerInterface>(q); // deleted in unbind
68  wl_resource_set_implementation(resource, &s_interface, ref, unbind);
69 }
70 
71 void ContrastManagerInterface::Private::unbind(wl_resource *resource)
72 {
73  delete reinterpret_cast<QPointer<ContrastManagerInterface> *>(wl_resource_get_user_data(resource));
74 }
75 
76 void ContrastManagerInterface::Private::createCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface)
77 {
78  auto m = cast(resource);
79  if (!m) {
80  return; // will happen if global is deleted
81  }
82  m->createContrast(client, resource, id, surface);
83 }
84 
85 void ContrastManagerInterface::Private::createContrast(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface)
86 {
87  SurfaceInterface *s = SurfaceInterface::get(surface);
88  if (!s) {
89  return;
90  }
91 
92  ContrastInterface *contrast = new ContrastInterface(q, resource);
93  contrast->create(display->getConnection(client), wl_resource_get_version(resource), id);
94  if (!contrast->resource()) {
95  wl_resource_post_no_memory(resource);
96  delete contrast;
97  return;
98  }
99  s->d_func()->setContrast(QPointer<ContrastInterface>(contrast));
100 }
101 
102 void ContrastManagerInterface::Private::unsetCallback(wl_client *client, wl_resource *resource, wl_resource *surface)
103 {
104  Q_UNUSED(client)
105  Q_UNUSED(resource)
106  SurfaceInterface *s = SurfaceInterface::get(surface);
107  if (!s) {
108  return;
109  }
110  s->d_func()->setContrast(QPointer<ContrastInterface>());
111 }
112 
113 ContrastManagerInterface::ContrastManagerInterface(Display *display, QObject *parent)
114  : Global(new Private(this, display), parent)
115 {
116 }
117 
118 ContrastManagerInterface::~ContrastManagerInterface() = default;
119 
120 class ContrastInterface::Private : public Resource::Private
121 {
122 public:
123  Private(ContrastInterface *q, ContrastManagerInterface *c, wl_resource *parentResource);
124  ~Private() override;
125 
126  QRegion pendingRegion;
127  QRegion currentRegion;
128  qreal pendingContrast;
129  qreal currentContrast;
130  qreal pendingIntensity;
131  qreal currentIntensity;
132  qreal pendingSaturation;
133  qreal currentSaturation;
134 
135 private:
136  void commit();
137  // TODO
138  ContrastInterface *q_func()
139  {
140  return reinterpret_cast<ContrastInterface *>(q);
141  }
142 
143  static void commitCallback(wl_client *client, wl_resource *resource);
144  static void setRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region);
145  static void setContrastCallback(wl_client *client, wl_resource *resource, wl_fixed_t contrast);
146  static void setIntensityCallback(wl_client *client, wl_resource *resource, wl_fixed_t intensity);
147  static void setSaturationCallback(wl_client *client, wl_resource *resource, wl_fixed_t saturation);
148 
149  static const struct org_kde_kwin_contrast_interface s_interface;
150 };
151 
152 #ifndef K_DOXYGEN
153 const struct org_kde_kwin_contrast_interface ContrastInterface::Private::s_interface =
154  {commitCallback, setRegionCallback, setContrastCallback, setIntensityCallback, setSaturationCallback, resourceDestroyedCallback};
155 #endif
156 
157 void ContrastInterface::Private::commitCallback(wl_client *client, wl_resource *resource)
158 {
159  Q_UNUSED(client)
160  cast<Private>(resource)->commit();
161 }
162 
163 void ContrastInterface::Private::commit()
164 {
165  currentRegion = pendingRegion;
166  currentContrast = pendingContrast;
167  currentIntensity = pendingIntensity;
168  currentSaturation = pendingSaturation;
169 }
170 
171 void ContrastInterface::Private::setRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region)
172 {
173  Q_UNUSED(client)
174  Private *p = cast<Private>(resource);
175  RegionInterface *r = RegionInterface::get(region);
176  if (r) {
177  p->pendingRegion = r->region();
178  } else {
179  p->pendingRegion = QRegion();
180  }
181 }
182 
183 void ContrastInterface::Private::setContrastCallback(wl_client *client, wl_resource *resource, wl_fixed_t contrast)
184 {
185  Q_UNUSED(client)
186  Private *p = cast<Private>(resource);
187  p->pendingContrast = wl_fixed_to_double(contrast);
188 }
189 
190 void ContrastInterface::Private::setIntensityCallback(wl_client *client, wl_resource *resource, wl_fixed_t intensity)
191 {
192  Q_UNUSED(client)
193  Private *p = cast<Private>(resource);
194  p->pendingIntensity = wl_fixed_to_double(intensity);
195 }
196 
197 void ContrastInterface::Private::setSaturationCallback(wl_client *client, wl_resource *resource, wl_fixed_t saturation)
198 {
199  Q_UNUSED(client)
200  Private *p = cast<Private>(resource);
201  p->pendingSaturation = wl_fixed_to_double(saturation);
202 }
203 
204 ContrastInterface::Private::Private(ContrastInterface *q, ContrastManagerInterface *c, wl_resource *parentResource)
205  : Resource::Private(q, c, parentResource, &org_kde_kwin_contrast_interface, &s_interface)
206 {
207 }
208 
209 ContrastInterface::Private::~Private() = default;
210 
211 ContrastInterface::ContrastInterface(ContrastManagerInterface *parent, wl_resource *parentResource)
212  : Resource(new Private(this, parent, parentResource))
213 {
214 }
215 
216 ContrastInterface::~ContrastInterface() = default;
217 
218 QRegion ContrastInterface::region() const
219 {
220  Q_D();
221  return d->currentRegion;
222 }
223 
224 qreal ContrastInterface::contrast() const
225 {
226  Q_D();
227  return d->currentContrast;
228 }
229 
230 qreal ContrastInterface::intensity() const
231 {
232  Q_D();
233  return d->currentIntensity;
234 }
235 
236 qreal ContrastInterface::saturation() const
237 {
238  Q_D();
239  return d->currentSaturation;
240 }
241 
242 ContrastInterface::Private *ContrastInterface::d_func() const
243 {
244  return reinterpret_cast<Private *>(d.data());
245 }
246 
247 }
248 }
void ref()
Q_D(Todo)
virtual QVariant get(ScriptableExtension *callerPrincipal, quint64 objId, const QString &propName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 03:56:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.