blob: 649bddaa8d6b91eac46d807ce65882c2ce31d21c [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>
Bryce Harringtona7680262014-11-19 17:18:34 -080029
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080030#include "../src/compositor.h"
Derek Foremanf6a65922015-02-24 09:32:14 -060031#include "weston-test-server-protocol.h"
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080032
Neil Roberts40c0c3f2013-10-29 20:13:45 +000033#ifdef ENABLE_EGL
34#include <EGL/egl.h>
35#include <EGL/eglext.h>
Daniel Stonee7897712015-02-11 18:15:17 +000036#include "../src/weston-egl-ext.h"
Neil Roberts40c0c3f2013-10-29 20:13:45 +000037#endif /* ENABLE_EGL */
38
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080039struct weston_test {
40 struct weston_compositor *compositor;
41 struct weston_layer layer;
42 struct weston_process process;
43};
44
45struct weston_test_surface {
46 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -050047 struct weston_view *view;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080048 int32_t x, y;
49 struct weston_test *test;
50};
51
52static void
53test_client_sigchld(struct weston_process *process, int status)
54{
55 struct weston_test *test =
56 container_of(process, struct weston_test, process);
57
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010058 /* Chain up from weston-test-runner's exit code so that automake
59 * knows the exit status and can report e.g. skipped tests. */
60 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
61 exit(WEXITSTATUS(status));
62
63 /* In case the child aborted or segfaulted... */
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080064 assert(status == 0);
65
66 wl_display_terminate(test->compositor->wl_display);
67}
68
69static struct weston_seat *
70get_seat(struct weston_test *test)
71{
72 struct wl_list *seat_list;
73 struct weston_seat *seat;
74
75 seat_list = &test->compositor->seat_list;
76 assert(wl_list_length(seat_list) == 1);
77 seat = container_of(seat_list->next, struct weston_seat, link);
78
79 return seat;
80}
81
82static void
83notify_pointer_position(struct weston_test *test, struct wl_resource *resource)
84{
85 struct weston_seat *seat = get_seat(test);
Kristian Høgsberge3148752013-05-06 23:19:49 -040086 struct weston_pointer *pointer = seat->pointer;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080087
Derek Foremanf6a65922015-02-24 09:32:14 -060088 weston_test_send_pointer_position(resource, pointer->x, pointer->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080089}
90
91static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -060092test_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080093{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +010094 struct weston_test_surface *test_surface = surface->configure_private;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080095 struct weston_test *test = test_surface->test;
96
Giulio Camuffo412e6a52014-07-09 22:12:56 +030097 if (wl_list_empty(&test_surface->view->layer_link.link))
98 weston_layer_entry_insert(&test->layer.view_list,
99 &test_surface->view->layer_link);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800100
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600101 weston_view_set_position(test_surface->view,
102 test_surface->x, test_surface->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800103
Kristian Høgsbergace0a392013-11-13 21:55:57 -0800104 weston_view_update_transform(test_surface->view);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800105}
106
107static void
108move_surface(struct wl_client *client, struct wl_resource *resource,
109 struct wl_resource *surface_resource,
110 int32_t x, int32_t y)
111{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400112 struct weston_surface *surface =
113 wl_resource_get_user_data(surface_resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800114 struct weston_test_surface *test_surface;
115
Giulio Camuffo7fe01b12013-03-28 18:02:42 +0100116 test_surface = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500117 if (!test_surface) {
118 test_surface = malloc(sizeof *test_surface);
119 if (!test_surface) {
120 wl_resource_post_no_memory(resource);
121 return;
122 }
123
124 test_surface->view = weston_view_create(surface);
125 if (!test_surface->view) {
126 wl_resource_post_no_memory(resource);
127 free(test_surface);
128 return;
129 }
130
131 surface->configure_private = test_surface;
132 surface->configure = test_surface_configure;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800133 }
134
135 test_surface->surface = surface;
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400136 test_surface->test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800137 test_surface->x = x;
138 test_surface->y = y;
139}
140
141static void
142move_pointer(struct wl_client *client, struct wl_resource *resource,
143 int32_t x, int32_t y)
144{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400145 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800146 struct weston_seat *seat = get_seat(test);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400147 struct weston_pointer *pointer = seat->pointer;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800148
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500149 notify_motion(seat, 100,
150 wl_fixed_from_int(x) - pointer->x,
151 wl_fixed_from_int(y) - pointer->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800152
153 notify_pointer_position(test, resource);
154}
155
156static void
157send_button(struct wl_client *client, struct wl_resource *resource,
158 int32_t button, uint32_t state)
159{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400160 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800161 struct weston_seat *seat = get_seat(test);
162
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800163 notify_button(seat, 100, button, state);
164}
165
166static void
167activate_surface(struct wl_client *client, struct wl_resource *resource,
168 struct wl_resource *surface_resource)
169{
170 struct weston_surface *surface = surface_resource ?
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400171 wl_resource_get_user_data(surface_resource) : NULL;
172 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800173 struct weston_seat *seat;
174
175 seat = get_seat(test);
176
177 if (surface) {
178 weston_surface_activate(surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400179 notify_keyboard_focus_in(seat, &seat->keyboard->keys,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800180 STATE_UPDATE_AUTOMATIC);
181 }
182 else {
183 notify_keyboard_focus_out(seat);
184 weston_surface_activate(surface, seat);
185 }
186}
187
188static void
189send_key(struct wl_client *client, struct wl_resource *resource,
190 uint32_t key, enum wl_keyboard_key_state state)
191{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400192 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800193 struct weston_seat *seat = get_seat(test);
194
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800195 notify_key(seat, 100, key, state, STATE_UPDATE_AUTOMATIC);
196}
197
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000198#ifdef ENABLE_EGL
199static int
200is_egl_buffer(struct wl_resource *resource)
201{
202 PFNEGLQUERYWAYLANDBUFFERWL query_buffer =
203 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
204 EGLint format;
205
206 if (query_buffer(eglGetCurrentDisplay(),
207 resource,
208 EGL_TEXTURE_FORMAT,
209 &format))
210 return 1;
211
212 return 0;
213}
214#endif /* ENABLE_EGL */
215
216static void
217get_n_buffers(struct wl_client *client, struct wl_resource *resource)
218{
219 int n_buffers = 0;
220
221#ifdef ENABLE_EGL
222 struct wl_resource *buffer_resource;
223 int i;
224
225 for (i = 0; i < 1000; i++) {
226 buffer_resource = wl_client_get_object(client, i);
227
228 if (buffer_resource == NULL)
229 continue;
230
231 if (is_egl_buffer(buffer_resource))
232 n_buffers++;
233 }
234#endif /* ENABLE_EGL */
235
Derek Foremanf6a65922015-02-24 09:32:14 -0600236 weston_test_send_n_egl_buffers(resource, n_buffers);
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000237}
238
Derek Foremanf6a65922015-02-24 09:32:14 -0600239static const struct weston_test_interface test_implementation = {
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800240 move_surface,
241 move_pointer,
242 send_button,
243 activate_surface,
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000244 send_key,
245 get_n_buffers,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800246};
247
248static void
249bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id)
250{
251 struct weston_test *test = data;
252 struct wl_resource *resource;
253
Derek Foremanf6a65922015-02-24 09:32:14 -0600254 resource = wl_resource_create(client, &weston_test_interface, 1, id);
Marek Chalupa42ebdda2014-07-11 12:33:02 +0200255 if (!resource) {
256 wl_client_post_no_memory(client);
257 return;
258 }
259
Kristian Høgsberg442a5fa2013-07-03 18:13:33 -0400260 wl_resource_set_implementation(resource,
261 &test_implementation, test, NULL);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800262
263 notify_pointer_position(test, resource);
264}
265
266static void
267idle_launch_client(void *data)
268{
269 struct weston_test *test = data;
270 pid_t pid;
271 sigset_t allsigs;
272 char *path;
273
274 path = getenv("WESTON_TEST_CLIENT_PATH");
275 if (path == NULL)
Pekka Paalanenf72e4792013-11-21 16:23:57 +0200276 return;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800277 pid = fork();
278 if (pid == -1)
279 exit(EXIT_FAILURE);
280 if (pid == 0) {
281 sigfillset(&allsigs);
282 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
283 execl(path, path, NULL);
284 weston_log("compositor: executing '%s' failed: %m\n", path);
285 exit(EXIT_FAILURE);
286 }
287
288 test->process.pid = pid;
289 test->process.cleanup = test_client_sigchld;
290 weston_watch_process(&test->process);
291}
292
293WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500294module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700295 int *argc, char *argv[])
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800296{
297 struct weston_test *test;
298 struct wl_event_loop *loop;
299
Peter Huttererf3d62272013-08-08 11:57:05 +1000300 test = zalloc(sizeof *test);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800301 if (test == NULL)
302 return -1;
303
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800304 test->compositor = ec;
305 weston_layer_init(&test->layer, &ec->cursor_layer.link);
306
Derek Foremanf6a65922015-02-24 09:32:14 -0600307 if (wl_global_create(ec->wl_display, &weston_test_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400308 test, bind_test) == NULL)
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800309 return -1;
310
311 loop = wl_display_get_event_loop(ec->wl_display);
312 wl_event_loop_add_idle(loop, idle_launch_client, test);
313
314 return 0;
315}