Make QPlatformIntegration not have a factory for eventdispatcher
[profile/ivi/qtwayland.git] / src / plugins / platforms / wayland / qwaylanddisplay.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 plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwaylanddisplay.h"
43
44 #include "qwaylandwindow.h"
45 #include "qwaylandscreen.h"
46 #include "qwaylandcursor.h"
47 #include "qwaylandinputdevice.h"
48 #include "qwaylandclipboard.h"
49 #include "qwaylanddnd.h"
50
51 #ifdef QT_WAYLAND_GL_SUPPORT
52 #include "gl_integration/qwaylandglintegration.h"
53 #endif
54
55 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
56 #include "windowmanager_integration/qwaylandwindowmanagerintegration.h"
57 #endif
58
59 #include <QtCore/QAbstractEventDispatcher>
60 #include <QtGui/private/qguiapplication_p.h>
61
62 #include <unistd.h>
63 #include <fcntl.h>
64 #include <stdio.h>
65 #include <errno.h>
66
67 struct wl_surface *QWaylandDisplay::createSurface(void *handle)
68 {
69     struct wl_surface * surface = wl_compositor_create_surface(mCompositor);
70     wl_surface_set_user_data(surface, handle);
71     return surface;
72 }
73
74 struct wl_buffer *QWaylandDisplay::createShmBuffer(int fd,
75                                                    int width, int height,
76                                                    uint32_t stride,
77                                                    struct wl_visual *visual)
78 {
79     return wl_shm_create_buffer(mShm, fd, width, height, stride, visual);
80 }
81
82 struct wl_visual *QWaylandDisplay::rgbVisual()
83 {
84     return rgb_visual;
85 }
86
87 struct wl_visual *QWaylandDisplay::argbVisual()
88 {
89     return argb_visual;
90 }
91
92 struct wl_visual *QWaylandDisplay::argbPremultipliedVisual()
93 {
94     return premultiplied_argb_visual;
95 }
96
97 #ifdef QT_WAYLAND_GL_SUPPORT
98 QWaylandGLIntegration * QWaylandDisplay::eglIntegration()
99 {
100     return mEglIntegration;
101 }
102 #endif
103
104 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
105 QWaylandWindowManagerIntegration *QWaylandDisplay::windowManagerIntegration()
106 {
107     return mWindowManagerIntegration;
108 }
109 #endif
110
111 void QWaylandDisplay::shellHandleConfigure(void *data, struct wl_shell *shell,
112                                            uint32_t time, uint32_t edges,
113                                            struct wl_surface *surface,
114                                            int32_t width, int32_t height)
115 {
116     Q_UNUSED(data);
117     Q_UNUSED(shell);
118     Q_UNUSED(time);
119     Q_UNUSED(edges);
120     QWaylandWindow *ww = (QWaylandWindow *) wl_surface_get_user_data(surface);
121
122     ww->configure(time, edges, 0, 0, width, height);
123 }
124
125 const struct wl_shell_listener QWaylandDisplay::shellListener = {
126     QWaylandDisplay::shellHandleConfigure,
127 };
128
129 static QWaylandDisplay *display = 0;
130
131 QWaylandDisplay::QWaylandDisplay(void)
132     : argb_visual(0), premultiplied_argb_visual(0), rgb_visual(0)
133 {
134     display = this;
135     qRegisterMetaType<uint32_t>("uint32_t");
136
137     mDisplay = wl_display_connect(NULL);
138     if (mDisplay == NULL) {
139         qErrnoWarning(errno, "Failed to create display");
140         qFatal("No wayland connection available.");
141     }
142
143     wl_display_add_global_listener(mDisplay, QWaylandDisplay::displayHandleGlobal, this);
144
145     mFd = wl_display_get_fd(mDisplay, sourceUpdate, this);
146     QAbstractEventDispatcher *dispatcher = QGuiApplicationPrivate::eventDispatcher;
147     connect(dispatcher, SIGNAL(aboutToBlock()), this, SLOT(flushRequests()));
148
149     mReadNotifier = new QSocketNotifier(mFd, QSocketNotifier::Read, this);
150     connect(mReadNotifier, SIGNAL(activated(int)), this, SLOT(readEvents()));
151
152 #ifdef QT_WAYLAND_GL_SUPPORT
153     mEglIntegration = QWaylandGLIntegration::createGLIntegration(this);
154 #endif
155
156 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
157     mWindowManagerIntegration = QWaylandWindowManagerIntegration::createIntegration(this);
158 #endif
159
160     blockingReadEvents();
161
162 #ifdef QT_WAYLAND_GL_SUPPORT
163     mEglIntegration->initialize();
164 #endif
165
166     waitForScreens();
167 }
168
169 QWaylandDisplay::~QWaylandDisplay(void)
170 {
171     close(mFd);
172 #ifdef QT_WAYLAND_GL_SUPPORT
173     delete mEglIntegration;
174 #endif
175     wl_display_destroy(mDisplay);
176 }
177
178 void QWaylandDisplay::createNewScreen(struct wl_output *output, QRect geometry)
179 {
180     QWaylandScreen *waylandScreen = new QWaylandScreen(this,output,geometry);
181     mScreens.append(waylandScreen);
182 }
183
184 void QWaylandDisplay::syncCallback(wl_display_sync_func_t func, void *data)
185 {
186     wl_display_sync_callback(mDisplay, func, data);
187 }
188
189 void QWaylandDisplay::frameCallback(wl_display_frame_func_t func, struct wl_surface *surface, void *data)
190 {
191     wl_display_frame_callback(mDisplay, surface, func, data);
192 }
193
194 void QWaylandDisplay::flushRequests()
195 {
196     if (mSocketMask & WL_DISPLAY_WRITABLE)
197         wl_display_iterate(mDisplay, WL_DISPLAY_WRITABLE);
198 }
199
200 void QWaylandDisplay::readEvents()
201 {
202 // verify that there is still data on the socket
203     fd_set fds;
204     FD_ZERO(&fds);
205     FD_SET(mFd, &fds);
206     fd_set nds;
207     FD_ZERO(&nds);
208     fd_set rs = fds;
209     fd_set ws = nds;
210     fd_set es = nds;
211     timeval timeout;
212     timeout.tv_sec = 0;
213     timeout.tv_usec = 0;
214     int ret = ::select(mFd+1, &rs, &ws, &es, &timeout );
215
216     if (ret <= 0) {
217         //qDebug("QWaylandDisplay::readEvents() No data... blocking avoided");
218         return;
219     }
220
221     wl_display_iterate(mDisplay, WL_DISPLAY_READABLE);
222 }
223
224 void QWaylandDisplay::blockingReadEvents()
225 {
226     wl_display_iterate(mDisplay, WL_DISPLAY_READABLE);
227 }
228
229 int QWaylandDisplay::sourceUpdate(uint32_t mask, void *data)
230 {
231     QWaylandDisplay *waylandDisplay = static_cast<QWaylandDisplay *>(data);
232     waylandDisplay->mSocketMask = mask;
233
234     return 0;
235 }
236
237 void QWaylandDisplay::outputHandleGeometry(void *data,
238                                            wl_output *output,
239                                            int32_t x, int32_t y,
240                                            int32_t physicalWidth,
241                                            int32_t physicalHeight,
242                                            int subpixel,
243                                            const char *make, const char *model)
244 {
245     QWaylandDisplay *waylandDisplay = static_cast<QWaylandDisplay *>(data);
246     QRect outputRect = QRect(x, y, physicalWidth, physicalHeight);
247     waylandDisplay->createNewScreen(output,outputRect);
248 }
249
250 void QWaylandDisplay::mode(void *data,
251              struct wl_output *wl_output,
252              uint32_t flags,
253              int width,
254              int height,
255              int refresh)
256 {
257     Q_UNUSED(data);
258     Q_UNUSED(wl_output);
259     Q_UNUSED(flags);
260     Q_UNUSED(width);
261     Q_UNUSED(height);
262     Q_UNUSED(refresh);
263 }
264
265 const struct wl_output_listener QWaylandDisplay::outputListener = {
266     QWaylandDisplay::outputHandleGeometry,
267     QWaylandDisplay::mode
268 };
269
270 const struct wl_compositor_listener QWaylandDisplay::compositorListener = {
271     QWaylandDisplay::handleVisual,
272 };
273
274
275 void QWaylandDisplay::waitForScreens()
276 {
277     flushRequests();
278     while (mScreens.isEmpty())
279         blockingReadEvents();
280 }
281
282 void QWaylandDisplay::displayHandleGlobal(struct wl_display *display,
283                                           uint32_t id,
284                                           const char *interface,
285                                           uint32_t version,
286                                           void *data)
287 {
288     Q_UNUSED(display);
289     QWaylandDisplay *that = static_cast<QWaylandDisplay *>(data);
290     that->displayHandleGlobal(id, QByteArray(interface), version);
291 }
292
293 void QWaylandDisplay::displayHandleGlobal(uint32_t id,
294                                           const QByteArray &interface,
295                                           uint32_t version)
296 {
297     Q_UNUSED(version);
298     if (interface == "wl_output") {
299         struct wl_output *output = wl_output_create(mDisplay, id, 1);
300         wl_output_add_listener(output, &outputListener, this);
301     } else if (interface == "wl_compositor") {
302         mCompositor = wl_compositor_create(mDisplay, id, 1);
303         wl_compositor_add_listener(mCompositor,
304                                    &compositorListener, this);
305     } else if (interface == "wl_shm") {
306         mShm = wl_shm_create(mDisplay, id, 1);
307     } else if (interface == "wl_shell"){
308         mShell = wl_shell_create(mDisplay, id, 1);
309         wl_shell_add_listener(mShell, &shellListener, this);
310     } else if (interface == "wl_input_device") {
311         QWaylandInputDevice *inputDevice =
312             new QWaylandInputDevice(this, id);
313         mInputDevices.append(inputDevice);
314     } else if (interface == "wl_selection_offer") {
315         QWaylandClipboard::instance(display)->createSelectionOffer(id);
316     } else if (interface == "wl_drag_offer") {
317         QWaylandDrag::instance(display)->createDragOffer(id);
318     }
319 }
320
321 void QWaylandDisplay::handleVisual(void *data,
322                                    struct wl_compositor *compositor,
323                                    uint32_t id, uint32_t token)
324 {
325     QWaylandDisplay *self = static_cast<QWaylandDisplay *>(data);
326
327     switch (token) {
328     case WL_COMPOSITOR_VISUAL_ARGB32:
329         self->argb_visual = wl_visual_create(self->mDisplay, id, 1);
330         break;
331     case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32:
332         self->premultiplied_argb_visual =
333             wl_visual_create(self->mDisplay, id, 1);
334         break;
335     case WL_COMPOSITOR_VISUAL_XRGB32:
336         self->rgb_visual = wl_visual_create(self->mDisplay, id, 1);
337         break;
338     }
339 }