1 /********************************************************************
2 Copyright 2020  Faveraux Adrien <ad1rie3@hotmail.fr>
3 
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11 
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library.  If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
20 #include "contrast.h"
21 #include "contrast_p.h"
22 
23 #include "wayland/client.h"
24 #include "wayland/global.h"
25 #include "wayland/resource.h"
26 
27 #include "client.h"
28 #include "display.h"
29 #include "region.h"
30 #include "surface.h"
31 #include "surface_p.h"
32 
33 #include <wayland-server.h>
34 
35 namespace Wrapland::Server
36 {
37 
38 const struct org_kde_kwin_contrast_manager_interface ContrastManager::Private::s_interface = {
39     cb<createCallback>,
40     unsetCallback,
41 };
42 
Private(Display * display,ContrastManager * q)43 ContrastManager::Private::Private(Display* display, ContrastManager* q)
44     : ContrastManagerGlobal(q, display, &org_kde_kwin_contrast_manager_interface, &s_interface)
45 {
46     create();
47 }
48 
49 ContrastManager::Private::~Private() = default;
50 
createCallback(ContrastManagerBind * bind,uint32_t id,wl_resource * wlSurface)51 void ContrastManager::Private::createCallback(ContrastManagerBind* bind,
52                                               uint32_t id,
53                                               wl_resource* wlSurface)
54 {
55     auto surface = Wayland::Resource<Surface>::handle(wlSurface);
56 
57     auto contrast = new Contrast(bind->client()->handle(), bind->version(), id);
58     if (!contrast->d_ptr->resource()) {
59         bind->post_no_memory();
60         delete contrast;
61         return;
62     }
63 
64     surface->d_ptr->setContrast(QPointer<Contrast>(contrast));
65 }
66 
unsetCallback(wl_client * wlClient,wl_resource * wlResource,wl_resource * wlSurface)67 void ContrastManager::Private::unsetCallback([[maybe_unused]] wl_client* wlClient,
68                                              [[maybe_unused]] wl_resource* wlResource,
69                                              wl_resource* wlSurface)
70 {
71     auto surface = Wayland::Resource<Surface>::handle(wlSurface);
72     surface->d_ptr->setContrast(QPointer<Contrast>());
73 }
74 
ContrastManager(Display * display,QObject * parent)75 ContrastManager::ContrastManager(Display* display, QObject* parent)
76     : QObject(parent)
77     , d_ptr(new Private(display, this))
78 {
79 }
80 
81 ContrastManager::~ContrastManager() = default;
82 
83 const struct org_kde_kwin_contrast_interface Contrast::Private::s_interface = {
84     commitCallback,
85     setRegionCallback,
86     setContrastCallback,
87     setIntensityCallback,
88     setSaturationCallback,
89     destroyCallback,
90 };
91 
Private(Client * client,uint32_t version,uint32_t id,Contrast * q)92 Contrast::Private::Private(Client* client, uint32_t version, uint32_t id, Contrast* q)
93     : Wayland::Resource<Contrast>(client,
94                                   version,
95                                   id,
96                                   &org_kde_kwin_contrast_interface,
97                                   &s_interface,
98                                   q)
99 {
100 }
101 
102 Contrast::Private::~Private() = default;
103 
commitCallback(wl_client * wlClient,wl_resource * wlResource)104 void Contrast::Private::commitCallback([[maybe_unused]] wl_client* wlClient,
105                                        wl_resource* wlResource)
106 {
107     auto priv = handle(wlResource)->d_ptr;
108     priv->commit();
109 }
110 
commit()111 void Contrast::Private::commit()
112 {
113     currentRegion = pendingRegion;
114     currentContrast = pendingContrast;
115     currentIntensity = pendingIntensity;
116     currentSaturation = pendingSaturation;
117 }
118 
setRegionCallback(wl_client * wlClient,wl_resource * wlResource,wl_resource * wlRegion)119 void Contrast::Private::setRegionCallback([[maybe_unused]] wl_client* wlClient,
120                                           wl_resource* wlResource,
121                                           wl_resource* wlRegion)
122 {
123     auto priv = handle(wlResource)->d_ptr;
124     auto region = Wayland::Resource<Region>::handle(wlRegion);
125 
126     priv->pendingRegion = region ? region->region() : QRegion();
127 }
128 
setContrastCallback(wl_client * wlClient,wl_resource * wlResource,wl_fixed_t contrast)129 void Contrast::Private::setContrastCallback([[maybe_unused]] wl_client* wlClient,
130                                             wl_resource* wlResource,
131                                             wl_fixed_t contrast)
132 {
133     auto priv = handle(wlResource)->d_ptr;
134     priv->pendingContrast = wl_fixed_to_double(contrast);
135 }
136 
setIntensityCallback(wl_client * wlClient,wl_resource * wlResource,wl_fixed_t intensity)137 void Contrast::Private::setIntensityCallback([[maybe_unused]] wl_client* wlClient,
138                                              wl_resource* wlResource,
139                                              wl_fixed_t intensity)
140 {
141     auto priv = handle(wlResource)->d_ptr;
142     priv->pendingIntensity = wl_fixed_to_double(intensity);
143 }
144 
setSaturationCallback(wl_client * wlClient,wl_resource * wlResource,wl_fixed_t saturation)145 void Contrast::Private::setSaturationCallback([[maybe_unused]] wl_client* wlClient,
146                                               wl_resource* wlResource,
147                                               wl_fixed_t saturation)
148 {
149     auto priv = handle(wlResource)->d_ptr;
150     priv->pendingSaturation = wl_fixed_to_double(saturation);
151 }
152 
Contrast(Client * client,uint32_t version,uint32_t id)153 Contrast::Contrast(Client* client, uint32_t version, uint32_t id)
154     : d_ptr(new Private(client, version, id, this))
155 {
156 }
157 
region() const158 QRegion Contrast::region() const
159 {
160     return d_ptr->currentRegion;
161 }
162 
contrast() const163 qreal Contrast::contrast() const
164 {
165     return d_ptr->currentContrast;
166 }
167 
intensity() const168 qreal Contrast::intensity() const
169 {
170     return d_ptr->currentIntensity;
171 }
172 
saturation() const173 qreal Contrast::saturation() const
174 {
175     return d_ptr->currentSaturation;
176 }
177 }
178