summaryrefslogtreecommitdiffstats
path: root/examples/qwidget-compositor/main.cpp
blob: 2217d10ec2f51949139be7f8e8f7196c322a042c (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/****************************************************************************
**
** This file is part of QtCompositor**
**
** Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
**
** Contact:  Nokia Corporation qt-info@nokia.com
**
** You may use this file under the terms of the BSD license as follows:
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
**
** Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
****************************************************************************/

#include "waylandcompositor.h"

#include "waylandsurface.h"

#include <QApplication>
#include <QWidget>
#include <QTimer>
#include <QPainter>
#include <QMouseEvent>

#ifdef QT_COMPOSITOR_WAYLAND_GL
#include <QGLContext>
#include <QGLWidget>
#endif

#include <QDebug>

//#include "qtouchscreen.h"
//
//static int touch_x_min, touch_x_max, touch_y_min, touch_y_max;
//
//class QWidgetCompositor;
//
//class TouchObserver : public QTouchScreenObserver
//{
//public:
//    TouchObserver(QWidgetCompositor *compositor)
//        : m_compositor(compositor) { }
//    void touch_configure(int x_min, int x_max, int y_min, int y_max);
//    void touch_point(QEvent::Type state, const QList<QWindowSystemInterface::TouchPoint> &points);
//
//private:
//    QWidgetCompositor *m_compositor;
//};

#ifdef QT_COMPOSITOR_WAYLAND_GL
class QWidgetCompositor : public QGLWidget, public WaylandCompositor
#else
class QWidgetCompositor : public QWidget, public WaylandCompositor
#endif
{
    Q_OBJECT
public:
    QWidgetCompositor() : WaylandCompositor(windowHandle(),const_cast<QGLContext *>(context())), m_moveSurface(0), m_dragSourceSurface(0) {
        setMouseTracking(true);
        setRetainedSelectionEnabled(true);
        m_background = QImage(QLatin1String("background.jpg"));
        //make sure we get the window id and create the glcontext
        //so that clients can successfully initialize egl
        winId();
#ifdef QT_COMPOSITOR_WAYLAND_GL
        if (windowHandle()) {
//            windowHandle()->surfaceHandle();
        }
#endif
    }

private slots:
    void surfaceDestroyed(QObject *object) {
        WaylandSurface *surface = static_cast<WaylandSurface *>(object);
        m_surfaces.removeAll(surface);
        update();
    }

    void surfaceMapped(const QSize &size) {
        WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
        QPoint pos;
        if (!m_surfaces.contains(surface)) {
            uint px = 1 + (qrand() % (width() - size.width() - 2));
            uint py = 1 + (qrand() % (height() - size.height() - 2));
            pos = QPoint(px, py);
            surface->setGeometry(QRect(pos, size));
            m_surfaces.append(surface);
        } else {
            surface->setGeometry(QRect(geometry().topLeft(),size));
        }
        setInputFocus(surface);
        update();
    }

    void surfaceDamaged(const QRect &rect) {
        WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
        surfaceDamaged(surface, rect);
    }

protected:
    void surfaceDamaged(WaylandSurface *surface, const QRect &rect)
    {
#ifdef QT_COMPOSITOR_WAYLAND_GL
        Q_UNUSED(surface);
        Q_UNUSED(rect);
        update();
#else
        update(rect.translated(surface->geometry().topLeft()));
#endif
    }

    void surfaceCreated(WaylandSurface *surface) {
        connect(surface, SIGNAL(destroyed(QObject *)), this, SLOT(surfaceDestroyed(QObject *)));
        connect(surface, SIGNAL(mapped(const QSize &)), this, SLOT(surfaceMapped(const QSize &)));
        connect(surface, SIGNAL(damaged(const QRect &)), this, SLOT(surfaceDamaged(const QRect &)));
        update();
    }

    void paintEvent(QPaintEvent *) {
        QPainter p(this);

        if (!m_background.isNull())
            p.drawPixmap(rect(), m_backgroundScaled);

        for (int i = 0; i < m_surfaces.size(); ++i) {
            if (m_surfaces.at(i)->type() == WaylandSurface::Texture) {
#ifdef QT_COMPOSITOR_WAYLAND_GL
                drawTexture(m_surfaces.at(i)->geometry(), m_surfaces.at(i)->texture());
                break;
#endif //QT_COMPOSITOR_WAYLAND_GL
            } else if (m_surfaces.at(i)->type() == WaylandSurface::Shm) {
                QImage img = m_surfaces.at(i)->image();
                p.drawImage(m_surfaces.at(i)->geometry(), img);
            }
        }

        if (!m_cursor.isNull())
            p.drawImage(m_cursorPos - m_cursorHotspot, m_cursor);

        frameFinished();

#ifdef QT_COMPOSITOR_WAYLAND_GL
        //jl:FIX FIX FIX:)
//        update();
        glFinish();
#endif
    }

    void resizeEvent(QResizeEvent *)
    {
        if (!m_background.isNull()) {
            m_backgroundScaled = QPixmap::fromImage(m_background.scaled(size(),
                Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
        }
    }

    void raise(WaylandSurface *surface) {
        setInputFocus(surface);
        surfaceDamaged(surface, QRect(QPoint(), surface->geometry().size()));
        m_surfaces.removeOne(surface);
        m_surfaces.append(surface);
    }

    void mousePressEvent(QMouseEvent *e) {
        m_cursorPos = e->pos();
        if (!m_cursor.isNull())
            update();
        QPoint local;
        if (WaylandSurface *surface = surfaceAt(e->pos(), &local)) {
            raise(surface);
            if (e->modifiers() & Qt::ControlModifier) {
                m_moveSurface = surface;
                m_moveOffset = local;
            } else {
                surface->sendMousePressEvent(local, e->button());
            }
        }
    }

    void mouseMoveEvent(QMouseEvent *e) {
        m_cursorPos = e->pos();
        if (!m_cursor.isNull())
            update();
        if (isDragging()) {
            QPoint global = e->pos(); // "global" here means the window of the compositor
            QPoint local;
            WaylandSurface *surface = surfaceAt(e->pos(), &local);
            if (surface) {
                if (!m_dragSourceSurface)
                    m_dragSourceSurface = surface;
                if (m_dragSourceSurface == surface)
                    m_lastDragSourcePos = local;
                raise(surface);
            }
            sendDragMoveEvent(global, local, surface);
            return;
        }
        if (m_moveSurface) {
            QRect geometry = m_moveSurface->geometry();
            geometry.moveTo(e->pos() - m_moveOffset);
            m_moveSurface->setGeometry(geometry);
            update();
            return;
        }
        QPoint local;
        if (WaylandSurface *surface = surfaceAt(e->pos(), &local))
            surface->sendMouseMoveEvent(local);
    }

    void mouseReleaseEvent(QMouseEvent *e) {
        if (isDragging()) {
            sendDragEndEvent();
            if (m_dragSourceSurface) {
                // Must send a release event to the source too, no matter where the cursor is now.
                m_dragSourceSurface->sendMouseReleaseEvent(m_lastDragSourcePos, e->button());
                m_dragSourceSurface = 0;
            }
        }
        if (m_moveSurface) {
            m_moveSurface = 0;
            return;
        }
        QPoint local;
        if (WaylandSurface *surface = surfaceAt(e->pos(), &local))
            surface->sendMouseReleaseEvent(local, e->button());
    }

    void keyPressEvent(QKeyEvent *event)
    {
        if (m_surfaces.isEmpty())
            return;
        m_surfaces.last()->sendKeyPressEvent(event->nativeScanCode());
    }

    void keyReleaseEvent(QKeyEvent *event)
    {
        if (m_surfaces.isEmpty())
            return;
        m_surfaces.last()->sendKeyReleaseEvent(event->nativeScanCode());
    }

    WaylandSurface *surfaceAt(const QPoint &point, QPoint *local = 0) {
        for (int i = m_surfaces.size() - 1; i >= 0; --i) {
            if (m_surfaces.at(i)->geometry().contains(point)) {
                if (local)
                    *local = point - m_surfaces.at(i)->geometry().topLeft();
                return m_surfaces.at(i);
            }
        }
        return 0;
    }

    void changeCursor(const QImage &image, int hotspotX, int hotspotY) {
        m_cursor = image;
        m_cursorHotspot = QPoint(hotspotX, hotspotY);
        update();
    }

private:
    QImage m_background;
    QPixmap m_backgroundScaled;

    QList<WaylandSurface *> m_surfaces;

    WaylandSurface *m_moveSurface;
    QPoint m_moveOffset;
    WaylandSurface *m_dragSourceSurface;
    QPoint m_lastDragSourcePos;

    QImage m_cursor;
    QPoint m_cursorPos;
    QPoint m_cursorHotspot;

    friend class TouchObserver;
};

//void TouchObserver::touch_configure(int x_min, int x_max, int y_min, int y_max)
//{
//    touch_x_min = x_min;
//    touch_x_max = x_max;
//    touch_y_min = y_min;
//    touch_y_max = y_max;
//}

//void TouchObserver::touch_point(QEvent::Type state, const QList<QWindowSystemInterface::TouchPoint> &points)
//{
//    Q_UNUSED(state);
//    WaylandSurface *focusSurface = m_compositor->inputFocus();
//    if (focusSurface) {
//        if (points.isEmpty())
//            return;
//        for (int i = 0; i < points.count(); ++i) {
//            const QWindowSystemInterface::TouchPoint &point(points.at(i));

//            // These are hw coordinates.
//            int x = int(point.area.left());
//            int y = int(point.area.top());

//            // Wayland expects surface-relative coordinates.

//            // Translate so that (0, 0) is the top-left corner.
//            x = qBound(touch_x_min, x, touch_x_max) - touch_x_min;
//            y = qBound(touch_y_min, y, touch_y_max) - touch_y_min;

//            // Get a normalized position in range 0..1.
//            const int hw_w = touch_x_max - touch_x_min;
//            const int hw_h = touch_y_max - touch_y_min;
//            const qreal nx = x / qreal(hw_w);
//            const qreal ny = y / qreal(hw_h);

//            // Map to surface.
//            QRect winRect(focusSurface->geometry());
//            x = int(nx * winRect.width());
//            y = int(ny * winRect.height());

//            focusSurface->sendTouchPointEvent(point.id,
//                                              x, y,
//                                              point.state);
//        }
//        focusSurface->sendTouchFrameEvent();
//    }
//}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidgetCompositor compositor;
    compositor.resize(800, 600);
    compositor.show();

//    QTouchScreenHandlerThread t(QString(), new TouchObserver(&compositor));

    return app.exec();
}

#include "main.moc"