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 /* The attach here is necessary because commit() will call congfigure
88 * only on surfaces newly attached, and the one that sets the surface
89 * position is the configure. */
90 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
91 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
94 frame_callback_set(surface->wl_surface, &done);
96 wl_surface_commit(surface->wl_surface);
98 frame_callback_wait(client, &done);
102 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
103 uint32_t serial, struct wl_surface *wl_surface,
104 wl_fixed_t x, wl_fixed_t y)
106 struct pointer *pointer = data;
108 pointer->focus = wl_surface_get_user_data(wl_surface);
109 pointer->x = wl_fixed_to_int(x);
110 pointer->y = wl_fixed_to_int(y);
112 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
113 pointer->x, pointer->y, pointer->focus);
117 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
118 uint32_t serial, struct wl_surface *wl_surface)
120 struct pointer *pointer = data;
122 pointer->focus = NULL;
124 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
125 wl_surface_get_user_data(wl_surface));
129 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
130 uint32_t time, wl_fixed_t x, wl_fixed_t y)
132 struct pointer *pointer = data;
134 pointer->x = wl_fixed_to_int(x);
135 pointer->y = wl_fixed_to_int(y);
137 fprintf(stderr, "test-client: got pointer motion %d %d\n",
138 pointer->x, pointer->y);
142 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
143 uint32_t serial, uint32_t time, uint32_t button,
146 struct pointer *pointer = data;
148 pointer->button = button;
149 pointer->state = state;
151 fprintf(stderr, "test-client: got pointer button %u %u\n",
156 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
157 uint32_t time, uint32_t axis, wl_fixed_t value)
159 fprintf(stderr, "test-client: got pointer axis %u %d\n", axis, value);
162 static const struct wl_pointer_listener pointer_listener = {
163 pointer_handle_enter,
164 pointer_handle_leave,
165 pointer_handle_motion,
166 pointer_handle_button,
171 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
172 uint32_t format, int fd, uint32_t size)
176 fprintf(stderr, "test-client: got keyboard keymap\n");
180 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
181 uint32_t serial, struct wl_surface *wl_surface,
182 struct wl_array *keys)
184 struct keyboard *keyboard = data;
186 keyboard->focus = wl_surface_get_user_data(wl_surface);
188 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
193 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
194 uint32_t serial, struct wl_surface *wl_surface)
196 struct keyboard *keyboard = data;
198 keyboard->focus = NULL;
200 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
201 wl_surface_get_user_data(wl_surface));
205 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
206 uint32_t serial, uint32_t time, uint32_t key,
209 struct keyboard *keyboard = data;
212 keyboard->state = state;
214 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
218 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
219 uint32_t serial, uint32_t mods_depressed,
220 uint32_t mods_latched, uint32_t mods_locked,
223 struct keyboard *keyboard = data;
225 keyboard->mods_depressed = mods_depressed;
226 keyboard->mods_latched = mods_latched;
227 keyboard->mods_locked = mods_locked;
228 keyboard->group = group;
230 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
231 mods_depressed, mods_latched, mods_locked, group);
234 static const struct wl_keyboard_listener keyboard_listener = {
235 keyboard_handle_keymap,
236 keyboard_handle_enter,
237 keyboard_handle_leave,
239 keyboard_handle_modifiers,
243 surface_enter(void *data,
244 struct wl_surface *wl_surface, struct wl_output *output)
246 struct surface *surface = data;
248 surface->output = wl_output_get_user_data(output);
250 fprintf(stderr, "test-client: got surface enter output %p\n",
255 surface_leave(void *data,
256 struct wl_surface *wl_surface, struct wl_output *output)
258 struct surface *surface = data;
260 surface->output = NULL;
262 fprintf(stderr, "test-client: got surface leave output %p\n",
263 wl_output_get_user_data(output));
266 static const struct wl_surface_listener surface_listener = {
272 create_shm_buffer(struct client *client, int width, int height, void **pixels)
274 struct wl_shm *shm = client->wl_shm;
275 int stride = width * 4;
276 int size = stride * height;
277 struct wl_shm_pool *pool;
278 struct wl_buffer *buffer;
282 fd = os_create_anonymous_file(size);
285 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
286 if (data == MAP_FAILED) {
288 assert(data != MAP_FAILED);
291 pool = wl_shm_create_pool(shm, fd, size);
292 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
293 WL_SHM_FORMAT_ARGB8888);
294 wl_shm_pool_destroy(pool);
305 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
307 struct client *client = data;
309 if (format == WL_SHM_FORMAT_ARGB8888)
310 client->has_argb = 1;
313 struct wl_shm_listener shm_listener = {
318 test_handle_pointer_position(void *data, struct wl_test *wl_test,
319 wl_fixed_t x, wl_fixed_t y)
321 struct test *test = data;
322 test->pointer_x = wl_fixed_to_int(x);
323 test->pointer_y = wl_fixed_to_int(y);
325 fprintf(stderr, "test-client: got global pointer %d %d\n",
326 test->pointer_x, test->pointer_y);
329 static const struct wl_test_listener test_listener = {
330 test_handle_pointer_position
334 seat_handle_capabilities(void *data, struct wl_seat *seat,
335 enum wl_seat_capability caps)
337 struct input *input = data;
338 struct pointer *pointer;
339 struct keyboard *keyboard;
341 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
342 pointer = calloc(1, sizeof *pointer);
343 pointer->wl_pointer = wl_seat_get_pointer(seat);
344 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
345 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
347 input->pointer = pointer;
348 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
349 wl_pointer_destroy(input->pointer->wl_pointer);
350 free(input->pointer);
351 input->pointer = NULL;
354 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
355 keyboard = calloc(1, sizeof *keyboard);
356 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
357 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
358 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
360 input->keyboard = keyboard;
361 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
362 wl_keyboard_destroy(input->keyboard->wl_keyboard);
363 free(input->keyboard);
364 input->keyboard = NULL;
368 static const struct wl_seat_listener seat_listener = {
369 seat_handle_capabilities,
373 output_handle_geometry(void *data,
374 struct wl_output *wl_output,
383 struct output *output = data;
390 output_handle_mode(void *data,
391 struct wl_output *wl_output,
397 struct output *output = data;
399 if (flags & WL_OUTPUT_MODE_CURRENT) {
400 output->width = width;
401 output->height = height;
405 static const struct wl_output_listener output_listener = {
406 output_handle_geometry,
411 handle_global(void *data, struct wl_registry *registry,
412 uint32_t id, const char *interface, uint32_t version)
414 struct client *client = data;
416 struct output *output;
418 struct global *global;
420 global = malloc(sizeof *global);
423 global->interface = strdup(interface);
425 global->version = version;
426 wl_list_insert(client->global_list.prev, &global->link);
428 if (strcmp(interface, "wl_compositor") == 0) {
429 client->wl_compositor =
430 wl_registry_bind(registry, id,
431 &wl_compositor_interface, 1);
432 } else if (strcmp(interface, "wl_seat") == 0) {
433 input = calloc(1, sizeof *input);
435 wl_registry_bind(registry, id,
436 &wl_seat_interface, 1);
437 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
438 client->input = input;
439 } else if (strcmp(interface, "wl_shm") == 0) {
441 wl_registry_bind(registry, id,
442 &wl_shm_interface, 1);
443 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
444 } else if (strcmp(interface, "wl_output") == 0) {
445 output = malloc(sizeof *output);
447 wl_registry_bind(registry, id,
448 &wl_output_interface, 1);
449 wl_output_add_listener(output->wl_output,
450 &output_listener, output);
451 client->output = output;
452 } else if (strcmp(interface, "wl_test") == 0) {
453 test = calloc(1, sizeof *test);
455 wl_registry_bind(registry, id,
456 &wl_test_interface, 1);
457 wl_test_add_listener(test->wl_test, &test_listener, test);
462 static const struct wl_registry_listener registry_listener = {
467 log_handler(const char *fmt, va_list args)
469 fprintf(stderr, "libwayland: ");
470 vfprintf(stderr, fmt, args);
474 client_create(int x, int y, int width, int height)
476 struct client *client;
477 struct surface *surface;
479 wl_log_set_handler_client(log_handler);
481 /* connect to display */
482 client = calloc(1, sizeof *client);
483 client->wl_display = wl_display_connect(NULL);
484 assert(client->wl_display);
485 wl_list_init(&client->global_list);
487 /* setup registry so we can bind to interfaces */
488 client->wl_registry = wl_display_get_registry(client->wl_display);
489 wl_registry_add_listener(client->wl_registry, ®istry_listener, client);
491 /* trigger global listener */
492 wl_display_dispatch(client->wl_display);
493 wl_display_roundtrip(client->wl_display);
495 /* must have WL_SHM_FORMAT_ARGB32 */
496 assert(client->has_argb);
498 /* must have wl_test interface */
499 assert(client->test);
501 /* must have an output */
502 assert(client->output);
504 /* initialize the client surface */
505 surface = calloc(1, sizeof *surface);
507 surface->wl_surface =
508 wl_compositor_create_surface(client->wl_compositor);
509 assert(surface->wl_surface);
511 wl_surface_add_listener(surface->wl_surface, &surface_listener,
514 client->surface = surface;
515 wl_surface_set_user_data(surface->wl_surface, surface);
517 surface->width = width;
518 surface->height = height;
519 surface->wl_buffer = create_shm_buffer(client, width, height,
522 memset(surface->data, 64, width * height * 4);
524 move_client(client, x, y);