Compositor memory leak fixes
[profile/ivi/qtwayland.git] / src / compositor / windowmanagerprotocol / waylandwindowmanagerintegration.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 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 Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
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 "wayland-server.h"
47 #include "wayland-windowmanager-server-protocol.h"
48
49 WindowManagerServerIntegration::WindowManagerServerIntegration(QObject *parent)
50     : QObject(parent)
51 {
52 }
53
54 WindowManagerServerIntegration::~WindowManagerServerIntegration()
55 {
56     qDeleteAll(m_managedClients);
57 }
58
59 void WindowManagerServerIntegration::initialize(Wayland::Display *waylandDisplay)
60 {
61     wl_display_add_global(waylandDisplay->handle(),&wl_windowmanager_interface,this,WindowManagerServerIntegration::bind_func);
62 }
63
64 void WindowManagerServerIntegration::removeClient(wl_client *client)
65 {
66     WaylandManagedClient *managedClient = m_managedClients.take(client);
67     delete managedClient;
68 }
69
70 WaylandManagedClient *WindowManagerServerIntegration::managedClient(wl_client *client) const
71 {
72     return m_managedClients.value(client, 0);
73 }
74
75 void WindowManagerServerIntegration::mapClientToProcess(wl_client *client, uint32_t processId)
76 {
77     WaylandManagedClient *managedClient = m_managedClients.value(client);
78     if (!managedClient)
79         managedClient = new WaylandManagedClient;
80     managedClient->m_processId = processId;
81     m_managedClients.insert(client, managedClient);
82 }
83
84 void WindowManagerServerIntegration::authenticateWithToken(wl_client *client, const char *token)
85 {
86     Q_ASSERT(token != 0 && *token != 0);
87
88     WaylandManagedClient *managedClient = m_managedClients.value(client);
89     if (!managedClient)
90         managedClient = new WaylandManagedClient;
91     managedClient->m_authenticationToken = QByteArray(token);
92     m_managedClients.insert(client, managedClient);
93
94     emit clientAuthenticated(client);
95 }
96
97 void WindowManagerServerIntegration::bind_func(struct wl_client *client, void *data,
98                                       uint32_t version, uint32_t id)
99 {
100     Q_UNUSED(version);
101     WindowManagerServerIntegration *win_mgr = static_cast<WindowManagerServerIntegration *>(data);
102     struct wl_resource *resource = wl_client_add_object(client,&wl_windowmanager_interface,&windowmanager_interface,id,data);
103     win_mgr->registerResource(resource);
104 }
105
106
107 void WindowManagerServerIntegration::map_client_to_process(struct wl_client *client,
108                                                            struct wl_resource *window_mgr_resource,
109                                                            uint32_t process_id)
110 {
111     WindowManagerServerIntegration *window_mgr = static_cast<WindowManagerServerIntegration *>(window_mgr_resource->data);
112     window_mgr->mapClientToProcess(client,process_id);
113 }
114
115 void WindowManagerServerIntegration::authenticate_with_token(struct wl_client *client,
116                                                              struct wl_resource *window_mgr_resource,
117                                                              const char *wl_authentication_token)
118 {
119     WindowManagerServerIntegration *window_mgr = static_cast<WindowManagerServerIntegration *>(window_mgr_resource->data);
120     window_mgr->authenticateWithToken(client,wl_authentication_token);
121 }
122
123 const struct wl_windowmanager_interface WindowManagerServerIntegration::windowmanager_interface = {
124     WindowManagerServerIntegration::map_client_to_process,
125     WindowManagerServerIntegration::authenticate_with_token,
126 };
127
128
129 /// ///
130 /// / WaylandManagedClient
131 /// ///
132
133 WaylandManagedClient::WaylandManagedClient()
134     : m_processId(0)
135 {
136 }
137
138 qint64 WaylandManagedClient::processId() const
139 {
140     return m_processId;
141 }
142
143 QByteArray WaylandManagedClient::authenticationToken() const
144 {
145     return m_authenticationToken;
146 }