summaryrefslogtreecommitdiffstats
path: root/tests/manual/wip-cpp-compositor/compositor.cpp
blob: 2f702511525959ea444e548dcccb590a0b607d95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "compositor.h"
#include "window.h"

#include <QtWaylandCompositor/QWaylandOutput>
#include <QtWaylandCompositor/QWaylandXdgShell>
#include <QtWaylandCompositor/QWaylandSeat>

#include <QOpenGLFunctions>

QOpenGLTexture *View::getTexture() {
    if (advance())
        m_texture = currentBuffer().toOpenGLTexture();
    return m_texture;
}

void View::setGlobalPosition(const QPoint &position)
{
    if (m_globalPosition == position)
        return;

    m_globalPosition = position;
    emit globalPositionChanged();
}

QPoint View::mapToLocal(const QPoint &globalPosition) const
{
    return globalPosition - this->globalPosition();
}

void View::updateAnchoredPosition()
{
    QPoint offset;
    QSize size = surface()->bufferSize();
    QSize delta = size - m_lastSize;
    if (m_anchorEdges & Qt::RightEdge)
        offset.setX(-delta.width());
    if (m_anchorEdges & Qt::BottomEdge)
        offset.setY(-delta.height());
    setGlobalPosition(globalPosition() + offset);
    m_lastSize = size;
}

void View::handleResizeMove(const QPoint &delta)
{
    Q_UNUSED(delta);
    qWarning() << "Resize not implemented for this view";
}

ToplevelView::ToplevelView(QWaylandXdgToplevel *toplevel)
    : m_toplevel(toplevel)
{
    QWaylandXdgSurface *xdgSurface = toplevel->xdgSurface();
    setSurface(xdgSurface->surface());
    connect(toplevel, &QWaylandXdgToplevel::startMove, this, &View::startMove);
    connect(toplevel, &QWaylandXdgToplevel::startResize, this, [this](QWaylandSeat *, Qt::Edges edges) {
        m_resize.edges = edges;
        m_resize.initialSize = m_toplevel->xdgSurface()->windowGeometry().size();
        Qt::Edges opposite = edges ^ Qt::Edges(0b1111);
        setAnchorEdges(opposite);
        emit startResize();
    });
    QList<QWaylandXdgToplevel::State> states{QWaylandXdgToplevel::ActivatedState};
    toplevel->sendConfigure(QSize(0, 0), states);
}

void ToplevelView::handleResizeMove(const QPoint &delta)
{
    QSize newSize = m_toplevel->sizeForResize(m_resize.initialSize, delta, m_resize.edges);
    m_toplevel->sendResizing(newSize);
}

void ToplevelView::handleResizeRelease()
{
    setAnchorEdges({});
    m_resize.edges = {};
    m_resize.initialSize = {};
}

Compositor::~Compositor()
{
    delete m_xdgShell;
}

void Compositor::create()
{
    QWaylandCompositor::create();

    m_xdgShell = new QWaylandXdgShell(this);
    connect(m_xdgShell, &QWaylandXdgShell::toplevelCreated, this, &Compositor::handleXdgToplevelCreated);
}

View *Compositor::viewAt(const QPoint &position)
{
    // Since views are stored in painting order (back to front), we have to iterate backwards
    // to find the topmost view at the given point
    for (auto it = m_views.crbegin(); it != m_views.crend(); ++it) {
        View *view = *it;
        if (view->globalGeometry().contains(position))
            return view;
    }
    return nullptr;
}

void Compositor::raise(View *view)
{
    m_views.removeAll(view);
    m_views << view;
    defaultSeat()->setKeyboardFocus(view->surface());
    triggerRender();
}

void Compositor::handleMousePress(const QPoint &position, Qt::MouseButton button)
{
    if (m_grab.state == Grab::None) {
        m_grab.view = viewAt(position);
        if (m_grab.view) {
            m_grab.state = Grab::Input;
            m_grab.startGlobalPosition = position;
            m_grab.startLocalPosition = m_grab.view->mapToLocal(position);
            raise(m_grab.view);
        }
    }

    switch (m_grab.state) {
    case Grab::Input: {
        auto *seat = defaultSeat();
        seat->sendMouseMoveEvent(m_grab.view, m_grab.view->mapToLocal(position));
        seat->sendMousePressEvent(button);
        break;
    }
    case Grab::Move:
    case Grab::Resize:
    case Grab::None:
        break;
    }
}

void Compositor::handleMouseRelease(const QPoint &position, Qt::MouseButton button, Qt::MouseButtons buttons)
{
    auto *seat = defaultSeat();

    switch (m_grab.state) {
    case Grab::Input:
        seat->sendMouseMoveEvent(m_grab.view, m_grab.view->mapToLocal(position));
        seat->sendMouseReleaseEvent(button);
        if (buttons == Qt::NoButton) {
            View *newView = viewAt(position);
            if (newView != m_grab.view) {
                seat->setMouseFocus(newView);
                if (newView)
                    seat->sendMouseMoveEvent(newView, newView->mapToLocal(position));
            }
            m_grab.view = nullptr;
            m_grab.state = Grab::None;
        }
        break;
    case Grab::Move:
    case Grab::Resize:
        m_grab.state = Grab::None;
        m_grab.view = nullptr;
        if (View *view = viewAt(position))
            seat->sendMouseMoveEvent(view, view->mapToLocal(position));
        break;
    case Grab::None:
        if (View *view = viewAt(position))
            seat->sendMouseMoveEvent(view, view->mapToLocal(position));
        break;
    }
}

void Compositor::handleMouseMove(const QPoint &position)
{
    switch (m_grab.state) {
    case Grab::Input:
        defaultSeat()->sendMouseMoveEvent(m_grab.view, m_grab.view->mapToLocal(position));
        break;
    case Grab::None:
        if (View *view = viewAt(position))
            defaultSeat()->sendMouseMoveEvent(view, view->mapToLocal(position));
        break;
    case Grab::Resize:
        m_grab.view->handleResizeMove(position - m_grab.startGlobalPosition);
        break;
    case Grab::Move:
        m_grab.view->setGlobalPosition(position - m_grab.startLocalPosition);
        break;
    }
}

void Compositor::handleMouseWheel(Qt::Orientation orientation, int delta)
{
    defaultSeat()->sendMouseWheelEvent(orientation, delta);
}

void Compositor::handleKeyPress(quint32 nativeScanCode)
{
    defaultSeat()->sendKeyPressEvent(nativeScanCode);
}

void Compositor::handleKeyRelease(quint32 nativeScanCode)
{
    defaultSeat()->sendKeyReleaseEvent(nativeScanCode);
}

void Compositor::handleXdgToplevelCreated(QWaylandXdgToplevel *toplevel, QWaylandXdgSurface *xdgSurface)
{
    Q_UNUSED(xdgSurface);
    auto *view = new ToplevelView(toplevel);
    addView(view);
}

void Compositor::addView(View *view)
{
    view->setOutput(outputFor(m_window));
    m_views << view;
    connect(view, &QWaylandView::surfaceDestroyed, this, &Compositor::handleViewSurfaceDestroyed);
    connect(view, &View::globalPositionChanged, this, &Compositor::triggerRender);
    connect(view->surface(), &QWaylandSurface::redraw, this, &Compositor::triggerRender);
    connect(view, &View::startMove, this, [this, view](){
        m_grab.view = view;
        m_grab.state = Grab::Move;
    });
    connect(view, &View::startResize, this, [this, view]() {
        m_grab.view = view;
        m_grab.state = Grab::Resize;
    });
}

void Compositor::handleViewSurfaceDestroyed()
{
    auto *view = qobject_cast<ToplevelView*>(sender());
    m_views.removeAll(view);
    delete view;
    triggerRender();
}

void Compositor::triggerRender()
{
    m_window->requestUpdate();
}