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