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 "wayland/client.h"
21 #include "wayland/global.h"
22 #include "wayland/resource.h"
23 
24 #include "client.h"
25 #include "display.h"
26 #include "region.h"
27 #include "slide_p.h"
28 #include "surface.h"
29 #include "surface_p.h"
30 
31 #include <wayland-server.h>
32 
33 namespace Wrapland::Server
34 {
35 
36 const struct org_kde_kwin_slide_manager_interface SlideManager::Private::s_interface = {
37     cb<createCallback>,
38     cb<unsetCallback>,
39 };
40 
Private(Display * display,SlideManager * qptr)41 SlideManager::Private::Private(Display* display, SlideManager* qptr)
42     : SlideManagerGlobal(qptr, display, &org_kde_kwin_slide_manager_interface, &s_interface)
43 {
44     create();
45 }
46 
createCallback(SlideManagerBind * bind,uint32_t id,wl_resource * wlSurface)47 void SlideManager::Private::createCallback(SlideManagerBind* bind,
48                                            uint32_t id,
49                                            wl_resource* wlSurface)
50 {
51     auto surface = Wayland::Resource<Surface>::handle(wlSurface);
52 
53     auto slide = new Slide(bind->client()->handle(), bind->version(), id);
54     if (!slide->d_ptr->resource()) {
55         bind->post_no_memory();
56         delete slide;
57         return;
58     }
59 
60     surface->d_ptr->setSlide(QPointer<Slide>(slide));
61 }
62 
unsetCallback(SlideManagerBind * bind,wl_resource * wlSurface)63 void SlideManager::Private::unsetCallback([[maybe_unused]] SlideManagerBind* bind,
64                                           wl_resource* wlSurface)
65 {
66     auto surface = Wayland::Resource<Surface>::handle(wlSurface);
67     surface->d_ptr->setSlide(QPointer<Slide>());
68 }
69 
SlideManager(Display * display,QObject * parent)70 SlideManager::SlideManager(Display* display, QObject* parent)
71     : QObject(parent)
72     , d_ptr(new Private(display, this))
73 {
74 }
75 
76 SlideManager::~SlideManager() = default;
77 
78 const struct org_kde_kwin_slide_interface Slide::Private::s_interface = {
79     commitCallback,
80     setLocationCallback,
81     setOffsetCallback,
82     destroyCallback,
83 };
84 
Private(Client * client,uint32_t version,uint32_t id,Slide * qptr)85 Slide::Private::Private(Client* client, uint32_t version, uint32_t id, Slide* qptr)
86     : Wayland::Resource<Slide>(client,
87                                version,
88                                id,
89                                &org_kde_kwin_slide_interface,
90                                &s_interface,
91                                qptr)
92 {
93 }
94 
commitCallback(wl_client * wlClient,wl_resource * wlResource)95 void Slide::Private::commitCallback([[maybe_unused]] wl_client* wlClient, wl_resource* wlResource)
96 {
97     auto priv = handle(wlResource)->d_ptr;
98     priv->currentLocation = priv->pendingLocation;
99     priv->currentOffset = priv->pendingOffset;
100 }
101 
setLocationCallback(wl_client * wlClient,wl_resource * wlResource,uint32_t location)102 void Slide::Private::setLocationCallback([[maybe_unused]] wl_client* wlClient,
103                                          wl_resource* wlResource,
104                                          uint32_t location)
105 {
106     auto priv = handle(wlResource)->d_ptr;
107     priv->pendingLocation = static_cast<Slide::Location>(location);
108 }
109 
setOffsetCallback(wl_client * wlClient,wl_resource * wlResource,int32_t offset)110 void Slide::Private::setOffsetCallback([[maybe_unused]] wl_client* wlClient,
111                                        wl_resource* wlResource,
112                                        int32_t offset)
113 {
114     auto priv = handle(wlResource)->d_ptr;
115     priv->pendingOffset = offset;
116 }
117 
Slide(Client * client,uint32_t version,uint32_t id)118 Slide::Slide(Client* client, uint32_t version, uint32_t id)
119     : d_ptr(new Private(client, version, id, this))
120 {
121 }
122 
location() const123 Slide::Location Slide::location() const
124 {
125     return d_ptr->currentLocation;
126 }
127 
offset() const128 uint32_t Slide::offset() const
129 {
130     return d_ptr->currentOffset;
131 }
132 
133 }
134