Change copyrights from Nokia to Digia
[profile/ivi/qtwayland.git] / tests / auto / client / mockinput.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 "mockcompositor.h"
43 #include "mocksurface.h"
44
45 namespace Impl {
46
47 void Compositor::destroyInputResource(wl_resource *resource)
48 {
49     Compositor *compositor = static_cast<Compositor *>(resource->data);
50     wl_keyboard *keyboard = &compositor->m_keyboard;
51     wl_pointer *pointer = &compositor->m_pointer;
52
53     if (keyboard->focus_resource == resource)
54         keyboard->focus_resource = 0;
55     if (pointer->focus_resource == resource)
56         pointer->focus_resource = 0;
57
58     wl_list_remove(&resource->link);
59
60     free(resource);
61 }
62
63 static void destroyInputDevice(wl_resource *resource)
64 {
65     wl_list_remove(&resource->link);
66     free(resource);
67 }
68
69 void pointer_attach(wl_client *client,
70                     wl_resource *device_resource,
71                     uint32_t time,
72                     wl_resource *buffer_resource, int32_t x, int32_t y)
73 {
74     Q_UNUSED(client);
75     Q_UNUSED(device_resource);
76     Q_UNUSED(time);
77     Q_UNUSED(buffer_resource);
78     Q_UNUSED(QPoint(x, y));
79 }
80
81 void Compositor::get_pointer(wl_client *client,
82                              wl_resource *resource,
83                              uint32_t id)
84 {
85     static const struct wl_pointer_interface pointer_interface = {
86         pointer_attach
87     };
88     Compositor *compositor = static_cast<Compositor *>(resource->data);
89     wl_pointer *pointer = &compositor->m_pointer;
90     wl_resource *clientResource = wl_client_add_object(client,
91                                                        &wl_pointer_interface,
92                                                        &pointer_interface,
93                                                        id,
94                                                        pointer);
95     wl_list_insert(&pointer->resource_list, &clientResource->link);
96     clientResource->destroy = destroyInputDevice;
97 }
98
99 void Compositor::get_keyboard(wl_client *client,
100                               wl_resource *resource,
101                               uint32_t id)
102 {
103     Compositor *compositor = static_cast<Compositor *>(resource->data);
104     wl_keyboard *keyboard = &compositor->m_keyboard;
105     wl_resource *clientResource = wl_client_add_object(client,
106                                                        &wl_keyboard_interface,
107                                                        0,
108                                                        id,
109                                                        keyboard);
110     wl_list_insert(&keyboard->resource_list, &clientResource->link);
111     clientResource->destroy = destroyInputDevice;
112 }
113
114 void Compositor::get_touch(wl_client *client,
115                            wl_resource *resource,
116                            uint32_t id)
117 {
118     Q_UNUSED(client);
119     Q_UNUSED(resource);
120     Q_UNUSED(id);
121 }
122
123 void Compositor::bindSeat(wl_client *client, void *compositorData, uint32_t version, uint32_t id)
124 {
125     static const struct wl_seat_interface seatInterface = {
126         get_pointer,
127         get_keyboard,
128         get_touch
129     };
130
131     Q_UNUSED(version);
132     wl_resource *resource = wl_client_add_object(client, &wl_seat_interface, &seatInterface, id, compositorData);
133     resource->destroy = destroyInputResource;
134
135     Compositor *compositor = static_cast<Compositor *>(compositorData);
136     wl_list_insert(&compositor->m_seat.base_resource_list, &resource->link);
137
138     wl_seat_send_capabilities(resource, WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_KEYBOARD);
139 }
140
141 static wl_surface *resolveSurface(const QVariant &v)
142 {
143     QSharedPointer<MockSurface> mockSurface = v.value<QSharedPointer<MockSurface> >();
144     Surface *surface = mockSurface ? mockSurface->handle() : 0;
145     return surface ? surface->handle() : 0;
146 }
147
148 void Compositor::setKeyboardFocus(void *data, const QList<QVariant> &parameters)
149 {
150     Compositor *compositor = static_cast<Compositor *>(data);
151     wl_keyboard_set_focus(&compositor->m_keyboard, resolveSurface(parameters.first()));
152 }
153
154 void Compositor::sendMousePress(void *data, const QList<QVariant> &parameters)
155 {
156     Compositor *compositor = static_cast<Compositor *>(data);
157     wl_surface *surface = resolveSurface(parameters.first());
158     if (!surface)
159         return;
160
161     QPoint pos = parameters.last().toPoint();
162     wl_pointer_set_focus(&compositor->m_pointer, surface,
163                          wl_fixed_from_int(pos.x()), wl_fixed_from_int(pos.y()));
164     wl_pointer_send_motion(compositor->m_pointer.focus_resource, compositor->time(),
165                            wl_fixed_from_double(pos.x()), wl_fixed_from_double(pos.y()));
166     wl_pointer_send_button(compositor->m_pointer.focus_resource,
167                            compositor->nextSerial(), compositor->time(), 0x110, 1);
168 }
169
170 void Compositor::sendMouseRelease(void *data, const QList<QVariant> &parameters)
171 {
172     Compositor *compositor = static_cast<Compositor *>(data);
173     wl_surface *surface = resolveSurface(parameters.first());
174     if (!surface)
175         return;
176
177     wl_pointer_send_button(compositor->m_pointer.focus_resource,
178                            compositor->nextSerial(), compositor->time(), 0x110, 0);
179 }
180
181 void Compositor::sendKeyPress(void *data, const QList<QVariant> &parameters)
182 {
183     Compositor *compositor = static_cast<Compositor *>(data);
184     wl_surface *surface = resolveSurface(parameters.first());
185     if (!surface)
186         return;
187
188     wl_keyboard_send_key(compositor->m_keyboard.focus_resource,
189                          compositor->nextSerial(), compositor->time(), parameters.last().toUInt() - 8, 1);
190 }
191
192 void Compositor::sendKeyRelease(void *data, const QList<QVariant> &parameters)
193 {
194     Compositor *compositor = static_cast<Compositor *>(data);
195     wl_surface *surface = resolveSurface(parameters.first());
196     if (!surface)
197         return;
198
199     wl_keyboard_send_key(compositor->m_keyboard.focus_resource,
200                          compositor->nextSerial(), compositor->time(), parameters.last().toUInt() - 8, 0);
201 }
202
203 }