1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Robin Burchell <robin.burchell@viroteck.net>
4 ** Copyright (C) 2016 The Qt Company Ltd.
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include "qwaylandabstractdecoration_p.h"
42 
43 #include <private/qobject_p.h>
44 #include "qwaylandwindow_p.h"
45 #include "qwaylandshellsurface_p.h"
46 #include "qwaylandinputdevice_p.h"
47 #include "qwaylandscreen_p.h"
48 
49 #include <QtGui/QImage>
50 
51 QT_BEGIN_NAMESPACE
52 
53 namespace QtWaylandClient {
54 
55 class QWaylandAbstractDecorationPrivate : public QObjectPrivate
56 {
57     Q_DECLARE_PUBLIC(QWaylandAbstractDecoration)
58 
59 public:
60     QWaylandAbstractDecorationPrivate();
61     ~QWaylandAbstractDecorationPrivate() override;
62 
63     QWindow *m_window = nullptr;
64     QWaylandWindow *m_wayland_window = nullptr;
65 
66     bool m_isDirty = true;
67     QImage m_decorationContentImage;
68 
69     Qt::MouseButtons m_mouseButtons = Qt::NoButton;
70 };
71 
QWaylandAbstractDecorationPrivate()72 QWaylandAbstractDecorationPrivate::QWaylandAbstractDecorationPrivate()
73     : m_decorationContentImage(nullptr)
74 {
75 }
76 
~QWaylandAbstractDecorationPrivate()77 QWaylandAbstractDecorationPrivate::~QWaylandAbstractDecorationPrivate()
78 {
79 }
80 
QWaylandAbstractDecoration()81 QWaylandAbstractDecoration::QWaylandAbstractDecoration()
82     : QObject(*new QWaylandAbstractDecorationPrivate)
83 {
84 }
85 
~QWaylandAbstractDecoration()86 QWaylandAbstractDecoration::~QWaylandAbstractDecoration()
87 {
88 }
89 
90 // we do this as a setter to get around plugin factory creates not really
91 // being a great way to pass arguments
setWaylandWindow(QWaylandWindow * window)92 void QWaylandAbstractDecoration::setWaylandWindow(QWaylandWindow *window)
93 {
94     Q_D(QWaylandAbstractDecoration);
95 
96     // double initialization is probably not great
97     Q_ASSERT(!d->m_window && !d->m_wayland_window);
98 
99     d->m_window = window->window();
100     d->m_wayland_window = window;
101 }
102 
103 // Creates regions like this on the outside of a rectangle with inner size \a size
104 // -----
105 // |   |
106 // -----
107 // I.e. the top and bottom extends into the corners
marginsRegion(const QSize & size,const QMargins & margins)108 static QRegion marginsRegion(const QSize &size, const QMargins &margins)
109 {
110     QRegion r;
111 
112     r += QRect(0, 0, size.width(), margins.top()); // top
113     r += QRect(0, size.height()-margins.bottom(), size.width(), margins.bottom()); //bottom
114     r += QRect(0, margins.top(), margins.left(), size.height()); //left
115     r += QRect(size.width()-margins.left(), margins.top(), margins.right(), size.height()-margins.top()); // right
116     return r;
117 }
118 
contentImage()119 const QImage &QWaylandAbstractDecoration::contentImage()
120 {
121     Q_D(QWaylandAbstractDecoration);
122     if (d->m_isDirty) {
123         // Update the decoration backingstore
124 
125         const int bufferScale = waylandWindow()->scale();
126         const QSize imageSize = waylandWindow()->surfaceSize() * bufferScale;
127         d->m_decorationContentImage = QImage(imageSize, QImage::Format_ARGB32_Premultiplied);
128         // Only scale by buffer scale, not QT_SCALE_FACTOR etc.
129         d->m_decorationContentImage.setDevicePixelRatio(bufferScale);
130         d->m_decorationContentImage.fill(Qt::transparent);
131         this->paint(&d->m_decorationContentImage);
132 
133         QRegion damage = marginsRegion(waylandWindow()->surfaceSize(), waylandWindow()->frameMargins());
134         for (QRect r : damage)
135             waylandWindow()->damage(r);
136 
137         d->m_isDirty = false;
138     }
139 
140     return d->m_decorationContentImage;
141 }
142 
update()143 void QWaylandAbstractDecoration::update()
144 {
145     Q_D(QWaylandAbstractDecoration);
146     d->m_isDirty = true;
147 }
148 
setMouseButtons(Qt::MouseButtons mb)149 void QWaylandAbstractDecoration::setMouseButtons(Qt::MouseButtons mb)
150 {
151     Q_D(QWaylandAbstractDecoration);
152     d->m_mouseButtons = mb;
153 }
154 
startResize(QWaylandInputDevice * inputDevice,Qt::Edges edges,Qt::MouseButtons buttons)155 void QWaylandAbstractDecoration::startResize(QWaylandInputDevice *inputDevice, Qt::Edges edges, Qt::MouseButtons buttons)
156 {
157     Q_D(QWaylandAbstractDecoration);
158     if (isLeftClicked(buttons) && d->m_wayland_window->shellSurface()) {
159         d->m_wayland_window->shellSurface()->resize(inputDevice, edges);
160         inputDevice->removeMouseButtonFromState(Qt::LeftButton);
161     }
162 }
163 
startMove(QWaylandInputDevice * inputDevice,Qt::MouseButtons buttons)164 void QWaylandAbstractDecoration::startMove(QWaylandInputDevice *inputDevice, Qt::MouseButtons buttons)
165 {
166     Q_D(QWaylandAbstractDecoration);
167     if (isLeftClicked(buttons) && d->m_wayland_window->shellSurface()) {
168         d->m_wayland_window->shellSurface()->move(inputDevice);
169         inputDevice->removeMouseButtonFromState(Qt::LeftButton);
170     }
171 }
172 
showWindowMenu(QWaylandInputDevice * inputDevice)173 void QWaylandAbstractDecoration::showWindowMenu(QWaylandInputDevice *inputDevice)
174 {
175     Q_D(QWaylandAbstractDecoration);
176     if (auto *s = d->m_wayland_window->shellSurface())
177         s->showWindowMenu(inputDevice);
178 }
179 
isLeftClicked(Qt::MouseButtons newMouseButtonState)180 bool QWaylandAbstractDecoration::isLeftClicked(Qt::MouseButtons newMouseButtonState)
181 {
182     Q_D(QWaylandAbstractDecoration);
183     return !(d->m_mouseButtons & Qt::LeftButton) && (newMouseButtonState & Qt::LeftButton);
184 }
185 
isRightClicked(Qt::MouseButtons newMouseButtonState)186 bool QWaylandAbstractDecoration::isRightClicked(Qt::MouseButtons newMouseButtonState)
187 {
188     Q_D(QWaylandAbstractDecoration);
189     return !(d->m_mouseButtons & Qt::RightButton) && (newMouseButtonState & Qt::RightButton);
190 }
191 
isLeftReleased(Qt::MouseButtons newMouseButtonState)192 bool QWaylandAbstractDecoration::isLeftReleased(Qt::MouseButtons newMouseButtonState)
193 {
194     Q_D(QWaylandAbstractDecoration);
195     return (d->m_mouseButtons & Qt::LeftButton) && !(newMouseButtonState & Qt::LeftButton);
196 }
197 
isDirty() const198 bool QWaylandAbstractDecoration::isDirty() const
199 {
200     Q_D(const QWaylandAbstractDecoration);
201     return d->m_isDirty;
202 }
203 
window() const204 QWindow *QWaylandAbstractDecoration::window() const
205 {
206     Q_D(const QWaylandAbstractDecoration);
207     return d->m_window;
208 }
209 
waylandWindow() const210 QWaylandWindow *QWaylandAbstractDecoration::waylandWindow() const
211 {
212     Q_D(const QWaylandAbstractDecoration);
213     return d->m_wayland_window;
214 }
215 
216 }
217 
218 QT_END_NAMESPACE
219