Get wayland plugin working again.
[profile/ivi/qtwayland.git] / src / plugins / platforms / wayland / qwaylandwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the config.tests of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwaylandwindow.h"
43
44 #include "qwaylandbuffer.h"
45 #include "qwaylanddisplay.h"
46 #include "qwaylandinputdevice.h"
47 #include "qwaylandscreen.h"
48
49 #include <QtGui/QWindow>
50
51 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
52 #include "windowmanager_integration/qwaylandwindowmanagerintegration.h"
53 #endif
54
55 #include <QCoreApplication>
56 #include <QtGui/QWindowSystemInterface>
57
58 #include <QDebug>
59
60 QWaylandWindow::QWaylandWindow(QWindow *window)
61     : QPlatformWindow(window)
62     , mDisplay(QWaylandScreen::waylandScreenFromWindow(window)->display())
63     , mBuffer(0)
64     , mWaitingForFrameSync(false)
65 {
66     static WId id = 1;
67     mWindowId = id++;
68
69 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
70         mDisplay->windowManagerIntegration()->mapClientToProcess(qApp->applicationPid());
71         mDisplay->windowManagerIntegration()->authenticateWithToken();
72 #endif
73
74     mSurface = mDisplay->createSurface(this);
75 }
76
77 QWaylandWindow::~QWaylandWindow()
78 {
79     if (mSurface)
80         wl_surface_destroy(mSurface);
81
82     QList<QWaylandInputDevice *> inputDevices = mDisplay->inputDevices();
83     for (int i = 0; i < inputDevices.size(); ++i)
84         inputDevices.at(i)->handleWindowDestroyed(this);
85 }
86
87 WId QWaylandWindow::winId() const
88 {
89     return mWindowId;
90 }
91
92 void QWaylandWindow::setParent(const QPlatformWindow *parent)
93 {
94     Q_UNUSED(parent);
95     qWarning("Sub window is not supported");
96 }
97
98 void QWaylandWindow::setVisible(bool visible)
99 {
100     if (!mSurface && visible) {
101         mSurface = mDisplay->createSurface(this);
102         newSurfaceCreated();
103     }
104
105     if (visible) {
106         wl_surface_map_toplevel(mSurface);
107         QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
108     } else {
109         wl_surface_destroy(mSurface);
110         mSurface = NULL;
111     }
112 }
113
114 void QWaylandWindow::configure(uint32_t time, uint32_t edges,
115                                int32_t x, int32_t y,
116                                int32_t width, int32_t height)
117 {
118     Q_UNUSED(time);
119     Q_UNUSED(edges);
120
121     QRect geometry = QRect(x, y, width, height);
122     setGeometry(geometry);
123
124     QWindowSystemInterface::handleGeometryChange(window(), geometry);
125 }
126
127 void QWaylandWindow::attach(QWaylandBuffer *buffer)
128 {
129     mBuffer = buffer;
130     if (mSurface) {
131         wl_surface_attach(mSurface, buffer->buffer(),0,0);
132     }
133 }
134
135 void QWaylandWindow::damage(const QRegion &region)
136 {
137     //We have to do sync stuff before calling damage, or we might
138     //get a frame callback before we get the timestamp
139     mDisplay->frameCallback(QWaylandWindow::frameCallback, mSurface, this);
140     mWaitingForFrameSync = true;
141
142     QVector<QRect> rects = region.rects();
143     for (int i = 0; i < rects.size(); i++) {
144         const QRect rect = rects.at(i);
145         wl_buffer_damage(mBuffer->buffer(), rect.x(), rect.y(), rect.width(), rect.height());
146         wl_surface_damage(mSurface,
147                           rect.x(), rect.y(), rect.width(), rect.height());
148     }
149 }
150
151 void QWaylandWindow::newSurfaceCreated()
152 {
153     if (mBuffer) {
154         wl_surface_attach(mSurface,mBuffer->buffer(),0,0);
155     }
156 }
157
158 void QWaylandWindow::frameCallback(struct wl_surface *surface, void *data, uint32_t time)
159 {
160     Q_UNUSED(time);
161     Q_UNUSED(surface);
162     QWaylandWindow *self = static_cast<QWaylandWindow*>(data);
163     self->mWaitingForFrameSync = false;
164 }
165
166 void QWaylandWindow::waitForFrameSync()
167 {
168     mDisplay->flushRequests();
169     while (mWaitingForFrameSync)
170         mDisplay->blockingReadEvents();
171 }