Change copyrights from Nokia to Digia
[profile/ivi/qtwayland.git] / tests / auto / client / mockcompositor.h
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 #ifndef MOCKCOMPOSITOR_H
43 #define MOCKCOMPOSITOR_H
44
45 #include <pthread.h>
46 #include <qglobal.h>
47 #include <wayland-server.h>
48
49 #include <QImage>
50 #include <QMutex>
51 #include <QRect>
52 #include <QSharedPointer>
53 #include <QVariant>
54 #include <QVector>
55 #include <QWaitCondition>
56
57 namespace Impl {
58
59 typedef void (**Implementation)(void);
60
61 class Surface;
62
63 class Compositor
64 {
65 public:
66     Compositor();
67     ~Compositor();
68
69     int fileDescriptor() const { return m_fd; }
70     void dispatchEvents(int timeout = 0);
71
72     uint32_t nextSerial();
73     uint32_t time() { return ++m_time; }
74
75     static void setOutputGeometry(void *compositor, const QList<QVariant> &parameters);
76
77     QVector<Surface *> surfaces() const;
78
79     void addSurface(Surface *surface);
80     void removeSurface(Surface *surface);
81
82     static void setKeyboardFocus(void *data, const QList<QVariant> &parameters);
83     static void sendMousePress(void *data, const QList<QVariant> &parameters);
84     static void sendMouseRelease(void *data, const QList<QVariant> &parameters);
85     static void sendKeyPress(void *data, const QList<QVariant> &parameters);
86     static void sendKeyRelease(void *data, const QList<QVariant> &parameters);
87
88 private:
89     static void bindCompositor(wl_client *client, void *data, uint32_t version, uint32_t id);
90     static void bindSeat(wl_client *client, void *data, uint32_t version, uint32_t id);
91     static void bindOutput(wl_client *client, void *data, uint32_t version, uint32_t id);
92     static void bindShell(wl_client *client, void *data, uint32_t version, uint32_t id);
93
94     static void get_pointer(wl_client *client, wl_resource *resource, uint32_t id);
95     static void get_keyboard(wl_client *client, wl_resource *resource, uint32_t id);
96     static void get_touch(wl_client *client, wl_resource *resource, uint32_t id);
97
98     static void destroyInputResource(wl_resource *resource);
99
100     void initShm();
101
102     void sendOutputGeometry(wl_resource *resource);
103     void sendOutputMode(wl_resource *resource);
104
105     QRect m_outputGeometry;
106
107     wl_display *m_display;
108     wl_event_loop *m_loop;
109     wl_shm *m_shm;
110     int m_fd;
111
112     wl_list m_outputResources;
113     uint32_t m_time;
114
115     wl_seat m_seat;
116     wl_pointer m_pointer;
117     wl_keyboard m_keyboard;
118     QVector<Surface *> m_surfaces;
119 };
120
121 void registerResource(wl_list *list, wl_resource *resource);
122
123 }
124
125 class MockSurface
126 {
127 public:
128     Impl::Surface *handle() const { return m_surface; }
129
130     QImage image;
131
132 private:
133     MockSurface(Impl::Surface *surface);
134     friend class Impl::Compositor;
135     friend class Impl::Surface;
136
137     Impl::Surface *m_surface;
138 };
139
140 Q_DECLARE_METATYPE(QSharedPointer<MockSurface>)
141
142 class MockCompositor
143 {
144 public:
145     MockCompositor();
146     ~MockCompositor();
147
148     void applicationInitialized();
149
150     int waylandFileDescriptor() const;
151     void processWaylandEvents();
152
153     void setOutputGeometry(const QRect &rect);
154     void setKeyboardFocus(const QSharedPointer<MockSurface> &surface);
155     void sendMousePress(const QSharedPointer<MockSurface> &surface, const QPoint &pos);
156     void sendMouseRelease(const QSharedPointer<MockSurface> &surface);
157     void sendKeyPress(const QSharedPointer<MockSurface> &surface, uint code);
158     void sendKeyRelease(const QSharedPointer<MockSurface> &surface, uint code);
159
160     QSharedPointer<MockSurface> surface();
161
162     void lock();
163     void unlock();
164
165 private:
166     struct Command
167     {
168         typedef void (*Callback)(void *target, const QList<QVariant> &parameters);
169
170         Callback callback;
171         void *target;
172         QList<QVariant> parameters;
173     };
174
175     static Command makeCommand(Command::Callback callback, void *target);
176
177     void processCommand(const Command &command);
178     void dispatchCommands();
179
180     static void *run(void *data);
181
182     bool m_alive;
183     bool m_ready;
184     pthread_t m_thread;
185     QMutex m_mutex;
186     QWaitCondition m_waitCondition;
187
188     Impl::Compositor *m_compositor;
189
190     QList<Command> m_commandQueue;
191 };
192
193 #endif