| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2019 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 "compositor.h" |
| 52 |
#include "window.h" |
| 53 |
|
| 54 |
#include <QtWaylandCompositor/QWaylandOutput> |
| 55 |
#include <QtWaylandCompositor/QWaylandXdgShell> |
| 56 |
#include <QtWaylandCompositor/QWaylandSeat> |
| 57 |
|
| 58 |
#include <QOpenGLFunctions> |
| 59 |
|
| 60 |
QOpenGLTexture *View::getTexture() { |
| 61 |
if (advance()) |
| 62 |
m_texture = currentBuffer().toOpenGLTexture(); |
| 63 |
return m_texture; |
| 64 |
} |
| 65 |
|
| 66 |
void View::setGlobalPosition(const QPoint &position) |
| 67 |
{ |
| 68 |
if (m_globalPosition == position) |
| 69 |
return; |
| 70 |
|
| 71 |
m_globalPosition = position; |
| 72 |
emit globalPositionChanged(); |
| 73 |
} |
| 74 |
|
| 75 |
QPoint View::mapToLocal(const QPoint &globalPosition) const |
| 76 |
{ |
| 77 |
return globalPosition - this->globalPosition(); |
| 78 |
} |
| 79 |
|
| 80 |
void View::updateAnchoredPosition() |
| 81 |
{ |
| 82 |
QPoint offset; |
| 83 |
QSize size = surface()->size(); |
| 84 |
QSize delta = size - m_lastSize; |
| 85 |
if (m_anchorEdges & Qt::RightEdge) |
| 86 |
offset.setX(-delta.width()); |
| 87 |
if (m_anchorEdges & Qt::BottomEdge) |
| 88 |
offset.setY(-delta.height()); |
| 89 |
setGlobalPosition(globalPosition() + offset); |
| 90 |
m_lastSize = size; |
| 91 |
} |
| 92 |
|
| 93 |
void View::handleResizeMove(const QPoint &delta) |
| 94 |
{ |
| 95 |
Q_UNUSED(delta); |
| 96 |
qWarning() << "Resize not implemented for this view"; |
| 97 |
} |
| 98 |
|
| 99 |
ToplevelView::ToplevelView(QWaylandXdgToplevel *toplevel) |
| 100 |
: m_toplevel(toplevel) |
| 101 |
{ |
| 102 |
QWaylandXdgSurface *xdgSurface = toplevel->xdgSurface(); |
| 103 |
setSurface(xdgSurface->surface()); |
| 104 |
connect(toplevel, &QWaylandXdgToplevel::startMove, this, &View::startMove); |
| 105 |
connect(toplevel, &QWaylandXdgToplevel::startResize, this, [this](QWaylandSeat *, Qt::Edges edges) { |
| 106 |
m_resize.edges = edges; |
| 107 |
m_resize.initialSize = m_toplevel->xdgSurface()->windowGeometry().size(); |
| 108 |
Qt::Edges opposite = edges ^ Qt::Edges(0b1111); |
| 109 |
setAnchorEdges(opposite); |
| 110 |
emit startResize(); |
| 111 |
}); |
| 112 |
QVector<QWaylandXdgToplevel::State> states{QWaylandXdgToplevel::ActivatedState}; |
| 113 |
toplevel->sendConfigure(QSize(0, 0), states); |
| 114 |
} |
| 115 |
|
| 116 |
void ToplevelView::handleResizeMove(const QPoint &delta) |
| 117 |
{ |
| 118 |
QSize newSize = m_toplevel->sizeForResize(m_resize.initialSize, delta, m_resize.edges); |
| 119 |
m_toplevel->sendResizing(newSize); |
| 120 |
} |
| 121 |
|
| 122 |
void ToplevelView::handleResizeRelease() |
| 123 |
{ |
| 124 |
setAnchorEdges({}); |
| 125 |
m_resize.edges = {}; |
| 126 |
m_resize.initialSize = {}; |
| 127 |
} |
| 128 |
|
| 129 |
Compositor::~Compositor() |
| 130 |
{ |
| 131 |
delete m_xdgShell; |
| 132 |
} |
| 133 |
|
| 134 |
void Compositor::create() |
| 135 |
{ |
| 136 |
QWaylandCompositor::create(); |
| 137 |
|
| 138 |
m_xdgShell = new QWaylandXdgShell(this); |
| 139 |
connect(m_xdgShell, &QWaylandXdgShell::toplevelCreated, this, &Compositor::handleXdgToplevelCreated); |
| 140 |
} |
| 141 |
|
| 142 |
View *Compositor::viewAt(const QPoint &position) |
| 143 |
{ |
| 144 |
// Since views are stored in painting order (back to front), we have to iterate backwards |
| 145 |
// to find the topmost view at the given point |
| 146 |
for (auto it = m_views.crbegin(); it != m_views.crend(); ++it) { |
| 147 |
View *view = *it; |
| 148 |
if (view->globalGeometry().contains(position)) |
| 149 |
return view; |
| 150 |
} |
| 151 |
return nullptr; |
| 152 |
} |
| 153 |
|
| 154 |
void Compositor::raise(View *view) |
| 155 |
{ |
| 156 |
m_views.removeAll(view); |
| 157 |
m_views << view; |
| 158 |
defaultSeat()->setKeyboardFocus(view->surface()); |
| 159 |
triggerRender(); |
| 160 |
} |
| 161 |
|
| 162 |
void Compositor::handleMousePress(const QPoint &position, Qt::MouseButton button) |
| 163 |
{ |
| 164 |
if (m_grab.state == Grab::None) { |
| 165 |
m_grab.view = viewAt(position); |
| 166 |
if (m_grab.view) { |
| 167 |
m_grab.state = Grab::Input; |
| 168 |
m_grab.startGlobalPosition = position; |
| 169 |
m_grab.startLocalPosition = m_grab.view->mapToLocal(position); |
| 170 |
raise(m_grab.view); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
switch (m_grab.state) { |
| 175 |
case Grab::Input: { |
| 176 |
auto *seat = defaultSeat(); |
| 177 |
seat->sendMouseMoveEvent(m_grab.view, m_grab.view->mapToLocal(position)); |
| 178 |
seat->sendMousePressEvent(button); |
| 179 |
break; |
| 180 |
} |
| 181 |
case Grab::Move: |
| 182 |
case Grab::Resize: |
| 183 |
case Grab::None: |
| 184 |
break; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
void Compositor::handleMouseRelease(const QPoint &position, Qt::MouseButton button, Qt::MouseButtons buttons) |
| 189 |
{ |
| 190 |
auto *seat = defaultSeat(); |
| 191 |
|
| 192 |
switch (m_grab.state) { |
| 193 |
case Grab::Input: |
| 194 |
seat->sendMouseMoveEvent(m_grab.view, m_grab.view->mapToLocal(position)); |
| 195 |
seat->sendMouseReleaseEvent(button); |
| 196 |
if (buttons == Qt::NoButton) { |
| 197 |
View *newView = viewAt(position); |
| 198 |
if (newView != m_grab.view) { |
| 199 |
seat->setMouseFocus(newView); |
| 200 |
if (newView) |
| 201 |
seat->sendMouseMoveEvent(newView, newView->mapToLocal(position)); |
| 202 |
} |
| 203 |
m_grab.view = nullptr; |
| 204 |
m_grab.state = Grab::None; |
| 205 |
} |
| 206 |
break; |
| 207 |
case Grab::Move: |
| 208 |
case Grab::Resize: |
| 209 |
m_grab.state = Grab::None; |
| 210 |
m_grab.view = nullptr; |
| 211 |
if (View *view = viewAt(position)) |
| 212 |
seat->sendMouseMoveEvent(view, view->mapToLocal(position)); |
| 213 |
break; |
| 214 |
case Grab::None: |
| 215 |
if (View *view = viewAt(position)) |
| 216 |
seat->sendMouseMoveEvent(view, view->mapToLocal(position)); |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
void Compositor::handleMouseMove(const QPoint &position) |
| 222 |
{ |
| 223 |
switch (m_grab.state) { |
| 224 |
case Grab::Input: |
| 225 |
defaultSeat()->sendMouseMoveEvent(m_grab.view, m_grab.view->mapToLocal(position)); |
| 226 |
break; |
| 227 |
case Grab::None: |
| 228 |
if (View *view = viewAt(position)) |
| 229 |
defaultSeat()->sendMouseMoveEvent(view, view->mapToLocal(position)); |
| 230 |
break; |
| 231 |
case Grab::Resize: |
| 232 |
m_grab.view->handleResizeMove(position - m_grab.startGlobalPosition); |
| 233 |
break; |
| 234 |
case Grab::Move: |
| 235 |
m_grab.view->setGlobalPosition(position - m_grab.startLocalPosition); |
| 236 |
break; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void Compositor::handleMouseWheel(Qt::Orientation orientation, int delta) |
| 241 |
{ |
| 242 |
defaultSeat()->sendMouseWheelEvent(orientation, delta); |
| 243 |
} |
| 244 |
|
| 245 |
void Compositor::handleKeyPress(quint32 nativeScanCode) |
| 246 |
{ |
| 247 |
defaultSeat()->sendKeyPressEvent(nativeScanCode); |
| 248 |
} |
| 249 |
|
| 250 |
void Compositor::handleKeyRelease(quint32 nativeScanCode) |
| 251 |
{ |
| 252 |
defaultSeat()->sendKeyReleaseEvent(nativeScanCode); |
| 253 |
} |
| 254 |
|
| 255 |
void Compositor::handleXdgToplevelCreated(QWaylandXdgToplevel *toplevel, QWaylandXdgSurface *xdgSurface) |
| 256 |
{ |
| 257 |
Q_UNUSED(xdgSurface); |
| 258 |
auto *view = new ToplevelView(toplevel); |
| 259 |
addView(view); |
| 260 |
} |
| 261 |
|
| 262 |
void Compositor::addView(View *view) |
| 263 |
{ |
| 264 |
view->setOutput(outputFor(m_window)); |
| 265 |
m_views << view; |
| 266 |
connect(view, &QWaylandView::surfaceDestroyed, this, &Compositor::handleViewSurfaceDestroyed); |
| 267 |
connect(view, &View::globalPositionChanged, this, &Compositor::triggerRender); |
| 268 |
connect(view->surface(), &QWaylandSurface::redraw, this, &Compositor::triggerRender); |
| 269 |
connect(view, &View::startMove, this, [this, view](){ |
| 270 |
m_grab.view = view; |
| 271 |
m_grab.state = Grab::Move; |
| 272 |
}); |
| 273 |
connect(view, &View::startResize, this, [this, view]() { |
| 274 |
m_grab.view = view; |
| 275 |
m_grab.state = Grab::Resize; |
| 276 |
}); |
| 277 |
} |
| 278 |
|
| 279 |
void Compositor::handleViewSurfaceDestroyed() |
| 280 |
{ |
| 281 |
auto *view = qobject_cast<ToplevelView*>(sender()); |
| 282 |
m_views.removeAll(view); |
| 283 |
delete view; |
| 284 |
triggerRender(); |
| 285 |
} |
| 286 |
|
| 287 |
void Compositor::triggerRender() |
| 288 |
{ |
| 289 |
m_window->requestUpdate(); |
| 290 |
} |