Implemented graceful quitting of clients.
[profile/ivi/qtwayland.git] / src / plugins / platforms / wayland / windowmanager_integration / qwaylandwindowmanagerintegration.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 "qwaylandwindowmanagerintegration.h"
43 #include "wayland-windowmanager-client-protocol.h"
44 #include "qwaylandscreen.h"
45 #include "qwaylandwindow.h"
46
47 #include <stdint.h>
48 #include <QtCore/QEvent>
49 #include <QtCore/QHash>
50 #include <QtCore/QUrl>
51 #include <qpa/qplatformnativeinterface.h>
52 #include <qpa/qplatformwindow.h>
53 #include <QtGui/QtEvents>
54 #include <QtGui/QGuiApplication>
55
56 #include <QDebug>
57
58 class QWaylandWindowManagerIntegrationPrivate {
59 public:
60     QWaylandWindowManagerIntegrationPrivate(QWaylandDisplay *waylandDisplay);
61     bool m_blockPropertyUpdates;
62     QWaylandDisplay *m_waylandDisplay;
63     struct wl_windowmanager *m_waylandWindowManager;
64     QHash<QWindow*, QVariantMap> m_queuedProperties;
65     bool m_showIsFullScreen;
66 };
67
68 QWaylandWindowManagerIntegrationPrivate::QWaylandWindowManagerIntegrationPrivate(QWaylandDisplay *waylandDisplay)
69     : m_blockPropertyUpdates(false)
70     , m_waylandDisplay(waylandDisplay)
71     , m_waylandWindowManager(0)
72     , m_showIsFullScreen(false)
73 {
74
75 }
76
77 QWaylandWindowManagerIntegration *QWaylandWindowManagerIntegration::m_instance = 0;
78
79 QWaylandWindowManagerIntegration *QWaylandWindowManagerIntegration::createIntegration(QWaylandDisplay *waylandDisplay)
80 {
81     return new QWaylandWindowManagerIntegration(waylandDisplay);
82 }
83
84 QWaylandWindowManagerIntegration::QWaylandWindowManagerIntegration(QWaylandDisplay *waylandDisplay)
85     : d_ptr(new QWaylandWindowManagerIntegrationPrivate(waylandDisplay))
86 {
87     m_instance = this;
88
89     wl_display_add_global_listener(d_ptr->m_waylandDisplay->wl_display(),
90                                    QWaylandWindowManagerIntegration::wlHandleListenerGlobal,
91                                    this);
92 }
93
94 QWaylandWindowManagerIntegration::~QWaylandWindowManagerIntegration()
95 {
96
97 }
98
99 QWaylandWindowManagerIntegration *QWaylandWindowManagerIntegration::instance()
100 {
101     return m_instance;
102 }
103
104 struct wl_windowmanager *QWaylandWindowManagerIntegration::windowManager() const
105 {
106     Q_D(const QWaylandWindowManagerIntegration);
107     return d->m_waylandWindowManager;
108 }
109
110 bool QWaylandWindowManagerIntegration::showIsFullScreen() const
111 {
112     Q_D(const QWaylandWindowManagerIntegration);
113     return d->m_showIsFullScreen;
114 }
115
116 void QWaylandWindowManagerIntegration::wlHandleListenerGlobal(wl_display *display, uint32_t id, const char *interface, uint32_t version, void *data)
117 {
118     Q_UNUSED(version);
119     if (strcmp(interface, "wl_windowmanager") == 0) {
120         QWaylandWindowManagerIntegration *integration = static_cast<QWaylandWindowManagerIntegration *>(data);
121         integration->d_ptr->m_waylandWindowManager =
122                 static_cast<struct wl_windowmanager *>(wl_display_bind(display, id, &wl_windowmanager_interface));
123         wl_windowmanager_add_listener(integration->d_ptr->m_waylandWindowManager, &windowmanager_listener, integration);
124     }
125 }
126
127 const struct wl_windowmanager_listener QWaylandWindowManagerIntegration::windowmanager_listener = {
128     QWaylandWindowManagerIntegration::handle_hints,
129     QWaylandWindowManagerIntegration::handle_quit
130 };
131
132 void QWaylandWindowManagerIntegration::handle_hints(void *data, wl_windowmanager *ext, int32_t showIsFullScreen)
133 {
134     Q_UNUSED(ext);
135     QWaylandWindowManagerIntegration *self = static_cast<QWaylandWindowManagerIntegration *>(data);
136     self->d_func()->m_showIsFullScreen = showIsFullScreen;
137 }
138
139 void QWaylandWindowManagerIntegration::handle_quit(void *data, wl_windowmanager *ext)
140 {
141     Q_UNUSED(data);
142     Q_UNUSED(ext);
143     QGuiApplication::quit();
144 }
145
146 void QWaylandWindowManagerIntegration::mapClientToProcess(long long processId)
147 {
148     Q_D(QWaylandWindowManagerIntegration);
149     if (d->m_waylandWindowManager)
150         wl_windowmanager_map_client_to_process(d->m_waylandWindowManager, (uint32_t) processId);
151 }
152
153 void QWaylandWindowManagerIntegration::authenticateWithToken(const QByteArray &token)
154 {
155     Q_D(QWaylandWindowManagerIntegration);
156     QByteArray authToken = token;
157     if (authToken.isEmpty())
158         authToken = qgetenv("WL_AUTHENTICATION_TOKEN");
159
160     if (d->m_waylandWindowManager && !authToken.isEmpty()) {
161         wl_windowmanager_authenticate_with_token(d->m_waylandWindowManager, authToken.constData());
162     }
163 }
164
165 void QWaylandWindowManagerIntegration::openUrl_helper(const QUrl &url)
166 {
167     Q_D(QWaylandWindowManagerIntegration);
168     if (d->m_waylandWindowManager) {
169         QByteArray data = url.toString().toUtf8();
170
171         static const int chunkSize = 128;
172         while (!data.isEmpty()) {
173             QByteArray chunk = data.left(chunkSize);
174             data = data.mid(chunkSize);
175             wl_windowmanager_open_url(d->m_waylandWindowManager, !data.isEmpty(), chunk.constData());
176         }
177     }
178 }
179
180 bool QWaylandWindowManagerIntegration::openUrl(const QUrl &url)
181 {
182     openUrl_helper(url);
183     return true;
184 }
185
186 bool QWaylandWindowManagerIntegration::openDocument(const QUrl &url)
187 {
188     openUrl_helper(url);
189     return true;
190 }