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