U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 1 | /* |
| 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 Harrington | 12cc405 | 2014-11-19 17:18:33 -0800 | [diff] [blame] | 23 | #include "config.h" |
Kristian Høgsberg | c7d2c4c | 2013-08-26 14:43:17 -0700 | [diff] [blame] | 24 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
Marek Chalupa | 4d06d46 | 2014-07-16 11:27:06 +0200 | [diff] [blame] | 29 | #include <errno.h> |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 30 | #include <sys/mman.h> |
| 31 | |
| 32 | #include "../shared/os-compatibility.h" |
| 33 | #include "weston-test-client-helper.h" |
| 34 | |
| 35 | int |
| 36 | surface_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øgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 46 | static void |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 47 | frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 48 | { |
| 49 | int *done = data; |
| 50 | |
| 51 | *done = 1; |
| 52 | |
| 53 | wl_callback_destroy(callback); |
| 54 | } |
| 55 | |
| 56 | static const struct wl_callback_listener frame_listener = { |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 57 | frame_callback_handler |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 58 | }; |
| 59 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 60 | struct wl_callback * |
| 61 | frame_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 Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 72 | int |
| 73 | frame_callback_wait_nofail(struct client *client, int *done) |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 74 | { |
| 75 | while (!*done) { |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 76 | if (wl_display_dispatch(client->wl_display) < 0) |
| 77 | return 0; |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 78 | } |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 79 | |
| 80 | return 1; |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 81 | } |
| 82 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 83 | void |
| 84 | move_client(struct client *client, int x, int y) |
| 85 | { |
| 86 | struct surface *surface = client->surface; |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 87 | int done; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 88 | |
| 89 | client->surface->x = x; |
| 90 | client->surface->y = y; |
| 91 | wl_test_move_surface(client->test->wl_test, surface->wl_surface, |
| 92 | surface->x, surface->y); |
Bryce Harrington | cb50ed2 | 2014-12-10 18:33:47 -0800 | [diff] [blame^] | 93 | /* The attach here is necessary because commit() will call configure |
Giulio Camuffo | 82cb505 | 2013-02-28 18:44:54 +0100 | [diff] [blame] | 94 | * 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 Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 97 | wl_surface_damage(surface->wl_surface, 0, 0, surface->width, |
| 98 | surface->height); |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 99 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 100 | frame_callback_set(surface->wl_surface, &done); |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 101 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 102 | wl_surface_commit(surface->wl_surface); |
| 103 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 104 | frame_callback_wait(client, &done); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 107 | int |
| 108 | get_n_egl_buffers(struct client *client) |
| 109 | { |
| 110 | client->test->n_egl_buffers = -1; |
| 111 | |
| 112 | wl_test_get_n_egl_buffers(client->test->wl_test); |
| 113 | wl_display_roundtrip(client->wl_display); |
| 114 | |
| 115 | return client->test->n_egl_buffers; |
| 116 | } |
| 117 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 118 | static void |
| 119 | pointer_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 | |
| 133 | static void |
| 134 | pointer_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 | |
| 145 | static void |
| 146 | pointer_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 | |
| 158 | static void |
| 159 | pointer_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 | |
| 172 | static void |
| 173 | pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, |
| 174 | uint32_t time, uint32_t axis, wl_fixed_t value) |
| 175 | { |
Kristian Høgsberg | 4c8ce20 | 2013-10-09 14:23:00 -0700 | [diff] [blame] | 176 | fprintf(stderr, "test-client: got pointer axis %u %f\n", |
| 177 | axis, wl_fixed_to_double(value)); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static 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 | |
| 188 | static void |
| 189 | keyboard_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 | |
| 197 | static void |
| 198 | keyboard_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 | |
| 210 | static void |
| 211 | keyboard_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 | |
| 222 | static void |
| 223 | keyboard_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 | |
| 235 | static void |
| 236 | keyboard_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 | |
| 252 | static const struct wl_keyboard_listener keyboard_listener = { |
| 253 | keyboard_handle_keymap, |
| 254 | keyboard_handle_enter, |
| 255 | keyboard_handle_leave, |
| 256 | keyboard_handle_key, |
| 257 | keyboard_handle_modifiers, |
| 258 | }; |
| 259 | |
| 260 | static void |
| 261 | surface_enter(void *data, |
| 262 | struct wl_surface *wl_surface, struct wl_output *output) |
| 263 | { |
| 264 | struct surface *surface = data; |
| 265 | |
| 266 | surface->output = wl_output_get_user_data(output); |
| 267 | |
| 268 | fprintf(stderr, "test-client: got surface enter output %p\n", |
| 269 | surface->output); |
| 270 | } |
| 271 | |
| 272 | static void |
| 273 | surface_leave(void *data, |
| 274 | struct wl_surface *wl_surface, struct wl_output *output) |
| 275 | { |
| 276 | struct surface *surface = data; |
| 277 | |
| 278 | surface->output = NULL; |
| 279 | |
| 280 | fprintf(stderr, "test-client: got surface leave output %p\n", |
| 281 | wl_output_get_user_data(output)); |
| 282 | } |
| 283 | |
| 284 | static const struct wl_surface_listener surface_listener = { |
| 285 | surface_enter, |
| 286 | surface_leave |
| 287 | }; |
| 288 | |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 289 | struct wl_buffer * |
| 290 | create_shm_buffer(struct client *client, int width, int height, void **pixels) |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 291 | { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 292 | struct wl_shm *shm = client->wl_shm; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 293 | int stride = width * 4; |
| 294 | int size = stride * height; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 295 | struct wl_shm_pool *pool; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 296 | struct wl_buffer *buffer; |
| 297 | int fd; |
| 298 | void *data; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 299 | |
| 300 | fd = os_create_anonymous_file(size); |
| 301 | assert(fd >= 0); |
| 302 | |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 303 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 304 | if (data == MAP_FAILED) { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 305 | close(fd); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 306 | assert(data != MAP_FAILED); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | pool = wl_shm_create_pool(shm, fd, size); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 310 | buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, |
| 311 | WL_SHM_FORMAT_ARGB8888); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 312 | wl_shm_pool_destroy(pool); |
| 313 | |
| 314 | close(fd); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 315 | |
| 316 | if (pixels) |
| 317 | *pixels = data; |
| 318 | |
| 319 | return buffer; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void |
| 323 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 324 | { |
| 325 | struct client *client = data; |
| 326 | |
| 327 | if (format == WL_SHM_FORMAT_ARGB8888) |
| 328 | client->has_argb = 1; |
| 329 | } |
| 330 | |
| 331 | struct wl_shm_listener shm_listener = { |
| 332 | shm_format |
| 333 | }; |
| 334 | |
| 335 | static void |
| 336 | test_handle_pointer_position(void *data, struct wl_test *wl_test, |
| 337 | wl_fixed_t x, wl_fixed_t y) |
| 338 | { |
| 339 | struct test *test = data; |
| 340 | test->pointer_x = wl_fixed_to_int(x); |
| 341 | test->pointer_y = wl_fixed_to_int(y); |
| 342 | |
| 343 | fprintf(stderr, "test-client: got global pointer %d %d\n", |
| 344 | test->pointer_x, test->pointer_y); |
| 345 | } |
| 346 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 347 | static void |
| 348 | test_handle_n_egl_buffers(void *data, struct wl_test *wl_test, uint32_t n) |
| 349 | { |
| 350 | struct test *test = data; |
| 351 | |
| 352 | test->n_egl_buffers = n; |
| 353 | } |
| 354 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 355 | static const struct wl_test_listener test_listener = { |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 356 | test_handle_pointer_position, |
| 357 | test_handle_n_egl_buffers, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 358 | }; |
| 359 | |
| 360 | static void |
| 361 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 362 | enum wl_seat_capability caps) |
| 363 | { |
| 364 | struct input *input = data; |
| 365 | struct pointer *pointer; |
| 366 | struct keyboard *keyboard; |
| 367 | |
| 368 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 369 | pointer = xzalloc(sizeof *pointer); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 370 | pointer->wl_pointer = wl_seat_get_pointer(seat); |
| 371 | wl_pointer_set_user_data(pointer->wl_pointer, pointer); |
| 372 | wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener, |
| 373 | pointer); |
| 374 | input->pointer = pointer; |
| 375 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 376 | wl_pointer_destroy(input->pointer->wl_pointer); |
| 377 | free(input->pointer); |
| 378 | input->pointer = NULL; |
| 379 | } |
| 380 | |
| 381 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 382 | keyboard = xzalloc(sizeof *keyboard); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 383 | keyboard->wl_keyboard = wl_seat_get_keyboard(seat); |
| 384 | wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard); |
| 385 | wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener, |
| 386 | keyboard); |
| 387 | input->keyboard = keyboard; |
| 388 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 389 | wl_keyboard_destroy(input->keyboard->wl_keyboard); |
| 390 | free(input->keyboard); |
| 391 | input->keyboard = NULL; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | static const struct wl_seat_listener seat_listener = { |
| 396 | seat_handle_capabilities, |
| 397 | }; |
| 398 | |
| 399 | static void |
| 400 | output_handle_geometry(void *data, |
| 401 | struct wl_output *wl_output, |
| 402 | int x, int y, |
| 403 | int physical_width, |
| 404 | int physical_height, |
| 405 | int subpixel, |
| 406 | const char *make, |
| 407 | const char *model, |
| 408 | int32_t transform) |
| 409 | { |
| 410 | struct output *output = data; |
| 411 | |
| 412 | output->x = x; |
| 413 | output->y = y; |
| 414 | } |
| 415 | |
| 416 | static void |
| 417 | output_handle_mode(void *data, |
| 418 | struct wl_output *wl_output, |
| 419 | uint32_t flags, |
| 420 | int width, |
| 421 | int height, |
| 422 | int refresh) |
| 423 | { |
| 424 | struct output *output = data; |
| 425 | |
| 426 | if (flags & WL_OUTPUT_MODE_CURRENT) { |
| 427 | output->width = width; |
| 428 | output->height = height; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static const struct wl_output_listener output_listener = { |
| 433 | output_handle_geometry, |
| 434 | output_handle_mode |
| 435 | }; |
| 436 | |
| 437 | static void |
| 438 | handle_global(void *data, struct wl_registry *registry, |
| 439 | uint32_t id, const char *interface, uint32_t version) |
| 440 | { |
| 441 | struct client *client = data; |
| 442 | struct input *input; |
| 443 | struct output *output; |
| 444 | struct test *test; |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 445 | struct global *global; |
| 446 | |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 447 | global = xzalloc(sizeof *global); |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 448 | global->name = id; |
| 449 | global->interface = strdup(interface); |
| 450 | assert(interface); |
| 451 | global->version = version; |
| 452 | wl_list_insert(client->global_list.prev, &global->link); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 453 | |
| 454 | if (strcmp(interface, "wl_compositor") == 0) { |
| 455 | client->wl_compositor = |
| 456 | wl_registry_bind(registry, id, |
| 457 | &wl_compositor_interface, 1); |
| 458 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 459 | input = xzalloc(sizeof *input); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 460 | input->wl_seat = |
| 461 | wl_registry_bind(registry, id, |
| 462 | &wl_seat_interface, 1); |
| 463 | wl_seat_add_listener(input->wl_seat, &seat_listener, input); |
| 464 | client->input = input; |
| 465 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 466 | client->wl_shm = |
| 467 | wl_registry_bind(registry, id, |
| 468 | &wl_shm_interface, 1); |
| 469 | wl_shm_add_listener(client->wl_shm, &shm_listener, client); |
| 470 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 471 | output = xzalloc(sizeof *output); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 472 | output->wl_output = |
| 473 | wl_registry_bind(registry, id, |
| 474 | &wl_output_interface, 1); |
| 475 | wl_output_add_listener(output->wl_output, |
| 476 | &output_listener, output); |
| 477 | client->output = output; |
| 478 | } else if (strcmp(interface, "wl_test") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 479 | test = xzalloc(sizeof *test); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 480 | test->wl_test = |
| 481 | wl_registry_bind(registry, id, |
| 482 | &wl_test_interface, 1); |
| 483 | wl_test_add_listener(test->wl_test, &test_listener, test); |
| 484 | client->test = test; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | static const struct wl_registry_listener registry_listener = { |
| 489 | handle_global |
| 490 | }; |
| 491 | |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 492 | void |
| 493 | skip(const char *fmt, ...) |
| 494 | { |
| 495 | va_list argp; |
| 496 | |
| 497 | va_start(argp, fmt); |
| 498 | vfprintf(stderr, fmt, argp); |
| 499 | va_end(argp); |
| 500 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 501 | /* automake tests uses exit code 77. weston-test-runner will see |
| 502 | * this and use it, and then weston-test's sigchld handler (in the |
| 503 | * weston process) will use that as an exit status, which is what |
| 504 | * automake will see in the end. */ |
| 505 | exit(77); |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Marek Chalupa | 4d06d46 | 2014-07-16 11:27:06 +0200 | [diff] [blame] | 508 | void |
| 509 | expect_protocol_error(struct client *client, |
| 510 | const struct wl_interface *intf, |
| 511 | uint32_t code) |
| 512 | { |
| 513 | int err; |
| 514 | uint32_t errcode, failed = 0; |
| 515 | const struct wl_interface *interface; |
| 516 | unsigned int id; |
| 517 | |
| 518 | /* if the error has not come yet, make it happen */ |
| 519 | wl_display_roundtrip(client->wl_display); |
| 520 | |
| 521 | err = wl_display_get_error(client->wl_display); |
| 522 | |
| 523 | assert(err && "Expected protocol error but nothing came"); |
| 524 | assert(err == EPROTO && "Expected protocol error but got local error"); |
| 525 | |
| 526 | errcode = wl_display_get_protocol_error(client->wl_display, |
| 527 | &interface, &id); |
| 528 | |
| 529 | /* check error */ |
| 530 | if (errcode != code) { |
| 531 | fprintf(stderr, "Should get error code %d but got %d\n", |
| 532 | code, errcode); |
| 533 | failed = 1; |
| 534 | } |
| 535 | |
| 536 | /* this should be definitely set */ |
| 537 | assert(interface); |
| 538 | |
| 539 | if (strcmp(intf->name, interface->name) != 0) { |
| 540 | fprintf(stderr, "Should get interface '%s' but got '%s'\n", |
| 541 | intf->name, interface->name); |
| 542 | failed = 1; |
| 543 | } |
| 544 | |
| 545 | if (failed) { |
| 546 | fprintf(stderr, "Expected other protocol error\n"); |
| 547 | abort(); |
| 548 | } |
| 549 | |
| 550 | /* all OK */ |
| 551 | fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) " |
| 552 | "with code %d\n", interface->name, id, errcode); |
| 553 | } |
| 554 | |
Pekka Paalanen | 07921d7 | 2012-12-12 14:26:40 +0200 | [diff] [blame] | 555 | static void |
| 556 | log_handler(const char *fmt, va_list args) |
| 557 | { |
| 558 | fprintf(stderr, "libwayland: "); |
| 559 | vfprintf(stderr, fmt, args); |
| 560 | } |
| 561 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 562 | struct client * |
| 563 | client_create(int x, int y, int width, int height) |
| 564 | { |
| 565 | struct client *client; |
| 566 | struct surface *surface; |
| 567 | |
Pekka Paalanen | 07921d7 | 2012-12-12 14:26:40 +0200 | [diff] [blame] | 568 | wl_log_set_handler_client(log_handler); |
| 569 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 570 | /* connect to display */ |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 571 | client = xzalloc(sizeof *client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 572 | client->wl_display = wl_display_connect(NULL); |
| 573 | assert(client->wl_display); |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 574 | wl_list_init(&client->global_list); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 575 | |
| 576 | /* setup registry so we can bind to interfaces */ |
| 577 | client->wl_registry = wl_display_get_registry(client->wl_display); |
| 578 | wl_registry_add_listener(client->wl_registry, ®istry_listener, client); |
| 579 | |
| 580 | /* trigger global listener */ |
| 581 | wl_display_dispatch(client->wl_display); |
| 582 | wl_display_roundtrip(client->wl_display); |
| 583 | |
| 584 | /* must have WL_SHM_FORMAT_ARGB32 */ |
| 585 | assert(client->has_argb); |
| 586 | |
| 587 | /* must have wl_test interface */ |
| 588 | assert(client->test); |
| 589 | |
| 590 | /* must have an output */ |
| 591 | assert(client->output); |
| 592 | |
| 593 | /* initialize the client surface */ |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 594 | surface = xzalloc(sizeof *surface); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 595 | surface->wl_surface = |
| 596 | wl_compositor_create_surface(client->wl_compositor); |
| 597 | assert(surface->wl_surface); |
| 598 | |
| 599 | wl_surface_add_listener(surface->wl_surface, &surface_listener, |
| 600 | surface); |
| 601 | |
| 602 | client->surface = surface; |
| 603 | wl_surface_set_user_data(surface->wl_surface, surface); |
| 604 | |
| 605 | surface->width = width; |
| 606 | surface->height = height; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 607 | surface->wl_buffer = create_shm_buffer(client, width, height, |
| 608 | &surface->data); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 609 | |
| 610 | memset(surface->data, 64, width * height * 4); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 611 | |
| 612 | move_client(client, x, y); |
| 613 | |
| 614 | return client; |
| 615 | } |