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