KWaylandServer

viewporter_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #include "viewporter_interface.h"
8 #include "display.h"
9 #include "surface_interface_p.h"
10 #include "viewporter_interface_p.h"
11 
12 static const int s_version = 1;
13 
14 namespace KWaylandServer
15 {
16 class ViewporterInterfacePrivate : public QtWaylandServer::wp_viewporter
17 {
18 protected:
19  void wp_viewporter_destroy(Resource *resource) override;
20  void wp_viewporter_get_viewport(Resource *resource, uint32_t id, struct ::wl_resource *surface) override;
21 };
22 
23 void ViewporterInterfacePrivate::wp_viewporter_destroy(Resource *resource)
24 {
25  wl_resource_destroy(resource->handle);
26 }
27 
28 void ViewporterInterfacePrivate::wp_viewporter_get_viewport(Resource *resource, uint32_t id, struct ::wl_resource *surface_resource)
29 {
30  SurfaceInterface *surface = SurfaceInterface::get(surface_resource);
31  ViewportInterface *viewport = ViewportInterface::get(surface);
32 
33  if (viewport) {
34  wl_resource_post_error(resource->handle, error_viewport_exists, "the specified surface already has a viewport");
35  return;
36  }
37 
38  wl_resource *viewportResource = wl_resource_create(resource->client(), &wp_viewport_interface, resource->version(), id);
39 
40  new ViewportInterface(surface, viewportResource);
41 }
42 
43 ViewportInterface::ViewportInterface(SurfaceInterface *surface, wl_resource *resource)
44  : QtWaylandServer::wp_viewport(resource)
45  , surface(surface)
46 {
47  SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(surface);
48  surfacePrivate->viewportExtension = this;
49 }
50 
51 ViewportInterface::~ViewportInterface()
52 {
53  if (surface) {
54  SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(surface);
55  surfacePrivate->viewportExtension = nullptr;
56  }
57 }
58 
59 ViewportInterface *ViewportInterface::get(SurfaceInterface *surface)
60 {
61  return SurfaceInterfacePrivate::get(surface)->viewportExtension;
62 }
63 
64 void ViewportInterface::wp_viewport_destroy_resource(Resource *resource)
65 {
66  Q_UNUSED(resource)
67  delete this;
68 }
69 
70 void ViewportInterface::wp_viewport_destroy(Resource *resource)
71 {
72  if (surface) {
73  SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(surface);
74  surfacePrivate->pending.viewport.sourceGeometry = QRectF();
75  surfacePrivate->pending.viewport.sourceGeometryIsSet = true;
76  surfacePrivate->pending.viewport.destinationSize = QSize();
77  surfacePrivate->pending.viewport.destinationSizeIsSet = true;
78  }
79 
80  wl_resource_destroy(resource->handle);
81 }
82 
83 void ViewportInterface::wp_viewport_set_source(Resource *resource, wl_fixed_t x_fixed, wl_fixed_t y_fixed, wl_fixed_t width_fixed, wl_fixed_t height_fixed)
84 {
85  if (!surface) {
86  wl_resource_post_error(resource->handle, error_no_surface, "the wl_surface for this viewport no longer exists");
87  return;
88  }
89 
90  const qreal x = wl_fixed_to_double(x_fixed);
91  const qreal y = wl_fixed_to_double(y_fixed);
92  const qreal width = wl_fixed_to_double(width_fixed);
93  const qreal height = wl_fixed_to_double(height_fixed);
94 
95  if (x == -1 && y == -1 && width == -1 && height == -1) {
96  SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(surface);
97  surfacePrivate->pending.viewport.sourceGeometry = QRectF();
98  surfacePrivate->pending.viewport.sourceGeometryIsSet = true;
99  return;
100  }
101 
102  if (x < 0 || y < 0 || width <= 0 || height <= 0) {
103  wl_resource_post_error(resource->handle, error_bad_value, "invalid source geometry");
104  return;
105  }
106 
107  SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(surface);
108  surfacePrivate->pending.viewport.sourceGeometry = QRectF(x, y, width, height);
109  surfacePrivate->pending.viewport.sourceGeometryIsSet = true;
110 }
111 
112 void ViewportInterface::wp_viewport_set_destination(Resource *resource, int32_t width, int32_t height)
113 {
114  if (!surface) {
115  wl_resource_post_error(resource->handle, error_no_surface, "the wl_surface for this viewport no longer exists");
116  return;
117  }
118 
119  if (width == -1 && height == -1) {
120  SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(surface);
121  surfacePrivate->pending.viewport.destinationSize = QSize();
122  surfacePrivate->pending.viewport.destinationSizeIsSet = true;
123  return;
124  }
125 
126  if (width <= 0 || height <= 0) {
127  wl_resource_post_error(resource->handle, error_bad_value, "invalid destination size");
128  return;
129  }
130 
131  SurfaceInterfacePrivate *surfacePrivate = SurfaceInterfacePrivate::get(surface);
132  surfacePrivate->pending.viewport.destinationSize = QSize(width, height);
133  surfacePrivate->pending.viewport.destinationSizeIsSet = true;
134 }
135 
136 ViewporterInterface::ViewporterInterface(Display *display, QObject *parent)
137  : QObject(parent)
138  , d(new ViewporterInterfacePrivate)
139 {
140  d->init(*display, s_version);
141 }
142 
143 ViewporterInterface::~ViewporterInterface()
144 {
145 }
146 
147 } // namespace KWaylandServer
static SurfaceInterface * get(wl_resource *native)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:08:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.