blob: 16786d9b567d50507cb0b1a3291e777264ef524a [file] [log] [blame]
U. Artie Eoff1ba9b382012-12-07 13:50:32 -08001/*
2 * Copyright © 2012 Intel Corporation
3 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080011 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080024 */
25
Bryce Harrington12cc4052014-11-19 17:18:33 -080026#include "config.h"
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070027
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080028#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <unistd.h>
Marek Chalupa4d06d462014-07-16 11:27:06 +020032#include <errno.h>
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080033#include <sys/mman.h>
Bryce Harrington892122e2015-09-24 14:31:44 -070034#include <cairo.h>
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080035
Bryce Harrington892122e2015-09-24 14:31:44 -070036#include "zalloc.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070037#include "shared/os-compatibility.h"
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080038#include "weston-test-client-helper.h"
39
Bryce Harrington273c2852014-12-15 15:51:25 -080040#define max(a, b) (((a) > (b)) ? (a) : (b))
41#define min(a, b) (((a) > (b)) ? (b) : (a))
42#define clip(x, a, b) min(max(x, a), b)
43
Bryce Harrington61a64362015-04-22 18:23:01 -070044void *
45fail_on_null(void *p)
46{
47 if (p == NULL) {
48 fprintf(stderr, "out of memory\n");
49 exit(EXIT_FAILURE);
50 }
51 return p;
52}
53
54
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080055int
56surface_contains(struct surface *surface, int x, int y)
57{
58 /* test whether a global x,y point is contained in the surface */
59 int sx = surface->x;
60 int sy = surface->y;
61 int sw = surface->width;
62 int sh = surface->height;
63 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
64}
65
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050066static void
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020067frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050068{
69 int *done = data;
70
71 *done = 1;
72
73 wl_callback_destroy(callback);
74}
75
76static const struct wl_callback_listener frame_listener = {
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020077 frame_callback_handler
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050078};
79
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020080struct wl_callback *
81frame_callback_set(struct wl_surface *surface, int *done)
82{
83 struct wl_callback *callback;
84
85 *done = 0;
86 callback = wl_surface_frame(surface);
87 wl_callback_add_listener(callback, &frame_listener, done);
88
89 return callback;
90}
91
Marek Chalupa1740aa82014-07-16 11:32:50 +020092int
93frame_callback_wait_nofail(struct client *client, int *done)
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020094{
95 while (!*done) {
Marek Chalupa1740aa82014-07-16 11:32:50 +020096 if (wl_display_dispatch(client->wl_display) < 0)
97 return 0;
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020098 }
Marek Chalupa1740aa82014-07-16 11:32:50 +020099
100 return 1;
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200101}
102
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800103void
104move_client(struct client *client, int x, int y)
105{
106 struct surface *surface = client->surface;
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500107 int done;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800108
109 client->surface->x = x;
110 client->surface->y = y;
Derek Foremanf6a65922015-02-24 09:32:14 -0600111 weston_test_move_surface(client->test->weston_test, surface->wl_surface,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800112 surface->x, surface->y);
Bryce Harringtoncb50ed22014-12-10 18:33:47 -0800113 /* The attach here is necessary because commit() will call configure
Giulio Camuffo82cb5052013-02-28 18:44:54 +0100114 * only on surfaces newly attached, and the one that sets the surface
115 * position is the configure. */
116 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800117 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
118 surface->height);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500119
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200120 frame_callback_set(surface->wl_surface, &done);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500121
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800122 wl_surface_commit(surface->wl_surface);
123
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200124 frame_callback_wait(client, &done);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800125}
126
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000127int
128get_n_egl_buffers(struct client *client)
129{
130 client->test->n_egl_buffers = -1;
131
Derek Foremanf6a65922015-02-24 09:32:14 -0600132 weston_test_get_n_egl_buffers(client->test->weston_test);
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000133 wl_display_roundtrip(client->wl_display);
134
135 return client->test->n_egl_buffers;
136}
137
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800138static void
139pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
140 uint32_t serial, struct wl_surface *wl_surface,
141 wl_fixed_t x, wl_fixed_t y)
142{
143 struct pointer *pointer = data;
144
145 pointer->focus = wl_surface_get_user_data(wl_surface);
146 pointer->x = wl_fixed_to_int(x);
147 pointer->y = wl_fixed_to_int(y);
148
149 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
150 pointer->x, pointer->y, pointer->focus);
151}
152
153static void
154pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
155 uint32_t serial, struct wl_surface *wl_surface)
156{
157 struct pointer *pointer = data;
158
159 pointer->focus = NULL;
160
161 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
162 wl_surface_get_user_data(wl_surface));
163}
164
165static void
166pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
167 uint32_t time, wl_fixed_t x, wl_fixed_t y)
168{
169 struct pointer *pointer = data;
170
171 pointer->x = wl_fixed_to_int(x);
172 pointer->y = wl_fixed_to_int(y);
173
174 fprintf(stderr, "test-client: got pointer motion %d %d\n",
175 pointer->x, pointer->y);
176}
177
178static void
179pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
180 uint32_t serial, uint32_t time, uint32_t button,
181 uint32_t state)
182{
183 struct pointer *pointer = data;
184
185 pointer->button = button;
186 pointer->state = state;
187
188 fprintf(stderr, "test-client: got pointer button %u %u\n",
189 button, state);
190}
191
192static void
193pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
194 uint32_t time, uint32_t axis, wl_fixed_t value)
195{
Kristian Høgsberg4c8ce202013-10-09 14:23:00 -0700196 fprintf(stderr, "test-client: got pointer axis %u %f\n",
197 axis, wl_fixed_to_double(value));
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800198}
199
200static const struct wl_pointer_listener pointer_listener = {
201 pointer_handle_enter,
202 pointer_handle_leave,
203 pointer_handle_motion,
204 pointer_handle_button,
205 pointer_handle_axis,
206};
207
208static void
209keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
210 uint32_t format, int fd, uint32_t size)
211{
212 close(fd);
213
214 fprintf(stderr, "test-client: got keyboard keymap\n");
215}
216
217static void
218keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
219 uint32_t serial, struct wl_surface *wl_surface,
220 struct wl_array *keys)
221{
222 struct keyboard *keyboard = data;
223
224 keyboard->focus = wl_surface_get_user_data(wl_surface);
225
226 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
227 keyboard->focus);
228}
229
230static void
231keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
232 uint32_t serial, struct wl_surface *wl_surface)
233{
234 struct keyboard *keyboard = data;
235
236 keyboard->focus = NULL;
237
238 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
239 wl_surface_get_user_data(wl_surface));
240}
241
242static void
243keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
244 uint32_t serial, uint32_t time, uint32_t key,
245 uint32_t state)
246{
247 struct keyboard *keyboard = data;
248
249 keyboard->key = key;
250 keyboard->state = state;
251
252 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
253}
254
255static void
256keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
257 uint32_t serial, uint32_t mods_depressed,
258 uint32_t mods_latched, uint32_t mods_locked,
259 uint32_t group)
260{
261 struct keyboard *keyboard = data;
262
263 keyboard->mods_depressed = mods_depressed;
264 keyboard->mods_latched = mods_latched;
265 keyboard->mods_locked = mods_locked;
266 keyboard->group = group;
267
268 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
269 mods_depressed, mods_latched, mods_locked, group);
270}
271
Marek Chalupa643d85f2015-03-30 06:37:56 -0400272static void
273keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
274 int32_t rate, int32_t delay)
275{
276 struct keyboard *keyboard = data;
277
278 keyboard->repeat_info.rate = rate;
279 keyboard->repeat_info.delay = delay;
280
281 fprintf(stderr, "test-client: got keyboard repeat_info %d %d\n",
282 rate, delay);
283}
284
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800285static const struct wl_keyboard_listener keyboard_listener = {
286 keyboard_handle_keymap,
287 keyboard_handle_enter,
288 keyboard_handle_leave,
289 keyboard_handle_key,
290 keyboard_handle_modifiers,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400291 keyboard_handle_repeat_info,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800292};
293
294static void
Marek Chalupa8a5523c2015-03-30 09:20:07 -0400295touch_handle_down(void *data, struct wl_touch *wl_touch,
296 uint32_t serial, uint32_t time, struct wl_surface *surface,
297 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
298{
299 struct touch *touch = data;
300
301 touch->down_x = wl_fixed_to_int(x_w);
302 touch->down_y = wl_fixed_to_int(y_w);
303 touch->id = id;
304
305 fprintf(stderr, "test-client: got touch down %d %d, surf: %p, id: %d\n",
306 touch->down_x, touch->down_y, surface, id);
307}
308
309static void
310touch_handle_up(void *data, struct wl_touch *wl_touch,
311 uint32_t serial, uint32_t time, int32_t id)
312{
313 struct touch *touch = data;
314 touch->up_id = id;
315
316 fprintf(stderr, "test-client: got touch up, id: %d\n", id);
317}
318
319static void
320touch_handle_motion(void *data, struct wl_touch *wl_touch,
321 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
322{
323 struct touch *touch = data;
324 touch->x = wl_fixed_to_int(x_w);
325 touch->y = wl_fixed_to_int(y_w);
326
327 fprintf(stderr, "test-client: got touch motion, %d %d, id: %d\n",
328 touch->x, touch->y, id);
329}
330
331static void
332touch_handle_frame(void *data, struct wl_touch *wl_touch)
333{
334 struct touch *touch = data;
335
336 ++touch->frame_no;
337
338 fprintf(stderr, "test-client: got touch frame (%d)\n", touch->frame_no);
339}
340
341static void
342touch_handle_cancel(void *data, struct wl_touch *wl_touch)
343{
344 struct touch *touch = data;
345
346 ++touch->cancel_no;
347
348 fprintf(stderr, "test-client: got touch cancel (%d)\n", touch->cancel_no);
349}
350
351static const struct wl_touch_listener touch_listener = {
352 touch_handle_down,
353 touch_handle_up,
354 touch_handle_motion,
355 touch_handle_frame,
356 touch_handle_cancel,
357};
358
359static void
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800360surface_enter(void *data,
361 struct wl_surface *wl_surface, struct wl_output *output)
362{
363 struct surface *surface = data;
364
365 surface->output = wl_output_get_user_data(output);
366
367 fprintf(stderr, "test-client: got surface enter output %p\n",
368 surface->output);
369}
370
371static void
372surface_leave(void *data,
373 struct wl_surface *wl_surface, struct wl_output *output)
374{
375 struct surface *surface = data;
376
377 surface->output = NULL;
378
379 fprintf(stderr, "test-client: got surface leave output %p\n",
380 wl_output_get_user_data(output));
381}
382
383static const struct wl_surface_listener surface_listener = {
384 surface_enter,
385 surface_leave
386};
387
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200388struct wl_buffer *
389create_shm_buffer(struct client *client, int width, int height, void **pixels)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800390{
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800391 struct wl_shm *shm = client->wl_shm;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200392 int stride = width * 4;
393 int size = stride * height;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800394 struct wl_shm_pool *pool;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200395 struct wl_buffer *buffer;
396 int fd;
397 void *data;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800398
399 fd = os_create_anonymous_file(size);
400 assert(fd >= 0);
401
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200402 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
403 if (data == MAP_FAILED) {
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800404 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200405 assert(data != MAP_FAILED);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800406 }
407
408 pool = wl_shm_create_pool(shm, fd, size);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200409 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
410 WL_SHM_FORMAT_ARGB8888);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800411 wl_shm_pool_destroy(pool);
412
413 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200414
415 if (pixels)
416 *pixels = data;
417
418 return buffer;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800419}
420
421static void
422shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
423{
424 struct client *client = data;
425
426 if (format == WL_SHM_FORMAT_ARGB8888)
427 client->has_argb = 1;
428}
429
430struct wl_shm_listener shm_listener = {
431 shm_format
432};
433
434static void
Derek Foremanf6a65922015-02-24 09:32:14 -0600435test_handle_pointer_position(void *data, struct weston_test *weston_test,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800436 wl_fixed_t x, wl_fixed_t y)
437{
438 struct test *test = data;
439 test->pointer_x = wl_fixed_to_int(x);
440 test->pointer_y = wl_fixed_to_int(y);
441
442 fprintf(stderr, "test-client: got global pointer %d %d\n",
443 test->pointer_x, test->pointer_y);
444}
445
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000446static void
Derek Foremanf6a65922015-02-24 09:32:14 -0600447test_handle_n_egl_buffers(void *data, struct weston_test *weston_test, uint32_t n)
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000448{
449 struct test *test = data;
450
451 test->n_egl_buffers = n;
452}
453
Bryce Harrington692275f2015-04-23 16:33:49 -0700454static void
455test_handle_capture_screenshot_done(void *data, struct weston_test *weston_test)
456{
457 struct test *test = data;
458
459 printf("Screenshot has been captured\n");
460 test->buffer_copy_done = 1;
461}
462
Derek Foremanf6a65922015-02-24 09:32:14 -0600463static const struct weston_test_listener test_listener = {
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000464 test_handle_pointer_position,
465 test_handle_n_egl_buffers,
Bryce Harrington692275f2015-04-23 16:33:49 -0700466 test_handle_capture_screenshot_done,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800467};
468
469static void
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400470input_update_devices(struct input *input)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800471{
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800472 struct pointer *pointer;
473 struct keyboard *keyboard;
Marek Chalupa8a5523c2015-03-30 09:20:07 -0400474 struct touch *touch;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800475
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400476 struct wl_seat *seat = input->wl_seat;
477 enum wl_seat_capability caps = input->caps;
478
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800479 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700480 pointer = xzalloc(sizeof *pointer);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800481 pointer->wl_pointer = wl_seat_get_pointer(seat);
482 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
483 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
484 pointer);
485 input->pointer = pointer;
486 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
487 wl_pointer_destroy(input->pointer->wl_pointer);
488 free(input->pointer);
489 input->pointer = NULL;
490 }
491
492 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700493 keyboard = xzalloc(sizeof *keyboard);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800494 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
495 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
496 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
497 keyboard);
498 input->keyboard = keyboard;
499 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
500 wl_keyboard_destroy(input->keyboard->wl_keyboard);
501 free(input->keyboard);
502 input->keyboard = NULL;
503 }
Marek Chalupa8a5523c2015-03-30 09:20:07 -0400504
505 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
506 touch = xzalloc(sizeof *touch);
507 touch->wl_touch = wl_seat_get_touch(seat);
508 wl_touch_set_user_data(touch->wl_touch, touch);
509 wl_touch_add_listener(touch->wl_touch, &touch_listener,
510 touch);
511 input->touch = touch;
512 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
513 wl_touch_destroy(input->touch->wl_touch);
514 free(input->touch);
515 input->touch = NULL;
516 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800517}
518
Marek Chalupa643d85f2015-03-30 06:37:56 -0400519static void
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400520seat_handle_capabilities(void *data, struct wl_seat *seat,
521 enum wl_seat_capability caps)
522{
523 struct input *input = data;
524
525 input->caps = caps;
526
527 /* we will create/update the devices only with the right (test) seat.
528 * If we haven't discovered which seat is the test seat, just
529 * store capabilities and bail out */
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300530 if (input->seat_name && strcmp(input->seat_name, "test-seat") == 0)
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400531 input_update_devices(input);
532
533 fprintf(stderr, "test-client: got seat %p capabilities: %x\n",
534 input, caps);
535}
536
537static void
Marek Chalupa643d85f2015-03-30 06:37:56 -0400538seat_handle_name(void *data, struct wl_seat *seat, const char *name)
539{
540 struct input *input = data;
541
542 input->seat_name = strdup(name);
543 assert(input->seat_name && "No memory");
544
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400545 fprintf(stderr, "test-client: got seat %p name: \'%s\'\n",
546 input, name);
Marek Chalupa643d85f2015-03-30 06:37:56 -0400547}
548
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800549static const struct wl_seat_listener seat_listener = {
550 seat_handle_capabilities,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400551 seat_handle_name,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800552};
553
554static void
555output_handle_geometry(void *data,
556 struct wl_output *wl_output,
557 int x, int y,
558 int physical_width,
559 int physical_height,
560 int subpixel,
561 const char *make,
562 const char *model,
563 int32_t transform)
564{
565 struct output *output = data;
566
567 output->x = x;
568 output->y = y;
569}
570
571static void
572output_handle_mode(void *data,
573 struct wl_output *wl_output,
574 uint32_t flags,
575 int width,
576 int height,
577 int refresh)
578{
579 struct output *output = data;
580
581 if (flags & WL_OUTPUT_MODE_CURRENT) {
582 output->width = width;
583 output->height = height;
584 }
585}
586
Marek Chalupa643d85f2015-03-30 06:37:56 -0400587static void
588output_handle_scale(void *data,
589 struct wl_output *wl_output,
590 int scale)
591{
592 struct output *output = data;
593
594 output->scale = scale;
595}
596
597static void
598output_handle_done(void *data,
599 struct wl_output *wl_output)
600{
601 struct output *output = data;
602
603 output->initialized = 1;
604}
605
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800606static const struct wl_output_listener output_listener = {
607 output_handle_geometry,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400608 output_handle_mode,
609 output_handle_done,
610 output_handle_scale,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800611};
612
613static void
614handle_global(void *data, struct wl_registry *registry,
615 uint32_t id, const char *interface, uint32_t version)
616{
617 struct client *client = data;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800618 struct output *output;
619 struct test *test;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500620 struct global *global;
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400621 struct input *input;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500622
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700623 global = xzalloc(sizeof *global);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500624 global->name = id;
625 global->interface = strdup(interface);
626 assert(interface);
627 global->version = version;
628 wl_list_insert(client->global_list.prev, &global->link);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800629
630 if (strcmp(interface, "wl_compositor") == 0) {
631 client->wl_compositor =
632 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400633 &wl_compositor_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800634 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700635 input = xzalloc(sizeof *input);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800636 input->wl_seat =
637 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400638 &wl_seat_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800639 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400640 wl_list_insert(&client->inputs, &input->link);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800641 } else if (strcmp(interface, "wl_shm") == 0) {
642 client->wl_shm =
643 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400644 &wl_shm_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800645 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
646 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700647 output = xzalloc(sizeof *output);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800648 output->wl_output =
649 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400650 &wl_output_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800651 wl_output_add_listener(output->wl_output,
652 &output_listener, output);
653 client->output = output;
Derek Foremanf6a65922015-02-24 09:32:14 -0600654 } else if (strcmp(interface, "weston_test") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700655 test = xzalloc(sizeof *test);
Derek Foremanf6a65922015-02-24 09:32:14 -0600656 test->weston_test =
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800657 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400658 &weston_test_interface, version);
Derek Foremanf6a65922015-02-24 09:32:14 -0600659 weston_test_add_listener(test->weston_test, &test_listener, test);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800660 client->test = test;
Derek Foreman9bb13392015-01-23 12:12:36 -0600661 } else if (strcmp(interface, "wl_drm") == 0) {
662 client->has_wl_drm = true;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800663 }
664}
665
666static const struct wl_registry_listener registry_listener = {
667 handle_global
668};
669
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800670void
671skip(const char *fmt, ...)
672{
673 va_list argp;
674
675 va_start(argp, fmt);
676 vfprintf(stderr, fmt, argp);
677 va_end(argp);
678
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100679 /* automake tests uses exit code 77. weston-test-runner will see
680 * this and use it, and then weston-test's sigchld handler (in the
681 * weston process) will use that as an exit status, which is what
682 * automake will see in the end. */
683 exit(77);
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800684}
685
Marek Chalupa4d06d462014-07-16 11:27:06 +0200686void
687expect_protocol_error(struct client *client,
688 const struct wl_interface *intf,
689 uint32_t code)
690{
691 int err;
692 uint32_t errcode, failed = 0;
693 const struct wl_interface *interface;
694 unsigned int id;
695
696 /* if the error has not come yet, make it happen */
697 wl_display_roundtrip(client->wl_display);
698
699 err = wl_display_get_error(client->wl_display);
700
701 assert(err && "Expected protocol error but nothing came");
702 assert(err == EPROTO && "Expected protocol error but got local error");
703
704 errcode = wl_display_get_protocol_error(client->wl_display,
705 &interface, &id);
706
707 /* check error */
708 if (errcode != code) {
709 fprintf(stderr, "Should get error code %d but got %d\n",
710 code, errcode);
711 failed = 1;
712 }
713
714 /* this should be definitely set */
715 assert(interface);
716
717 if (strcmp(intf->name, interface->name) != 0) {
718 fprintf(stderr, "Should get interface '%s' but got '%s'\n",
719 intf->name, interface->name);
720 failed = 1;
721 }
722
723 if (failed) {
724 fprintf(stderr, "Expected other protocol error\n");
725 abort();
726 }
727
728 /* all OK */
729 fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) "
730 "with code %d\n", interface->name, id, errcode);
731}
732
Pekka Paalanen07921d72012-12-12 14:26:40 +0200733static void
734log_handler(const char *fmt, va_list args)
735{
736 fprintf(stderr, "libwayland: ");
737 vfprintf(stderr, fmt, args);
738}
739
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400740static void
741input_destroy(struct input *inp)
742{
743 wl_list_remove(&inp->link);
744 wl_seat_destroy(inp->wl_seat);
745 free(inp);
746}
747
748/* find the test-seat and set it in client.
749 * Destroy other inputs */
750static void
751client_set_input(struct client *cl)
752{
753 struct input *inp, *inptmp;
754 wl_list_for_each_safe(inp, inptmp, &cl->inputs, link) {
755 assert(inp->seat_name && "BUG: input with no name");
756 if (strcmp(inp->seat_name, "test-seat") == 0) {
757 cl->input = inp;
758 input_update_devices(inp);
759 } else {
760 input_destroy(inp);
761 }
762 }
763
764 /* we keep only one input */
765 assert(wl_list_length(&cl->inputs) == 1);
766}
767
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800768struct client *
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200769create_client(void)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800770{
771 struct client *client;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800772
Pekka Paalanen07921d72012-12-12 14:26:40 +0200773 wl_log_set_handler_client(log_handler);
774
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800775 /* connect to display */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700776 client = xzalloc(sizeof *client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800777 client->wl_display = wl_display_connect(NULL);
778 assert(client->wl_display);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500779 wl_list_init(&client->global_list);
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400780 wl_list_init(&client->inputs);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800781
782 /* setup registry so we can bind to interfaces */
783 client->wl_registry = wl_display_get_registry(client->wl_display);
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200784 wl_registry_add_listener(client->wl_registry, &registry_listener,
785 client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800786
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400787 /* this roundtrip makes sure we have all globals and we bound to them */
788 client_roundtrip(client);
789 /* this roundtrip makes sure we got all wl_shm.format and wl_seat.*
790 * events */
791 client_roundtrip(client);
792
793 /* find the right input for us */
794 client_set_input(client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800795
796 /* must have WL_SHM_FORMAT_ARGB32 */
797 assert(client->has_argb);
798
Derek Foremanf6a65922015-02-24 09:32:14 -0600799 /* must have weston_test interface */
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800800 assert(client->test);
801
802 /* must have an output */
803 assert(client->output);
804
Marek Chalupa643d85f2015-03-30 06:37:56 -0400805 /* the output must be initialized */
806 assert(client->output->initialized == 1);
807
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400808 /* must have seat set */
809 assert(client->input);
810
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200811 return client;
812}
813
814struct client *
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200815create_client_and_test_surface(int x, int y, int width, int height)
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200816{
817 struct client *client;
818 struct surface *surface;
819
820 client = create_client();
821
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800822 /* initialize the client surface */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700823 surface = xzalloc(sizeof *surface);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800824 surface->wl_surface =
825 wl_compositor_create_surface(client->wl_compositor);
826 assert(surface->wl_surface);
827
828 wl_surface_add_listener(surface->wl_surface, &surface_listener,
829 surface);
830
831 client->surface = surface;
832 wl_surface_set_user_data(surface->wl_surface, surface);
833
834 surface->width = width;
835 surface->height = height;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200836 surface->wl_buffer = create_shm_buffer(client, width, height,
837 &surface->data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800838
839 memset(surface->data, 64, width * height * 4);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800840
841 move_client(client, x, y);
842
843 return client;
844}
Bryce Harringtonc1a1d6c2014-12-09 14:46:36 -0800845
846static const char*
Bryce Harrington3835d052015-05-18 17:47:13 -0700847output_path(void)
848{
Bryce Harringtonc1a1d6c2014-12-09 14:46:36 -0800849 char *path = getenv("WESTON_TEST_OUTPUT_PATH");
850
851 if (!path)
852 return ".";
853 return path;
854 }
855
856char*
Bryce Harrington3835d052015-05-18 17:47:13 -0700857screenshot_output_filename(const char *basename, uint32_t seq)
858{
Bryce Harringtonc1a1d6c2014-12-09 14:46:36 -0800859 char *filename;
860
861 if (asprintf(&filename, "%s/%s-%02d.png",
862 output_path(), basename, seq) < 0)
863 return NULL;
864 return filename;
865}
866
867static const char*
Bryce Harrington3835d052015-05-18 17:47:13 -0700868reference_path(void)
869{
Bryce Harringtonc1a1d6c2014-12-09 14:46:36 -0800870 char *path = getenv("WESTON_TEST_REFERENCE_PATH");
871
872 if (!path)
873 return "./tests/reference";
874 return path;
875}
876
877char*
Bryce Harrington3835d052015-05-18 17:47:13 -0700878screenshot_reference_filename(const char *basename, uint32_t seq)
879{
Bryce Harringtonc1a1d6c2014-12-09 14:46:36 -0800880 char *filename;
881
882 if (asprintf(&filename, "%s/%s-%02d.png",
883 reference_path(), basename, seq) < 0)
884 return NULL;
885 return filename;
886}
Bryce Harrington273c2852014-12-15 15:51:25 -0800887
888/**
Bryce Harrington39a5be22015-05-14 14:36:18 -0700889 * check_surfaces_geometry() - verifies two surfaces are same size
890 *
891 * @returns true if surfaces have the same width and height, or false
892 * if not, or if there is no actual data.
893 */
894bool
895check_surfaces_geometry(const struct surface *a, const struct surface *b)
896{
897 if (a == NULL || b == NULL) {
898 printf("Undefined surfaces\n");
899 return false;
900 }
901 else if (a->data == NULL || b->data == NULL) {
902 printf("Undefined data\n");
903 return false;
904 }
905 else if (a->width != b->width || a->height != b->height) {
906 printf("Mismatched dimensions: %d,%d != %d,%d\n",
907 a->width, a->height, b->width, b->height);
908 return false;
909 }
910 return true;
911}
912
913/**
Bryce Harrington273c2852014-12-15 15:51:25 -0800914 * check_surfaces_equal() - tests if two surfaces are pixel-identical
915 *
916 * Returns true if surface buffers have all the same byte values,
917 * false if the surfaces don't match or can't be compared due to
918 * different dimensions.
919 */
920bool
921check_surfaces_equal(const struct surface *a, const struct surface *b)
922{
923 int bpp = 4; /* Assumes ARGB */
924
Bryce Harrington39a5be22015-05-14 14:36:18 -0700925 if (!check_surfaces_geometry(a, b))
Bryce Harrington273c2852014-12-15 15:51:25 -0800926 return false;
927
928 return (memcmp(a->data, b->data, bpp * a->width * a->height) == 0);
929}
930
931/**
932 * check_surfaces_match_in_clip() - tests if a given region within two
933 * surfaces are pixel-identical.
934 *
935 * Returns true if the two surfaces have the same byte values within the
936 * given clipping region, or false if they don't match or the surfaces
937 * can't be compared.
938 */
939bool
940check_surfaces_match_in_clip(const struct surface *a, const struct surface *b, const struct rectangle *clip_rect)
941{
942 int i, j;
943 int x0, y0, x1, y1;
944 void *p, *q;
945 int bpp = 4; /* Assumes ARGB */
946
Bryce Harrington39a5be22015-05-14 14:36:18 -0700947 if (!check_surfaces_geometry(a, b) || clip_rect == NULL)
Bryce Harrington273c2852014-12-15 15:51:25 -0800948 return false;
949
Bryce Harrington273c2852014-12-15 15:51:25 -0800950 if (clip_rect->x > a->width || clip_rect->y > a->height) {
951 printf("Clip outside image boundaries\n");
952 return true;
953 }
954
955 x0 = max(0, clip_rect->x);
956 y0 = max(0, clip_rect->y);
957 x1 = min(a->width, clip_rect->x + clip_rect->width);
958 y1 = min(a->height, clip_rect->y + clip_rect->height);
959
960 if (x0 == x1 || y0 == y1) {
961 printf("Degenerate comparison\n");
962 return true;
963 }
964
965 printf("Bytewise comparison inside clip\n");
966 for (i=y0; i<y1; i++) {
967 p = a->data + i * a->width * bpp + x0 * bpp;
968 q = b->data + i * b->width * bpp + x0 * bpp;
969 if (memcmp(p, q, (x1-x0)*bpp) != 0) {
970 /* Dump the bad row */
971 printf("Mismatched image on row %d\n", i);
972 for (j=0; j<(x1-x0)*bpp; j++) {
973 char a_char = *((char*)(p+j*bpp));
974 char b_char = *((char*)(q+j*bpp));
975 printf("%d,%d: %8x %8x %s\n", i, j, a_char, b_char,
976 (a_char != b_char)? " <---": "");
977 }
978 return false;
979 }
980 }
981
982 return true;
983}
Bryce Harrington892122e2015-09-24 14:31:44 -0700984
985/** write_surface_as_png()
986 *
987 * Writes out a given weston test surface to disk as a PNG image
988 * using the provided filename (with path).
989 *
990 * @returns true if successfully saved file; false otherwise.
991 */
992bool
993write_surface_as_png(const struct surface *weston_surface, const char *fname)
994{
995 cairo_surface_t *cairo_surface;
996 cairo_status_t status;
997 int bpp = 4; /* Assume ARGB */
998 int stride = bpp * weston_surface->width;
999
1000 cairo_surface = cairo_image_surface_create_for_data(weston_surface->data,
1001 CAIRO_FORMAT_ARGB32,
1002 weston_surface->width,
1003 weston_surface->height,
1004 stride);
1005 printf("Writing PNG to disk\n");
1006 status = cairo_surface_write_to_png(cairo_surface, fname);
1007 if (status != CAIRO_STATUS_SUCCESS) {
1008 printf("Failed to save screenshot: %s\n",
1009 cairo_status_to_string(status));
1010 return false;
1011 }
1012 cairo_surface_destroy(cairo_surface);
1013 return true;
1014}
1015
1016/** load_surface_from_png()
1017 *
1018 * Reads a PNG image from disk using the given filename (and path)
1019 * and returns as a freshly allocated weston test surface.
1020 *
1021 * @returns weston test surface with image, which should be free'd
1022 * when no longer used; or, NULL in case of error.
1023 */
1024struct surface *
1025load_surface_from_png(const char *fname)
1026{
1027 struct surface *reference;
1028 cairo_surface_t *reference_cairo_surface;
1029 cairo_status_t status;
1030 size_t source_data_size;
1031 int bpp;
1032 int stride;
1033
1034 reference_cairo_surface = cairo_image_surface_create_from_png(fname);
1035 status = cairo_surface_status(reference_cairo_surface);
1036 if (status != CAIRO_STATUS_SUCCESS) {
1037 printf("Could not open %s: %s\n", fname, cairo_status_to_string(status));
1038 cairo_surface_destroy(reference_cairo_surface);
1039 return NULL;
1040 }
1041
1042 /* Disguise the cairo surface in a weston test surface */
1043 reference = zalloc(sizeof *reference);
1044 if (reference == NULL) {
1045 perror("zalloc reference");
1046 cairo_surface_destroy(reference_cairo_surface);
1047 return NULL;
1048 }
1049 reference->width = cairo_image_surface_get_width(reference_cairo_surface);
1050 reference->height = cairo_image_surface_get_height(reference_cairo_surface);
1051 stride = cairo_image_surface_get_stride(reference_cairo_surface);
1052 source_data_size = stride * reference->height;
1053
1054 /* Check that the file's stride matches our assumption */
1055 bpp = 4;
1056 if (stride != bpp * reference->width) {
1057 printf("Mismatched stride for screenshot reference image %s\n", fname);
1058 cairo_surface_destroy(reference_cairo_surface);
1059 free(reference);
1060 return NULL;
1061 }
1062
1063 /* Allocate new buffer for our weston reference, and copy the data from
1064 the cairo surface so we can destroy it */
1065 reference->data = zalloc(source_data_size);
1066 if (reference->data == NULL) {
1067 perror("zalloc reference data");
1068 cairo_surface_destroy(reference_cairo_surface);
1069 free(reference);
1070 return NULL;
1071 }
1072 memcpy(reference->data,
1073 cairo_image_surface_get_data(reference_cairo_surface),
1074 source_data_size);
1075
1076 cairo_surface_destroy(reference_cairo_surface);
1077 return reference;
1078}
1079
1080/** create_screenshot_surface()
1081 *
1082 * Allocates and initializes a weston test surface for use in
1083 * storing a screenshot of the client's output. Establishes a
1084 * shm backed wl_buffer for retrieving screenshot image data
1085 * from the server, sized to match the client's output display.
1086 *
1087 * @returns stack allocated surface image, which should be
1088 * free'd when done using it.
1089 */
1090struct surface *
1091create_screenshot_surface(struct client *client)
1092{
1093 struct surface *screenshot;
1094 screenshot = zalloc(sizeof *screenshot);
1095 if (screenshot == NULL)
1096 return NULL;
1097 screenshot->wl_buffer = create_shm_buffer(client,
1098 client->output->width,
1099 client->output->height,
1100 &screenshot->data);
1101 screenshot->height = client->output->height;
1102 screenshot->width = client->output->width;
1103
1104 return screenshot;
1105}
1106
1107/** capture_screenshot_of_output()
1108 *
1109 * Requests a screenshot from the server of the output that the
1110 * client appears on. The image data returned from the server
1111 * can be accessed from the screenshot surface's data member.
1112 *
1113 * @returns a new surface object, which should be free'd when no
1114 * longer needed.
1115 */
1116struct surface *
1117capture_screenshot_of_output(struct client *client)
1118{
1119 struct surface *screenshot;
1120
1121 /* Create a surface to hold the screenshot */
1122 screenshot = create_screenshot_surface(client);
1123
1124 client->test->buffer_copy_done = 0;
1125 weston_test_capture_screenshot(client->test->weston_test,
1126 client->output->wl_output,
1127 screenshot->wl_buffer);
1128 while (client->test->buffer_copy_done == 0)
1129 if (wl_display_dispatch(client->wl_display) < 0)
1130 break;
1131
1132 /* FIXME: Document somewhere the orientation the screenshot is taken
1133 * and how the clip coords are interpreted, in case of scaling/transform.
1134 * If we're using read_pixels() just make sure it is documented somewhere.
1135 * Protocol docs in the XML, comparison function docs in Doxygen style.
1136 */
1137
1138 return screenshot;
1139}