blob: f019b0a409fac6f60a1615b00e2a2020cf373a72 [file] [log] [blame]
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -08001/*
2 * Copyright © 2012 Intel Corporation
3 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07004 * 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 Eoff65e7e7a2012-12-07 13:50:29 -080011 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070012 * 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 Eoff65e7e7a2012-12-07 13:50:29 -080024 */
25
Neil Roberts40c0c3f2013-10-29 20:13:45 +000026#include "config.h"
27
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030028#include <stdint.h>
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080029#include <stdlib.h>
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080030#include <assert.h>
31#include <signal.h>
32#include <unistd.h>
Marek Chalupac8daf772015-03-30 06:37:55 -040033#include <string.h>
Antonio Borneo39578632019-04-26 23:57:31 +020034#include <errno.h>
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020035#include <fcntl.h>
36#include <pthread.h>
Bryce Harringtona7680262014-11-19 17:18:34 -080037
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020038#include <libweston/libweston.h>
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020039#include <libweston/weston-log.h>
Marius Vlad5d649b62019-07-16 23:44:21 +030040#include "backend.h"
Marius Vlad0bf3f5a2019-07-10 17:32:42 +030041#include "libweston-internal.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030042#include "compositor/weston.h"
Derek Foremanf6a65922015-02-24 09:32:14 -060043#include "weston-test-server-protocol.h"
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020044#include "weston.h"
45#include "weston-testsuite-data.h"
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080046
Jon Cruz867d50e2015-06-15 15:37:10 -070047#include "shared/helpers.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020048#include "shared/timespec-util.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070049
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -050050#define MAX_TOUCH_DEVICES 32
51
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080052struct weston_test {
53 struct weston_compositor *compositor;
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020054 struct wl_listener destroy_listener;
55
56 struct weston_log_scope *log;
57
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080058 struct weston_layer layer;
59 struct weston_process process;
Marek Chalupac3c3fc42015-03-30 09:17:40 -040060 struct weston_seat seat;
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -050061 struct weston_touch_device *touch_device[MAX_TOUCH_DEVICES];
62 int nr_touch_devices;
Alexandros Frantzis85f84322018-01-26 18:48:00 +020063 bool is_seat_initialized;
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020064
65 pthread_t client_thread;
66 struct wl_event_source *client_source;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080067};
68
69struct weston_test_surface {
70 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -050071 struct weston_view *view;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080072 int32_t x, y;
73 struct weston_test *test;
74};
75
76static void
77test_client_sigchld(struct weston_process *process, int status)
78{
79 struct weston_test *test =
80 container_of(process, struct weston_test, process);
81
Pekka Paalanenbe61a1f2019-03-14 16:56:27 +020082 /* Chain up from weston-test-runner's exit code so that ninja
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010083 * 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 Eoff65e7e7a2012-12-07 13:50:29 -080088 assert(status == 0);
89
Pekka Paalanena37920e2019-02-06 10:53:35 +020090 weston_compositor_exit(test->compositor);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080091}
92
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -050093static void
94touch_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
109static void
110touch_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 Frantzis85f84322018-01-26 18:48:00 +0200121static int
122test_seat_init(struct weston_test *test)
123{
Pekka Paalanenf39249a2018-02-26 14:14:14 +0200124 assert(!test->is_seat_initialized &&
125 "Trying to add already added test seat");
126
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200127 /* 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é-Boulianne6ef59c92017-11-28 20:42:47 -0500136 touch_device_add(test);
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200137
138 return 0;
139}
140
Pekka Paalanenf39249a2018-02-26 14:14:14 +0200141static void
142test_seat_release(struct weston_test *test)
143{
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500144 while (test->nr_touch_devices > 0)
145 touch_device_remove(test);
146
Pekka Paalanenf39249a2018-02-26 14:14:14 +0200147 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 Eoff65e7e7a2012-12-07 13:50:29 -0800154static struct weston_seat *
155get_seat(struct weston_test *test)
156{
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400157 return &test->seat;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800158}
159
160static void
161notify_pointer_position(struct weston_test *test, struct wl_resource *resource)
162{
163 struct weston_seat *seat = get_seat(test);
Derek Foreman1281a362015-07-31 16:55:32 -0500164 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800165
Derek Foremanf6a65922015-02-24 09:32:14 -0600166 weston_test_send_pointer_position(resource, pointer->x, pointer->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800167}
168
169static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200170test_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800171{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200172 struct weston_test_surface *test_surface = surface->committed_private;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800173 struct weston_test *test = test_surface->test;
174
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300175 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 Eoff65e7e7a2012-12-07 13:50:29 -0800178
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600179 weston_view_set_position(test_surface->view,
180 test_surface->x, test_surface->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800181
Kristian Høgsbergace0a392013-11-13 21:55:57 -0800182 weston_view_update_transform(test_surface->view);
Armin Krezovićd0cf4412016-06-30 06:04:32 +0200183
184 test_surface->surface->is_mapped = true;
185 test_surface->view->is_mapped = true;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800186}
187
188static void
189move_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øgsberg8f7f4832013-06-25 16:18:35 -0400193 struct weston_surface *surface =
194 wl_resource_get_user_data(surface_resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800195 struct weston_test_surface *test_surface;
196
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200197 test_surface = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500198 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 Glidic2edc3d52016-08-12 10:41:33 +0200212 surface->committed_private = test_surface;
213 surface->committed = test_surface_committed;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800214 }
215
216 test_surface->surface = surface;
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400217 test_surface->test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800218 test_surface->x = x;
219 test_surface->y = y;
220}
221
222static void
223move_pointer(struct wl_client *client, struct wl_resource *resource,
Alexandros Frantzis21808582017-12-13 13:27:55 +0200224 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800225 int32_t x, int32_t y)
226{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400227 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800228 struct weston_seat *seat = get_seat(test);
Derek Foreman1281a362015-07-31 16:55:32 -0500229 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +0200230 struct weston_pointer_motion_event event = { 0 };
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200231 struct timespec time;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800232
Jonas Ådahld2510102014-10-05 21:39:14 +0200233 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 Frantzis21808582017-12-13 13:27:55 +0200239 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200240
241 notify_motion(seat, &time, &event);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800242
243 notify_pointer_position(test, resource);
244}
245
246static void
247send_button(struct wl_client *client, struct wl_resource *resource,
Alexandros Frantzis21808582017-12-13 13:27:55 +0200248 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800249 int32_t button, uint32_t state)
250{
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200251 struct timespec time;
252
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400253 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800254 struct weston_seat *seat = get_seat(test);
255
Alexandros Frantzis21808582017-12-13 13:27:55 +0200256 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200257
258 notify_button(seat, &time, button, state);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800259}
260
261static void
Alexandros Frantzisb0341ae2017-12-18 12:16:55 +0200262send_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
280static void
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800281activate_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øgsberg8f7f4832013-06-25 16:18:35 -0400285 wl_resource_get_user_data(surface_resource) : NULL;
286 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800287 struct weston_seat *seat;
Derek Foreman1281a362015-07-31 16:55:32 -0500288 struct weston_keyboard *keyboard;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800289
290 seat = get_seat(test);
Derek Foreman1281a362015-07-31 16:55:32 -0500291 keyboard = weston_seat_get_keyboard(seat);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800292 if (surface) {
Bryce Harrington260c2ff2016-06-29 19:04:06 -0700293 weston_seat_set_keyboard_focus(seat, surface);
Derek Foreman1281a362015-07-31 16:55:32 -0500294 notify_keyboard_focus_in(seat, &keyboard->keys,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800295 STATE_UPDATE_AUTOMATIC);
296 }
297 else {
298 notify_keyboard_focus_out(seat);
Bryce Harrington260c2ff2016-06-29 19:04:06 -0700299 weston_seat_set_keyboard_focus(seat, surface);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800300 }
301}
302
303static void
304send_key(struct wl_client *client, struct wl_resource *resource,
Alexandros Frantzisdae224c2017-12-13 13:27:57 +0200305 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800306 uint32_t key, enum wl_keyboard_key_state state)
307{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400308 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800309 struct weston_seat *seat = get_seat(test);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200310 struct timespec time;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800311
Alexandros Frantzisdae224c2017-12-13 13:27:57 +0200312 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200313
314 notify_key(seat, &time, key, state, STATE_UPDATE_AUTOMATIC);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800315}
316
Marek Chalupac8daf772015-03-30 06:37:55 -0400317static void
318device_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é-Boulianne6ef59c92017-11-28 20:42:47 -0500329 touch_device_remove(test);
Marek Chalupac8daf772015-03-30 06:37:55 -0400330 weston_seat_release_touch(seat);
331 } else if (strcmp(device, "seat") == 0) {
Pekka Paalanenf39249a2018-02-26 14:14:14 +0200332 test_seat_release(test);
Marek Chalupac8daf772015-03-30 06:37:55 -0400333 } else {
334 assert(0 && "Unsupported device");
335 }
336}
337
338static void
339device_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é-Boulianne6ef59c92017-11-28 20:42:47 -0500351 touch_device_add(test);
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200352 } else if (strcmp(device, "seat") == 0) {
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200353 test_seat_init(test);
Marek Chalupac8daf772015-03-30 06:37:55 -0400354 } else {
355 assert(0 && "Unsupported device");
356 }
357}
358
Bryce Harringtonf280d112015-05-05 15:13:20 -0700359enum weston_test_screenshot_outcome {
360 WESTON_TEST_SCREENSHOT_SUCCESS,
361 WESTON_TEST_SCREENSHOT_NO_MEMORY,
362 WESTON_TEST_SCREENSHOT_BAD_BUFFER
363 };
364
365typedef void (*weston_test_screenshot_done_func_t)(void *data,
366 enum weston_test_screenshot_outcome outcome);
367
368struct 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
376struct test_screenshot_frame_listener {
377 struct wl_listener listener;
378 struct weston_buffer *buffer;
Leandro Ribeiro442ffcb2019-11-27 12:47:19 -0300379 struct weston_output *output;
Bryce Harringtonf280d112015-05-05 15:13:20 -0700380 weston_test_screenshot_done_func_t done;
381 void *data;
382};
383
384static void
385copy_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
398static void
399copy_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
405static void
406copy_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
422static void
423copy_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
435static void
436copy_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
448static void
449test_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 Ribeiro442ffcb2019-11-27 12:47:19 -0300454 struct weston_output *output = l->output;
Bryce Harringtonf280d112015-05-05 15:13:20 -0700455 struct weston_compositor *compositor = output->compositor;
456 int32_t stride;
457 uint8_t *pixels, *d, *s;
458
Ankit Nautiyal93dde242019-07-08 11:46:42 +0530459 weston_output_disable_planes_decr(output);
Bryce Harringtonf280d112015-05-05 15:13:20 -0700460 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 Michael2ec5f2a2015-12-03 12:23:12 -0500470 /* FIXME: Needs to handle output transformations */
Bryce Harringtonf280d112015-05-05 15:13:20 -0700471
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
515static bool
516weston_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 Ribeiro442ffcb2019-11-27 12:47:19 -0300549 l->output = output;
Bryce Harringtonf280d112015-05-05 15:13:20 -0700550 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 Nautiyal93dde242019-07-08 11:46:42 +0530556 weston_output_disable_planes_incr(output);
Bryce Harringtonf280d112015-05-05 15:13:20 -0700557 weston_output_schedule_repaint(output);
558
559 return true;
560}
561
562static void
563capture_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 */
583static void
584capture_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 Paalanen055c1132017-03-27 16:31:25 +0300590 weston_head_from_resource(output_resource)->output;
Bryce Harringtonf280d112015-05-05 15:13:20 -0700591 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 Frantzisc20b5802017-12-13 13:27:58 +0200603static void
604send_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 Paalanenbcbce332018-02-26 14:43:00 +0200609 struct weston_touch_device *device = test->touch_device[0];
Alexandros Frantzisc20b5802017-12-13 13:27:58 +0200610 struct timespec time;
611
Pekka Paalanenbcbce332018-02-26 14:43:00 +0200612 assert(device);
613
Alexandros Frantzisc20b5802017-12-13 13:27:58 +0200614 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
615
Pekka Paalanenbcbce332018-02-26 14:43:00 +0200616 notify_touch(device, &time, touch_id, wl_fixed_to_double(x),
Alexandros Frantzisc20b5802017-12-13 13:27:58 +0200617 wl_fixed_to_double(y), touch_type);
618}
619
Derek Foremanf6a65922015-02-24 09:32:14 -0600620static const struct weston_test_interface test_implementation = {
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800621 move_surface,
622 move_pointer,
623 send_button,
Alexandros Frantzisb0341ae2017-12-18 12:16:55 +0200624 send_axis,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800625 activate_surface,
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000626 send_key,
Marek Chalupac8daf772015-03-30 06:37:55 -0400627 device_release,
628 device_add,
Bryce Harringtonf280d112015-05-05 15:13:20 -0700629 capture_screenshot,
Alexandros Frantzisc20b5802017-12-13 13:27:58 +0200630 send_touch,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800631};
632
633static void
634bind_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 Foremanf6a65922015-02-24 09:32:14 -0600639 resource = wl_resource_create(client, &weston_test_interface, 1, id);
Marek Chalupa42ebdda2014-07-11 12:33:02 +0200640 if (!resource) {
641 wl_client_post_no_memory(client);
642 return;
643 }
644
Kristian Høgsberg442a5fa2013-07-03 18:13:33 -0400645 wl_resource_set_implementation(resource,
646 &test_implementation, test, NULL);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800647
648 notify_pointer_position(test, resource);
649}
650
651static void
652idle_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 Paalanenf72e4792013-11-21 16:23:57 +0200661 return;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800662 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 Borneo39578632019-04-26 23:57:31 +0200669 weston_log("compositor: executing '%s' failed: %s\n", path,
670 strerror(errno));
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800671 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 Paalanenbabb3b32019-11-01 14:02:15 +0200679static void
680client_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
688static void *
689client_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
701static void
702client_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
713static int
714handle_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
730static int
731create_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
784out_source:
785 data->thread_event_pipe = -1;
786 wl_event_source_remove(test->client_source);
787 test->client_source = NULL;
788
789out_pipe:
790 close(pipefd[1]);
791
792 return -1;
793}
794
795static void
796idle_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
829static void
830handle_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 Eoff65e7e7a2012-12-07 13:50:29 -0800847WL_EXPORT int
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100848wet_module_init(struct weston_compositor *ec,
849 int *argc, char *argv[])
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800850{
851 struct weston_test *test;
852 struct wl_event_loop *loop;
853
Peter Huttererf3d62272013-08-08 11:57:05 +1000854 test = zalloc(sizeof *test);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800855 if (test == NULL)
856 return -1;
857
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200858 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 Eoff65e7e7a2012-12-07 13:50:29 -0800865 test->compositor = ec;
Quentin Glidic82681572016-12-17 13:40:51 +0100866 weston_layer_init(&test->layer, ec);
867 weston_layer_set_position(&test->layer, WESTON_LAYER_POSITION_CURSOR - 1);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800868
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200869 test->log = weston_compositor_add_log_scope(ec, "test-harness-plugin",
870 "weston-test plugin's own actions",
871 NULL, NULL, NULL);
872
Derek Foremanf6a65922015-02-24 09:32:14 -0600873 if (wl_global_create(ec->wl_display, &weston_test_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400874 test, bind_test) == NULL)
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200875 goto out_free;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800876
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200877 if (test_seat_init(test) == -1)
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200878 goto out_free;
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400879
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800880 loop = wl_display_get_event_loop(ec->wl_display);
881 wl_event_loop_add_idle(loop, idle_launch_client, test);
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200882 wl_event_loop_add_idle(loop, idle_launch_testsuite, test);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800883
884 return 0;
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200885
886out_free:
887 wl_list_remove(&test->destroy_listener.link);
888 free(test);
889 return -1;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800890}