c7cd1476d67e52bd6a1769ee787774dd643134d8
[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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwaylandwindow.h"
43
44 #include "qwaylanddisplay.h"
45 #include "qwaylandscreen.h"
46 #include "qwaylandbuffer.h"
47
48 #include <QtGui/QWidget>
49 #include <QtGui/QWindowSystemInterface>
50
51 #include <QDebug>
52
53 QWaylandWindow::QWaylandWindow(QWidget *window)
54     : QPlatformWindow(window)
55     , mDisplay(QWaylandScreen::waylandScreenFromWidget(window)->display())
56     , mBuffer(0)
57     , mWaitingForFrameSync(false)
58 {
59     static WId id = 1;
60     mWindowId = id++;
61
62     mSurface = mDisplay->createSurface(this);
63 }
64
65 QWaylandWindow::~QWaylandWindow()
66 {
67     if (mSurface)
68         wl_surface_destroy(mSurface);
69 }
70
71 WId QWaylandWindow::winId() const
72 {
73     return mWindowId;
74 }
75
76 void QWaylandWindow::setParent(const QPlatformWindow *parent)
77 {
78     Q_UNUSED(parent);
79     qWarning("Sub window is not supported");
80 }
81
82 void QWaylandWindow::setVisible(bool visible)
83 {
84     if (!mSurface && visible) {
85         mSurface = mDisplay->createSurface(this);
86         newSurfaceCreated();
87     }
88
89     if (visible) {
90         wl_surface_map_toplevel(mSurface);
91     } else {
92         wl_surface_destroy(mSurface);
93         mSurface = NULL;
94     }
95 }
96
97 void QWaylandWindow::configure(uint32_t time, uint32_t edges,
98                                int32_t x, int32_t y,
99                                int32_t width, int32_t height)
100 {
101     Q_UNUSED(time);
102     Q_UNUSED(edges);
103     QRect geometry = QRect(x, y, width, height);
104
105     setGeometry(geometry);
106
107     QWindowSystemInterface::handleGeometryChange(widget(), geometry);
108 }
109
110 void QWaylandWindow::attach(QWaylandBuffer *buffer)
111 {
112     mBuffer = buffer;
113     if (mSurface) {
114         wl_surface_attach(mSurface, buffer->buffer(),0,0);
115     }
116 }
117
118
119 void QWaylandWindow::damage(const QRegion &region)
120 {
121     //We have to do sync stuff before calling damage, or we might
122     //get a frame callback before we get the timestamp
123     mDisplay->frameCallback(QWaylandWindow::frameCallback, this);
124     mWaitingForFrameSync = true;
125
126     QVector<QRect> rects = region.rects();
127     for (int i = 0; i < rects.size(); i++) {
128         const QRect rect = rects.at(i);
129         wl_surface_damage(mSurface,
130                           rect.x(), rect.y(), rect.width(), rect.height());
131     }
132 }
133
134 void QWaylandWindow::newSurfaceCreated()
135 {
136     if (mBuffer) {
137         wl_surface_attach(mSurface,mBuffer->buffer(),0,0);
138     }
139 }
140
141 void QWaylandWindow::frameCallback(void *data, uint32_t time)
142 {
143     Q_UNUSED(time);
144     QWaylandWindow *self = static_cast<QWaylandWindow*>(data);
145     self->mWaitingForFrameSync = false;
146 }
147
148 void QWaylandWindow::waitForFrameSync()
149 {
150     mDisplay->flushRequests();
151     while (mWaitingForFrameSync)
152         mDisplay->readEvents();
153 }