1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the plugins of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qwaylanddisplay.h"
44 #include "qwaylandwindow.h"
45 #include "qwaylandscreen.h"
46 #include "qwaylandcursor.h"
47 #include "qwaylandinputdevice.h"
48 #include "qwaylandclipboard.h"
49 #include "qwaylanddnd.h"
51 #ifdef QT_WAYLAND_GL_SUPPORT
52 #include "gl_integration/qwaylandglintegration.h"
55 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
56 #include "windowmanager_integration/qwaylandwindowmanagerintegration.h"
59 #include <QtCore/QAbstractEventDispatcher>
60 #include <QtGui/private/qguiapplication_p.h>
67 struct wl_surface *QWaylandDisplay::createSurface(void *handle)
69 struct wl_surface * surface = wl_compositor_create_surface(mCompositor);
70 wl_surface_set_user_data(surface, handle);
74 struct wl_buffer *QWaylandDisplay::createShmBuffer(int fd,
75 int width, int height,
77 struct wl_visual *visual)
79 return wl_shm_create_buffer(mShm, fd, width, height, stride, visual);
82 struct wl_visual *QWaylandDisplay::rgbVisual()
87 struct wl_visual *QWaylandDisplay::argbVisual()
92 struct wl_visual *QWaylandDisplay::argbPremultipliedVisual()
94 return premultiplied_argb_visual;
97 #ifdef QT_WAYLAND_GL_SUPPORT
98 QWaylandGLIntegration * QWaylandDisplay::eglIntegration()
100 return mEglIntegration;
104 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
105 QWaylandWindowManagerIntegration *QWaylandDisplay::windowManagerIntegration()
107 return mWindowManagerIntegration;
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)
120 QWaylandWindow *ww = (QWaylandWindow *) wl_surface_get_user_data(surface);
122 ww->configure(time, edges, 0, 0, width, height);
125 const struct wl_shell_listener QWaylandDisplay::shellListener = {
126 QWaylandDisplay::shellHandleConfigure,
129 static QWaylandDisplay *display = 0;
131 QWaylandDisplay::QWaylandDisplay(void)
132 : argb_visual(0), premultiplied_argb_visual(0), rgb_visual(0)
135 qRegisterMetaType<uint32_t>("uint32_t");
137 mDisplay = wl_display_connect(NULL);
138 if (mDisplay == NULL) {
139 qErrnoWarning(errno, "Failed to create display");
140 qFatal("No wayland connection available.");
143 wl_display_add_global_listener(mDisplay, QWaylandDisplay::displayHandleGlobal, this);
145 mFd = wl_display_get_fd(mDisplay, sourceUpdate, this);
146 QAbstractEventDispatcher *dispatcher = QGuiApplicationPrivate::eventDispatcher;
147 connect(dispatcher, SIGNAL(aboutToBlock()), this, SLOT(flushRequests()));
149 mReadNotifier = new QSocketNotifier(mFd, QSocketNotifier::Read, this);
150 connect(mReadNotifier, SIGNAL(activated(int)), this, SLOT(readEvents()));
152 #ifdef QT_WAYLAND_GL_SUPPORT
153 mEglIntegration = QWaylandGLIntegration::createGLIntegration(this);
156 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
157 mWindowManagerIntegration = QWaylandWindowManagerIntegration::createIntegration(this);
160 blockingReadEvents();
162 #ifdef QT_WAYLAND_GL_SUPPORT
163 mEglIntegration->initialize();
169 QWaylandDisplay::~QWaylandDisplay(void)
172 #ifdef QT_WAYLAND_GL_SUPPORT
173 delete mEglIntegration;
175 wl_display_destroy(mDisplay);
178 void QWaylandDisplay::createNewScreen(struct wl_output *output, QRect geometry)
180 QWaylandScreen *waylandScreen = new QWaylandScreen(this,output,geometry);
181 mScreens.append(waylandScreen);
184 void QWaylandDisplay::syncCallback(wl_display_sync_func_t func, void *data)
186 wl_display_sync_callback(mDisplay, func, data);
189 void QWaylandDisplay::frameCallback(wl_display_frame_func_t func, struct wl_surface *surface, void *data)
191 wl_display_frame_callback(mDisplay, surface, func, data);
194 void QWaylandDisplay::flushRequests()
196 if (mSocketMask & WL_DISPLAY_WRITABLE)
197 wl_display_iterate(mDisplay, WL_DISPLAY_WRITABLE);
200 void QWaylandDisplay::readEvents()
202 // verify that there is still data on the socket
214 int ret = ::select(mFd+1, &rs, &ws, &es, &timeout );
217 //qDebug("QWaylandDisplay::readEvents() No data... blocking avoided");
221 wl_display_iterate(mDisplay, WL_DISPLAY_READABLE);
224 void QWaylandDisplay::blockingReadEvents()
226 wl_display_iterate(mDisplay, WL_DISPLAY_READABLE);
229 int QWaylandDisplay::sourceUpdate(uint32_t mask, void *data)
231 QWaylandDisplay *waylandDisplay = static_cast<QWaylandDisplay *>(data);
232 waylandDisplay->mSocketMask = mask;
237 void QWaylandDisplay::outputHandleGeometry(void *data,
239 int32_t x, int32_t y,
240 int32_t physicalWidth,
241 int32_t physicalHeight,
243 const char *make, const char *model)
245 QWaylandDisplay *waylandDisplay = static_cast<QWaylandDisplay *>(data);
246 QRect outputRect = QRect(x, y, physicalWidth, physicalHeight);
247 waylandDisplay->createNewScreen(output,outputRect);
250 void QWaylandDisplay::mode(void *data,
251 struct wl_output *wl_output,
265 const struct wl_output_listener QWaylandDisplay::outputListener = {
266 QWaylandDisplay::outputHandleGeometry,
267 QWaylandDisplay::mode
270 const struct wl_compositor_listener QWaylandDisplay::compositorListener = {
271 QWaylandDisplay::handleVisual,
275 void QWaylandDisplay::waitForScreens()
278 while (mScreens.isEmpty())
279 blockingReadEvents();
282 void QWaylandDisplay::displayHandleGlobal(struct wl_display *display,
284 const char *interface,
289 QWaylandDisplay *that = static_cast<QWaylandDisplay *>(data);
290 that->displayHandleGlobal(id, QByteArray(interface), version);
293 void QWaylandDisplay::displayHandleGlobal(uint32_t id,
294 const QByteArray &interface,
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);
321 void QWaylandDisplay::handleVisual(void *data,
322 struct wl_compositor *compositor,
323 uint32_t id, uint32_t token)
325 QWaylandDisplay *self = static_cast<QWaylandDisplay *>(data);
328 case WL_COMPOSITOR_VISUAL_ARGB32:
329 self->argb_visual = wl_visual_create(self->mDisplay, id, 1);
331 case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32:
332 self->premultiplied_argb_visual =
333 wl_visual_create(self->mDisplay, id, 1);
335 case WL_COMPOSITOR_VISUAL_XRGB32:
336 self->rgb_visual = wl_visual_create(self->mDisplay, id, 1);