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"
35 surface_contains(struct surface *surface, int x, int y)
37 /* test whether a global x,y point is contained in the surface */
40 int sw = surface->width;
41 int sh = surface->height;
42 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
46 frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
52 wl_callback_destroy(callback);
55 static const struct wl_callback_listener frame_listener = {
56 frame_callback_handler
60 frame_callback_set(struct wl_surface *surface, int *done)
62 struct wl_callback *callback;
65 callback = wl_surface_frame(surface);
66 wl_callback_add_listener(callback, &frame_listener, done);
72 frame_callback_wait(struct client *client, int *done)
75 assert(wl_display_dispatch(client->wl_display) >= 0);
80 move_client(struct client *client, int x, int y)
82 struct surface *surface = client->surface;
85 client->surface->x = x;
86 client->surface->y = y;
87 wl_test_move_surface(client->test->wl_test, surface->wl_surface,
88 surface->x, surface->y);
89 /* The attach here is necessary because commit() will call congfigure
90 * only on surfaces newly attached, and the one that sets the surface
91 * position is the configure. */
92 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
93 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
96 frame_callback_set(surface->wl_surface, &done);
98 wl_surface_commit(surface->wl_surface);
100 frame_callback_wait(client, &done);
104 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
105 uint32_t serial, struct wl_surface *wl_surface,
106 wl_fixed_t x, wl_fixed_t y)
108 struct pointer *pointer = data;
110 pointer->focus = wl_surface_get_user_data(wl_surface);
111 pointer->x = wl_fixed_to_int(x);
112 pointer->y = wl_fixed_to_int(y);
114 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
115 pointer->x, pointer->y, pointer->focus);
119 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
120 uint32_t serial, struct wl_surface *wl_surface)
122 struct pointer *pointer = data;
124 pointer->focus = NULL;
126 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
127 wl_surface_get_user_data(wl_surface));
131 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
132 uint32_t time, wl_fixed_t x, wl_fixed_t y)
134 struct pointer *pointer = data;
136 pointer->x = wl_fixed_to_int(x);
137 pointer->y = wl_fixed_to_int(y);
139 fprintf(stderr, "test-client: got pointer motion %d %d\n",
140 pointer->x, pointer->y);
144 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
145 uint32_t serial, uint32_t time, uint32_t button,
148 struct pointer *pointer = data;
150 pointer->button = button;
151 pointer->state = state;
153 fprintf(stderr, "test-client: got pointer button %u %u\n",
158 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
159 uint32_t time, uint32_t axis, wl_fixed_t value)
161 fprintf(stderr, "test-client: got pointer axis %u %d\n", axis, value);
164 static const struct wl_pointer_listener pointer_listener = {
165 pointer_handle_enter,
166 pointer_handle_leave,
167 pointer_handle_motion,
168 pointer_handle_button,
173 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
174 uint32_t format, int fd, uint32_t size)
178 fprintf(stderr, "test-client: got keyboard keymap\n");
182 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
183 uint32_t serial, struct wl_surface *wl_surface,
184 struct wl_array *keys)
186 struct keyboard *keyboard = data;
188 keyboard->focus = wl_surface_get_user_data(wl_surface);
190 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
195 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
196 uint32_t serial, struct wl_surface *wl_surface)
198 struct keyboard *keyboard = data;
200 keyboard->focus = NULL;
202 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
203 wl_surface_get_user_data(wl_surface));
207 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
208 uint32_t serial, uint32_t time, uint32_t key,
211 struct keyboard *keyboard = data;
214 keyboard->state = state;
216 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
220 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
221 uint32_t serial, uint32_t mods_depressed,
222 uint32_t mods_latched, uint32_t mods_locked,
225 struct keyboard *keyboard = data;
227 keyboard->mods_depressed = mods_depressed;
228 keyboard->mods_latched = mods_latched;
229 keyboard->mods_locked = mods_locked;
230 keyboard->group = group;
232 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
233 mods_depressed, mods_latched, mods_locked, group);
236 static const struct wl_keyboard_listener keyboard_listener = {
237 keyboard_handle_keymap,
238 keyboard_handle_enter,
239 keyboard_handle_leave,
241 keyboard_handle_modifiers,
245 surface_enter(void *data,
246 struct wl_surface *wl_surface, struct wl_output *output)
248 struct surface *surface = data;
250 surface->output = wl_output_get_user_data(output);
252 fprintf(stderr, "test-client: got surface enter output %p\n",
257 surface_leave(void *data,
258 struct wl_surface *wl_surface, struct wl_output *output)
260 struct surface *surface = data;
262 surface->output = NULL;
264 fprintf(stderr, "test-client: got surface leave output %p\n",
265 wl_output_get_user_data(output));
268 static const struct wl_surface_listener surface_listener = {
274 create_shm_buffer(struct client *client, int width, int height, void **pixels)
276 struct wl_shm *shm = client->wl_shm;
277 int stride = width * 4;
278 int size = stride * height;
279 struct wl_shm_pool *pool;
280 struct wl_buffer *buffer;
284 fd = os_create_anonymous_file(size);
287 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
288 if (data == MAP_FAILED) {
290 assert(data != MAP_FAILED);
293 pool = wl_shm_create_pool(shm, fd, size);
294 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
295 WL_SHM_FORMAT_ARGB8888);
296 wl_shm_pool_destroy(pool);
307 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
309 struct client *client = data;
311 if (format == WL_SHM_FORMAT_ARGB8888)
312 client->has_argb = 1;
315 struct wl_shm_listener shm_listener = {
320 test_handle_pointer_position(void *data, struct wl_test *wl_test,
321 wl_fixed_t x, wl_fixed_t y)
323 struct test *test = data;
324 test->pointer_x = wl_fixed_to_int(x);
325 test->pointer_y = wl_fixed_to_int(y);
327 fprintf(stderr, "test-client: got global pointer %d %d\n",
328 test->pointer_x, test->pointer_y);
331 static const struct wl_test_listener test_listener = {
332 test_handle_pointer_position
336 seat_handle_capabilities(void *data, struct wl_seat *seat,
337 enum wl_seat_capability caps)
339 struct input *input = data;
340 struct pointer *pointer;
341 struct keyboard *keyboard;
343 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
344 pointer = calloc(1, sizeof *pointer);
345 pointer->wl_pointer = wl_seat_get_pointer(seat);
346 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
347 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
349 input->pointer = pointer;
350 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
351 wl_pointer_destroy(input->pointer->wl_pointer);
352 free(input->pointer);
353 input->pointer = NULL;
356 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
357 keyboard = calloc(1, sizeof *keyboard);
358 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
359 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
360 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
362 input->keyboard = keyboard;
363 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
364 wl_keyboard_destroy(input->keyboard->wl_keyboard);
365 free(input->keyboard);
366 input->keyboard = NULL;
370 static const struct wl_seat_listener seat_listener = {
371 seat_handle_capabilities,
375 output_handle_geometry(void *data,
376 struct wl_output *wl_output,
385 struct output *output = data;
392 output_handle_mode(void *data,
393 struct wl_output *wl_output,
399 struct output *output = data;
401 if (flags & WL_OUTPUT_MODE_CURRENT) {
402 output->width = width;
403 output->height = height;
407 static const struct wl_output_listener output_listener = {
408 output_handle_geometry,
413 handle_global(void *data, struct wl_registry *registry,
414 uint32_t id, const char *interface, uint32_t version)
416 struct client *client = data;
418 struct output *output;
420 struct global *global;
422 global = malloc(sizeof *global);
425 global->interface = strdup(interface);
427 global->version = version;
428 wl_list_insert(client->global_list.prev, &global->link);
430 if (strcmp(interface, "wl_compositor") == 0) {
431 client->wl_compositor =
432 wl_registry_bind(registry, id,
433 &wl_compositor_interface, 1);
434 } else if (strcmp(interface, "wl_seat") == 0) {
435 input = calloc(1, sizeof *input);
437 wl_registry_bind(registry, id,
438 &wl_seat_interface, 1);
439 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
440 client->input = input;
441 } else if (strcmp(interface, "wl_shm") == 0) {
443 wl_registry_bind(registry, id,
444 &wl_shm_interface, 1);
445 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
446 } else if (strcmp(interface, "wl_output") == 0) {
447 output = malloc(sizeof *output);
449 wl_registry_bind(registry, id,
450 &wl_output_interface, 1);
451 wl_output_add_listener(output->wl_output,
452 &output_listener, output);
453 client->output = output;
454 } else if (strcmp(interface, "wl_test") == 0) {
455 test = calloc(1, sizeof *test);
457 wl_registry_bind(registry, id,
458 &wl_test_interface, 1);
459 wl_test_add_listener(test->wl_test, &test_listener, test);
464 static const struct wl_registry_listener registry_listener = {
469 log_handler(const char *fmt, va_list args)
471 fprintf(stderr, "libwayland: ");
472 vfprintf(stderr, fmt, args);
476 client_create(int x, int y, int width, int height)
478 struct client *client;
479 struct surface *surface;
481 wl_log_set_handler_client(log_handler);
483 /* connect to display */
484 client = calloc(1, sizeof *client);
485 client->wl_display = wl_display_connect(NULL);
486 assert(client->wl_display);
487 wl_list_init(&client->global_list);
489 /* setup registry so we can bind to interfaces */
490 client->wl_registry = wl_display_get_registry(client->wl_display);
491 wl_registry_add_listener(client->wl_registry, ®istry_listener, client);
493 /* trigger global listener */
494 wl_display_dispatch(client->wl_display);
495 wl_display_roundtrip(client->wl_display);
497 /* must have WL_SHM_FORMAT_ARGB32 */
498 assert(client->has_argb);
500 /* must have wl_test interface */
501 assert(client->test);
503 /* must have an output */
504 assert(client->output);
506 /* initialize the client surface */
507 surface = calloc(1, sizeof *surface);
509 surface->wl_surface =
510 wl_compositor_create_surface(client->wl_compositor);
511 assert(surface->wl_surface);
513 wl_surface_add_listener(surface->wl_surface, &surface_listener,
516 client->surface = surface;
517 wl_surface_set_user_data(surface->wl_surface, surface);
519 surface->width = width;
520 surface->height = height;
521 surface->wl_buffer = create_shm_buffer(client, width, height,
524 memset(surface->data, 64, width * height * 4);
526 move_client(client, x, y);