blob: f8db286b855ba004d338c2ce18f0889e50caca39 [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;
Marek Chalupac3c3fc42015-03-30 09:17:40 -040059 struct weston_seat seat;
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -050060 struct weston_touch_device *touch_device[MAX_TOUCH_DEVICES];
61 int nr_touch_devices;
Alexandros Frantzis85f84322018-01-26 18:48:00 +020062 bool is_seat_initialized;
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020063
64 pthread_t client_thread;
65 struct wl_event_source *client_source;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080066};
67
68struct weston_test_surface {
69 struct weston_surface *surface;
Pekka Paalanen7514bf72021-05-21 16:03:08 +030070 struct wl_listener surface_destroy_listener;
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
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -050077touch_device_add(struct weston_test *test)
78{
79 char buf[128];
80 int i = test->nr_touch_devices;
81
82 assert(i < MAX_TOUCH_DEVICES);
83 assert(!test->touch_device[i]);
84
85 snprintf(buf, sizeof buf, "test-touch-device-%d", i);
86
87 test->touch_device[i] = weston_touch_create_touch_device(
88 test->seat.touch_state, buf, NULL, NULL);
89 test->nr_touch_devices++;
90}
91
92static void
93touch_device_remove(struct weston_test *test)
94{
95 int i = test->nr_touch_devices - 1;
96
97 assert(i >= 0);
98 assert(test->touch_device[i]);
99 weston_touch_device_destroy(test->touch_device[i]);
100 test->touch_device[i] = NULL;
101 --test->nr_touch_devices;
102}
103
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200104static int
105test_seat_init(struct weston_test *test)
106{
Pekka Paalanenf39249a2018-02-26 14:14:14 +0200107 assert(!test->is_seat_initialized &&
108 "Trying to add already added test seat");
109
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200110 /* create our own seat */
111 weston_seat_init(&test->seat, test->compositor, "test-seat");
112 test->is_seat_initialized = true;
113
114 /* add devices */
115 weston_seat_init_pointer(&test->seat);
116 if (weston_seat_init_keyboard(&test->seat, NULL) < 0)
117 return -1;
118 weston_seat_init_touch(&test->seat);
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500119 touch_device_add(test);
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200120
121 return 0;
122}
123
Pekka Paalanenf39249a2018-02-26 14:14:14 +0200124static void
125test_seat_release(struct weston_test *test)
126{
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500127 while (test->nr_touch_devices > 0)
128 touch_device_remove(test);
129
Pekka Paalanenf39249a2018-02-26 14:14:14 +0200130 assert(test->is_seat_initialized &&
131 "Trying to release already released test seat");
132 test->is_seat_initialized = false;
133 weston_seat_release(&test->seat);
134 memset(&test->seat, 0, sizeof test->seat);
135}
136
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800137static struct weston_seat *
138get_seat(struct weston_test *test)
139{
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400140 return &test->seat;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800141}
142
143static void
144notify_pointer_position(struct weston_test *test, struct wl_resource *resource)
145{
146 struct weston_seat *seat = get_seat(test);
Derek Foreman1281a362015-07-31 16:55:32 -0500147 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800148
Derek Foremanf6a65922015-02-24 09:32:14 -0600149 weston_test_send_pointer_position(resource, pointer->x, pointer->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800150}
151
152static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200153test_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800154{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200155 struct weston_test_surface *test_surface = surface->committed_private;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800156 struct weston_test *test = test_surface->test;
157
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300158 if (wl_list_empty(&test_surface->view->layer_link.link))
159 weston_layer_entry_insert(&test->layer.view_list,
160 &test_surface->view->layer_link);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800161
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600162 weston_view_set_position(test_surface->view,
163 test_surface->x, test_surface->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800164
Kristian Høgsbergace0a392013-11-13 21:55:57 -0800165 weston_view_update_transform(test_surface->view);
Armin Krezovićd0cf4412016-06-30 06:04:32 +0200166
167 test_surface->surface->is_mapped = true;
168 test_surface->view->is_mapped = true;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800169}
170
Pekka Paalanen7514bf72021-05-21 16:03:08 +0300171static int
172test_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
173{
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +0200174 return snprintf(buf, len, "test suite surface");
Pekka Paalanen7514bf72021-05-21 16:03:08 +0300175}
176
177static void
178test_surface_destroy(struct weston_test_surface *test_surface)
179{
180 weston_view_destroy(test_surface->view);
181
182 test_surface->surface->committed = NULL;
183 test_surface->surface->committed_private = NULL;
184 weston_surface_set_label_func(test_surface->surface, NULL);
185
186 wl_list_remove(&test_surface->surface_destroy_listener.link);
187 free(test_surface);
188}
189
190static void
191test_surface_handle_surface_destroy(struct wl_listener *l, void *data)
192{
193 struct weston_test_surface *test_surface =
194 wl_container_of(l, test_surface, surface_destroy_listener);
195
196 assert(test_surface->surface == data);
197
198 test_surface_destroy(test_surface);
199}
200
201static struct weston_test_surface *
202weston_test_surface_create(struct wl_resource *test_resource,
203 struct weston_surface *surface)
204{
205 struct wl_client *client = wl_resource_get_client(test_resource);
206 struct wl_resource *display_resource;
207 struct weston_test_surface *test_surface;
208
209 test_surface = zalloc(sizeof *test_surface);
210 if (!test_surface)
211 goto err_post_no_mem;
212
213 test_surface->surface = surface;
214 test_surface->test = wl_resource_get_user_data(test_resource);
215
216 test_surface->view = weston_view_create(surface);
217 if (!test_surface->view)
218 goto err_free_surface;
219
220 /* Protocol does not define this error so abuse wl_display */
221 display_resource = wl_client_get_object(client, 1);
222 if (weston_surface_set_role(surface, "weston_test_surface",
223 display_resource,
224 WL_DISPLAY_ERROR_INVALID_OBJECT) < 0)
225 goto err_free_view;
226
227 surface->committed_private = test_surface;
228 surface->committed = test_surface_committed;
229 weston_surface_set_label_func(surface, test_surface_get_label);
230
231 test_surface->surface_destroy_listener.notify =
232 test_surface_handle_surface_destroy;
233 wl_signal_add(&surface->destroy_signal,
234 &test_surface->surface_destroy_listener);
235
236 return test_surface;
237
238err_free_view:
239 weston_view_destroy(test_surface->view);
240
241err_free_surface:
242 free(test_surface);
243
244err_post_no_mem:
245 wl_resource_post_no_memory(test_resource);
246 return NULL;
247}
248
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800249static void
250move_surface(struct wl_client *client, struct wl_resource *resource,
251 struct wl_resource *surface_resource,
252 int32_t x, int32_t y)
253{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400254 struct weston_surface *surface =
255 wl_resource_get_user_data(surface_resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800256 struct weston_test_surface *test_surface;
Pekka Paalanen7514bf72021-05-21 16:03:08 +0300257 struct wl_resource *display_resource;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800258
Pekka Paalanen7514bf72021-05-21 16:03:08 +0300259 if (surface->committed &&
260 surface->committed != test_surface_committed) {
261 display_resource = wl_client_get_object(client, 1);
262 wl_resource_post_error(display_resource,
263 WL_DISPLAY_ERROR_INVALID_OBJECT,
264 "weston_test.move_surface: wl_surface@%u has a role.",
265 wl_resource_get_id(surface_resource));
266 return;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800267 }
268
Pekka Paalanen7514bf72021-05-21 16:03:08 +0300269 test_surface = surface->committed_private;
270 if (!test_surface)
271 test_surface = weston_test_surface_create(resource, surface);
272 if (!test_surface)
273 return;
274
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800275 test_surface->x = x;
276 test_surface->y = y;
277}
278
279static void
280move_pointer(struct wl_client *client, struct wl_resource *resource,
Alexandros Frantzis21808582017-12-13 13:27:55 +0200281 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800282 int32_t x, int32_t y)
283{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400284 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800285 struct weston_seat *seat = get_seat(test);
Derek Foreman1281a362015-07-31 16:55:32 -0500286 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jonas Ådahld2510102014-10-05 21:39:14 +0200287 struct weston_pointer_motion_event event = { 0 };
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200288 struct timespec time;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800289
Jonas Ådahld2510102014-10-05 21:39:14 +0200290 event = (struct weston_pointer_motion_event) {
291 .mask = WESTON_POINTER_MOTION_REL,
292 .dx = wl_fixed_to_double(wl_fixed_from_int(x) - pointer->x),
293 .dy = wl_fixed_to_double(wl_fixed_from_int(y) - pointer->y),
294 };
295
Alexandros Frantzis21808582017-12-13 13:27:55 +0200296 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200297
298 notify_motion(seat, &time, &event);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800299
300 notify_pointer_position(test, resource);
301}
302
303static void
304send_button(struct wl_client *client, struct wl_resource *resource,
Alexandros Frantzis21808582017-12-13 13:27:55 +0200305 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800306 int32_t button, uint32_t state)
307{
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200308 struct timespec time;
309
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400310 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800311 struct weston_seat *seat = get_seat(test);
312
Alexandros Frantzis21808582017-12-13 13:27:55 +0200313 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200314
315 notify_button(seat, &time, button, state);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800316}
317
318static void
Alexandros Frantzisb0341ae2017-12-18 12:16:55 +0200319send_axis(struct wl_client *client, struct wl_resource *resource,
320 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
321 uint32_t axis, wl_fixed_t value)
322{
323 struct weston_test *test = wl_resource_get_user_data(resource);
324 struct weston_seat *seat = get_seat(test);
325 struct timespec time;
326 struct weston_pointer_axis_event axis_event;
327
328 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
329 axis_event.axis = axis;
330 axis_event.value = wl_fixed_to_double(value);
331 axis_event.has_discrete = false;
332 axis_event.discrete = 0;
333
334 notify_axis(seat, &time, &axis_event);
335}
336
337static void
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800338activate_surface(struct wl_client *client, struct wl_resource *resource,
339 struct wl_resource *surface_resource)
340{
341 struct weston_surface *surface = surface_resource ?
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400342 wl_resource_get_user_data(surface_resource) : NULL;
343 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800344 struct weston_seat *seat;
Derek Foreman1281a362015-07-31 16:55:32 -0500345 struct weston_keyboard *keyboard;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800346
347 seat = get_seat(test);
Derek Foreman1281a362015-07-31 16:55:32 -0500348 keyboard = weston_seat_get_keyboard(seat);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800349 if (surface) {
Bryce Harrington260c2ff2016-06-29 19:04:06 -0700350 weston_seat_set_keyboard_focus(seat, surface);
Derek Foreman1281a362015-07-31 16:55:32 -0500351 notify_keyboard_focus_in(seat, &keyboard->keys,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800352 STATE_UPDATE_AUTOMATIC);
353 }
354 else {
355 notify_keyboard_focus_out(seat);
Bryce Harrington260c2ff2016-06-29 19:04:06 -0700356 weston_seat_set_keyboard_focus(seat, surface);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800357 }
358}
359
360static void
361send_key(struct wl_client *client, struct wl_resource *resource,
Alexandros Frantzisdae224c2017-12-13 13:27:57 +0200362 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800363 uint32_t key, enum wl_keyboard_key_state state)
364{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400365 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800366 struct weston_seat *seat = get_seat(test);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200367 struct timespec time;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800368
Alexandros Frantzisdae224c2017-12-13 13:27:57 +0200369 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200370
371 notify_key(seat, &time, key, state, STATE_UPDATE_AUTOMATIC);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800372}
373
Marek Chalupac8daf772015-03-30 06:37:55 -0400374static void
375device_release(struct wl_client *client,
376 struct wl_resource *resource, const char *device)
377{
378 struct weston_test *test = wl_resource_get_user_data(resource);
379 struct weston_seat *seat = get_seat(test);
380
381 if (strcmp(device, "pointer") == 0) {
382 weston_seat_release_pointer(seat);
383 } else if (strcmp(device, "keyboard") == 0) {
384 weston_seat_release_keyboard(seat);
385 } else if (strcmp(device, "touch") == 0) {
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500386 touch_device_remove(test);
Marek Chalupac8daf772015-03-30 06:37:55 -0400387 weston_seat_release_touch(seat);
388 } else if (strcmp(device, "seat") == 0) {
Pekka Paalanenf39249a2018-02-26 14:14:14 +0200389 test_seat_release(test);
Marek Chalupac8daf772015-03-30 06:37:55 -0400390 } else {
391 assert(0 && "Unsupported device");
392 }
393}
394
395static void
396device_add(struct wl_client *client,
397 struct wl_resource *resource, const char *device)
398{
399 struct weston_test *test = wl_resource_get_user_data(resource);
400 struct weston_seat *seat = get_seat(test);
401
402 if (strcmp(device, "pointer") == 0) {
403 weston_seat_init_pointer(seat);
404 } else if (strcmp(device, "keyboard") == 0) {
405 weston_seat_init_keyboard(seat, NULL);
406 } else if (strcmp(device, "touch") == 0) {
407 weston_seat_init_touch(seat);
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500408 touch_device_add(test);
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200409 } else if (strcmp(device, "seat") == 0) {
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200410 test_seat_init(test);
Marek Chalupac8daf772015-03-30 06:37:55 -0400411 } else {
412 assert(0 && "Unsupported device");
413 }
414}
415
Alexandros Frantzisc20b5802017-12-13 13:27:58 +0200416static void
417send_touch(struct wl_client *client, struct wl_resource *resource,
418 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
419 int32_t touch_id, wl_fixed_t x, wl_fixed_t y, uint32_t touch_type)
420{
421 struct weston_test *test = wl_resource_get_user_data(resource);
Pekka Paalanenbcbce332018-02-26 14:43:00 +0200422 struct weston_touch_device *device = test->touch_device[0];
Alexandros Frantzisc20b5802017-12-13 13:27:58 +0200423 struct timespec time;
424
Pekka Paalanenbcbce332018-02-26 14:43:00 +0200425 assert(device);
426
Alexandros Frantzisc20b5802017-12-13 13:27:58 +0200427 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
428
Pekka Paalanenbcbce332018-02-26 14:43:00 +0200429 notify_touch(device, &time, touch_id, wl_fixed_to_double(x),
Alexandros Frantzisc20b5802017-12-13 13:27:58 +0200430 wl_fixed_to_double(y), touch_type);
431}
432
Derek Foremanf6a65922015-02-24 09:32:14 -0600433static const struct weston_test_interface test_implementation = {
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800434 move_surface,
435 move_pointer,
436 send_button,
Alexandros Frantzisb0341ae2017-12-18 12:16:55 +0200437 send_axis,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800438 activate_surface,
Neil Roberts40c0c3f2013-10-29 20:13:45 +0000439 send_key,
Marek Chalupac8daf772015-03-30 06:37:55 -0400440 device_release,
441 device_add,
Alexandros Frantzisc20b5802017-12-13 13:27:58 +0200442 send_touch,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800443};
444
445static void
446bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id)
447{
448 struct weston_test *test = data;
449 struct wl_resource *resource;
450
Derek Foremanf6a65922015-02-24 09:32:14 -0600451 resource = wl_resource_create(client, &weston_test_interface, 1, id);
Marek Chalupa42ebdda2014-07-11 12:33:02 +0200452 if (!resource) {
453 wl_client_post_no_memory(client);
454 return;
455 }
456
Kristian Høgsberg442a5fa2013-07-03 18:13:33 -0400457 wl_resource_set_implementation(resource,
458 &test_implementation, test, NULL);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800459
460 notify_pointer_position(test, resource);
461}
462
463static void
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200464client_thread_cleanup(void *data_)
465{
466 struct wet_testsuite_data *data = data_;
467
468 close(data->thread_event_pipe);
469 data->thread_event_pipe = -1;
470}
471
472static void *
473client_thread_routine(void *data_)
474{
475 struct wet_testsuite_data *data = data_;
476
477 pthread_setname_np(pthread_self(), "client");
478 pthread_cleanup_push(client_thread_cleanup, data);
479 data->run(data);
480 pthread_cleanup_pop(true);
481
482 return NULL;
483}
484
485static void
486client_thread_join(struct weston_test *test)
487{
488 assert(test->client_source);
489
490 pthread_join(test->client_thread, NULL);
491 wl_event_source_remove(test->client_source);
492 test->client_source = NULL;
493
494 weston_log_scope_printf(test->log, "Test thread reaped.\n");
495}
496
497static int
498handle_client_thread_event(int fd, uint32_t mask, void *data_)
499{
500 struct weston_test *test = data_;
501
502 weston_log_scope_printf(test->log,
503 "Received thread event mask 0x%x\n", mask);
504
505 if (mask != WL_EVENT_HANGUP)
506 weston_log("%s: unexpected event %u\n", __func__, mask);
507
508 client_thread_join(test);
509 weston_compositor_exit(test->compositor);
510
511 return 0;
512}
513
514static int
515create_client_thread(struct weston_test *test, struct wet_testsuite_data *data)
516{
517 struct wl_event_loop *loop;
518 int pipefd[2] = { -1, -1 };
519 sigset_t saved;
520 sigset_t blocked;
521 int ret;
522
523 weston_log_scope_printf(test->log, "Creating a thread for running tests...\n");
524
525 if (pipe2(pipefd, O_CLOEXEC | O_NONBLOCK) < 0) {
526 weston_log("Creating pipe for a client thread failed: %s\n",
527 strerror(errno));
528 return -1;
529 }
530
531 loop = wl_display_get_event_loop(test->compositor->wl_display);
532 test->client_source = wl_event_loop_add_fd(loop, pipefd[0],
533 WL_EVENT_READABLE,
534 handle_client_thread_event,
535 test);
536 close(pipefd[0]);
537
538 if (!test->client_source) {
539 weston_log("Adding client thread fd to event loop failed.\n");
540 goto out_pipe;
541 }
542
543 data->thread_event_pipe = pipefd[1];
544
545 /* Ensure we don't accidentally get signals to the thread. */
546 sigfillset(&blocked);
547 sigdelset(&blocked, SIGSEGV);
548 sigdelset(&blocked, SIGFPE);
549 sigdelset(&blocked, SIGILL);
550 sigdelset(&blocked, SIGCONT);
551 sigdelset(&blocked, SIGSYS);
552 if (pthread_sigmask(SIG_BLOCK, &blocked, &saved) != 0)
553 goto out_source;
554
555 ret = pthread_create(&test->client_thread, NULL,
556 client_thread_routine, data);
557
558 pthread_sigmask(SIG_SETMASK, &saved, NULL);
559
560 if (ret != 0) {
561 weston_log("Creating client thread failed: %s (%d)\n",
562 strerror(ret), ret);
563 goto out_source;
564 }
565
566 return 0;
567
568out_source:
569 data->thread_event_pipe = -1;
570 wl_event_source_remove(test->client_source);
571 test->client_source = NULL;
572
573out_pipe:
574 close(pipefd[1]);
575
576 return -1;
577}
578
579static void
580idle_launch_testsuite(void *test_)
581{
582 struct weston_test *test = test_;
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -0300583 struct wet_testsuite_data *data = weston_compositor_get_test_data(test->compositor);
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200584
585 if (!data)
586 return;
587
588 switch (data->type) {
589 case TEST_TYPE_CLIENT:
590 if (create_client_thread(test, data) < 0) {
591 weston_log("Error: creating client thread for test suite failed.\n");
592 weston_compositor_exit_with_code(test->compositor,
593 RESULT_HARD_ERROR);
594 }
595 break;
596
597 case TEST_TYPE_PLUGIN:
598 data->compositor = test->compositor;
599 weston_log_scope_printf(test->log,
600 "Running tests from idle handler...\n");
601 data->run(data);
602 weston_compositor_exit(test->compositor);
603 break;
604
605 case TEST_TYPE_STANDALONE:
606 weston_log("Error: unknown test internal type %d.\n",
607 data->type);
608 weston_compositor_exit_with_code(test->compositor,
609 RESULT_HARD_ERROR);
610 }
611}
612
613static void
614handle_compositor_destroy(struct wl_listener *listener,
615 void *weston_compositor)
616{
617 struct weston_test *test;
618
619 test = wl_container_of(listener, test, destroy_listener);
620
Derek Foreman3759ad12022-01-19 10:40:06 -0600621 wl_list_remove(&test->destroy_listener.link);
622
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200623 if (test->client_source) {
624 weston_log_scope_printf(test->log, "Cancelling client thread...\n");
625 pthread_cancel(test->client_thread);
626 client_thread_join(test);
627 }
628
Guillaume Champagne7bce28b2020-02-02 19:59:52 -0500629 if (test->is_seat_initialized)
630 test_seat_release(test);
631
632 wl_list_remove(&test->layer.view_list.link);
633 wl_list_remove(&test->layer.link);
634
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200635 weston_log_scope_destroy(test->log);
Guillaume Champagne7bce28b2020-02-02 19:59:52 -0500636 free(test);
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200637}
638
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800639WL_EXPORT int
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100640wet_module_init(struct weston_compositor *ec,
641 int *argc, char *argv[])
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800642{
643 struct weston_test *test;
644 struct wl_event_loop *loop;
645
Peter Huttererf3d62272013-08-08 11:57:05 +1000646 test = zalloc(sizeof *test);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800647 if (test == NULL)
648 return -1;
649
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200650 if (!weston_compositor_add_destroy_listener_once(ec,
651 &test->destroy_listener,
652 handle_compositor_destroy)) {
653 free(test);
654 return 0;
655 }
656
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800657 test->compositor = ec;
Quentin Glidic82681572016-12-17 13:40:51 +0100658 weston_layer_init(&test->layer, ec);
659 weston_layer_set_position(&test->layer, WESTON_LAYER_POSITION_CURSOR - 1);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800660
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200661 test->log = weston_compositor_add_log_scope(ec, "test-harness-plugin",
662 "weston-test plugin's own actions",
663 NULL, NULL, NULL);
664
Derek Foremanf6a65922015-02-24 09:32:14 -0600665 if (wl_global_create(ec->wl_display, &weston_test_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400666 test, bind_test) == NULL)
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200667 goto out_free;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800668
Alexandros Frantzis85f84322018-01-26 18:48:00 +0200669 if (test_seat_init(test) == -1)
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200670 goto out_free;
Marek Chalupac3c3fc42015-03-30 09:17:40 -0400671
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800672 loop = wl_display_get_event_loop(ec->wl_display);
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200673 wl_event_loop_add_idle(loop, idle_launch_testsuite, test);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800674
675 return 0;
Pekka Paalanenbabb3b32019-11-01 14:02:15 +0200676
677out_free:
678 wl_list_remove(&test->destroy_listener.link);
679 free(test);
680 return -1;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800681}