fix weston launch error
[platform/upstream/weston.git] / tests / weston-test-client-helper.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * 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:
11  *
12  * 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.
24  */
25
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <sys/mman.h>
35 #include <cairo.h>
36
37 #include "shared/os-compatibility.h"
38 #include "shared/xalloc.h"
39 #include "shared/zalloc.h"
40 #include "weston-test-client-helper.h"
41
42 #define max(a, b) (((a) > (b)) ? (a) : (b))
43 #define min(a, b) (((a) > (b)) ? (b) : (a))
44 #define clip(x, a, b)  min(max(x, a), b)
45
46 int
47 surface_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
57 static void
58 frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
59 {
60         int *done = data;
61
62         *done = 1;
63
64         wl_callback_destroy(callback);
65 }
66
67 static const struct wl_callback_listener frame_listener = {
68         frame_callback_handler
69 };
70
71 struct wl_callback *
72 frame_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
83 int
84 frame_callback_wait_nofail(struct client *client, int *done)
85 {
86         while (!*done) {
87                 if (wl_display_dispatch(client->wl_display) < 0)
88                         return 0;
89         }
90
91         return 1;
92 }
93
94 void
95 move_client(struct client *client, int x, int y)
96 {
97         struct surface *surface = client->surface;
98         int done;
99
100         client->surface->x = x;
101         client->surface->y = y;
102         weston_test_move_surface(client->test->weston_test, surface->wl_surface,
103                              surface->x, surface->y);
104         /* The attach here is necessary because commit() will call configure
105          * 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->buffer->proxy, 0, 0);
108         wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
109                           surface->height);
110
111         frame_callback_set(surface->wl_surface, &done);
112
113         wl_surface_commit(surface->wl_surface);
114
115         frame_callback_wait(client, &done);
116 }
117
118 static void
119 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
120                      uint32_t serial, struct wl_surface *wl_surface,
121                      wl_fixed_t x, wl_fixed_t y)
122 {
123         struct pointer *pointer = data;
124
125         if (wl_surface)
126                 pointer->focus = wl_surface_get_user_data(wl_surface);
127         else
128                 pointer->focus = NULL;
129
130         pointer->x = wl_fixed_to_int(x);
131         pointer->y = wl_fixed_to_int(y);
132
133         fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
134                 pointer->x, pointer->y, pointer->focus);
135 }
136
137 static void
138 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
139                      uint32_t serial, struct wl_surface *wl_surface)
140 {
141         struct pointer *pointer = data;
142
143         pointer->focus = NULL;
144
145         fprintf(stderr, "test-client: got pointer leave, surface %p\n",
146                 wl_surface ? wl_surface_get_user_data(wl_surface) : NULL);
147 }
148
149 static void
150 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
151                       uint32_t time_msec, wl_fixed_t x, wl_fixed_t y)
152 {
153         struct pointer *pointer = data;
154
155         pointer->x = wl_fixed_to_int(x);
156         pointer->y = wl_fixed_to_int(y);
157         pointer->motion_time_msec = time_msec;
158         pointer->motion_time_timespec = pointer->input_timestamp;
159         pointer->input_timestamp = (struct timespec) { 0 };
160
161         fprintf(stderr, "test-client: got pointer motion %d %d\n",
162                 pointer->x, pointer->y);
163 }
164
165 static void
166 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
167                       uint32_t serial, uint32_t time_msec, uint32_t button,
168                       uint32_t state)
169 {
170         struct pointer *pointer = data;
171
172         pointer->button = button;
173         pointer->state = state;
174         pointer->button_time_msec = time_msec;
175         pointer->button_time_timespec = pointer->input_timestamp;
176         pointer->input_timestamp = (struct timespec) { 0 };
177
178         fprintf(stderr, "test-client: got pointer button %u %u\n",
179                 button, state);
180 }
181
182 static void
183 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
184                     uint32_t time_msec, uint32_t axis, wl_fixed_t value)
185 {
186         struct pointer *pointer = data;
187
188         pointer->axis = axis;
189         pointer->axis_value = wl_fixed_to_double(value);
190         pointer->axis_time_msec = time_msec;
191         pointer->axis_time_timespec = pointer->input_timestamp;
192         pointer->input_timestamp = (struct timespec) { 0 };
193
194         fprintf(stderr, "test-client: got pointer axis %u %f\n",
195                 axis, wl_fixed_to_double(value));
196 }
197
198 static void
199 pointer_handle_frame(void *data, struct wl_pointer *wl_pointer)
200 {
201         fprintf(stderr, "test-client: got pointer frame\n");
202 }
203
204 static void
205 pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer,
206                              uint32_t source)
207 {
208         fprintf(stderr, "test-client: got pointer axis source %u\n", source);
209 }
210
211 static void
212 pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer,
213                          uint32_t time_msec, uint32_t axis)
214 {
215         struct pointer *pointer = data;
216
217         pointer->axis = axis;
218         pointer->axis_stop_time_msec = time_msec;
219         pointer->axis_stop_time_timespec = pointer->input_timestamp;
220         pointer->input_timestamp = (struct timespec) { 0 };
221
222         fprintf(stderr, "test-client: got pointer axis stop %u\n", axis);
223 }
224
225 static void
226 pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer,
227                              uint32_t axis, int32_t value)
228 {
229         fprintf(stderr, "test-client: got pointer axis discrete %u %d\n",
230                 axis, value);
231 }
232
233 static const struct wl_pointer_listener pointer_listener = {
234         pointer_handle_enter,
235         pointer_handle_leave,
236         pointer_handle_motion,
237         pointer_handle_button,
238         pointer_handle_axis,
239         pointer_handle_frame,
240         pointer_handle_axis_source,
241         pointer_handle_axis_stop,
242         pointer_handle_axis_discrete,
243 };
244
245 static void
246 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
247                        uint32_t format, int fd, uint32_t size)
248 {
249         close(fd);
250
251         fprintf(stderr, "test-client: got keyboard keymap\n");
252 }
253
254 static void
255 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
256                       uint32_t serial, struct wl_surface *wl_surface,
257                       struct wl_array *keys)
258 {
259         struct keyboard *keyboard = data;
260
261         if (wl_surface)
262                 keyboard->focus = wl_surface_get_user_data(wl_surface);
263         else
264                 keyboard->focus = NULL;
265
266         fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
267                 keyboard->focus);
268 }
269
270 static void
271 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
272                       uint32_t serial, struct wl_surface *wl_surface)
273 {
274         struct keyboard *keyboard = data;
275
276         keyboard->focus = NULL;
277
278         fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
279                 wl_surface ? wl_surface_get_user_data(wl_surface) : NULL);
280 }
281
282 static void
283 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
284                     uint32_t serial, uint32_t time_msec, uint32_t key,
285                     uint32_t state)
286 {
287         struct keyboard *keyboard = data;
288
289         keyboard->key = key;
290         keyboard->state = state;
291         keyboard->key_time_msec = time_msec;
292         keyboard->key_time_timespec = keyboard->input_timestamp;
293         keyboard->input_timestamp = (struct timespec) { 0 };
294
295         fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
296 }
297
298 static void
299 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
300                           uint32_t serial, uint32_t mods_depressed,
301                           uint32_t mods_latched, uint32_t mods_locked,
302                           uint32_t group)
303 {
304         struct keyboard *keyboard = data;
305
306         keyboard->mods_depressed = mods_depressed;
307         keyboard->mods_latched = mods_latched;
308         keyboard->mods_locked = mods_locked;
309         keyboard->group = group;
310
311         fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
312                 mods_depressed, mods_latched, mods_locked, group);
313 }
314
315 static void
316 keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
317                             int32_t rate, int32_t delay)
318 {
319         struct keyboard *keyboard = data;
320
321         keyboard->repeat_info.rate = rate;
322         keyboard->repeat_info.delay = delay;
323
324         fprintf(stderr, "test-client: got keyboard repeat_info %d %d\n",
325                 rate, delay);
326 }
327
328 static const struct wl_keyboard_listener keyboard_listener = {
329         keyboard_handle_keymap,
330         keyboard_handle_enter,
331         keyboard_handle_leave,
332         keyboard_handle_key,
333         keyboard_handle_modifiers,
334         keyboard_handle_repeat_info,
335 };
336
337 static void
338 touch_handle_down(void *data, struct wl_touch *wl_touch,
339                   uint32_t serial, uint32_t time_msec,
340                   struct wl_surface *surface, int32_t id,
341                   wl_fixed_t x_w, wl_fixed_t y_w)
342 {
343         struct touch *touch = data;
344
345         touch->down_x = wl_fixed_to_int(x_w);
346         touch->down_y = wl_fixed_to_int(y_w);
347         touch->id = id;
348         touch->down_time_msec = time_msec;
349         touch->down_time_timespec = touch->input_timestamp;
350         touch->input_timestamp = (struct timespec) { 0 };
351
352         fprintf(stderr, "test-client: got touch down %d %d, surf: %p, id: %d\n",
353                 touch->down_x, touch->down_y, surface, id);
354 }
355
356 static void
357 touch_handle_up(void *data, struct wl_touch *wl_touch,
358                 uint32_t serial, uint32_t time_msec, int32_t id)
359 {
360         struct touch *touch = data;
361         touch->up_id = id;
362         touch->up_time_msec = time_msec;
363         touch->up_time_timespec = touch->input_timestamp;
364         touch->input_timestamp = (struct timespec) { 0 };
365
366         fprintf(stderr, "test-client: got touch up, id: %d\n", id);
367 }
368
369 static void
370 touch_handle_motion(void *data, struct wl_touch *wl_touch,
371                     uint32_t time_msec, int32_t id,
372                     wl_fixed_t x_w, wl_fixed_t y_w)
373 {
374         struct touch *touch = data;
375         touch->x = wl_fixed_to_int(x_w);
376         touch->y = wl_fixed_to_int(y_w);
377         touch->motion_time_msec = time_msec;
378         touch->motion_time_timespec = touch->input_timestamp;
379         touch->input_timestamp = (struct timespec) { 0 };
380
381         fprintf(stderr, "test-client: got touch motion, %d %d, id: %d\n",
382                 touch->x, touch->y, id);
383 }
384
385 static void
386 touch_handle_frame(void *data, struct wl_touch *wl_touch)
387 {
388         struct touch *touch = data;
389
390         ++touch->frame_no;
391
392         fprintf(stderr, "test-client: got touch frame (%d)\n", touch->frame_no);
393 }
394
395 static void
396 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
397 {
398         struct touch *touch = data;
399
400         ++touch->cancel_no;
401
402         fprintf(stderr, "test-client: got touch cancel (%d)\n", touch->cancel_no);
403 }
404
405 static const struct wl_touch_listener touch_listener = {
406         touch_handle_down,
407         touch_handle_up,
408         touch_handle_motion,
409         touch_handle_frame,
410         touch_handle_cancel,
411 };
412
413 static void
414 surface_enter(void *data,
415               struct wl_surface *wl_surface, struct wl_output *output)
416 {
417         struct surface *surface = data;
418
419         surface->output = wl_output_get_user_data(output);
420
421         fprintf(stderr, "test-client: got surface enter output %p\n",
422                 surface->output);
423 }
424
425 static void
426 surface_leave(void *data,
427               struct wl_surface *wl_surface, struct wl_output *output)
428 {
429         struct surface *surface = data;
430
431         surface->output = NULL;
432
433         fprintf(stderr, "test-client: got surface leave output %p\n",
434                 wl_output_get_user_data(output));
435 }
436
437 static const struct wl_surface_listener surface_listener = {
438         surface_enter,
439         surface_leave
440 };
441
442 static struct buffer *
443 create_shm_buffer(struct client *client, int width, int height,
444                   pixman_format_code_t format, uint32_t wlfmt)
445 {
446         struct wl_shm *shm = client->wl_shm;
447         struct buffer *buf;
448         size_t stride_bytes;
449         struct wl_shm_pool *pool;
450         int fd;
451         void *data;
452         size_t bytes_pp;
453
454         assert(width > 0);
455         assert(height > 0);
456
457         buf = xzalloc(sizeof *buf);
458
459         bytes_pp = PIXMAN_FORMAT_BPP(format) / 8;
460         stride_bytes = width * bytes_pp;
461         /* round up to multiple of 4 bytes for Pixman */
462         stride_bytes = (stride_bytes + 3) & ~3u;
463         assert(stride_bytes / bytes_pp >= (unsigned)width);
464
465         buf->len = stride_bytes * height;
466         assert(buf->len / stride_bytes == (unsigned)height);
467
468         fd = os_create_anonymous_file(buf->len);
469         assert(fd >= 0);
470
471         data = mmap(NULL, buf->len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
472         if (data == MAP_FAILED) {
473                 close(fd);
474                 assert(data != MAP_FAILED);
475         }
476
477         pool = wl_shm_create_pool(shm, fd, buf->len);
478         buf->proxy = wl_shm_pool_create_buffer(pool, 0, width, height,
479                                                stride_bytes, wlfmt);
480         wl_shm_pool_destroy(pool);
481         close(fd);
482
483         buf->image = pixman_image_create_bits(format, width, height,
484                                               data, stride_bytes);
485
486         assert(buf->proxy);
487         assert(buf->image);
488
489         return buf;
490 }
491
492 struct buffer *
493 create_shm_buffer_a8r8g8b8(struct client *client, int width, int height)
494 {
495         assert(client->has_argb);
496
497         return create_shm_buffer(client, width, height,
498                                  PIXMAN_a8r8g8b8, WL_SHM_FORMAT_ARGB8888);
499 }
500
501 void
502 buffer_destroy(struct buffer *buf)
503 {
504         void *pixels;
505
506         pixels = pixman_image_get_data(buf->image);
507
508         if (buf->proxy) {
509                 wl_buffer_destroy(buf->proxy);
510                 assert(munmap(pixels, buf->len) == 0);
511         }
512
513         assert(pixman_image_unref(buf->image));
514
515         free(buf);
516 }
517
518 static void
519 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
520 {
521         struct client *client = data;
522
523         if (format == WL_SHM_FORMAT_ARGB8888)
524                 client->has_argb = 1;
525 }
526
527 struct wl_shm_listener shm_listener = {
528         shm_format
529 };
530
531 static void
532 test_handle_pointer_position(void *data, struct weston_test *weston_test,
533                              wl_fixed_t x, wl_fixed_t y)
534 {
535         struct test *test = data;
536         test->pointer_x = wl_fixed_to_int(x);
537         test->pointer_y = wl_fixed_to_int(y);
538
539         fprintf(stderr, "test-client: got global pointer %d %d\n",
540                 test->pointer_x, test->pointer_y);
541 }
542
543 static void
544 test_handle_capture_screenshot_done(void *data, struct weston_test *weston_test)
545 {
546         struct test *test = data;
547
548         printf("Screenshot has been captured\n");
549         test->buffer_copy_done = 1;
550 }
551
552 static const struct weston_test_listener test_listener = {
553         test_handle_pointer_position,
554         test_handle_capture_screenshot_done,
555 };
556
557 static void
558 input_destroy(struct input *inp)
559 {
560         if (inp->pointer) {
561                 wl_pointer_release(inp->pointer->wl_pointer);
562                 free(inp->pointer);
563         }
564         if (inp->keyboard) {
565                 wl_keyboard_release(inp->keyboard->wl_keyboard);
566                 free(inp->keyboard);
567         }
568         if (inp->touch) {
569                 wl_touch_release(inp->touch->wl_touch);
570                 free(inp->touch);
571         }
572         wl_list_remove(&inp->link);
573         wl_seat_release(inp->wl_seat);
574         free(inp->seat_name);
575         free(inp);
576 }
577
578 static void
579 input_update_devices(struct input *input)
580 {
581         struct pointer *pointer;
582         struct keyboard *keyboard;
583         struct touch *touch;
584
585         struct wl_seat *seat = input->wl_seat;
586         enum wl_seat_capability caps = input->caps;
587
588         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
589                 pointer = xzalloc(sizeof *pointer);
590                 pointer->wl_pointer = wl_seat_get_pointer(seat);
591                 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
592                 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
593                                         pointer);
594                 input->pointer = pointer;
595         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
596                 wl_pointer_destroy(input->pointer->wl_pointer);
597                 free(input->pointer);
598                 input->pointer = NULL;
599         }
600
601         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
602                 keyboard = xzalloc(sizeof *keyboard);
603                 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
604                 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
605                 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
606                                          keyboard);
607                 input->keyboard = keyboard;
608         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
609                 wl_keyboard_destroy(input->keyboard->wl_keyboard);
610                 free(input->keyboard);
611                 input->keyboard = NULL;
612         }
613
614         if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
615                 touch = xzalloc(sizeof *touch);
616                 touch->wl_touch = wl_seat_get_touch(seat);
617                 wl_touch_set_user_data(touch->wl_touch, touch);
618                 wl_touch_add_listener(touch->wl_touch, &touch_listener,
619                                          touch);
620                 input->touch = touch;
621         } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
622                 wl_touch_destroy(input->touch->wl_touch);
623                 free(input->touch);
624                 input->touch = NULL;
625         }
626 }
627
628 static void
629 seat_handle_capabilities(void *data, struct wl_seat *seat,
630                          enum wl_seat_capability caps)
631 {
632         struct input *input = data;
633
634         input->caps = caps;
635
636         /* we will create/update the devices only with the right (test) seat.
637          * If we haven't discovered which seat is the test seat, just
638          * store capabilities and bail out */
639         if (input->seat_name && strcmp(input->seat_name, "test-seat") == 0)
640                 input_update_devices(input);
641
642         fprintf(stderr, "test-client: got seat %p capabilities: %x\n",
643                 input, caps);
644 }
645
646 static void
647 seat_handle_name(void *data, struct wl_seat *seat, const char *name)
648 {
649         struct input *input = data;
650
651         input->seat_name = strdup(name);
652         assert(input->seat_name && "No memory");
653
654         /* We only update the devices and set client input for the test seat */
655         if (strcmp(name, "test-seat") == 0) {
656                 assert(!input->client->input &&
657                        "Multiple test seats detected!");
658
659                 input_update_devices(input);
660                 input->client->input = input;
661         }
662
663         fprintf(stderr, "test-client: got seat %p name: \'%s\'\n",
664                 input, name);
665 }
666
667 static const struct wl_seat_listener seat_listener = {
668         seat_handle_capabilities,
669         seat_handle_name,
670 };
671
672 static void
673 output_handle_geometry(void *data,
674                        struct wl_output *wl_output,
675                        int x, int y,
676                        int physical_width,
677                        int physical_height,
678                        int subpixel,
679                        const char *make,
680                        const char *model,
681                        int32_t transform)
682 {
683         struct output *output = data;
684
685         output->x = x;
686         output->y = y;
687 }
688
689 static void
690 output_handle_mode(void *data,
691                    struct wl_output *wl_output,
692                    uint32_t flags,
693                    int width,
694                    int height,
695                    int refresh)
696 {
697         struct output *output = data;
698
699         if (flags & WL_OUTPUT_MODE_CURRENT) {
700                 output->width = width;
701                 output->height = height;
702         }
703 }
704
705 static void
706 output_handle_scale(void *data,
707                     struct wl_output *wl_output,
708                     int scale)
709 {
710         struct output *output = data;
711
712         output->scale = scale;
713 }
714
715 static void
716 output_handle_done(void *data,
717                    struct wl_output *wl_output)
718 {
719         struct output *output = data;
720
721         output->initialized = 1;
722 }
723
724 static const struct wl_output_listener output_listener = {
725         output_handle_geometry,
726         output_handle_mode,
727         output_handle_done,
728         output_handle_scale,
729 };
730
731 static void
732 handle_global(void *data, struct wl_registry *registry,
733               uint32_t id, const char *interface, uint32_t version)
734 {
735         struct client *client = data;
736         struct output *output;
737         struct test *test;
738         struct global *global;
739         struct input *input;
740
741         global = xzalloc(sizeof *global);
742         global->name = id;
743         global->interface = strdup(interface);
744         assert(interface);
745         global->version = version;
746         wl_list_insert(client->global_list.prev, &global->link);
747
748         if (strcmp(interface, "wl_compositor") == 0) {
749                 client->wl_compositor =
750                         wl_registry_bind(registry, id,
751                                          &wl_compositor_interface, version);
752         } else if (strcmp(interface, "wl_seat") == 0) {
753                 input = xzalloc(sizeof *input);
754                 input->client = client;
755                 input->global_name = global->name;
756                 input->wl_seat =
757                         wl_registry_bind(registry, id,
758                                          &wl_seat_interface, version);
759                 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
760                 wl_list_insert(&client->inputs, &input->link);
761         } else if (strcmp(interface, "wl_shm") == 0) {
762                 client->wl_shm =
763                         wl_registry_bind(registry, id,
764                                          &wl_shm_interface, version);
765                 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
766         } else if (strcmp(interface, "wl_output") == 0) {
767                 output = xzalloc(sizeof *output);
768                 output->wl_output =
769                         wl_registry_bind(registry, id,
770                                          &wl_output_interface, version);
771                 wl_output_add_listener(output->wl_output,
772                                        &output_listener, output);
773                 client->output = output;
774         } else if (strcmp(interface, "weston_test") == 0) {
775                 test = xzalloc(sizeof *test);
776                 test->weston_test =
777                         wl_registry_bind(registry, id,
778                                          &weston_test_interface, version);
779                 weston_test_add_listener(test->weston_test, &test_listener, test);
780                 client->test = test;
781         } else if (strcmp(interface, "wl_drm") == 0) {
782                 client->has_wl_drm = true;
783         }
784 }
785
786 static struct global *
787 client_find_global_with_name(struct client *client, uint32_t name)
788 {
789         struct global *global;
790
791         wl_list_for_each(global, &client->global_list, link) {
792                 if (global->name == name)
793                         return global;
794         }
795
796         return NULL;
797 }
798
799 static struct input *
800 client_find_input_with_name(struct client *client, uint32_t name)
801 {
802         struct input *input;
803
804         wl_list_for_each(input, &client->inputs, link) {
805                 if (input->global_name == name)
806                         return input;
807         }
808
809         return NULL;
810 }
811
812 static void
813 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
814 {
815         struct client *client = data;
816         struct global *global;
817         struct input *input;
818
819         global = client_find_global_with_name(client, name);
820         assert(global && "Request to remove unknown global");
821
822         if (strcmp(global->interface, "wl_seat") == 0) {
823                 input = client_find_input_with_name(client, name);
824                 if (input) {
825                         if (client->input == input)
826                                 client->input = NULL;
827                         input_destroy(input);
828                 }
829         }
830
831         wl_list_remove(&global->link);
832         free(global->interface);
833         free(global);
834 }
835
836 static const struct wl_registry_listener registry_listener = {
837         handle_global,
838         handle_global_remove,
839 };
840
841 void
842 skip(const char *fmt, ...)
843 {
844         va_list argp;
845
846         va_start(argp, fmt);
847         vfprintf(stderr, fmt, argp);
848         va_end(argp);
849
850         /* automake tests uses exit code 77. weston-test-runner will see
851          * this and use it, and then weston-test's sigchld handler (in the
852          * weston process) will use that as an exit status, which is what
853          * automake will see in the end. */
854         exit(77);
855 }
856
857 void
858 expect_protocol_error(struct client *client,
859                       const struct wl_interface *intf,
860                       uint32_t code)
861 {
862         int err;
863         uint32_t errcode, failed = 0;
864         const struct wl_interface *interface;
865         unsigned int id;
866
867         /* if the error has not come yet, make it happen */
868         wl_display_roundtrip(client->wl_display);
869
870         err = wl_display_get_error(client->wl_display);
871
872         assert(err && "Expected protocol error but nothing came");
873         assert(err == EPROTO && "Expected protocol error but got local error");
874
875         errcode = wl_display_get_protocol_error(client->wl_display,
876                                                 &interface, &id);
877
878         /* check error */
879         if (errcode != code) {
880                 fprintf(stderr, "Should get error code %d but got %d\n",
881                         code, errcode);
882                 failed = 1;
883         }
884
885         /* this should be definitely set */
886         assert(interface);
887
888         if (strcmp(intf->name, interface->name) != 0) {
889                 fprintf(stderr, "Should get interface '%s' but got '%s'\n",
890                         intf->name, interface->name);
891                 failed = 1;
892         }
893
894         if (failed) {
895                 fprintf(stderr, "Expected other protocol error\n");
896                 abort();
897         }
898
899         /* all OK */
900         fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) "
901                         "with code %d\n", interface->name, id, errcode);
902 }
903
904 static void
905 log_handler(const char *fmt, va_list args)
906 {
907         fprintf(stderr, "libwayland: ");
908         vfprintf(stderr, fmt, args);
909 }
910
911 struct client *
912 create_client(void)
913 {
914         struct client *client;
915
916         wl_log_set_handler_client(log_handler);
917
918         /* connect to display */
919         client = xzalloc(sizeof *client);
920         client->wl_display = wl_display_connect(NULL);
921         assert(client->wl_display);
922         wl_list_init(&client->global_list);
923         wl_list_init(&client->inputs);
924
925         /* setup registry so we can bind to interfaces */
926         client->wl_registry = wl_display_get_registry(client->wl_display);
927         wl_registry_add_listener(client->wl_registry, &registry_listener,
928                                  client);
929
930         /* this roundtrip makes sure we have all globals and we bound to them */
931         client_roundtrip(client);
932         /* this roundtrip makes sure we got all wl_shm.format and wl_seat.*
933          * events */
934         client_roundtrip(client);
935
936         /* must have WL_SHM_FORMAT_ARGB32 */
937         assert(client->has_argb);
938
939         /* must have weston_test interface */
940         assert(client->test);
941
942         /* must have an output */
943         assert(client->output);
944
945         /* the output must be initialized */
946         assert(client->output->initialized == 1);
947
948         /* must have seat set */
949         assert(client->input);
950
951         return client;
952 }
953
954 struct surface *
955 create_test_surface(struct client *client)
956 {
957         struct surface *surface;
958
959         surface = xzalloc(sizeof *surface);
960
961         surface->wl_surface =
962                 wl_compositor_create_surface(client->wl_compositor);
963         assert(surface->wl_surface);
964
965         wl_surface_add_listener(surface->wl_surface, &surface_listener,
966                                 surface);
967
968         wl_surface_set_user_data(surface->wl_surface, surface);
969
970         return surface;
971 }
972
973 struct client *
974 create_client_and_test_surface(int x, int y, int width, int height)
975 {
976         struct client *client;
977         struct surface *surface;
978         pixman_color_t color = { 16384, 16384, 16384, 16384 }; /* uint16_t */
979         pixman_image_t *solid;
980
981         client = create_client();
982
983         /* initialize the client surface */
984         surface = create_test_surface(client);
985         client->surface = surface;
986
987         surface->width = width;
988         surface->height = height;
989         surface->buffer = create_shm_buffer_a8r8g8b8(client, width, height);
990
991         solid = pixman_image_create_solid_fill(&color);
992         pixman_image_composite32(PIXMAN_OP_SRC,
993                                  solid, /* src */
994                                  NULL, /* mask */
995                                  surface->buffer->image, /* dst */
996                                  0, 0, /* src x,y */
997                                  0, 0, /* mask x,y */
998                                  0, 0, /* dst x,y */
999                                  width, height);
1000         pixman_image_unref(solid);
1001
1002         move_client(client, x, y);
1003
1004         return client;
1005 }
1006
1007 static const char*
1008 output_path(void)
1009 {
1010         char *path = getenv("WESTON_TEST_OUTPUT_PATH");
1011
1012         if (!path)
1013                 return "./logs";
1014
1015         return path;
1016 }
1017
1018 char*
1019 screenshot_output_filename(const char *basename, uint32_t seq)
1020 {
1021         char *filename;
1022
1023         if (asprintf(&filename, "%s/%s-%02d.png",
1024                                  output_path(), basename, seq) < 0)
1025                 return NULL;
1026         return filename;
1027 }
1028
1029 static const char*
1030 reference_path(void)
1031 {
1032         char *path = getenv("WESTON_TEST_REFERENCE_PATH");
1033
1034         if (!path)
1035                 return "./tests/reference";
1036         return path;
1037 }
1038
1039 char*
1040 screenshot_reference_filename(const char *basename, uint32_t seq)
1041 {
1042         char *filename;
1043
1044         if (asprintf(&filename, "%s/%s-%02d.png",
1045                                  reference_path(), basename, seq) < 0)
1046                 return NULL;
1047         return filename;
1048 }
1049
1050 struct format_map_entry {
1051         cairo_format_t cairo;
1052         pixman_format_code_t pixman;
1053 };
1054
1055 static const struct format_map_entry format_map[] = {
1056         { CAIRO_FORMAT_ARGB32,    PIXMAN_a8r8g8b8 },
1057         { CAIRO_FORMAT_RGB24,     PIXMAN_x8r8g8b8 },
1058         { CAIRO_FORMAT_A8,        PIXMAN_a8 },
1059         { CAIRO_FORMAT_RGB16_565, PIXMAN_r5g6b5 },
1060 };
1061
1062 static pixman_format_code_t
1063 format_cairo2pixman(cairo_format_t fmt)
1064 {
1065         unsigned i;
1066
1067         for (i = 0; i < ARRAY_LENGTH(format_map); i++)
1068                 if (format_map[i].cairo == fmt)
1069                         return format_map[i].pixman;
1070
1071         assert(0 && "unknown Cairo pixel format");
1072 }
1073
1074 static cairo_format_t
1075 format_pixman2cairo(pixman_format_code_t fmt)
1076 {
1077         unsigned i;
1078
1079         for (i = 0; i < ARRAY_LENGTH(format_map); i++)
1080                 if (format_map[i].pixman == fmt)
1081                         return format_map[i].cairo;
1082
1083         assert(0 && "unknown Pixman pixel format");
1084 }
1085
1086 /**
1087  * Compute the ROI for image comparisons
1088  *
1089  * \param img_a An image.
1090  * \param img_b Another image.
1091  * \param clip_rect Explicit ROI, or NULL for using the whole
1092  * image area.
1093  *
1094  * \return The region of interest (ROI) that is guaranteed to be inside both
1095  * images.
1096  *
1097  * If clip_rect is given, it must fall inside of both images.
1098  * If clip_rect is NULL, the images must be of the same size.
1099  * If any precondition is violated, this function aborts with an error.
1100  *
1101  * The ROI is given as pixman_box32_t, where x2,y2 are non-inclusive.
1102  */
1103 static pixman_box32_t
1104 image_check_get_roi(pixman_image_t *img_a, pixman_image_t *img_b,
1105                    const struct rectangle *clip_rect)
1106 {
1107         int width_a;
1108         int width_b;
1109         int height_a;
1110         int height_b;
1111         pixman_box32_t box;
1112
1113         width_a = pixman_image_get_width(img_a);
1114         height_a = pixman_image_get_height(img_a);
1115
1116         width_b = pixman_image_get_width(img_b);
1117         height_b = pixman_image_get_height(img_b);
1118
1119         if (clip_rect) {
1120                 box.x1 = clip_rect->x;
1121                 box.y1 = clip_rect->y;
1122                 box.x2 = clip_rect->x + clip_rect->width;
1123                 box.y2 = clip_rect->y + clip_rect->height;
1124         } else {
1125                 box.x1 = 0;
1126                 box.y1 = 0;
1127                 box.x2 = max(width_a, width_b);
1128                 box.y2 = max(height_a, height_b);
1129         }
1130
1131         assert(box.x1 >= 0);
1132         assert(box.y1 >= 0);
1133         assert(box.x2 > box.x1);
1134         assert(box.y2 > box.y1);
1135         assert(box.x2 <= width_a);
1136         assert(box.x2 <= width_b);
1137         assert(box.y2 <= height_a);
1138         assert(box.y2 <= height_b);
1139
1140         return box;
1141 }
1142
1143 struct image_iterator {
1144         char *data;
1145         int stride; /* bytes */
1146 };
1147
1148 static void
1149 image_iter_init(struct image_iterator *it, pixman_image_t *image)
1150 {
1151         pixman_format_code_t fmt;
1152
1153         it->stride = pixman_image_get_stride(image);
1154         it->data = (void *)pixman_image_get_data(image);
1155
1156         fmt = pixman_image_get_format(image);
1157         assert(PIXMAN_FORMAT_BPP(fmt) == 32);
1158 }
1159
1160 static uint32_t *
1161 image_iter_get_row(struct image_iterator *it, int y)
1162 {
1163         return (uint32_t *)(it->data + y * it->stride);
1164 }
1165
1166 /**
1167  * Test if a given region within two images are pixel-identical.
1168  *
1169  * Returns true if the two images pixel-wise identical, and false otherwise.
1170  *
1171  * \param img_a First image.
1172  * \param img_b Second image.
1173  * \param clip_rect The region of interest, or NULL for comparing the whole
1174  * images.
1175  *
1176  * This function hard-fails if clip_rect is not inside both images. If clip_rect
1177  * is given, the images do not have to match in size, otherwise size mismatch
1178  * will be a hard failure.
1179  */
1180 bool
1181 check_images_match(pixman_image_t *img_a, pixman_image_t *img_b,
1182                    const struct rectangle *clip_rect)
1183 {
1184         struct image_iterator it_a;
1185         struct image_iterator it_b;
1186         pixman_box32_t box;
1187         int x, y;
1188         uint32_t *pix_a;
1189         uint32_t *pix_b;
1190
1191         box = image_check_get_roi(img_a, img_b, clip_rect);
1192
1193         image_iter_init(&it_a, img_a);
1194         image_iter_init(&it_b, img_b);
1195
1196         for (y = box.y1; y < box.y2; y++) {
1197                 pix_a = image_iter_get_row(&it_a, y) + box.x1;
1198                 pix_b = image_iter_get_row(&it_b, y) + box.x1;
1199
1200                 for (x = box.x1; x < box.x2; x++) {
1201                         if (*pix_a != *pix_b)
1202                                 return false;
1203
1204                         pix_a++;
1205                         pix_b++;
1206                 }
1207         }
1208
1209         return true;
1210 }
1211
1212 /**
1213  * Tint a color
1214  *
1215  * \param src Source pixel as x8r8g8b8.
1216  * \param add The tint as x8r8g8b8, x8 must be zero; r8, g8 and b8 must be
1217  * no greater than 0xc0 to avoid overflow to another channel.
1218  * \return The tinted pixel color as x8r8g8b8, x8 guaranteed to be 0xff.
1219  *
1220  * The source pixel RGB values are divided by 4, and then the tint is added.
1221  * To achieve colors outside of the range of src, a tint color channel must be
1222  * at least 0x40. (0xff / 4 = 0x3f, 0xff - 0x3f = 0xc0)
1223  */
1224 static uint32_t
1225 tint(uint32_t src, uint32_t add)
1226 {
1227         uint32_t v;
1228
1229         v = ((src & 0xfcfcfcfc) >> 2) | 0xff000000;
1230
1231         return v + add;
1232 }
1233
1234 /**
1235  * Create a visualization of image differences.
1236  *
1237  * \param img_a First image, which is used as the basis for the output.
1238  * \param img_b Second image.
1239  * \param clip_rect The region of interest, or NULL for comparing the whole
1240  * images.
1241  * \return A new image with the differences highlighted.
1242  *
1243  * Regions outside of the region of interest are shaded with black, matching
1244  * pixels are shaded with green, and differing pixels are shaded with
1245  * bright red.
1246  *
1247  * This function hard-fails if clip_rect is not inside both images. If clip_rect
1248  * is given, the images do not have to match in size, otherwise size mismatch
1249  * will be a hard failure.
1250  */
1251 pixman_image_t *
1252 visualize_image_difference(pixman_image_t *img_a, pixman_image_t *img_b,
1253                            const struct rectangle *clip_rect)
1254 {
1255         pixman_image_t *diffimg;
1256         pixman_image_t *shade;
1257         struct image_iterator it_a;
1258         struct image_iterator it_b;
1259         struct image_iterator it_d;
1260         int width;
1261         int height;
1262         pixman_box32_t box;
1263         int x, y;
1264         uint32_t *pix_a;
1265         uint32_t *pix_b;
1266         uint32_t *pix_d;
1267         pixman_color_t shade_color = { 0, 0, 0, 32768 };
1268
1269         width = pixman_image_get_width(img_a);
1270         height = pixman_image_get_height(img_a);
1271         box = image_check_get_roi(img_a, img_b, clip_rect);
1272
1273         diffimg = pixman_image_create_bits_no_clear(PIXMAN_x8r8g8b8,
1274                                                     width, height, NULL, 0);
1275
1276         /* Fill diffimg with a black-shaded copy of img_a, and then fill
1277          * the clip_rect area with original img_a.
1278          */
1279         shade = pixman_image_create_solid_fill(&shade_color);
1280         pixman_image_composite32(PIXMAN_OP_SRC, img_a, shade, diffimg,
1281                                  0, 0, 0, 0, 0, 0, width, height);
1282         pixman_image_unref(shade);
1283         pixman_image_composite32(PIXMAN_OP_SRC, img_a, NULL, diffimg,
1284                                  box.x1, box.y1, 0, 0, box.x1, box.y1,
1285                                  box.x2 - box.x1, box.y2 - box.y1);
1286
1287         image_iter_init(&it_a, img_a);
1288         image_iter_init(&it_b, img_b);
1289         image_iter_init(&it_d, diffimg);
1290
1291         for (y = box.y1; y < box.y2; y++) {
1292                 pix_a = image_iter_get_row(&it_a, y) + box.x1;
1293                 pix_b = image_iter_get_row(&it_b, y) + box.x1;
1294                 pix_d = image_iter_get_row(&it_d, y) + box.x1;
1295
1296                 for (x = box.x1; x < box.x2; x++) {
1297                         if (*pix_a == *pix_b)
1298                                 *pix_d = tint(*pix_d, 0x00008000); /* green */
1299                         else
1300                                 *pix_d = tint(*pix_d, 0x00c00000); /* red */
1301
1302                         pix_a++;
1303                         pix_b++;
1304                         pix_d++;
1305                 }
1306         }
1307
1308         return diffimg;
1309 }
1310
1311 /**
1312  * Write an image into a PNG file.
1313  *
1314  * \param image The image.
1315  * \param fname The name and path for the file.
1316  *
1317  * \returns true if successfully saved file; false otherwise.
1318  *
1319  * \note Only image formats directly supported by Cairo are accepted, not all
1320  * Pixman formats.
1321  */
1322 bool
1323 write_image_as_png(pixman_image_t *image, const char *fname)
1324 {
1325         cairo_surface_t *cairo_surface;
1326         cairo_status_t status;
1327         cairo_format_t fmt;
1328
1329         fmt = format_pixman2cairo(pixman_image_get_format(image));
1330
1331         cairo_surface = cairo_image_surface_create_for_data(
1332                         (void *)pixman_image_get_data(image),
1333                         fmt,
1334                         pixman_image_get_width(image),
1335                         pixman_image_get_height(image),
1336                         pixman_image_get_stride(image));
1337
1338         status = cairo_surface_write_to_png(cairo_surface, fname);
1339         if (status != CAIRO_STATUS_SUCCESS) {
1340                 fprintf(stderr, "Failed to save image '%s': %s\n", fname,
1341                         cairo_status_to_string(status));
1342
1343                 return false;
1344         }
1345
1346         cairo_surface_destroy(cairo_surface);
1347
1348         return true;
1349 }
1350
1351 static pixman_image_t *
1352 image_convert_to_a8r8g8b8(pixman_image_t *image)
1353 {
1354         pixman_image_t *ret;
1355         int width;
1356         int height;
1357
1358         if (pixman_image_get_format(image) == PIXMAN_a8r8g8b8)
1359                 return pixman_image_ref(image);
1360
1361         width = pixman_image_get_width(image);
1362         height = pixman_image_get_height(image);
1363
1364         ret = pixman_image_create_bits_no_clear(PIXMAN_a8r8g8b8, width, height,
1365                                                 NULL, 0);
1366         assert(ret);
1367
1368         pixman_image_composite32(PIXMAN_OP_SRC, image, NULL, ret,
1369                                  0, 0, 0, 0, 0, 0, width, height);
1370
1371         return ret;
1372 }
1373
1374 static void
1375 destroy_cairo_surface(pixman_image_t *image, void *data)
1376 {
1377         cairo_surface_t *surface = data;
1378
1379         cairo_surface_destroy(surface);
1380 }
1381
1382 /**
1383  * Load an image from a PNG file
1384  *
1385  * Reads a PNG image from disk using the given filename (and path)
1386  * and returns as a Pixman image. Use pixman_image_unref() to free it.
1387  *
1388  * The returned image is always in PIXMAN_a8r8g8b8 format.
1389  *
1390  * @returns Pixman image, or NULL in case of error.
1391  */
1392 pixman_image_t *
1393 load_image_from_png(const char *fname)
1394 {
1395         pixman_image_t *image;
1396         pixman_image_t *converted;
1397         cairo_format_t cairo_fmt;
1398         pixman_format_code_t pixman_fmt;
1399         cairo_surface_t *reference_cairo_surface;
1400         cairo_status_t status;
1401         int width;
1402         int height;
1403         int stride;
1404         void *data;
1405
1406         reference_cairo_surface = cairo_image_surface_create_from_png(fname);
1407         cairo_surface_flush(reference_cairo_surface);
1408         status = cairo_surface_status(reference_cairo_surface);
1409         if (status != CAIRO_STATUS_SUCCESS) {
1410                 printf("Could not open %s: %s\n", fname, cairo_status_to_string(status));
1411                 cairo_surface_destroy(reference_cairo_surface);
1412                 return NULL;
1413         }
1414
1415         cairo_fmt = cairo_image_surface_get_format(reference_cairo_surface);
1416         pixman_fmt = format_cairo2pixman(cairo_fmt);
1417
1418         width = cairo_image_surface_get_width(reference_cairo_surface);
1419         height = cairo_image_surface_get_height(reference_cairo_surface);
1420         stride = cairo_image_surface_get_stride(reference_cairo_surface);
1421         data = cairo_image_surface_get_data(reference_cairo_surface);
1422
1423         /* The Cairo surface will own the data, so we keep it around. */
1424         image = pixman_image_create_bits_no_clear(pixman_fmt,
1425                                                   width, height, data, stride);
1426         assert(image);
1427
1428         pixman_image_set_destroy_function(image, destroy_cairo_surface,
1429                                           reference_cairo_surface);
1430
1431         converted = image_convert_to_a8r8g8b8(image);
1432         pixman_image_unref(image);
1433
1434         return converted;
1435 }
1436
1437 /**
1438  * Take screenshot of a single output
1439  *
1440  * Requests a screenshot from the server of the output that the
1441  * client appears on. This implies that the compositor goes through an output
1442  * repaint to provide the screenshot before this function returns. This
1443  * function is therefore both a server roundtrip and a wait for a repaint.
1444  *
1445  * @returns A new buffer object, that should be freed with buffer_destroy().
1446  */
1447 struct buffer *
1448 capture_screenshot_of_output(struct client *client)
1449 {
1450         struct buffer *buffer;
1451
1452         buffer = create_shm_buffer_a8r8g8b8(client,
1453                                             client->output->width,
1454                                             client->output->height);
1455
1456         client->test->buffer_copy_done = 0;
1457         weston_test_capture_screenshot(client->test->weston_test,
1458                                        client->output->wl_output,
1459                                        buffer->proxy);
1460         while (client->test->buffer_copy_done == 0)
1461                 if (wl_display_dispatch(client->wl_display) < 0)
1462                         break;
1463
1464         /* FIXME: Document somewhere the orientation the screenshot is taken
1465          * and how the clip coords are interpreted, in case of scaling/transform.
1466          * If we're using read_pixels() just make sure it is documented somewhere.
1467          * Protocol docs in the XML, comparison function docs in Doxygen style.
1468          */
1469
1470         return buffer;
1471 }