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