blob: 79dd5dadaa22a96b6837c1c606cd77148b1a001b [file] [log] [blame]
U. Artie Eoff1ba9b382012-12-07 13:50:32 -08001/*
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
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070023#include <config.h>
24
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080025#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
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -070034static inline void *
35xzalloc(size_t size)
36{
37 void *p;
38
39 p = calloc(1, size);
40 assert(p);
41
42 return p;
43}
44
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080045int
46surface_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
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050056static void
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020057frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050058{
59 int *done = data;
60
61 *done = 1;
62
63 wl_callback_destroy(callback);
64}
65
66static const struct wl_callback_listener frame_listener = {
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020067 frame_callback_handler
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050068};
69
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020070struct wl_callback *
71frame_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
82void
83frame_callback_wait(struct client *client, int *done)
84{
85 while (!*done) {
86 assert(wl_display_dispatch(client->wl_display) >= 0);
87 }
88}
89
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080090void
91move_client(struct client *client, int x, int y)
92{
93 struct surface *surface = client->surface;
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050094 int done;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080095
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);
Giulio Camuffo82cb5052013-02-28 18:44:54 +0100100 /* 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);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800104 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
105 surface->height);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500106
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200107 frame_callback_set(surface->wl_surface, &done);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500108
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800109 wl_surface_commit(surface->wl_surface);
110
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200111 frame_callback_wait(client, &done);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800112}
113
114static void
115pointer_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
129static void
130pointer_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
141static void
142pointer_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
154static void
155pointer_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
168static void
169pointer_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 %d\n", axis, value);
173}
174
175static const struct wl_pointer_listener pointer_listener = {
176 pointer_handle_enter,
177 pointer_handle_leave,
178 pointer_handle_motion,
179 pointer_handle_button,
180 pointer_handle_axis,
181};
182
183static void
184keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
185 uint32_t format, int fd, uint32_t size)
186{
187 close(fd);
188
189 fprintf(stderr, "test-client: got keyboard keymap\n");
190}
191
192static void
193keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
194 uint32_t serial, struct wl_surface *wl_surface,
195 struct wl_array *keys)
196{
197 struct keyboard *keyboard = data;
198
199 keyboard->focus = wl_surface_get_user_data(wl_surface);
200
201 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
202 keyboard->focus);
203}
204
205static void
206keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
207 uint32_t serial, struct wl_surface *wl_surface)
208{
209 struct keyboard *keyboard = data;
210
211 keyboard->focus = NULL;
212
213 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
214 wl_surface_get_user_data(wl_surface));
215}
216
217static void
218keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
219 uint32_t serial, uint32_t time, uint32_t key,
220 uint32_t state)
221{
222 struct keyboard *keyboard = data;
223
224 keyboard->key = key;
225 keyboard->state = state;
226
227 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
228}
229
230static void
231keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
232 uint32_t serial, uint32_t mods_depressed,
233 uint32_t mods_latched, uint32_t mods_locked,
234 uint32_t group)
235{
236 struct keyboard *keyboard = data;
237
238 keyboard->mods_depressed = mods_depressed;
239 keyboard->mods_latched = mods_latched;
240 keyboard->mods_locked = mods_locked;
241 keyboard->group = group;
242
243 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
244 mods_depressed, mods_latched, mods_locked, group);
245}
246
247static const struct wl_keyboard_listener keyboard_listener = {
248 keyboard_handle_keymap,
249 keyboard_handle_enter,
250 keyboard_handle_leave,
251 keyboard_handle_key,
252 keyboard_handle_modifiers,
253};
254
255static void
256surface_enter(void *data,
257 struct wl_surface *wl_surface, struct wl_output *output)
258{
259 struct surface *surface = data;
260
261 surface->output = wl_output_get_user_data(output);
262
263 fprintf(stderr, "test-client: got surface enter output %p\n",
264 surface->output);
265}
266
267static void
268surface_leave(void *data,
269 struct wl_surface *wl_surface, struct wl_output *output)
270{
271 struct surface *surface = data;
272
273 surface->output = NULL;
274
275 fprintf(stderr, "test-client: got surface leave output %p\n",
276 wl_output_get_user_data(output));
277}
278
279static const struct wl_surface_listener surface_listener = {
280 surface_enter,
281 surface_leave
282};
283
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200284struct wl_buffer *
285create_shm_buffer(struct client *client, int width, int height, void **pixels)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800286{
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800287 struct wl_shm *shm = client->wl_shm;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200288 int stride = width * 4;
289 int size = stride * height;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800290 struct wl_shm_pool *pool;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200291 struct wl_buffer *buffer;
292 int fd;
293 void *data;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800294
295 fd = os_create_anonymous_file(size);
296 assert(fd >= 0);
297
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200298 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
299 if (data == MAP_FAILED) {
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800300 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200301 assert(data != MAP_FAILED);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800302 }
303
304 pool = wl_shm_create_pool(shm, fd, size);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200305 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
306 WL_SHM_FORMAT_ARGB8888);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800307 wl_shm_pool_destroy(pool);
308
309 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200310
311 if (pixels)
312 *pixels = data;
313
314 return buffer;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800315}
316
317static void
318shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
319{
320 struct client *client = data;
321
322 if (format == WL_SHM_FORMAT_ARGB8888)
323 client->has_argb = 1;
324}
325
326struct wl_shm_listener shm_listener = {
327 shm_format
328};
329
330static void
331test_handle_pointer_position(void *data, struct wl_test *wl_test,
332 wl_fixed_t x, wl_fixed_t y)
333{
334 struct test *test = data;
335 test->pointer_x = wl_fixed_to_int(x);
336 test->pointer_y = wl_fixed_to_int(y);
337
338 fprintf(stderr, "test-client: got global pointer %d %d\n",
339 test->pointer_x, test->pointer_y);
340}
341
342static const struct wl_test_listener test_listener = {
343 test_handle_pointer_position
344};
345
346static void
347seat_handle_capabilities(void *data, struct wl_seat *seat,
348 enum wl_seat_capability caps)
349{
350 struct input *input = data;
351 struct pointer *pointer;
352 struct keyboard *keyboard;
353
354 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700355 pointer = xzalloc(sizeof *pointer);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800356 pointer->wl_pointer = wl_seat_get_pointer(seat);
357 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
358 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
359 pointer);
360 input->pointer = pointer;
361 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
362 wl_pointer_destroy(input->pointer->wl_pointer);
363 free(input->pointer);
364 input->pointer = NULL;
365 }
366
367 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700368 keyboard = xzalloc(sizeof *keyboard);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800369 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
370 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
371 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
372 keyboard);
373 input->keyboard = keyboard;
374 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
375 wl_keyboard_destroy(input->keyboard->wl_keyboard);
376 free(input->keyboard);
377 input->keyboard = NULL;
378 }
379}
380
381static const struct wl_seat_listener seat_listener = {
382 seat_handle_capabilities,
383};
384
385static void
386output_handle_geometry(void *data,
387 struct wl_output *wl_output,
388 int x, int y,
389 int physical_width,
390 int physical_height,
391 int subpixel,
392 const char *make,
393 const char *model,
394 int32_t transform)
395{
396 struct output *output = data;
397
398 output->x = x;
399 output->y = y;
400}
401
402static void
403output_handle_mode(void *data,
404 struct wl_output *wl_output,
405 uint32_t flags,
406 int width,
407 int height,
408 int refresh)
409{
410 struct output *output = data;
411
412 if (flags & WL_OUTPUT_MODE_CURRENT) {
413 output->width = width;
414 output->height = height;
415 }
416}
417
418static const struct wl_output_listener output_listener = {
419 output_handle_geometry,
420 output_handle_mode
421};
422
423static void
424handle_global(void *data, struct wl_registry *registry,
425 uint32_t id, const char *interface, uint32_t version)
426{
427 struct client *client = data;
428 struct input *input;
429 struct output *output;
430 struct test *test;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500431 struct global *global;
432
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700433 global = xzalloc(sizeof *global);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500434 global->name = id;
435 global->interface = strdup(interface);
436 assert(interface);
437 global->version = version;
438 wl_list_insert(client->global_list.prev, &global->link);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800439
440 if (strcmp(interface, "wl_compositor") == 0) {
441 client->wl_compositor =
442 wl_registry_bind(registry, id,
443 &wl_compositor_interface, 1);
444 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700445 input = xzalloc(sizeof *input);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800446 input->wl_seat =
447 wl_registry_bind(registry, id,
448 &wl_seat_interface, 1);
449 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
450 client->input = input;
451 } else if (strcmp(interface, "wl_shm") == 0) {
452 client->wl_shm =
453 wl_registry_bind(registry, id,
454 &wl_shm_interface, 1);
455 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
456 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700457 output = xzalloc(sizeof *output);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800458 output->wl_output =
459 wl_registry_bind(registry, id,
460 &wl_output_interface, 1);
461 wl_output_add_listener(output->wl_output,
462 &output_listener, output);
463 client->output = output;
464 } else if (strcmp(interface, "wl_test") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700465 test = xzalloc(sizeof *test);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800466 test->wl_test =
467 wl_registry_bind(registry, id,
468 &wl_test_interface, 1);
469 wl_test_add_listener(test->wl_test, &test_listener, test);
470 client->test = test;
471 }
472}
473
474static const struct wl_registry_listener registry_listener = {
475 handle_global
476};
477
Pekka Paalanen07921d72012-12-12 14:26:40 +0200478static void
479log_handler(const char *fmt, va_list args)
480{
481 fprintf(stderr, "libwayland: ");
482 vfprintf(stderr, fmt, args);
483}
484
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800485struct client *
486client_create(int x, int y, int width, int height)
487{
488 struct client *client;
489 struct surface *surface;
490
Pekka Paalanen07921d72012-12-12 14:26:40 +0200491 wl_log_set_handler_client(log_handler);
492
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800493 /* connect to display */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700494 client = xzalloc(sizeof *client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800495 client->wl_display = wl_display_connect(NULL);
496 assert(client->wl_display);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500497 wl_list_init(&client->global_list);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800498
499 /* setup registry so we can bind to interfaces */
500 client->wl_registry = wl_display_get_registry(client->wl_display);
501 wl_registry_add_listener(client->wl_registry, &registry_listener, client);
502
503 /* trigger global listener */
504 wl_display_dispatch(client->wl_display);
505 wl_display_roundtrip(client->wl_display);
506
507 /* must have WL_SHM_FORMAT_ARGB32 */
508 assert(client->has_argb);
509
510 /* must have wl_test interface */
511 assert(client->test);
512
513 /* must have an output */
514 assert(client->output);
515
516 /* initialize the client surface */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700517 surface = xzalloc(sizeof *surface);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800518 surface->wl_surface =
519 wl_compositor_create_surface(client->wl_compositor);
520 assert(surface->wl_surface);
521
522 wl_surface_add_listener(surface->wl_surface, &surface_listener,
523 surface);
524
525 client->surface = surface;
526 wl_surface_set_user_data(surface->wl_surface, surface);
527
528 surface->width = width;
529 surface->height = height;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200530 surface->wl_buffer = create_shm_buffer(client, width, height,
531 &surface->data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800532
533 memset(surface->data, 64, width * height * 4);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800534
535 move_client(client, x, y);
536
537 return client;
538}