Change copyrights from Nokia to Digia
[profile/ivi/qtwayland.git] / src / compositor / windowmanagerprotocol / waylandwindowmanagerintegration.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the Qt Compositor.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 **     of its contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "waylandwindowmanagerintegration.h"
42
43 #include "wayland_wrapper/wldisplay.h"
44 #include "wayland_wrapper/wlcompositor.h"
45
46 #include "compositor_api/waylandcompositor.h"
47
48 #include "wayland-server.h"
49 #include "wayland-windowmanager-server-protocol.h"
50
51 #include <QUrl>
52
53 WindowManagerServerIntegration::WindowManagerServerIntegration(WaylandCompositor *compositor, QObject *parent)
54     : QObject(parent)
55     , m_showIsFullScreen(false)
56     , m_compositor(compositor)
57 {
58 }
59
60 WindowManagerServerIntegration::~WindowManagerServerIntegration()
61 {
62     qDeleteAll(m_managedClients);
63 }
64
65 void WindowManagerServerIntegration::initialize(Wayland::Display *waylandDisplay)
66 {
67     wl_display_add_global(waylandDisplay->handle(),&wl_windowmanager_interface,this,WindowManagerServerIntegration::bind_func);
68 }
69
70 void WindowManagerServerIntegration::removeClient(wl_client *client)
71 {
72     WaylandManagedClient *managedClient = m_managedClients.take(client);
73     delete managedClient;
74 }
75
76 WaylandManagedClient *WindowManagerServerIntegration::managedClient(wl_client *client) const
77 {
78     return m_managedClients.value(client, 0);
79 }
80
81 void WindowManagerServerIntegration::mapClientToProcess(wl_client *client, uint32_t processId)
82 {
83     WaylandManagedClient *managedClient = m_managedClients.value(client);
84     if (!managedClient)
85         managedClient = new WaylandManagedClient;
86     managedClient->m_processId = processId;
87     m_managedClients.insert(client, managedClient);
88 }
89
90 void WindowManagerServerIntegration::authenticateWithToken(wl_client *client, const char *token)
91 {
92     Q_ASSERT(token != 0 && *token != 0);
93
94     WaylandManagedClient *managedClient = m_managedClients.value(client);
95     if (!managedClient)
96         managedClient = new WaylandManagedClient;
97     managedClient->m_authenticationToken = QByteArray(token);
98     m_managedClients.insert(client, managedClient);
99
100     emit clientAuthenticated(client);
101 }
102
103 void WindowManagerServerIntegration::setShowIsFullScreen(bool value)
104 {
105     m_showIsFullScreen = value;
106     struct wl_resource *resource;
107     wl_list_for_each(resource,&client_resources, link) {
108         wl_windowmanager_send_hints(resource, int32_t(m_showIsFullScreen));
109     }
110 }
111
112 void WindowManagerServerIntegration::sendQuitMessage(wl_client *client)
113 {
114     struct wl_resource *resource;
115     wl_list_for_each(resource, &client_resources, link) {
116         if (resource->client == client) {
117             wl_windowmanager_send_quit(resource);
118             return;
119         }
120     }
121 }
122
123 struct WindowManagerServerIntegrationClientData
124 {
125     QByteArray url;
126     WindowManagerServerIntegration *integration;
127 };
128
129 void WindowManagerServerIntegration::bind_func(struct wl_client *client, void *data,
130                                       uint32_t version, uint32_t id)
131 {
132     Q_UNUSED(version);
133
134     WindowManagerServerIntegrationClientData *clientData = new WindowManagerServerIntegrationClientData;
135     clientData->integration = static_cast<WindowManagerServerIntegration *>(data);
136
137     wl_resource *resource = wl_client_add_object(client,&wl_windowmanager_interface,&windowmanager_interface,id,clientData);
138     resource->destroy = WindowManagerServerIntegration::destroy_resource;
139     clientData->integration->registerResource(resource);
140     wl_windowmanager_send_hints(resource, int32_t(clientData->integration->m_showIsFullScreen));
141 }
142
143 void WindowManagerServerIntegration::destroy_resource(wl_resource *resource)
144 {
145     WindowManagerServerIntegrationClientData *data = static_cast<WindowManagerServerIntegrationClientData *>(resource->data);
146     WindowManagerServerIntegration *window_mgr = data->integration;
147
148     window_mgr->removeClient(resource->client);
149
150     delete data;
151     free(resource);
152 }
153
154 void WindowManagerServerIntegration::map_client_to_process(struct wl_client *client,
155                                                            struct wl_resource *window_mgr_resource,
156                                                            uint32_t process_id)
157 {
158     WindowManagerServerIntegration *window_mgr = static_cast<WindowManagerServerIntegrationClientData *>(window_mgr_resource->data)->integration;
159     window_mgr->mapClientToProcess(client,process_id);
160 }
161
162 void WindowManagerServerIntegration::authenticate_with_token(struct wl_client *client,
163                                                              struct wl_resource *window_mgr_resource,
164                                                              const char *wl_authentication_token)
165 {
166     WindowManagerServerIntegration *window_mgr = static_cast<WindowManagerServerIntegrationClientData *>(window_mgr_resource->data)->integration;
167     window_mgr->authenticateWithToken(client,wl_authentication_token);
168 }
169
170 void WindowManagerServerIntegration::open_url(struct wl_client *client,
171                                               struct wl_resource *window_mgr_resource,
172                                               uint32_t remaining,
173                                               const char *url)
174 {
175     WindowManagerServerIntegrationClientData *data = static_cast<WindowManagerServerIntegrationClientData *>(window_mgr_resource->data);
176     WindowManagerServerIntegration *window_mgr = data->integration;
177
178     data->url.append(url);
179
180     if (!remaining) {
181         window_mgr->m_compositor->openUrl(client, QUrl(QString::fromUtf8(data->url)));
182         data->url = QByteArray();
183     }
184 }
185
186 const struct wl_windowmanager_interface WindowManagerServerIntegration::windowmanager_interface = {
187     WindowManagerServerIntegration::map_client_to_process,
188     WindowManagerServerIntegration::authenticate_with_token,
189     WindowManagerServerIntegration::open_url
190 };
191
192
193 /// ///
194 /// / WaylandManagedClient
195 /// ///
196
197 WaylandManagedClient::WaylandManagedClient()
198     : m_processId(0)
199 {
200 }
201
202 qint64 WaylandManagedClient::processId() const
203 {
204     return m_processId;
205 }
206
207 QByteArray WaylandManagedClient::authenticationToken() const
208 {
209     return m_authenticationToken;
210 }