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> |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 34 | #include <errno.h> |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 35 | #include <fcntl.h> |
| 36 | #include <pthread.h> |
Bryce Harrington | a768026 | 2014-11-19 17:18:34 -0800 | [diff] [blame] | 37 | |
Pekka Paalanen | 3d5d947 | 2019-03-28 16:28:47 +0200 | [diff] [blame] | 38 | #include <libweston/libweston.h> |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 39 | #include <libweston/weston-log.h> |
Marius Vlad | 5d649b6 | 2019-07-16 23:44:21 +0300 | [diff] [blame] | 40 | #include "backend.h" |
Marius Vlad | 0bf3f5a | 2019-07-10 17:32:42 +0300 | [diff] [blame] | 41 | #include "libweston-internal.h" |
Pekka Paalanen | 58f98c9 | 2016-06-03 16:45:21 +0300 | [diff] [blame] | 42 | #include "compositor/weston.h" |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 43 | #include "weston-test-server-protocol.h" |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 44 | #include "weston.h" |
| 45 | #include "weston-testsuite-data.h" |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 46 | |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 47 | #include "shared/helpers.h" |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 48 | #include "shared/timespec-util.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 49 | |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 50 | #define MAX_TOUCH_DEVICES 32 |
| 51 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 52 | struct weston_test { |
| 53 | struct weston_compositor *compositor; |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 54 | struct wl_listener destroy_listener; |
| 55 | |
| 56 | struct weston_log_scope *log; |
| 57 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 58 | struct weston_layer layer; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 59 | struct weston_seat seat; |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 60 | struct weston_touch_device *touch_device[MAX_TOUCH_DEVICES]; |
| 61 | int nr_touch_devices; |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 62 | bool is_seat_initialized; |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 63 | |
| 64 | pthread_t client_thread; |
| 65 | struct wl_event_source *client_source; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | struct weston_test_surface { |
| 69 | struct weston_surface *surface; |
Pekka Paalanen | 7514bf7 | 2021-05-21 16:03:08 +0300 | [diff] [blame] | 70 | struct wl_listener surface_destroy_listener; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 71 | struct weston_view *view; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 72 | int32_t x, y; |
| 73 | struct weston_test *test; |
| 74 | }; |
| 75 | |
| 76 | static void |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 77 | touch_device_add(struct weston_test *test) |
| 78 | { |
| 79 | char buf[128]; |
| 80 | int i = test->nr_touch_devices; |
| 81 | |
| 82 | assert(i < MAX_TOUCH_DEVICES); |
| 83 | assert(!test->touch_device[i]); |
| 84 | |
| 85 | snprintf(buf, sizeof buf, "test-touch-device-%d", i); |
| 86 | |
| 87 | test->touch_device[i] = weston_touch_create_touch_device( |
| 88 | test->seat.touch_state, buf, NULL, NULL); |
| 89 | test->nr_touch_devices++; |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | touch_device_remove(struct weston_test *test) |
| 94 | { |
| 95 | int i = test->nr_touch_devices - 1; |
| 96 | |
| 97 | assert(i >= 0); |
| 98 | assert(test->touch_device[i]); |
| 99 | weston_touch_device_destroy(test->touch_device[i]); |
| 100 | test->touch_device[i] = NULL; |
| 101 | --test->nr_touch_devices; |
| 102 | } |
| 103 | |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 104 | static int |
| 105 | test_seat_init(struct weston_test *test) |
| 106 | { |
Pekka Paalanen | f39249a | 2018-02-26 14:14:14 +0200 | [diff] [blame] | 107 | assert(!test->is_seat_initialized && |
| 108 | "Trying to add already added test seat"); |
| 109 | |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 110 | /* create our own seat */ |
| 111 | weston_seat_init(&test->seat, test->compositor, "test-seat"); |
| 112 | test->is_seat_initialized = true; |
| 113 | |
| 114 | /* add devices */ |
| 115 | weston_seat_init_pointer(&test->seat); |
| 116 | if (weston_seat_init_keyboard(&test->seat, NULL) < 0) |
| 117 | return -1; |
| 118 | weston_seat_init_touch(&test->seat); |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 119 | touch_device_add(test); |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Pekka Paalanen | f39249a | 2018-02-26 14:14:14 +0200 | [diff] [blame] | 124 | static void |
| 125 | test_seat_release(struct weston_test *test) |
| 126 | { |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 127 | while (test->nr_touch_devices > 0) |
| 128 | touch_device_remove(test); |
| 129 | |
Pekka Paalanen | f39249a | 2018-02-26 14:14:14 +0200 | [diff] [blame] | 130 | assert(test->is_seat_initialized && |
| 131 | "Trying to release already released test seat"); |
| 132 | test->is_seat_initialized = false; |
| 133 | weston_seat_release(&test->seat); |
| 134 | memset(&test->seat, 0, sizeof test->seat); |
| 135 | } |
| 136 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 137 | static struct weston_seat * |
| 138 | get_seat(struct weston_test *test) |
| 139 | { |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 140 | return &test->seat; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void |
| 144 | notify_pointer_position(struct weston_test *test, struct wl_resource *resource) |
| 145 | { |
| 146 | struct weston_seat *seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 147 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 148 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 149 | weston_test_send_pointer_position(resource, pointer->x, pointer->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 153 | 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] | 154 | { |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 155 | struct weston_test_surface *test_surface = surface->committed_private; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 156 | struct weston_test *test = test_surface->test; |
| 157 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 158 | if (wl_list_empty(&test_surface->view->layer_link.link)) |
| 159 | weston_layer_entry_insert(&test->layer.view_list, |
| 160 | &test_surface->view->layer_link); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 161 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 162 | weston_view_set_position(test_surface->view, |
| 163 | test_surface->x, test_surface->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 164 | |
Kristian Høgsberg | ace0a39 | 2013-11-13 21:55:57 -0800 | [diff] [blame] | 165 | weston_view_update_transform(test_surface->view); |
Armin Krezović | d0cf441 | 2016-06-30 06:04:32 +0200 | [diff] [blame] | 166 | |
| 167 | test_surface->surface->is_mapped = true; |
| 168 | test_surface->view->is_mapped = true; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Pekka Paalanen | 7514bf7 | 2021-05-21 16:03:08 +0300 | [diff] [blame] | 171 | static int |
| 172 | test_surface_get_label(struct weston_surface *surface, char *buf, size_t len) |
| 173 | { |
Emmanuel Gil Peyrot | eff793a | 2021-07-31 17:25:41 +0200 | [diff] [blame] | 174 | return snprintf(buf, len, "test suite surface"); |
Pekka Paalanen | 7514bf7 | 2021-05-21 16:03:08 +0300 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void |
| 178 | test_surface_destroy(struct weston_test_surface *test_surface) |
| 179 | { |
| 180 | weston_view_destroy(test_surface->view); |
| 181 | |
| 182 | test_surface->surface->committed = NULL; |
| 183 | test_surface->surface->committed_private = NULL; |
| 184 | weston_surface_set_label_func(test_surface->surface, NULL); |
| 185 | |
| 186 | wl_list_remove(&test_surface->surface_destroy_listener.link); |
| 187 | free(test_surface); |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | test_surface_handle_surface_destroy(struct wl_listener *l, void *data) |
| 192 | { |
| 193 | struct weston_test_surface *test_surface = |
| 194 | wl_container_of(l, test_surface, surface_destroy_listener); |
| 195 | |
| 196 | assert(test_surface->surface == data); |
| 197 | |
| 198 | test_surface_destroy(test_surface); |
| 199 | } |
| 200 | |
| 201 | static struct weston_test_surface * |
| 202 | weston_test_surface_create(struct wl_resource *test_resource, |
| 203 | struct weston_surface *surface) |
| 204 | { |
| 205 | struct wl_client *client = wl_resource_get_client(test_resource); |
| 206 | struct wl_resource *display_resource; |
| 207 | struct weston_test_surface *test_surface; |
| 208 | |
| 209 | test_surface = zalloc(sizeof *test_surface); |
| 210 | if (!test_surface) |
| 211 | goto err_post_no_mem; |
| 212 | |
| 213 | test_surface->surface = surface; |
| 214 | test_surface->test = wl_resource_get_user_data(test_resource); |
| 215 | |
| 216 | test_surface->view = weston_view_create(surface); |
| 217 | if (!test_surface->view) |
| 218 | goto err_free_surface; |
| 219 | |
| 220 | /* Protocol does not define this error so abuse wl_display */ |
| 221 | display_resource = wl_client_get_object(client, 1); |
| 222 | if (weston_surface_set_role(surface, "weston_test_surface", |
| 223 | display_resource, |
| 224 | WL_DISPLAY_ERROR_INVALID_OBJECT) < 0) |
| 225 | goto err_free_view; |
| 226 | |
| 227 | surface->committed_private = test_surface; |
| 228 | surface->committed = test_surface_committed; |
| 229 | weston_surface_set_label_func(surface, test_surface_get_label); |
| 230 | |
| 231 | test_surface->surface_destroy_listener.notify = |
| 232 | test_surface_handle_surface_destroy; |
| 233 | wl_signal_add(&surface->destroy_signal, |
| 234 | &test_surface->surface_destroy_listener); |
| 235 | |
| 236 | return test_surface; |
| 237 | |
| 238 | err_free_view: |
| 239 | weston_view_destroy(test_surface->view); |
| 240 | |
| 241 | err_free_surface: |
| 242 | free(test_surface); |
| 243 | |
| 244 | err_post_no_mem: |
| 245 | wl_resource_post_no_memory(test_resource); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 249 | static void |
| 250 | move_surface(struct wl_client *client, struct wl_resource *resource, |
| 251 | struct wl_resource *surface_resource, |
| 252 | int32_t x, int32_t y) |
| 253 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 254 | struct weston_surface *surface = |
| 255 | wl_resource_get_user_data(surface_resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 256 | struct weston_test_surface *test_surface; |
Pekka Paalanen | 7514bf7 | 2021-05-21 16:03:08 +0300 | [diff] [blame] | 257 | struct wl_resource *display_resource; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 258 | |
Pekka Paalanen | 7514bf7 | 2021-05-21 16:03:08 +0300 | [diff] [blame] | 259 | if (surface->committed && |
| 260 | surface->committed != test_surface_committed) { |
| 261 | display_resource = wl_client_get_object(client, 1); |
| 262 | wl_resource_post_error(display_resource, |
| 263 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 264 | "weston_test.move_surface: wl_surface@%u has a role.", |
| 265 | wl_resource_get_id(surface_resource)); |
| 266 | return; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Pekka Paalanen | 7514bf7 | 2021-05-21 16:03:08 +0300 | [diff] [blame] | 269 | test_surface = surface->committed_private; |
| 270 | if (!test_surface) |
| 271 | test_surface = weston_test_surface_create(resource, surface); |
| 272 | if (!test_surface) |
| 273 | return; |
| 274 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 275 | test_surface->x = x; |
| 276 | test_surface->y = y; |
| 277 | } |
| 278 | |
| 279 | static void |
| 280 | move_pointer(struct wl_client *client, struct wl_resource *resource, |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 281 | 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] | 282 | int32_t x, int32_t y) |
| 283 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 284 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 285 | struct weston_seat *seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 286 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 287 | struct weston_pointer_motion_event event = { 0 }; |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 288 | struct timespec time; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 289 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 290 | event = (struct weston_pointer_motion_event) { |
| 291 | .mask = WESTON_POINTER_MOTION_REL, |
| 292 | .dx = wl_fixed_to_double(wl_fixed_from_int(x) - pointer->x), |
| 293 | .dy = wl_fixed_to_double(wl_fixed_from_int(y) - pointer->y), |
| 294 | }; |
| 295 | |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 296 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 297 | |
| 298 | notify_motion(seat, &time, &event); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 299 | |
| 300 | notify_pointer_position(test, resource); |
| 301 | } |
| 302 | |
| 303 | static void |
| 304 | send_button(struct wl_client *client, struct wl_resource *resource, |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 305 | 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] | 306 | int32_t button, uint32_t state) |
| 307 | { |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 308 | struct timespec time; |
| 309 | |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 310 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 311 | struct weston_seat *seat = get_seat(test); |
| 312 | |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 313 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 314 | |
| 315 | notify_button(seat, &time, button, state); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void |
Alexandros Frantzis | b0341ae | 2017-12-18 12:16:55 +0200 | [diff] [blame] | 319 | send_axis(struct wl_client *client, struct wl_resource *resource, |
| 320 | uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec, |
| 321 | uint32_t axis, wl_fixed_t value) |
| 322 | { |
| 323 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 324 | struct weston_seat *seat = get_seat(test); |
| 325 | struct timespec time; |
| 326 | struct weston_pointer_axis_event axis_event; |
| 327 | |
| 328 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
| 329 | axis_event.axis = axis; |
| 330 | axis_event.value = wl_fixed_to_double(value); |
| 331 | axis_event.has_discrete = false; |
| 332 | axis_event.discrete = 0; |
| 333 | |
| 334 | notify_axis(seat, &time, &axis_event); |
| 335 | } |
| 336 | |
| 337 | static void |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 338 | activate_surface(struct wl_client *client, struct wl_resource *resource, |
| 339 | struct wl_resource *surface_resource) |
| 340 | { |
| 341 | struct weston_surface *surface = surface_resource ? |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 342 | wl_resource_get_user_data(surface_resource) : NULL; |
| 343 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 344 | struct weston_seat *seat; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 345 | struct weston_keyboard *keyboard; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 346 | |
| 347 | seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 348 | keyboard = weston_seat_get_keyboard(seat); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 349 | if (surface) { |
Bryce Harrington | 260c2ff | 2016-06-29 19:04:06 -0700 | [diff] [blame] | 350 | weston_seat_set_keyboard_focus(seat, surface); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 351 | notify_keyboard_focus_in(seat, &keyboard->keys, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 352 | STATE_UPDATE_AUTOMATIC); |
| 353 | } |
| 354 | else { |
| 355 | notify_keyboard_focus_out(seat); |
Bryce Harrington | 260c2ff | 2016-06-29 19:04:06 -0700 | [diff] [blame] | 356 | weston_seat_set_keyboard_focus(seat, surface); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | static void |
| 361 | send_key(struct wl_client *client, struct wl_resource *resource, |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 362 | 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] | 363 | uint32_t key, enum wl_keyboard_key_state state) |
| 364 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 365 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 366 | struct weston_seat *seat = get_seat(test); |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 367 | struct timespec time; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 368 | |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 369 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 370 | |
| 371 | notify_key(seat, &time, key, state, STATE_UPDATE_AUTOMATIC); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 374 | static void |
| 375 | device_release(struct wl_client *client, |
| 376 | struct wl_resource *resource, const char *device) |
| 377 | { |
| 378 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 379 | struct weston_seat *seat = get_seat(test); |
| 380 | |
| 381 | if (strcmp(device, "pointer") == 0) { |
| 382 | weston_seat_release_pointer(seat); |
| 383 | } else if (strcmp(device, "keyboard") == 0) { |
| 384 | weston_seat_release_keyboard(seat); |
| 385 | } else if (strcmp(device, "touch") == 0) { |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 386 | touch_device_remove(test); |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 387 | weston_seat_release_touch(seat); |
| 388 | } else if (strcmp(device, "seat") == 0) { |
Pekka Paalanen | f39249a | 2018-02-26 14:14:14 +0200 | [diff] [blame] | 389 | test_seat_release(test); |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 390 | } else { |
| 391 | assert(0 && "Unsupported device"); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | static void |
| 396 | device_add(struct wl_client *client, |
| 397 | struct wl_resource *resource, const char *device) |
| 398 | { |
| 399 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 400 | struct weston_seat *seat = get_seat(test); |
| 401 | |
| 402 | if (strcmp(device, "pointer") == 0) { |
| 403 | weston_seat_init_pointer(seat); |
| 404 | } else if (strcmp(device, "keyboard") == 0) { |
| 405 | weston_seat_init_keyboard(seat, NULL); |
| 406 | } else if (strcmp(device, "touch") == 0) { |
| 407 | weston_seat_init_touch(seat); |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 408 | touch_device_add(test); |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 409 | } else if (strcmp(device, "seat") == 0) { |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 410 | test_seat_init(test); |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 411 | } else { |
| 412 | assert(0 && "Unsupported device"); |
| 413 | } |
| 414 | } |
| 415 | |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 416 | static void |
| 417 | send_touch(struct wl_client *client, struct wl_resource *resource, |
| 418 | uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec, |
| 419 | int32_t touch_id, wl_fixed_t x, wl_fixed_t y, uint32_t touch_type) |
| 420 | { |
| 421 | struct weston_test *test = wl_resource_get_user_data(resource); |
Pekka Paalanen | bcbce33 | 2018-02-26 14:43:00 +0200 | [diff] [blame] | 422 | struct weston_touch_device *device = test->touch_device[0]; |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 423 | struct timespec time; |
| 424 | |
Pekka Paalanen | bcbce33 | 2018-02-26 14:43:00 +0200 | [diff] [blame] | 425 | assert(device); |
| 426 | |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 427 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
| 428 | |
Pekka Paalanen | bcbce33 | 2018-02-26 14:43:00 +0200 | [diff] [blame] | 429 | notify_touch(device, &time, touch_id, wl_fixed_to_double(x), |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 430 | wl_fixed_to_double(y), touch_type); |
| 431 | } |
| 432 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 433 | static const struct weston_test_interface test_implementation = { |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 434 | move_surface, |
| 435 | move_pointer, |
| 436 | send_button, |
Alexandros Frantzis | b0341ae | 2017-12-18 12:16:55 +0200 | [diff] [blame] | 437 | send_axis, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 438 | activate_surface, |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 439 | send_key, |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 440 | device_release, |
| 441 | device_add, |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 442 | send_touch, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 443 | }; |
| 444 | |
| 445 | static void |
| 446 | bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 447 | { |
| 448 | struct weston_test *test = data; |
| 449 | struct wl_resource *resource; |
| 450 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 451 | resource = wl_resource_create(client, &weston_test_interface, 1, id); |
Marek Chalupa | 42ebdda | 2014-07-11 12:33:02 +0200 | [diff] [blame] | 452 | if (!resource) { |
| 453 | wl_client_post_no_memory(client); |
| 454 | return; |
| 455 | } |
| 456 | |
Kristian Høgsberg | 442a5fa | 2013-07-03 18:13:33 -0400 | [diff] [blame] | 457 | wl_resource_set_implementation(resource, |
| 458 | &test_implementation, test, NULL); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 459 | |
| 460 | notify_pointer_position(test, resource); |
| 461 | } |
| 462 | |
| 463 | static void |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 464 | client_thread_cleanup(void *data_) |
| 465 | { |
| 466 | struct wet_testsuite_data *data = data_; |
| 467 | |
| 468 | close(data->thread_event_pipe); |
| 469 | data->thread_event_pipe = -1; |
| 470 | } |
| 471 | |
| 472 | static void * |
| 473 | client_thread_routine(void *data_) |
| 474 | { |
| 475 | struct wet_testsuite_data *data = data_; |
| 476 | |
| 477 | pthread_setname_np(pthread_self(), "client"); |
| 478 | pthread_cleanup_push(client_thread_cleanup, data); |
| 479 | data->run(data); |
| 480 | pthread_cleanup_pop(true); |
| 481 | |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | static void |
| 486 | client_thread_join(struct weston_test *test) |
| 487 | { |
| 488 | assert(test->client_source); |
| 489 | |
| 490 | pthread_join(test->client_thread, NULL); |
| 491 | wl_event_source_remove(test->client_source); |
| 492 | test->client_source = NULL; |
| 493 | |
| 494 | weston_log_scope_printf(test->log, "Test thread reaped.\n"); |
| 495 | } |
| 496 | |
| 497 | static int |
| 498 | handle_client_thread_event(int fd, uint32_t mask, void *data_) |
| 499 | { |
| 500 | struct weston_test *test = data_; |
| 501 | |
| 502 | weston_log_scope_printf(test->log, |
| 503 | "Received thread event mask 0x%x\n", mask); |
| 504 | |
| 505 | if (mask != WL_EVENT_HANGUP) |
| 506 | weston_log("%s: unexpected event %u\n", __func__, mask); |
| 507 | |
| 508 | client_thread_join(test); |
| 509 | weston_compositor_exit(test->compositor); |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static int |
| 515 | create_client_thread(struct weston_test *test, struct wet_testsuite_data *data) |
| 516 | { |
| 517 | struct wl_event_loop *loop; |
| 518 | int pipefd[2] = { -1, -1 }; |
| 519 | sigset_t saved; |
| 520 | sigset_t blocked; |
| 521 | int ret; |
| 522 | |
| 523 | weston_log_scope_printf(test->log, "Creating a thread for running tests...\n"); |
| 524 | |
| 525 | if (pipe2(pipefd, O_CLOEXEC | O_NONBLOCK) < 0) { |
| 526 | weston_log("Creating pipe for a client thread failed: %s\n", |
| 527 | strerror(errno)); |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | loop = wl_display_get_event_loop(test->compositor->wl_display); |
| 532 | test->client_source = wl_event_loop_add_fd(loop, pipefd[0], |
| 533 | WL_EVENT_READABLE, |
| 534 | handle_client_thread_event, |
| 535 | test); |
| 536 | close(pipefd[0]); |
| 537 | |
| 538 | if (!test->client_source) { |
| 539 | weston_log("Adding client thread fd to event loop failed.\n"); |
| 540 | goto out_pipe; |
| 541 | } |
| 542 | |
| 543 | data->thread_event_pipe = pipefd[1]; |
| 544 | |
| 545 | /* Ensure we don't accidentally get signals to the thread. */ |
| 546 | sigfillset(&blocked); |
| 547 | sigdelset(&blocked, SIGSEGV); |
| 548 | sigdelset(&blocked, SIGFPE); |
| 549 | sigdelset(&blocked, SIGILL); |
| 550 | sigdelset(&blocked, SIGCONT); |
| 551 | sigdelset(&blocked, SIGSYS); |
| 552 | if (pthread_sigmask(SIG_BLOCK, &blocked, &saved) != 0) |
| 553 | goto out_source; |
| 554 | |
| 555 | ret = pthread_create(&test->client_thread, NULL, |
| 556 | client_thread_routine, data); |
| 557 | |
| 558 | pthread_sigmask(SIG_SETMASK, &saved, NULL); |
| 559 | |
| 560 | if (ret != 0) { |
| 561 | weston_log("Creating client thread failed: %s (%d)\n", |
| 562 | strerror(ret), ret); |
| 563 | goto out_source; |
| 564 | } |
| 565 | |
| 566 | return 0; |
| 567 | |
| 568 | out_source: |
| 569 | data->thread_event_pipe = -1; |
| 570 | wl_event_source_remove(test->client_source); |
| 571 | test->client_source = NULL; |
| 572 | |
| 573 | out_pipe: |
| 574 | close(pipefd[1]); |
| 575 | |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | static void |
| 580 | idle_launch_testsuite(void *test_) |
| 581 | { |
| 582 | struct weston_test *test = test_; |
Leandro Ribeiro | 32a5acd | 2020-10-19 16:06:22 -0300 | [diff] [blame] | 583 | struct wet_testsuite_data *data = weston_compositor_get_test_data(test->compositor); |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 584 | |
| 585 | if (!data) |
| 586 | return; |
| 587 | |
| 588 | switch (data->type) { |
| 589 | case TEST_TYPE_CLIENT: |
| 590 | if (create_client_thread(test, data) < 0) { |
| 591 | weston_log("Error: creating client thread for test suite failed.\n"); |
| 592 | weston_compositor_exit_with_code(test->compositor, |
| 593 | RESULT_HARD_ERROR); |
| 594 | } |
| 595 | break; |
| 596 | |
| 597 | case TEST_TYPE_PLUGIN: |
| 598 | data->compositor = test->compositor; |
| 599 | weston_log_scope_printf(test->log, |
| 600 | "Running tests from idle handler...\n"); |
| 601 | data->run(data); |
| 602 | weston_compositor_exit(test->compositor); |
| 603 | break; |
| 604 | |
| 605 | case TEST_TYPE_STANDALONE: |
| 606 | weston_log("Error: unknown test internal type %d.\n", |
| 607 | data->type); |
| 608 | weston_compositor_exit_with_code(test->compositor, |
| 609 | RESULT_HARD_ERROR); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | static void |
| 614 | handle_compositor_destroy(struct wl_listener *listener, |
| 615 | void *weston_compositor) |
| 616 | { |
| 617 | struct weston_test *test; |
| 618 | |
| 619 | test = wl_container_of(listener, test, destroy_listener); |
| 620 | |
Derek Foreman | 3759ad1 | 2022-01-19 10:40:06 -0600 | [diff] [blame] | 621 | wl_list_remove(&test->destroy_listener.link); |
| 622 | |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 623 | if (test->client_source) { |
| 624 | weston_log_scope_printf(test->log, "Cancelling client thread...\n"); |
| 625 | pthread_cancel(test->client_thread); |
| 626 | client_thread_join(test); |
| 627 | } |
| 628 | |
Guillaume Champagne | 7bce28b | 2020-02-02 19:59:52 -0500 | [diff] [blame] | 629 | if (test->is_seat_initialized) |
| 630 | test_seat_release(test); |
| 631 | |
| 632 | wl_list_remove(&test->layer.view_list.link); |
| 633 | wl_list_remove(&test->layer.link); |
| 634 | |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 635 | weston_log_scope_destroy(test->log); |
Guillaume Champagne | 7bce28b | 2020-02-02 19:59:52 -0500 | [diff] [blame] | 636 | free(test); |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 637 | } |
| 638 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 639 | WL_EXPORT int |
Quentin Glidic | 8af2bec | 2016-12-02 14:21:46 +0100 | [diff] [blame] | 640 | wet_module_init(struct weston_compositor *ec, |
| 641 | int *argc, char *argv[]) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 642 | { |
| 643 | struct weston_test *test; |
| 644 | struct wl_event_loop *loop; |
| 645 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 646 | test = zalloc(sizeof *test); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 647 | if (test == NULL) |
| 648 | return -1; |
| 649 | |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 650 | if (!weston_compositor_add_destroy_listener_once(ec, |
| 651 | &test->destroy_listener, |
| 652 | handle_compositor_destroy)) { |
| 653 | free(test); |
| 654 | return 0; |
| 655 | } |
| 656 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 657 | test->compositor = ec; |
Quentin Glidic | 8268157 | 2016-12-17 13:40:51 +0100 | [diff] [blame] | 658 | weston_layer_init(&test->layer, ec); |
| 659 | weston_layer_set_position(&test->layer, WESTON_LAYER_POSITION_CURSOR - 1); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 660 | |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 661 | test->log = weston_compositor_add_log_scope(ec, "test-harness-plugin", |
| 662 | "weston-test plugin's own actions", |
| 663 | NULL, NULL, NULL); |
| 664 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 665 | if (wl_global_create(ec->wl_display, &weston_test_interface, 1, |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 666 | test, bind_test) == NULL) |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 667 | goto out_free; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 668 | |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 669 | if (test_seat_init(test) == -1) |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 670 | goto out_free; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 671 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 672 | loop = wl_display_get_event_loop(ec->wl_display); |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 673 | wl_event_loop_add_idle(loop, idle_launch_testsuite, test); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 674 | |
| 675 | return 0; |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 676 | |
| 677 | out_free: |
| 678 | wl_list_remove(&test->destroy_listener.link); |
| 679 | free(test); |
| 680 | return -1; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 681 | } |