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
31 #include "../shared/os-compatibility.h"
32 #include "weston-test-client-helper.h"
46 surface_contains(struct surface *surface, int x, int y)
48 /* test whether a global x,y point is contained in the surface */
51 int sw = surface->width;
52 int sh = surface->height;
53 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
57 frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
63 wl_callback_destroy(callback);
66 static const struct wl_callback_listener frame_listener = {
67 frame_callback_handler
71 frame_callback_set(struct wl_surface *surface, int *done)
73 struct wl_callback *callback;
76 callback = wl_surface_frame(surface);
77 wl_callback_add_listener(callback, &frame_listener, done);
83 frame_callback_wait(struct client *client, int *done)
86 assert(wl_display_dispatch(client->wl_display) >= 0);
91 move_client(struct client *client, int x, int y)
93 struct surface *surface = client->surface;
96 client->surface->x = x;
97 client->surface->y = y;
98 wl_test_move_surface(client->test->wl_test, surface->wl_surface,
99 surface->x, surface->y);
100 /* The attach here is necessary because commit() will call congfigure
101 * only on surfaces newly attached, and the one that sets the surface
102 * position is the configure. */
103 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
104 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
107 frame_callback_set(surface->wl_surface, &done);
109 wl_surface_commit(surface->wl_surface);
111 frame_callback_wait(client, &done);
115 get_n_egl_buffers(struct client *client)
117 client->test->n_egl_buffers = -1;
119 wl_test_get_n_egl_buffers(client->test->wl_test);
120 wl_display_roundtrip(client->wl_display);
122 return client->test->n_egl_buffers;
126 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
127 uint32_t serial, struct wl_surface *wl_surface,
128 wl_fixed_t x, wl_fixed_t y)
130 struct pointer *pointer = data;
132 pointer->focus = wl_surface_get_user_data(wl_surface);
133 pointer->x = wl_fixed_to_int(x);
134 pointer->y = wl_fixed_to_int(y);
136 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
137 pointer->x, pointer->y, pointer->focus);
141 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
142 uint32_t serial, struct wl_surface *wl_surface)
144 struct pointer *pointer = data;
146 pointer->focus = NULL;
148 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
149 wl_surface_get_user_data(wl_surface));
153 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
154 uint32_t time, wl_fixed_t x, wl_fixed_t y)
156 struct pointer *pointer = data;
158 pointer->x = wl_fixed_to_int(x);
159 pointer->y = wl_fixed_to_int(y);
161 fprintf(stderr, "test-client: got pointer motion %d %d\n",
162 pointer->x, pointer->y);
166 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
167 uint32_t serial, uint32_t time, uint32_t button,
170 struct pointer *pointer = data;
172 pointer->button = button;
173 pointer->state = state;
175 fprintf(stderr, "test-client: got pointer button %u %u\n",
180 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
181 uint32_t time, uint32_t axis, wl_fixed_t value)
183 fprintf(stderr, "test-client: got pointer axis %u %f\n",
184 axis, wl_fixed_to_double(value));
187 static const struct wl_pointer_listener pointer_listener = {
188 pointer_handle_enter,
189 pointer_handle_leave,
190 pointer_handle_motion,
191 pointer_handle_button,
196 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
197 uint32_t format, int fd, uint32_t size)
201 fprintf(stderr, "test-client: got keyboard keymap\n");
205 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
206 uint32_t serial, struct wl_surface *wl_surface,
207 struct wl_array *keys)
209 struct keyboard *keyboard = data;
211 keyboard->focus = wl_surface_get_user_data(wl_surface);
213 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
218 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
219 uint32_t serial, struct wl_surface *wl_surface)
221 struct keyboard *keyboard = data;
223 keyboard->focus = NULL;
225 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
226 wl_surface_get_user_data(wl_surface));
230 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
231 uint32_t serial, uint32_t time, uint32_t key,
234 struct keyboard *keyboard = data;
237 keyboard->state = state;
239 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
243 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
244 uint32_t serial, uint32_t mods_depressed,
245 uint32_t mods_latched, uint32_t mods_locked,
248 struct keyboard *keyboard = data;
250 keyboard->mods_depressed = mods_depressed;
251 keyboard->mods_latched = mods_latched;
252 keyboard->mods_locked = mods_locked;
253 keyboard->group = group;
255 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
256 mods_depressed, mods_latched, mods_locked, group);
259 static const struct wl_keyboard_listener keyboard_listener = {
260 keyboard_handle_keymap,
261 keyboard_handle_enter,
262 keyboard_handle_leave,
264 keyboard_handle_modifiers,
268 surface_enter(void *data,
269 struct wl_surface *wl_surface, struct wl_output *output)
271 struct surface *surface = data;
273 surface->output = wl_output_get_user_data(output);
275 fprintf(stderr, "test-client: got surface enter output %p\n",
280 surface_leave(void *data,
281 struct wl_surface *wl_surface, struct wl_output *output)
283 struct surface *surface = data;
285 surface->output = NULL;
287 fprintf(stderr, "test-client: got surface leave output %p\n",
288 wl_output_get_user_data(output));
291 static const struct wl_surface_listener surface_listener = {
297 create_shm_buffer(struct client *client, int width, int height, void **pixels)
299 struct wl_shm *shm = client->wl_shm;
300 int stride = width * 4;
301 int size = stride * height;
302 struct wl_shm_pool *pool;
303 struct wl_buffer *buffer;
307 fd = os_create_anonymous_file(size);
310 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
311 if (data == MAP_FAILED) {
313 assert(data != MAP_FAILED);
316 pool = wl_shm_create_pool(shm, fd, size);
317 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
318 WL_SHM_FORMAT_ARGB8888);
319 wl_shm_pool_destroy(pool);
330 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
332 struct client *client = data;
334 if (format == WL_SHM_FORMAT_ARGB8888)
335 client->has_argb = 1;
338 struct wl_shm_listener shm_listener = {
343 test_handle_pointer_position(void *data, struct wl_test *wl_test,
344 wl_fixed_t x, wl_fixed_t y)
346 struct test *test = data;
347 test->pointer_x = wl_fixed_to_int(x);
348 test->pointer_y = wl_fixed_to_int(y);
350 fprintf(stderr, "test-client: got global pointer %d %d\n",
351 test->pointer_x, test->pointer_y);
355 test_handle_n_egl_buffers(void *data, struct wl_test *wl_test, uint32_t n)
357 struct test *test = data;
359 test->n_egl_buffers = n;
362 static const struct wl_test_listener test_listener = {
363 test_handle_pointer_position,
364 test_handle_n_egl_buffers,
368 seat_handle_capabilities(void *data, struct wl_seat *seat,
369 enum wl_seat_capability caps)
371 struct input *input = data;
372 struct pointer *pointer;
373 struct keyboard *keyboard;
375 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
376 pointer = xzalloc(sizeof *pointer);
377 pointer->wl_pointer = wl_seat_get_pointer(seat);
378 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
379 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
381 input->pointer = pointer;
382 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
383 wl_pointer_destroy(input->pointer->wl_pointer);
384 free(input->pointer);
385 input->pointer = NULL;
388 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
389 keyboard = xzalloc(sizeof *keyboard);
390 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
391 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
392 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
394 input->keyboard = keyboard;
395 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
396 wl_keyboard_destroy(input->keyboard->wl_keyboard);
397 free(input->keyboard);
398 input->keyboard = NULL;
402 static const struct wl_seat_listener seat_listener = {
403 seat_handle_capabilities,
407 output_handle_geometry(void *data,
408 struct wl_output *wl_output,
417 struct output *output = data;
424 output_handle_mode(void *data,
425 struct wl_output *wl_output,
431 struct output *output = data;
433 if (flags & WL_OUTPUT_MODE_CURRENT) {
434 output->width = width;
435 output->height = height;
439 static const struct wl_output_listener output_listener = {
440 output_handle_geometry,
445 handle_global(void *data, struct wl_registry *registry,
446 uint32_t id, const char *interface, uint32_t version)
448 struct client *client = data;
450 struct output *output;
452 struct global *global;
454 global = xzalloc(sizeof *global);
456 global->interface = strdup(interface);
458 global->version = version;
459 wl_list_insert(client->global_list.prev, &global->link);
461 if (strcmp(interface, "wl_compositor") == 0) {
462 client->wl_compositor =
463 wl_registry_bind(registry, id,
464 &wl_compositor_interface, 1);
465 } else if (strcmp(interface, "wl_seat") == 0) {
466 input = xzalloc(sizeof *input);
468 wl_registry_bind(registry, id,
469 &wl_seat_interface, 1);
470 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
471 client->input = input;
472 } else if (strcmp(interface, "wl_shm") == 0) {
474 wl_registry_bind(registry, id,
475 &wl_shm_interface, 1);
476 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
477 } else if (strcmp(interface, "wl_output") == 0) {
478 output = xzalloc(sizeof *output);
480 wl_registry_bind(registry, id,
481 &wl_output_interface, 1);
482 wl_output_add_listener(output->wl_output,
483 &output_listener, output);
484 client->output = output;
485 } else if (strcmp(interface, "wl_test") == 0) {
486 test = xzalloc(sizeof *test);
488 wl_registry_bind(registry, id,
489 &wl_test_interface, 1);
490 wl_test_add_listener(test->wl_test, &test_listener, test);
495 static const struct wl_registry_listener registry_listener = {
500 skip(const char *fmt, ...)
505 vfprintf(stderr, fmt, argp);
508 /* automake tests uses exit code 77, but we don't have a good
509 * way to make weston exit with that from here. */
514 log_handler(const char *fmt, va_list args)
516 fprintf(stderr, "libwayland: ");
517 vfprintf(stderr, fmt, args);
521 client_create(int x, int y, int width, int height)
523 struct client *client;
524 struct surface *surface;
526 wl_log_set_handler_client(log_handler);
528 /* connect to display */
529 client = xzalloc(sizeof *client);
530 client->wl_display = wl_display_connect(NULL);
531 assert(client->wl_display);
532 wl_list_init(&client->global_list);
534 /* setup registry so we can bind to interfaces */
535 client->wl_registry = wl_display_get_registry(client->wl_display);
536 wl_registry_add_listener(client->wl_registry, ®istry_listener, client);
538 /* trigger global listener */
539 wl_display_dispatch(client->wl_display);
540 wl_display_roundtrip(client->wl_display);
542 /* must have WL_SHM_FORMAT_ARGB32 */
543 assert(client->has_argb);
545 /* must have wl_test interface */
546 assert(client->test);
548 /* must have an output */
549 assert(client->output);
551 /* initialize the client surface */
552 surface = xzalloc(sizeof *surface);
553 surface->wl_surface =
554 wl_compositor_create_surface(client->wl_compositor);
555 assert(surface->wl_surface);
557 wl_surface_add_listener(surface->wl_surface, &surface_listener,
560 client->surface = surface;
561 wl_surface_set_user_data(surface->wl_surface, surface);
563 surface->width = width;
564 surface->height = height;
565 surface->wl_buffer = create_shm_buffer(client, width, height,
568 memset(surface->data, 64, width * height * 4);
570 move_client(client, x, y);