Change copyrights from Nokia to Digia
[profile/ivi/qtwayland.git] / tests / auto / compositor / mockclient.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 test suite of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "mockclient.h"
43
44 #include <QElapsedTimer>
45 #include <QSocketNotifier>
46
47 #include <private/qguiapplication_p.h>
48
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include <errno.h>
53 #include <sys/mman.h>
54
55
56 MockClient::MockClient()
57     : display(wl_display_connect(0))
58     , compositor(0)
59     , output(0)
60 {
61     if (!display)
62         qFatal("MockClient(): wl_display_connect() failed");
63
64     wl_display_add_global_listener(display, handleGlobal, this);
65
66     fd = wl_display_get_fd(display, 0, 0);
67
68     QSocketNotifier *readNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
69     connect(readNotifier, SIGNAL(activated(int)), this, SLOT(readEvents()));
70
71     QAbstractEventDispatcher *dispatcher = QGuiApplicationPrivate::eventDispatcher;
72     connect(dispatcher, SIGNAL(awake()), this, SLOT(flushDisplay()));
73
74     QElapsedTimer timeout;
75     timeout.start();
76     do {
77         QCoreApplication::processEvents();
78     } while (!(compositor && output) && timeout.elapsed() < 1000);
79
80     if (!compositor || !output)
81         qFatal("MockClient(): failed to receive globals from display");
82 }
83
84 const wl_output_listener MockClient::outputListener = {
85     MockClient::outputGeometryEvent,
86     MockClient::outputModeEvent
87 };
88
89 MockClient::~MockClient()
90 {
91     wl_display_disconnect(display);
92 }
93
94 void MockClient::handleGlobal(wl_display *, uint32_t id, const char *interface, uint32_t, void *data)
95 {
96     resolve(data)->handleGlobal(id, QByteArray(interface));
97 }
98
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 *,
103                                      int32_t )
104 {
105     resolve(data)->geometry = QRect(x, y, width, height);
106 }
107
108 void MockClient::outputModeEvent(void *, wl_output *, uint32_t,
109                                  int, int, int)
110 {
111 }
112
113 void MockClient::readEvents()
114 {
115     wl_display_iterate(display, WL_DISPLAY_READABLE);
116 }
117
118 void MockClient::flushDisplay()
119 {
120     wl_display_flush(display);
121 }
122
123 void MockClient::handleGlobal(uint32_t id, const QByteArray &interface)
124 {
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));
132     }
133 }
134
135 wl_surface *MockClient::createSurface()
136 {
137     flushDisplay();
138     return wl_compositor_create_surface(compositor);
139 }
140
141 ShmBuffer::ShmBuffer(const QSize &size, wl_shm *shm)
142     : handle(0)
143 {
144     int stride = size.width() * 4;
145     int alloc = stride * size.height();
146
147     char filename[] = "/tmp/wayland-shm-XXXXXX";
148
149     int fd = mkstemp(filename);
150     if (fd < 0) {
151         qWarning("open %s failed: %s", filename, strerror(errno));
152         return;
153     }
154
155     if (ftruncate(fd, alloc) < 0) {
156         qWarning("ftruncate failed: %s", strerror(errno));
157         close(fd);
158         return;
159     }
160
161     void *data = mmap(0, alloc, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
162     unlink(filename);
163
164     if (data == MAP_FAILED) {
165         qWarning("mmap failed: %s", strerror(errno));
166         close(fd);
167         return;
168     }
169
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);
174     close(fd);
175 }
176
177 ShmBuffer::~ShmBuffer()
178 {
179     munmap(image.bits(), image.byteCount());
180     wl_buffer_destroy(handle);
181     wl_shm_pool_destroy(shm_pool);
182 }
183