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