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