Prevented crash due to dangling pointer in QWaylandInputDevice.
[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 "qwaylandbuffer.h"
45 #include "qwaylanddisplay.h"
46 #include "qwaylandinputdevice.h"
47 #include "qwaylandscreen.h"
48
49 #include <QtGui/QWidget>
50 #include <QtGui/QWindowSystemInterface>
51
52 #include <QDebug>
53
54 QWaylandWindow::QWaylandWindow(QWidget *window)
55     : QPlatformWindow(window)
56     , mDisplay(QWaylandScreen::waylandScreenFromWidget(window)->display())
57     , mBuffer(0)
58     , mWaitingForFrameSync(false)
59 {
60     static WId id = 1;
61     mWindowId = id++;
62
63     mSurface = mDisplay->createSurface(this);
64 }
65
66 QWaylandWindow::~QWaylandWindow()
67 {
68     if (mSurface)
69         wl_surface_destroy(mSurface);
70
71     QList<QWaylandInputDevice *> inputDevices = mDisplay->inputDevices();
72     for (int i = 0; i < inputDevices.size(); ++i)
73         inputDevices.at(i)->handleWindowDestroyed(this);
74 }
75
76 WId QWaylandWindow::winId() const
77 {
78     return mWindowId;
79 }
80
81 void QWaylandWindow::setParent(const QPlatformWindow *parent)
82 {
83     Q_UNUSED(parent);
84     qWarning("Sub window is not supported");
85 }
86
87 void QWaylandWindow::setVisible(bool visible)
88 {
89     if (!mSurface && visible) {
90         mSurface = mDisplay->createSurface(this);
91         newSurfaceCreated();
92     }
93
94     if (visible) {
95         wl_surface_map_toplevel(mSurface);
96     } else {
97         wl_surface_destroy(mSurface);
98         mSurface = NULL;
99     }
100 }
101
102 void QWaylandWindow::configure(uint32_t time, uint32_t edges,
103                                int32_t x, int32_t y,
104                                int32_t width, int32_t height)
105 {
106     Q_UNUSED(time);
107     Q_UNUSED(edges);
108     QRect geometry = QRect(x, y, width, height);
109
110     setGeometry(geometry);
111
112     QWindowSystemInterface::handleGeometryChange(widget(), geometry);
113 }
114
115 void QWaylandWindow::attach(QWaylandBuffer *buffer)
116 {
117     mBuffer = buffer;
118     if (mSurface) {
119         wl_surface_attach(mSurface, buffer->buffer(),0,0);
120     }
121 }
122
123
124 void QWaylandWindow::damage(const QRegion &region)
125 {
126     //We have to do sync stuff before calling damage, or we might
127     //get a frame callback before we get the timestamp
128     mDisplay->frameCallback(QWaylandWindow::frameCallback, this);
129     mWaitingForFrameSync = true;
130
131     QVector<QRect> rects = region.rects();
132     for (int i = 0; i < rects.size(); i++) {
133         const QRect rect = rects.at(i);
134         wl_surface_damage(mSurface,
135                           rect.x(), rect.y(), rect.width(), rect.height());
136     }
137 }
138
139 void QWaylandWindow::newSurfaceCreated()
140 {
141     if (mBuffer) {
142         wl_surface_attach(mSurface,mBuffer->buffer(),0,0);
143     }
144 }
145
146 void QWaylandWindow::frameCallback(void *data, uint32_t time)
147 {
148     Q_UNUSED(time);
149     QWaylandWindow *self = static_cast<QWaylandWindow*>(data);
150     self->mWaitingForFrameSync = false;
151 }
152
153 void QWaylandWindow::waitForFrameSync()
154 {
155     mDisplay->flushRequests();
156     while (mWaitingForFrameSync)
157         mDisplay->readEvents();
158 }