blob: 080bb629bf3113317c2a665ca9676f006610b6ec [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
Bryce Harrington12cc4052014-11-19 17:18:33 -080023#include "config.h"
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070024
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080025#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
Marek Chalupa4d06d462014-07-16 11:27:06 +020029#include <errno.h>
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080030#include <sys/mman.h>
31
32#include "../shared/os-compatibility.h"
33#include "weston-test-client-helper.h"
34
Bryce Harrington61a64362015-04-22 18:23:01 -070035void *
36fail_on_null(void *p)
37{
38 if (p == NULL) {
39 fprintf(stderr, "out of memory\n");
40 exit(EXIT_FAILURE);
41 }
42 return p;
43}
44
45
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080046int
47surface_contains(struct surface *surface, int x, int y)
48{
49 /* test whether a global x,y point is contained in the surface */
50 int sx = surface->x;
51 int sy = surface->y;
52 int sw = surface->width;
53 int sh = surface->height;
54 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
55}
56
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050057static void
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020058frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050059{
60 int *done = data;
61
62 *done = 1;
63
64 wl_callback_destroy(callback);
65}
66
67static const struct wl_callback_listener frame_listener = {
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020068 frame_callback_handler
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050069};
70
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020071struct wl_callback *
72frame_callback_set(struct wl_surface *surface, int *done)
73{
74 struct wl_callback *callback;
75
76 *done = 0;
77 callback = wl_surface_frame(surface);
78 wl_callback_add_listener(callback, &frame_listener, done);
79
80 return callback;
81}
82
Marek Chalupa1740aa82014-07-16 11:32:50 +020083int
84frame_callback_wait_nofail(struct client *client, int *done)
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020085{
86 while (!*done) {
Marek Chalupa1740aa82014-07-16 11:32:50 +020087 if (wl_display_dispatch(client->wl_display) < 0)
88 return 0;
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020089 }
Marek Chalupa1740aa82014-07-16 11:32:50 +020090
91 return 1;
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020092}
93
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080094void
95move_client(struct client *client, int x, int y)
96{
97 struct surface *surface = client->surface;
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050098 int done;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080099
100 client->surface->x = x;
101 client->surface->y = y;
Derek Foremanf6a65922015-02-24 09:32:14 -0600102 weston_test_move_surface(client->test->weston_test, surface->wl_surface,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800103 surface->x, surface->y);
Bryce Harringtoncb50ed22014-12-10 18:33:47 -0800104 /* The attach here is necessary because commit() will call configure
Giulio Camuffo82cb5052013-02-28 18:44:54 +0100105 * only on surfaces newly attached, and the one that sets the surface
106 * position is the configure. */
107 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800108 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
109 surface->height);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500110
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200111 frame_callback_set(surface->wl_surface, &done);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500112
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800113 wl_surface_commit(surface->wl_surface);
114
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200115 frame_callback_wait(client, &done);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800116}
117
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000118int
119get_n_egl_buffers(struct client *client)
120{
121 client->test->n_egl_buffers = -1;
122
Derek Foremanf6a65922015-02-24 09:32:14 -0600123 weston_test_get_n_egl_buffers(client->test->weston_test);
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000124 wl_display_roundtrip(client->wl_display);
125
126 return client->test->n_egl_buffers;
127}
128
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800129static void
130pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
131 uint32_t serial, struct wl_surface *wl_surface,
132 wl_fixed_t x, wl_fixed_t y)
133{
134 struct pointer *pointer = data;
135
136 pointer->focus = wl_surface_get_user_data(wl_surface);
137 pointer->x = wl_fixed_to_int(x);
138 pointer->y = wl_fixed_to_int(y);
139
140 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
141 pointer->x, pointer->y, pointer->focus);
142}
143
144static void
145pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
146 uint32_t serial, struct wl_surface *wl_surface)
147{
148 struct pointer *pointer = data;
149
150 pointer->focus = NULL;
151
152 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
153 wl_surface_get_user_data(wl_surface));
154}
155
156static void
157pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
158 uint32_t time, wl_fixed_t x, wl_fixed_t y)
159{
160 struct pointer *pointer = data;
161
162 pointer->x = wl_fixed_to_int(x);
163 pointer->y = wl_fixed_to_int(y);
164
165 fprintf(stderr, "test-client: got pointer motion %d %d\n",
166 pointer->x, pointer->y);
167}
168
169static void
170pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
171 uint32_t serial, uint32_t time, uint32_t button,
172 uint32_t state)
173{
174 struct pointer *pointer = data;
175
176 pointer->button = button;
177 pointer->state = state;
178
179 fprintf(stderr, "test-client: got pointer button %u %u\n",
180 button, state);
181}
182
183static void
184pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
185 uint32_t time, uint32_t axis, wl_fixed_t value)
186{
Kristian Høgsberg4c8ce202013-10-09 14:23:00 -0700187 fprintf(stderr, "test-client: got pointer axis %u %f\n",
188 axis, wl_fixed_to_double(value));
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800189}
190
191static const struct wl_pointer_listener pointer_listener = {
192 pointer_handle_enter,
193 pointer_handle_leave,
194 pointer_handle_motion,
195 pointer_handle_button,
196 pointer_handle_axis,
197};
198
199static void
200keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
201 uint32_t format, int fd, uint32_t size)
202{
203 close(fd);
204
205 fprintf(stderr, "test-client: got keyboard keymap\n");
206}
207
208static void
209keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
210 uint32_t serial, struct wl_surface *wl_surface,
211 struct wl_array *keys)
212{
213 struct keyboard *keyboard = data;
214
215 keyboard->focus = wl_surface_get_user_data(wl_surface);
216
217 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
218 keyboard->focus);
219}
220
221static void
222keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
223 uint32_t serial, struct wl_surface *wl_surface)
224{
225 struct keyboard *keyboard = data;
226
227 keyboard->focus = NULL;
228
229 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
230 wl_surface_get_user_data(wl_surface));
231}
232
233static void
234keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
235 uint32_t serial, uint32_t time, uint32_t key,
236 uint32_t state)
237{
238 struct keyboard *keyboard = data;
239
240 keyboard->key = key;
241 keyboard->state = state;
242
243 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
244}
245
246static void
247keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
248 uint32_t serial, uint32_t mods_depressed,
249 uint32_t mods_latched, uint32_t mods_locked,
250 uint32_t group)
251{
252 struct keyboard *keyboard = data;
253
254 keyboard->mods_depressed = mods_depressed;
255 keyboard->mods_latched = mods_latched;
256 keyboard->mods_locked = mods_locked;
257 keyboard->group = group;
258
259 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
260 mods_depressed, mods_latched, mods_locked, group);
261}
262
Marek Chalupa643d85f2015-03-30 06:37:56 -0400263static void
264keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
265 int32_t rate, int32_t delay)
266{
267 struct keyboard *keyboard = data;
268
269 keyboard->repeat_info.rate = rate;
270 keyboard->repeat_info.delay = delay;
271
272 fprintf(stderr, "test-client: got keyboard repeat_info %d %d\n",
273 rate, delay);
274}
275
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800276static const struct wl_keyboard_listener keyboard_listener = {
277 keyboard_handle_keymap,
278 keyboard_handle_enter,
279 keyboard_handle_leave,
280 keyboard_handle_key,
281 keyboard_handle_modifiers,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400282 keyboard_handle_repeat_info,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800283};
284
285static void
Marek Chalupa8a5523c2015-03-30 09:20:07 -0400286touch_handle_down(void *data, struct wl_touch *wl_touch,
287 uint32_t serial, uint32_t time, struct wl_surface *surface,
288 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
289{
290 struct touch *touch = data;
291
292 touch->down_x = wl_fixed_to_int(x_w);
293 touch->down_y = wl_fixed_to_int(y_w);
294 touch->id = id;
295
296 fprintf(stderr, "test-client: got touch down %d %d, surf: %p, id: %d\n",
297 touch->down_x, touch->down_y, surface, id);
298}
299
300static void
301touch_handle_up(void *data, struct wl_touch *wl_touch,
302 uint32_t serial, uint32_t time, int32_t id)
303{
304 struct touch *touch = data;
305 touch->up_id = id;
306
307 fprintf(stderr, "test-client: got touch up, id: %d\n", id);
308}
309
310static void
311touch_handle_motion(void *data, struct wl_touch *wl_touch,
312 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
313{
314 struct touch *touch = data;
315 touch->x = wl_fixed_to_int(x_w);
316 touch->y = wl_fixed_to_int(y_w);
317
318 fprintf(stderr, "test-client: got touch motion, %d %d, id: %d\n",
319 touch->x, touch->y, id);
320}
321
322static void
323touch_handle_frame(void *data, struct wl_touch *wl_touch)
324{
325 struct touch *touch = data;
326
327 ++touch->frame_no;
328
329 fprintf(stderr, "test-client: got touch frame (%d)\n", touch->frame_no);
330}
331
332static void
333touch_handle_cancel(void *data, struct wl_touch *wl_touch)
334{
335 struct touch *touch = data;
336
337 ++touch->cancel_no;
338
339 fprintf(stderr, "test-client: got touch cancel (%d)\n", touch->cancel_no);
340}
341
342static const struct wl_touch_listener touch_listener = {
343 touch_handle_down,
344 touch_handle_up,
345 touch_handle_motion,
346 touch_handle_frame,
347 touch_handle_cancel,
348};
349
350static void
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800351surface_enter(void *data,
352 struct wl_surface *wl_surface, struct wl_output *output)
353{
354 struct surface *surface = data;
355
356 surface->output = wl_output_get_user_data(output);
357
358 fprintf(stderr, "test-client: got surface enter output %p\n",
359 surface->output);
360}
361
362static void
363surface_leave(void *data,
364 struct wl_surface *wl_surface, struct wl_output *output)
365{
366 struct surface *surface = data;
367
368 surface->output = NULL;
369
370 fprintf(stderr, "test-client: got surface leave output %p\n",
371 wl_output_get_user_data(output));
372}
373
374static const struct wl_surface_listener surface_listener = {
375 surface_enter,
376 surface_leave
377};
378
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200379struct wl_buffer *
380create_shm_buffer(struct client *client, int width, int height, void **pixels)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800381{
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800382 struct wl_shm *shm = client->wl_shm;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200383 int stride = width * 4;
384 int size = stride * height;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800385 struct wl_shm_pool *pool;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200386 struct wl_buffer *buffer;
387 int fd;
388 void *data;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800389
390 fd = os_create_anonymous_file(size);
391 assert(fd >= 0);
392
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200393 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
394 if (data == MAP_FAILED) {
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800395 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200396 assert(data != MAP_FAILED);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800397 }
398
399 pool = wl_shm_create_pool(shm, fd, size);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200400 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
401 WL_SHM_FORMAT_ARGB8888);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800402 wl_shm_pool_destroy(pool);
403
404 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200405
406 if (pixels)
407 *pixels = data;
408
409 return buffer;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800410}
411
412static void
413shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
414{
415 struct client *client = data;
416
417 if (format == WL_SHM_FORMAT_ARGB8888)
418 client->has_argb = 1;
419}
420
421struct wl_shm_listener shm_listener = {
422 shm_format
423};
424
425static void
Derek Foremanf6a65922015-02-24 09:32:14 -0600426test_handle_pointer_position(void *data, struct weston_test *weston_test,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800427 wl_fixed_t x, wl_fixed_t y)
428{
429 struct test *test = data;
430 test->pointer_x = wl_fixed_to_int(x);
431 test->pointer_y = wl_fixed_to_int(y);
432
433 fprintf(stderr, "test-client: got global pointer %d %d\n",
434 test->pointer_x, test->pointer_y);
435}
436
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000437static void
Derek Foremanf6a65922015-02-24 09:32:14 -0600438test_handle_n_egl_buffers(void *data, struct weston_test *weston_test, uint32_t n)
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000439{
440 struct test *test = data;
441
442 test->n_egl_buffers = n;
443}
444
Derek Foremanf6a65922015-02-24 09:32:14 -0600445static const struct weston_test_listener test_listener = {
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000446 test_handle_pointer_position,
447 test_handle_n_egl_buffers,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800448};
449
450static void
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400451input_update_devices(struct input *input)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800452{
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800453 struct pointer *pointer;
454 struct keyboard *keyboard;
Marek Chalupa8a5523c2015-03-30 09:20:07 -0400455 struct touch *touch;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800456
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400457 struct wl_seat *seat = input->wl_seat;
458 enum wl_seat_capability caps = input->caps;
459
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800460 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700461 pointer = xzalloc(sizeof *pointer);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800462 pointer->wl_pointer = wl_seat_get_pointer(seat);
463 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
464 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
465 pointer);
466 input->pointer = pointer;
467 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
468 wl_pointer_destroy(input->pointer->wl_pointer);
469 free(input->pointer);
470 input->pointer = NULL;
471 }
472
473 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700474 keyboard = xzalloc(sizeof *keyboard);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800475 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
476 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
477 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
478 keyboard);
479 input->keyboard = keyboard;
480 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
481 wl_keyboard_destroy(input->keyboard->wl_keyboard);
482 free(input->keyboard);
483 input->keyboard = NULL;
484 }
Marek Chalupa8a5523c2015-03-30 09:20:07 -0400485
486 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
487 touch = xzalloc(sizeof *touch);
488 touch->wl_touch = wl_seat_get_touch(seat);
489 wl_touch_set_user_data(touch->wl_touch, touch);
490 wl_touch_add_listener(touch->wl_touch, &touch_listener,
491 touch);
492 input->touch = touch;
493 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
494 wl_touch_destroy(input->touch->wl_touch);
495 free(input->touch);
496 input->touch = NULL;
497 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800498}
499
Marek Chalupa643d85f2015-03-30 06:37:56 -0400500static void
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400501seat_handle_capabilities(void *data, struct wl_seat *seat,
502 enum wl_seat_capability caps)
503{
504 struct input *input = data;
505
506 input->caps = caps;
507
508 /* we will create/update the devices only with the right (test) seat.
509 * If we haven't discovered which seat is the test seat, just
510 * store capabilities and bail out */
511 if(input->seat_name && strcmp(input->seat_name, "test-seat") == 0)
512 input_update_devices(input);
513
514 fprintf(stderr, "test-client: got seat %p capabilities: %x\n",
515 input, caps);
516}
517
518static void
Marek Chalupa643d85f2015-03-30 06:37:56 -0400519seat_handle_name(void *data, struct wl_seat *seat, const char *name)
520{
521 struct input *input = data;
522
523 input->seat_name = strdup(name);
524 assert(input->seat_name && "No memory");
525
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400526 fprintf(stderr, "test-client: got seat %p name: \'%s\'\n",
527 input, name);
Marek Chalupa643d85f2015-03-30 06:37:56 -0400528}
529
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800530static const struct wl_seat_listener seat_listener = {
531 seat_handle_capabilities,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400532 seat_handle_name,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800533};
534
535static void
536output_handle_geometry(void *data,
537 struct wl_output *wl_output,
538 int x, int y,
539 int physical_width,
540 int physical_height,
541 int subpixel,
542 const char *make,
543 const char *model,
544 int32_t transform)
545{
546 struct output *output = data;
547
548 output->x = x;
549 output->y = y;
550}
551
552static void
553output_handle_mode(void *data,
554 struct wl_output *wl_output,
555 uint32_t flags,
556 int width,
557 int height,
558 int refresh)
559{
560 struct output *output = data;
561
562 if (flags & WL_OUTPUT_MODE_CURRENT) {
563 output->width = width;
564 output->height = height;
565 }
566}
567
Marek Chalupa643d85f2015-03-30 06:37:56 -0400568static void
569output_handle_scale(void *data,
570 struct wl_output *wl_output,
571 int scale)
572{
573 struct output *output = data;
574
575 output->scale = scale;
576}
577
578static void
579output_handle_done(void *data,
580 struct wl_output *wl_output)
581{
582 struct output *output = data;
583
584 output->initialized = 1;
585}
586
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800587static const struct wl_output_listener output_listener = {
588 output_handle_geometry,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400589 output_handle_mode,
590 output_handle_done,
591 output_handle_scale,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800592};
593
594static void
595handle_global(void *data, struct wl_registry *registry,
596 uint32_t id, const char *interface, uint32_t version)
597{
598 struct client *client = data;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800599 struct output *output;
600 struct test *test;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500601 struct global *global;
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400602 struct input *input;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500603
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700604 global = xzalloc(sizeof *global);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500605 global->name = id;
606 global->interface = strdup(interface);
607 assert(interface);
608 global->version = version;
609 wl_list_insert(client->global_list.prev, &global->link);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800610
611 if (strcmp(interface, "wl_compositor") == 0) {
612 client->wl_compositor =
613 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400614 &wl_compositor_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800615 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700616 input = xzalloc(sizeof *input);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800617 input->wl_seat =
618 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400619 &wl_seat_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800620 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400621 wl_list_insert(&client->inputs, &input->link);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800622 } else if (strcmp(interface, "wl_shm") == 0) {
623 client->wl_shm =
624 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400625 &wl_shm_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800626 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
627 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700628 output = xzalloc(sizeof *output);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800629 output->wl_output =
630 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400631 &wl_output_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800632 wl_output_add_listener(output->wl_output,
633 &output_listener, output);
634 client->output = output;
Derek Foremanf6a65922015-02-24 09:32:14 -0600635 } else if (strcmp(interface, "weston_test") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700636 test = xzalloc(sizeof *test);
Derek Foremanf6a65922015-02-24 09:32:14 -0600637 test->weston_test =
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800638 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400639 &weston_test_interface, version);
Derek Foremanf6a65922015-02-24 09:32:14 -0600640 weston_test_add_listener(test->weston_test, &test_listener, test);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800641 client->test = test;
Derek Foreman9bb13392015-01-23 12:12:36 -0600642 } else if (strcmp(interface, "wl_drm") == 0) {
643 client->has_wl_drm = true;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800644 }
645}
646
647static const struct wl_registry_listener registry_listener = {
648 handle_global
649};
650
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800651void
652skip(const char *fmt, ...)
653{
654 va_list argp;
655
656 va_start(argp, fmt);
657 vfprintf(stderr, fmt, argp);
658 va_end(argp);
659
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100660 /* automake tests uses exit code 77. weston-test-runner will see
661 * this and use it, and then weston-test's sigchld handler (in the
662 * weston process) will use that as an exit status, which is what
663 * automake will see in the end. */
664 exit(77);
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800665}
666
Marek Chalupa4d06d462014-07-16 11:27:06 +0200667void
668expect_protocol_error(struct client *client,
669 const struct wl_interface *intf,
670 uint32_t code)
671{
672 int err;
673 uint32_t errcode, failed = 0;
674 const struct wl_interface *interface;
675 unsigned int id;
676
677 /* if the error has not come yet, make it happen */
678 wl_display_roundtrip(client->wl_display);
679
680 err = wl_display_get_error(client->wl_display);
681
682 assert(err && "Expected protocol error but nothing came");
683 assert(err == EPROTO && "Expected protocol error but got local error");
684
685 errcode = wl_display_get_protocol_error(client->wl_display,
686 &interface, &id);
687
688 /* check error */
689 if (errcode != code) {
690 fprintf(stderr, "Should get error code %d but got %d\n",
691 code, errcode);
692 failed = 1;
693 }
694
695 /* this should be definitely set */
696 assert(interface);
697
698 if (strcmp(intf->name, interface->name) != 0) {
699 fprintf(stderr, "Should get interface '%s' but got '%s'\n",
700 intf->name, interface->name);
701 failed = 1;
702 }
703
704 if (failed) {
705 fprintf(stderr, "Expected other protocol error\n");
706 abort();
707 }
708
709 /* all OK */
710 fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) "
711 "with code %d\n", interface->name, id, errcode);
712}
713
Pekka Paalanen07921d72012-12-12 14:26:40 +0200714static void
715log_handler(const char *fmt, va_list args)
716{
717 fprintf(stderr, "libwayland: ");
718 vfprintf(stderr, fmt, args);
719}
720
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400721static void
722input_destroy(struct input *inp)
723{
724 wl_list_remove(&inp->link);
725 wl_seat_destroy(inp->wl_seat);
726 free(inp);
727}
728
729/* find the test-seat and set it in client.
730 * Destroy other inputs */
731static void
732client_set_input(struct client *cl)
733{
734 struct input *inp, *inptmp;
735 wl_list_for_each_safe(inp, inptmp, &cl->inputs, link) {
736 assert(inp->seat_name && "BUG: input with no name");
737 if (strcmp(inp->seat_name, "test-seat") == 0) {
738 cl->input = inp;
739 input_update_devices(inp);
740 } else {
741 input_destroy(inp);
742 }
743 }
744
745 /* we keep only one input */
746 assert(wl_list_length(&cl->inputs) == 1);
747}
748
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800749struct client *
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200750create_client(void)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800751{
752 struct client *client;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800753
Pekka Paalanen07921d72012-12-12 14:26:40 +0200754 wl_log_set_handler_client(log_handler);
755
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800756 /* connect to display */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700757 client = xzalloc(sizeof *client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800758 client->wl_display = wl_display_connect(NULL);
759 assert(client->wl_display);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500760 wl_list_init(&client->global_list);
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400761 wl_list_init(&client->inputs);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800762
763 /* setup registry so we can bind to interfaces */
764 client->wl_registry = wl_display_get_registry(client->wl_display);
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200765 wl_registry_add_listener(client->wl_registry, &registry_listener,
766 client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800767
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400768 /* this roundtrip makes sure we have all globals and we bound to them */
769 client_roundtrip(client);
770 /* this roundtrip makes sure we got all wl_shm.format and wl_seat.*
771 * events */
772 client_roundtrip(client);
773
774 /* find the right input for us */
775 client_set_input(client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800776
777 /* must have WL_SHM_FORMAT_ARGB32 */
778 assert(client->has_argb);
779
Derek Foremanf6a65922015-02-24 09:32:14 -0600780 /* must have weston_test interface */
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800781 assert(client->test);
782
783 /* must have an output */
784 assert(client->output);
785
Marek Chalupa643d85f2015-03-30 06:37:56 -0400786 /* the output must be initialized */
787 assert(client->output->initialized == 1);
788
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400789 /* must have seat set */
790 assert(client->input);
791
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200792 return client;
793}
794
795struct client *
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200796create_client_and_test_surface(int x, int y, int width, int height)
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200797{
798 struct client *client;
799 struct surface *surface;
800
801 client = create_client();
802
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800803 /* initialize the client surface */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700804 surface = xzalloc(sizeof *surface);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800805 surface->wl_surface =
806 wl_compositor_create_surface(client->wl_compositor);
807 assert(surface->wl_surface);
808
809 wl_surface_add_listener(surface->wl_surface, &surface_listener,
810 surface);
811
812 client->surface = surface;
813 wl_surface_set_user_data(surface->wl_surface, surface);
814
815 surface->width = width;
816 surface->height = height;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200817 surface->wl_buffer = create_shm_buffer(client, width, height,
818 &surface->data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800819
820 memset(surface->data, 64, width * height * 4);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800821
822 move_client(client, x, y);
823
824 return client;
825}
Bryce Harringtonc1a1d6c2014-12-09 14:46:36 -0800826
827static const char*
828output_path(void) {
829 char *path = getenv("WESTON_TEST_OUTPUT_PATH");
830
831 if (!path)
832 return ".";
833 return path;
834 }
835
836char*
837screenshot_output_filename(const char *basename, uint32_t seq) {
838 char *filename;
839
840 if (asprintf(&filename, "%s/%s-%02d.png",
841 output_path(), basename, seq) < 0)
842 return NULL;
843 return filename;
844}
845
846static const char*
847reference_path(void) {
848 char *path = getenv("WESTON_TEST_REFERENCE_PATH");
849
850 if (!path)
851 return "./tests/reference";
852 return path;
853}
854
855char*
856screenshot_reference_filename(const char *basename, uint32_t seq) {
857 char *filename;
858
859 if (asprintf(&filename, "%s/%s-%02d.png",
860 reference_path(), basename, seq) < 0)
861 return NULL;
862 return filename;
863}