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 "region_interface.h"
9 #include "display.h"
10 #include "global_p.h"
11 #include "resource_p.h"
12 #include "surface_interface_p.h"
13 
14 #include <wayland-server.h>
15 #include <wayland-contrast-server-protocol.h>
16 
17 namespace KWayland
18 {
19 namespace Server
20 {
21 
22 class ContrastManagerInterface::Private : public Global::Private
23 {
24 public:
25  Private(ContrastManagerInterface *q, Display *d);
26 
27 private:
28  void bind(wl_client *client, uint32_t version, uint32_t id) override;
29  void createContrast(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface);
30 
31  static void createCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface);
32  static void unsetCallback(wl_client *client, wl_resource *resource, wl_resource *surface);
33  static void unbind(wl_resource *resource);
34  static Private *cast(wl_resource *r) {
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 = {
51  createCallback,
52  unsetCallback
53 };
54 #endif
55 
56 ContrastManagerInterface::Private::Private(ContrastManagerInterface *q, Display *d)
57  : Global::Private(d, &org_kde_kwin_contrast_manager_interface, s_version)
58  , q(q)
59 {
60 }
61 
62 void ContrastManagerInterface::Private::bind(wl_client *client, uint32_t version, uint32_t id)
63 {
64  auto c = display->getConnection(client);
65  wl_resource *resource = c->createResource(&org_kde_kwin_contrast_manager_interface, qMin(version, s_version), id);
66  if (!resource) {
67  wl_client_post_no_memory(client);
68  return;
69  }
70  auto ref = new QPointer<ContrastManagerInterface>(q);//deleted in unbind
71  wl_resource_set_implementation(resource, &s_interface, ref, unbind);
72 }
73 
74 void ContrastManagerInterface::Private::unbind(wl_resource *resource)
75 {
76  delete reinterpret_cast<QPointer<ContrastManagerInterface>*>(wl_resource_get_user_data(resource));
77 }
78 
79 void ContrastManagerInterface::Private::createCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface)
80 {
81  auto m = cast(resource);
82  if (!m) {
83  return;// will happen if global is deleted
84  }
85  m->createContrast(client, resource, id, surface);
86 }
87 
88 void ContrastManagerInterface::Private::createContrast(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *surface)
89 {
90  SurfaceInterface *s = SurfaceInterface::get(surface);
91  if (!s) {
92  return;
93  }
94 
95  ContrastInterface *contrast = new ContrastInterface(q, resource);
96  contrast->create(display->getConnection(client), wl_resource_get_version(resource), id);
97  if (!contrast->resource()) {
98  wl_resource_post_no_memory(resource);
99  delete contrast;
100  return;
101  }
102  s->d_func()->setContrast(QPointer<ContrastInterface>(contrast));
103 }
104 
105 void ContrastManagerInterface::Private::unsetCallback(wl_client *client, wl_resource *resource, wl_resource *surface)
106 {
107  Q_UNUSED(client)
108  Q_UNUSED(resource)
109  SurfaceInterface *s = SurfaceInterface::get(surface);
110  if (!s) {
111  return;
112  }
113  s->d_func()->setContrast(QPointer<ContrastInterface>());
114 }
115 
116 ContrastManagerInterface::ContrastManagerInterface(Display *display, QObject *parent)
117  : Global(new Private(this, display), parent)
118 {
119 }
120 
121 ContrastManagerInterface::~ContrastManagerInterface() = default;
122 
123 class ContrastInterface::Private : public Resource::Private
124 {
125 public:
126  Private(ContrastInterface *q, ContrastManagerInterface *c, wl_resource *parentResource);
127  ~Private();
128 
129  QRegion pendingRegion;
130  QRegion currentRegion;
131  qreal pendingContrast;
132  qreal currentContrast;
133  qreal pendingIntensity;
134  qreal currentIntensity;
135  qreal pendingSaturation;
136  qreal currentSaturation;
137 
138 private:
139  void commit();
140  //TODO
141  ContrastInterface *q_func() {
142  return reinterpret_cast<ContrastInterface *>(q);
143  }
144 
145  static void commitCallback(wl_client *client, wl_resource *resource);
146  static void setRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region);
147  static void setContrastCallback(wl_client *client, wl_resource *resource, wl_fixed_t contrast);
148  static void setIntensityCallback(wl_client *client, wl_resource *resource, wl_fixed_t intensity);
149  static void setSaturationCallback(wl_client *client, wl_resource *resource, wl_fixed_t saturation);
150 
151  static const struct org_kde_kwin_contrast_interface s_interface;
152 };
153 
154 #ifndef K_DOXYGEN
155 const struct org_kde_kwin_contrast_interface ContrastInterface::Private::s_interface = {
156  commitCallback,
157  setRegionCallback,
158  setContrastCallback,
159  setIntensityCallback,
160  setSaturationCallback,
161  resourceDestroyedCallback
162 };
163 #endif
164 
165 void ContrastInterface::Private::commitCallback(wl_client *client, wl_resource *resource)
166 {
167  Q_UNUSED(client)
168  cast<Private>(resource)->commit();
169 }
170 
171 void ContrastInterface::Private::commit()
172 {
173  currentRegion = pendingRegion;
174  currentContrast = pendingContrast;
175  currentIntensity = pendingIntensity;
176  currentSaturation = pendingSaturation;
177 }
178 
179 void ContrastInterface::Private::setRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region)
180 {
181  Q_UNUSED(client)
182  Private *p = cast<Private>(resource);
183  RegionInterface *r = RegionInterface::get(region);
184  if (r) {
185  p->pendingRegion = r->region();
186  } else {
187  p->pendingRegion = QRegion();
188  }
189 }
190 
191 void ContrastInterface::Private::setContrastCallback(wl_client *client, wl_resource *resource, wl_fixed_t contrast)
192 {
193  Q_UNUSED(client)
194  Private *p = cast<Private>(resource);
195  p->pendingContrast = wl_fixed_to_double(contrast);
196 }
197 
198 void ContrastInterface::Private::setIntensityCallback(wl_client *client, wl_resource *resource, wl_fixed_t intensity)
199 {
200  Q_UNUSED(client)
201  Private *p = cast<Private>(resource);
202  p->pendingIntensity = wl_fixed_to_double(intensity);
203 }
204 
205 void ContrastInterface::Private::setSaturationCallback(wl_client *client, wl_resource *resource, wl_fixed_t saturation)
206 {
207  Q_UNUSED(client)
208  Private *p = cast<Private>(resource);
209  p->pendingSaturation = wl_fixed_to_double(saturation);
210 }
211 
212 ContrastInterface::Private::Private(ContrastInterface *q, ContrastManagerInterface *c, wl_resource *parentResource)
213  : Resource::Private(q, c, parentResource, &org_kde_kwin_contrast_interface, &s_interface)
214 {
215 }
216 
217 ContrastInterface::Private::~Private() = default;
218 
219 ContrastInterface::ContrastInterface(ContrastManagerInterface *parent, wl_resource *parentResource)
220  : Resource(new Private(this, parent, parentResource))
221 {
222 }
223 
224 ContrastInterface::~ContrastInterface() = default;
225 
226 QRegion ContrastInterface::region() const
227 {
228  Q_D();
229  return d->currentRegion;
230 }
231 
232 qreal ContrastInterface::contrast() const
233 {
234  Q_D();
235  return d->currentContrast;
236 }
237 
238 qreal ContrastInterface::intensity() const
239 {
240  Q_D();
241  return d->currentIntensity;
242 }
243 
244 qreal ContrastInterface::saturation() const
245 {
246  Q_D();
247  return d->currentSaturation;
248 }
249 
250 ContrastInterface::Private *ContrastInterface::d_func() const
251 {
252  return reinterpret_cast<Private*>(d.data());
253 }
254 
255 }
256 }
void ref()
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 Thu Jul 9 2020 22:49:29 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.