Revert "Suppress QWindowSystemInterface inclusion warnings."
[profile/ivi/qtwayland.git] / src / plugins / platforms / wayland / qwaylandextendedsurface.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwaylandextendedsurface.h"
43
44 #include "qwaylandwindow.h"
45
46 #include "wayland-client.h"
47 #include "wayland-surface-extension-client-protocol.h"
48
49 #include "qwaylanddisplay.h"
50
51 #include "qwaylandnativeinterface.h"
52
53 #include <QtGui/QGuiApplication>
54 #include <qpa/qplatformnativeinterface.h>
55 #include <QtGui/QWindowSystemInterface>
56
57 QWaylandSurfaceExtension::QWaylandSurfaceExtension(QWaylandDisplay *display, uint32_t id)
58 {
59     m_surface_extension = static_cast<struct wl_surface_extension *>(
60                 wl_display_bind(display->wl_display(),id, &wl_surface_extension_interface));
61 }
62
63 QWaylandExtendedSurface *QWaylandSurfaceExtension::getExtendedWindow(QWaylandWindow *window)
64 {
65     struct wl_surface *surface = window->wl_surface();
66     Q_ASSERT(surface);
67     struct wl_extended_surface *extended_surface =
68             wl_surface_extension_get_extended_surface(m_surface_extension,surface);
69
70     return new QWaylandExtendedSurface(window,extended_surface);
71 }
72
73
74 QWaylandExtendedSurface::QWaylandExtendedSurface(QWaylandWindow *window, struct wl_extended_surface *extended_surface)
75     : m_window(window)
76     , m_extended_surface(extended_surface)
77     , m_exposed(true)
78 {
79     wl_extended_surface_add_listener(m_extended_surface,&QWaylandExtendedSurface::extended_surface_listener,this);
80 }
81
82 void QWaylandExtendedSurface::updateGenericProperty(const QString &name, const QVariant &value)
83 {
84
85     QByteArray byteValue;
86     QDataStream ds(&byteValue, QIODevice::WriteOnly);
87     ds << value;
88
89     struct wl_array data;
90     data.size = byteValue.size();
91     data.data = (void*)byteValue.constData();
92     data.alloc = 0;
93
94     wl_extended_surface_update_generic_property(m_extended_surface,qPrintable(name),&data);
95
96     m_properties.insert(name,value);
97     QWaylandNativeInterface *nativeInterface = static_cast<QWaylandNativeInterface *>(
98                 QGuiApplication::platformNativeInterface());
99     nativeInterface->emitWindowPropertyChanged(m_window,name);
100 }
101
102 static int32_t waylandRotationFromScreenOrientation(Qt::ScreenOrientation orientation)
103 {
104     switch (orientation) {
105     case Qt::PortraitOrientation: return WL_EXTENDED_SURFACE_ORIENTATION_PORTRAITORIENTATION;
106     case Qt::InvertedPortraitOrientation: return WL_EXTENDED_SURFACE_ORIENTATION_INVERTEDPORTRAITORIENTATION;
107     case Qt::LandscapeOrientation: return WL_EXTENDED_SURFACE_ORIENTATION_LANDSCAPEORIENTATION;
108     case Qt::InvertedLandscapeOrientation: return WL_EXTENDED_SURFACE_ORIENTATION_INVERTEDLANDSCAPEORIENTATION;
109     default: return WL_EXTENDED_SURFACE_ORIENTATION_PRIMARYORIENTATION;
110     }
111 }
112
113 void QWaylandExtendedSurface::setWindowOrientation(Qt::ScreenOrientation orientation)
114 {
115     wl_extended_surface_set_window_orientation(m_extended_surface, waylandRotationFromScreenOrientation(orientation));
116 }
117
118 void QWaylandExtendedSurface::setContentOrientation(Qt::ScreenOrientation orientation)
119 {
120     wl_extended_surface_set_content_orientation(m_extended_surface, waylandRotationFromScreenOrientation(orientation));
121 }
122
123 QVariantMap QWaylandExtendedSurface::properties() const
124 {
125     return m_properties;
126 }
127
128 QVariant QWaylandExtendedSurface::property(const QString &name)
129 {
130     return m_properties.value(name);
131 }
132
133 QVariant QWaylandExtendedSurface::property(const QString &name, const QVariant &defaultValue)
134 {
135     return m_properties.value(name,defaultValue);
136 }
137
138 void QWaylandExtendedSurface::onscreen_visibility(void *data, wl_extended_surface *wl_extended_surface, int32_t visible)
139 {
140     QWaylandExtendedSurface *extendedWindow = static_cast<QWaylandExtendedSurface *>(data);
141     Q_UNUSED(extendedWindow);
142     Q_UNUSED(wl_extended_surface);
143
144     // Do not send events when the state is not changing...
145     if (visible == extendedWindow->m_window->isExposed())
146         return;
147
148     extendedWindow->m_exposed = visible;
149     QWaylandWindow *w = extendedWindow->m_window;
150     QWindowSystemInterface::handleSynchronousExposeEvent(w->window(),
151                                                          visible
152                                                          ? QRegion(w->geometry())
153                                                          : QRegion());
154 }
155
156 void QWaylandExtendedSurface::set_generic_property(void *data, wl_extended_surface *wl_extended_surface, const char *name, wl_array *value)
157 {
158     Q_UNUSED(wl_extended_surface);
159
160     QWaylandExtendedSurface *extended_window = static_cast<QWaylandExtendedSurface *>(data);
161
162     QVariant variantValue;
163     QByteArray baValue = QByteArray((const char*)value->data, value->size);
164     QDataStream ds(&baValue, QIODevice::ReadOnly);
165     ds >> variantValue;
166
167     QString qstring_name = QString::fromLatin1(name);
168     extended_window->m_properties.insert(qstring_name,variantValue);
169
170     QWaylandNativeInterface *nativeInterface = static_cast<QWaylandNativeInterface *>(
171                 QGuiApplication::platformNativeInterface());
172     nativeInterface->emitWindowPropertyChanged(extended_window->m_window,QString::fromLatin1(name));
173 }
174
175 Qt::WindowFlags QWaylandExtendedSurface::setWindowFlags(Qt::WindowFlags flags)
176 {
177     uint wlFlags = 0;
178     if (flags & Qt::WindowStaysOnTopHint) wlFlags |= WL_EXTENDED_SURFACE_WINDOWFLAG_STAYSONTOP;
179     if (flags & Qt::WindowOverridesSystemGestures) wlFlags |= WL_EXTENDED_SURFACE_WINDOWFLAG_OVERRIDESSYSTEMGESTURES;
180
181     wl_extended_surface_set_window_flags(m_extended_surface, wlFlags);
182
183     return flags & (
184                     Qt::WindowStaysOnTopHint
185                     | Qt::WindowOverridesSystemGestures
186                    );
187 }
188
189 const struct wl_extended_surface_listener QWaylandExtendedSurface::extended_surface_listener = {
190     QWaylandExtendedSurface::onscreen_visibility,
191     QWaylandExtendedSurface::set_generic_property
192 };