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
29 #include "../shared/os-compatibility.h"
30 #include "weston-test-client-helper.h"
33 surface_contains(struct surface *surface, int x, int y)
35 /* test whether a global x,y point is contained in the surface */
38 int sw = surface->width;
39 int sh = surface->height;
40 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
44 frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
50 wl_callback_destroy(callback);
53 static const struct wl_callback_listener frame_listener = {
54 frame_callback_handler
58 frame_callback_set(struct wl_surface *surface, int *done)
60 struct wl_callback *callback;
63 callback = wl_surface_frame(surface);
64 wl_callback_add_listener(callback, &frame_listener, done);
70 frame_callback_wait(struct client *client, int *done)
73 assert(wl_display_dispatch(client->wl_display) >= 0);
78 move_client(struct client *client, int x, int y)
80 struct surface *surface = client->surface;
83 client->surface->x = x;
84 client->surface->y = y;
85 wl_test_move_surface(client->test->wl_test, surface->wl_surface,
86 surface->x, surface->y);
87 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
90 frame_callback_set(surface->wl_surface, &done);
92 wl_surface_commit(surface->wl_surface);
94 frame_callback_wait(client, &done);
98 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
99 uint32_t serial, struct wl_surface *wl_surface,
100 wl_fixed_t x, wl_fixed_t y)
102 struct pointer *pointer = data;
104 pointer->focus = wl_surface_get_user_data(wl_surface);
105 pointer->x = wl_fixed_to_int(x);
106 pointer->y = wl_fixed_to_int(y);
108 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
109 pointer->x, pointer->y, pointer->focus);
113 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
114 uint32_t serial, struct wl_surface *wl_surface)
116 struct pointer *pointer = data;
118 pointer->focus = NULL;
120 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
121 wl_surface_get_user_data(wl_surface));
125 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
126 uint32_t time, wl_fixed_t x, wl_fixed_t y)
128 struct pointer *pointer = data;
130 pointer->x = wl_fixed_to_int(x);
131 pointer->y = wl_fixed_to_int(y);
133 fprintf(stderr, "test-client: got pointer motion %d %d\n",
134 pointer->x, pointer->y);
138 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
139 uint32_t serial, uint32_t time, uint32_t button,
142 struct pointer *pointer = data;
144 pointer->button = button;
145 pointer->state = state;
147 fprintf(stderr, "test-client: got pointer button %u %u\n",
152 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
153 uint32_t time, uint32_t axis, wl_fixed_t value)
155 fprintf(stderr, "test-client: got pointer axis %u %d\n", axis, value);
158 static const struct wl_pointer_listener pointer_listener = {
159 pointer_handle_enter,
160 pointer_handle_leave,
161 pointer_handle_motion,
162 pointer_handle_button,
167 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
168 uint32_t format, int fd, uint32_t size)
172 fprintf(stderr, "test-client: got keyboard keymap\n");
176 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
177 uint32_t serial, struct wl_surface *wl_surface,
178 struct wl_array *keys)
180 struct keyboard *keyboard = data;
182 keyboard->focus = wl_surface_get_user_data(wl_surface);
184 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
189 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
190 uint32_t serial, struct wl_surface *wl_surface)
192 struct keyboard *keyboard = data;
194 keyboard->focus = NULL;
196 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
197 wl_surface_get_user_data(wl_surface));
201 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
202 uint32_t serial, uint32_t time, uint32_t key,
205 struct keyboard *keyboard = data;
208 keyboard->state = state;
210 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
214 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
215 uint32_t serial, uint32_t mods_depressed,
216 uint32_t mods_latched, uint32_t mods_locked,
219 struct keyboard *keyboard = data;
221 keyboard->mods_depressed = mods_depressed;
222 keyboard->mods_latched = mods_latched;
223 keyboard->mods_locked = mods_locked;
224 keyboard->group = group;
226 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
227 mods_depressed, mods_latched, mods_locked, group);
230 static const struct wl_keyboard_listener keyboard_listener = {
231 keyboard_handle_keymap,
232 keyboard_handle_enter,
233 keyboard_handle_leave,
235 keyboard_handle_modifiers,
239 surface_enter(void *data,
240 struct wl_surface *wl_surface, struct wl_output *output)
242 struct surface *surface = data;
244 surface->output = wl_output_get_user_data(output);
246 fprintf(stderr, "test-client: got surface enter output %p\n",
251 surface_leave(void *data,
252 struct wl_surface *wl_surface, struct wl_output *output)
254 struct surface *surface = data;
256 surface->output = NULL;
258 fprintf(stderr, "test-client: got surface leave output %p\n",
259 wl_output_get_user_data(output));
262 static const struct wl_surface_listener surface_listener = {
268 create_shm_buffer(struct client *client, int width, int height, void **pixels)
270 struct wl_shm *shm = client->wl_shm;
271 int stride = width * 4;
272 int size = stride * height;
273 struct wl_shm_pool *pool;
274 struct wl_buffer *buffer;
278 fd = os_create_anonymous_file(size);
281 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
282 if (data == MAP_FAILED) {
284 assert(data != MAP_FAILED);
287 pool = wl_shm_create_pool(shm, fd, size);
288 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
289 WL_SHM_FORMAT_ARGB8888);
290 wl_shm_pool_destroy(pool);
301 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
303 struct client *client = data;
305 if (format == WL_SHM_FORMAT_ARGB8888)
306 client->has_argb = 1;
309 struct wl_shm_listener shm_listener = {
314 test_handle_pointer_position(void *data, struct wl_test *wl_test,
315 wl_fixed_t x, wl_fixed_t y)
317 struct test *test = data;
318 test->pointer_x = wl_fixed_to_int(x);
319 test->pointer_y = wl_fixed_to_int(y);
321 fprintf(stderr, "test-client: got global pointer %d %d\n",
322 test->pointer_x, test->pointer_y);
325 static const struct wl_test_listener test_listener = {
326 test_handle_pointer_position
330 seat_handle_capabilities(void *data, struct wl_seat *seat,
331 enum wl_seat_capability caps)
333 struct input *input = data;
334 struct pointer *pointer;
335 struct keyboard *keyboard;
337 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
338 pointer = calloc(1, sizeof *pointer);
339 pointer->wl_pointer = wl_seat_get_pointer(seat);
340 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
341 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
343 input->pointer = pointer;
344 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
345 wl_pointer_destroy(input->pointer->wl_pointer);
346 free(input->pointer);
347 input->pointer = NULL;
350 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
351 keyboard = calloc(1, sizeof *keyboard);
352 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
353 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
354 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
356 input->keyboard = keyboard;
357 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
358 wl_keyboard_destroy(input->keyboard->wl_keyboard);
359 free(input->keyboard);
360 input->keyboard = NULL;
364 static const struct wl_seat_listener seat_listener = {
365 seat_handle_capabilities,
369 output_handle_geometry(void *data,
370 struct wl_output *wl_output,
379 struct output *output = data;
386 output_handle_mode(void *data,
387 struct wl_output *wl_output,
393 struct output *output = data;
395 if (flags & WL_OUTPUT_MODE_CURRENT) {
396 output->width = width;
397 output->height = height;
401 static const struct wl_output_listener output_listener = {
402 output_handle_geometry,
407 handle_global(void *data, struct wl_registry *registry,
408 uint32_t id, const char *interface, uint32_t version)
410 struct client *client = data;
412 struct output *output;
414 struct global *global;
416 global = malloc(sizeof *global);
419 global->interface = strdup(interface);
421 global->version = version;
422 wl_list_insert(client->global_list.prev, &global->link);
424 if (strcmp(interface, "wl_compositor") == 0) {
425 client->wl_compositor =
426 wl_registry_bind(registry, id,
427 &wl_compositor_interface, 1);
428 } else if (strcmp(interface, "wl_seat") == 0) {
429 input = calloc(1, sizeof *input);
431 wl_registry_bind(registry, id,
432 &wl_seat_interface, 1);
433 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
434 client->input = input;
435 } else if (strcmp(interface, "wl_shm") == 0) {
437 wl_registry_bind(registry, id,
438 &wl_shm_interface, 1);
439 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
440 } else if (strcmp(interface, "wl_output") == 0) {
441 output = malloc(sizeof *output);
443 wl_registry_bind(registry, id,
444 &wl_output_interface, 1);
445 wl_output_add_listener(output->wl_output,
446 &output_listener, output);
447 client->output = output;
448 } else if (strcmp(interface, "wl_test") == 0) {
449 test = calloc(1, sizeof *test);
451 wl_registry_bind(registry, id,
452 &wl_test_interface, 1);
453 wl_test_add_listener(test->wl_test, &test_listener, test);
458 static const struct wl_registry_listener registry_listener = {
463 log_handler(const char *fmt, va_list args)
465 fprintf(stderr, "libwayland: ");
466 vfprintf(stderr, fmt, args);
470 client_create(int x, int y, int width, int height)
472 struct client *client;
473 struct surface *surface;
475 wl_log_set_handler_client(log_handler);
477 /* connect to display */
478 client = calloc(1, sizeof *client);
479 client->wl_display = wl_display_connect(NULL);
480 assert(client->wl_display);
481 wl_list_init(&client->global_list);
483 /* setup registry so we can bind to interfaces */
484 client->wl_registry = wl_display_get_registry(client->wl_display);
485 wl_registry_add_listener(client->wl_registry, ®istry_listener, client);
487 /* trigger global listener */
488 wl_display_dispatch(client->wl_display);
489 wl_display_roundtrip(client->wl_display);
491 /* must have WL_SHM_FORMAT_ARGB32 */
492 assert(client->has_argb);
494 /* must have wl_test interface */
495 assert(client->test);
497 /* must have an output */
498 assert(client->output);
500 /* initialize the client surface */
501 surface = calloc(1, sizeof *surface);
503 surface->wl_surface =
504 wl_compositor_create_surface(client->wl_compositor);
505 assert(surface->wl_surface);
507 wl_surface_add_listener(surface->wl_surface, &surface_listener,
510 client->surface = surface;
511 wl_surface_set_user_data(surface->wl_surface, surface);
513 surface->width = width;
514 surface->height = height;
515 surface->wl_buffer = create_shm_buffer(client, width, height,
518 memset(surface->data, 64, width * height * 4);
519 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
521 move_client(client, x, y);