U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -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 | 65e7e7a | 2012-12-07 13:50:29 -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 | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 28 | #include <stdint.h> |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 29 | #include <stdlib.h> |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 30 | #include <assert.h> |
| 31 | #include <signal.h> |
| 32 | #include <unistd.h> |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 33 | #include <string.h> |
Bryce Harrington | a768026 | 2014-11-19 17:18:34 -0800 | [diff] [blame] | 34 | |
Pekka Paalanen | b5e3ea2 | 2016-06-03 17:12:10 +0300 | [diff] [blame] | 35 | #include "compositor.h" |
Pekka Paalanen | 58f98c9 | 2016-06-03 16:45:21 +0300 | [diff] [blame] | 36 | #include "compositor/weston.h" |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 37 | #include "weston-test-server-protocol.h" |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 38 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 39 | #ifdef ENABLE_EGL |
| 40 | #include <EGL/egl.h> |
| 41 | #include <EGL/eglext.h> |
Pekka Paalanen | b5e3ea2 | 2016-06-03 17:12:10 +0300 | [diff] [blame] | 42 | #include "weston-egl-ext.h" |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 43 | #endif /* ENABLE_EGL */ |
| 44 | |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 45 | #include "shared/helpers.h" |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 46 | #include "shared/timespec-util.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 47 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 48 | struct weston_test { |
| 49 | struct weston_compositor *compositor; |
| 50 | struct weston_layer layer; |
| 51 | struct weston_process process; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 52 | struct weston_seat seat; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct weston_test_surface { |
| 56 | struct weston_surface *surface; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 57 | struct weston_view *view; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 58 | int32_t x, y; |
| 59 | struct weston_test *test; |
| 60 | }; |
| 61 | |
| 62 | static void |
| 63 | test_client_sigchld(struct weston_process *process, int status) |
| 64 | { |
| 65 | struct weston_test *test = |
| 66 | container_of(process, struct weston_test, process); |
| 67 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 68 | /* Chain up from weston-test-runner's exit code so that automake |
| 69 | * knows the exit status and can report e.g. skipped tests. */ |
| 70 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 71 | exit(WEXITSTATUS(status)); |
| 72 | |
| 73 | /* In case the child aborted or segfaulted... */ |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 74 | assert(status == 0); |
| 75 | |
| 76 | wl_display_terminate(test->compositor->wl_display); |
| 77 | } |
| 78 | |
| 79 | static struct weston_seat * |
| 80 | get_seat(struct weston_test *test) |
| 81 | { |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 82 | return &test->seat; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void |
| 86 | notify_pointer_position(struct weston_test *test, struct wl_resource *resource) |
| 87 | { |
| 88 | struct weston_seat *seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 89 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 90 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 91 | weston_test_send_pointer_position(resource, pointer->x, pointer->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 95 | test_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 96 | { |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 97 | struct weston_test_surface *test_surface = surface->committed_private; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 98 | struct weston_test *test = test_surface->test; |
| 99 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 100 | if (wl_list_empty(&test_surface->view->layer_link.link)) |
| 101 | weston_layer_entry_insert(&test->layer.view_list, |
| 102 | &test_surface->view->layer_link); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 103 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 104 | weston_view_set_position(test_surface->view, |
| 105 | test_surface->x, test_surface->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 106 | |
Kristian Høgsberg | ace0a39 | 2013-11-13 21:55:57 -0800 | [diff] [blame] | 107 | weston_view_update_transform(test_surface->view); |
Armin Krezović | d0cf441 | 2016-06-30 06:04:32 +0200 | [diff] [blame] | 108 | |
| 109 | test_surface->surface->is_mapped = true; |
| 110 | test_surface->view->is_mapped = true; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void |
| 114 | move_surface(struct wl_client *client, struct wl_resource *resource, |
| 115 | struct wl_resource *surface_resource, |
| 116 | int32_t x, int32_t y) |
| 117 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 118 | struct weston_surface *surface = |
| 119 | wl_resource_get_user_data(surface_resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 120 | struct weston_test_surface *test_surface; |
| 121 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 122 | test_surface = surface->committed_private; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 123 | if (!test_surface) { |
| 124 | test_surface = malloc(sizeof *test_surface); |
| 125 | if (!test_surface) { |
| 126 | wl_resource_post_no_memory(resource); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | test_surface->view = weston_view_create(surface); |
| 131 | if (!test_surface->view) { |
| 132 | wl_resource_post_no_memory(resource); |
| 133 | free(test_surface); |
| 134 | return; |
| 135 | } |
| 136 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 137 | surface->committed_private = test_surface; |
| 138 | surface->committed = test_surface_committed; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | test_surface->surface = surface; |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 142 | test_surface->test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 143 | test_surface->x = x; |
| 144 | test_surface->y = y; |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | move_pointer(struct wl_client *client, struct wl_resource *resource, |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 149 | uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 150 | int32_t x, int32_t y) |
| 151 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 152 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 153 | struct weston_seat *seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 154 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 155 | struct weston_pointer_motion_event event = { 0 }; |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 156 | struct timespec time; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 157 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 158 | event = (struct weston_pointer_motion_event) { |
| 159 | .mask = WESTON_POINTER_MOTION_REL, |
| 160 | .dx = wl_fixed_to_double(wl_fixed_from_int(x) - pointer->x), |
| 161 | .dy = wl_fixed_to_double(wl_fixed_from_int(y) - pointer->y), |
| 162 | }; |
| 163 | |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 164 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 165 | |
| 166 | notify_motion(seat, &time, &event); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 167 | |
| 168 | notify_pointer_position(test, resource); |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | send_button(struct wl_client *client, struct wl_resource *resource, |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 173 | uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 174 | int32_t button, uint32_t state) |
| 175 | { |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 176 | struct timespec time; |
| 177 | |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 178 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 179 | struct weston_seat *seat = get_seat(test); |
| 180 | |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 181 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 182 | |
| 183 | notify_button(seat, &time, button, state); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void |
| 187 | activate_surface(struct wl_client *client, struct wl_resource *resource, |
| 188 | struct wl_resource *surface_resource) |
| 189 | { |
| 190 | struct weston_surface *surface = surface_resource ? |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 191 | wl_resource_get_user_data(surface_resource) : NULL; |
| 192 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 193 | struct weston_seat *seat; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 194 | struct weston_keyboard *keyboard; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 195 | |
| 196 | seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 197 | keyboard = weston_seat_get_keyboard(seat); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 198 | if (surface) { |
Bryce Harrington | 260c2ff | 2016-06-29 19:04:06 -0700 | [diff] [blame] | 199 | weston_seat_set_keyboard_focus(seat, surface); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 200 | notify_keyboard_focus_in(seat, &keyboard->keys, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 201 | STATE_UPDATE_AUTOMATIC); |
| 202 | } |
| 203 | else { |
| 204 | notify_keyboard_focus_out(seat); |
Bryce Harrington | 260c2ff | 2016-06-29 19:04:06 -0700 | [diff] [blame] | 205 | weston_seat_set_keyboard_focus(seat, surface); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | static void |
| 210 | send_key(struct wl_client *client, struct wl_resource *resource, |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 211 | uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 212 | uint32_t key, enum wl_keyboard_key_state state) |
| 213 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 214 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 215 | struct weston_seat *seat = get_seat(test); |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 216 | struct timespec time; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 217 | |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 218 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 219 | |
| 220 | notify_key(seat, &time, key, state, STATE_UPDATE_AUTOMATIC); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 223 | static void |
| 224 | device_release(struct wl_client *client, |
| 225 | struct wl_resource *resource, const char *device) |
| 226 | { |
| 227 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 228 | struct weston_seat *seat = get_seat(test); |
| 229 | |
| 230 | if (strcmp(device, "pointer") == 0) { |
| 231 | weston_seat_release_pointer(seat); |
| 232 | } else if (strcmp(device, "keyboard") == 0) { |
| 233 | weston_seat_release_keyboard(seat); |
| 234 | } else if (strcmp(device, "touch") == 0) { |
| 235 | weston_seat_release_touch(seat); |
| 236 | } else if (strcmp(device, "seat") == 0) { |
| 237 | weston_seat_release(seat); |
| 238 | } else { |
| 239 | assert(0 && "Unsupported device"); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static void |
| 244 | device_add(struct wl_client *client, |
| 245 | struct wl_resource *resource, const char *device) |
| 246 | { |
| 247 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 248 | struct weston_seat *seat = get_seat(test); |
| 249 | |
| 250 | if (strcmp(device, "pointer") == 0) { |
| 251 | weston_seat_init_pointer(seat); |
| 252 | } else if (strcmp(device, "keyboard") == 0) { |
| 253 | weston_seat_init_keyboard(seat, NULL); |
| 254 | } else if (strcmp(device, "touch") == 0) { |
| 255 | weston_seat_init_touch(seat); |
| 256 | } else { |
| 257 | assert(0 && "Unsupported device"); |
| 258 | } |
| 259 | } |
| 260 | |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 261 | enum weston_test_screenshot_outcome { |
| 262 | WESTON_TEST_SCREENSHOT_SUCCESS, |
| 263 | WESTON_TEST_SCREENSHOT_NO_MEMORY, |
| 264 | WESTON_TEST_SCREENSHOT_BAD_BUFFER |
| 265 | }; |
| 266 | |
| 267 | typedef void (*weston_test_screenshot_done_func_t)(void *data, |
| 268 | enum weston_test_screenshot_outcome outcome); |
| 269 | |
| 270 | struct test_screenshot { |
| 271 | struct weston_compositor *compositor; |
| 272 | struct wl_global *global; |
| 273 | struct wl_client *client; |
| 274 | struct weston_process process; |
| 275 | struct wl_listener destroy_listener; |
| 276 | }; |
| 277 | |
| 278 | struct test_screenshot_frame_listener { |
| 279 | struct wl_listener listener; |
| 280 | struct weston_buffer *buffer; |
| 281 | weston_test_screenshot_done_func_t done; |
| 282 | void *data; |
| 283 | }; |
| 284 | |
| 285 | static void |
| 286 | copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride) |
| 287 | { |
| 288 | uint8_t *end; |
| 289 | |
| 290 | end = dst + height * stride; |
| 291 | while (dst < end) { |
| 292 | memcpy(dst, src, stride); |
| 293 | dst += stride; |
| 294 | src -= stride; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | |
| 299 | static void |
| 300 | copy_bgra(uint8_t *dst, uint8_t *src, int height, int stride) |
| 301 | { |
| 302 | /* TODO: optimize this out */ |
| 303 | memcpy(dst, src, height * stride); |
| 304 | } |
| 305 | |
| 306 | static void |
| 307 | copy_row_swap_RB(void *vdst, void *vsrc, int bytes) |
| 308 | { |
| 309 | uint32_t *dst = vdst; |
| 310 | uint32_t *src = vsrc; |
| 311 | uint32_t *end = dst + bytes / 4; |
| 312 | |
| 313 | while (dst < end) { |
| 314 | uint32_t v = *src++; |
| 315 | /* A R G B */ |
| 316 | uint32_t tmp = v & 0xff00ff00; |
| 317 | tmp |= (v >> 16) & 0x000000ff; |
| 318 | tmp |= (v << 16) & 0x00ff0000; |
| 319 | *dst++ = tmp; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static void |
| 324 | copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride) |
| 325 | { |
| 326 | uint8_t *end; |
| 327 | |
| 328 | end = dst + height * stride; |
| 329 | while (dst < end) { |
| 330 | copy_row_swap_RB(dst, src, stride); |
| 331 | dst += stride; |
| 332 | src -= stride; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | copy_rgba(uint8_t *dst, uint8_t *src, int height, int stride) |
| 338 | { |
| 339 | uint8_t *end; |
| 340 | |
| 341 | end = dst + height * stride; |
| 342 | while (dst < end) { |
| 343 | copy_row_swap_RB(dst, src, stride); |
| 344 | dst += stride; |
| 345 | src += stride; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static void |
| 350 | test_screenshot_frame_notify(struct wl_listener *listener, void *data) |
| 351 | { |
| 352 | struct test_screenshot_frame_listener *l = |
| 353 | container_of(listener, |
| 354 | struct test_screenshot_frame_listener, listener); |
| 355 | struct weston_output *output = data; |
| 356 | struct weston_compositor *compositor = output->compositor; |
| 357 | int32_t stride; |
| 358 | uint8_t *pixels, *d, *s; |
| 359 | |
| 360 | output->disable_planes--; |
| 361 | wl_list_remove(&listener->link); |
| 362 | stride = l->buffer->width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8); |
| 363 | pixels = malloc(stride * l->buffer->height); |
| 364 | |
| 365 | if (pixels == NULL) { |
| 366 | l->done(l->data, WESTON_TEST_SCREENSHOT_NO_MEMORY); |
| 367 | free(l); |
| 368 | return; |
| 369 | } |
| 370 | |
Chris Michael | 2ec5f2a | 2015-12-03 12:23:12 -0500 | [diff] [blame] | 371 | /* FIXME: Needs to handle output transformations */ |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 372 | |
| 373 | compositor->renderer->read_pixels(output, |
| 374 | compositor->read_format, |
| 375 | pixels, |
| 376 | 0, 0, |
| 377 | output->current_mode->width, |
| 378 | output->current_mode->height); |
| 379 | |
| 380 | stride = wl_shm_buffer_get_stride(l->buffer->shm_buffer); |
| 381 | |
| 382 | d = wl_shm_buffer_get_data(l->buffer->shm_buffer); |
| 383 | s = pixels + stride * (l->buffer->height - 1); |
| 384 | |
| 385 | wl_shm_buffer_begin_access(l->buffer->shm_buffer); |
| 386 | |
| 387 | /* XXX: It would be nice if we used Pixman to do all this rather |
| 388 | * than our own implementation |
| 389 | */ |
| 390 | switch (compositor->read_format) { |
| 391 | case PIXMAN_a8r8g8b8: |
| 392 | case PIXMAN_x8r8g8b8: |
| 393 | if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP) |
| 394 | copy_bgra_yflip(d, s, output->current_mode->height, stride); |
| 395 | else |
| 396 | copy_bgra(d, pixels, output->current_mode->height, stride); |
| 397 | break; |
| 398 | case PIXMAN_x8b8g8r8: |
| 399 | case PIXMAN_a8b8g8r8: |
| 400 | if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP) |
| 401 | copy_rgba_yflip(d, s, output->current_mode->height, stride); |
| 402 | else |
| 403 | copy_rgba(d, pixels, output->current_mode->height, stride); |
| 404 | break; |
| 405 | default: |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | wl_shm_buffer_end_access(l->buffer->shm_buffer); |
| 410 | |
| 411 | l->done(l->data, WESTON_TEST_SCREENSHOT_SUCCESS); |
| 412 | free(pixels); |
| 413 | free(l); |
| 414 | } |
| 415 | |
| 416 | static bool |
| 417 | weston_test_screenshot_shoot(struct weston_output *output, |
| 418 | struct weston_buffer *buffer, |
| 419 | weston_test_screenshot_done_func_t done, |
| 420 | void *data) |
| 421 | { |
| 422 | struct test_screenshot_frame_listener *l; |
| 423 | |
| 424 | /* Get the shm buffer resource the client created */ |
| 425 | if (!wl_shm_buffer_get(buffer->resource)) { |
| 426 | done(data, WESTON_TEST_SCREENSHOT_BAD_BUFFER); |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | buffer->shm_buffer = wl_shm_buffer_get(buffer->resource); |
| 431 | buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer); |
| 432 | buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer); |
| 433 | |
| 434 | /* Verify buffer is big enough */ |
| 435 | if (buffer->width < output->current_mode->width || |
| 436 | buffer->height < output->current_mode->height) { |
| 437 | done(data, WESTON_TEST_SCREENSHOT_BAD_BUFFER); |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | /* allocate the frame listener */ |
| 442 | l = malloc(sizeof *l); |
| 443 | if (l == NULL) { |
| 444 | done(data, WESTON_TEST_SCREENSHOT_NO_MEMORY); |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | /* Set up the listener */ |
| 449 | l->buffer = buffer; |
| 450 | l->done = done; |
| 451 | l->data = data; |
| 452 | l->listener.notify = test_screenshot_frame_notify; |
| 453 | wl_signal_add(&output->frame_signal, &l->listener); |
| 454 | |
| 455 | /* Fire off a repaint */ |
| 456 | output->disable_planes++; |
| 457 | weston_output_schedule_repaint(output); |
| 458 | |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | static void |
| 463 | capture_screenshot_done(void *data, enum weston_test_screenshot_outcome outcome) |
| 464 | { |
| 465 | struct wl_resource *resource = data; |
| 466 | |
| 467 | switch (outcome) { |
| 468 | case WESTON_TEST_SCREENSHOT_SUCCESS: |
| 469 | weston_test_send_capture_screenshot_done(resource); |
| 470 | break; |
| 471 | case WESTON_TEST_SCREENSHOT_NO_MEMORY: |
| 472 | wl_resource_post_no_memory(resource); |
| 473 | break; |
| 474 | default: |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /** |
| 481 | * Grabs a snapshot of the screen. |
| 482 | */ |
| 483 | static void |
| 484 | capture_screenshot(struct wl_client *client, |
| 485 | struct wl_resource *resource, |
| 486 | struct wl_resource *output_resource, |
| 487 | struct wl_resource *buffer_resource) |
| 488 | { |
| 489 | struct weston_output *output = |
Pekka Paalanen | 9ffb250 | 2017-03-27 15:14:32 +0300 | [diff] [blame] | 490 | weston_output_from_resource(output_resource); |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 491 | struct weston_buffer *buffer = |
| 492 | weston_buffer_from_resource(buffer_resource); |
| 493 | |
| 494 | if (buffer == NULL) { |
| 495 | wl_resource_post_no_memory(resource); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | weston_test_screenshot_shoot(output, buffer, |
| 500 | capture_screenshot_done, resource); |
| 501 | } |
| 502 | |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame^] | 503 | static void |
| 504 | send_touch(struct wl_client *client, struct wl_resource *resource, |
| 505 | uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec, |
| 506 | int32_t touch_id, wl_fixed_t x, wl_fixed_t y, uint32_t touch_type) |
| 507 | { |
| 508 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 509 | struct weston_seat *seat = get_seat(test); |
| 510 | struct timespec time; |
| 511 | |
| 512 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
| 513 | |
| 514 | notify_touch(seat, &time, touch_id, wl_fixed_to_double(x), |
| 515 | wl_fixed_to_double(y), touch_type); |
| 516 | } |
| 517 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 518 | static const struct weston_test_interface test_implementation = { |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 519 | move_surface, |
| 520 | move_pointer, |
| 521 | send_button, |
| 522 | activate_surface, |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 523 | send_key, |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 524 | device_release, |
| 525 | device_add, |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 526 | capture_screenshot, |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame^] | 527 | send_touch, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 528 | }; |
| 529 | |
| 530 | static void |
| 531 | bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 532 | { |
| 533 | struct weston_test *test = data; |
| 534 | struct wl_resource *resource; |
| 535 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 536 | resource = wl_resource_create(client, &weston_test_interface, 1, id); |
Marek Chalupa | 42ebdda | 2014-07-11 12:33:02 +0200 | [diff] [blame] | 537 | if (!resource) { |
| 538 | wl_client_post_no_memory(client); |
| 539 | return; |
| 540 | } |
| 541 | |
Kristian Høgsberg | 442a5fa | 2013-07-03 18:13:33 -0400 | [diff] [blame] | 542 | wl_resource_set_implementation(resource, |
| 543 | &test_implementation, test, NULL); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 544 | |
| 545 | notify_pointer_position(test, resource); |
| 546 | } |
| 547 | |
| 548 | static void |
| 549 | idle_launch_client(void *data) |
| 550 | { |
| 551 | struct weston_test *test = data; |
| 552 | pid_t pid; |
| 553 | sigset_t allsigs; |
| 554 | char *path; |
| 555 | |
| 556 | path = getenv("WESTON_TEST_CLIENT_PATH"); |
| 557 | if (path == NULL) |
Pekka Paalanen | f72e479 | 2013-11-21 16:23:57 +0200 | [diff] [blame] | 558 | return; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 559 | pid = fork(); |
| 560 | if (pid == -1) |
| 561 | exit(EXIT_FAILURE); |
| 562 | if (pid == 0) { |
| 563 | sigfillset(&allsigs); |
| 564 | sigprocmask(SIG_UNBLOCK, &allsigs, NULL); |
| 565 | execl(path, path, NULL); |
| 566 | weston_log("compositor: executing '%s' failed: %m\n", path); |
| 567 | exit(EXIT_FAILURE); |
| 568 | } |
| 569 | |
| 570 | test->process.pid = pid; |
| 571 | test->process.cleanup = test_client_sigchld; |
| 572 | weston_watch_process(&test->process); |
| 573 | } |
| 574 | |
| 575 | WL_EXPORT int |
Quentin Glidic | 8af2bec | 2016-12-02 14:21:46 +0100 | [diff] [blame] | 576 | wet_module_init(struct weston_compositor *ec, |
| 577 | int *argc, char *argv[]) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 578 | { |
| 579 | struct weston_test *test; |
| 580 | struct wl_event_loop *loop; |
| 581 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 582 | test = zalloc(sizeof *test); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 583 | if (test == NULL) |
| 584 | return -1; |
| 585 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 586 | test->compositor = ec; |
Quentin Glidic | 8268157 | 2016-12-17 13:40:51 +0100 | [diff] [blame] | 587 | weston_layer_init(&test->layer, ec); |
| 588 | weston_layer_set_position(&test->layer, WESTON_LAYER_POSITION_CURSOR - 1); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 589 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 590 | if (wl_global_create(ec->wl_display, &weston_test_interface, 1, |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 591 | test, bind_test) == NULL) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 592 | return -1; |
| 593 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 594 | /* create our own seat */ |
| 595 | weston_seat_init(&test->seat, ec, "test-seat"); |
| 596 | |
| 597 | /* add devices */ |
| 598 | weston_seat_init_pointer(&test->seat); |
Pekka Paalanen | 315bf8c | 2016-06-16 12:04:53 +0300 | [diff] [blame] | 599 | if (weston_seat_init_keyboard(&test->seat, NULL) < 0) |
| 600 | return -1; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 601 | weston_seat_init_touch(&test->seat); |
| 602 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 603 | loop = wl_display_get_event_loop(ec->wl_display); |
| 604 | wl_event_loop_add_idle(loop, idle_launch_client, test); |
| 605 | |
| 606 | return 0; |
| 607 | } |