U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 11 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
Bryce Harrington | 12cc405 | 2014-11-19 17:18:33 -0800 | [diff] [blame] | 26 | #include "config.h" |
Kristian Høgsberg | c7d2c4c | 2013-08-26 14:43:17 -0700 | [diff] [blame] | 27 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <unistd.h> |
Marek Chalupa | 4d06d46 | 2014-07-16 11:27:06 +0200 | [diff] [blame] | 32 | #include <errno.h> |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 33 | #include <sys/mman.h> |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame^] | 34 | #include <cairo.h> |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 35 | |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame^] | 36 | #include "zalloc.h" |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 37 | #include "shared/os-compatibility.h" |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 38 | #include "weston-test-client-helper.h" |
| 39 | |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 40 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 41 | #define min(a, b) (((a) > (b)) ? (b) : (a)) |
| 42 | #define clip(x, a, b) min(max(x, a), b) |
| 43 | |
Bryce Harrington | 61a6436 | 2015-04-22 18:23:01 -0700 | [diff] [blame] | 44 | void * |
| 45 | fail_on_null(void *p) |
| 46 | { |
| 47 | if (p == NULL) { |
| 48 | fprintf(stderr, "out of memory\n"); |
| 49 | exit(EXIT_FAILURE); |
| 50 | } |
| 51 | return p; |
| 52 | } |
| 53 | |
| 54 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 55 | int |
| 56 | surface_contains(struct surface *surface, int x, int y) |
| 57 | { |
| 58 | /* test whether a global x,y point is contained in the surface */ |
| 59 | int sx = surface->x; |
| 60 | int sy = surface->y; |
| 61 | int sw = surface->width; |
| 62 | int sh = surface->height; |
| 63 | return x >= sx && y >= sy && x < sx + sw && y < sy + sh; |
| 64 | } |
| 65 | |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 66 | static void |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 67 | 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] | 68 | { |
| 69 | int *done = data; |
| 70 | |
| 71 | *done = 1; |
| 72 | |
| 73 | wl_callback_destroy(callback); |
| 74 | } |
| 75 | |
| 76 | static const struct wl_callback_listener frame_listener = { |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 77 | frame_callback_handler |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 80 | struct wl_callback * |
| 81 | frame_callback_set(struct wl_surface *surface, int *done) |
| 82 | { |
| 83 | struct wl_callback *callback; |
| 84 | |
| 85 | *done = 0; |
| 86 | callback = wl_surface_frame(surface); |
| 87 | wl_callback_add_listener(callback, &frame_listener, done); |
| 88 | |
| 89 | return callback; |
| 90 | } |
| 91 | |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 92 | int |
| 93 | frame_callback_wait_nofail(struct client *client, int *done) |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 94 | { |
| 95 | while (!*done) { |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 96 | if (wl_display_dispatch(client->wl_display) < 0) |
| 97 | return 0; |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 98 | } |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 99 | |
| 100 | return 1; |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 101 | } |
| 102 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 103 | void |
| 104 | move_client(struct client *client, int x, int y) |
| 105 | { |
| 106 | struct surface *surface = client->surface; |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 107 | int done; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 108 | |
| 109 | client->surface->x = x; |
| 110 | client->surface->y = y; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 111 | weston_test_move_surface(client->test->weston_test, surface->wl_surface, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 112 | surface->x, surface->y); |
Bryce Harrington | cb50ed2 | 2014-12-10 18:33:47 -0800 | [diff] [blame] | 113 | /* The attach here is necessary because commit() will call configure |
Giulio Camuffo | 82cb505 | 2013-02-28 18:44:54 +0100 | [diff] [blame] | 114 | * only on surfaces newly attached, and the one that sets the surface |
| 115 | * position is the configure. */ |
| 116 | wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 117 | wl_surface_damage(surface->wl_surface, 0, 0, surface->width, |
| 118 | surface->height); |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 119 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 120 | frame_callback_set(surface->wl_surface, &done); |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 121 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 122 | wl_surface_commit(surface->wl_surface); |
| 123 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 124 | frame_callback_wait(client, &done); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 127 | int |
| 128 | get_n_egl_buffers(struct client *client) |
| 129 | { |
| 130 | client->test->n_egl_buffers = -1; |
| 131 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 132 | weston_test_get_n_egl_buffers(client->test->weston_test); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 133 | wl_display_roundtrip(client->wl_display); |
| 134 | |
| 135 | return client->test->n_egl_buffers; |
| 136 | } |
| 137 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 138 | static void |
| 139 | pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, |
| 140 | uint32_t serial, struct wl_surface *wl_surface, |
| 141 | wl_fixed_t x, wl_fixed_t y) |
| 142 | { |
| 143 | struct pointer *pointer = data; |
| 144 | |
| 145 | pointer->focus = wl_surface_get_user_data(wl_surface); |
| 146 | pointer->x = wl_fixed_to_int(x); |
| 147 | pointer->y = wl_fixed_to_int(y); |
| 148 | |
| 149 | fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n", |
| 150 | pointer->x, pointer->y, pointer->focus); |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, |
| 155 | uint32_t serial, struct wl_surface *wl_surface) |
| 156 | { |
| 157 | struct pointer *pointer = data; |
| 158 | |
| 159 | pointer->focus = NULL; |
| 160 | |
| 161 | fprintf(stderr, "test-client: got pointer leave, surface %p\n", |
| 162 | wl_surface_get_user_data(wl_surface)); |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, |
| 167 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 168 | { |
| 169 | struct pointer *pointer = data; |
| 170 | |
| 171 | pointer->x = wl_fixed_to_int(x); |
| 172 | pointer->y = wl_fixed_to_int(y); |
| 173 | |
| 174 | fprintf(stderr, "test-client: got pointer motion %d %d\n", |
| 175 | pointer->x, pointer->y); |
| 176 | } |
| 177 | |
| 178 | static void |
| 179 | pointer_handle_button(void *data, struct wl_pointer *wl_pointer, |
| 180 | uint32_t serial, uint32_t time, uint32_t button, |
| 181 | uint32_t state) |
| 182 | { |
| 183 | struct pointer *pointer = data; |
| 184 | |
| 185 | pointer->button = button; |
| 186 | pointer->state = state; |
| 187 | |
| 188 | fprintf(stderr, "test-client: got pointer button %u %u\n", |
| 189 | button, state); |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, |
| 194 | uint32_t time, uint32_t axis, wl_fixed_t value) |
| 195 | { |
Kristian Høgsberg | 4c8ce20 | 2013-10-09 14:23:00 -0700 | [diff] [blame] | 196 | fprintf(stderr, "test-client: got pointer axis %u %f\n", |
| 197 | axis, wl_fixed_to_double(value)); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static const struct wl_pointer_listener pointer_listener = { |
| 201 | pointer_handle_enter, |
| 202 | pointer_handle_leave, |
| 203 | pointer_handle_motion, |
| 204 | pointer_handle_button, |
| 205 | pointer_handle_axis, |
| 206 | }; |
| 207 | |
| 208 | static void |
| 209 | keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard, |
| 210 | uint32_t format, int fd, uint32_t size) |
| 211 | { |
| 212 | close(fd); |
| 213 | |
| 214 | fprintf(stderr, "test-client: got keyboard keymap\n"); |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, |
| 219 | uint32_t serial, struct wl_surface *wl_surface, |
| 220 | struct wl_array *keys) |
| 221 | { |
| 222 | struct keyboard *keyboard = data; |
| 223 | |
| 224 | keyboard->focus = wl_surface_get_user_data(wl_surface); |
| 225 | |
| 226 | fprintf(stderr, "test-client: got keyboard enter, surface %p\n", |
| 227 | keyboard->focus); |
| 228 | } |
| 229 | |
| 230 | static void |
| 231 | keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard, |
| 232 | uint32_t serial, struct wl_surface *wl_surface) |
| 233 | { |
| 234 | struct keyboard *keyboard = data; |
| 235 | |
| 236 | keyboard->focus = NULL; |
| 237 | |
| 238 | fprintf(stderr, "test-client: got keyboard leave, surface %p\n", |
| 239 | wl_surface_get_user_data(wl_surface)); |
| 240 | } |
| 241 | |
| 242 | static void |
| 243 | keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, |
| 244 | uint32_t serial, uint32_t time, uint32_t key, |
| 245 | uint32_t state) |
| 246 | { |
| 247 | struct keyboard *keyboard = data; |
| 248 | |
| 249 | keyboard->key = key; |
| 250 | keyboard->state = state; |
| 251 | |
| 252 | fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state); |
| 253 | } |
| 254 | |
| 255 | static void |
| 256 | keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, |
| 257 | uint32_t serial, uint32_t mods_depressed, |
| 258 | uint32_t mods_latched, uint32_t mods_locked, |
| 259 | uint32_t group) |
| 260 | { |
| 261 | struct keyboard *keyboard = data; |
| 262 | |
| 263 | keyboard->mods_depressed = mods_depressed; |
| 264 | keyboard->mods_latched = mods_latched; |
| 265 | keyboard->mods_locked = mods_locked; |
| 266 | keyboard->group = group; |
| 267 | |
| 268 | fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n", |
| 269 | mods_depressed, mods_latched, mods_locked, group); |
| 270 | } |
| 271 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 272 | static void |
| 273 | keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard, |
| 274 | int32_t rate, int32_t delay) |
| 275 | { |
| 276 | struct keyboard *keyboard = data; |
| 277 | |
| 278 | keyboard->repeat_info.rate = rate; |
| 279 | keyboard->repeat_info.delay = delay; |
| 280 | |
| 281 | fprintf(stderr, "test-client: got keyboard repeat_info %d %d\n", |
| 282 | rate, delay); |
| 283 | } |
| 284 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 285 | static const struct wl_keyboard_listener keyboard_listener = { |
| 286 | keyboard_handle_keymap, |
| 287 | keyboard_handle_enter, |
| 288 | keyboard_handle_leave, |
| 289 | keyboard_handle_key, |
| 290 | keyboard_handle_modifiers, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 291 | keyboard_handle_repeat_info, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | static void |
Marek Chalupa | 8a5523c | 2015-03-30 09:20:07 -0400 | [diff] [blame] | 295 | touch_handle_down(void *data, struct wl_touch *wl_touch, |
| 296 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 297 | int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 298 | { |
| 299 | struct touch *touch = data; |
| 300 | |
| 301 | touch->down_x = wl_fixed_to_int(x_w); |
| 302 | touch->down_y = wl_fixed_to_int(y_w); |
| 303 | touch->id = id; |
| 304 | |
| 305 | fprintf(stderr, "test-client: got touch down %d %d, surf: %p, id: %d\n", |
| 306 | touch->down_x, touch->down_y, surface, id); |
| 307 | } |
| 308 | |
| 309 | static void |
| 310 | touch_handle_up(void *data, struct wl_touch *wl_touch, |
| 311 | uint32_t serial, uint32_t time, int32_t id) |
| 312 | { |
| 313 | struct touch *touch = data; |
| 314 | touch->up_id = id; |
| 315 | |
| 316 | fprintf(stderr, "test-client: got touch up, id: %d\n", id); |
| 317 | } |
| 318 | |
| 319 | static void |
| 320 | touch_handle_motion(void *data, struct wl_touch *wl_touch, |
| 321 | uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 322 | { |
| 323 | struct touch *touch = data; |
| 324 | touch->x = wl_fixed_to_int(x_w); |
| 325 | touch->y = wl_fixed_to_int(y_w); |
| 326 | |
| 327 | fprintf(stderr, "test-client: got touch motion, %d %d, id: %d\n", |
| 328 | touch->x, touch->y, id); |
| 329 | } |
| 330 | |
| 331 | static void |
| 332 | touch_handle_frame(void *data, struct wl_touch *wl_touch) |
| 333 | { |
| 334 | struct touch *touch = data; |
| 335 | |
| 336 | ++touch->frame_no; |
| 337 | |
| 338 | fprintf(stderr, "test-client: got touch frame (%d)\n", touch->frame_no); |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
| 343 | { |
| 344 | struct touch *touch = data; |
| 345 | |
| 346 | ++touch->cancel_no; |
| 347 | |
| 348 | fprintf(stderr, "test-client: got touch cancel (%d)\n", touch->cancel_no); |
| 349 | } |
| 350 | |
| 351 | static const struct wl_touch_listener touch_listener = { |
| 352 | touch_handle_down, |
| 353 | touch_handle_up, |
| 354 | touch_handle_motion, |
| 355 | touch_handle_frame, |
| 356 | touch_handle_cancel, |
| 357 | }; |
| 358 | |
| 359 | static void |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 360 | surface_enter(void *data, |
| 361 | struct wl_surface *wl_surface, struct wl_output *output) |
| 362 | { |
| 363 | struct surface *surface = data; |
| 364 | |
| 365 | surface->output = wl_output_get_user_data(output); |
| 366 | |
| 367 | fprintf(stderr, "test-client: got surface enter output %p\n", |
| 368 | surface->output); |
| 369 | } |
| 370 | |
| 371 | static void |
| 372 | surface_leave(void *data, |
| 373 | struct wl_surface *wl_surface, struct wl_output *output) |
| 374 | { |
| 375 | struct surface *surface = data; |
| 376 | |
| 377 | surface->output = NULL; |
| 378 | |
| 379 | fprintf(stderr, "test-client: got surface leave output %p\n", |
| 380 | wl_output_get_user_data(output)); |
| 381 | } |
| 382 | |
| 383 | static const struct wl_surface_listener surface_listener = { |
| 384 | surface_enter, |
| 385 | surface_leave |
| 386 | }; |
| 387 | |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 388 | struct wl_buffer * |
| 389 | 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] | 390 | { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 391 | struct wl_shm *shm = client->wl_shm; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 392 | int stride = width * 4; |
| 393 | int size = stride * height; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 394 | struct wl_shm_pool *pool; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 395 | struct wl_buffer *buffer; |
| 396 | int fd; |
| 397 | void *data; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 398 | |
| 399 | fd = os_create_anonymous_file(size); |
| 400 | assert(fd >= 0); |
| 401 | |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 402 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 403 | if (data == MAP_FAILED) { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 404 | close(fd); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 405 | assert(data != MAP_FAILED); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | pool = wl_shm_create_pool(shm, fd, size); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 409 | buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, |
| 410 | WL_SHM_FORMAT_ARGB8888); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 411 | wl_shm_pool_destroy(pool); |
| 412 | |
| 413 | close(fd); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 414 | |
| 415 | if (pixels) |
| 416 | *pixels = data; |
| 417 | |
| 418 | return buffer; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static void |
| 422 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 423 | { |
| 424 | struct client *client = data; |
| 425 | |
| 426 | if (format == WL_SHM_FORMAT_ARGB8888) |
| 427 | client->has_argb = 1; |
| 428 | } |
| 429 | |
| 430 | struct wl_shm_listener shm_listener = { |
| 431 | shm_format |
| 432 | }; |
| 433 | |
| 434 | static void |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 435 | test_handle_pointer_position(void *data, struct weston_test *weston_test, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 436 | wl_fixed_t x, wl_fixed_t y) |
| 437 | { |
| 438 | struct test *test = data; |
| 439 | test->pointer_x = wl_fixed_to_int(x); |
| 440 | test->pointer_y = wl_fixed_to_int(y); |
| 441 | |
| 442 | fprintf(stderr, "test-client: got global pointer %d %d\n", |
| 443 | test->pointer_x, test->pointer_y); |
| 444 | } |
| 445 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 446 | static void |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 447 | 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] | 448 | { |
| 449 | struct test *test = data; |
| 450 | |
| 451 | test->n_egl_buffers = n; |
| 452 | } |
| 453 | |
Bryce Harrington | 692275f | 2015-04-23 16:33:49 -0700 | [diff] [blame] | 454 | static void |
| 455 | test_handle_capture_screenshot_done(void *data, struct weston_test *weston_test) |
| 456 | { |
| 457 | struct test *test = data; |
| 458 | |
| 459 | printf("Screenshot has been captured\n"); |
| 460 | test->buffer_copy_done = 1; |
| 461 | } |
| 462 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 463 | static const struct weston_test_listener test_listener = { |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 464 | test_handle_pointer_position, |
| 465 | test_handle_n_egl_buffers, |
Bryce Harrington | 692275f | 2015-04-23 16:33:49 -0700 | [diff] [blame] | 466 | test_handle_capture_screenshot_done, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 467 | }; |
| 468 | |
| 469 | static void |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 470 | input_update_devices(struct input *input) |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 471 | { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 472 | struct pointer *pointer; |
| 473 | struct keyboard *keyboard; |
Marek Chalupa | 8a5523c | 2015-03-30 09:20:07 -0400 | [diff] [blame] | 474 | struct touch *touch; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 475 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 476 | struct wl_seat *seat = input->wl_seat; |
| 477 | enum wl_seat_capability caps = input->caps; |
| 478 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 479 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 480 | pointer = xzalloc(sizeof *pointer); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 481 | pointer->wl_pointer = wl_seat_get_pointer(seat); |
| 482 | wl_pointer_set_user_data(pointer->wl_pointer, pointer); |
| 483 | wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener, |
| 484 | pointer); |
| 485 | input->pointer = pointer; |
| 486 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 487 | wl_pointer_destroy(input->pointer->wl_pointer); |
| 488 | free(input->pointer); |
| 489 | input->pointer = NULL; |
| 490 | } |
| 491 | |
| 492 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 493 | keyboard = xzalloc(sizeof *keyboard); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 494 | keyboard->wl_keyboard = wl_seat_get_keyboard(seat); |
| 495 | wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard); |
| 496 | wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener, |
| 497 | keyboard); |
| 498 | input->keyboard = keyboard; |
| 499 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 500 | wl_keyboard_destroy(input->keyboard->wl_keyboard); |
| 501 | free(input->keyboard); |
| 502 | input->keyboard = NULL; |
| 503 | } |
Marek Chalupa | 8a5523c | 2015-03-30 09:20:07 -0400 | [diff] [blame] | 504 | |
| 505 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) { |
| 506 | touch = xzalloc(sizeof *touch); |
| 507 | touch->wl_touch = wl_seat_get_touch(seat); |
| 508 | wl_touch_set_user_data(touch->wl_touch, touch); |
| 509 | wl_touch_add_listener(touch->wl_touch, &touch_listener, |
| 510 | touch); |
| 511 | input->touch = touch; |
| 512 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) { |
| 513 | wl_touch_destroy(input->touch->wl_touch); |
| 514 | free(input->touch); |
| 515 | input->touch = NULL; |
| 516 | } |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 519 | static void |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 520 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 521 | enum wl_seat_capability caps) |
| 522 | { |
| 523 | struct input *input = data; |
| 524 | |
| 525 | input->caps = caps; |
| 526 | |
| 527 | /* we will create/update the devices only with the right (test) seat. |
| 528 | * If we haven't discovered which seat is the test seat, just |
| 529 | * store capabilities and bail out */ |
Dawid Gajownik | 74a635b | 2015-08-06 17:12:19 -0300 | [diff] [blame] | 530 | if (input->seat_name && strcmp(input->seat_name, "test-seat") == 0) |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 531 | input_update_devices(input); |
| 532 | |
| 533 | fprintf(stderr, "test-client: got seat %p capabilities: %x\n", |
| 534 | input, caps); |
| 535 | } |
| 536 | |
| 537 | static void |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 538 | seat_handle_name(void *data, struct wl_seat *seat, const char *name) |
| 539 | { |
| 540 | struct input *input = data; |
| 541 | |
| 542 | input->seat_name = strdup(name); |
| 543 | assert(input->seat_name && "No memory"); |
| 544 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 545 | fprintf(stderr, "test-client: got seat %p name: \'%s\'\n", |
| 546 | input, name); |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 547 | } |
| 548 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 549 | static const struct wl_seat_listener seat_listener = { |
| 550 | seat_handle_capabilities, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 551 | seat_handle_name, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | static void |
| 555 | output_handle_geometry(void *data, |
| 556 | struct wl_output *wl_output, |
| 557 | int x, int y, |
| 558 | int physical_width, |
| 559 | int physical_height, |
| 560 | int subpixel, |
| 561 | const char *make, |
| 562 | const char *model, |
| 563 | int32_t transform) |
| 564 | { |
| 565 | struct output *output = data; |
| 566 | |
| 567 | output->x = x; |
| 568 | output->y = y; |
| 569 | } |
| 570 | |
| 571 | static void |
| 572 | output_handle_mode(void *data, |
| 573 | struct wl_output *wl_output, |
| 574 | uint32_t flags, |
| 575 | int width, |
| 576 | int height, |
| 577 | int refresh) |
| 578 | { |
| 579 | struct output *output = data; |
| 580 | |
| 581 | if (flags & WL_OUTPUT_MODE_CURRENT) { |
| 582 | output->width = width; |
| 583 | output->height = height; |
| 584 | } |
| 585 | } |
| 586 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 587 | static void |
| 588 | output_handle_scale(void *data, |
| 589 | struct wl_output *wl_output, |
| 590 | int scale) |
| 591 | { |
| 592 | struct output *output = data; |
| 593 | |
| 594 | output->scale = scale; |
| 595 | } |
| 596 | |
| 597 | static void |
| 598 | output_handle_done(void *data, |
| 599 | struct wl_output *wl_output) |
| 600 | { |
| 601 | struct output *output = data; |
| 602 | |
| 603 | output->initialized = 1; |
| 604 | } |
| 605 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 606 | static const struct wl_output_listener output_listener = { |
| 607 | output_handle_geometry, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 608 | output_handle_mode, |
| 609 | output_handle_done, |
| 610 | output_handle_scale, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 611 | }; |
| 612 | |
| 613 | static void |
| 614 | handle_global(void *data, struct wl_registry *registry, |
| 615 | uint32_t id, const char *interface, uint32_t version) |
| 616 | { |
| 617 | struct client *client = data; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 618 | struct output *output; |
| 619 | struct test *test; |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 620 | struct global *global; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 621 | struct input *input; |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 622 | |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 623 | global = xzalloc(sizeof *global); |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 624 | global->name = id; |
| 625 | global->interface = strdup(interface); |
| 626 | assert(interface); |
| 627 | global->version = version; |
| 628 | wl_list_insert(client->global_list.prev, &global->link); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 629 | |
| 630 | if (strcmp(interface, "wl_compositor") == 0) { |
| 631 | client->wl_compositor = |
| 632 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 633 | &wl_compositor_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 634 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 635 | input = xzalloc(sizeof *input); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 636 | input->wl_seat = |
| 637 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 638 | &wl_seat_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 639 | wl_seat_add_listener(input->wl_seat, &seat_listener, input); |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 640 | wl_list_insert(&client->inputs, &input->link); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 641 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 642 | client->wl_shm = |
| 643 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 644 | &wl_shm_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 645 | wl_shm_add_listener(client->wl_shm, &shm_listener, client); |
| 646 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 647 | output = xzalloc(sizeof *output); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 648 | output->wl_output = |
| 649 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 650 | &wl_output_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 651 | wl_output_add_listener(output->wl_output, |
| 652 | &output_listener, output); |
| 653 | client->output = output; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 654 | } else if (strcmp(interface, "weston_test") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 655 | test = xzalloc(sizeof *test); |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 656 | test->weston_test = |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 657 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 658 | &weston_test_interface, version); |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 659 | weston_test_add_listener(test->weston_test, &test_listener, test); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 660 | client->test = test; |
Derek Foreman | 9bb1339 | 2015-01-23 12:12:36 -0600 | [diff] [blame] | 661 | } else if (strcmp(interface, "wl_drm") == 0) { |
| 662 | client->has_wl_drm = true; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | static const struct wl_registry_listener registry_listener = { |
| 667 | handle_global |
| 668 | }; |
| 669 | |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 670 | void |
| 671 | skip(const char *fmt, ...) |
| 672 | { |
| 673 | va_list argp; |
| 674 | |
| 675 | va_start(argp, fmt); |
| 676 | vfprintf(stderr, fmt, argp); |
| 677 | va_end(argp); |
| 678 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 679 | /* automake tests uses exit code 77. weston-test-runner will see |
| 680 | * this and use it, and then weston-test's sigchld handler (in the |
| 681 | * weston process) will use that as an exit status, which is what |
| 682 | * automake will see in the end. */ |
| 683 | exit(77); |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Marek Chalupa | 4d06d46 | 2014-07-16 11:27:06 +0200 | [diff] [blame] | 686 | void |
| 687 | expect_protocol_error(struct client *client, |
| 688 | const struct wl_interface *intf, |
| 689 | uint32_t code) |
| 690 | { |
| 691 | int err; |
| 692 | uint32_t errcode, failed = 0; |
| 693 | const struct wl_interface *interface; |
| 694 | unsigned int id; |
| 695 | |
| 696 | /* if the error has not come yet, make it happen */ |
| 697 | wl_display_roundtrip(client->wl_display); |
| 698 | |
| 699 | err = wl_display_get_error(client->wl_display); |
| 700 | |
| 701 | assert(err && "Expected protocol error but nothing came"); |
| 702 | assert(err == EPROTO && "Expected protocol error but got local error"); |
| 703 | |
| 704 | errcode = wl_display_get_protocol_error(client->wl_display, |
| 705 | &interface, &id); |
| 706 | |
| 707 | /* check error */ |
| 708 | if (errcode != code) { |
| 709 | fprintf(stderr, "Should get error code %d but got %d\n", |
| 710 | code, errcode); |
| 711 | failed = 1; |
| 712 | } |
| 713 | |
| 714 | /* this should be definitely set */ |
| 715 | assert(interface); |
| 716 | |
| 717 | if (strcmp(intf->name, interface->name) != 0) { |
| 718 | fprintf(stderr, "Should get interface '%s' but got '%s'\n", |
| 719 | intf->name, interface->name); |
| 720 | failed = 1; |
| 721 | } |
| 722 | |
| 723 | if (failed) { |
| 724 | fprintf(stderr, "Expected other protocol error\n"); |
| 725 | abort(); |
| 726 | } |
| 727 | |
| 728 | /* all OK */ |
| 729 | fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) " |
| 730 | "with code %d\n", interface->name, id, errcode); |
| 731 | } |
| 732 | |
Pekka Paalanen | 07921d7 | 2012-12-12 14:26:40 +0200 | [diff] [blame] | 733 | static void |
| 734 | log_handler(const char *fmt, va_list args) |
| 735 | { |
| 736 | fprintf(stderr, "libwayland: "); |
| 737 | vfprintf(stderr, fmt, args); |
| 738 | } |
| 739 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 740 | static void |
| 741 | input_destroy(struct input *inp) |
| 742 | { |
| 743 | wl_list_remove(&inp->link); |
| 744 | wl_seat_destroy(inp->wl_seat); |
| 745 | free(inp); |
| 746 | } |
| 747 | |
| 748 | /* find the test-seat and set it in client. |
| 749 | * Destroy other inputs */ |
| 750 | static void |
| 751 | client_set_input(struct client *cl) |
| 752 | { |
| 753 | struct input *inp, *inptmp; |
| 754 | wl_list_for_each_safe(inp, inptmp, &cl->inputs, link) { |
| 755 | assert(inp->seat_name && "BUG: input with no name"); |
| 756 | if (strcmp(inp->seat_name, "test-seat") == 0) { |
| 757 | cl->input = inp; |
| 758 | input_update_devices(inp); |
| 759 | } else { |
| 760 | input_destroy(inp); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /* we keep only one input */ |
| 765 | assert(wl_list_length(&cl->inputs) == 1); |
| 766 | } |
| 767 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 768 | struct client * |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 769 | create_client(void) |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 770 | { |
| 771 | struct client *client; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 772 | |
Pekka Paalanen | 07921d7 | 2012-12-12 14:26:40 +0200 | [diff] [blame] | 773 | wl_log_set_handler_client(log_handler); |
| 774 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 775 | /* connect to display */ |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 776 | client = xzalloc(sizeof *client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 777 | client->wl_display = wl_display_connect(NULL); |
| 778 | assert(client->wl_display); |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 779 | wl_list_init(&client->global_list); |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 780 | wl_list_init(&client->inputs); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 781 | |
| 782 | /* setup registry so we can bind to interfaces */ |
| 783 | client->wl_registry = wl_display_get_registry(client->wl_display); |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 784 | wl_registry_add_listener(client->wl_registry, ®istry_listener, |
| 785 | client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 786 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 787 | /* this roundtrip makes sure we have all globals and we bound to them */ |
| 788 | client_roundtrip(client); |
| 789 | /* this roundtrip makes sure we got all wl_shm.format and wl_seat.* |
| 790 | * events */ |
| 791 | client_roundtrip(client); |
| 792 | |
| 793 | /* find the right input for us */ |
| 794 | client_set_input(client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 795 | |
| 796 | /* must have WL_SHM_FORMAT_ARGB32 */ |
| 797 | assert(client->has_argb); |
| 798 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 799 | /* must have weston_test interface */ |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 800 | assert(client->test); |
| 801 | |
| 802 | /* must have an output */ |
| 803 | assert(client->output); |
| 804 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 805 | /* the output must be initialized */ |
| 806 | assert(client->output->initialized == 1); |
| 807 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 808 | /* must have seat set */ |
| 809 | assert(client->input); |
| 810 | |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 811 | return client; |
| 812 | } |
| 813 | |
| 814 | struct client * |
Pekka Paalanen | 4ac06ff | 2015-03-26 12:56:10 +0200 | [diff] [blame] | 815 | 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] | 816 | { |
| 817 | struct client *client; |
| 818 | struct surface *surface; |
| 819 | |
| 820 | client = create_client(); |
| 821 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 822 | /* initialize the client surface */ |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 823 | surface = xzalloc(sizeof *surface); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 824 | surface->wl_surface = |
| 825 | wl_compositor_create_surface(client->wl_compositor); |
| 826 | assert(surface->wl_surface); |
| 827 | |
| 828 | wl_surface_add_listener(surface->wl_surface, &surface_listener, |
| 829 | surface); |
| 830 | |
| 831 | client->surface = surface; |
| 832 | wl_surface_set_user_data(surface->wl_surface, surface); |
| 833 | |
| 834 | surface->width = width; |
| 835 | surface->height = height; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 836 | surface->wl_buffer = create_shm_buffer(client, width, height, |
| 837 | &surface->data); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 838 | |
| 839 | memset(surface->data, 64, width * height * 4); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 840 | |
| 841 | move_client(client, x, y); |
| 842 | |
| 843 | return client; |
| 844 | } |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 845 | |
| 846 | static const char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame] | 847 | output_path(void) |
| 848 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 849 | char *path = getenv("WESTON_TEST_OUTPUT_PATH"); |
| 850 | |
| 851 | if (!path) |
| 852 | return "."; |
| 853 | return path; |
| 854 | } |
| 855 | |
| 856 | char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame] | 857 | screenshot_output_filename(const char *basename, uint32_t seq) |
| 858 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 859 | char *filename; |
| 860 | |
| 861 | if (asprintf(&filename, "%s/%s-%02d.png", |
| 862 | output_path(), basename, seq) < 0) |
| 863 | return NULL; |
| 864 | return filename; |
| 865 | } |
| 866 | |
| 867 | static const char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame] | 868 | reference_path(void) |
| 869 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 870 | char *path = getenv("WESTON_TEST_REFERENCE_PATH"); |
| 871 | |
| 872 | if (!path) |
| 873 | return "./tests/reference"; |
| 874 | return path; |
| 875 | } |
| 876 | |
| 877 | char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame] | 878 | screenshot_reference_filename(const char *basename, uint32_t seq) |
| 879 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 880 | char *filename; |
| 881 | |
| 882 | if (asprintf(&filename, "%s/%s-%02d.png", |
| 883 | reference_path(), basename, seq) < 0) |
| 884 | return NULL; |
| 885 | return filename; |
| 886 | } |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 887 | |
| 888 | /** |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 889 | * check_surfaces_geometry() - verifies two surfaces are same size |
| 890 | * |
| 891 | * @returns true if surfaces have the same width and height, or false |
| 892 | * if not, or if there is no actual data. |
| 893 | */ |
| 894 | bool |
| 895 | check_surfaces_geometry(const struct surface *a, const struct surface *b) |
| 896 | { |
| 897 | if (a == NULL || b == NULL) { |
| 898 | printf("Undefined surfaces\n"); |
| 899 | return false; |
| 900 | } |
| 901 | else if (a->data == NULL || b->data == NULL) { |
| 902 | printf("Undefined data\n"); |
| 903 | return false; |
| 904 | } |
| 905 | else if (a->width != b->width || a->height != b->height) { |
| 906 | printf("Mismatched dimensions: %d,%d != %d,%d\n", |
| 907 | a->width, a->height, b->width, b->height); |
| 908 | return false; |
| 909 | } |
| 910 | return true; |
| 911 | } |
| 912 | |
| 913 | /** |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 914 | * check_surfaces_equal() - tests if two surfaces are pixel-identical |
| 915 | * |
| 916 | * Returns true if surface buffers have all the same byte values, |
| 917 | * false if the surfaces don't match or can't be compared due to |
| 918 | * different dimensions. |
| 919 | */ |
| 920 | bool |
| 921 | check_surfaces_equal(const struct surface *a, const struct surface *b) |
| 922 | { |
| 923 | int bpp = 4; /* Assumes ARGB */ |
| 924 | |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 925 | if (!check_surfaces_geometry(a, b)) |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 926 | return false; |
| 927 | |
| 928 | return (memcmp(a->data, b->data, bpp * a->width * a->height) == 0); |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * check_surfaces_match_in_clip() - tests if a given region within two |
| 933 | * surfaces are pixel-identical. |
| 934 | * |
| 935 | * Returns true if the two surfaces have the same byte values within the |
| 936 | * given clipping region, or false if they don't match or the surfaces |
| 937 | * can't be compared. |
| 938 | */ |
| 939 | bool |
| 940 | check_surfaces_match_in_clip(const struct surface *a, const struct surface *b, const struct rectangle *clip_rect) |
| 941 | { |
| 942 | int i, j; |
| 943 | int x0, y0, x1, y1; |
| 944 | void *p, *q; |
| 945 | int bpp = 4; /* Assumes ARGB */ |
| 946 | |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 947 | if (!check_surfaces_geometry(a, b) || clip_rect == NULL) |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 948 | return false; |
| 949 | |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 950 | if (clip_rect->x > a->width || clip_rect->y > a->height) { |
| 951 | printf("Clip outside image boundaries\n"); |
| 952 | return true; |
| 953 | } |
| 954 | |
| 955 | x0 = max(0, clip_rect->x); |
| 956 | y0 = max(0, clip_rect->y); |
| 957 | x1 = min(a->width, clip_rect->x + clip_rect->width); |
| 958 | y1 = min(a->height, clip_rect->y + clip_rect->height); |
| 959 | |
| 960 | if (x0 == x1 || y0 == y1) { |
| 961 | printf("Degenerate comparison\n"); |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | printf("Bytewise comparison inside clip\n"); |
| 966 | for (i=y0; i<y1; i++) { |
| 967 | p = a->data + i * a->width * bpp + x0 * bpp; |
| 968 | q = b->data + i * b->width * bpp + x0 * bpp; |
| 969 | if (memcmp(p, q, (x1-x0)*bpp) != 0) { |
| 970 | /* Dump the bad row */ |
| 971 | printf("Mismatched image on row %d\n", i); |
| 972 | for (j=0; j<(x1-x0)*bpp; j++) { |
| 973 | char a_char = *((char*)(p+j*bpp)); |
| 974 | char b_char = *((char*)(q+j*bpp)); |
| 975 | printf("%d,%d: %8x %8x %s\n", i, j, a_char, b_char, |
| 976 | (a_char != b_char)? " <---": ""); |
| 977 | } |
| 978 | return false; |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | return true; |
| 983 | } |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame^] | 984 | |
| 985 | /** write_surface_as_png() |
| 986 | * |
| 987 | * Writes out a given weston test surface to disk as a PNG image |
| 988 | * using the provided filename (with path). |
| 989 | * |
| 990 | * @returns true if successfully saved file; false otherwise. |
| 991 | */ |
| 992 | bool |
| 993 | write_surface_as_png(const struct surface *weston_surface, const char *fname) |
| 994 | { |
| 995 | cairo_surface_t *cairo_surface; |
| 996 | cairo_status_t status; |
| 997 | int bpp = 4; /* Assume ARGB */ |
| 998 | int stride = bpp * weston_surface->width; |
| 999 | |
| 1000 | cairo_surface = cairo_image_surface_create_for_data(weston_surface->data, |
| 1001 | CAIRO_FORMAT_ARGB32, |
| 1002 | weston_surface->width, |
| 1003 | weston_surface->height, |
| 1004 | stride); |
| 1005 | printf("Writing PNG to disk\n"); |
| 1006 | status = cairo_surface_write_to_png(cairo_surface, fname); |
| 1007 | if (status != CAIRO_STATUS_SUCCESS) { |
| 1008 | printf("Failed to save screenshot: %s\n", |
| 1009 | cairo_status_to_string(status)); |
| 1010 | return false; |
| 1011 | } |
| 1012 | cairo_surface_destroy(cairo_surface); |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
| 1016 | /** load_surface_from_png() |
| 1017 | * |
| 1018 | * Reads a PNG image from disk using the given filename (and path) |
| 1019 | * and returns as a freshly allocated weston test surface. |
| 1020 | * |
| 1021 | * @returns weston test surface with image, which should be free'd |
| 1022 | * when no longer used; or, NULL in case of error. |
| 1023 | */ |
| 1024 | struct surface * |
| 1025 | load_surface_from_png(const char *fname) |
| 1026 | { |
| 1027 | struct surface *reference; |
| 1028 | cairo_surface_t *reference_cairo_surface; |
| 1029 | cairo_status_t status; |
| 1030 | size_t source_data_size; |
| 1031 | int bpp; |
| 1032 | int stride; |
| 1033 | |
| 1034 | reference_cairo_surface = cairo_image_surface_create_from_png(fname); |
| 1035 | status = cairo_surface_status(reference_cairo_surface); |
| 1036 | if (status != CAIRO_STATUS_SUCCESS) { |
| 1037 | printf("Could not open %s: %s\n", fname, cairo_status_to_string(status)); |
| 1038 | cairo_surface_destroy(reference_cairo_surface); |
| 1039 | return NULL; |
| 1040 | } |
| 1041 | |
| 1042 | /* Disguise the cairo surface in a weston test surface */ |
| 1043 | reference = zalloc(sizeof *reference); |
| 1044 | if (reference == NULL) { |
| 1045 | perror("zalloc reference"); |
| 1046 | cairo_surface_destroy(reference_cairo_surface); |
| 1047 | return NULL; |
| 1048 | } |
| 1049 | reference->width = cairo_image_surface_get_width(reference_cairo_surface); |
| 1050 | reference->height = cairo_image_surface_get_height(reference_cairo_surface); |
| 1051 | stride = cairo_image_surface_get_stride(reference_cairo_surface); |
| 1052 | source_data_size = stride * reference->height; |
| 1053 | |
| 1054 | /* Check that the file's stride matches our assumption */ |
| 1055 | bpp = 4; |
| 1056 | if (stride != bpp * reference->width) { |
| 1057 | printf("Mismatched stride for screenshot reference image %s\n", fname); |
| 1058 | cairo_surface_destroy(reference_cairo_surface); |
| 1059 | free(reference); |
| 1060 | return NULL; |
| 1061 | } |
| 1062 | |
| 1063 | /* Allocate new buffer for our weston reference, and copy the data from |
| 1064 | the cairo surface so we can destroy it */ |
| 1065 | reference->data = zalloc(source_data_size); |
| 1066 | if (reference->data == NULL) { |
| 1067 | perror("zalloc reference data"); |
| 1068 | cairo_surface_destroy(reference_cairo_surface); |
| 1069 | free(reference); |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | memcpy(reference->data, |
| 1073 | cairo_image_surface_get_data(reference_cairo_surface), |
| 1074 | source_data_size); |
| 1075 | |
| 1076 | cairo_surface_destroy(reference_cairo_surface); |
| 1077 | return reference; |
| 1078 | } |
| 1079 | |
| 1080 | /** create_screenshot_surface() |
| 1081 | * |
| 1082 | * Allocates and initializes a weston test surface for use in |
| 1083 | * storing a screenshot of the client's output. Establishes a |
| 1084 | * shm backed wl_buffer for retrieving screenshot image data |
| 1085 | * from the server, sized to match the client's output display. |
| 1086 | * |
| 1087 | * @returns stack allocated surface image, which should be |
| 1088 | * free'd when done using it. |
| 1089 | */ |
| 1090 | struct surface * |
| 1091 | create_screenshot_surface(struct client *client) |
| 1092 | { |
| 1093 | struct surface *screenshot; |
| 1094 | screenshot = zalloc(sizeof *screenshot); |
| 1095 | if (screenshot == NULL) |
| 1096 | return NULL; |
| 1097 | screenshot->wl_buffer = create_shm_buffer(client, |
| 1098 | client->output->width, |
| 1099 | client->output->height, |
| 1100 | &screenshot->data); |
| 1101 | screenshot->height = client->output->height; |
| 1102 | screenshot->width = client->output->width; |
| 1103 | |
| 1104 | return screenshot; |
| 1105 | } |
| 1106 | |
| 1107 | /** capture_screenshot_of_output() |
| 1108 | * |
| 1109 | * Requests a screenshot from the server of the output that the |
| 1110 | * client appears on. The image data returned from the server |
| 1111 | * can be accessed from the screenshot surface's data member. |
| 1112 | * |
| 1113 | * @returns a new surface object, which should be free'd when no |
| 1114 | * longer needed. |
| 1115 | */ |
| 1116 | struct surface * |
| 1117 | capture_screenshot_of_output(struct client *client) |
| 1118 | { |
| 1119 | struct surface *screenshot; |
| 1120 | |
| 1121 | /* Create a surface to hold the screenshot */ |
| 1122 | screenshot = create_screenshot_surface(client); |
| 1123 | |
| 1124 | client->test->buffer_copy_done = 0; |
| 1125 | weston_test_capture_screenshot(client->test->weston_test, |
| 1126 | client->output->wl_output, |
| 1127 | screenshot->wl_buffer); |
| 1128 | while (client->test->buffer_copy_done == 0) |
| 1129 | if (wl_display_dispatch(client->wl_display) < 0) |
| 1130 | break; |
| 1131 | |
| 1132 | /* FIXME: Document somewhere the orientation the screenshot is taken |
| 1133 | * and how the clip coords are interpreted, in case of scaling/transform. |
| 1134 | * If we're using read_pixels() just make sure it is documented somewhere. |
| 1135 | * Protocol docs in the XML, comparison function docs in Doxygen style. |
| 1136 | */ |
| 1137 | |
| 1138 | return screenshot; |
| 1139 | } |