blob: 01405d83ee8bb5aa56a4f3d81feafc19bf2870c2 [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
43void
44yield(struct client *client)
45{
46 /*
47 * FIXME: ugh! how do we ensure all events have finished
48 * propagating to the client. The calls to usleep seem to do a
49 * pretty reasonable job... and without them, tests can fail
50 * intermittently.
51 */
52 usleep(0.02 * 1e6);
53 wl_display_flush(client->wl_display);
54 wl_display_roundtrip(client->wl_display);
55 usleep(0.02 * 1e6);
56}
57
58void
59move_pointer(struct client *client, int x, int y)
60{
61 wl_test_move_pointer(client->test->wl_test, x, y);
62
63 yield(client);
64}
65
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050066static void
67move_client_frame_handler(void *data,
68 struct wl_callback *callback, uint32_t time)
69{
70 int *done = data;
71
72 *done = 1;
73
74 wl_callback_destroy(callback);
75}
76
77static const struct wl_callback_listener frame_listener = {
78 move_client_frame_handler
79};
80
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080081void
82move_client(struct client *client, int x, int y)
83{
84 struct surface *surface = client->surface;
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050085 struct wl_callback *callback;
86 int done;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080087
88 client->surface->x = x;
89 client->surface->y = y;
90 wl_test_move_surface(client->test->wl_test, surface->wl_surface,
91 surface->x, surface->y);
92 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
93 surface->height);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050094
95 callback = wl_surface_frame(surface->wl_surface);
96 done = 0;
97 wl_callback_add_listener(callback, &frame_listener, &done);
98
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080099 wl_surface_commit(surface->wl_surface);
100
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500101 while (!done)
102 wl_display_dispatch(client->wl_display);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800103}
104
105static void
106pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
107 uint32_t serial, struct wl_surface *wl_surface,
108 wl_fixed_t x, wl_fixed_t y)
109{
110 struct pointer *pointer = data;
111
112 pointer->focus = wl_surface_get_user_data(wl_surface);
113 pointer->x = wl_fixed_to_int(x);
114 pointer->y = wl_fixed_to_int(y);
115
116 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
117 pointer->x, pointer->y, pointer->focus);
118}
119
120static void
121pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
122 uint32_t serial, struct wl_surface *wl_surface)
123{
124 struct pointer *pointer = data;
125
126 pointer->focus = NULL;
127
128 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
129 wl_surface_get_user_data(wl_surface));
130}
131
132static void
133pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
134 uint32_t time, wl_fixed_t x, wl_fixed_t y)
135{
136 struct pointer *pointer = data;
137
138 pointer->x = wl_fixed_to_int(x);
139 pointer->y = wl_fixed_to_int(y);
140
141 fprintf(stderr, "test-client: got pointer motion %d %d\n",
142 pointer->x, pointer->y);
143}
144
145static void
146pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
147 uint32_t serial, uint32_t time, uint32_t button,
148 uint32_t state)
149{
150 struct pointer *pointer = data;
151
152 pointer->button = button;
153 pointer->state = state;
154
155 fprintf(stderr, "test-client: got pointer button %u %u\n",
156 button, state);
157}
158
159static void
160pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
161 uint32_t time, uint32_t axis, wl_fixed_t value)
162{
163 fprintf(stderr, "test-client: got pointer axis %u %d\n", axis, value);
164}
165
166static const struct wl_pointer_listener pointer_listener = {
167 pointer_handle_enter,
168 pointer_handle_leave,
169 pointer_handle_motion,
170 pointer_handle_button,
171 pointer_handle_axis,
172};
173
174static void
175keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
176 uint32_t format, int fd, uint32_t size)
177{
178 close(fd);
179
180 fprintf(stderr, "test-client: got keyboard keymap\n");
181}
182
183static void
184keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
185 uint32_t serial, struct wl_surface *wl_surface,
186 struct wl_array *keys)
187{
188 struct keyboard *keyboard = data;
189
190 keyboard->focus = wl_surface_get_user_data(wl_surface);
191
192 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
193 keyboard->focus);
194}
195
196static void
197keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
198 uint32_t serial, struct wl_surface *wl_surface)
199{
200 struct keyboard *keyboard = data;
201
202 keyboard->focus = NULL;
203
204 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
205 wl_surface_get_user_data(wl_surface));
206}
207
208static void
209keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
210 uint32_t serial, uint32_t time, uint32_t key,
211 uint32_t state)
212{
213 struct keyboard *keyboard = data;
214
215 keyboard->key = key;
216 keyboard->state = state;
217
218 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
219}
220
221static void
222keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
223 uint32_t serial, uint32_t mods_depressed,
224 uint32_t mods_latched, uint32_t mods_locked,
225 uint32_t group)
226{
227 struct keyboard *keyboard = data;
228
229 keyboard->mods_depressed = mods_depressed;
230 keyboard->mods_latched = mods_latched;
231 keyboard->mods_locked = mods_locked;
232 keyboard->group = group;
233
234 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
235 mods_depressed, mods_latched, mods_locked, group);
236}
237
238static const struct wl_keyboard_listener keyboard_listener = {
239 keyboard_handle_keymap,
240 keyboard_handle_enter,
241 keyboard_handle_leave,
242 keyboard_handle_key,
243 keyboard_handle_modifiers,
244};
245
246static void
247surface_enter(void *data,
248 struct wl_surface *wl_surface, struct wl_output *output)
249{
250 struct surface *surface = data;
251
252 surface->output = wl_output_get_user_data(output);
253
254 fprintf(stderr, "test-client: got surface enter output %p\n",
255 surface->output);
256}
257
258static void
259surface_leave(void *data,
260 struct wl_surface *wl_surface, struct wl_output *output)
261{
262 struct surface *surface = data;
263
264 surface->output = NULL;
265
266 fprintf(stderr, "test-client: got surface leave output %p\n",
267 wl_output_get_user_data(output));
268}
269
270static const struct wl_surface_listener surface_listener = {
271 surface_enter,
272 surface_leave
273};
274
275static void
276create_shm_buffer(struct client *client)
277{
278 struct surface *surface = client->surface;
279 struct wl_shm *shm = client->wl_shm;
280 struct wl_shm_pool *pool;
281 int fd, size, stride;
282
283 stride = surface->width * 4;
284 size = stride * surface->height;
285
286 fd = os_create_anonymous_file(size);
287 assert(fd >= 0);
288
289 surface->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
290 fd, 0);
291 if (surface->data == MAP_FAILED) {
292 close(fd);
293 assert(surface->data != MAP_FAILED);
294 }
295
296 pool = wl_shm_create_pool(shm, fd, size);
297 surface->wl_buffer =
298 wl_shm_pool_create_buffer(pool, 0, surface->width,
299 surface->height, stride,
300 WL_SHM_FORMAT_ARGB8888);
301 wl_shm_pool_destroy(pool);
302
303 close(fd);
304}
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;
420
421 if (strcmp(interface, "wl_compositor") == 0) {
422 client->wl_compositor =
423 wl_registry_bind(registry, id,
424 &wl_compositor_interface, 1);
425 } else if (strcmp(interface, "wl_seat") == 0) {
426 input = calloc(1, sizeof *input);
427 input->wl_seat =
428 wl_registry_bind(registry, id,
429 &wl_seat_interface, 1);
430 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
431 client->input = input;
432 } else if (strcmp(interface, "wl_shm") == 0) {
433 client->wl_shm =
434 wl_registry_bind(registry, id,
435 &wl_shm_interface, 1);
436 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
437 } else if (strcmp(interface, "wl_output") == 0) {
438 output = malloc(sizeof *output);
439 output->wl_output =
440 wl_registry_bind(registry, id,
441 &wl_output_interface, 1);
442 wl_output_add_listener(output->wl_output,
443 &output_listener, output);
444 client->output = output;
445 } else if (strcmp(interface, "wl_test") == 0) {
446 test = calloc(1, sizeof *test);
447 test->wl_test =
448 wl_registry_bind(registry, id,
449 &wl_test_interface, 1);
450 wl_test_add_listener(test->wl_test, &test_listener, test);
451 client->test = test;
452 }
453}
454
455static const struct wl_registry_listener registry_listener = {
456 handle_global
457};
458
459struct client *
460client_create(int x, int y, int width, int height)
461{
462 struct client *client;
463 struct surface *surface;
464
465 /* connect to display */
466 client = calloc(1, sizeof *client);
467 client->wl_display = wl_display_connect(NULL);
468 assert(client->wl_display);
469
470 /* setup registry so we can bind to interfaces */
471 client->wl_registry = wl_display_get_registry(client->wl_display);
472 wl_registry_add_listener(client->wl_registry, &registry_listener, client);
473
474 /* trigger global listener */
475 wl_display_dispatch(client->wl_display);
476 wl_display_roundtrip(client->wl_display);
477
478 /* must have WL_SHM_FORMAT_ARGB32 */
479 assert(client->has_argb);
480
481 /* must have wl_test interface */
482 assert(client->test);
483
484 /* must have an output */
485 assert(client->output);
486
487 /* initialize the client surface */
488 surface = calloc(1, sizeof *surface);
489
490 surface->wl_surface =
491 wl_compositor_create_surface(client->wl_compositor);
492 assert(surface->wl_surface);
493
494 wl_surface_add_listener(surface->wl_surface, &surface_listener,
495 surface);
496
497 client->surface = surface;
498 wl_surface_set_user_data(surface->wl_surface, surface);
499
500 surface->width = width;
501 surface->height = height;
502 create_shm_buffer(client);
503
504 memset(surface->data, 64, width * height * 4);
505 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
506
507 move_client(client, x, y);
508
509 return client;
510}