malloc + memset -> zalloc
[platform/upstream/weston.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 <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28
29 #include "../shared/os-compatibility.h"
30 #include "weston-test-client-helper.h"
31
32 int
33 surface_contains(struct surface *surface, int x, int y)
34 {
35         /* test whether a global x,y point is contained in the surface */
36         int sx = surface->x;
37         int sy = surface->y;
38         int sw = surface->width;
39         int sh = surface->height;
40         return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
41 }
42
43 static void
44 frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
45 {
46         int *done = data;
47
48         *done = 1;
49
50         wl_callback_destroy(callback);
51 }
52
53 static const struct wl_callback_listener frame_listener = {
54         frame_callback_handler
55 };
56
57 struct wl_callback *
58 frame_callback_set(struct wl_surface *surface, int *done)
59 {
60         struct wl_callback *callback;
61
62         *done = 0;
63         callback = wl_surface_frame(surface);
64         wl_callback_add_listener(callback, &frame_listener, done);
65
66         return callback;
67 }
68
69 void
70 frame_callback_wait(struct client *client, int *done)
71 {
72         while (!*done) {
73                 assert(wl_display_dispatch(client->wl_display) >= 0);
74         }
75 }
76
77 void
78 move_client(struct client *client, int x, int y)
79 {
80         struct surface *surface = client->surface;
81         int done;
82
83         client->surface->x = x;
84         client->surface->y = y;
85         wl_test_move_surface(client->test->wl_test, surface->wl_surface,
86                              surface->x, surface->y);
87         /* The attach here is necessary because commit() will call congfigure
88          * only on surfaces newly attached, and the one that sets the surface
89          * position is the configure. */
90         wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
91         wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
92                           surface->height);
93
94         frame_callback_set(surface->wl_surface, &done);
95
96         wl_surface_commit(surface->wl_surface);
97
98         frame_callback_wait(client, &done);
99 }
100
101 static void
102 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
103                      uint32_t serial, struct wl_surface *wl_surface,
104                      wl_fixed_t x, wl_fixed_t y)
105 {
106         struct pointer *pointer = data;
107
108         pointer->focus = wl_surface_get_user_data(wl_surface);
109         pointer->x = wl_fixed_to_int(x);
110         pointer->y = wl_fixed_to_int(y);
111
112         fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
113                 pointer->x, pointer->y, pointer->focus);
114 }
115
116 static void
117 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
118                      uint32_t serial, struct wl_surface *wl_surface)
119 {
120         struct pointer *pointer = data;
121
122         pointer->focus = NULL;
123
124         fprintf(stderr, "test-client: got pointer leave, surface %p\n",
125                 wl_surface_get_user_data(wl_surface));
126 }
127
128 static void
129 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
130                       uint32_t time, wl_fixed_t x, wl_fixed_t y)
131 {
132         struct pointer *pointer = data;
133
134         pointer->x = wl_fixed_to_int(x);
135         pointer->y = wl_fixed_to_int(y);
136
137         fprintf(stderr, "test-client: got pointer motion %d %d\n",
138                 pointer->x, pointer->y);
139 }
140
141 static void
142 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
143                       uint32_t serial, uint32_t time, uint32_t button,
144                       uint32_t state)
145 {
146         struct pointer *pointer = data;
147
148         pointer->button = button;
149         pointer->state = state;
150
151         fprintf(stderr, "test-client: got pointer button %u %u\n",
152                 button, state);
153 }
154
155 static void
156 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
157                     uint32_t time, uint32_t axis, wl_fixed_t value)
158 {
159         fprintf(stderr, "test-client: got pointer axis %u %d\n", axis, value);
160 }
161
162 static const struct wl_pointer_listener pointer_listener = {
163         pointer_handle_enter,
164         pointer_handle_leave,
165         pointer_handle_motion,
166         pointer_handle_button,
167         pointer_handle_axis,
168 };
169
170 static void
171 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
172                        uint32_t format, int fd, uint32_t size)
173 {
174         close(fd);
175
176         fprintf(stderr, "test-client: got keyboard keymap\n");
177 }
178
179 static void
180 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
181                       uint32_t serial, struct wl_surface *wl_surface,
182                       struct wl_array *keys)
183 {
184         struct keyboard *keyboard = data;
185
186         keyboard->focus = wl_surface_get_user_data(wl_surface);
187
188         fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
189                 keyboard->focus);
190 }
191
192 static void
193 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
194                       uint32_t serial, struct wl_surface *wl_surface)
195 {
196         struct keyboard *keyboard = data;
197
198         keyboard->focus = NULL;
199
200         fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
201                 wl_surface_get_user_data(wl_surface));
202 }
203
204 static void
205 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
206                     uint32_t serial, uint32_t time, uint32_t key,
207                     uint32_t state)
208 {
209         struct keyboard *keyboard = data;
210
211         keyboard->key = key;
212         keyboard->state = state;
213
214         fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
215 }
216
217 static void
218 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
219                           uint32_t serial, uint32_t mods_depressed,
220                           uint32_t mods_latched, uint32_t mods_locked,
221                           uint32_t group)
222 {
223         struct keyboard *keyboard = data;
224
225         keyboard->mods_depressed = mods_depressed;
226         keyboard->mods_latched = mods_latched;
227         keyboard->mods_locked = mods_locked;
228         keyboard->group = group;
229
230         fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
231                 mods_depressed, mods_latched, mods_locked, group);
232 }
233
234 static const struct wl_keyboard_listener keyboard_listener = {
235         keyboard_handle_keymap,
236         keyboard_handle_enter,
237         keyboard_handle_leave,
238         keyboard_handle_key,
239         keyboard_handle_modifiers,
240 };
241
242 static void
243 surface_enter(void *data,
244               struct wl_surface *wl_surface, struct wl_output *output)
245 {
246         struct surface *surface = data;
247
248         surface->output = wl_output_get_user_data(output);
249
250         fprintf(stderr, "test-client: got surface enter output %p\n",
251                 surface->output);
252 }
253
254 static void
255 surface_leave(void *data,
256               struct wl_surface *wl_surface, struct wl_output *output)
257 {
258         struct surface *surface = data;
259
260         surface->output = NULL;
261
262         fprintf(stderr, "test-client: got surface leave output %p\n",
263                 wl_output_get_user_data(output));
264 }
265
266 static const struct wl_surface_listener surface_listener = {
267         surface_enter,
268         surface_leave
269 };
270
271 struct wl_buffer *
272 create_shm_buffer(struct client *client, int width, int height, void **pixels)
273 {
274         struct wl_shm *shm = client->wl_shm;
275         int stride = width * 4;
276         int size = stride * height;
277         struct wl_shm_pool *pool;
278         struct wl_buffer *buffer;
279         int fd;
280         void *data;
281
282         fd = os_create_anonymous_file(size);
283         assert(fd >= 0);
284
285         data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
286         if (data == MAP_FAILED) {
287                 close(fd);
288                 assert(data != MAP_FAILED);
289         }
290
291         pool = wl_shm_create_pool(shm, fd, size);
292         buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
293                                            WL_SHM_FORMAT_ARGB8888);
294         wl_shm_pool_destroy(pool);
295
296         close(fd);
297
298         if (pixels)
299                 *pixels = data;
300
301         return buffer;
302 }
303
304 static void
305 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
306 {
307         struct client *client = data;
308
309         if (format == WL_SHM_FORMAT_ARGB8888)
310                 client->has_argb = 1;
311 }
312
313 struct wl_shm_listener shm_listener = {
314         shm_format
315 };
316
317 static void
318 test_handle_pointer_position(void *data, struct wl_test *wl_test,
319                              wl_fixed_t x, wl_fixed_t y)
320 {
321         struct test *test = data;
322         test->pointer_x = wl_fixed_to_int(x);
323         test->pointer_y = wl_fixed_to_int(y);
324
325         fprintf(stderr, "test-client: got global pointer %d %d\n",
326                 test->pointer_x, test->pointer_y);
327 }
328
329 static const struct wl_test_listener test_listener = {
330         test_handle_pointer_position
331 };
332
333 static void
334 seat_handle_capabilities(void *data, struct wl_seat *seat,
335                          enum wl_seat_capability caps)
336 {
337         struct input *input = data;
338         struct pointer *pointer;
339         struct keyboard *keyboard;
340
341         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
342                 pointer = calloc(1, sizeof *pointer);
343                 pointer->wl_pointer = wl_seat_get_pointer(seat);
344                 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
345                 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
346                                         pointer);
347                 input->pointer = pointer;
348         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
349                 wl_pointer_destroy(input->pointer->wl_pointer);
350                 free(input->pointer);
351                 input->pointer = NULL;
352         }
353
354         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
355                 keyboard = calloc(1, sizeof *keyboard);
356                 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
357                 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
358                 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
359                                          keyboard);
360                 input->keyboard = keyboard;
361         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
362                 wl_keyboard_destroy(input->keyboard->wl_keyboard);
363                 free(input->keyboard);
364                 input->keyboard = NULL;
365         }
366 }
367
368 static const struct wl_seat_listener seat_listener = {
369         seat_handle_capabilities,
370 };
371
372 static void
373 output_handle_geometry(void *data,
374                        struct wl_output *wl_output,
375                        int x, int y,
376                        int physical_width,
377                        int physical_height,
378                        int subpixel,
379                        const char *make,
380                        const char *model,
381                        int32_t transform)
382 {
383         struct output *output = data;
384
385         output->x = x;
386         output->y = y;
387 }
388
389 static void
390 output_handle_mode(void *data,
391                    struct wl_output *wl_output,
392                    uint32_t flags,
393                    int width,
394                    int height,
395                    int refresh)
396 {
397         struct output *output = data;
398
399         if (flags & WL_OUTPUT_MODE_CURRENT) {
400                 output->width = width;
401                 output->height = height;
402         }
403 }
404
405 static const struct wl_output_listener output_listener = {
406         output_handle_geometry,
407         output_handle_mode
408 };
409
410 static void
411 handle_global(void *data, struct wl_registry *registry,
412               uint32_t id, const char *interface, uint32_t version)
413 {
414         struct client *client = data;
415         struct input *input;
416         struct output *output;
417         struct test *test;
418         struct global *global;
419
420         global = malloc(sizeof *global);
421         assert(global);
422         global->name = id;
423         global->interface = strdup(interface);
424         assert(interface);
425         global->version = version;
426         wl_list_insert(client->global_list.prev, &global->link);
427
428         if (strcmp(interface, "wl_compositor") == 0) {
429                 client->wl_compositor =
430                         wl_registry_bind(registry, id,
431                                          &wl_compositor_interface, 1);
432         } else if (strcmp(interface, "wl_seat") == 0) {
433                 input = calloc(1, sizeof *input);
434                 input->wl_seat =
435                         wl_registry_bind(registry, id,
436                                          &wl_seat_interface, 1);
437                 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
438                 client->input = input;
439         } else if (strcmp(interface, "wl_shm") == 0) {
440                 client->wl_shm =
441                         wl_registry_bind(registry, id,
442                                          &wl_shm_interface, 1);
443                 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
444         } else if (strcmp(interface, "wl_output") == 0) {
445                 output = malloc(sizeof *output);
446                 output->wl_output =
447                         wl_registry_bind(registry, id,
448                                          &wl_output_interface, 1);
449                 wl_output_add_listener(output->wl_output,
450                                        &output_listener, output);
451                 client->output = output;
452         } else if (strcmp(interface, "wl_test") == 0) {
453                 test = calloc(1, sizeof *test);
454                 test->wl_test =
455                         wl_registry_bind(registry, id,
456                                          &wl_test_interface, 1);
457                 wl_test_add_listener(test->wl_test, &test_listener, test);
458                 client->test = test;
459         }
460 }
461
462 static const struct wl_registry_listener registry_listener = {
463         handle_global
464 };
465
466 static void
467 log_handler(const char *fmt, va_list args)
468 {
469         fprintf(stderr, "libwayland: ");
470         vfprintf(stderr, fmt, args);
471 }
472
473 struct client *
474 client_create(int x, int y, int width, int height)
475 {
476         struct client *client;
477         struct surface *surface;
478
479         wl_log_set_handler_client(log_handler);
480
481         /* connect to display */
482         client = calloc(1, sizeof *client);
483         client->wl_display = wl_display_connect(NULL);
484         assert(client->wl_display);
485         wl_list_init(&client->global_list);
486
487         /* setup registry so we can bind to interfaces */
488         client->wl_registry = wl_display_get_registry(client->wl_display);
489         wl_registry_add_listener(client->wl_registry, &registry_listener, client);
490
491         /* trigger global listener */
492         wl_display_dispatch(client->wl_display);
493         wl_display_roundtrip(client->wl_display);
494
495         /* must have WL_SHM_FORMAT_ARGB32 */
496         assert(client->has_argb);
497
498         /* must have wl_test interface */
499         assert(client->test);
500
501         /* must have an output */
502         assert(client->output);
503
504         /* initialize the client surface */
505         surface = calloc(1, sizeof *surface);
506
507         surface->wl_surface =
508                 wl_compositor_create_surface(client->wl_compositor);
509         assert(surface->wl_surface);
510
511         wl_surface_add_listener(surface->wl_surface, &surface_listener,
512                                 surface);
513
514         client->surface = surface;
515         wl_surface_set_user_data(surface->wl_surface, surface);
516
517         surface->width = width;
518         surface->height = height;
519         surface->wl_buffer = create_shm_buffer(client, width, height,
520                                                &surface->data);
521
522         memset(surface->data, 64, width * height * 4);
523
524         move_client(client, x, y);
525
526         return client;
527 }