blob: 8d846ff36d57fb2e8acb444b8be4438a57f4f560 [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
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
32int
33surface_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
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050043static void
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020044frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050045{
46 int *done = data;
47
48 *done = 1;
49
50 wl_callback_destroy(callback);
51}
52
53static const struct wl_callback_listener frame_listener = {
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020054 frame_callback_handler
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050055};
56
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020057struct wl_callback *
58frame_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
69void
70frame_callback_wait(struct client *client, int *done)
71{
72 while (!*done) {
73 assert(wl_display_dispatch(client->wl_display) >= 0);
74 }
75}
76
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080077void
78move_client(struct client *client, int x, int y)
79{
80 struct surface *surface = client->surface;
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050081 int done;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080082
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);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050089
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020090 frame_callback_set(surface->wl_surface, &done);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050091
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080092 wl_surface_commit(surface->wl_surface);
93
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020094 frame_callback_wait(client, &done);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080095}
96
97static void
98pointer_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
112static void
113pointer_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
124static void
125pointer_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
137static void
138pointer_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
151static void
152pointer_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
158static 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
166static void
167keyboard_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
175static void
176keyboard_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
188static void
189keyboard_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
200static void
201keyboard_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
213static void
214keyboard_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
230static 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
238static void
239surface_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
250static void
251surface_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
262static const struct wl_surface_listener surface_listener = {
263 surface_enter,
264 surface_leave
265};
266
267static void
268create_shm_buffer(struct client *client)
269{
270 struct surface *surface = client->surface;
271 struct wl_shm *shm = client->wl_shm;
272 struct wl_shm_pool *pool;
273 int fd, size, stride;
274
275 stride = surface->width * 4;
276 size = stride * surface->height;
277
278 fd = os_create_anonymous_file(size);
279 assert(fd >= 0);
280
281 surface->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
282 fd, 0);
283 if (surface->data == MAP_FAILED) {
284 close(fd);
285 assert(surface->data != MAP_FAILED);
286 }
287
288 pool = wl_shm_create_pool(shm, fd, size);
289 surface->wl_buffer =
290 wl_shm_pool_create_buffer(pool, 0, surface->width,
291 surface->height, stride,
292 WL_SHM_FORMAT_ARGB8888);
293 wl_shm_pool_destroy(pool);
294
295 close(fd);
296}
297
298static void
299shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
300{
301 struct client *client = data;
302
303 if (format == WL_SHM_FORMAT_ARGB8888)
304 client->has_argb = 1;
305}
306
307struct wl_shm_listener shm_listener = {
308 shm_format
309};
310
311static void
312test_handle_pointer_position(void *data, struct wl_test *wl_test,
313 wl_fixed_t x, wl_fixed_t y)
314{
315 struct test *test = data;
316 test->pointer_x = wl_fixed_to_int(x);
317 test->pointer_y = wl_fixed_to_int(y);
318
319 fprintf(stderr, "test-client: got global pointer %d %d\n",
320 test->pointer_x, test->pointer_y);
321}
322
323static const struct wl_test_listener test_listener = {
324 test_handle_pointer_position
325};
326
327static void
328seat_handle_capabilities(void *data, struct wl_seat *seat,
329 enum wl_seat_capability caps)
330{
331 struct input *input = data;
332 struct pointer *pointer;
333 struct keyboard *keyboard;
334
335 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
336 pointer = calloc(1, sizeof *pointer);
337 pointer->wl_pointer = wl_seat_get_pointer(seat);
338 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
339 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
340 pointer);
341 input->pointer = pointer;
342 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
343 wl_pointer_destroy(input->pointer->wl_pointer);
344 free(input->pointer);
345 input->pointer = NULL;
346 }
347
348 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
349 keyboard = calloc(1, sizeof *keyboard);
350 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
351 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
352 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
353 keyboard);
354 input->keyboard = keyboard;
355 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
356 wl_keyboard_destroy(input->keyboard->wl_keyboard);
357 free(input->keyboard);
358 input->keyboard = NULL;
359 }
360}
361
362static const struct wl_seat_listener seat_listener = {
363 seat_handle_capabilities,
364};
365
366static void
367output_handle_geometry(void *data,
368 struct wl_output *wl_output,
369 int x, int y,
370 int physical_width,
371 int physical_height,
372 int subpixel,
373 const char *make,
374 const char *model,
375 int32_t transform)
376{
377 struct output *output = data;
378
379 output->x = x;
380 output->y = y;
381}
382
383static void
384output_handle_mode(void *data,
385 struct wl_output *wl_output,
386 uint32_t flags,
387 int width,
388 int height,
389 int refresh)
390{
391 struct output *output = data;
392
393 if (flags & WL_OUTPUT_MODE_CURRENT) {
394 output->width = width;
395 output->height = height;
396 }
397}
398
399static const struct wl_output_listener output_listener = {
400 output_handle_geometry,
401 output_handle_mode
402};
403
404static void
405handle_global(void *data, struct wl_registry *registry,
406 uint32_t id, const char *interface, uint32_t version)
407{
408 struct client *client = data;
409 struct input *input;
410 struct output *output;
411 struct test *test;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500412 struct global *global;
413
414 global = malloc(sizeof *global);
415 assert(global);
416 global->name = id;
417 global->interface = strdup(interface);
418 assert(interface);
419 global->version = version;
420 wl_list_insert(client->global_list.prev, &global->link);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800421
422 if (strcmp(interface, "wl_compositor") == 0) {
423 client->wl_compositor =
424 wl_registry_bind(registry, id,
425 &wl_compositor_interface, 1);
426 } else if (strcmp(interface, "wl_seat") == 0) {
427 input = calloc(1, sizeof *input);
428 input->wl_seat =
429 wl_registry_bind(registry, id,
430 &wl_seat_interface, 1);
431 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
432 client->input = input;
433 } else if (strcmp(interface, "wl_shm") == 0) {
434 client->wl_shm =
435 wl_registry_bind(registry, id,
436 &wl_shm_interface, 1);
437 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
438 } else if (strcmp(interface, "wl_output") == 0) {
439 output = malloc(sizeof *output);
440 output->wl_output =
441 wl_registry_bind(registry, id,
442 &wl_output_interface, 1);
443 wl_output_add_listener(output->wl_output,
444 &output_listener, output);
445 client->output = output;
446 } else if (strcmp(interface, "wl_test") == 0) {
447 test = calloc(1, sizeof *test);
448 test->wl_test =
449 wl_registry_bind(registry, id,
450 &wl_test_interface, 1);
451 wl_test_add_listener(test->wl_test, &test_listener, test);
452 client->test = test;
453 }
454}
455
456static const struct wl_registry_listener registry_listener = {
457 handle_global
458};
459
Pekka Paalanen07921d72012-12-12 14:26:40 +0200460static void
461log_handler(const char *fmt, va_list args)
462{
463 fprintf(stderr, "libwayland: ");
464 vfprintf(stderr, fmt, args);
465}
466
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800467struct client *
468client_create(int x, int y, int width, int height)
469{
470 struct client *client;
471 struct surface *surface;
472
Pekka Paalanen07921d72012-12-12 14:26:40 +0200473 wl_log_set_handler_client(log_handler);
474
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800475 /* connect to display */
476 client = calloc(1, sizeof *client);
477 client->wl_display = wl_display_connect(NULL);
478 assert(client->wl_display);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500479 wl_list_init(&client->global_list);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800480
481 /* setup registry so we can bind to interfaces */
482 client->wl_registry = wl_display_get_registry(client->wl_display);
483 wl_registry_add_listener(client->wl_registry, &registry_listener, client);
484
485 /* trigger global listener */
486 wl_display_dispatch(client->wl_display);
487 wl_display_roundtrip(client->wl_display);
488
489 /* must have WL_SHM_FORMAT_ARGB32 */
490 assert(client->has_argb);
491
492 /* must have wl_test interface */
493 assert(client->test);
494
495 /* must have an output */
496 assert(client->output);
497
498 /* initialize the client surface */
499 surface = calloc(1, sizeof *surface);
500
501 surface->wl_surface =
502 wl_compositor_create_surface(client->wl_compositor);
503 assert(surface->wl_surface);
504
505 wl_surface_add_listener(surface->wl_surface, &surface_listener,
506 surface);
507
508 client->surface = surface;
509 wl_surface_set_user_data(surface->wl_surface, surface);
510
511 surface->width = width;
512 surface->height = height;
513 create_shm_buffer(client);
514
515 memset(surface->data, 64, width * height * 4);
516 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
517
518 move_client(client, x, y);
519
520 return client;
521}