1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef MOCKCOMPOSITOR_CORECOMPOSITOR_H
#define MOCKCOMPOSITOR_CORECOMPOSITOR_H
#include <QtTest/QtTest>
#include <wayland-server-core.h>
struct wl_resource;
namespace MockCompositor {
class Global : public QObject
{
Q_OBJECT
public:
virtual bool isClean() { return true; }
virtual QString dirtyMessage() { return isClean() ? "clean" : "dirty"; }
};
class CoreCompositor
{
public:
explicit CoreCompositor();
~CoreCompositor();
bool isClean();
QString dirtyMessage();
void dispatch();
template<typename function_type, typename... arg_types>
auto exec(function_type func, arg_types&&... args) -> decltype(func())
{
Lock lock(this);
return func(std::forward<arg_types>(args)...);
}
template<typename function_type, typename... arg_types>
auto call(function_type func, arg_types&&... args) -> decltype(func())
{
Lock lock(this);
auto boundFunc = std::bind(func, this);
return boundFunc(this, std::forward<arg_types>(args)...);
}
// Unsafe section below, YOU are responsible that the compositor is locked or
// this is run through the mutex() method!
void add(Global *global);
void remove(Global *global);
/*!
* \brief Constructs and adds a new global with the given parameters
*
* Convenience function. i.e.
*
* compositor->add(new MyGlobal(compositor, version);
*
* can be written as:
*
* compositor->add<MyGlobal>(version);
*
* Returns the new global
*/
template<typename global_type, typename... arg_types>
global_type *add(arg_types&&... args)
{
warnIfNotLockedByThread(Q_FUNC_INFO);
auto *global = new global_type(this, std::forward<arg_types>(args)...);
m_globals.append(global);
return global;
}
/*!
* \brief Removes all globals of the given type
*
* Convenience function
*/
template<typename global_type, typename... arg_types>
void removeAll()
{
const auto globals = getAll<global_type>();
for (auto global : globals)
remove(global);
}
/*!
* \brief Returns a global with the given type, if any
*/
template<typename global_type>
global_type *get()
{
warnIfNotLockedByThread(Q_FUNC_INFO);
for (auto *global : qAsConst(m_globals)) {
if (auto *casted = qobject_cast<global_type *>(global))
return casted;
}
return nullptr;
}
/*!
* \brief Returns the nth global with the given type, if any
*/
template<typename global_type>
global_type *get(int index)
{
warnIfNotLockedByThread(Q_FUNC_INFO);
for (auto *global : qAsConst(m_globals)) {
if (auto *casted = qobject_cast<global_type *>(global)) {
if (index--)
continue;
return casted;
}
}
return nullptr;
}
/*!
* \brief Returns all globals with the given type, if any
*/
template<typename global_type>
QVector<global_type *> getAll()
{
warnIfNotLockedByThread(Q_FUNC_INFO);
QVector<global_type *> matching;
for (auto *global : qAsConst(m_globals)) {
if (auto *casted = qobject_cast<global_type *>(global))
matching.append(casted);
}
return matching;
}
uint nextSerial();
uint currentTimeMilliseconds();
wl_client *client(int index = 0);
void warnIfNotLockedByThread(const char* caller = "warnIfNotLockedbyThread");
public:
// Only use this carefully from the test thread (i.e. lock first)
wl_display *m_display = nullptr;
protected:
class Lock {
public:
explicit Lock(CoreCompositor *compositor)
: m_compositor(compositor)
, m_threadId(std::this_thread::get_id())
{
// Can't use a QMutexLocker here, as it's not movable
compositor->m_mutex.lock();
Q_ASSERT(compositor->m_lock == nullptr);
compositor->m_lock = this;
}
~Lock()
{
Q_ASSERT(m_compositor->m_lock == this);
m_compositor->m_lock = nullptr;
m_compositor->m_mutex.unlock();
}
// Move semantics
Lock(Lock &&) = default;
Lock &operator=(Lock &&) = default;
// Disable copying
Lock(const Lock &) = delete;
Lock &operator=(const Lock &) = delete;
bool isOwnedByCurrentThread() const { return m_threadId == std::this_thread::get_id(); }
private:
CoreCompositor *m_compositor = nullptr;
std::thread::id m_threadId;
};
QByteArray m_socketName;
wl_event_loop *m_eventLoop = nullptr;
bool m_running = true;
QVector<Global *> m_globals;
QElapsedTimer m_timer;
private:
Lock *m_lock = nullptr;
QMutex m_mutex;
std::thread m_dispatchThread;
};
template<typename container_type>
QByteArray toByteArray(container_type container)
{
return QByteArray(reinterpret_cast<const char *>(container.data()), sizeof (container[0]) * container.size());
}
template<typename return_type>
return_type *fromResource(::wl_resource *resource) {
if (auto *r = return_type::Resource::fromResource(resource))
return static_cast<return_type *>(r->object());
return nullptr;
}
} // namespace MockCompositor
#endif // MOCKCOMPOSITOR_CORECOMPOSITOR_H
|