blob: b19be4000124b8363601a90e65b37c8b3b719af0 [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{
Kristian Høgsberg4c8ce202013-10-09 14:23:00 -0700172 fprintf(stderr, "test-client: got pointer axis %u %f\n",
173 axis, wl_fixed_to_double(value));
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800174}
175
176static 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
184static void
185keyboard_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
193static void
194keyboard_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
206static void
207keyboard_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
218static void
219keyboard_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
231static void
232keyboard_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
248static 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
256static void
257surface_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
268static void
269surface_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
280static const struct wl_surface_listener surface_listener = {
281 surface_enter,
282 surface_leave
283};
284
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200285struct wl_buffer *
286create_shm_buffer(struct client *client, int width, int height, void **pixels)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800287{
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800288 struct wl_shm *shm = client->wl_shm;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200289 int stride = width * 4;
290 int size = stride * height;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800291 struct wl_shm_pool *pool;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200292 struct wl_buffer *buffer;
293 int fd;
294 void *data;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800295
296 fd = os_create_anonymous_file(size);
297 assert(fd >= 0);
298
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200299 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
300 if (data == MAP_FAILED) {
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800301 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200302 assert(data != MAP_FAILED);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800303 }
304
305 pool = wl_shm_create_pool(shm, fd, size);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200306 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
307 WL_SHM_FORMAT_ARGB8888);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800308 wl_shm_pool_destroy(pool);
309
310 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200311
312 if (pixels)
313 *pixels = data;
314
315 return buffer;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800316}
317
318static void
319shm_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
327struct wl_shm_listener shm_listener = {
328 shm_format
329};
330
331static void
332test_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
343static const struct wl_test_listener test_listener = {
344 test_handle_pointer_position
345};
346
347static void
348seat_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) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700356 pointer = xzalloc(sizeof *pointer);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800357 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) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700369 keyboard = xzalloc(sizeof *keyboard);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800370 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
382static const struct wl_seat_listener seat_listener = {
383 seat_handle_capabilities,
384};
385
386static void
387output_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
403static void
404output_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
419static const struct wl_output_listener output_listener = {
420 output_handle_geometry,
421 output_handle_mode
422};
423
424static void
425handle_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;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500432 struct global *global;
433
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700434 global = xzalloc(sizeof *global);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500435 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);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800440
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) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700446 input = xzalloc(sizeof *input);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800447 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) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700458 output = xzalloc(sizeof *output);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800459 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) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700466 test = xzalloc(sizeof *test);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800467 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
475static const struct wl_registry_listener registry_listener = {
476 handle_global
477};
478
Pekka Paalanen07921d72012-12-12 14:26:40 +0200479static void
480log_handler(const char *fmt, va_list args)
481{
482 fprintf(stderr, "libwayland: ");
483 vfprintf(stderr, fmt, args);
484}
485
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800486struct client *
487client_create(int x, int y, int width, int height)
488{
489 struct client *client;
490 struct surface *surface;
491
Pekka Paalanen07921d72012-12-12 14:26:40 +0200492 wl_log_set_handler_client(log_handler);
493
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800494 /* connect to display */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700495 client = xzalloc(sizeof *client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800496 client->wl_display = wl_display_connect(NULL);
497 assert(client->wl_display);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500498 wl_list_init(&client->global_list);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800499
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 */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700518 surface = xzalloc(sizeof *surface);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800519 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;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200531 surface->wl_buffer = create_shm_buffer(client, width, height,
532 &surface->data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800533
534 memset(surface->data, 64, width * height * 4);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800535
536 move_client(client, x, y);
537
538 return client;
539}