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