blob: eb431f3afd3836e2003b3257def7e4e8832c321b [file] [log] [blame]
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -08001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Neil Roberts40c0c3f2013-10-29 20:13:45 +000023#include "config.h"
24
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080025#include <stdlib.h>
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080026#include <assert.h>
27#include <signal.h>
28#include <unistd.h>
Marek Chalupac8daf772015-03-30 06:37:55 -040029#include <string.h>
Bryce Harringtona7680262014-11-19 17:18:34 -080030
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080031#include "../src/compositor.h"
Derek Foremanf6a65922015-02-24 09:32:14 -060032#include "weston-test-server-protocol.h"
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080033
Neil Roberts40c0c3f2013-10-29 20:13:45 +000034#ifdef ENABLE_EGL
35#include <EGL/egl.h>
36#include <EGL/eglext.h>
Daniel Stonee7897712015-02-11 18:15:17 +000037#include "../src/weston-egl-ext.h"
Neil Roberts40c0c3f2013-10-29 20:13:45 +000038#endif /* ENABLE_EGL */
39
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080040struct weston_test {
41 struct weston_compositor *compositor;
42 struct weston_layer layer;
43 struct weston_process process;
44};
45
46struct weston_test_surface {
47 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -050048 struct weston_view *view;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080049 int32_t x, y;
50 struct weston_test *test;
51};
52
53static void
54test_client_sigchld(struct weston_process *process, int status)
55{
56 struct weston_test *test =
57 container_of(process, struct weston_test, process);
58
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010059 /* Chain up from weston-test-runner's exit code so that automake
60 * knows the exit status and can report e.g. skipped tests. */
61 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
62 exit(WEXITSTATUS(status));
63
64 /* In case the child aborted or segfaulted... */
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080065 assert(status == 0);
66
67 wl_display_terminate(test->compositor->wl_display);
68}
69
70static struct weston_seat *
71get_seat(struct weston_test *test)
72{
73 struct wl_list *seat_list;
74 struct weston_seat *seat;
75
76 seat_list = &test->compositor->seat_list;
77 assert(wl_list_length(seat_list) == 1);
78 seat = container_of(seat_list->next, struct weston_seat, link);
79
80 return seat;
81}
82
83static void
84notify_pointer_position(struct weston_test *test, struct wl_resource *resource)
85{
86 struct weston_seat *seat = get_seat(test);
Kristian Høgsberge3148752013-05-06 23:19:49 -040087 struct weston_pointer *pointer = seat->pointer;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080088
Derek Foremanf6a65922015-02-24 09:32:14 -060089 weston_test_send_pointer_position(resource, pointer->x, pointer->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080090}
91
92static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -060093test_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080094{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +010095 struct weston_test_surface *test_surface = surface->configure_private;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080096 struct weston_test *test = test_surface->test;
97
Giulio Camuffo412e6a52014-07-09 22:12:56 +030098 if (wl_list_empty(&test_surface->view->layer_link.link))
99 weston_layer_entry_insert(&test->layer.view_list,
100 &test_surface->view->layer_link);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800101
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600102 weston_view_set_position(test_surface->view,
103 test_surface->x, test_surface->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800104
Kristian Høgsbergace0a392013-11-13 21:55:57 -0800105 weston_view_update_transform(test_surface->view);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800106}
107
108static void
109move_surface(struct wl_client *client, struct wl_resource *resource,
110 struct wl_resource *surface_resource,
111 int32_t x, int32_t y)
112{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400113 struct weston_surface *surface =
114 wl_resource_get_user_data(surface_resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800115 struct weston_test_surface *test_surface;
116
Giulio Camuffo7fe01b12013-03-28 18:02:42 +0100117 test_surface = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500118 if (!test_surface) {
119 test_surface = malloc(sizeof *test_surface);
120 if (!test_surface) {
121 wl_resource_post_no_memory(resource);
122 return;
123 }
124
125 test_surface->view = weston_view_create(surface);
126 if (!test_surface->view) {
127 wl_resource_post_no_memory(resource);
128 free(test_surface);
129 return;
130 }
131
132 surface->configure_private = test_surface;
133 surface->configure = test_surface_configure;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800134 }
135
136 test_surface->surface = surface;
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400137 test_surface->test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800138 test_surface->x = x;
139 test_surface->y = y;
140}
141
142static void
143move_pointer(struct wl_client *client, struct wl_resource *resource,
144 int32_t x, int32_t y)
145{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400146 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800147 struct weston_seat *seat = get_seat(test);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400148 struct weston_pointer *pointer = seat->pointer;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800149
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500150 notify_motion(seat, 100,
151 wl_fixed_from_int(x) - pointer->x,
152 wl_fixed_from_int(y) - pointer->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800153
154 notify_pointer_position(test, resource);
155}
156
157static void
158send_button(struct wl_client *client, struct wl_resource *resource,
159 int32_t button, uint32_t state)
160{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400161 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800162 struct weston_seat *seat = get_seat(test);
163
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800164 notify_button(seat, 100, button, state);
165}
166
167static void
168activate_surface(struct wl_client *client, struct wl_resource *resource,
169 struct wl_resource *surface_resource)
170{
171 struct weston_surface *surface = surface_resource ?
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400172 wl_resource_get_user_data(surface_resource) : NULL;
173 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800174 struct weston_seat *seat;
175
176 seat = get_seat(test);
177
178 if (surface) {
179 weston_surface_activate(surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400180 notify_keyboard_focus_in(seat, &seat->keyboard->keys,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800181 STATE_UPDATE_AUTOMATIC);
182 }
183 else {
184 notify_keyboard_focus_out(seat);
185 weston_surface_activate(surface, seat);
186 }
187}
188
189static void
190send_key(struct wl_client *client, struct wl_resource *resource,
191 uint32_t key, enum wl_keyboard_key_state state)
192{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400193 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800194 struct weston_seat *seat = get_seat(test);
195
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800196 notify_key(seat, 100, key, state, STATE_UPDATE_AUTOMATIC);
197}
198
Marek Chalupac8daf772015-03-30 06:37:55 -0400199static void
200device_release(struct wl_client *client,
201 struct wl_resource *resource, const char *device)
202{
203 struct weston_test *test = wl_resource_get_user_data(resource);
204 struct weston_seat *seat = get_seat(test);
205
206 if (strcmp(device, "pointer") == 0) {
207 weston_seat_release_pointer(seat);
208 } else if (strcmp(device, "keyboard") == 0) {
209 weston_seat_release_keyboard(seat);
210 } else if (strcmp(device, "touch") == 0) {
211 weston_seat_release_touch(seat);
212 } else if (strcmp(device, "seat") == 0) {
213 weston_seat_release(seat);
214 } else {
215 assert(0 && "Unsupported device");
216 }
217}
218
219static void
220device_add(struct wl_client *client,
221 struct wl_resource *resource, const char *device)
222{
223 struct weston_test *test = wl_resource_get_user_data(resource);
224 struct weston_seat *seat = get_seat(test);
225
226 if (strcmp(device, "pointer") == 0) {
227 weston_seat_init_pointer(seat);
228 } else if (strcmp(device, "keyboard") == 0) {
229 weston_seat_init_keyboard(seat, NULL);
230 } else if (strcmp(device, "touch") == 0) {
231 weston_seat_init_touch(seat);
232 } else {
233 assert(0 && "Unsupported device");
234 }
235}
236
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000237#ifdef ENABLE_EGL
238static int
239is_egl_buffer(struct wl_resource *resource)
240{
241 PFNEGLQUERYWAYLANDBUFFERWL query_buffer =
242 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
243 EGLint format;
244
245 if (query_buffer(eglGetCurrentDisplay(),
246 resource,
247 EGL_TEXTURE_FORMAT,
248 &format))
249 return 1;
250
251 return 0;
252}
253#endif /* ENABLE_EGL */
254
255static void
256get_n_buffers(struct wl_client *client, struct wl_resource *resource)
257{
258 int n_buffers = 0;
259
260#ifdef ENABLE_EGL
261 struct wl_resource *buffer_resource;
262 int i;
263
264 for (i = 0; i < 1000; i++) {
265 buffer_resource = wl_client_get_object(client, i);
266
267 if (buffer_resource == NULL)
268 continue;
269
270 if (is_egl_buffer(buffer_resource))
271 n_buffers++;
272 }
273#endif /* ENABLE_EGL */
274
Derek Foremanf6a65922015-02-24 09:32:14 -0600275 weston_test_send_n_egl_buffers(resource, n_buffers);
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000276}
277
Derek Foremanf6a65922015-02-24 09:32:14 -0600278static const struct weston_test_interface test_implementation = {
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800279 move_surface,
280 move_pointer,
281 send_button,
282 activate_surface,
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000283 send_key,
Marek Chalupac8daf772015-03-30 06:37:55 -0400284 device_release,
285 device_add,
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000286 get_n_buffers,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800287};
288
289static void
290bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id)
291{
292 struct weston_test *test = data;
293 struct wl_resource *resource;
294
Derek Foremanf6a65922015-02-24 09:32:14 -0600295 resource = wl_resource_create(client, &weston_test_interface, 1, id);
Marek Chalupa42ebdda2014-07-11 12:33:02 +0200296 if (!resource) {
297 wl_client_post_no_memory(client);
298 return;
299 }
300
Kristian Høgsberg442a5fa2013-07-03 18:13:33 -0400301 wl_resource_set_implementation(resource,
302 &test_implementation, test, NULL);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800303
304 notify_pointer_position(test, resource);
305}
306
307static void
308idle_launch_client(void *data)
309{
310 struct weston_test *test = data;
311 pid_t pid;
312 sigset_t allsigs;
313 char *path;
314
315 path = getenv("WESTON_TEST_CLIENT_PATH");
316 if (path == NULL)
Pekka Paalanenf72e4792013-11-21 16:23:57 +0200317 return;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800318 pid = fork();
319 if (pid == -1)
320 exit(EXIT_FAILURE);
321 if (pid == 0) {
322 sigfillset(&allsigs);
323 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
324 execl(path, path, NULL);
325 weston_log("compositor: executing '%s' failed: %m\n", path);
326 exit(EXIT_FAILURE);
327 }
328
329 test->process.pid = pid;
330 test->process.cleanup = test_client_sigchld;
331 weston_watch_process(&test->process);
332}
333
334WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500335module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700336 int *argc, char *argv[])
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800337{
338 struct weston_test *test;
339 struct wl_event_loop *loop;
340
Peter Huttererf3d62272013-08-08 11:57:05 +1000341 test = zalloc(sizeof *test);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800342 if (test == NULL)
343 return -1;
344
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800345 test->compositor = ec;
346 weston_layer_init(&test->layer, &ec->cursor_layer.link);
347
Derek Foremanf6a65922015-02-24 09:32:14 -0600348 if (wl_global_create(ec->wl_display, &weston_test_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400349 test, bind_test) == NULL)
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800350 return -1;
351
352 loop = wl_display_get_event_loop(ec->wl_display);
353 wl_event_loop_add_idle(loop, idle_launch_client, test);
354
355 return 0;
356}