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 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
116 uint32_t serial, struct wl_surface *wl_surface,
117 wl_fixed_t x, wl_fixed_t y)
119 struct pointer *pointer = data;
121 pointer->focus = wl_surface_get_user_data(wl_surface);
122 pointer->x = wl_fixed_to_int(x);
123 pointer->y = wl_fixed_to_int(y);
125 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
126 pointer->x, pointer->y, pointer->focus);
130 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
131 uint32_t serial, struct wl_surface *wl_surface)
133 struct pointer *pointer = data;
135 pointer->focus = NULL;
137 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
138 wl_surface_get_user_data(wl_surface));
142 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
143 uint32_t time, wl_fixed_t x, wl_fixed_t y)
145 struct pointer *pointer = data;
147 pointer->x = wl_fixed_to_int(x);
148 pointer->y = wl_fixed_to_int(y);
150 fprintf(stderr, "test-client: got pointer motion %d %d\n",
151 pointer->x, pointer->y);
155 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
156 uint32_t serial, uint32_t time, uint32_t button,
159 struct pointer *pointer = data;
161 pointer->button = button;
162 pointer->state = state;
164 fprintf(stderr, "test-client: got pointer button %u %u\n",
169 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
170 uint32_t time, uint32_t axis, wl_fixed_t value)
172 fprintf(stderr, "test-client: got pointer axis %u %f\n",
173 axis, wl_fixed_to_double(value));
176 static const struct wl_pointer_listener pointer_listener = {
177 pointer_handle_enter,
178 pointer_handle_leave,
179 pointer_handle_motion,
180 pointer_handle_button,
185 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
186 uint32_t format, int fd, uint32_t size)
190 fprintf(stderr, "test-client: got keyboard keymap\n");
194 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
195 uint32_t serial, struct wl_surface *wl_surface,
196 struct wl_array *keys)
198 struct keyboard *keyboard = data;
200 keyboard->focus = wl_surface_get_user_data(wl_surface);
202 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
207 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
208 uint32_t serial, struct wl_surface *wl_surface)
210 struct keyboard *keyboard = data;
212 keyboard->focus = NULL;
214 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
215 wl_surface_get_user_data(wl_surface));
219 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
220 uint32_t serial, uint32_t time, uint32_t key,
223 struct keyboard *keyboard = data;
226 keyboard->state = state;
228 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
232 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
233 uint32_t serial, uint32_t mods_depressed,
234 uint32_t mods_latched, uint32_t mods_locked,
237 struct keyboard *keyboard = data;
239 keyboard->mods_depressed = mods_depressed;
240 keyboard->mods_latched = mods_latched;
241 keyboard->mods_locked = mods_locked;
242 keyboard->group = group;
244 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
245 mods_depressed, mods_latched, mods_locked, group);
248 static const struct wl_keyboard_listener keyboard_listener = {
249 keyboard_handle_keymap,
250 keyboard_handle_enter,
251 keyboard_handle_leave,
253 keyboard_handle_modifiers,
257 surface_enter(void *data,
258 struct wl_surface *wl_surface, struct wl_output *output)
260 struct surface *surface = data;
262 surface->output = wl_output_get_user_data(output);
264 fprintf(stderr, "test-client: got surface enter output %p\n",
269 surface_leave(void *data,
270 struct wl_surface *wl_surface, struct wl_output *output)
272 struct surface *surface = data;
274 surface->output = NULL;
276 fprintf(stderr, "test-client: got surface leave output %p\n",
277 wl_output_get_user_data(output));
280 static const struct wl_surface_listener surface_listener = {
286 create_shm_buffer(struct client *client, int width, int height, void **pixels)
288 struct wl_shm *shm = client->wl_shm;
289 int stride = width * 4;
290 int size = stride * height;
291 struct wl_shm_pool *pool;
292 struct wl_buffer *buffer;
296 fd = os_create_anonymous_file(size);
299 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
300 if (data == MAP_FAILED) {
302 assert(data != MAP_FAILED);
305 pool = wl_shm_create_pool(shm, fd, size);
306 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
307 WL_SHM_FORMAT_ARGB8888);
308 wl_shm_pool_destroy(pool);
319 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
321 struct client *client = data;
323 if (format == WL_SHM_FORMAT_ARGB8888)
324 client->has_argb = 1;
327 struct wl_shm_listener shm_listener = {
332 test_handle_pointer_position(void *data, struct wl_test *wl_test,
333 wl_fixed_t x, wl_fixed_t y)
335 struct test *test = data;
336 test->pointer_x = wl_fixed_to_int(x);
337 test->pointer_y = wl_fixed_to_int(y);
339 fprintf(stderr, "test-client: got global pointer %d %d\n",
340 test->pointer_x, test->pointer_y);
343 static const struct wl_test_listener test_listener = {
344 test_handle_pointer_position
348 seat_handle_capabilities(void *data, struct wl_seat *seat,
349 enum wl_seat_capability caps)
351 struct input *input = data;
352 struct pointer *pointer;
353 struct keyboard *keyboard;
355 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
356 pointer = xzalloc(sizeof *pointer);
357 pointer->wl_pointer = wl_seat_get_pointer(seat);
358 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
359 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
361 input->pointer = pointer;
362 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
363 wl_pointer_destroy(input->pointer->wl_pointer);
364 free(input->pointer);
365 input->pointer = NULL;
368 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
369 keyboard = xzalloc(sizeof *keyboard);
370 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
371 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
372 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
374 input->keyboard = keyboard;
375 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
376 wl_keyboard_destroy(input->keyboard->wl_keyboard);
377 free(input->keyboard);
378 input->keyboard = NULL;
382 static const struct wl_seat_listener seat_listener = {
383 seat_handle_capabilities,
387 output_handle_geometry(void *data,
388 struct wl_output *wl_output,
397 struct output *output = data;
404 output_handle_mode(void *data,
405 struct wl_output *wl_output,
411 struct output *output = data;
413 if (flags & WL_OUTPUT_MODE_CURRENT) {
414 output->width = width;
415 output->height = height;
419 static const struct wl_output_listener output_listener = {
420 output_handle_geometry,
425 handle_global(void *data, struct wl_registry *registry,
426 uint32_t id, const char *interface, uint32_t version)
428 struct client *client = data;
430 struct output *output;
432 struct global *global;
434 global = xzalloc(sizeof *global);
436 global->interface = strdup(interface);
438 global->version = version;
439 wl_list_insert(client->global_list.prev, &global->link);
441 if (strcmp(interface, "wl_compositor") == 0) {
442 client->wl_compositor =
443 wl_registry_bind(registry, id,
444 &wl_compositor_interface, 1);
445 } else if (strcmp(interface, "wl_seat") == 0) {
446 input = xzalloc(sizeof *input);
448 wl_registry_bind(registry, id,
449 &wl_seat_interface, 1);
450 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
451 client->input = input;
452 } else if (strcmp(interface, "wl_shm") == 0) {
454 wl_registry_bind(registry, id,
455 &wl_shm_interface, 1);
456 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
457 } else if (strcmp(interface, "wl_output") == 0) {
458 output = xzalloc(sizeof *output);
460 wl_registry_bind(registry, id,
461 &wl_output_interface, 1);
462 wl_output_add_listener(output->wl_output,
463 &output_listener, output);
464 client->output = output;
465 } else if (strcmp(interface, "wl_test") == 0) {
466 test = xzalloc(sizeof *test);
468 wl_registry_bind(registry, id,
469 &wl_test_interface, 1);
470 wl_test_add_listener(test->wl_test, &test_listener, test);
475 static const struct wl_registry_listener registry_listener = {
480 log_handler(const char *fmt, va_list args)
482 fprintf(stderr, "libwayland: ");
483 vfprintf(stderr, fmt, args);
487 client_create(int x, int y, int width, int height)
489 struct client *client;
490 struct surface *surface;
492 wl_log_set_handler_client(log_handler);
494 /* connect to display */
495 client = xzalloc(sizeof *client);
496 client->wl_display = wl_display_connect(NULL);
497 assert(client->wl_display);
498 wl_list_init(&client->global_list);
500 /* setup registry so we can bind to interfaces */
501 client->wl_registry = wl_display_get_registry(client->wl_display);
502 wl_registry_add_listener(client->wl_registry, ®istry_listener, client);
504 /* trigger global listener */
505 wl_display_dispatch(client->wl_display);
506 wl_display_roundtrip(client->wl_display);
508 /* must have WL_SHM_FORMAT_ARGB32 */
509 assert(client->has_argb);
511 /* must have wl_test interface */
512 assert(client->test);
514 /* must have an output */
515 assert(client->output);
517 /* initialize the client surface */
518 surface = xzalloc(sizeof *surface);
519 surface->wl_surface =
520 wl_compositor_create_surface(client->wl_compositor);
521 assert(surface->wl_surface);
523 wl_surface_add_listener(surface->wl_surface, &surface_listener,
526 client->surface = surface;
527 wl_surface_set_user_data(surface->wl_surface, surface);
529 surface->width = width;
530 surface->height = height;
531 surface->wl_buffer = create_shm_buffer(client, width, height,
534 memset(surface->data, 64, width * height * 4);
536 move_client(client, x, y);