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 | |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 35 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 36 | #define min(a, b) (((a) > (b)) ? (b) : (a)) |
| 37 | #define clip(x, a, b) min(max(x, a), b) |
| 38 | |
Bryce Harrington | 61a6436 | 2015-04-22 18:23:01 -0700 | [diff] [blame] | 39 | void * |
| 40 | fail_on_null(void *p) |
| 41 | { |
| 42 | if (p == NULL) { |
| 43 | fprintf(stderr, "out of memory\n"); |
| 44 | exit(EXIT_FAILURE); |
| 45 | } |
| 46 | return p; |
| 47 | } |
| 48 | |
| 49 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 50 | int |
| 51 | surface_contains(struct surface *surface, int x, int y) |
| 52 | { |
| 53 | /* test whether a global x,y point is contained in the surface */ |
| 54 | int sx = surface->x; |
| 55 | int sy = surface->y; |
| 56 | int sw = surface->width; |
| 57 | int sh = surface->height; |
| 58 | return x >= sx && y >= sy && x < sx + sw && y < sy + sh; |
| 59 | } |
| 60 | |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 61 | static void |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 62 | 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] | 63 | { |
| 64 | int *done = data; |
| 65 | |
| 66 | *done = 1; |
| 67 | |
| 68 | wl_callback_destroy(callback); |
| 69 | } |
| 70 | |
| 71 | static const struct wl_callback_listener frame_listener = { |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 72 | frame_callback_handler |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 75 | struct wl_callback * |
| 76 | frame_callback_set(struct wl_surface *surface, int *done) |
| 77 | { |
| 78 | struct wl_callback *callback; |
| 79 | |
| 80 | *done = 0; |
| 81 | callback = wl_surface_frame(surface); |
| 82 | wl_callback_add_listener(callback, &frame_listener, done); |
| 83 | |
| 84 | return callback; |
| 85 | } |
| 86 | |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 87 | int |
| 88 | frame_callback_wait_nofail(struct client *client, int *done) |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 89 | { |
| 90 | while (!*done) { |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 91 | if (wl_display_dispatch(client->wl_display) < 0) |
| 92 | return 0; |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 93 | } |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 94 | |
| 95 | return 1; |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 96 | } |
| 97 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 98 | void |
| 99 | move_client(struct client *client, int x, int y) |
| 100 | { |
| 101 | struct surface *surface = client->surface; |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 102 | int done; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 103 | |
| 104 | client->surface->x = x; |
| 105 | client->surface->y = y; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 106 | weston_test_move_surface(client->test->weston_test, surface->wl_surface, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 107 | surface->x, surface->y); |
Bryce Harrington | cb50ed2 | 2014-12-10 18:33:47 -0800 | [diff] [blame] | 108 | /* The attach here is necessary because commit() will call configure |
Giulio Camuffo | 82cb505 | 2013-02-28 18:44:54 +0100 | [diff] [blame] | 109 | * only on surfaces newly attached, and the one that sets the surface |
| 110 | * position is the configure. */ |
| 111 | wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 112 | wl_surface_damage(surface->wl_surface, 0, 0, surface->width, |
| 113 | surface->height); |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 114 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 115 | frame_callback_set(surface->wl_surface, &done); |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 116 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 117 | wl_surface_commit(surface->wl_surface); |
| 118 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 119 | frame_callback_wait(client, &done); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 122 | int |
| 123 | get_n_egl_buffers(struct client *client) |
| 124 | { |
| 125 | client->test->n_egl_buffers = -1; |
| 126 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 127 | weston_test_get_n_egl_buffers(client->test->weston_test); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 128 | wl_display_roundtrip(client->wl_display); |
| 129 | |
| 130 | return client->test->n_egl_buffers; |
| 131 | } |
| 132 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 133 | static void |
| 134 | pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, |
| 135 | uint32_t serial, struct wl_surface *wl_surface, |
| 136 | wl_fixed_t x, wl_fixed_t y) |
| 137 | { |
| 138 | struct pointer *pointer = data; |
| 139 | |
| 140 | pointer->focus = wl_surface_get_user_data(wl_surface); |
| 141 | pointer->x = wl_fixed_to_int(x); |
| 142 | pointer->y = wl_fixed_to_int(y); |
| 143 | |
| 144 | fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n", |
| 145 | pointer->x, pointer->y, pointer->focus); |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, |
| 150 | uint32_t serial, struct wl_surface *wl_surface) |
| 151 | { |
| 152 | struct pointer *pointer = data; |
| 153 | |
| 154 | pointer->focus = NULL; |
| 155 | |
| 156 | fprintf(stderr, "test-client: got pointer leave, surface %p\n", |
| 157 | wl_surface_get_user_data(wl_surface)); |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, |
| 162 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 163 | { |
| 164 | struct pointer *pointer = data; |
| 165 | |
| 166 | pointer->x = wl_fixed_to_int(x); |
| 167 | pointer->y = wl_fixed_to_int(y); |
| 168 | |
| 169 | fprintf(stderr, "test-client: got pointer motion %d %d\n", |
| 170 | pointer->x, pointer->y); |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | pointer_handle_button(void *data, struct wl_pointer *wl_pointer, |
| 175 | uint32_t serial, uint32_t time, uint32_t button, |
| 176 | uint32_t state) |
| 177 | { |
| 178 | struct pointer *pointer = data; |
| 179 | |
| 180 | pointer->button = button; |
| 181 | pointer->state = state; |
| 182 | |
| 183 | fprintf(stderr, "test-client: got pointer button %u %u\n", |
| 184 | button, state); |
| 185 | } |
| 186 | |
| 187 | static void |
| 188 | pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, |
| 189 | uint32_t time, uint32_t axis, wl_fixed_t value) |
| 190 | { |
Kristian Høgsberg | 4c8ce20 | 2013-10-09 14:23:00 -0700 | [diff] [blame] | 191 | fprintf(stderr, "test-client: got pointer axis %u %f\n", |
| 192 | axis, wl_fixed_to_double(value)); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static const struct wl_pointer_listener pointer_listener = { |
| 196 | pointer_handle_enter, |
| 197 | pointer_handle_leave, |
| 198 | pointer_handle_motion, |
| 199 | pointer_handle_button, |
| 200 | pointer_handle_axis, |
| 201 | }; |
| 202 | |
| 203 | static void |
| 204 | keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard, |
| 205 | uint32_t format, int fd, uint32_t size) |
| 206 | { |
| 207 | close(fd); |
| 208 | |
| 209 | fprintf(stderr, "test-client: got keyboard keymap\n"); |
| 210 | } |
| 211 | |
| 212 | static void |
| 213 | keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, |
| 214 | uint32_t serial, struct wl_surface *wl_surface, |
| 215 | struct wl_array *keys) |
| 216 | { |
| 217 | struct keyboard *keyboard = data; |
| 218 | |
| 219 | keyboard->focus = wl_surface_get_user_data(wl_surface); |
| 220 | |
| 221 | fprintf(stderr, "test-client: got keyboard enter, surface %p\n", |
| 222 | keyboard->focus); |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard, |
| 227 | uint32_t serial, struct wl_surface *wl_surface) |
| 228 | { |
| 229 | struct keyboard *keyboard = data; |
| 230 | |
| 231 | keyboard->focus = NULL; |
| 232 | |
| 233 | fprintf(stderr, "test-client: got keyboard leave, surface %p\n", |
| 234 | wl_surface_get_user_data(wl_surface)); |
| 235 | } |
| 236 | |
| 237 | static void |
| 238 | keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, |
| 239 | uint32_t serial, uint32_t time, uint32_t key, |
| 240 | uint32_t state) |
| 241 | { |
| 242 | struct keyboard *keyboard = data; |
| 243 | |
| 244 | keyboard->key = key; |
| 245 | keyboard->state = state; |
| 246 | |
| 247 | fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state); |
| 248 | } |
| 249 | |
| 250 | static void |
| 251 | keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, |
| 252 | uint32_t serial, uint32_t mods_depressed, |
| 253 | uint32_t mods_latched, uint32_t mods_locked, |
| 254 | uint32_t group) |
| 255 | { |
| 256 | struct keyboard *keyboard = data; |
| 257 | |
| 258 | keyboard->mods_depressed = mods_depressed; |
| 259 | keyboard->mods_latched = mods_latched; |
| 260 | keyboard->mods_locked = mods_locked; |
| 261 | keyboard->group = group; |
| 262 | |
| 263 | fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n", |
| 264 | mods_depressed, mods_latched, mods_locked, group); |
| 265 | } |
| 266 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 267 | static void |
| 268 | keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard, |
| 269 | int32_t rate, int32_t delay) |
| 270 | { |
| 271 | struct keyboard *keyboard = data; |
| 272 | |
| 273 | keyboard->repeat_info.rate = rate; |
| 274 | keyboard->repeat_info.delay = delay; |
| 275 | |
| 276 | fprintf(stderr, "test-client: got keyboard repeat_info %d %d\n", |
| 277 | rate, delay); |
| 278 | } |
| 279 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 280 | static const struct wl_keyboard_listener keyboard_listener = { |
| 281 | keyboard_handle_keymap, |
| 282 | keyboard_handle_enter, |
| 283 | keyboard_handle_leave, |
| 284 | keyboard_handle_key, |
| 285 | keyboard_handle_modifiers, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 286 | keyboard_handle_repeat_info, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | static void |
Marek Chalupa | 8a5523c | 2015-03-30 09:20:07 -0400 | [diff] [blame] | 290 | touch_handle_down(void *data, struct wl_touch *wl_touch, |
| 291 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 292 | int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 293 | { |
| 294 | struct touch *touch = data; |
| 295 | |
| 296 | touch->down_x = wl_fixed_to_int(x_w); |
| 297 | touch->down_y = wl_fixed_to_int(y_w); |
| 298 | touch->id = id; |
| 299 | |
| 300 | fprintf(stderr, "test-client: got touch down %d %d, surf: %p, id: %d\n", |
| 301 | touch->down_x, touch->down_y, surface, id); |
| 302 | } |
| 303 | |
| 304 | static void |
| 305 | touch_handle_up(void *data, struct wl_touch *wl_touch, |
| 306 | uint32_t serial, uint32_t time, int32_t id) |
| 307 | { |
| 308 | struct touch *touch = data; |
| 309 | touch->up_id = id; |
| 310 | |
| 311 | fprintf(stderr, "test-client: got touch up, id: %d\n", id); |
| 312 | } |
| 313 | |
| 314 | static void |
| 315 | touch_handle_motion(void *data, struct wl_touch *wl_touch, |
| 316 | uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 317 | { |
| 318 | struct touch *touch = data; |
| 319 | touch->x = wl_fixed_to_int(x_w); |
| 320 | touch->y = wl_fixed_to_int(y_w); |
| 321 | |
| 322 | fprintf(stderr, "test-client: got touch motion, %d %d, id: %d\n", |
| 323 | touch->x, touch->y, id); |
| 324 | } |
| 325 | |
| 326 | static void |
| 327 | touch_handle_frame(void *data, struct wl_touch *wl_touch) |
| 328 | { |
| 329 | struct touch *touch = data; |
| 330 | |
| 331 | ++touch->frame_no; |
| 332 | |
| 333 | fprintf(stderr, "test-client: got touch frame (%d)\n", touch->frame_no); |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
| 338 | { |
| 339 | struct touch *touch = data; |
| 340 | |
| 341 | ++touch->cancel_no; |
| 342 | |
| 343 | fprintf(stderr, "test-client: got touch cancel (%d)\n", touch->cancel_no); |
| 344 | } |
| 345 | |
| 346 | static const struct wl_touch_listener touch_listener = { |
| 347 | touch_handle_down, |
| 348 | touch_handle_up, |
| 349 | touch_handle_motion, |
| 350 | touch_handle_frame, |
| 351 | touch_handle_cancel, |
| 352 | }; |
| 353 | |
| 354 | static void |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 355 | surface_enter(void *data, |
| 356 | struct wl_surface *wl_surface, struct wl_output *output) |
| 357 | { |
| 358 | struct surface *surface = data; |
| 359 | |
| 360 | surface->output = wl_output_get_user_data(output); |
| 361 | |
| 362 | fprintf(stderr, "test-client: got surface enter output %p\n", |
| 363 | surface->output); |
| 364 | } |
| 365 | |
| 366 | static void |
| 367 | surface_leave(void *data, |
| 368 | struct wl_surface *wl_surface, struct wl_output *output) |
| 369 | { |
| 370 | struct surface *surface = data; |
| 371 | |
| 372 | surface->output = NULL; |
| 373 | |
| 374 | fprintf(stderr, "test-client: got surface leave output %p\n", |
| 375 | wl_output_get_user_data(output)); |
| 376 | } |
| 377 | |
| 378 | static const struct wl_surface_listener surface_listener = { |
| 379 | surface_enter, |
| 380 | surface_leave |
| 381 | }; |
| 382 | |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 383 | struct wl_buffer * |
| 384 | 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] | 385 | { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 386 | struct wl_shm *shm = client->wl_shm; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 387 | int stride = width * 4; |
| 388 | int size = stride * height; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 389 | struct wl_shm_pool *pool; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 390 | struct wl_buffer *buffer; |
| 391 | int fd; |
| 392 | void *data; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 393 | |
| 394 | fd = os_create_anonymous_file(size); |
| 395 | assert(fd >= 0); |
| 396 | |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 397 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 398 | if (data == MAP_FAILED) { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 399 | close(fd); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 400 | assert(data != MAP_FAILED); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | pool = wl_shm_create_pool(shm, fd, size); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 404 | buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, |
| 405 | WL_SHM_FORMAT_ARGB8888); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 406 | wl_shm_pool_destroy(pool); |
| 407 | |
| 408 | close(fd); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 409 | |
| 410 | if (pixels) |
| 411 | *pixels = data; |
| 412 | |
| 413 | return buffer; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void |
| 417 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 418 | { |
| 419 | struct client *client = data; |
| 420 | |
| 421 | if (format == WL_SHM_FORMAT_ARGB8888) |
| 422 | client->has_argb = 1; |
| 423 | } |
| 424 | |
| 425 | struct wl_shm_listener shm_listener = { |
| 426 | shm_format |
| 427 | }; |
| 428 | |
| 429 | static void |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 430 | test_handle_pointer_position(void *data, struct weston_test *weston_test, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 431 | wl_fixed_t x, wl_fixed_t y) |
| 432 | { |
| 433 | struct test *test = data; |
| 434 | test->pointer_x = wl_fixed_to_int(x); |
| 435 | test->pointer_y = wl_fixed_to_int(y); |
| 436 | |
| 437 | fprintf(stderr, "test-client: got global pointer %d %d\n", |
| 438 | test->pointer_x, test->pointer_y); |
| 439 | } |
| 440 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 441 | static void |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 442 | test_handle_n_egl_buffers(void *data, struct weston_test *weston_test, uint32_t n) |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 443 | { |
| 444 | struct test *test = data; |
| 445 | |
| 446 | test->n_egl_buffers = n; |
| 447 | } |
| 448 | |
Bryce Harrington | 692275f | 2015-04-23 16:33:49 -0700 | [diff] [blame] | 449 | static void |
| 450 | test_handle_capture_screenshot_done(void *data, struct weston_test *weston_test) |
| 451 | { |
| 452 | struct test *test = data; |
| 453 | |
| 454 | printf("Screenshot has been captured\n"); |
| 455 | test->buffer_copy_done = 1; |
| 456 | } |
| 457 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 458 | static const struct weston_test_listener test_listener = { |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 459 | test_handle_pointer_position, |
| 460 | test_handle_n_egl_buffers, |
Bryce Harrington | 692275f | 2015-04-23 16:33:49 -0700 | [diff] [blame] | 461 | test_handle_capture_screenshot_done, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 462 | }; |
| 463 | |
| 464 | static void |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 465 | input_update_devices(struct input *input) |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 466 | { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 467 | struct pointer *pointer; |
| 468 | struct keyboard *keyboard; |
Marek Chalupa | 8a5523c | 2015-03-30 09:20:07 -0400 | [diff] [blame] | 469 | struct touch *touch; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 470 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 471 | struct wl_seat *seat = input->wl_seat; |
| 472 | enum wl_seat_capability caps = input->caps; |
| 473 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 474 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 475 | pointer = xzalloc(sizeof *pointer); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 476 | pointer->wl_pointer = wl_seat_get_pointer(seat); |
| 477 | wl_pointer_set_user_data(pointer->wl_pointer, pointer); |
| 478 | wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener, |
| 479 | pointer); |
| 480 | input->pointer = pointer; |
| 481 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 482 | wl_pointer_destroy(input->pointer->wl_pointer); |
| 483 | free(input->pointer); |
| 484 | input->pointer = NULL; |
| 485 | } |
| 486 | |
| 487 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 488 | keyboard = xzalloc(sizeof *keyboard); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 489 | keyboard->wl_keyboard = wl_seat_get_keyboard(seat); |
| 490 | wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard); |
| 491 | wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener, |
| 492 | keyboard); |
| 493 | input->keyboard = keyboard; |
| 494 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 495 | wl_keyboard_destroy(input->keyboard->wl_keyboard); |
| 496 | free(input->keyboard); |
| 497 | input->keyboard = NULL; |
| 498 | } |
Marek Chalupa | 8a5523c | 2015-03-30 09:20:07 -0400 | [diff] [blame] | 499 | |
| 500 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) { |
| 501 | touch = xzalloc(sizeof *touch); |
| 502 | touch->wl_touch = wl_seat_get_touch(seat); |
| 503 | wl_touch_set_user_data(touch->wl_touch, touch); |
| 504 | wl_touch_add_listener(touch->wl_touch, &touch_listener, |
| 505 | touch); |
| 506 | input->touch = touch; |
| 507 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) { |
| 508 | wl_touch_destroy(input->touch->wl_touch); |
| 509 | free(input->touch); |
| 510 | input->touch = NULL; |
| 511 | } |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 514 | static void |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 515 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 516 | enum wl_seat_capability caps) |
| 517 | { |
| 518 | struct input *input = data; |
| 519 | |
| 520 | input->caps = caps; |
| 521 | |
| 522 | /* we will create/update the devices only with the right (test) seat. |
| 523 | * If we haven't discovered which seat is the test seat, just |
| 524 | * store capabilities and bail out */ |
| 525 | if(input->seat_name && strcmp(input->seat_name, "test-seat") == 0) |
| 526 | input_update_devices(input); |
| 527 | |
| 528 | fprintf(stderr, "test-client: got seat %p capabilities: %x\n", |
| 529 | input, caps); |
| 530 | } |
| 531 | |
| 532 | static void |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 533 | seat_handle_name(void *data, struct wl_seat *seat, const char *name) |
| 534 | { |
| 535 | struct input *input = data; |
| 536 | |
| 537 | input->seat_name = strdup(name); |
| 538 | assert(input->seat_name && "No memory"); |
| 539 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 540 | fprintf(stderr, "test-client: got seat %p name: \'%s\'\n", |
| 541 | input, name); |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 542 | } |
| 543 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 544 | static const struct wl_seat_listener seat_listener = { |
| 545 | seat_handle_capabilities, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 546 | seat_handle_name, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 547 | }; |
| 548 | |
| 549 | static void |
| 550 | output_handle_geometry(void *data, |
| 551 | struct wl_output *wl_output, |
| 552 | int x, int y, |
| 553 | int physical_width, |
| 554 | int physical_height, |
| 555 | int subpixel, |
| 556 | const char *make, |
| 557 | const char *model, |
| 558 | int32_t transform) |
| 559 | { |
| 560 | struct output *output = data; |
| 561 | |
| 562 | output->x = x; |
| 563 | output->y = y; |
| 564 | } |
| 565 | |
| 566 | static void |
| 567 | output_handle_mode(void *data, |
| 568 | struct wl_output *wl_output, |
| 569 | uint32_t flags, |
| 570 | int width, |
| 571 | int height, |
| 572 | int refresh) |
| 573 | { |
| 574 | struct output *output = data; |
| 575 | |
| 576 | if (flags & WL_OUTPUT_MODE_CURRENT) { |
| 577 | output->width = width; |
| 578 | output->height = height; |
| 579 | } |
| 580 | } |
| 581 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 582 | static void |
| 583 | output_handle_scale(void *data, |
| 584 | struct wl_output *wl_output, |
| 585 | int scale) |
| 586 | { |
| 587 | struct output *output = data; |
| 588 | |
| 589 | output->scale = scale; |
| 590 | } |
| 591 | |
| 592 | static void |
| 593 | output_handle_done(void *data, |
| 594 | struct wl_output *wl_output) |
| 595 | { |
| 596 | struct output *output = data; |
| 597 | |
| 598 | output->initialized = 1; |
| 599 | } |
| 600 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 601 | static const struct wl_output_listener output_listener = { |
| 602 | output_handle_geometry, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 603 | output_handle_mode, |
| 604 | output_handle_done, |
| 605 | output_handle_scale, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | static void |
| 609 | handle_global(void *data, struct wl_registry *registry, |
| 610 | uint32_t id, const char *interface, uint32_t version) |
| 611 | { |
| 612 | struct client *client = data; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 613 | struct output *output; |
| 614 | struct test *test; |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 615 | struct global *global; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 616 | struct input *input; |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 617 | |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 618 | global = xzalloc(sizeof *global); |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 619 | global->name = id; |
| 620 | global->interface = strdup(interface); |
| 621 | assert(interface); |
| 622 | global->version = version; |
| 623 | wl_list_insert(client->global_list.prev, &global->link); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 624 | |
| 625 | if (strcmp(interface, "wl_compositor") == 0) { |
| 626 | client->wl_compositor = |
| 627 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 628 | &wl_compositor_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 629 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 630 | input = xzalloc(sizeof *input); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 631 | input->wl_seat = |
| 632 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 633 | &wl_seat_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 634 | wl_seat_add_listener(input->wl_seat, &seat_listener, input); |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 635 | wl_list_insert(&client->inputs, &input->link); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 636 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 637 | client->wl_shm = |
| 638 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 639 | &wl_shm_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 640 | wl_shm_add_listener(client->wl_shm, &shm_listener, client); |
| 641 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 642 | output = xzalloc(sizeof *output); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 643 | output->wl_output = |
| 644 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 645 | &wl_output_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 646 | wl_output_add_listener(output->wl_output, |
| 647 | &output_listener, output); |
| 648 | client->output = output; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 649 | } else if (strcmp(interface, "weston_test") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 650 | test = xzalloc(sizeof *test); |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 651 | test->weston_test = |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 652 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 653 | &weston_test_interface, version); |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 654 | weston_test_add_listener(test->weston_test, &test_listener, test); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 655 | client->test = test; |
Derek Foreman | 9bb1339 | 2015-01-23 12:12:36 -0600 | [diff] [blame] | 656 | } else if (strcmp(interface, "wl_drm") == 0) { |
| 657 | client->has_wl_drm = true; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | static const struct wl_registry_listener registry_listener = { |
| 662 | handle_global |
| 663 | }; |
| 664 | |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 665 | void |
| 666 | skip(const char *fmt, ...) |
| 667 | { |
| 668 | va_list argp; |
| 669 | |
| 670 | va_start(argp, fmt); |
| 671 | vfprintf(stderr, fmt, argp); |
| 672 | va_end(argp); |
| 673 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 674 | /* automake tests uses exit code 77. weston-test-runner will see |
| 675 | * this and use it, and then weston-test's sigchld handler (in the |
| 676 | * weston process) will use that as an exit status, which is what |
| 677 | * automake will see in the end. */ |
| 678 | exit(77); |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Marek Chalupa | 4d06d46 | 2014-07-16 11:27:06 +0200 | [diff] [blame] | 681 | void |
| 682 | expect_protocol_error(struct client *client, |
| 683 | const struct wl_interface *intf, |
| 684 | uint32_t code) |
| 685 | { |
| 686 | int err; |
| 687 | uint32_t errcode, failed = 0; |
| 688 | const struct wl_interface *interface; |
| 689 | unsigned int id; |
| 690 | |
| 691 | /* if the error has not come yet, make it happen */ |
| 692 | wl_display_roundtrip(client->wl_display); |
| 693 | |
| 694 | err = wl_display_get_error(client->wl_display); |
| 695 | |
| 696 | assert(err && "Expected protocol error but nothing came"); |
| 697 | assert(err == EPROTO && "Expected protocol error but got local error"); |
| 698 | |
| 699 | errcode = wl_display_get_protocol_error(client->wl_display, |
| 700 | &interface, &id); |
| 701 | |
| 702 | /* check error */ |
| 703 | if (errcode != code) { |
| 704 | fprintf(stderr, "Should get error code %d but got %d\n", |
| 705 | code, errcode); |
| 706 | failed = 1; |
| 707 | } |
| 708 | |
| 709 | /* this should be definitely set */ |
| 710 | assert(interface); |
| 711 | |
| 712 | if (strcmp(intf->name, interface->name) != 0) { |
| 713 | fprintf(stderr, "Should get interface '%s' but got '%s'\n", |
| 714 | intf->name, interface->name); |
| 715 | failed = 1; |
| 716 | } |
| 717 | |
| 718 | if (failed) { |
| 719 | fprintf(stderr, "Expected other protocol error\n"); |
| 720 | abort(); |
| 721 | } |
| 722 | |
| 723 | /* all OK */ |
| 724 | fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) " |
| 725 | "with code %d\n", interface->name, id, errcode); |
| 726 | } |
| 727 | |
Pekka Paalanen | 07921d7 | 2012-12-12 14:26:40 +0200 | [diff] [blame] | 728 | static void |
| 729 | log_handler(const char *fmt, va_list args) |
| 730 | { |
| 731 | fprintf(stderr, "libwayland: "); |
| 732 | vfprintf(stderr, fmt, args); |
| 733 | } |
| 734 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 735 | static void |
| 736 | input_destroy(struct input *inp) |
| 737 | { |
| 738 | wl_list_remove(&inp->link); |
| 739 | wl_seat_destroy(inp->wl_seat); |
| 740 | free(inp); |
| 741 | } |
| 742 | |
| 743 | /* find the test-seat and set it in client. |
| 744 | * Destroy other inputs */ |
| 745 | static void |
| 746 | client_set_input(struct client *cl) |
| 747 | { |
| 748 | struct input *inp, *inptmp; |
| 749 | wl_list_for_each_safe(inp, inptmp, &cl->inputs, link) { |
| 750 | assert(inp->seat_name && "BUG: input with no name"); |
| 751 | if (strcmp(inp->seat_name, "test-seat") == 0) { |
| 752 | cl->input = inp; |
| 753 | input_update_devices(inp); |
| 754 | } else { |
| 755 | input_destroy(inp); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /* we keep only one input */ |
| 760 | assert(wl_list_length(&cl->inputs) == 1); |
| 761 | } |
| 762 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 763 | struct client * |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 764 | create_client(void) |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 765 | { |
| 766 | struct client *client; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 767 | |
Pekka Paalanen | 07921d7 | 2012-12-12 14:26:40 +0200 | [diff] [blame] | 768 | wl_log_set_handler_client(log_handler); |
| 769 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 770 | /* connect to display */ |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 771 | client = xzalloc(sizeof *client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 772 | client->wl_display = wl_display_connect(NULL); |
| 773 | assert(client->wl_display); |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 774 | wl_list_init(&client->global_list); |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 775 | wl_list_init(&client->inputs); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 776 | |
| 777 | /* setup registry so we can bind to interfaces */ |
| 778 | client->wl_registry = wl_display_get_registry(client->wl_display); |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 779 | wl_registry_add_listener(client->wl_registry, ®istry_listener, |
| 780 | client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 781 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 782 | /* this roundtrip makes sure we have all globals and we bound to them */ |
| 783 | client_roundtrip(client); |
| 784 | /* this roundtrip makes sure we got all wl_shm.format and wl_seat.* |
| 785 | * events */ |
| 786 | client_roundtrip(client); |
| 787 | |
| 788 | /* find the right input for us */ |
| 789 | client_set_input(client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 790 | |
| 791 | /* must have WL_SHM_FORMAT_ARGB32 */ |
| 792 | assert(client->has_argb); |
| 793 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 794 | /* must have weston_test interface */ |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 795 | assert(client->test); |
| 796 | |
| 797 | /* must have an output */ |
| 798 | assert(client->output); |
| 799 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 800 | /* the output must be initialized */ |
| 801 | assert(client->output->initialized == 1); |
| 802 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 803 | /* must have seat set */ |
| 804 | assert(client->input); |
| 805 | |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 806 | return client; |
| 807 | } |
| 808 | |
| 809 | struct client * |
Pekka Paalanen | 4ac06ff | 2015-03-26 12:56:10 +0200 | [diff] [blame] | 810 | create_client_and_test_surface(int x, int y, int width, int height) |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 811 | { |
| 812 | struct client *client; |
| 813 | struct surface *surface; |
| 814 | |
| 815 | client = create_client(); |
| 816 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 817 | /* initialize the client surface */ |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 818 | surface = xzalloc(sizeof *surface); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 819 | surface->wl_surface = |
| 820 | wl_compositor_create_surface(client->wl_compositor); |
| 821 | assert(surface->wl_surface); |
| 822 | |
| 823 | wl_surface_add_listener(surface->wl_surface, &surface_listener, |
| 824 | surface); |
| 825 | |
| 826 | client->surface = surface; |
| 827 | wl_surface_set_user_data(surface->wl_surface, surface); |
| 828 | |
| 829 | surface->width = width; |
| 830 | surface->height = height; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 831 | surface->wl_buffer = create_shm_buffer(client, width, height, |
| 832 | &surface->data); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 833 | |
| 834 | memset(surface->data, 64, width * height * 4); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 835 | |
| 836 | move_client(client, x, y); |
| 837 | |
| 838 | return client; |
| 839 | } |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 840 | |
| 841 | static const char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame^] | 842 | output_path(void) |
| 843 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 844 | char *path = getenv("WESTON_TEST_OUTPUT_PATH"); |
| 845 | |
| 846 | if (!path) |
| 847 | return "."; |
| 848 | return path; |
| 849 | } |
| 850 | |
| 851 | char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame^] | 852 | screenshot_output_filename(const char *basename, uint32_t seq) |
| 853 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 854 | char *filename; |
| 855 | |
| 856 | if (asprintf(&filename, "%s/%s-%02d.png", |
| 857 | output_path(), basename, seq) < 0) |
| 858 | return NULL; |
| 859 | return filename; |
| 860 | } |
| 861 | |
| 862 | static const char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame^] | 863 | reference_path(void) |
| 864 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 865 | char *path = getenv("WESTON_TEST_REFERENCE_PATH"); |
| 866 | |
| 867 | if (!path) |
| 868 | return "./tests/reference"; |
| 869 | return path; |
| 870 | } |
| 871 | |
| 872 | char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame^] | 873 | screenshot_reference_filename(const char *basename, uint32_t seq) |
| 874 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 875 | char *filename; |
| 876 | |
| 877 | if (asprintf(&filename, "%s/%s-%02d.png", |
| 878 | reference_path(), basename, seq) < 0) |
| 879 | return NULL; |
| 880 | return filename; |
| 881 | } |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 882 | |
| 883 | /** |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 884 | * check_surfaces_geometry() - verifies two surfaces are same size |
| 885 | * |
| 886 | * @returns true if surfaces have the same width and height, or false |
| 887 | * if not, or if there is no actual data. |
| 888 | */ |
| 889 | bool |
| 890 | check_surfaces_geometry(const struct surface *a, const struct surface *b) |
| 891 | { |
| 892 | if (a == NULL || b == NULL) { |
| 893 | printf("Undefined surfaces\n"); |
| 894 | return false; |
| 895 | } |
| 896 | else if (a->data == NULL || b->data == NULL) { |
| 897 | printf("Undefined data\n"); |
| 898 | return false; |
| 899 | } |
| 900 | else if (a->width != b->width || a->height != b->height) { |
| 901 | printf("Mismatched dimensions: %d,%d != %d,%d\n", |
| 902 | a->width, a->height, b->width, b->height); |
| 903 | return false; |
| 904 | } |
| 905 | return true; |
| 906 | } |
| 907 | |
| 908 | /** |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 909 | * check_surfaces_equal() - tests if two surfaces are pixel-identical |
| 910 | * |
| 911 | * Returns true if surface buffers have all the same byte values, |
| 912 | * false if the surfaces don't match or can't be compared due to |
| 913 | * different dimensions. |
| 914 | */ |
| 915 | bool |
| 916 | check_surfaces_equal(const struct surface *a, const struct surface *b) |
| 917 | { |
| 918 | int bpp = 4; /* Assumes ARGB */ |
| 919 | |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 920 | if (!check_surfaces_geometry(a, b)) |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 921 | return false; |
| 922 | |
| 923 | return (memcmp(a->data, b->data, bpp * a->width * a->height) == 0); |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * check_surfaces_match_in_clip() - tests if a given region within two |
| 928 | * surfaces are pixel-identical. |
| 929 | * |
| 930 | * Returns true if the two surfaces have the same byte values within the |
| 931 | * given clipping region, or false if they don't match or the surfaces |
| 932 | * can't be compared. |
| 933 | */ |
| 934 | bool |
| 935 | check_surfaces_match_in_clip(const struct surface *a, const struct surface *b, const struct rectangle *clip_rect) |
| 936 | { |
| 937 | int i, j; |
| 938 | int x0, y0, x1, y1; |
| 939 | void *p, *q; |
| 940 | int bpp = 4; /* Assumes ARGB */ |
| 941 | |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 942 | if (!check_surfaces_geometry(a, b) || clip_rect == NULL) |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 943 | return false; |
| 944 | |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 945 | if (clip_rect->x > a->width || clip_rect->y > a->height) { |
| 946 | printf("Clip outside image boundaries\n"); |
| 947 | return true; |
| 948 | } |
| 949 | |
| 950 | x0 = max(0, clip_rect->x); |
| 951 | y0 = max(0, clip_rect->y); |
| 952 | x1 = min(a->width, clip_rect->x + clip_rect->width); |
| 953 | y1 = min(a->height, clip_rect->y + clip_rect->height); |
| 954 | |
| 955 | if (x0 == x1 || y0 == y1) { |
| 956 | printf("Degenerate comparison\n"); |
| 957 | return true; |
| 958 | } |
| 959 | |
| 960 | printf("Bytewise comparison inside clip\n"); |
| 961 | for (i=y0; i<y1; i++) { |
| 962 | p = a->data + i * a->width * bpp + x0 * bpp; |
| 963 | q = b->data + i * b->width * bpp + x0 * bpp; |
| 964 | if (memcmp(p, q, (x1-x0)*bpp) != 0) { |
| 965 | /* Dump the bad row */ |
| 966 | printf("Mismatched image on row %d\n", i); |
| 967 | for (j=0; j<(x1-x0)*bpp; j++) { |
| 968 | char a_char = *((char*)(p+j*bpp)); |
| 969 | char b_char = *((char*)(q+j*bpp)); |
| 970 | printf("%d,%d: %8x %8x %s\n", i, j, a_char, b_char, |
| 971 | (a_char != b_char)? " <---": ""); |
| 972 | } |
| 973 | return false; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | return true; |
| 978 | } |