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; |
| 59 | struct weston_process process; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 60 | struct weston_seat seat; |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 61 | struct weston_touch_device *touch_device[MAX_TOUCH_DEVICES]; |
| 62 | int nr_touch_devices; |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 63 | bool is_seat_initialized; |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame^] | 64 | |
| 65 | pthread_t client_thread; |
| 66 | struct wl_event_source *client_source; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct weston_test_surface { |
| 70 | struct weston_surface *surface; |
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 |
| 77 | test_client_sigchld(struct weston_process *process, int status) |
| 78 | { |
| 79 | struct weston_test *test = |
| 80 | container_of(process, struct weston_test, process); |
| 81 | |
Pekka Paalanen | be61a1f | 2019-03-14 16:56:27 +0200 | [diff] [blame] | 82 | /* Chain up from weston-test-runner's exit code so that ninja |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 83 | * knows the exit status and can report e.g. skipped tests. */ |
| 84 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 85 | exit(WEXITSTATUS(status)); |
| 86 | |
| 87 | /* In case the child aborted or segfaulted... */ |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 88 | assert(status == 0); |
| 89 | |
Pekka Paalanen | a37920e | 2019-02-06 10:53:35 +0200 | [diff] [blame] | 90 | weston_compositor_exit(test->compositor); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 93 | static void |
| 94 | touch_device_add(struct weston_test *test) |
| 95 | { |
| 96 | char buf[128]; |
| 97 | int i = test->nr_touch_devices; |
| 98 | |
| 99 | assert(i < MAX_TOUCH_DEVICES); |
| 100 | assert(!test->touch_device[i]); |
| 101 | |
| 102 | snprintf(buf, sizeof buf, "test-touch-device-%d", i); |
| 103 | |
| 104 | test->touch_device[i] = weston_touch_create_touch_device( |
| 105 | test->seat.touch_state, buf, NULL, NULL); |
| 106 | test->nr_touch_devices++; |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | touch_device_remove(struct weston_test *test) |
| 111 | { |
| 112 | int i = test->nr_touch_devices - 1; |
| 113 | |
| 114 | assert(i >= 0); |
| 115 | assert(test->touch_device[i]); |
| 116 | weston_touch_device_destroy(test->touch_device[i]); |
| 117 | test->touch_device[i] = NULL; |
| 118 | --test->nr_touch_devices; |
| 119 | } |
| 120 | |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 121 | static int |
| 122 | test_seat_init(struct weston_test *test) |
| 123 | { |
Pekka Paalanen | f39249a | 2018-02-26 14:14:14 +0200 | [diff] [blame] | 124 | assert(!test->is_seat_initialized && |
| 125 | "Trying to add already added test seat"); |
| 126 | |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 127 | /* create our own seat */ |
| 128 | weston_seat_init(&test->seat, test->compositor, "test-seat"); |
| 129 | test->is_seat_initialized = true; |
| 130 | |
| 131 | /* add devices */ |
| 132 | weston_seat_init_pointer(&test->seat); |
| 133 | if (weston_seat_init_keyboard(&test->seat, NULL) < 0) |
| 134 | return -1; |
| 135 | weston_seat_init_touch(&test->seat); |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 136 | touch_device_add(test); |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Pekka Paalanen | f39249a | 2018-02-26 14:14:14 +0200 | [diff] [blame] | 141 | static void |
| 142 | test_seat_release(struct weston_test *test) |
| 143 | { |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 144 | while (test->nr_touch_devices > 0) |
| 145 | touch_device_remove(test); |
| 146 | |
Pekka Paalanen | f39249a | 2018-02-26 14:14:14 +0200 | [diff] [blame] | 147 | assert(test->is_seat_initialized && |
| 148 | "Trying to release already released test seat"); |
| 149 | test->is_seat_initialized = false; |
| 150 | weston_seat_release(&test->seat); |
| 151 | memset(&test->seat, 0, sizeof test->seat); |
| 152 | } |
| 153 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 154 | static struct weston_seat * |
| 155 | get_seat(struct weston_test *test) |
| 156 | { |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 157 | return &test->seat; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void |
| 161 | notify_pointer_position(struct weston_test *test, struct wl_resource *resource) |
| 162 | { |
| 163 | struct weston_seat *seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 164 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 165 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 166 | weston_test_send_pointer_position(resource, pointer->x, pointer->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 170 | 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] | 171 | { |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 172 | struct weston_test_surface *test_surface = surface->committed_private; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 173 | struct weston_test *test = test_surface->test; |
| 174 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 175 | if (wl_list_empty(&test_surface->view->layer_link.link)) |
| 176 | weston_layer_entry_insert(&test->layer.view_list, |
| 177 | &test_surface->view->layer_link); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 178 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 179 | weston_view_set_position(test_surface->view, |
| 180 | test_surface->x, test_surface->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 181 | |
Kristian Høgsberg | ace0a39 | 2013-11-13 21:55:57 -0800 | [diff] [blame] | 182 | weston_view_update_transform(test_surface->view); |
Armin Krezović | d0cf441 | 2016-06-30 06:04:32 +0200 | [diff] [blame] | 183 | |
| 184 | test_surface->surface->is_mapped = true; |
| 185 | test_surface->view->is_mapped = true; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void |
| 189 | move_surface(struct wl_client *client, struct wl_resource *resource, |
| 190 | struct wl_resource *surface_resource, |
| 191 | int32_t x, int32_t y) |
| 192 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 193 | struct weston_surface *surface = |
| 194 | wl_resource_get_user_data(surface_resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 195 | struct weston_test_surface *test_surface; |
| 196 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 197 | test_surface = surface->committed_private; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 198 | if (!test_surface) { |
| 199 | test_surface = malloc(sizeof *test_surface); |
| 200 | if (!test_surface) { |
| 201 | wl_resource_post_no_memory(resource); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | test_surface->view = weston_view_create(surface); |
| 206 | if (!test_surface->view) { |
| 207 | wl_resource_post_no_memory(resource); |
| 208 | free(test_surface); |
| 209 | return; |
| 210 | } |
| 211 | |
Quentin Glidic | 2edc3d5 | 2016-08-12 10:41:33 +0200 | [diff] [blame] | 212 | surface->committed_private = test_surface; |
| 213 | surface->committed = test_surface_committed; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | test_surface->surface = surface; |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 217 | test_surface->test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 218 | test_surface->x = x; |
| 219 | test_surface->y = y; |
| 220 | } |
| 221 | |
| 222 | static void |
| 223 | move_pointer(struct wl_client *client, struct wl_resource *resource, |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 224 | 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] | 225 | int32_t x, int32_t y) |
| 226 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 227 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 228 | struct weston_seat *seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 229 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 230 | struct weston_pointer_motion_event event = { 0 }; |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 231 | struct timespec time; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 232 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 233 | event = (struct weston_pointer_motion_event) { |
| 234 | .mask = WESTON_POINTER_MOTION_REL, |
| 235 | .dx = wl_fixed_to_double(wl_fixed_from_int(x) - pointer->x), |
| 236 | .dy = wl_fixed_to_double(wl_fixed_from_int(y) - pointer->y), |
| 237 | }; |
| 238 | |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 239 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
Alexandros Frantzis | 84b31f8 | 2017-11-24 18:01:46 +0200 | [diff] [blame] | 240 | |
| 241 | notify_motion(seat, &time, &event); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 242 | |
| 243 | notify_pointer_position(test, resource); |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | send_button(struct wl_client *client, struct wl_resource *resource, |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 248 | 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] | 249 | int32_t button, uint32_t state) |
| 250 | { |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 251 | struct timespec time; |
| 252 | |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 253 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 254 | struct weston_seat *seat = get_seat(test); |
| 255 | |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 256 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
Alexandros Frantzis | 215bedc | 2017-11-16 18:20:55 +0200 | [diff] [blame] | 257 | |
| 258 | notify_button(seat, &time, button, state); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static void |
Alexandros Frantzis | b0341ae | 2017-12-18 12:16:55 +0200 | [diff] [blame] | 262 | send_axis(struct wl_client *client, struct wl_resource *resource, |
| 263 | uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec, |
| 264 | uint32_t axis, wl_fixed_t value) |
| 265 | { |
| 266 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 267 | struct weston_seat *seat = get_seat(test); |
| 268 | struct timespec time; |
| 269 | struct weston_pointer_axis_event axis_event; |
| 270 | |
| 271 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
| 272 | axis_event.axis = axis; |
| 273 | axis_event.value = wl_fixed_to_double(value); |
| 274 | axis_event.has_discrete = false; |
| 275 | axis_event.discrete = 0; |
| 276 | |
| 277 | notify_axis(seat, &time, &axis_event); |
| 278 | } |
| 279 | |
| 280 | static void |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 281 | activate_surface(struct wl_client *client, struct wl_resource *resource, |
| 282 | struct wl_resource *surface_resource) |
| 283 | { |
| 284 | struct weston_surface *surface = surface_resource ? |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 285 | wl_resource_get_user_data(surface_resource) : NULL; |
| 286 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 287 | struct weston_seat *seat; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 288 | struct weston_keyboard *keyboard; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 289 | |
| 290 | seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 291 | keyboard = weston_seat_get_keyboard(seat); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 292 | if (surface) { |
Bryce Harrington | 260c2ff | 2016-06-29 19:04:06 -0700 | [diff] [blame] | 293 | weston_seat_set_keyboard_focus(seat, surface); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 294 | notify_keyboard_focus_in(seat, &keyboard->keys, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 295 | STATE_UPDATE_AUTOMATIC); |
| 296 | } |
| 297 | else { |
| 298 | notify_keyboard_focus_out(seat); |
Bryce Harrington | 260c2ff | 2016-06-29 19:04:06 -0700 | [diff] [blame] | 299 | weston_seat_set_keyboard_focus(seat, surface); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | static void |
| 304 | send_key(struct wl_client *client, struct wl_resource *resource, |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +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 | uint32_t key, enum wl_keyboard_key_state state) |
| 307 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 308 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 309 | struct weston_seat *seat = get_seat(test); |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 310 | struct timespec time; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 311 | |
Alexandros Frantzis | dae224c | 2017-12-13 13:27:57 +0200 | [diff] [blame] | 312 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
Alexandros Frantzis | 47e79c8 | 2017-11-16 18:20:57 +0200 | [diff] [blame] | 313 | |
| 314 | notify_key(seat, &time, key, state, STATE_UPDATE_AUTOMATIC); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 317 | static void |
| 318 | device_release(struct wl_client *client, |
| 319 | struct wl_resource *resource, const char *device) |
| 320 | { |
| 321 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 322 | struct weston_seat *seat = get_seat(test); |
| 323 | |
| 324 | if (strcmp(device, "pointer") == 0) { |
| 325 | weston_seat_release_pointer(seat); |
| 326 | } else if (strcmp(device, "keyboard") == 0) { |
| 327 | weston_seat_release_keyboard(seat); |
| 328 | } else if (strcmp(device, "touch") == 0) { |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 329 | touch_device_remove(test); |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 330 | weston_seat_release_touch(seat); |
| 331 | } else if (strcmp(device, "seat") == 0) { |
Pekka Paalanen | f39249a | 2018-02-26 14:14:14 +0200 | [diff] [blame] | 332 | test_seat_release(test); |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 333 | } else { |
| 334 | assert(0 && "Unsupported device"); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | static void |
| 339 | device_add(struct wl_client *client, |
| 340 | struct wl_resource *resource, const char *device) |
| 341 | { |
| 342 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 343 | struct weston_seat *seat = get_seat(test); |
| 344 | |
| 345 | if (strcmp(device, "pointer") == 0) { |
| 346 | weston_seat_init_pointer(seat); |
| 347 | } else if (strcmp(device, "keyboard") == 0) { |
| 348 | weston_seat_init_keyboard(seat, NULL); |
| 349 | } else if (strcmp(device, "touch") == 0) { |
| 350 | weston_seat_init_touch(seat); |
Louis-Francis Ratté-Boulianne | 6ef59c9 | 2017-11-28 20:42:47 -0500 | [diff] [blame] | 351 | touch_device_add(test); |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 352 | } else if (strcmp(device, "seat") == 0) { |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 353 | test_seat_init(test); |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 354 | } else { |
| 355 | assert(0 && "Unsupported device"); |
| 356 | } |
| 357 | } |
| 358 | |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 359 | enum weston_test_screenshot_outcome { |
| 360 | WESTON_TEST_SCREENSHOT_SUCCESS, |
| 361 | WESTON_TEST_SCREENSHOT_NO_MEMORY, |
| 362 | WESTON_TEST_SCREENSHOT_BAD_BUFFER |
| 363 | }; |
| 364 | |
| 365 | typedef void (*weston_test_screenshot_done_func_t)(void *data, |
| 366 | enum weston_test_screenshot_outcome outcome); |
| 367 | |
| 368 | struct test_screenshot { |
| 369 | struct weston_compositor *compositor; |
| 370 | struct wl_global *global; |
| 371 | struct wl_client *client; |
| 372 | struct weston_process process; |
| 373 | struct wl_listener destroy_listener; |
| 374 | }; |
| 375 | |
| 376 | struct test_screenshot_frame_listener { |
| 377 | struct wl_listener listener; |
| 378 | struct weston_buffer *buffer; |
Leandro Ribeiro | 442ffcb | 2019-11-27 12:47:19 -0300 | [diff] [blame] | 379 | struct weston_output *output; |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 380 | weston_test_screenshot_done_func_t done; |
| 381 | void *data; |
| 382 | }; |
| 383 | |
| 384 | static void |
| 385 | copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride) |
| 386 | { |
| 387 | uint8_t *end; |
| 388 | |
| 389 | end = dst + height * stride; |
| 390 | while (dst < end) { |
| 391 | memcpy(dst, src, stride); |
| 392 | dst += stride; |
| 393 | src -= stride; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | static void |
| 399 | copy_bgra(uint8_t *dst, uint8_t *src, int height, int stride) |
| 400 | { |
| 401 | /* TODO: optimize this out */ |
| 402 | memcpy(dst, src, height * stride); |
| 403 | } |
| 404 | |
| 405 | static void |
| 406 | copy_row_swap_RB(void *vdst, void *vsrc, int bytes) |
| 407 | { |
| 408 | uint32_t *dst = vdst; |
| 409 | uint32_t *src = vsrc; |
| 410 | uint32_t *end = dst + bytes / 4; |
| 411 | |
| 412 | while (dst < end) { |
| 413 | uint32_t v = *src++; |
| 414 | /* A R G B */ |
| 415 | uint32_t tmp = v & 0xff00ff00; |
| 416 | tmp |= (v >> 16) & 0x000000ff; |
| 417 | tmp |= (v << 16) & 0x00ff0000; |
| 418 | *dst++ = tmp; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | static void |
| 423 | copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride) |
| 424 | { |
| 425 | uint8_t *end; |
| 426 | |
| 427 | end = dst + height * stride; |
| 428 | while (dst < end) { |
| 429 | copy_row_swap_RB(dst, src, stride); |
| 430 | dst += stride; |
| 431 | src -= stride; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | static void |
| 436 | copy_rgba(uint8_t *dst, uint8_t *src, int height, int stride) |
| 437 | { |
| 438 | uint8_t *end; |
| 439 | |
| 440 | end = dst + height * stride; |
| 441 | while (dst < end) { |
| 442 | copy_row_swap_RB(dst, src, stride); |
| 443 | dst += stride; |
| 444 | src += stride; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | static void |
| 449 | test_screenshot_frame_notify(struct wl_listener *listener, void *data) |
| 450 | { |
| 451 | struct test_screenshot_frame_listener *l = |
| 452 | container_of(listener, |
| 453 | struct test_screenshot_frame_listener, listener); |
Leandro Ribeiro | 442ffcb | 2019-11-27 12:47:19 -0300 | [diff] [blame] | 454 | struct weston_output *output = l->output; |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 455 | struct weston_compositor *compositor = output->compositor; |
| 456 | int32_t stride; |
| 457 | uint8_t *pixels, *d, *s; |
| 458 | |
Ankit Nautiyal | 93dde24 | 2019-07-08 11:46:42 +0530 | [diff] [blame] | 459 | weston_output_disable_planes_decr(output); |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 460 | wl_list_remove(&listener->link); |
| 461 | stride = l->buffer->width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8); |
| 462 | pixels = malloc(stride * l->buffer->height); |
| 463 | |
| 464 | if (pixels == NULL) { |
| 465 | l->done(l->data, WESTON_TEST_SCREENSHOT_NO_MEMORY); |
| 466 | free(l); |
| 467 | return; |
| 468 | } |
| 469 | |
Chris Michael | 2ec5f2a | 2015-12-03 12:23:12 -0500 | [diff] [blame] | 470 | /* FIXME: Needs to handle output transformations */ |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 471 | |
| 472 | compositor->renderer->read_pixels(output, |
| 473 | compositor->read_format, |
| 474 | pixels, |
| 475 | 0, 0, |
| 476 | output->current_mode->width, |
| 477 | output->current_mode->height); |
| 478 | |
| 479 | stride = wl_shm_buffer_get_stride(l->buffer->shm_buffer); |
| 480 | |
| 481 | d = wl_shm_buffer_get_data(l->buffer->shm_buffer); |
| 482 | s = pixels + stride * (l->buffer->height - 1); |
| 483 | |
| 484 | wl_shm_buffer_begin_access(l->buffer->shm_buffer); |
| 485 | |
| 486 | /* XXX: It would be nice if we used Pixman to do all this rather |
| 487 | * than our own implementation |
| 488 | */ |
| 489 | switch (compositor->read_format) { |
| 490 | case PIXMAN_a8r8g8b8: |
| 491 | case PIXMAN_x8r8g8b8: |
| 492 | if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP) |
| 493 | copy_bgra_yflip(d, s, output->current_mode->height, stride); |
| 494 | else |
| 495 | copy_bgra(d, pixels, output->current_mode->height, stride); |
| 496 | break; |
| 497 | case PIXMAN_x8b8g8r8: |
| 498 | case PIXMAN_a8b8g8r8: |
| 499 | if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP) |
| 500 | copy_rgba_yflip(d, s, output->current_mode->height, stride); |
| 501 | else |
| 502 | copy_rgba(d, pixels, output->current_mode->height, stride); |
| 503 | break; |
| 504 | default: |
| 505 | break; |
| 506 | } |
| 507 | |
| 508 | wl_shm_buffer_end_access(l->buffer->shm_buffer); |
| 509 | |
| 510 | l->done(l->data, WESTON_TEST_SCREENSHOT_SUCCESS); |
| 511 | free(pixels); |
| 512 | free(l); |
| 513 | } |
| 514 | |
| 515 | static bool |
| 516 | weston_test_screenshot_shoot(struct weston_output *output, |
| 517 | struct weston_buffer *buffer, |
| 518 | weston_test_screenshot_done_func_t done, |
| 519 | void *data) |
| 520 | { |
| 521 | struct test_screenshot_frame_listener *l; |
| 522 | |
| 523 | /* Get the shm buffer resource the client created */ |
| 524 | if (!wl_shm_buffer_get(buffer->resource)) { |
| 525 | done(data, WESTON_TEST_SCREENSHOT_BAD_BUFFER); |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | buffer->shm_buffer = wl_shm_buffer_get(buffer->resource); |
| 530 | buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer); |
| 531 | buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer); |
| 532 | |
| 533 | /* Verify buffer is big enough */ |
| 534 | if (buffer->width < output->current_mode->width || |
| 535 | buffer->height < output->current_mode->height) { |
| 536 | done(data, WESTON_TEST_SCREENSHOT_BAD_BUFFER); |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | /* allocate the frame listener */ |
| 541 | l = malloc(sizeof *l); |
| 542 | if (l == NULL) { |
| 543 | done(data, WESTON_TEST_SCREENSHOT_NO_MEMORY); |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | /* Set up the listener */ |
| 548 | l->buffer = buffer; |
Leandro Ribeiro | 442ffcb | 2019-11-27 12:47:19 -0300 | [diff] [blame] | 549 | l->output = output; |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 550 | l->done = done; |
| 551 | l->data = data; |
| 552 | l->listener.notify = test_screenshot_frame_notify; |
| 553 | wl_signal_add(&output->frame_signal, &l->listener); |
| 554 | |
| 555 | /* Fire off a repaint */ |
Ankit Nautiyal | 93dde24 | 2019-07-08 11:46:42 +0530 | [diff] [blame] | 556 | weston_output_disable_planes_incr(output); |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 557 | weston_output_schedule_repaint(output); |
| 558 | |
| 559 | return true; |
| 560 | } |
| 561 | |
| 562 | static void |
| 563 | capture_screenshot_done(void *data, enum weston_test_screenshot_outcome outcome) |
| 564 | { |
| 565 | struct wl_resource *resource = data; |
| 566 | |
| 567 | switch (outcome) { |
| 568 | case WESTON_TEST_SCREENSHOT_SUCCESS: |
| 569 | weston_test_send_capture_screenshot_done(resource); |
| 570 | break; |
| 571 | case WESTON_TEST_SCREENSHOT_NO_MEMORY: |
| 572 | wl_resource_post_no_memory(resource); |
| 573 | break; |
| 574 | default: |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | |
| 580 | /** |
| 581 | * Grabs a snapshot of the screen. |
| 582 | */ |
| 583 | static void |
| 584 | capture_screenshot(struct wl_client *client, |
| 585 | struct wl_resource *resource, |
| 586 | struct wl_resource *output_resource, |
| 587 | struct wl_resource *buffer_resource) |
| 588 | { |
| 589 | struct weston_output *output = |
Pekka Paalanen | 055c113 | 2017-03-27 16:31:25 +0300 | [diff] [blame] | 590 | weston_head_from_resource(output_resource)->output; |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 591 | struct weston_buffer *buffer = |
| 592 | weston_buffer_from_resource(buffer_resource); |
| 593 | |
| 594 | if (buffer == NULL) { |
| 595 | wl_resource_post_no_memory(resource); |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | weston_test_screenshot_shoot(output, buffer, |
| 600 | capture_screenshot_done, resource); |
| 601 | } |
| 602 | |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 603 | static void |
| 604 | send_touch(struct wl_client *client, struct wl_resource *resource, |
| 605 | uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec, |
| 606 | int32_t touch_id, wl_fixed_t x, wl_fixed_t y, uint32_t touch_type) |
| 607 | { |
| 608 | struct weston_test *test = wl_resource_get_user_data(resource); |
Pekka Paalanen | bcbce33 | 2018-02-26 14:43:00 +0200 | [diff] [blame] | 609 | struct weston_touch_device *device = test->touch_device[0]; |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 610 | struct timespec time; |
| 611 | |
Pekka Paalanen | bcbce33 | 2018-02-26 14:43:00 +0200 | [diff] [blame] | 612 | assert(device); |
| 613 | |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 614 | timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec); |
| 615 | |
Pekka Paalanen | bcbce33 | 2018-02-26 14:43:00 +0200 | [diff] [blame] | 616 | notify_touch(device, &time, touch_id, wl_fixed_to_double(x), |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 617 | wl_fixed_to_double(y), touch_type); |
| 618 | } |
| 619 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 620 | static const struct weston_test_interface test_implementation = { |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 621 | move_surface, |
| 622 | move_pointer, |
| 623 | send_button, |
Alexandros Frantzis | b0341ae | 2017-12-18 12:16:55 +0200 | [diff] [blame] | 624 | send_axis, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 625 | activate_surface, |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 626 | send_key, |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 627 | device_release, |
| 628 | device_add, |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 629 | capture_screenshot, |
Alexandros Frantzis | c20b580 | 2017-12-13 13:27:58 +0200 | [diff] [blame] | 630 | send_touch, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | static void |
| 634 | bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 635 | { |
| 636 | struct weston_test *test = data; |
| 637 | struct wl_resource *resource; |
| 638 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 639 | resource = wl_resource_create(client, &weston_test_interface, 1, id); |
Marek Chalupa | 42ebdda | 2014-07-11 12:33:02 +0200 | [diff] [blame] | 640 | if (!resource) { |
| 641 | wl_client_post_no_memory(client); |
| 642 | return; |
| 643 | } |
| 644 | |
Kristian Høgsberg | 442a5fa | 2013-07-03 18:13:33 -0400 | [diff] [blame] | 645 | wl_resource_set_implementation(resource, |
| 646 | &test_implementation, test, NULL); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 647 | |
| 648 | notify_pointer_position(test, resource); |
| 649 | } |
| 650 | |
| 651 | static void |
| 652 | idle_launch_client(void *data) |
| 653 | { |
| 654 | struct weston_test *test = data; |
| 655 | pid_t pid; |
| 656 | sigset_t allsigs; |
| 657 | char *path; |
| 658 | |
| 659 | path = getenv("WESTON_TEST_CLIENT_PATH"); |
| 660 | if (path == NULL) |
Pekka Paalanen | f72e479 | 2013-11-21 16:23:57 +0200 | [diff] [blame] | 661 | return; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 662 | pid = fork(); |
| 663 | if (pid == -1) |
| 664 | exit(EXIT_FAILURE); |
| 665 | if (pid == 0) { |
| 666 | sigfillset(&allsigs); |
| 667 | sigprocmask(SIG_UNBLOCK, &allsigs, NULL); |
| 668 | execl(path, path, NULL); |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 669 | weston_log("compositor: executing '%s' failed: %s\n", path, |
| 670 | strerror(errno)); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 671 | exit(EXIT_FAILURE); |
| 672 | } |
| 673 | |
| 674 | test->process.pid = pid; |
| 675 | test->process.cleanup = test_client_sigchld; |
| 676 | weston_watch_process(&test->process); |
| 677 | } |
| 678 | |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame^] | 679 | static void |
| 680 | client_thread_cleanup(void *data_) |
| 681 | { |
| 682 | struct wet_testsuite_data *data = data_; |
| 683 | |
| 684 | close(data->thread_event_pipe); |
| 685 | data->thread_event_pipe = -1; |
| 686 | } |
| 687 | |
| 688 | static void * |
| 689 | client_thread_routine(void *data_) |
| 690 | { |
| 691 | struct wet_testsuite_data *data = data_; |
| 692 | |
| 693 | pthread_setname_np(pthread_self(), "client"); |
| 694 | pthread_cleanup_push(client_thread_cleanup, data); |
| 695 | data->run(data); |
| 696 | pthread_cleanup_pop(true); |
| 697 | |
| 698 | return NULL; |
| 699 | } |
| 700 | |
| 701 | static void |
| 702 | client_thread_join(struct weston_test *test) |
| 703 | { |
| 704 | assert(test->client_source); |
| 705 | |
| 706 | pthread_join(test->client_thread, NULL); |
| 707 | wl_event_source_remove(test->client_source); |
| 708 | test->client_source = NULL; |
| 709 | |
| 710 | weston_log_scope_printf(test->log, "Test thread reaped.\n"); |
| 711 | } |
| 712 | |
| 713 | static int |
| 714 | handle_client_thread_event(int fd, uint32_t mask, void *data_) |
| 715 | { |
| 716 | struct weston_test *test = data_; |
| 717 | |
| 718 | weston_log_scope_printf(test->log, |
| 719 | "Received thread event mask 0x%x\n", mask); |
| 720 | |
| 721 | if (mask != WL_EVENT_HANGUP) |
| 722 | weston_log("%s: unexpected event %u\n", __func__, mask); |
| 723 | |
| 724 | client_thread_join(test); |
| 725 | weston_compositor_exit(test->compositor); |
| 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static int |
| 731 | create_client_thread(struct weston_test *test, struct wet_testsuite_data *data) |
| 732 | { |
| 733 | struct wl_event_loop *loop; |
| 734 | int pipefd[2] = { -1, -1 }; |
| 735 | sigset_t saved; |
| 736 | sigset_t blocked; |
| 737 | int ret; |
| 738 | |
| 739 | weston_log_scope_printf(test->log, "Creating a thread for running tests...\n"); |
| 740 | |
| 741 | if (pipe2(pipefd, O_CLOEXEC | O_NONBLOCK) < 0) { |
| 742 | weston_log("Creating pipe for a client thread failed: %s\n", |
| 743 | strerror(errno)); |
| 744 | return -1; |
| 745 | } |
| 746 | |
| 747 | loop = wl_display_get_event_loop(test->compositor->wl_display); |
| 748 | test->client_source = wl_event_loop_add_fd(loop, pipefd[0], |
| 749 | WL_EVENT_READABLE, |
| 750 | handle_client_thread_event, |
| 751 | test); |
| 752 | close(pipefd[0]); |
| 753 | |
| 754 | if (!test->client_source) { |
| 755 | weston_log("Adding client thread fd to event loop failed.\n"); |
| 756 | goto out_pipe; |
| 757 | } |
| 758 | |
| 759 | data->thread_event_pipe = pipefd[1]; |
| 760 | |
| 761 | /* Ensure we don't accidentally get signals to the thread. */ |
| 762 | sigfillset(&blocked); |
| 763 | sigdelset(&blocked, SIGSEGV); |
| 764 | sigdelset(&blocked, SIGFPE); |
| 765 | sigdelset(&blocked, SIGILL); |
| 766 | sigdelset(&blocked, SIGCONT); |
| 767 | sigdelset(&blocked, SIGSYS); |
| 768 | if (pthread_sigmask(SIG_BLOCK, &blocked, &saved) != 0) |
| 769 | goto out_source; |
| 770 | |
| 771 | ret = pthread_create(&test->client_thread, NULL, |
| 772 | client_thread_routine, data); |
| 773 | |
| 774 | pthread_sigmask(SIG_SETMASK, &saved, NULL); |
| 775 | |
| 776 | if (ret != 0) { |
| 777 | weston_log("Creating client thread failed: %s (%d)\n", |
| 778 | strerror(ret), ret); |
| 779 | goto out_source; |
| 780 | } |
| 781 | |
| 782 | return 0; |
| 783 | |
| 784 | out_source: |
| 785 | data->thread_event_pipe = -1; |
| 786 | wl_event_source_remove(test->client_source); |
| 787 | test->client_source = NULL; |
| 788 | |
| 789 | out_pipe: |
| 790 | close(pipefd[1]); |
| 791 | |
| 792 | return -1; |
| 793 | } |
| 794 | |
| 795 | static void |
| 796 | idle_launch_testsuite(void *test_) |
| 797 | { |
| 798 | struct weston_test *test = test_; |
| 799 | struct wet_testsuite_data *data = wet_testsuite_data_get(); |
| 800 | |
| 801 | if (!data) |
| 802 | return; |
| 803 | |
| 804 | switch (data->type) { |
| 805 | case TEST_TYPE_CLIENT: |
| 806 | if (create_client_thread(test, data) < 0) { |
| 807 | weston_log("Error: creating client thread for test suite failed.\n"); |
| 808 | weston_compositor_exit_with_code(test->compositor, |
| 809 | RESULT_HARD_ERROR); |
| 810 | } |
| 811 | break; |
| 812 | |
| 813 | case TEST_TYPE_PLUGIN: |
| 814 | data->compositor = test->compositor; |
| 815 | weston_log_scope_printf(test->log, |
| 816 | "Running tests from idle handler...\n"); |
| 817 | data->run(data); |
| 818 | weston_compositor_exit(test->compositor); |
| 819 | break; |
| 820 | |
| 821 | case TEST_TYPE_STANDALONE: |
| 822 | weston_log("Error: unknown test internal type %d.\n", |
| 823 | data->type); |
| 824 | weston_compositor_exit_with_code(test->compositor, |
| 825 | RESULT_HARD_ERROR); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | static void |
| 830 | handle_compositor_destroy(struct wl_listener *listener, |
| 831 | void *weston_compositor) |
| 832 | { |
| 833 | struct weston_test *test; |
| 834 | |
| 835 | test = wl_container_of(listener, test, destroy_listener); |
| 836 | |
| 837 | if (test->client_source) { |
| 838 | weston_log_scope_printf(test->log, "Cancelling client thread...\n"); |
| 839 | pthread_cancel(test->client_thread); |
| 840 | client_thread_join(test); |
| 841 | } |
| 842 | |
| 843 | weston_log_scope_destroy(test->log); |
| 844 | test->log = NULL; |
| 845 | } |
| 846 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 847 | WL_EXPORT int |
Quentin Glidic | 8af2bec | 2016-12-02 14:21:46 +0100 | [diff] [blame] | 848 | wet_module_init(struct weston_compositor *ec, |
| 849 | int *argc, char *argv[]) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 850 | { |
| 851 | struct weston_test *test; |
| 852 | struct wl_event_loop *loop; |
| 853 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 854 | test = zalloc(sizeof *test); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 855 | if (test == NULL) |
| 856 | return -1; |
| 857 | |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame^] | 858 | if (!weston_compositor_add_destroy_listener_once(ec, |
| 859 | &test->destroy_listener, |
| 860 | handle_compositor_destroy)) { |
| 861 | free(test); |
| 862 | return 0; |
| 863 | } |
| 864 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 865 | test->compositor = ec; |
Quentin Glidic | 8268157 | 2016-12-17 13:40:51 +0100 | [diff] [blame] | 866 | weston_layer_init(&test->layer, ec); |
| 867 | weston_layer_set_position(&test->layer, WESTON_LAYER_POSITION_CURSOR - 1); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 868 | |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame^] | 869 | test->log = weston_compositor_add_log_scope(ec, "test-harness-plugin", |
| 870 | "weston-test plugin's own actions", |
| 871 | NULL, NULL, NULL); |
| 872 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 873 | if (wl_global_create(ec->wl_display, &weston_test_interface, 1, |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 874 | test, bind_test) == NULL) |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame^] | 875 | goto out_free; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 876 | |
Alexandros Frantzis | 85f8432 | 2018-01-26 18:48:00 +0200 | [diff] [blame] | 877 | if (test_seat_init(test) == -1) |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame^] | 878 | goto out_free; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 879 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 880 | loop = wl_display_get_event_loop(ec->wl_display); |
| 881 | wl_event_loop_add_idle(loop, idle_launch_client, test); |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame^] | 882 | wl_event_loop_add_idle(loop, idle_launch_testsuite, test); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 883 | |
| 884 | return 0; |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame^] | 885 | |
| 886 | out_free: |
| 887 | wl_list_remove(&test->destroy_listener.link); |
| 888 | free(test); |
| 889 | return -1; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 890 | } |