1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "mockclient.h"
44 #include <QElapsedTimer>
45 #include <QSocketNotifier>
47 #include <private/qguiapplication_p.h>
56 MockClient::MockClient()
57 : display(wl_display_connect(0))
62 qFatal("MockClient(): wl_display_connect() failed");
64 wl_display_add_global_listener(display, handleGlobal, this);
66 fd = wl_display_get_fd(display, 0, 0);
68 QSocketNotifier *readNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
69 connect(readNotifier, SIGNAL(activated(int)), this, SLOT(readEvents()));
71 QAbstractEventDispatcher *dispatcher = QGuiApplicationPrivate::eventDispatcher;
72 connect(dispatcher, SIGNAL(awake()), this, SLOT(flushDisplay()));
74 QElapsedTimer timeout;
77 QCoreApplication::processEvents();
78 } while (!(compositor && output) && timeout.elapsed() < 1000);
80 if (!compositor || !output)
81 qFatal("MockClient(): failed to receive globals from display");
84 const wl_output_listener MockClient::outputListener = {
85 MockClient::outputGeometryEvent,
86 MockClient::outputModeEvent
89 MockClient::~MockClient()
91 wl_display_disconnect(display);
94 void MockClient::handleGlobal(wl_display *, uint32_t id, const char *interface, uint32_t, void *data)
96 resolve(data)->handleGlobal(id, QByteArray(interface));
99 void MockClient::outputGeometryEvent(void *data, wl_output *,
100 int32_t x, int32_t y,
101 int32_t width, int32_t height,
102 int, const char *, const char *,
105 resolve(data)->geometry = QRect(x, y, width, height);
108 void MockClient::outputModeEvent(void *, wl_output *, uint32_t,
113 void MockClient::readEvents()
115 wl_display_iterate(display, WL_DISPLAY_READABLE);
118 void MockClient::flushDisplay()
120 wl_display_flush(display);
123 void MockClient::handleGlobal(uint32_t id, const QByteArray &interface)
125 if (interface == "wl_compositor") {
126 compositor = static_cast<wl_compositor *>(wl_display_bind(display, id, &wl_compositor_interface));
127 } else if (interface == "wl_output") {
128 output = static_cast<wl_output *>(wl_display_bind(display, id, &wl_output_interface));
129 wl_output_add_listener(output, &outputListener, this);
130 } else if (interface == "wl_shm") {
131 shm = static_cast<wl_shm *>(wl_display_bind(display, id, &wl_shm_interface));
135 wl_surface *MockClient::createSurface()
138 return wl_compositor_create_surface(compositor);
141 ShmBuffer::ShmBuffer(const QSize &size, wl_shm *shm)
144 int stride = size.width() * 4;
145 int alloc = stride * size.height();
147 char filename[] = "/tmp/wayland-shm-XXXXXX";
149 int fd = mkstemp(filename);
151 qWarning("open %s failed: %s", filename, strerror(errno));
155 if (ftruncate(fd, alloc) < 0) {
156 qWarning("ftruncate failed: %s", strerror(errno));
161 void *data = mmap(0, alloc, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
164 if (data == MAP_FAILED) {
165 qWarning("mmap failed: %s", strerror(errno));
170 image = QImage(static_cast<uchar *>(data), size.width(), size.height(), stride, QImage::Format_ARGB32_Premultiplied);
171 shm_pool = wl_shm_create_pool(shm,fd,alloc);
172 handle = wl_shm_pool_create_buffer(shm_pool,0, size.width(), size.height(),
173 stride, WL_SHM_FORMAT_ARGB8888);
177 ShmBuffer::~ShmBuffer()
179 munmap(image.bits(), image.byteCount());
180 wl_buffer_destroy(handle);
181 wl_shm_pool_destroy(shm_pool);