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