blob: 84eae7e670fb9653543876eba280e8fc7e05280a [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
Bryce Harrington12cc4052014-11-19 17:18:33 -080023#include "config.h"
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070024
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080025#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
Marek Chalupa4d06d462014-07-16 11:27:06 +020029#include <errno.h>
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080030#include <sys/mman.h>
31
32#include "../shared/os-compatibility.h"
33#include "weston-test-client-helper.h"
34
35int
36surface_contains(struct surface *surface, int x, int y)
37{
38 /* test whether a global x,y point is contained in the surface */
39 int sx = surface->x;
40 int sy = surface->y;
41 int sw = surface->width;
42 int sh = surface->height;
43 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
44}
45
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050046static void
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020047frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050048{
49 int *done = data;
50
51 *done = 1;
52
53 wl_callback_destroy(callback);
54}
55
56static const struct wl_callback_listener frame_listener = {
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020057 frame_callback_handler
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050058};
59
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020060struct wl_callback *
61frame_callback_set(struct wl_surface *surface, int *done)
62{
63 struct wl_callback *callback;
64
65 *done = 0;
66 callback = wl_surface_frame(surface);
67 wl_callback_add_listener(callback, &frame_listener, done);
68
69 return callback;
70}
71
Marek Chalupa1740aa82014-07-16 11:32:50 +020072int
73frame_callback_wait_nofail(struct client *client, int *done)
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020074{
75 while (!*done) {
Marek Chalupa1740aa82014-07-16 11:32:50 +020076 if (wl_display_dispatch(client->wl_display) < 0)
77 return 0;
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020078 }
Marek Chalupa1740aa82014-07-16 11:32:50 +020079
80 return 1;
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +020081}
82
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080083void
84move_client(struct client *client, int x, int y)
85{
86 struct surface *surface = client->surface;
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050087 int done;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080088
89 client->surface->x = x;
90 client->surface->y = y;
Derek Foremanf6a65922015-02-24 09:32:14 -060091 weston_test_move_surface(client->test->weston_test, surface->wl_surface,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080092 surface->x, surface->y);
Bryce Harringtoncb50ed22014-12-10 18:33:47 -080093 /* The attach here is necessary because commit() will call configure
Giulio Camuffo82cb5052013-02-28 18:44:54 +010094 * only on surfaces newly attached, and the one that sets the surface
95 * position is the configure. */
96 wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080097 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
98 surface->height);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -050099
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200100 frame_callback_set(surface->wl_surface, &done);
Kristian Høgsbergdb6dc7d2012-12-11 21:49:13 -0500101
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800102 wl_surface_commit(surface->wl_surface);
103
Pekka Paalanen8aaef7d2013-02-08 17:01:25 +0200104 frame_callback_wait(client, &done);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800105}
106
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000107int
108get_n_egl_buffers(struct client *client)
109{
110 client->test->n_egl_buffers = -1;
111
Derek Foremanf6a65922015-02-24 09:32:14 -0600112 weston_test_get_n_egl_buffers(client->test->weston_test);
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000113 wl_display_roundtrip(client->wl_display);
114
115 return client->test->n_egl_buffers;
116}
117
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800118static void
119pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
120 uint32_t serial, struct wl_surface *wl_surface,
121 wl_fixed_t x, wl_fixed_t y)
122{
123 struct pointer *pointer = data;
124
125 pointer->focus = wl_surface_get_user_data(wl_surface);
126 pointer->x = wl_fixed_to_int(x);
127 pointer->y = wl_fixed_to_int(y);
128
129 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
130 pointer->x, pointer->y, pointer->focus);
131}
132
133static void
134pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
135 uint32_t serial, struct wl_surface *wl_surface)
136{
137 struct pointer *pointer = data;
138
139 pointer->focus = NULL;
140
141 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
142 wl_surface_get_user_data(wl_surface));
143}
144
145static void
146pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
147 uint32_t time, wl_fixed_t x, wl_fixed_t y)
148{
149 struct pointer *pointer = data;
150
151 pointer->x = wl_fixed_to_int(x);
152 pointer->y = wl_fixed_to_int(y);
153
154 fprintf(stderr, "test-client: got pointer motion %d %d\n",
155 pointer->x, pointer->y);
156}
157
158static void
159pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
160 uint32_t serial, uint32_t time, uint32_t button,
161 uint32_t state)
162{
163 struct pointer *pointer = data;
164
165 pointer->button = button;
166 pointer->state = state;
167
168 fprintf(stderr, "test-client: got pointer button %u %u\n",
169 button, state);
170}
171
172static void
173pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
174 uint32_t time, uint32_t axis, wl_fixed_t value)
175{
Kristian Høgsberg4c8ce202013-10-09 14:23:00 -0700176 fprintf(stderr, "test-client: got pointer axis %u %f\n",
177 axis, wl_fixed_to_double(value));
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800178}
179
180static const struct wl_pointer_listener pointer_listener = {
181 pointer_handle_enter,
182 pointer_handle_leave,
183 pointer_handle_motion,
184 pointer_handle_button,
185 pointer_handle_axis,
186};
187
188static void
189keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
190 uint32_t format, int fd, uint32_t size)
191{
192 close(fd);
193
194 fprintf(stderr, "test-client: got keyboard keymap\n");
195}
196
197static void
198keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
199 uint32_t serial, struct wl_surface *wl_surface,
200 struct wl_array *keys)
201{
202 struct keyboard *keyboard = data;
203
204 keyboard->focus = wl_surface_get_user_data(wl_surface);
205
206 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
207 keyboard->focus);
208}
209
210static void
211keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
212 uint32_t serial, struct wl_surface *wl_surface)
213{
214 struct keyboard *keyboard = data;
215
216 keyboard->focus = NULL;
217
218 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
219 wl_surface_get_user_data(wl_surface));
220}
221
222static void
223keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
224 uint32_t serial, uint32_t time, uint32_t key,
225 uint32_t state)
226{
227 struct keyboard *keyboard = data;
228
229 keyboard->key = key;
230 keyboard->state = state;
231
232 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
233}
234
235static void
236keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
237 uint32_t serial, uint32_t mods_depressed,
238 uint32_t mods_latched, uint32_t mods_locked,
239 uint32_t group)
240{
241 struct keyboard *keyboard = data;
242
243 keyboard->mods_depressed = mods_depressed;
244 keyboard->mods_latched = mods_latched;
245 keyboard->mods_locked = mods_locked;
246 keyboard->group = group;
247
248 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
249 mods_depressed, mods_latched, mods_locked, group);
250}
251
Marek Chalupa643d85f2015-03-30 06:37:56 -0400252static void
253keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
254 int32_t rate, int32_t delay)
255{
256 struct keyboard *keyboard = data;
257
258 keyboard->repeat_info.rate = rate;
259 keyboard->repeat_info.delay = delay;
260
261 fprintf(stderr, "test-client: got keyboard repeat_info %d %d\n",
262 rate, delay);
263}
264
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800265static const struct wl_keyboard_listener keyboard_listener = {
266 keyboard_handle_keymap,
267 keyboard_handle_enter,
268 keyboard_handle_leave,
269 keyboard_handle_key,
270 keyboard_handle_modifiers,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400271 keyboard_handle_repeat_info,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800272};
273
274static void
Marek Chalupa8a5523c2015-03-30 09:20:07 -0400275touch_handle_down(void *data, struct wl_touch *wl_touch,
276 uint32_t serial, uint32_t time, struct wl_surface *surface,
277 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
278{
279 struct touch *touch = data;
280
281 touch->down_x = wl_fixed_to_int(x_w);
282 touch->down_y = wl_fixed_to_int(y_w);
283 touch->id = id;
284
285 fprintf(stderr, "test-client: got touch down %d %d, surf: %p, id: %d\n",
286 touch->down_x, touch->down_y, surface, id);
287}
288
289static void
290touch_handle_up(void *data, struct wl_touch *wl_touch,
291 uint32_t serial, uint32_t time, int32_t id)
292{
293 struct touch *touch = data;
294 touch->up_id = id;
295
296 fprintf(stderr, "test-client: got touch up, id: %d\n", id);
297}
298
299static void
300touch_handle_motion(void *data, struct wl_touch *wl_touch,
301 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
302{
303 struct touch *touch = data;
304 touch->x = wl_fixed_to_int(x_w);
305 touch->y = wl_fixed_to_int(y_w);
306
307 fprintf(stderr, "test-client: got touch motion, %d %d, id: %d\n",
308 touch->x, touch->y, id);
309}
310
311static void
312touch_handle_frame(void *data, struct wl_touch *wl_touch)
313{
314 struct touch *touch = data;
315
316 ++touch->frame_no;
317
318 fprintf(stderr, "test-client: got touch frame (%d)\n", touch->frame_no);
319}
320
321static void
322touch_handle_cancel(void *data, struct wl_touch *wl_touch)
323{
324 struct touch *touch = data;
325
326 ++touch->cancel_no;
327
328 fprintf(stderr, "test-client: got touch cancel (%d)\n", touch->cancel_no);
329}
330
331static const struct wl_touch_listener touch_listener = {
332 touch_handle_down,
333 touch_handle_up,
334 touch_handle_motion,
335 touch_handle_frame,
336 touch_handle_cancel,
337};
338
339static void
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800340surface_enter(void *data,
341 struct wl_surface *wl_surface, struct wl_output *output)
342{
343 struct surface *surface = data;
344
345 surface->output = wl_output_get_user_data(output);
346
347 fprintf(stderr, "test-client: got surface enter output %p\n",
348 surface->output);
349}
350
351static void
352surface_leave(void *data,
353 struct wl_surface *wl_surface, struct wl_output *output)
354{
355 struct surface *surface = data;
356
357 surface->output = NULL;
358
359 fprintf(stderr, "test-client: got surface leave output %p\n",
360 wl_output_get_user_data(output));
361}
362
363static const struct wl_surface_listener surface_listener = {
364 surface_enter,
365 surface_leave
366};
367
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200368struct wl_buffer *
369create_shm_buffer(struct client *client, int width, int height, void **pixels)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800370{
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800371 struct wl_shm *shm = client->wl_shm;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200372 int stride = width * 4;
373 int size = stride * height;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800374 struct wl_shm_pool *pool;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200375 struct wl_buffer *buffer;
376 int fd;
377 void *data;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800378
379 fd = os_create_anonymous_file(size);
380 assert(fd >= 0);
381
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200382 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
383 if (data == MAP_FAILED) {
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800384 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200385 assert(data != MAP_FAILED);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800386 }
387
388 pool = wl_shm_create_pool(shm, fd, size);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200389 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
390 WL_SHM_FORMAT_ARGB8888);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800391 wl_shm_pool_destroy(pool);
392
393 close(fd);
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200394
395 if (pixels)
396 *pixels = data;
397
398 return buffer;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800399}
400
401static void
402shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
403{
404 struct client *client = data;
405
406 if (format == WL_SHM_FORMAT_ARGB8888)
407 client->has_argb = 1;
408}
409
410struct wl_shm_listener shm_listener = {
411 shm_format
412};
413
414static void
Derek Foremanf6a65922015-02-24 09:32:14 -0600415test_handle_pointer_position(void *data, struct weston_test *weston_test,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800416 wl_fixed_t x, wl_fixed_t y)
417{
418 struct test *test = data;
419 test->pointer_x = wl_fixed_to_int(x);
420 test->pointer_y = wl_fixed_to_int(y);
421
422 fprintf(stderr, "test-client: got global pointer %d %d\n",
423 test->pointer_x, test->pointer_y);
424}
425
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000426static void
Derek Foremanf6a65922015-02-24 09:32:14 -0600427test_handle_n_egl_buffers(void *data, struct weston_test *weston_test, uint32_t n)
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000428{
429 struct test *test = data;
430
431 test->n_egl_buffers = n;
432}
433
Derek Foremanf6a65922015-02-24 09:32:14 -0600434static const struct weston_test_listener test_listener = {
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000435 test_handle_pointer_position,
436 test_handle_n_egl_buffers,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800437};
438
439static void
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400440input_update_devices(struct input *input)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800441{
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800442 struct pointer *pointer;
443 struct keyboard *keyboard;
Marek Chalupa8a5523c2015-03-30 09:20:07 -0400444 struct touch *touch;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800445
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400446 struct wl_seat *seat = input->wl_seat;
447 enum wl_seat_capability caps = input->caps;
448
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800449 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700450 pointer = xzalloc(sizeof *pointer);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800451 pointer->wl_pointer = wl_seat_get_pointer(seat);
452 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
453 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
454 pointer);
455 input->pointer = pointer;
456 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
457 wl_pointer_destroy(input->pointer->wl_pointer);
458 free(input->pointer);
459 input->pointer = NULL;
460 }
461
462 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700463 keyboard = xzalloc(sizeof *keyboard);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800464 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
465 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
466 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
467 keyboard);
468 input->keyboard = keyboard;
469 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
470 wl_keyboard_destroy(input->keyboard->wl_keyboard);
471 free(input->keyboard);
472 input->keyboard = NULL;
473 }
Marek Chalupa8a5523c2015-03-30 09:20:07 -0400474
475 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
476 touch = xzalloc(sizeof *touch);
477 touch->wl_touch = wl_seat_get_touch(seat);
478 wl_touch_set_user_data(touch->wl_touch, touch);
479 wl_touch_add_listener(touch->wl_touch, &touch_listener,
480 touch);
481 input->touch = touch;
482 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
483 wl_touch_destroy(input->touch->wl_touch);
484 free(input->touch);
485 input->touch = NULL;
486 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800487}
488
Marek Chalupa643d85f2015-03-30 06:37:56 -0400489static void
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400490seat_handle_capabilities(void *data, struct wl_seat *seat,
491 enum wl_seat_capability caps)
492{
493 struct input *input = data;
494
495 input->caps = caps;
496
497 /* we will create/update the devices only with the right (test) seat.
498 * If we haven't discovered which seat is the test seat, just
499 * store capabilities and bail out */
500 if(input->seat_name && strcmp(input->seat_name, "test-seat") == 0)
501 input_update_devices(input);
502
503 fprintf(stderr, "test-client: got seat %p capabilities: %x\n",
504 input, caps);
505}
506
507static void
Marek Chalupa643d85f2015-03-30 06:37:56 -0400508seat_handle_name(void *data, struct wl_seat *seat, const char *name)
509{
510 struct input *input = data;
511
512 input->seat_name = strdup(name);
513 assert(input->seat_name && "No memory");
514
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400515 fprintf(stderr, "test-client: got seat %p name: \'%s\'\n",
516 input, name);
Marek Chalupa643d85f2015-03-30 06:37:56 -0400517}
518
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800519static const struct wl_seat_listener seat_listener = {
520 seat_handle_capabilities,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400521 seat_handle_name,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800522};
523
524static void
525output_handle_geometry(void *data,
526 struct wl_output *wl_output,
527 int x, int y,
528 int physical_width,
529 int physical_height,
530 int subpixel,
531 const char *make,
532 const char *model,
533 int32_t transform)
534{
535 struct output *output = data;
536
537 output->x = x;
538 output->y = y;
539}
540
541static void
542output_handle_mode(void *data,
543 struct wl_output *wl_output,
544 uint32_t flags,
545 int width,
546 int height,
547 int refresh)
548{
549 struct output *output = data;
550
551 if (flags & WL_OUTPUT_MODE_CURRENT) {
552 output->width = width;
553 output->height = height;
554 }
555}
556
Marek Chalupa643d85f2015-03-30 06:37:56 -0400557static void
558output_handle_scale(void *data,
559 struct wl_output *wl_output,
560 int scale)
561{
562 struct output *output = data;
563
564 output->scale = scale;
565}
566
567static void
568output_handle_done(void *data,
569 struct wl_output *wl_output)
570{
571 struct output *output = data;
572
573 output->initialized = 1;
574}
575
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800576static const struct wl_output_listener output_listener = {
577 output_handle_geometry,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400578 output_handle_mode,
579 output_handle_done,
580 output_handle_scale,
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800581};
582
583static void
584handle_global(void *data, struct wl_registry *registry,
585 uint32_t id, const char *interface, uint32_t version)
586{
587 struct client *client = data;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800588 struct output *output;
589 struct test *test;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500590 struct global *global;
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400591 struct input *input;
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500592
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700593 global = xzalloc(sizeof *global);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500594 global->name = id;
595 global->interface = strdup(interface);
596 assert(interface);
597 global->version = version;
598 wl_list_insert(client->global_list.prev, &global->link);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800599
600 if (strcmp(interface, "wl_compositor") == 0) {
601 client->wl_compositor =
602 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400603 &wl_compositor_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800604 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700605 input = xzalloc(sizeof *input);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800606 input->wl_seat =
607 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400608 &wl_seat_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800609 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400610 wl_list_insert(&client->inputs, &input->link);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800611 } else if (strcmp(interface, "wl_shm") == 0) {
612 client->wl_shm =
613 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400614 &wl_shm_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800615 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
616 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700617 output = xzalloc(sizeof *output);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800618 output->wl_output =
619 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400620 &wl_output_interface, version);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800621 wl_output_add_listener(output->wl_output,
622 &output_listener, output);
623 client->output = output;
Derek Foremanf6a65922015-02-24 09:32:14 -0600624 } else if (strcmp(interface, "weston_test") == 0) {
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700625 test = xzalloc(sizeof *test);
Derek Foremanf6a65922015-02-24 09:32:14 -0600626 test->weston_test =
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800627 wl_registry_bind(registry, id,
Marek Chalupa643d85f2015-03-30 06:37:56 -0400628 &weston_test_interface, version);
Derek Foremanf6a65922015-02-24 09:32:14 -0600629 weston_test_add_listener(test->weston_test, &test_listener, test);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800630 client->test = test;
Derek Foreman9bb13392015-01-23 12:12:36 -0600631 } else if (strcmp(interface, "wl_drm") == 0) {
632 client->has_wl_drm = true;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800633 }
634}
635
636static const struct wl_registry_listener registry_listener = {
637 handle_global
638};
639
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800640void
641skip(const char *fmt, ...)
642{
643 va_list argp;
644
645 va_start(argp, fmt);
646 vfprintf(stderr, fmt, argp);
647 va_end(argp);
648
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100649 /* automake tests uses exit code 77. weston-test-runner will see
650 * this and use it, and then weston-test's sigchld handler (in the
651 * weston process) will use that as an exit status, which is what
652 * automake will see in the end. */
653 exit(77);
Kristian Høgsberg42284f52014-01-01 17:38:04 -0800654}
655
Marek Chalupa4d06d462014-07-16 11:27:06 +0200656void
657expect_protocol_error(struct client *client,
658 const struct wl_interface *intf,
659 uint32_t code)
660{
661 int err;
662 uint32_t errcode, failed = 0;
663 const struct wl_interface *interface;
664 unsigned int id;
665
666 /* if the error has not come yet, make it happen */
667 wl_display_roundtrip(client->wl_display);
668
669 err = wl_display_get_error(client->wl_display);
670
671 assert(err && "Expected protocol error but nothing came");
672 assert(err == EPROTO && "Expected protocol error but got local error");
673
674 errcode = wl_display_get_protocol_error(client->wl_display,
675 &interface, &id);
676
677 /* check error */
678 if (errcode != code) {
679 fprintf(stderr, "Should get error code %d but got %d\n",
680 code, errcode);
681 failed = 1;
682 }
683
684 /* this should be definitely set */
685 assert(interface);
686
687 if (strcmp(intf->name, interface->name) != 0) {
688 fprintf(stderr, "Should get interface '%s' but got '%s'\n",
689 intf->name, interface->name);
690 failed = 1;
691 }
692
693 if (failed) {
694 fprintf(stderr, "Expected other protocol error\n");
695 abort();
696 }
697
698 /* all OK */
699 fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) "
700 "with code %d\n", interface->name, id, errcode);
701}
702
Pekka Paalanen07921d72012-12-12 14:26:40 +0200703static void
704log_handler(const char *fmt, va_list args)
705{
706 fprintf(stderr, "libwayland: ");
707 vfprintf(stderr, fmt, args);
708}
709
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400710static void
711input_destroy(struct input *inp)
712{
713 wl_list_remove(&inp->link);
714 wl_seat_destroy(inp->wl_seat);
715 free(inp);
716}
717
718/* find the test-seat and set it in client.
719 * Destroy other inputs */
720static void
721client_set_input(struct client *cl)
722{
723 struct input *inp, *inptmp;
724 wl_list_for_each_safe(inp, inptmp, &cl->inputs, link) {
725 assert(inp->seat_name && "BUG: input with no name");
726 if (strcmp(inp->seat_name, "test-seat") == 0) {
727 cl->input = inp;
728 input_update_devices(inp);
729 } else {
730 input_destroy(inp);
731 }
732 }
733
734 /* we keep only one input */
735 assert(wl_list_length(&cl->inputs) == 1);
736}
737
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800738struct client *
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200739create_client(void)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800740{
741 struct client *client;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800742
Pekka Paalanen07921d72012-12-12 14:26:40 +0200743 wl_log_set_handler_client(log_handler);
744
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800745 /* connect to display */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700746 client = xzalloc(sizeof *client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800747 client->wl_display = wl_display_connect(NULL);
748 assert(client->wl_display);
Kristian Høgsberg1cb3df42012-12-11 23:03:56 -0500749 wl_list_init(&client->global_list);
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400750 wl_list_init(&client->inputs);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800751
752 /* setup registry so we can bind to interfaces */
753 client->wl_registry = wl_display_get_registry(client->wl_display);
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200754 wl_registry_add_listener(client->wl_registry, &registry_listener,
755 client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800756
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400757 /* this roundtrip makes sure we have all globals and we bound to them */
758 client_roundtrip(client);
759 /* this roundtrip makes sure we got all wl_shm.format and wl_seat.*
760 * events */
761 client_roundtrip(client);
762
763 /* find the right input for us */
764 client_set_input(client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800765
766 /* must have WL_SHM_FORMAT_ARGB32 */
767 assert(client->has_argb);
768
Derek Foremanf6a65922015-02-24 09:32:14 -0600769 /* must have weston_test interface */
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800770 assert(client->test);
771
772 /* must have an output */
773 assert(client->output);
774
Marek Chalupa643d85f2015-03-30 06:37:56 -0400775 /* the output must be initialized */
776 assert(client->output->initialized == 1);
777
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400778 /* must have seat set */
779 assert(client->input);
780
Pekka Paalanen1ffd4612015-03-26 12:49:35 +0200781 return client;
782}
783
784struct client *
785client_create(int x, int y, int width, int height)
786{
787 struct client *client;
788 struct surface *surface;
789
790 client = create_client();
791
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800792 /* initialize the client surface */
Kristian Høgsbergc1b244f2013-10-09 14:01:23 -0700793 surface = xzalloc(sizeof *surface);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800794 surface->wl_surface =
795 wl_compositor_create_surface(client->wl_compositor);
796 assert(surface->wl_surface);
797
798 wl_surface_add_listener(surface->wl_surface, &surface_listener,
799 surface);
800
801 client->surface = surface;
802 wl_surface_set_user_data(surface->wl_surface, surface);
803
804 surface->width = width;
805 surface->height = height;
Pekka Paalanen32ac9b92013-02-08 17:01:26 +0200806 surface->wl_buffer = create_shm_buffer(client, width, height,
807 &surface->data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800808
809 memset(surface->data, 64, width * height * 4);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800810
811 move_client(client, x, y);
812
813 return client;
814}