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