1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Wayland module
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "window.h"
52
53#include <QMouseEvent>
54#include <QOpenGLWindow>
55#include <QOpenGLTexture>
56#include <QOpenGLFunctions>
57#include <QMatrix4x4>
58
59#include "compositor.h"
60#include <QtWaylandCompositor/qwaylandseat.h>
61
62Window::Window()
63{
64}
65
66void Window::setCompositor(Compositor *comp) {
67 m_compositor = comp;
68 connect(sender: m_compositor, signal: &Compositor::startMove, receiver: this, slot: &Window::startMove);
69 connect(sender: m_compositor, signal: &Compositor::startResize, receiver: this, slot: &Window::startResize);
70 connect(sender: m_compositor, signal: &Compositor::dragStarted, receiver: this, slot: &Window::startDrag);
71}
72
73void Window::initializeGL()
74{
75 QImage backgroundImage = QImage(QLatin1String(":/background.jpg")).rgbSwapped();
76 backgroundImage.invertPixels();
77 m_backgroundTexture = new QOpenGLTexture(backgroundImage, QOpenGLTexture::DontGenerateMipMaps);
78 m_backgroundTexture->setMinificationFilter(QOpenGLTexture::Nearest);
79 m_backgroundImageSize = backgroundImage.size();
80 m_textureBlitter.create();
81 m_compositor->create(); // the compositor's hardware integration may depend on GL
82}
83
84void Window::drawBackground()
85{
86 for (int y = 0; y < height(); y += m_backgroundImageSize.height()) {
87 for (int x = 0; x < width(); x += m_backgroundImageSize.width()) {
88 QMatrix4x4 targetTransform = QOpenGLTextureBlitter::targetTransform(target: QRect(QPoint(x,y), m_backgroundImageSize), viewport: QRect(QPoint(0,0), size()));
89 m_textureBlitter.blit(texture: m_backgroundTexture->textureId(),
90 targetTransform,
91 sourceOrigin: QOpenGLTextureBlitter::OriginTopLeft);
92 }
93 }
94}
95
96QPointF Window::getAnchorPosition(const QPointF &position, int resizeEdge, const QSize &windowSize)
97{
98 float y = position.y();
99 if (resizeEdge & QWaylandXdgSurfaceV5::ResizeEdge::TopEdge)
100 y += windowSize.height();
101
102 float x = position.x();
103 if (resizeEdge & QWaylandXdgSurfaceV5::ResizeEdge::LeftEdge)
104 x += windowSize.width();
105
106 return QPointF(x, y);
107}
108
109QPointF Window::getAnchoredPosition(const QPointF &anchorPosition, int resizeEdge, const QSize &windowSize)
110{
111 return anchorPosition - getAnchorPosition(position: QPointF(), resizeEdge, windowSize);
112}
113
114void Window::paintGL()
115{
116 m_compositor->startRender();
117 QOpenGLFunctions *functions = context()->functions();
118 functions->glClearColor(red: 1.f, green: .6f, blue: .0f, alpha: 0.5f);
119 functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
120
121 m_textureBlitter.bind();
122 drawBackground();
123
124 functions->glEnable(GL_BLEND);
125 functions->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
126
127 GLenum currentTarget = GL_TEXTURE_2D;
128 const auto views = m_compositor->views();
129 for (View *view : views) {
130 if (view->isCursor())
131 continue;
132 auto texture = view->getTexture();
133 if (!texture)
134 continue;
135 if (texture->target() != currentTarget) {
136 currentTarget = texture->target();
137 m_textureBlitter.bind(target: currentTarget);
138 }
139 QWaylandSurface *surface = view->surface();
140 if ((surface && surface->hasContent()) || view->isBufferLocked()) {
141 QSize s = view->size();
142 if (!s.isEmpty()) {
143 if (m_mouseView == view && m_grabState == ResizeGrab && m_resizeAnchored)
144 view->setPosition(getAnchoredPosition(anchorPosition: m_resizeAnchorPosition, resizeEdge: m_resizeEdge, windowSize: s));
145 QPointF pos = view->position() + view->parentPosition();
146 QRectF surfaceGeometry(pos, s);
147 auto surfaceOrigin = view->textureOrigin();
148 auto sf = view->animationFactor();
149 QRectF targetRect(surfaceGeometry.topLeft() * sf, surfaceGeometry.size() * sf);
150 QMatrix4x4 targetTransform = QOpenGLTextureBlitter::targetTransform(target: targetRect, viewport: QRect(QPoint(), size()));
151 m_textureBlitter.blit(texture: texture->textureId(), targetTransform, sourceOrigin: surfaceOrigin);
152 }
153 }
154 }
155 functions->glDisable(GL_BLEND);
156
157 m_textureBlitter.release();
158 m_compositor->endRender();
159}
160
161View *Window::viewAt(const QPointF &point)
162{
163 View *ret = nullptr;
164 const auto views = m_compositor->views();
165 for (View *view : views) {
166 if (view == m_dragIconView)
167 continue;
168 QRectF geom(view->position(), view->size());
169 if (geom.contains(p: point))
170 ret = view;
171 }
172 return ret;
173}
174
175void Window::startMove()
176{
177 m_grabState = MoveGrab;
178}
179
180void Window::startResize(int edge, bool anchored)
181{
182 m_initialSize = m_mouseView->windowSize();
183 m_grabState = ResizeGrab;
184 m_resizeEdge = edge;
185 m_resizeAnchored = anchored;
186 m_resizeAnchorPosition = getAnchorPosition(position: m_mouseView->position(), resizeEdge: edge, windowSize: m_mouseView->surface()->destinationSize());
187}
188
189void Window::startDrag(View *dragIcon)
190{
191 m_grabState = DragGrab;
192 m_dragIconView = dragIcon;
193 m_compositor->raise(view: dragIcon);
194}
195
196void Window::mousePressEvent(QMouseEvent *e)
197{
198 if (mouseGrab())
199 return;
200 if (m_mouseView.isNull()) {
201 m_mouseView = viewAt(point: e->localPos());
202 if (!m_mouseView) {
203 m_compositor->closePopups();
204 return;
205 }
206 if (e->modifiers() == Qt::AltModifier || e->modifiers() == Qt::MetaModifier)
207 m_grabState = MoveGrab; //start move
208 else
209 m_compositor->raise(view: m_mouseView);
210 m_initialMousePos = e->localPos();
211 m_mouseOffset = e->localPos() - m_mouseView->position();
212
213 QMouseEvent moveEvent(QEvent::MouseMove, e->localPos(), e->globalPos(), Qt::NoButton, Qt::NoButton, e->modifiers());
214 sendMouseEvent(e: &moveEvent, target: m_mouseView);
215 }
216 sendMouseEvent(e, target: m_mouseView);
217}
218
219void Window::mouseReleaseEvent(QMouseEvent *e)
220{
221 if (!mouseGrab())
222 sendMouseEvent(e, target: m_mouseView);
223 if (e->buttons() == Qt::NoButton) {
224 if (m_grabState == DragGrab) {
225 View *view = viewAt(point: e->localPos());
226 m_compositor->handleDrag(target: view, me: e);
227 }
228 m_mouseView = nullptr;
229 m_grabState = NoGrab;
230 }
231}
232
233void Window::mouseMoveEvent(QMouseEvent *e)
234{
235 switch (m_grabState) {
236 case NoGrab: {
237 View *view = m_mouseView ? m_mouseView.data() : viewAt(point: e->localPos());
238 sendMouseEvent(e, target: view);
239 if (!view)
240 setCursor(Qt::ArrowCursor);
241 }
242 break;
243 case MoveGrab: {
244 m_mouseView->setPosition(e->localPos() - m_mouseOffset);
245 update();
246 }
247 break;
248 case ResizeGrab: {
249 QPoint delta = (e->localPos() - m_initialMousePos).toPoint();
250 m_compositor->handleResize(target: m_mouseView, initialSize: m_initialSize, delta, edge: m_resizeEdge);
251 }
252 break;
253 case DragGrab: {
254 View *view = viewAt(point: e->localPos());
255 m_compositor->handleDrag(target: view, me: e);
256 if (m_dragIconView) {
257 m_dragIconView->setPosition(e->localPos() + m_dragIconView->offset());
258 update();
259 }
260 }
261 break;
262 }
263}
264
265void Window::sendMouseEvent(QMouseEvent *e, View *target)
266{
267 QPointF mappedPos = e->localPos();
268 if (target)
269 mappedPos -= target->position();
270 QMouseEvent viewEvent(e->type(), mappedPos, e->localPos(), e->button(), e->buttons(), e->modifiers());
271 m_compositor->handleMouseEvent(target, me: &viewEvent);
272}
273
274void Window::keyPressEvent(QKeyEvent *e)
275{
276 m_compositor->defaultSeat()->sendKeyPressEvent(code: e->nativeScanCode());
277}
278
279void Window::keyReleaseEvent(QKeyEvent *e)
280{
281 m_compositor->defaultSeat()->sendKeyReleaseEvent(code: e->nativeScanCode());
282}
283

source code of qtwayland/examples/wayland/qwindow-compositor/window.cpp