mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
* fix(portal): map scaled GNOME input through EIS regions Keep unsupported portals on legacy input and leave capture/uinput independent of EIS. Cache logical geometry without the native handle lock and reject geometry changes during recovery. Add native capability, handshake, geometry and lifecycle regression coverage with sanitizer checks; preserve existing non-portal paths. Refs #23. * fix(portal): keep version 4 capture settings in pixel coordinates Keep native, selectable and current screen rectangles in pixels with DPR 1 so legacy setting requests cannot implicitly downscale a scaled desktop. Leave Portal and EIS coordinate mapping local and unchanged. Add inventory/request/ack/sync roundtrip regression coverage, including repeated settings changes and restoring native output. Refs #23.
449 lines
18 KiB
C
449 lines
18 KiB
C
#define _GNU_SOURCE
|
|
#include <gio/gio.h>
|
|
#include <gio/gunixfdlist.h>
|
|
#include <assert.h>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
|
|
struct harness;
|
|
static struct harness *active_harness;
|
|
static void *pump_server_during_open(void *data);
|
|
static GVariant *test_call_with_unix_fd_list_sync(GDBusConnection *connection,
|
|
const gchar *bus_name, const gchar *object_path, const gchar *interface_name,
|
|
const gchar *method_name, GVariant *parameters, const GVariantType *reply_type,
|
|
GDBusCallFlags flags, gint timeout_msec, GUnixFDList *fd_list,
|
|
GUnixFDList **out_fd_list, GCancellable *cancellable, GError **error);
|
|
#define g_dbus_connection_call_with_unix_fd_list_sync test_call_with_unix_fd_list_sync
|
|
#include "../c_engine/portal_engine.c"
|
|
#undef g_dbus_connection_call_with_unix_fd_list_sync
|
|
#include <libeis.h>
|
|
|
|
#define CAP_MASK (EI_DEVICE_CAP_POINTER | EI_DEVICE_CAP_POINTER_ABSOLUTE | \
|
|
EI_DEVICE_CAP_BUTTON | EI_DEVICE_CAP_SCROLL | EI_DEVICE_CAP_KEYBOARD)
|
|
#define EVENT_CAP_MASK (EIS_DEVICE_CAP_POINTER | EIS_DEVICE_CAP_POINTER_ABSOLUTE | \
|
|
EIS_DEVICE_CAP_BUTTON | EIS_DEVICE_CAP_SCROLL | EIS_DEVICE_CAP_KEYBOARD)
|
|
|
|
static const unsigned capabilities[] = {
|
|
EIS_DEVICE_CAP_POINTER,
|
|
EIS_DEVICE_CAP_POINTER_ABSOLUTE,
|
|
EIS_DEVICE_CAP_BUTTON,
|
|
EIS_DEVICE_CAP_SCROLL,
|
|
EIS_DEVICE_CAP_KEYBOARD,
|
|
};
|
|
|
|
struct received {
|
|
enum eis_event_type type;
|
|
double x, y;
|
|
uint32_t code;
|
|
int pressed, dx, dy;
|
|
};
|
|
|
|
struct geometry {
|
|
uint32_t width, height, logical_width, logical_height, x, y;
|
|
};
|
|
|
|
struct harness {
|
|
const struct geometry *geometry;
|
|
struct uu_portal *portal;
|
|
struct eis *server;
|
|
struct eis_client *client;
|
|
struct eis_seat *seat;
|
|
struct eis_device *device;
|
|
struct received events[64];
|
|
size_t n_events;
|
|
unsigned frames, starts, stops, diagnostics;
|
|
unsigned connect_calls;
|
|
atomic_int stop_server_pump;
|
|
pthread_t server_pump_thread;
|
|
};
|
|
|
|
static GVariant *test_call_with_unix_fd_list_sync(GDBusConnection *connection,
|
|
const gchar *bus_name, const gchar *object_path, const gchar *interface_name,
|
|
const gchar *method_name, GVariant *parameters, const GVariantType *reply_type,
|
|
GDBusCallFlags flags, gint timeout_msec, GUnixFDList *fd_list,
|
|
GUnixFDList **out_fd_list, GCancellable *cancellable, GError **error) {
|
|
(void)cancellable;
|
|
(void)error;
|
|
assert(active_harness);
|
|
assert(connection == active_harness->portal->bus);
|
|
assert(!strcmp(bus_name, "org.freedesktop.portal.Desktop"));
|
|
assert(!strcmp(object_path, "/org/freedesktop/portal/desktop"));
|
|
assert(!strcmp(interface_name, "org.freedesktop.portal.RemoteDesktop"));
|
|
assert(!strcmp(method_name, "ConnectToEIS"));
|
|
assert(g_variant_is_of_type(parameters, G_VARIANT_TYPE("(oa{sv})")));
|
|
assert(g_variant_type_equal(reply_type, G_VARIANT_TYPE("(h)")));
|
|
assert(flags == G_DBUS_CALL_FLAGS_NONE && timeout_msec == 3000);
|
|
assert(!fd_list && out_fd_list);
|
|
|
|
const char *session;
|
|
GVariant *options;
|
|
g_variant_get(parameters, "(&o@a{sv})", &session, &options);
|
|
assert(!strcmp(session, "/synthetic/session"));
|
|
assert(g_variant_n_children(options) == 0);
|
|
g_variant_unref(options);
|
|
|
|
int client_fd = eis_backend_fd_add_client(active_harness->server);
|
|
assert(client_fd >= 0);
|
|
GUnixFDList *fds = g_unix_fd_list_new();
|
|
int index = g_unix_fd_list_append(fds, client_fd, NULL);
|
|
close(client_fd);
|
|
assert(index == 0);
|
|
*out_fd_list = fds;
|
|
active_harness->connect_calls++;
|
|
g_variant_ref_sink(parameters); g_variant_unref(parameters);
|
|
/* Hand off the server only after creating the socket; libeis contexts
|
|
* must never be dispatched and mutated by different threads at once. */
|
|
assert(pthread_create(&active_harness->server_pump_thread, NULL,
|
|
pump_server_during_open, active_harness) == 0);
|
|
return g_variant_ref_sink(g_variant_new("(h)", index));
|
|
}
|
|
|
|
static void test_diagnostic(struct ei *ei, enum ei_log_priority priority,
|
|
const char *message, struct ei_log_context *context) {
|
|
(void)ei;
|
|
(void)priority;
|
|
(void)message;
|
|
(void)context;
|
|
if (active_harness) active_harness->diagnostics++;
|
|
}
|
|
|
|
static void add_device(struct harness *h) {
|
|
if (h->device) return;
|
|
h->device = eis_seat_new_device(h->seat);
|
|
assert(h->device);
|
|
eis_device_configure_type(h->device, EIS_DEVICE_TYPE_VIRTUAL);
|
|
eis_device_configure_name(h->device, "synthetic portal EIS device");
|
|
for (size_t i = 0; i < sizeof(capabilities) / sizeof(capabilities[0]); i++)
|
|
eis_device_configure_capability(h->device, capabilities[i]);
|
|
/* The first region belongs to another stream. Matching by order would
|
|
* produce valid EIS events on the wrong screen. */
|
|
struct eis_region *region = eis_device_new_region(h->device);
|
|
assert(region);
|
|
eis_region_set_offset(region, 0, 0);
|
|
eis_region_set_size(region, 100, 100);
|
|
eis_region_set_mapping_id(region, "another-screen");
|
|
eis_region_add(region);
|
|
eis_region_unref(region);
|
|
region = eis_device_new_region(h->device);
|
|
assert(region);
|
|
eis_region_set_offset(region, h->geometry->x, h->geometry->y);
|
|
eis_region_set_size(region, h->geometry->logical_width, h->geometry->logical_height);
|
|
eis_region_set_mapping_id(region, "synthetic-screen");
|
|
eis_region_add(region);
|
|
eis_region_unref(region);
|
|
eis_device_add(h->device);
|
|
eis_device_resume(h->device);
|
|
}
|
|
|
|
static void handle_server_events(struct harness *h) {
|
|
struct eis_event *event;
|
|
while ((event = eis_get_event(h->server))) {
|
|
enum eis_event_type type = eis_event_get_type(event);
|
|
if (type == EIS_EVENT_CLIENT_CONNECT) {
|
|
h->client = eis_client_ref(eis_event_get_client(event));
|
|
eis_client_connect(h->client);
|
|
h->seat = eis_client_new_seat(h->client, "synthetic-seat");
|
|
assert(h->seat);
|
|
for (size_t i = 0; i < sizeof(capabilities) / sizeof(capabilities[0]); i++)
|
|
eis_seat_configure_capability(h->seat, capabilities[i]);
|
|
eis_seat_add(h->seat);
|
|
} else if (type == EIS_EVENT_SEAT_BIND) {
|
|
unsigned bound = 0;
|
|
for (size_t i = 0; i < sizeof(capabilities) / sizeof(capabilities[0]); i++) {
|
|
if (eis_event_seat_has_capability(event, capabilities[i])) bound |= capabilities[i];
|
|
}
|
|
if (!h->device) {
|
|
assert(bound == EVENT_CAP_MASK);
|
|
add_device(h);
|
|
}
|
|
} else if (type == EIS_EVENT_FRAME) {
|
|
h->frames++;
|
|
} else if (type == EIS_EVENT_DEVICE_START_EMULATING) {
|
|
h->starts++;
|
|
} else if (type == EIS_EVENT_DEVICE_STOP_EMULATING) {
|
|
h->stops++;
|
|
} else if (type == EIS_EVENT_POINTER_MOTION || type == EIS_EVENT_POINTER_MOTION_ABSOLUTE ||
|
|
type == EIS_EVENT_BUTTON_BUTTON || type == EIS_EVENT_SCROLL_DISCRETE ||
|
|
type == EIS_EVENT_KEYBOARD_KEY) {
|
|
assert(h->n_events < sizeof(h->events) / sizeof(h->events[0]));
|
|
struct received *got = &h->events[h->n_events++];
|
|
got->type = type;
|
|
switch (type) {
|
|
case EIS_EVENT_POINTER_MOTION:
|
|
got->x = eis_event_pointer_get_dx(event);
|
|
got->y = eis_event_pointer_get_dy(event);
|
|
break;
|
|
case EIS_EVENT_POINTER_MOTION_ABSOLUTE:
|
|
got->x = eis_event_pointer_get_absolute_x(event);
|
|
got->y = eis_event_pointer_get_absolute_y(event);
|
|
break;
|
|
case EIS_EVENT_BUTTON_BUTTON:
|
|
got->code = eis_event_button_get_button(event);
|
|
got->pressed = eis_event_button_get_is_press(event);
|
|
break;
|
|
case EIS_EVENT_SCROLL_DISCRETE:
|
|
got->dx = eis_event_scroll_get_discrete_dx(event);
|
|
got->dy = eis_event_scroll_get_discrete_dy(event);
|
|
break;
|
|
case EIS_EVENT_KEYBOARD_KEY:
|
|
got->code = eis_event_keyboard_get_key(event);
|
|
got->pressed = eis_event_keyboard_get_key_is_press(event);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
eis_event_unref(event);
|
|
}
|
|
}
|
|
|
|
static void pump_server(struct harness *h) {
|
|
eis_dispatch(h->server);
|
|
handle_server_events(h);
|
|
}
|
|
|
|
static void pump(struct harness *h) {
|
|
pump_server(h);
|
|
uu_portal_input_dispatch(h->portal);
|
|
pump_server(h);
|
|
}
|
|
|
|
static void *pump_server_during_open(void *data) {
|
|
struct harness *h = data;
|
|
while (!atomic_load(&h->stop_server_pump)) {
|
|
pump_server(h);
|
|
g_usleep(1000);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static int ready(struct harness *h) {
|
|
return h->portal->ei_devices && h->device && h->starts > 0;
|
|
}
|
|
|
|
static int paused(struct harness *h) {
|
|
return !h->portal->ei_devices;
|
|
}
|
|
|
|
static int disconnected(struct harness *h) {
|
|
return atomic_load(&h->portal->ei_failed);
|
|
}
|
|
|
|
static void wait_for(struct harness *h, int (*condition)(struct harness *)) {
|
|
gint64 deadline = g_get_monotonic_time() + 3000000;
|
|
while (!condition(h) && g_get_monotonic_time() < deadline) {
|
|
pump(h);
|
|
g_usleep(1000);
|
|
}
|
|
assert(condition(h));
|
|
}
|
|
|
|
static void clear_events(struct harness *h) {
|
|
h->n_events = 0;
|
|
h->frames = 0;
|
|
}
|
|
|
|
static void wait_for_input(struct harness *h) {
|
|
gint64 deadline = g_get_monotonic_time() + 1000000;
|
|
while (!h->n_events && g_get_monotonic_time() < deadline) {
|
|
pump(h);
|
|
g_usleep(1000);
|
|
}
|
|
assert(h->n_events);
|
|
assert(h->frames > 0);
|
|
}
|
|
|
|
static void pause_resume(struct harness *h) {
|
|
/* libeis 1.5 pauses only a resumed device; pause while emulating is a
|
|
* no-op. End the client transaction before exercising pause/resume. */
|
|
assert(h->portal->ei_devices);
|
|
ei_device_stop_emulating(h->portal->ei_devices->data);
|
|
gint64 deadline = g_get_monotonic_time() + 3000000;
|
|
while (!h->stops && g_get_monotonic_time() < deadline) {
|
|
pump(h);
|
|
g_usleep(1000);
|
|
}
|
|
assert(h->stops == 1);
|
|
eis_device_pause(h->device);
|
|
deadline = g_get_monotonic_time() + 3000000;
|
|
while (h->portal->ei_devices && g_get_monotonic_time() < deadline) {
|
|
pump(h);
|
|
g_usleep(1000);
|
|
}
|
|
assert(!h->portal->ei_devices);
|
|
eis_device_resume(h->device);
|
|
deadline = g_get_monotonic_time() + 3000000;
|
|
while ((!h->portal->ei_devices || h->starts < 2) && g_get_monotonic_time() < deadline) {
|
|
pump(h);
|
|
g_usleep(1000);
|
|
}
|
|
assert(h->portal->ei_devices && h->starts == 2 && !h->diagnostics);
|
|
}
|
|
|
|
static void run_geometry(const struct geometry *geometry) {
|
|
struct harness h = {.geometry = geometry};
|
|
h.portal = calloc(1, sizeof(*h.portal));
|
|
assert(h.portal);
|
|
pthread_mutex_init(&h.portal->lock, NULL);
|
|
pthread_cond_init(&h.portal->changed, NULL);
|
|
h.portal->width = geometry->width;
|
|
h.portal->height = geometry->height;
|
|
h.portal->logical_width = geometry->logical_width;
|
|
h.portal->logical_height = geometry->logical_height;
|
|
h.portal->mapping_id = g_strdup("synthetic-screen");
|
|
h.portal->session = strdup("/synthetic/session");
|
|
h.portal->absolute_stream_pixels = 1;
|
|
|
|
h.server = eis_new(NULL);
|
|
assert(h.server && eis_setup_backend_fd(h.server) == 0);
|
|
active_harness = &h;
|
|
int scaled = geometry->width != geometry->logical_width || geometry->height != geometry->logical_height;
|
|
int open_result = scaled ? prepare_input(h.portal) : portal_eis_open(h.portal);
|
|
atomic_store(&h.stop_server_pump, 1);
|
|
assert(pthread_join(h.server_pump_thread, NULL) == 0);
|
|
assert(open_result == 0 && h.connect_calls == 1);
|
|
ei_log_set_priority(h.portal->ei, EI_LOG_PRIORITY_ERROR);
|
|
ei_log_set_handler(h.portal->ei, test_diagnostic);
|
|
wait_for(&h, ready);
|
|
|
|
assert(h.portal->ei_devices);
|
|
struct ei_device *device = h.portal->ei_devices->data;
|
|
assert(((ei_device_has_capability(device, EI_DEVICE_CAP_POINTER) ? EI_DEVICE_CAP_POINTER : 0) |
|
|
(ei_device_has_capability(device, EI_DEVICE_CAP_POINTER_ABSOLUTE) ? EI_DEVICE_CAP_POINTER_ABSOLUTE : 0) |
|
|
(ei_device_has_capability(device, EI_DEVICE_CAP_BUTTON) ? EI_DEVICE_CAP_BUTTON : 0) |
|
|
(ei_device_has_capability(device, EI_DEVICE_CAP_SCROLL) ? EI_DEVICE_CAP_SCROLL : 0) |
|
|
(ei_device_has_capability(device, EI_DEVICE_CAP_KEYBOARD) ? EI_DEVICE_CAP_KEYBOARD : 0)) == CAP_MASK);
|
|
struct ei_region *region = ei_device_get_region(device, 1);
|
|
assert(region && !strcmp(ei_region_get_mapping_id(region), "synthetic-screen"));
|
|
assert(ei_region_get_x(region) == geometry->x && ei_region_get_y(region) == geometry->y);
|
|
assert(ei_region_get_width(region) == geometry->logical_width && ei_region_get_height(region) == geometry->logical_height);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_move(h.portal, 0, 0, 0) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
|
|
assert(h.events[0].x == geometry->x && h.events[0].y == geometry->y);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_move(h.portal, geometry->width / 2.0, geometry->height / 2.0, 0) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
|
|
assert(h.events[0].x == geometry->x + geometry->logical_width / 2.0);
|
|
assert(h.events[0].y == geometry->y + geometry->logical_height / 2.0);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_move(h.portal, geometry->width - 1, geometry->height - 1, 0) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
|
|
double right = geometry->x + geometry->logical_width -
|
|
(double)geometry->logical_width / geometry->width;
|
|
double bottom = geometry->y + geometry->logical_height -
|
|
(double)geometry->logical_height / geometry->height;
|
|
assert(fabs(h.events[0].x - right) < 0.001 && fabs(h.events[0].y - bottom) < 0.001);
|
|
clear_events(&h);
|
|
assert(uu_portal_move(h.portal, geometry->width - 1, 0, 0) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
|
|
assert(fabs(h.events[0].x - right) < 0.001 && h.events[0].y == geometry->y);
|
|
clear_events(&h);
|
|
assert(uu_portal_move(h.portal, 0, geometry->height - 1, 0) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
|
|
assert(h.events[0].x == geometry->x && fabs(h.events[0].y - bottom) < 0.001);
|
|
assert(uu_portal_move(h.portal, geometry->width, geometry->height, 0) == 11);
|
|
assert(uu_portal_move(h.portal, -1, -1, 0) == 11);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_move(h.portal, geometry->width / 8.0, geometry->height / 8.0, 1) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION);
|
|
assert(h.events[0].x == geometry->logical_width / 8.0);
|
|
assert(h.events[0].y == geometry->logical_height / 8.0);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_key(h.portal, 'a', 30, 1) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_KEYBOARD_KEY);
|
|
assert(h.events[0].code == 30 && h.events[0].pressed);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_button(h.portal, 1, 1) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_BUTTON_BUTTON);
|
|
assert(h.events[0].code == 0x110 && h.events[0].pressed);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_button(h.portal, 4, 1) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_SCROLL_DISCRETE);
|
|
assert(h.events[0].dx == 0 && h.events[0].dy == -120);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_key(h.portal, 'a', 30, 0) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_KEYBOARD_KEY);
|
|
assert(h.events[0].code == 30 && !h.events[0].pressed);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_button(h.portal, 1, 0) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_BUTTON_BUTTON);
|
|
assert(h.events[0].code == 0x110 && !h.events[0].pressed);
|
|
|
|
pause_resume(&h);
|
|
|
|
clear_events(&h);
|
|
assert(uu_portal_move(h.portal, geometry->width - 1, geometry->height - 1, 0) == 0);
|
|
wait_for_input(&h);
|
|
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
|
|
assert(fabs(h.events[0].x - right) < 0.001 && fabs(h.events[0].y - bottom) < 0.001);
|
|
|
|
eis_device_remove(h.device);
|
|
wait_for(&h, paused);
|
|
clear_events(&h);
|
|
assert(uu_portal_move(h.portal, 100, 100, 0) == 9);
|
|
assert(uu_portal_key(h.portal, 'a', 30, 1) == 9);
|
|
assert(uu_portal_button(h.portal, 1, 1) == 9);
|
|
pump(&h);
|
|
assert(h.n_events == 0);
|
|
|
|
eis_client_disconnect(h.client);
|
|
wait_for(&h, disconnected);
|
|
assert(!uu_portal_alive(h.portal));
|
|
clear_events(&h);
|
|
assert(uu_portal_move(h.portal, 100, 100, 0) == 9);
|
|
assert(uu_portal_key(h.portal, 'a', 30, 1) == 9);
|
|
assert(uu_portal_button(h.portal, 1, 1) == 9);
|
|
pump(&h);
|
|
assert(h.n_events == 0);
|
|
|
|
assert(!h.diagnostics);
|
|
uu_portal_close(h.portal);
|
|
active_harness = NULL;
|
|
if (h.device) eis_device_unref(h.device);
|
|
if (h.seat) eis_seat_unref(h.seat);
|
|
if (h.client) eis_client_unref(h.client);
|
|
eis_unref(h.server);
|
|
}
|
|
|
|
int main(void) {
|
|
const struct geometry cases[] = {
|
|
{1920, 1080, 1920, 1080, 0, 0}, /* 100% */
|
|
{2560, 1440, 2048, 1152, 320, 90}, /* 125% */
|
|
{2560, 1440, 1707, 960, 0, 0}, /* 150%, rounded logical size */
|
|
{3840, 2160, 2560, 1440, 100, 200}, /* 150% */
|
|
{3840, 2160, 2194, 1234, 0, 0}, /* 175%, rounded logical size */
|
|
{3840, 2160, 1920, 1080, 320, 90}, /* 200% */
|
|
{5120, 2880, 2560, 1440, 0, 0}, /* 5K */
|
|
{7680, 4320, 3840, 2160, 7680, 0}, /* 8K, nonzero origin */
|
|
{3440, 1440, 2293, 960, 0, 1080}, /* ultrawide */
|
|
{2160, 3840, 1080, 1920, 1920, 0}, /* portrait */
|
|
{2257, 1505, 1806, 1204, 0, 0}, /* odd frame dimensions */
|
|
};
|
|
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) run_geometry(&cases[i]);
|
|
puts("Portal EIS integration checks passed: 11 geometries");
|
|
return 0;
|
|
}
|