Files
uu-remote-linux/c_engine/portal_engine.c
T

479 lines
26 KiB
C

#define _GNU_SOURCE
#include "portal_engine.h"
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include <pipewire/pipewire.h>
#include <spa/param/video/format-utils.h>
#include <spa/pod/builder.h>
#include <spa/buffer/meta.h>
#include <pthread.h>
#include <stdatomic.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DEST "org.freedesktop.portal.Desktop"
#define PATH "/org/freedesktop/portal/desktop"
#define REMOTE "org.freedesktop.portal.RemoteDesktop"
#define SCREEN "org.freedesktop.portal.ScreenCast"
#define REQUEST "org.freedesktop.portal.Request"
#define SESSION "org.freedesktop.portal.Session"
struct uu_portal {
GDBusConnection *bus;
GMainContext *context;
GMainLoop *main_loop;
pthread_t dbus_thread;
int dbus_started;
char *session;
char *restore_token;
guint closed_subscription, clipboard_owner_subscription, clipboard_transfer_subscription;
int clipboard_enabled, clipboard_changed;
char *clipboard_text, *clipboard_mime;
size_t clipboard_length;
uint64_t clipboard_revision;
struct pw_thread_loop *pw_loop;
struct pw_context *pw_context;
struct pw_core *core;
struct pw_stream *stream;
struct spa_hook stream_listener;
pthread_mutex_t lock;
pthread_cond_t changed;
atomic_int closed;
int error;
uint32_t node, width, height;
int logical_width, logical_height;
int absolute_stream_pixels;
enum spa_video_format format;
uint8_t *frame;
size_t frame_length;
uint64_t sequence;
};
#include "portal_clipboard.inc"
static pthread_once_t pw_once = PTHREAD_ONCE_INIT;
static atomic_uint token_sequence = 1;
static void initialize_pipewire(void) { pw_init(NULL, NULL); }
static GVariant *empty_options(void) { return g_variant_new_array(G_VARIANT_TYPE("{sv}"), NULL, 0); }
static void fail(struct uu_portal *p, int error) {
pthread_mutex_lock(&p->lock);
if (!p->error) p->error = error;
pthread_cond_broadcast(&p->changed);
pthread_mutex_unlock(&p->lock);
}
struct response { int done; guint code; GVariant *results; };
static void response_received(GDBusConnection *bus, const gchar *sender, const gchar *path,
const gchar *interface, const gchar *signal, GVariant *parameters, gpointer data) {
(void)bus; (void)sender; (void)path; (void)interface; (void)signal;
struct response *r = data;
if (r->done) return;
g_variant_get(parameters, "(u@a{sv})", &r->code, &r->results);
r->done = 1;
}
static void close_request(struct uu_portal *p, const char *path) {
if (!path) return;
GVariant *r = g_dbus_connection_call_sync(p->bus, DEST, path, REQUEST, "Close",
NULL, NULL, G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL);
if (r) g_variant_unref(r);
}
/* Subscribe before invocation, using the documented handle_token path. */
static GVariant *request(struct uu_portal *p, const char *interface, const char *method,
GVariantBuilder *options, int mode, int *error) {
char token[64];
snprintf(token, sizeof(token), "uu_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
g_variant_builder_add(options, "{sv}", "handle_token", g_variant_new_string(token));
char *sender = g_strdup(g_dbus_connection_get_unique_name(p->bus) + 1);
for (char *c = sender; *c; c++) if (*c == '.') *c = '_';
char *expected = g_strdup_printf("/org/freedesktop/portal/desktop/request/%s/%s", sender, token);
g_free(sender);
struct response response = {0};
guint subscription = g_dbus_connection_signal_subscribe(p->bus, DEST, REQUEST, "Response", expected,
NULL, G_DBUS_SIGNAL_FLAGS_NONE, response_received, &response, NULL);
GVariant *args = mode == 0 ? g_variant_new("(@a{sv})", g_variant_builder_end(options)) :
mode == 1 ? g_variant_new("(o@a{sv})", p->session, g_variant_builder_end(options)) :
g_variant_new("(os@a{sv})", p->session, "", g_variant_builder_end(options));
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, PATH, interface, method, args,
G_VARIANT_TYPE("(o)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, NULL);
char *actual = NULL;
if (!reply) { *error = 3; goto finish; }
const char *handle;
g_variant_get(reply, "(&o)", &handle);
actual = g_strdup(handle);
g_variant_unref(reply);
if (strcmp(actual, expected)) { *error = 3; goto finish; }
unsigned consent_seconds=120;
const char *configured_timeout=getenv("UU_PORTAL_CONSENT_TIMEOUT_SECONDS");
if(configured_timeout) {
char *end=NULL;
unsigned long parsed=strtoul(configured_timeout,&end,10);
if(end && !*end && parsed>=30 && parsed<=900) consent_seconds=(unsigned)parsed;
}
gint64 deadline = g_get_monotonic_time() + consent_seconds * G_TIME_SPAN_SECOND;
while (!response.done && !atomic_load(&p->closed) && g_get_monotonic_time() < deadline) {
while (g_main_context_iteration(p->context, FALSE)) {}
if (!response.done) g_usleep(5000);
}
if (!response.done) *error = atomic_load(&p->closed) ? 9 : 5;
else if (response.code != 0) *error = 4;
finish:
if (*error) close_request(p, actual ? actual : expected);
g_dbus_connection_signal_unsubscribe(p->bus, subscription);
g_free(actual); g_free(expected);
if (*error && response.results) { g_variant_unref(response.results); response.results = NULL; }
return response.results;
}
static void session_closed(GDBusConnection *bus, const gchar *sender, const gchar *path,
const gchar *interface, const gchar *signal, GVariant *parameters, gpointer data) {
(void)bus; (void)sender; (void)path; (void)interface; (void)signal; (void)parameters;
struct uu_portal *p = data;
atomic_store(&p->closed, 1); fail(p, 9);
}
static void *run_dbus(void *data) {
struct uu_portal *p = data;
g_main_context_push_thread_default(p->context);
g_main_loop_run(p->main_loop);
g_main_context_pop_thread_default(p->context);
return NULL;
}
static int has_interface(struct uu_portal *p, const char *interface) {
GVariant *r = g_dbus_connection_call_sync(p->bus, DEST, PATH, "org.freedesktop.DBus.Properties",
"Get", g_variant_new("(ss)", interface, "version"), G_VARIANT_TYPE("(v)"),
G_DBUS_CALL_FLAGS_NONE, 5000, NULL, NULL);
if (!r) return 0;
g_variant_unref(r); return 1;
}
static void stream_state(void *data, enum pw_stream_state old, enum pw_stream_state state, const char *message) {
(void)old; (void)message;
if (state == PW_STREAM_STATE_ERROR || state == PW_STREAM_STATE_UNCONNECTED) fail(data, 9);
}
static void stream_format(void *data, uint32_t id, const struct spa_pod *param) {
struct uu_portal *p = data;
if (id != SPA_PARAM_Format || !param) return;
struct spa_video_info_raw info = {0};
if (spa_format_video_raw_parse(param, &info) < 0 || !info.size.width || !info.size.height ||
info.size.width > 8192 || info.size.height > 8192 ||
(info.format != SPA_VIDEO_FORMAT_BGRA && info.format != SPA_VIDEO_FORMAT_BGRx &&
info.format != SPA_VIDEO_FORMAT_RGBA && info.format != SPA_VIDEO_FORMAT_RGBx)) { fail(p, 8); return; }
pthread_mutex_lock(&p->lock);
if (p->width && (p->width != info.size.width || p->height != info.size.height)) {
p->error = 8; pthread_cond_broadcast(&p->changed); pthread_mutex_unlock(&p->lock); return;
}
size_t length = (size_t)info.size.width * info.size.height * 4;
if (!p->frame) p->frame = malloc(length);
if (!p->frame) { p->error = 8; pthread_mutex_unlock(&p->lock); return; }
p->width = info.size.width; p->height = info.size.height; p->format = info.format;
p->frame_length = length;
pthread_cond_broadcast(&p->changed);
pthread_mutex_unlock(&p->lock);
}
/* Called with the frame mutex held. Invalid transient chunks are discarded;
* allocation capacity is not evidence that valid pixels have been supplied. */
static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
if (!p->error && p->frame && spa->n_datas >= 1) {
struct spa_data *d = &spa->datas[0];
struct spa_meta_header *header = spa_buffer_find_meta_data(spa, SPA_META_Header, sizeof(*header));
if (!d->chunk || (d->chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) ||
(header && (header->flags & SPA_META_HEADER_FLAG_CORRUPTED))) return;
if (d->chunk->flags & SPA_CHUNK_FLAG_EMPTY) {
/* EMPTY denotes media-neutral (black) video, never old pixels. */
memset(p->frame, 0, p->frame_length);
for (size_t pixel = 0; pixel < p->frame_length / 4; pixel++) p->frame[pixel * 4 + 3] = 255;
p->sequence++;
return;
}
struct spa_meta_region *crop = spa_buffer_find_meta_data(spa, SPA_META_VideoCrop, sizeof(*crop));
if (crop && spa_meta_region_is_valid(crop) && (crop->region.position.x || crop->region.position.y ||
crop->region.size.width != p->width || crop->region.size.height != p->height)) p->error = 8;
if (!d->data || d->chunk->stride < (int32_t)(p->width * 4) || !d->chunk->size ||
d->chunk->offset > d->maxsize ||
(d->type != SPA_DATA_MemPtr && d->type != SPA_DATA_MemFd)) return;
uint64_t required = (uint64_t)(p->height - 1) * (uint32_t)d->chunk->stride + p->width * 4;
if (required > d->maxsize - d->chunk->offset || required > d->chunk->size) return;
if (!p->error) {
uint8_t *src = (uint8_t *)d->data + d->chunk->offset;
for (uint32_t y = 0; y < p->height; y++) {
uint8_t *row = src + (size_t)y * d->chunk->stride;
uint8_t *dst = p->frame + (size_t)y * p->width * 4;
if (p->format == SPA_VIDEO_FORMAT_BGRA || p->format == SPA_VIDEO_FORMAT_BGRx) memcpy(dst, row, p->width * 4);
else for (uint32_t x = 0; x < p->width; x++) {
dst[4*x] = row[4*x+2]; dst[4*x+1] = row[4*x+1]; dst[4*x+2] = row[4*x]; dst[4*x+3] = 255;
}
}
p->sequence++;
}
}
}
static void stream_process(void *data) {
struct uu_portal *p = data;
struct pw_buffer *buffer, *newest = NULL;
while ((buffer = pw_stream_dequeue_buffer(p->stream))) {
if (newest) pw_stream_queue_buffer(p->stream, newest);
newest = buffer;
}
if (!newest) return;
pthread_mutex_lock(&p->lock);
copy_video_buffer(p, newest->buffer);
pthread_cond_broadcast(&p->changed);
pthread_mutex_unlock(&p->lock);
pw_stream_queue_buffer(p->stream, newest);
}
static const struct pw_stream_events stream_events = {
PW_VERSION_STREAM_EVENTS, .state_changed = stream_state, .param_changed = stream_format, .process = stream_process
};
static int connect_pipewire(struct uu_portal *p) {
GUnixFDList *fds = NULL;
GVariant *reply = g_dbus_connection_call_with_unix_fd_list_sync(p->bus, DEST, PATH, SCREEN,
"OpenPipeWireRemote", g_variant_new("(o@a{sv})", p->session, empty_options()),
G_VARIANT_TYPE("(h)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, &fds, NULL, NULL);
if (!reply) return 7;
gint32 index; g_variant_get(reply, "(h)", &index);
int fd = g_unix_fd_list_get(fds, index, NULL);
g_variant_unref(reply); g_object_unref(fds);
if (fd < 0) return 7;
pthread_once(&pw_once, initialize_pipewire);
p->pw_loop = pw_thread_loop_new("uuremote-portal", NULL);
if (!p->pw_loop) { close(fd); return 7; }
p->pw_context = pw_context_new(pw_thread_loop_get_loop(p->pw_loop), NULL, 0);
if (!p->pw_context) { close(fd); return 7; }
/* pw_context_connect_fd takes ownership of fd, including failure. */
p->core = pw_context_connect_fd(p->pw_context, fd, NULL, 0);
if (!p->core) return 7;
p->stream = pw_stream_new(p->core, "uuremote-portal-capture", pw_properties_new(
PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, "Capture", PW_KEY_MEDIA_ROLE, "Screen", NULL));
if (!p->stream) return 7;
pw_stream_add_listener(p->stream, &p->stream_listener, &stream_events, p);
uint8_t storage[1024]; struct spa_pod_builder builder = SPA_POD_BUILDER_INIT(storage, sizeof(storage));
const struct spa_pod *params[2];
params[0] = spa_pod_builder_add_object(&builder, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video), SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(4, SPA_VIDEO_FORMAT_BGRA, SPA_VIDEO_FORMAT_BGRx, SPA_VIDEO_FORMAT_RGBA, SPA_VIDEO_FORMAT_RGBx),
SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(&SPA_RECTANGLE(1920,1080), &SPA_RECTANGLE(1,1), &SPA_RECTANGLE(8192,8192)),
SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(&SPA_FRACTION(60,1), &SPA_FRACTION(0,1), &SPA_FRACTION(60,1)));
params[1] = spa_pod_builder_add_object(&builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
SPA_PARAM_BUFFERS_dataType, SPA_POD_CHOICE_FLAGS_Int((1 << SPA_DATA_MemPtr) | (1 << SPA_DATA_MemFd)));
/* Node ID belongs to this portal's restricted remote. Do not put it into
* target.object, whose numeric value means object.serial on modern PW. */
if (pw_stream_connect(p->stream, PW_DIRECTION_INPUT, p->node,
PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params, 2) < 0) return 7;
if (pw_thread_loop_start(p->pw_loop) < 0) return 7;
struct timespec until; clock_gettime(CLOCK_REALTIME, &until); until.tv_sec += 10;
pthread_mutex_lock(&p->lock);
while (!p->sequence && !p->error) {
if (pthread_cond_timedwait(&p->changed, &p->lock, &until)) break;
}
int result = p->error ? p->error : p->sequence ? 0 : 10;
pthread_mutex_unlock(&p->lock);
return result;
}
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *), void *data, int *error, uint32_t *width, uint32_t *height) {
*error = 0; *width = *height = 0;
struct uu_portal *p = calloc(1, sizeof(*p));
if (!p) { *error = 8; return NULL; }
/* GNOME forwards NotifyPointerMotionAbsolute into Mutter's stream-to-stage
* transform. It expects stream pixels, unlike the compositor-sized KDE path.
* Do not apply the compositor scale twice on fractional-scale displays. */
gchar **desktops = g_strsplit(g_getenv("XDG_CURRENT_DESKTOP") ? g_getenv("XDG_CURRENT_DESKTOP") : "", ":", -1);
for (gchar **desktop = desktops; *desktop; desktop++)
if (!g_ascii_strcasecmp(*desktop, "GNOME")) p->absolute_stream_pixels = 1;
g_strfreev(desktops);
pthread_mutex_init(&p->lock, NULL); pthread_cond_init(&p->changed, NULL);
p->context = g_main_context_new(); g_main_context_push_thread_default(p->context);
p->bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
if (!p->bus) { *error = 1; goto done; }
if (!has_interface(p, REMOTE) || !has_interface(p, SCREEN)) { *error = 2; goto done; }
GVariantBuilder options; GVariant *results;
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
char token[64]; snprintf(token, sizeof(token), "uu_session_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
g_variant_builder_add(&options, "{sv}", "session_handle_token", g_variant_new_string(token));
results = request(p, REMOTE, "CreateSession", &options, 0, error);
if (!results) goto done;
const char *handle = NULL;
if (g_variant_lookup(results, "session_handle", "&s", &handle)) p->session = g_strdup(handle);
g_variant_unref(results);
if (!p->session || !g_variant_is_object_path(p->session)) { *error = 3; goto done; }
p->closed_subscription = g_dbus_connection_signal_subscribe(p->bus, DEST, SESSION, "Closed", p->session,
NULL, G_DBUS_SIGNAL_FLAGS_NONE, session_closed, p, NULL);
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&options, "{sv}", "types", g_variant_new_uint32(3));
GVariant *version_reply = g_dbus_connection_call_sync(p->bus, DEST, PATH,
"org.freedesktop.DBus.Properties", "Get", g_variant_new("(ss)", REMOTE, "version"),
G_VARIANT_TYPE("(v)"), G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL);
guint version = 0;
if (version_reply) {
GVariant *value; g_variant_get(version_reply, "(v)", &value);
if (g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) version = g_variant_get_uint32(value);
g_variant_unref(value); g_variant_unref(version_reply);
}
if (version >= 2) {
g_variant_builder_add(&options, "{sv}", "persist_mode", g_variant_new_uint32(2));
if (restore && *restore) g_variant_builder_add(&options, "{sv}", "restore_token", g_variant_new_string(restore));
}
results = request(p, REMOTE, "SelectDevices", &options, 1, error);
if (!results) goto done;
g_variant_unref(results);
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&options, "{sv}", "types", g_variant_new_uint32(1));
g_variant_builder_add(&options, "{sv}", "multiple", g_variant_new_boolean(FALSE));
/* Hidden cursor avoids duplicate pointer; supported since ScreenCast v1. */
results = request(p, SCREEN, "SelectSources", &options, 1, error);
if (!results) goto done;
g_variant_unref(results);
if (has_interface(p, CLIPBOARD)) {
p->clipboard_owner_subscription=g_dbus_connection_signal_subscribe(p->bus,DEST,CLIPBOARD,
"SelectionOwnerChanged",PATH,NULL,G_DBUS_SIGNAL_FLAGS_NONE,selection_owner,p,NULL);
p->clipboard_transfer_subscription=g_dbus_connection_signal_subscribe(p->bus,DEST,CLIPBOARD,
"SelectionTransfer",PATH,NULL,G_DBUS_SIGNAL_FLAGS_NONE,selection_transfer,p,NULL);
GVariant *reply=g_dbus_connection_call_sync(p->bus,DEST,PATH,CLIPBOARD,"RequestClipboard",
g_variant_new("(o@a{sv})",p->session,empty_options()),NULL,G_DBUS_CALL_FLAGS_NONE,3000,NULL,NULL);
if (reply) g_variant_unref(reply);
}
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
results = request(p, REMOTE, "Start", &options, 2, error);
if (!results) goto done;
gboolean clipboard_enabled=FALSE;
g_variant_lookup(results,"clipboard_enabled","b",&clipboard_enabled);
p->clipboard_enabled=clipboard_enabled;
fprintf(stderr,"Portal clipboard permission: granted=%d\n",clipboard_enabled);
guint devices = 0; GVariant *streams = g_variant_lookup_value(results, "streams", G_VARIANT_TYPE("a(ua{sv})"));
gboolean has_devices=g_variant_lookup(results, "devices", "u", &devices);
fprintf(stderr,"Portal authorization result: devices_present=%d device_bits=%u stream_count=%zu\n",has_devices,devices,streams?(size_t)g_variant_n_children(streams):0);
if (!has_devices || (devices & 3) != 3 || !streams || g_variant_n_children(streams) != 1) {
*error = 6;
} else {
GVariant *entry = g_variant_get_child_value(streams, 0), *properties;
g_variant_get(entry, "(u@a{sv})", &p->node, &properties);
/* 'size' is compositor logical coordinates, not the pixel frame size. */
g_variant_lookup(properties, "size", "(ii)", &p->logical_width, &p->logical_height);
g_variant_unref(properties); g_variant_unref(entry);
}
if (!*error) {
const char *next = NULL;
if (g_variant_lookup(results, "restore_token", "&s", &next) && next && strlen(next) <= 4096)
p->restore_token = g_strdup(next);
fprintf(stderr, "Portal persistence: requested=%d restore_attempted=%d token_returned=%d\n",
version >= 2, restore && *restore, p->restore_token && *p->restore_token);
}
if (!*error && p->restore_token && save_token) save_token(p->restore_token, data);
if (streams) g_variant_unref(streams);
g_variant_unref(results);
if (*error) goto done;
p->main_loop = g_main_loop_new(p->context, FALSE);
g_main_context_pop_thread_default(p->context);
if (pthread_create(&p->dbus_thread, NULL, run_dbus, p)) { *error = 3; uu_portal_close(p); return NULL; }
p->dbus_started = 1;
*error = connect_pipewire(p);
if (*error) { uu_portal_close(p); return NULL; }
fprintf(stderr, "Portal pointer geometry: frame=%ux%u compositor=%dx%d absolute=%s\n",
p->width, p->height, p->logical_width, p->logical_height,
p->absolute_stream_pixels ? "stream-pixels" : "compositor-logical");
*width = p->width; *height = p->height;
return p;
done:
g_main_context_pop_thread_default(p->context);
uu_portal_close(p); return NULL;
}
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height) {
return uu_portal_open_persist(restore, NULL, NULL, error, width, height);
}
struct uu_portal *uu_portal_open(int *error, uint32_t *width, uint32_t *height) {
return uu_portal_open_restore(NULL, error, width, height);
}
const char *uu_portal_restore_token(struct uu_portal *p) { return p ? p->restore_token : NULL; }
static gboolean quit_dbus_loop(gpointer data) { g_main_loop_quit(data); return G_SOURCE_REMOVE; }
void uu_portal_close(struct uu_portal *p) {
if (!p) return;
atomic_store(&p->closed, 1);
if (p->bus && p->session) {
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, p->session, SESSION, "Close",
NULL, NULL, G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL);
if (reply) g_variant_unref(reply);
}
if (p->clipboard_owner_subscription) g_dbus_connection_signal_unsubscribe(p->bus,p->clipboard_owner_subscription);
if (p->clipboard_transfer_subscription) g_dbus_connection_signal_unsubscribe(p->bus,p->clipboard_transfer_subscription);
if (p->closed_subscription) g_dbus_connection_signal_unsubscribe(p->bus, p->closed_subscription);
if (p->dbus_started) {
/* A queued source also handles close racing with thread startup; an
* immediate g_main_loop_quit before run would leave run blocking. */
GSource *quit = g_idle_source_new();
g_source_set_callback(quit, quit_dbus_loop, p->main_loop, NULL);
g_source_attach(quit, p->context); g_source_unref(quit);
g_main_context_wakeup(p->context); pthread_join(p->dbus_thread, NULL);
}
if (p->pw_loop) pw_thread_loop_stop(p->pw_loop);
if (p->stream) pw_stream_destroy(p->stream);
if (p->core) pw_core_disconnect(p->core);
if (p->pw_context) pw_context_destroy(p->pw_context);
if (p->pw_loop) pw_thread_loop_destroy(p->pw_loop);
if (p->main_loop) g_main_loop_unref(p->main_loop);
if (p->context) g_main_context_unref(p->context);
if (p->bus) g_object_unref(p->bus);
g_free(p->clipboard_text); g_free(p->clipboard_mime); g_free(p->restore_token); free(p->session); free(p->frame);
pthread_cond_destroy(&p->changed); pthread_mutex_destroy(&p->lock); free(p);
}
void uu_portal_reset_frame(struct uu_portal *p) {
if (!p) return;
pthread_mutex_lock(&p->lock);
p->sequence = 0;
if (p->frame) memset(p->frame, 0, p->frame_length);
pthread_mutex_unlock(&p->lock);
}
int uu_portal_capture(struct uu_portal *p, uint8_t *frame, size_t length) {
if (!p || !frame) return 11;
pthread_mutex_lock(&p->lock);
int result = atomic_load(&p->closed) ? 9 : p->error ? p->error : !p->sequence ? 10 : length != p->frame_length ? 11 : 0;
if (!result) memcpy(frame, p->frame, length);
pthread_mutex_unlock(&p->lock); return result;
}
int uu_portal_alive(struct uu_portal *p) {
if (!p || atomic_load(&p->closed)) return 0;
pthread_mutex_lock(&p->lock);
int alive = !p->error;
pthread_mutex_unlock(&p->lock);
return alive;
}
static int notify(struct uu_portal *p, const char *method, GVariant *parameters) {
if (!uu_portal_alive(p)) { g_variant_ref_sink(parameters); g_variant_unref(parameters); return 9; }
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, PATH, REMOTE, method, parameters,
NULL, G_DBUS_CALL_FLAGS_NONE, 3000, NULL, &error);
if (!reply) {
static atomic_uint failures = 0;
if (atomic_fetch_add(&failures, 1) < 12) {
char *name = error ? g_dbus_error_get_remote_error(error) : NULL;
fprintf(stderr, "Portal input method failed: method=%s remote_error=%s\n", method, name ? name : "unavailable");
g_free(name);
}
g_clear_error(&error); return 9;
}
g_variant_unref(reply); return 0;
}
int uu_portal_move(struct uu_portal *p, double x, double y, int relative) {
if (!p || !isfinite(x) || !isfinite(y)) return 11;
if (p->logical_width <= 0 || p->logical_height <= 0 || !p->width || !p->height) return 11;
if (relative) return notify(p, "NotifyPointerMotion", g_variant_new("(o@a{sv}dd)", p->session, empty_options(),
x * p->logical_width / p->width, y * p->logical_height / p->height));
if (x < 0 || y < 0 || x >= p->width || y >= p->height) return 11;
return notify(p, "NotifyPointerMotionAbsolute", g_variant_new("(o@a{sv}udd)", p->session, empty_options(), p->node,
p->absolute_stream_pixels ? x : x * p->logical_width / p->width,
p->absolute_stream_pixels ? y : y * p->logical_height / p->height));
}
int uu_portal_button(struct uu_portal *p, int button, int pressed) {
if (!p) return 11;
if (button >= 4 && button <= 7) {
if (!pressed) return 0;
return notify(p, "NotifyPointerAxisDiscrete", g_variant_new("(o@a{sv}ui)", p->session, empty_options(),
(guint)(button <= 5 ? 0 : 1), button == 4 || button == 6 ? -1 : 1));
}
int code = button == 1 ? 0x110 : button == 2 ? 0x112 : button == 3 ? 0x111 : button == 8 ? 0x113 : button == 9 ? 0x114 : 0;
if (!code) return 11;
return notify(p, "NotifyPointerButton", g_variant_new("(o@a{sv}iu)", p->session, empty_options(), code, (guint)!!pressed));
}
int uu_portal_key(struct uu_portal *p, uint32_t symbol, int pressed) {
if (!p || !symbol || symbol > INT32_MAX) return 11;
return notify(p, "NotifyKeyboardKeysym", g_variant_new("(o@a{sv}iu)", p->session, empty_options(), (gint)symbol, (guint)!!pressed));
}