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 #ifndef WINDOWCOMPOSITOR_H
52 #define WINDOWCOMPOSITOR_H
53 
54 #include <QtWaylandCompositor/QWaylandCompositor>
55 #include <QtWaylandCompositor/QWaylandSurface>
56 #include <QtWaylandCompositor/QWaylandView>
57 #include <QtWaylandCompositor/QWaylandWlShellSurface>
58 #include <QtWaylandCompositor/QWaylandXdgSurfaceV5>
59 #include <QTimer>
60 #include <QOpenGLTextureBlitter>
61 
62 QT_BEGIN_NAMESPACE
63 
64 class QWaylandWlShell;
65 class QWaylandWlShellSurface;
66 class QWaylandXdgShellV5;
67 class QOpenGLTexture;
68 class Compositor;
69 
70 class View : public QWaylandView
71 {
72     Q_OBJECT
73 public:
74     View(Compositor *compositor);
75     QOpenGLTexture *getTexture();
76     QOpenGLTextureBlitter::Origin textureOrigin() const;
position()77     QPointF position() const { return m_position; }
setPosition(const QPointF & pos)78     void setPosition(const QPointF &pos) { m_position = pos; }
79     QSize size() const;
80     bool isCursor() const;
hasShell()81     bool hasShell() const { return m_wlShellSurface; }
setParentView(View * parent)82     void setParentView(View *parent) { m_parentView = parent; }
parentView()83     View *parentView() const { return m_parentView; }
parentPosition()84     QPointF parentPosition() const { return m_parentView ? (m_parentView->position() + m_parentView->parentPosition()) : QPointF(); }
windowSize()85     QSize windowSize() { return m_xdgSurface ? m_xdgSurface->windowGeometry().size() : surface() ? surface()->destinationSize() : m_size; }
offset()86     QPoint offset() const { return m_offset; }
87 
animationFactor()88     qreal animationFactor() const {return m_animationFactor; }
setAnimationFactor(qreal f)89     void setAnimationFactor(qreal f) {m_animationFactor = f; }
90 
91 signals:
92     void animationDone();
93 
94 protected:
95     void timerEvent(QTimerEvent *event) override;
96 
97 private:
98     friend class Compositor;
99     Compositor *m_compositor = nullptr;
100     GLenum m_textureTarget = GL_TEXTURE_2D;
101     QOpenGLTexture *m_texture = nullptr;
102     QOpenGLTextureBlitter::Origin m_origin;
103     QPointF m_position;
104     QSize m_size;
105     QWaylandWlShellSurface *m_wlShellSurface = nullptr;
106     QWaylandXdgSurfaceV5 *m_xdgSurface = nullptr;
107     QWaylandXdgPopupV5 *m_xdgPopup = nullptr;
108     View *m_parentView = nullptr;
109     QPoint m_offset;
110     qreal m_animationFactor = 1.0;
111     QBasicTimer m_animationTimer;
112     bool m_animationCountUp;
113 
114 public slots:
115     void onXdgSetMaximized();
116     void onXdgUnsetMaximized();
117     void onXdgSetFullscreen(QWaylandOutput *output);
118     void onXdgUnsetFullscreen();
119     void onOffsetForNextFrame(const QPoint &offset);
120 
121     void startAnimation(bool countUp);
122     void cancelAnimation();
123 };
124 
125 class Compositor : public QWaylandCompositor
126 {
127     Q_OBJECT
128 public:
129     Compositor(QWindow *window);
130     ~Compositor() override;
131     void create() override;
132 
133     void startRender();
134     void endRender();
135 
views()136     QList<View*> views() const { return m_views; }
137     void raise(View *view);
138 
139     void handleMouseEvent(QWaylandView *target, QMouseEvent *me);
140     void handleResize(View *target, const QSize &initialSize, const QPoint &delta, int edge);
141     void handleDrag(View *target, QMouseEvent *me);
142 
143     QWaylandClient *popupClient() const;
144     void closePopups();
145 protected:
146     void adjustCursorSurface(QWaylandSurface *surface, int hotspotX, int hotspotY);
147 
148 signals:
149     void startMove();
150     void startResize(int edge, bool anchored);
151     void dragStarted(View *dragIcon);
152     void frameOffset(const QPoint &offset);
153 
154 public slots:
155     void triggerRender();
156 
157 private slots:
158     void surfaceHasContentChanged();
159     void surfaceDestroyed();
160     void viewSurfaceDestroyed();
161     void onStartMove();
162     void onWlStartResize(QWaylandSeat *seat, QWaylandWlShellSurface::ResizeEdge edges);
163     void onXdgStartResize(QWaylandSeat *seat, QWaylandXdgSurfaceV5::ResizeEdge edges);
164 
165     void startDrag();
166 
167 
168     void onSurfaceCreated(QWaylandSurface *surface);
169     void onWlShellSurfaceCreated(QWaylandWlShellSurface *wlShellSurface);
170     void onXdgSurfaceCreated(QWaylandXdgSurfaceV5 *xdgSurface);
171     void onXdgPopupRequested(QWaylandSurface *surface, QWaylandSurface *parent, QWaylandSeat *seat,
172                              const QPoint &position, const QWaylandResource &resource);
173     void onSetTransient(QWaylandSurface *parentSurface, const QPoint &relativeToParent, bool inactive);
174     void onSetPopup(QWaylandSeat *seat, QWaylandSurface *parent, const QPoint &relativeToParent);
175 
176     void onSubsurfaceChanged(QWaylandSurface *child, QWaylandSurface *parent);
177     void onSubsurfacePositionChanged(const QPoint &position);
178 
179     void updateCursor();
180     void viewAnimationDone();
181 private:
182     View *findView(const QWaylandSurface *s) const;
183     QWindow *m_window = nullptr;
184     QList<View*> m_views;
185     QWaylandWlShell *m_wlShell = nullptr;
186     QWaylandXdgShellV5 *m_xdgShell = nullptr;
187     QWaylandView m_cursorView;
188     int m_cursorHotspotX;
189     int m_cursorHotspotY;
190 };
191 
192 
193 QT_END_NAMESPACE
194 
195 #endif // WINDOWCOMPOSITOR_H
196