blob: 399aa4436e355fc1eb49ca3289b0bc204688bf5d [file] [log] [blame]
U. Artie Eoff1ba9b382012-12-07 13:50:32 -08001/*
2 * Copyright © 2012 Intel Corporation
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070023#include <config.h>
24
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080025#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
29#include <sys/mman.h>
30
31#include "../shared/os-compatibility.h"
32#include "weston-test-client-helper.h"
33
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -070034static inline void *
35xzalloc(size_t size)
36{
37 void *p;
38
39 p = calloc(1, size);
40 assert(p);
41
42 return p;
43}
44
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080045int
46surface_contains(struct surface *surface, int x, int y)
47{
48 /* test whether a global x,y point is contained in the surface */
49 int sx = surface->x;
50 int sy = surface->y;
51 int sw = surface->width;
52 int sh = surface->height;
53 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
54}
55
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050056static void
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020057frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050058{
59 int *done = data;
60
61 *done = 1;
62
63 wl_callback_destroy(callback);
64}
65
66static const struct wl_callback_listener frame_listener = {
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020067 frame_callback_handler
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050068};
69
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020070struct wl_callback *
71frame_callback_set(struct wl_surface *surface, int *done)
72{
73 struct wl_callback *callback;
74
75 *done = 0;
76 callback = wl_surface_frame(surface);
77 wl_callback_add_listener(callback, &frame_listener, done);
78
79 return callback;
80}
81
82void
83frame_callback_wait(struct client *client, int *done)
84{
85 while (!*done) {
86 assert(wl_display_dispatch(client->wl_display) >= 0);
87 }
88}
89
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080090void
91move_client(struct client *client, int x, int y)
92{
93 struct surface *surface = client->surface;
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050094 int done;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080095
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);
Giulio Camuffo82cb5052013-02-28 18:44:54 +0100100 /* 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);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800104 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
105 surface->height);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500106
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200107 frame_callback_set(surface->wl_surface, &done);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500108
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800109 wl_surface_commit(surface->wl_surface);
110
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200111 frame_callback_wait(client, &done);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800112}
113
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000114int
115get_n_egl_buffers(struct client *client)
116{
117 client->test->n_egl_buffers = -1;
118
119 wl_test_get_n_egl_buffers(client->test->wl_test);
120 wl_display_roundtrip(client->wl_display);
121
122 return client->test->n_egl_buffers;
123}
124
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800125static void
126pointer_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)
129{
130 struct pointer *pointer = data;
131
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);
135
136 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
137 pointer->x, pointer->y, pointer->focus);
138}
139
140static void
141pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
142 uint32_t serial, struct wl_surface *wl_surface)
143{
144 struct pointer *pointer = data;
145
146 pointer->focus = NULL;
147
148 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
149 wl_surface_get_user_data(wl_surface));
150}
151
152static void
153pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
154 uint32_t time, wl_fixed_t x, wl_fixed_t y)
155{
156 struct pointer *pointer = data;
157
158 pointer->x = wl_fixed_to_int(x);
159 pointer->y = wl_fixed_to_int(y);
160
161 fprintf(stderr, "test-client: got pointer motion %d %d\n",
162 pointer->x, pointer->y);
163}
164
165static void
166pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
167 uint32_t serial, uint32_t time, uint32_t button,
168 uint32_t state)
169{
170 struct pointer *pointer = data;
171
172 pointer->button = button;
173 pointer->state = state;
174
175 fprintf(stderr, "test-client: got pointer button %u %u\n",
176 button, state);
177}
178
179static void
180pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
181 uint32_t time, uint32_t axis, wl_fixed_t value)
182{
Kristian Høgsberg4c8ce202013-10-09 14:23:00 -0700183 fprintf(stderr, "test-client: got pointer axis %u %f\n",
184 axis, wl_fixed_to_double(value));
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800185}
186
187static const struct wl_pointer_listener pointer_listener = {
188 pointer_handle_enter,
189 pointer_handle_leave,
190 pointer_handle_motion,
191 pointer_handle_button,
192 pointer_handle_axis,
193};
194
195static void
196keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
197 uint32_t format, int fd, uint32_t size)
198{
199 close(fd);
200
201 fprintf(stderr, "test-client: got keyboard keymap\n");
202}
203
204static void
205keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
206 uint32_t serial, struct wl_surface *wl_surface,
207 struct wl_array *keys)
208{
209 struct keyboard *keyboard = data;
210
211 keyboard->focus = wl_surface_get_user_data(wl_surface);
212
213 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
214 keyboard->focus);
215}
216
217static void
218keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
219 uint32_t serial, struct wl_surface *wl_surface)
220{
221 struct keyboard *keyboard = data;
222
223 keyboard->focus = NULL;
224
225 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
226 wl_surface_get_user_data(wl_surface));
227}
228
229static void
230keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
231 uint32_t serial, uint32_t time, uint32_t key,
232 uint32_t state)
233{
234 struct keyboard *keyboard = data;
235
236 keyboard->key = key;
237 keyboard->state = state;
238
239 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
240}
241
242static void
243keyboard_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,
246 uint32_t group)
247{
248 struct keyboard *keyboard = data;
249
250 keyboard->mods_depressed = mods_depressed;
251 keyboard->mods_latched = mods_latched;
252 keyboard->mods_locked = mods_locked;
253 keyboard->group = group;
254
255 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
256 mods_depressed, mods_latched, mods_locked, group);
257}
258
259static const struct wl_keyboard_listener keyboard_listener = {
260 keyboard_handle_keymap,
261 keyboard_handle_enter,
262 keyboard_handle_leave,
263 keyboard_handle_key,
264 keyboard_handle_modifiers,
265};
266
267static void
268surface_enter(void *data,
269 struct wl_surface *wl_surface, struct wl_output *output)
270{
271 struct surface *surface = data;
272
273 surface->output = wl_output_get_user_data(output);
274
275 fprintf(stderr, "test-client: got surface enter output %p\n",
276 surface->output);
277}
278
279static void
280surface_leave(void *data,
281 struct wl_surface *wl_surface, struct wl_output *output)
282{
283 struct surface *surface = data;
284
285 surface->output = NULL;
286
287 fprintf(stderr, "test-client: got surface leave output %p\n",
288 wl_output_get_user_data(output));
289}
290
291static const struct wl_surface_listener surface_listener = {
292 surface_enter,
293 surface_leave
294};
295
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200296struct wl_buffer *
297create_shm_buffer(struct client *client, int width, int height, void **pixels)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800298{
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800299 struct wl_shm *shm = client->wl_shm;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200300 int stride = width * 4;
301 int size = stride * height;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800302 struct wl_shm_pool *pool;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200303 struct wl_buffer *buffer;
304 int fd;
305 void *data;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800306
307 fd = os_create_anonymous_file(size);
308 assert(fd >= 0);
309
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200310 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
311 if (data == MAP_FAILED) {
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800312 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200313 assert(data != MAP_FAILED);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800314 }
315
316 pool = wl_shm_create_pool(shm, fd, size);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200317 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
318 WL_SHM_FORMAT_ARGB8888);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800319 wl_shm_pool_destroy(pool);
320
321 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200322
323 if (pixels)
324 *pixels = data;
325
326 return buffer;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800327}
328
329static void
330shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
331{
332 struct client *client = data;
333
334 if (format == WL_SHM_FORMAT_ARGB8888)
335 client->has_argb = 1;
336}
337
338struct wl_shm_listener shm_listener = {
339 shm_format
340};
341
342static void
343test_handle_pointer_position(void *data, struct wl_test *wl_test,
344 wl_fixed_t x, wl_fixed_t y)
345{
346 struct test *test = data;
347 test->pointer_x = wl_fixed_to_int(x);
348 test->pointer_y = wl_fixed_to_int(y);
349
350 fprintf(stderr, "test-client: got global pointer %d %d\n",
351 test->pointer_x, test->pointer_y);
352}
353
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000354static void
355test_handle_n_egl_buffers(void *data, struct wl_test *wl_test, uint32_t n)
356{
357 struct test *test = data;
358
359 test->n_egl_buffers = n;
360}
361
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800362static const struct wl_test_listener test_listener = {
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000363 test_handle_pointer_position,
364 test_handle_n_egl_buffers,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800365};
366
367static void
368seat_handle_capabilities(void *data, struct wl_seat *seat,
369 enum wl_seat_capability caps)
370{
371 struct input *input = data;
372 struct pointer *pointer;
373 struct keyboard *keyboard;
374
375 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700376 pointer = xzalloc(sizeof *pointer);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800377 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,
380 pointer);
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;
386 }
387
388 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700389 keyboard = xzalloc(sizeof *keyboard);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800390 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,
393 keyboard);
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;
399 }
400}
401
402static const struct wl_seat_listener seat_listener = {
403 seat_handle_capabilities,
404};
405
406static void
407output_handle_geometry(void *data,
408 struct wl_output *wl_output,
409 int x, int y,
410 int physical_width,
411 int physical_height,
412 int subpixel,
413 const char *make,
414 const char *model,
415 int32_t transform)
416{
417 struct output *output = data;
418
419 output->x = x;
420 output->y = y;
421}
422
423static void
424output_handle_mode(void *data,
425 struct wl_output *wl_output,
426 uint32_t flags,
427 int width,
428 int height,
429 int refresh)
430{
431 struct output *output = data;
432
433 if (flags & WL_OUTPUT_MODE_CURRENT) {
434 output->width = width;
435 output->height = height;
436 }
437}
438
439static const struct wl_output_listener output_listener = {
440 output_handle_geometry,
441 output_handle_mode
442};
443
444static void
445handle_global(void *data, struct wl_registry *registry,
446 uint32_t id, const char *interface, uint32_t version)
447{
448 struct client *client = data;
449 struct input *input;
450 struct output *output;
451 struct test *test;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500452 struct global *global;
453
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700454 global = xzalloc(sizeof *global);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500455 global->name = id;
456 global->interface = strdup(interface);
457 assert(interface);
458 global->version = version;
459 wl_list_insert(client->global_list.prev, &global->link);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800460
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) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700466 input = xzalloc(sizeof *input);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800467 input->wl_seat =
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) {
473 client->wl_shm =
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) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700478 output = xzalloc(sizeof *output);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800479 output->wl_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) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700486 test = xzalloc(sizeof *test);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800487 test->wl_test =
488 wl_registry_bind(registry, id,
489 &wl_test_interface, 1);
490 wl_test_add_listener(test->wl_test, &test_listener, test);
491 client->test = test;
492 }
493}
494
495static const struct wl_registry_listener registry_listener = {
496 handle_global
497};
498
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800499void
500skip(const char *fmt, ...)
501{
502 va_list argp;
503
504 va_start(argp, fmt);
505 vfprintf(stderr, fmt, argp);
506 va_end(argp);
507
508 /* automake tests uses exit code 77, but we don't have a good
509 * way to make weston exit with that from here. */
510 exit(0);
511}
512
Pekka Paalanen07921d72012-12-12 14:26:40 +0200513static void
514log_handler(const char *fmt, va_list args)
515{
516 fprintf(stderr, "libwayland: ");
517 vfprintf(stderr, fmt, args);
518}
519
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800520struct client *
521client_create(int x, int y, int width, int height)
522{
523 struct client *client;
524 struct surface *surface;
525
Pekka Paalanen07921d72012-12-12 14:26:40 +0200526 wl_log_set_handler_client(log_handler);
527
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800528 /* connect to display */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700529 client = xzalloc(sizeof *client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800530 client->wl_display = wl_display_connect(NULL);
531 assert(client->wl_display);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500532 wl_list_init(&client->global_list);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800533
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, &registry_listener, client);
537
538 /* trigger global listener */
539 wl_display_dispatch(client->wl_display);
540 wl_display_roundtrip(client->wl_display);
541
542 /* must have WL_SHM_FORMAT_ARGB32 */
543 assert(client->has_argb);
544
545 /* must have wl_test interface */
546 assert(client->test);
547
548 /* must have an output */
549 assert(client->output);
550
551 /* initialize the client surface */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700552 surface = xzalloc(sizeof *surface);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800553 surface->wl_surface =
554 wl_compositor_create_surface(client->wl_compositor);
555 assert(surface->wl_surface);
556
557 wl_surface_add_listener(surface->wl_surface, &surface_listener,
558 surface);
559
560 client->surface = surface;
561 wl_surface_set_user_data(surface->wl_surface, surface);
562
563 surface->width = width;
564 surface->height = height;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200565 surface->wl_buffer = create_shm_buffer(client, width, height,
566 &surface->data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800567
568 memset(surface->data, 64, width * height * 4);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800569
570 move_client(client, x, y);
571
572 return client;
573}