downstream: ivi-shell: configure the ivi surface when created
[profile/ivi/weston-ivi-shell.git] / tests / weston-test-client-helper.c
1 /*
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
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/mman.h>
31
32 #include "../shared/os-compatibility.h"
33 #include "weston-test-client-helper.h"
34
35 static inline void *
36 xzalloc(size_t size)
37 {
38         void *p;
39
40         p = calloc(1, size);
41         assert(p);
42
43         return p;
44 }
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         wl_test_move_surface(client->test->wl_test, surface->wl_surface,
103                              surface->x, surface->y);
104         /* The attach here is necessary because commit() will call congfigure
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->wl_buffer, 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 int
119 get_n_egl_buffers(struct client *client)
120 {
121         client->test->n_egl_buffers = -1;
122
123         wl_test_get_n_egl_buffers(client->test->wl_test);
124         wl_display_roundtrip(client->wl_display);
125
126         return client->test->n_egl_buffers;
127 }
128
129 static void
130 pointer_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
144 static void
145 pointer_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
156 static void
157 pointer_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
169 static void
170 pointer_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
183 static void
184 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
185                     uint32_t time, uint32_t axis, wl_fixed_t value)
186 {
187         fprintf(stderr, "test-client: got pointer axis %u %f\n",
188                 axis, wl_fixed_to_double(value));
189 }
190
191 static 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
199 static void
200 keyboard_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
208 static void
209 keyboard_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
221 static void
222 keyboard_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
233 static void
234 keyboard_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
246 static void
247 keyboard_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
263 static const struct wl_keyboard_listener keyboard_listener = {
264         keyboard_handle_keymap,
265         keyboard_handle_enter,
266         keyboard_handle_leave,
267         keyboard_handle_key,
268         keyboard_handle_modifiers,
269 };
270
271 static void
272 surface_enter(void *data,
273               struct wl_surface *wl_surface, struct wl_output *output)
274 {
275         struct surface *surface = data;
276
277         surface->output = wl_output_get_user_data(output);
278
279         fprintf(stderr, "test-client: got surface enter output %p\n",
280                 surface->output);
281 }
282
283 static void
284 surface_leave(void *data,
285               struct wl_surface *wl_surface, struct wl_output *output)
286 {
287         struct surface *surface = data;
288
289         surface->output = NULL;
290
291         fprintf(stderr, "test-client: got surface leave output %p\n",
292                 wl_output_get_user_data(output));
293 }
294
295 static const struct wl_surface_listener surface_listener = {
296         surface_enter,
297         surface_leave
298 };
299
300 struct wl_buffer *
301 create_shm_buffer(struct client *client, int width, int height, void **pixels)
302 {
303         struct wl_shm *shm = client->wl_shm;
304         int stride = width * 4;
305         int size = stride * height;
306         struct wl_shm_pool *pool;
307         struct wl_buffer *buffer;
308         int fd;
309         void *data;
310
311         fd = os_create_anonymous_file(size);
312         assert(fd >= 0);
313
314         data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
315         if (data == MAP_FAILED) {
316                 close(fd);
317                 assert(data != MAP_FAILED);
318         }
319
320         pool = wl_shm_create_pool(shm, fd, size);
321         buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
322                                            WL_SHM_FORMAT_ARGB8888);
323         wl_shm_pool_destroy(pool);
324
325         close(fd);
326
327         if (pixels)
328                 *pixels = data;
329
330         return buffer;
331 }
332
333 static void
334 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
335 {
336         struct client *client = data;
337
338         if (format == WL_SHM_FORMAT_ARGB8888)
339                 client->has_argb = 1;
340 }
341
342 struct wl_shm_listener shm_listener = {
343         shm_format
344 };
345
346 static void
347 test_handle_pointer_position(void *data, struct wl_test *wl_test,
348                              wl_fixed_t x, wl_fixed_t y)
349 {
350         struct test *test = data;
351         test->pointer_x = wl_fixed_to_int(x);
352         test->pointer_y = wl_fixed_to_int(y);
353
354         fprintf(stderr, "test-client: got global pointer %d %d\n",
355                 test->pointer_x, test->pointer_y);
356 }
357
358 static void
359 test_handle_n_egl_buffers(void *data, struct wl_test *wl_test, uint32_t n)
360 {
361         struct test *test = data;
362
363         test->n_egl_buffers = n;
364 }
365
366 static const struct wl_test_listener test_listener = {
367         test_handle_pointer_position,
368         test_handle_n_egl_buffers,
369 };
370
371 static void
372 seat_handle_capabilities(void *data, struct wl_seat *seat,
373                          enum wl_seat_capability caps)
374 {
375         struct input *input = data;
376         struct pointer *pointer;
377         struct keyboard *keyboard;
378
379         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
380                 pointer = xzalloc(sizeof *pointer);
381                 pointer->wl_pointer = wl_seat_get_pointer(seat);
382                 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
383                 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
384                                         pointer);
385                 input->pointer = pointer;
386         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
387                 wl_pointer_destroy(input->pointer->wl_pointer);
388                 free(input->pointer);
389                 input->pointer = NULL;
390         }
391
392         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
393                 keyboard = xzalloc(sizeof *keyboard);
394                 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
395                 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
396                 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
397                                          keyboard);
398                 input->keyboard = keyboard;
399         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
400                 wl_keyboard_destroy(input->keyboard->wl_keyboard);
401                 free(input->keyboard);
402                 input->keyboard = NULL;
403         }
404 }
405
406 static const struct wl_seat_listener seat_listener = {
407         seat_handle_capabilities,
408 };
409
410 static void
411 output_handle_geometry(void *data,
412                        struct wl_output *wl_output,
413                        int x, int y,
414                        int physical_width,
415                        int physical_height,
416                        int subpixel,
417                        const char *make,
418                        const char *model,
419                        int32_t transform)
420 {
421         struct output *output = data;
422
423         output->x = x;
424         output->y = y;
425 }
426
427 static void
428 output_handle_mode(void *data,
429                    struct wl_output *wl_output,
430                    uint32_t flags,
431                    int width,
432                    int height,
433                    int refresh)
434 {
435         struct output *output = data;
436
437         if (flags & WL_OUTPUT_MODE_CURRENT) {
438                 output->width = width;
439                 output->height = height;
440         }
441 }
442
443 static const struct wl_output_listener output_listener = {
444         output_handle_geometry,
445         output_handle_mode
446 };
447
448 static void
449 handle_global(void *data, struct wl_registry *registry,
450               uint32_t id, const char *interface, uint32_t version)
451 {
452         struct client *client = data;
453         struct input *input;
454         struct output *output;
455         struct test *test;
456         struct global *global;
457
458         global = xzalloc(sizeof *global);
459         global->name = id;
460         global->interface = strdup(interface);
461         assert(interface);
462         global->version = version;
463         wl_list_insert(client->global_list.prev, &global->link);
464
465         if (strcmp(interface, "wl_compositor") == 0) {
466                 client->wl_compositor =
467                         wl_registry_bind(registry, id,
468                                          &wl_compositor_interface, 1);
469         } else if (strcmp(interface, "wl_seat") == 0) {
470                 input = xzalloc(sizeof *input);
471                 input->wl_seat =
472                         wl_registry_bind(registry, id,
473                                          &wl_seat_interface, 1);
474                 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
475                 client->input = input;
476         } else if (strcmp(interface, "wl_shm") == 0) {
477                 client->wl_shm =
478                         wl_registry_bind(registry, id,
479                                          &wl_shm_interface, 1);
480                 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
481         } else if (strcmp(interface, "wl_output") == 0) {
482                 output = xzalloc(sizeof *output);
483                 output->wl_output =
484                         wl_registry_bind(registry, id,
485                                          &wl_output_interface, 1);
486                 wl_output_add_listener(output->wl_output,
487                                        &output_listener, output);
488                 client->output = output;
489         } else if (strcmp(interface, "wl_test") == 0) {
490                 test = xzalloc(sizeof *test);
491                 test->wl_test =
492                         wl_registry_bind(registry, id,
493                                          &wl_test_interface, 1);
494                 wl_test_add_listener(test->wl_test, &test_listener, test);
495                 client->test = test;
496         }
497 }
498
499 static const struct wl_registry_listener registry_listener = {
500         handle_global
501 };
502
503 void
504 skip(const char *fmt, ...)
505 {
506         va_list argp;
507
508         va_start(argp, fmt);
509         vfprintf(stderr, fmt, argp);
510         va_end(argp);
511
512         /* automake tests uses exit code 77. weston-test-runner will see
513          * this and use it, and then weston-test's sigchld handler (in the
514          * weston process) will use that as an exit status, which is what
515          * automake will see in the end. */
516         exit(77);
517 }
518
519 void
520 expect_protocol_error(struct client *client,
521                       const struct wl_interface *intf,
522                       uint32_t code)
523 {
524         int err;
525         uint32_t errcode, failed = 0;
526         const struct wl_interface *interface;
527         unsigned int id;
528
529         /* if the error has not come yet, make it happen */
530         wl_display_roundtrip(client->wl_display);
531
532         err = wl_display_get_error(client->wl_display);
533
534         assert(err && "Expected protocol error but nothing came");
535         assert(err == EPROTO && "Expected protocol error but got local error");
536
537         errcode = wl_display_get_protocol_error(client->wl_display,
538                                                 &interface, &id);
539
540         /* check error */
541         if (errcode != code) {
542                 fprintf(stderr, "Should get error code %d but got %d\n",
543                         code, errcode);
544                 failed = 1;
545         }
546
547         /* this should be definitely set */
548         assert(interface);
549
550         if (strcmp(intf->name, interface->name) != 0) {
551                 fprintf(stderr, "Should get interface '%s' but got '%s'\n",
552                         intf->name, interface->name);
553                 failed = 1;
554         }
555
556         if (failed) {
557                 fprintf(stderr, "Expected other protocol error\n");
558                 abort();
559         }
560
561         /* all OK */
562         fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) "
563                         "with code %d\n", interface->name, id, errcode);
564 }
565
566 static void
567 log_handler(const char *fmt, va_list args)
568 {
569         fprintf(stderr, "libwayland: ");
570         vfprintf(stderr, fmt, args);
571 }
572
573 struct client *
574 client_create(int x, int y, int width, int height)
575 {
576         struct client *client;
577         struct surface *surface;
578
579         wl_log_set_handler_client(log_handler);
580
581         /* connect to display */
582         client = xzalloc(sizeof *client);
583         client->wl_display = wl_display_connect(NULL);
584         assert(client->wl_display);
585         wl_list_init(&client->global_list);
586
587         /* setup registry so we can bind to interfaces */
588         client->wl_registry = wl_display_get_registry(client->wl_display);
589         wl_registry_add_listener(client->wl_registry, &registry_listener, client);
590
591         /* trigger global listener */
592         wl_display_dispatch(client->wl_display);
593         wl_display_roundtrip(client->wl_display);
594
595         /* must have WL_SHM_FORMAT_ARGB32 */
596         assert(client->has_argb);
597
598         /* must have wl_test interface */
599         assert(client->test);
600
601         /* must have an output */
602         assert(client->output);
603
604         /* initialize the client surface */
605         surface = xzalloc(sizeof *surface);
606         surface->wl_surface =
607                 wl_compositor_create_surface(client->wl_compositor);
608         assert(surface->wl_surface);
609
610         wl_surface_add_listener(surface->wl_surface, &surface_listener,
611                                 surface);
612
613         client->surface = surface;
614         wl_surface_set_user_data(surface->wl_surface, surface);
615
616         surface->width = width;
617         surface->height = height;
618         surface->wl_buffer = create_shm_buffer(client, width, height,
619                                                &surface->data);
620
621         memset(surface->data, 64, width * height * 4);
622
623         move_client(client, x, y);
624
625         return client;
626 }