2 * Copyright © 2012 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
32 #include "../shared/os-compatibility.h"
33 #include "weston-test-client-helper.h"
47 surface_contains(struct surface *surface, int x, int y)
49 /* test whether a global x,y point is contained in the surface */
52 int sw = surface->width;
53 int sh = surface->height;
54 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
58 frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
64 wl_callback_destroy(callback);
67 static const struct wl_callback_listener frame_listener = {
68 frame_callback_handler
72 frame_callback_set(struct wl_surface *surface, int *done)
74 struct wl_callback *callback;
77 callback = wl_surface_frame(surface);
78 wl_callback_add_listener(callback, &frame_listener, done);
84 frame_callback_wait_nofail(struct client *client, int *done)
87 if (wl_display_dispatch(client->wl_display) < 0)
95 move_client(struct client *client, int x, int y)
97 struct surface *surface = client->surface;
100 client->surface->x = x;
101 client->surface->y = y;
102 wl_test_move_surface(client->test->wl_test, surface->wl_surface,
103 surface->x, surface->y);
104 /* The attach here is necessary because commit() will call congfigure
105 * only on surfaces newly attached, and the one that sets the surface
106 * position is the configure. */
107 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
108 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
111 frame_callback_set(surface->wl_surface, &done);
113 wl_surface_commit(surface->wl_surface);
115 frame_callback_wait(client, &done);
119 get_n_egl_buffers(struct client *client)
121 client->test->n_egl_buffers = -1;
123 wl_test_get_n_egl_buffers(client->test->wl_test);
124 wl_display_roundtrip(client->wl_display);
126 return client->test->n_egl_buffers;
130 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
131 uint32_t serial, struct wl_surface *wl_surface,
132 wl_fixed_t x, wl_fixed_t y)
134 struct pointer *pointer = data;
136 pointer->focus = wl_surface_get_user_data(wl_surface);
137 pointer->x = wl_fixed_to_int(x);
138 pointer->y = wl_fixed_to_int(y);
140 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
141 pointer->x, pointer->y, pointer->focus);
145 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
146 uint32_t serial, struct wl_surface *wl_surface)
148 struct pointer *pointer = data;
150 pointer->focus = NULL;
152 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
153 wl_surface_get_user_data(wl_surface));
157 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
158 uint32_t time, wl_fixed_t x, wl_fixed_t y)
160 struct pointer *pointer = data;
162 pointer->x = wl_fixed_to_int(x);
163 pointer->y = wl_fixed_to_int(y);
165 fprintf(stderr, "test-client: got pointer motion %d %d\n",
166 pointer->x, pointer->y);
170 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
171 uint32_t serial, uint32_t time, uint32_t button,
174 struct pointer *pointer = data;
176 pointer->button = button;
177 pointer->state = state;
179 fprintf(stderr, "test-client: got pointer button %u %u\n",
184 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
185 uint32_t time, uint32_t axis, wl_fixed_t value)
187 fprintf(stderr, "test-client: got pointer axis %u %f\n",
188 axis, wl_fixed_to_double(value));
191 static const struct wl_pointer_listener pointer_listener = {
192 pointer_handle_enter,
193 pointer_handle_leave,
194 pointer_handle_motion,
195 pointer_handle_button,
200 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
201 uint32_t format, int fd, uint32_t size)
205 fprintf(stderr, "test-client: got keyboard keymap\n");
209 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
210 uint32_t serial, struct wl_surface *wl_surface,
211 struct wl_array *keys)
213 struct keyboard *keyboard = data;
215 keyboard->focus = wl_surface_get_user_data(wl_surface);
217 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
222 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
223 uint32_t serial, struct wl_surface *wl_surface)
225 struct keyboard *keyboard = data;
227 keyboard->focus = NULL;
229 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
230 wl_surface_get_user_data(wl_surface));
234 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
235 uint32_t serial, uint32_t time, uint32_t key,
238 struct keyboard *keyboard = data;
241 keyboard->state = state;
243 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
247 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
248 uint32_t serial, uint32_t mods_depressed,
249 uint32_t mods_latched, uint32_t mods_locked,
252 struct keyboard *keyboard = data;
254 keyboard->mods_depressed = mods_depressed;
255 keyboard->mods_latched = mods_latched;
256 keyboard->mods_locked = mods_locked;
257 keyboard->group = group;
259 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
260 mods_depressed, mods_latched, mods_locked, group);
263 static const struct wl_keyboard_listener keyboard_listener = {
264 keyboard_handle_keymap,
265 keyboard_handle_enter,
266 keyboard_handle_leave,
268 keyboard_handle_modifiers,
272 surface_enter(void *data,
273 struct wl_surface *wl_surface, struct wl_output *output)
275 struct surface *surface = data;
277 surface->output = wl_output_get_user_data(output);
279 fprintf(stderr, "test-client: got surface enter output %p\n",
284 surface_leave(void *data,
285 struct wl_surface *wl_surface, struct wl_output *output)
287 struct surface *surface = data;
289 surface->output = NULL;
291 fprintf(stderr, "test-client: got surface leave output %p\n",
292 wl_output_get_user_data(output));
295 static const struct wl_surface_listener surface_listener = {
301 create_shm_buffer(struct client *client, int width, int height, void **pixels)
303 struct wl_shm *shm = client->wl_shm;
304 int stride = width * 4;
305 int size = stride * height;
306 struct wl_shm_pool *pool;
307 struct wl_buffer *buffer;
311 fd = os_create_anonymous_file(size);
314 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
315 if (data == MAP_FAILED) {
317 assert(data != MAP_FAILED);
320 pool = wl_shm_create_pool(shm, fd, size);
321 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
322 WL_SHM_FORMAT_ARGB8888);
323 wl_shm_pool_destroy(pool);
334 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
336 struct client *client = data;
338 if (format == WL_SHM_FORMAT_ARGB8888)
339 client->has_argb = 1;
342 struct wl_shm_listener shm_listener = {
347 test_handle_pointer_position(void *data, struct wl_test *wl_test,
348 wl_fixed_t x, wl_fixed_t y)
350 struct test *test = data;
351 test->pointer_x = wl_fixed_to_int(x);
352 test->pointer_y = wl_fixed_to_int(y);
354 fprintf(stderr, "test-client: got global pointer %d %d\n",
355 test->pointer_x, test->pointer_y);
359 test_handle_n_egl_buffers(void *data, struct wl_test *wl_test, uint32_t n)
361 struct test *test = data;
363 test->n_egl_buffers = n;
366 static const struct wl_test_listener test_listener = {
367 test_handle_pointer_position,
368 test_handle_n_egl_buffers,
372 seat_handle_capabilities(void *data, struct wl_seat *seat,
373 enum wl_seat_capability caps)
375 struct input *input = data;
376 struct pointer *pointer;
377 struct keyboard *keyboard;
379 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
380 pointer = xzalloc(sizeof *pointer);
381 pointer->wl_pointer = wl_seat_get_pointer(seat);
382 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
383 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
385 input->pointer = pointer;
386 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
387 wl_pointer_destroy(input->pointer->wl_pointer);
388 free(input->pointer);
389 input->pointer = NULL;
392 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
393 keyboard = xzalloc(sizeof *keyboard);
394 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
395 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
396 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
398 input->keyboard = keyboard;
399 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
400 wl_keyboard_destroy(input->keyboard->wl_keyboard);
401 free(input->keyboard);
402 input->keyboard = NULL;
406 static const struct wl_seat_listener seat_listener = {
407 seat_handle_capabilities,
411 output_handle_geometry(void *data,
412 struct wl_output *wl_output,
421 struct output *output = data;
428 output_handle_mode(void *data,
429 struct wl_output *wl_output,
435 struct output *output = data;
437 if (flags & WL_OUTPUT_MODE_CURRENT) {
438 output->width = width;
439 output->height = height;
443 static const struct wl_output_listener output_listener = {
444 output_handle_geometry,
449 handle_global(void *data, struct wl_registry *registry,
450 uint32_t id, const char *interface, uint32_t version)
452 struct client *client = data;
454 struct output *output;
456 struct global *global;
458 global = xzalloc(sizeof *global);
460 global->interface = strdup(interface);
462 global->version = version;
463 wl_list_insert(client->global_list.prev, &global->link);
465 if (strcmp(interface, "wl_compositor") == 0) {
466 client->wl_compositor =
467 wl_registry_bind(registry, id,
468 &wl_compositor_interface, 1);
469 } else if (strcmp(interface, "wl_seat") == 0) {
470 input = xzalloc(sizeof *input);
472 wl_registry_bind(registry, id,
473 &wl_seat_interface, 1);
474 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
475 client->input = input;
476 } else if (strcmp(interface, "wl_shm") == 0) {
478 wl_registry_bind(registry, id,
479 &wl_shm_interface, 1);
480 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
481 } else if (strcmp(interface, "wl_output") == 0) {
482 output = xzalloc(sizeof *output);
484 wl_registry_bind(registry, id,
485 &wl_output_interface, 1);
486 wl_output_add_listener(output->wl_output,
487 &output_listener, output);
488 client->output = output;
489 } else if (strcmp(interface, "wl_test") == 0) {
490 test = xzalloc(sizeof *test);
492 wl_registry_bind(registry, id,
493 &wl_test_interface, 1);
494 wl_test_add_listener(test->wl_test, &test_listener, test);
499 static const struct wl_registry_listener registry_listener = {
504 skip(const char *fmt, ...)
509 vfprintf(stderr, fmt, argp);
512 /* automake tests uses exit code 77. weston-test-runner will see
513 * this and use it, and then weston-test's sigchld handler (in the
514 * weston process) will use that as an exit status, which is what
515 * automake will see in the end. */
520 expect_protocol_error(struct client *client,
521 const struct wl_interface *intf,
525 uint32_t errcode, failed = 0;
526 const struct wl_interface *interface;
529 /* if the error has not come yet, make it happen */
530 wl_display_roundtrip(client->wl_display);
532 err = wl_display_get_error(client->wl_display);
534 assert(err && "Expected protocol error but nothing came");
535 assert(err == EPROTO && "Expected protocol error but got local error");
537 errcode = wl_display_get_protocol_error(client->wl_display,
541 if (errcode != code) {
542 fprintf(stderr, "Should get error code %d but got %d\n",
547 /* this should be definitely set */
550 if (strcmp(intf->name, interface->name) != 0) {
551 fprintf(stderr, "Should get interface '%s' but got '%s'\n",
552 intf->name, interface->name);
557 fprintf(stderr, "Expected other protocol error\n");
562 fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) "
563 "with code %d\n", interface->name, id, errcode);
567 log_handler(const char *fmt, va_list args)
569 fprintf(stderr, "libwayland: ");
570 vfprintf(stderr, fmt, args);
574 client_create(int x, int y, int width, int height)
576 struct client *client;
577 struct surface *surface;
579 wl_log_set_handler_client(log_handler);
581 /* connect to display */
582 client = xzalloc(sizeof *client);
583 client->wl_display = wl_display_connect(NULL);
584 assert(client->wl_display);
585 wl_list_init(&client->global_list);
587 /* setup registry so we can bind to interfaces */
588 client->wl_registry = wl_display_get_registry(client->wl_display);
589 wl_registry_add_listener(client->wl_registry, ®istry_listener, client);
591 /* trigger global listener */
592 wl_display_dispatch(client->wl_display);
593 wl_display_roundtrip(client->wl_display);
595 /* must have WL_SHM_FORMAT_ARGB32 */
596 assert(client->has_argb);
598 /* must have wl_test interface */
599 assert(client->test);
601 /* must have an output */
602 assert(client->output);
604 /* initialize the client surface */
605 surface = xzalloc(sizeof *surface);
606 surface->wl_surface =
607 wl_compositor_create_surface(client->wl_compositor);
608 assert(surface->wl_surface);
610 wl_surface_add_listener(surface->wl_surface, &surface_listener,
613 client->surface = surface;
614 wl_surface_set_user_data(surface->wl_surface, surface);
616 surface->width = width;
617 surface->height = height;
618 surface->wl_buffer = create_shm_buffer(client, width, height,
621 memset(surface->data, 64, width * height * 4);
623 move_client(client, x, y);