1 /*
2     SPDX-FileCopyrightText: 2015 Marco Martin <notmart@gmail.com>
3 
4     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "slide_interface.h"
7 #include "display.h"
8 #include "surface_interface_p.h"
9 
10 #include <qwayland-server-slide.h>
11 
12 namespace KWaylandServer
13 {
14 static const quint32 s_version = 1;
15 
16 class SlideManagerInterfacePrivate : public QtWaylandServer::org_kde_kwin_slide_manager
17 {
18 public:
19     SlideManagerInterfacePrivate(SlideManagerInterface *_q, Display *display);
20 
21     SlideManagerInterface *q;
22 
23 protected:
24     void org_kde_kwin_slide_manager_destroy_global() override;
25     void org_kde_kwin_slide_manager_create(Resource *resource, uint32_t id, wl_resource *surface) override;
26     void org_kde_kwin_slide_manager_unset(Resource *resource, wl_resource *surface) override;
27 };
28 
org_kde_kwin_slide_manager_destroy_global()29 void SlideManagerInterfacePrivate::org_kde_kwin_slide_manager_destroy_global()
30 {
31     delete q;
32 }
33 
org_kde_kwin_slide_manager_create(Resource * resource,uint32_t id,wl_resource * surface)34 void SlideManagerInterfacePrivate::org_kde_kwin_slide_manager_create(Resource *resource, uint32_t id, wl_resource *surface)
35 {
36     SurfaceInterface *s = SurfaceInterface::get(surface);
37     if (!s) {
38         wl_resource_post_error(resource->handle, 0, "Invalid  surface");
39         return;
40     }
41 
42     wl_resource *slide_resource = wl_resource_create(resource->client(), &org_kde_kwin_slide_interface, resource->version(), id);
43     if (!slide_resource) {
44         wl_client_post_no_memory(resource->client());
45         return;
46     }
47 
48     auto slide = new SlideInterface(slide_resource);
49     SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(s);
50     surfacePrivate->setSlide(QPointer<SlideInterface>(slide));
51 }
52 
org_kde_kwin_slide_manager_unset(Resource * resource,wl_resource * surface)53 void SlideManagerInterfacePrivate::org_kde_kwin_slide_manager_unset(Resource *resource, wl_resource *surface)
54 {
55     SurfaceInterface *s = SurfaceInterface::get(surface);
56     if (!s) {
57         wl_resource_post_error(resource->handle, 0, "Invalid  surface");
58         return;
59     }
60     SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(s);
61     surfacePrivate->setSlide(QPointer<SlideInterface>());
62 }
63 
SlideManagerInterfacePrivate(SlideManagerInterface * _q,Display * display)64 SlideManagerInterfacePrivate::SlideManagerInterfacePrivate(SlideManagerInterface *_q, Display *display)
65     : QtWaylandServer::org_kde_kwin_slide_manager(*display, s_version)
66     , q(_q)
67 {
68 }
69 
SlideManagerInterface(Display * display,QObject * parent)70 SlideManagerInterface::SlideManagerInterface(Display *display, QObject *parent)
71     : QObject(parent)
72     , d(new SlideManagerInterfacePrivate(this, display))
73 {
74 }
75 
~SlideManagerInterface()76 SlideManagerInterface::~SlideManagerInterface()
77 {
78 }
79 
remove()80 void SlideManagerInterface::remove()
81 {
82     d->globalRemove();
83 }
84 
85 class SlideInterfacePrivate : public QtWaylandServer::org_kde_kwin_slide
86 {
87 public:
88     SlideInterfacePrivate(SlideInterface *_q, wl_resource *resource);
89 
90     SlideInterface::Location pendingLocation;
91     SlideInterface::Location currentLocation;
92     uint32_t pendingOffset;
93     uint32_t currentOffset;
94     SlideInterface *q;
95 
96 protected:
97     void org_kde_kwin_slide_commit(Resource *resource) override;
98     void org_kde_kwin_slide_set_location(Resource *resource, uint32_t location) override;
99     void org_kde_kwin_slide_set_offset(Resource *resource, int32_t offset) override;
100     void org_kde_kwin_slide_release(Resource *resource) override;
101     void org_kde_kwin_slide_destroy_resource(Resource *resource) override;
102 };
103 
org_kde_kwin_slide_commit(Resource * resource)104 void SlideInterfacePrivate::org_kde_kwin_slide_commit(Resource *resource)
105 {
106     Q_UNUSED(resource)
107     currentLocation = pendingLocation;
108     currentOffset = pendingOffset;
109 }
110 
org_kde_kwin_slide_set_location(Resource * resource,uint32_t location)111 void SlideInterfacePrivate::org_kde_kwin_slide_set_location(Resource *resource, uint32_t location)
112 {
113     Q_UNUSED(resource)
114     pendingLocation = (SlideInterface::Location)location;
115 }
116 
org_kde_kwin_slide_set_offset(Resource * resource,int32_t offset)117 void SlideInterfacePrivate::org_kde_kwin_slide_set_offset(Resource *resource, int32_t offset)
118 {
119     Q_UNUSED(resource)
120     pendingOffset = offset;
121 }
122 
org_kde_kwin_slide_release(Resource * resource)123 void SlideInterfacePrivate::org_kde_kwin_slide_release(Resource *resource)
124 {
125     wl_resource_destroy(resource->handle);
126 }
127 
org_kde_kwin_slide_destroy_resource(Resource * resource)128 void SlideInterfacePrivate::org_kde_kwin_slide_destroy_resource(Resource *resource)
129 {
130     Q_UNUSED(resource)
131     delete q;
132 }
133 
SlideInterfacePrivate(SlideInterface * _q,wl_resource * resource)134 SlideInterfacePrivate::SlideInterfacePrivate(SlideInterface *_q, wl_resource *resource)
135     : QtWaylandServer::org_kde_kwin_slide(resource)
136     , q(_q)
137 {
138 }
139 
SlideInterface(wl_resource * resource)140 SlideInterface::SlideInterface(wl_resource *resource)
141     : d(new SlideInterfacePrivate(this, resource))
142 {
143 }
144 
145 SlideInterface::~SlideInterface() = default;
146 
location() const147 SlideInterface::Location SlideInterface::location() const
148 {
149     return d->currentLocation;
150 }
151 
offset() const152 qint32 SlideInterface::offset() const
153 {
154     return d->currentOffset;
155 }
156 
157 }
158