mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 08:50:40 +01:00
portal: persist rotated consent before video starts (#175)
Co-authored-by: jinpeng2700-tech <[email protected]>
This commit is contained in:
1 parent
7a0c40af73
commit
8b6de40605
6 files changed
+221
-46
No files matched your search
@@ -594,8 +594,14 @@ static int read_stream_result(struct uu_portal *p, GVariant *results) {
|
|||||||
if (streams) g_variant_unref(streams);
|
if (streams) g_variant_unref(streams);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
static void save_restore_token(struct uu_portal *p, void (*save_token)(const char *, void *),
|
||||||
|
uu_portal_save_detailed_fn save_detailed, void *save_data) {
|
||||||
|
if (!p->restore_token) return;
|
||||||
|
if (save_token) save_token(p->restore_token, save_data);
|
||||||
|
if (save_detailed) save_detailed(p->restore_token, save_data, p->has_position, p->position_x, p->position_y);
|
||||||
|
}
|
||||||
static struct uu_portal *open_persist(const char *restore, void (*save_token)(const char *, void *),
|
static struct uu_portal *open_persist(const char *restore, void (*save_token)(const char *, void *),
|
||||||
void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data,
|
uu_portal_save_detailed_fn save_detailed, void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data,
|
||||||
int prefer_dmabuf, int *error, uint32_t *width, uint32_t *height, uint32_t source_types) {
|
int prefer_dmabuf, int *error, uint32_t *width, uint32_t *height, uint32_t source_types) {
|
||||||
*error = 0; *width = *height = 0;
|
*error = 0; *width = *height = 0;
|
||||||
struct uu_portal *p = calloc(1, sizeof(*p));
|
struct uu_portal *p = calloc(1, sizeof(*p));
|
||||||
@@ -675,7 +681,7 @@ static struct uu_portal *open_persist(const char *restore, void (*save_token)(co
|
|||||||
fprintf(stderr, "Portal persistence: requested=%d restore_attempted=%d token_returned=%d\n",
|
fprintf(stderr, "Portal persistence: requested=%d restore_attempted=%d token_returned=%d\n",
|
||||||
version >= 2, restore && *restore, p->restore_token && *p->restore_token);
|
version >= 2, restore && *restore, p->restore_token && *p->restore_token);
|
||||||
}
|
}
|
||||||
if (!*error && p->restore_token && save_token) save_token(p->restore_token, save_data);
|
if (!*error) save_restore_token(p, save_token, save_detailed, save_data);
|
||||||
g_variant_unref(results);
|
g_variant_unref(results);
|
||||||
if (*error) goto done;
|
if (*error) goto done;
|
||||||
p->main_loop = g_main_loop_new(p->context, FALSE);
|
p->main_loop = g_main_loop_new(p->context, FALSE);
|
||||||
@@ -708,21 +714,27 @@ struct uu_portal *uu_portal_open_persist_cancellable(const char *restore,
|
|||||||
void (*save_token)(const char *, void *), void *save_data,
|
void (*save_token)(const char *, void *), void *save_data,
|
||||||
uu_portal_cancelled_fn cancelled, void *cancel_data,
|
uu_portal_cancelled_fn cancelled, void *cancel_data,
|
||||||
int *error, uint32_t *width, uint32_t *height) {
|
int *error, uint32_t *width, uint32_t *height) {
|
||||||
return open_persist(restore, save_token, save_data, cancelled, cancel_data, 0, error, width, height, 1);
|
return open_persist(restore, save_token, NULL, save_data, cancelled, cancel_data, 0, error, width, height, 1);
|
||||||
}
|
}
|
||||||
struct uu_portal *uu_portal_open_persist_capture(const char *restore,
|
struct uu_portal *uu_portal_open_persist_capture(const char *restore,
|
||||||
void (*save_token)(const char *, void *), void *save_data,
|
void (*save_token)(const char *, void *), void *save_data,
|
||||||
uu_portal_cancelled_fn cancelled, void *cancel_data, int prefer_dmabuf,
|
uu_portal_cancelled_fn cancelled, void *cancel_data, int prefer_dmabuf,
|
||||||
int *error, uint32_t *width, uint32_t *height) {
|
int *error, uint32_t *width, uint32_t *height) {
|
||||||
return open_persist(restore, save_token, save_data, cancelled, cancel_data, prefer_dmabuf, error, width, height, 1);
|
return open_persist(restore, save_token, NULL, save_data, cancelled, cancel_data, prefer_dmabuf, error, width, height, 1);
|
||||||
}
|
}
|
||||||
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *),
|
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) {
|
void *data, int *error, uint32_t *width, uint32_t *height) {
|
||||||
return open_persist(restore, save_token, data, NULL, NULL, 0, error, width, height, 1);
|
return open_persist(restore, save_token, NULL, data, NULL, NULL, 0, error, width, height, 1);
|
||||||
}
|
}
|
||||||
struct uu_portal *uu_portal_open_virtual(const char *restore, void (*save_token)(const char *, void *),
|
struct uu_portal *uu_portal_open_virtual(const char *restore, void (*save_token)(const char *, void *),
|
||||||
void *data, int *error, uint32_t *width, uint32_t *height) {
|
void *data, int *error, uint32_t *width, uint32_t *height) {
|
||||||
return open_persist(restore, save_token, data, NULL, NULL, 0, error, width, height, 4);
|
return open_persist(restore, save_token, NULL, data, NULL, NULL, 0, error, width, height, 4);
|
||||||
|
}
|
||||||
|
struct uu_portal *uu_portal_open_detailed(const char *restore, uu_portal_save_detailed_fn save_token,
|
||||||
|
void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data, int prefer_dmabuf,
|
||||||
|
uint32_t source_types, int *error, uint32_t *width, uint32_t *height) {
|
||||||
|
if (source_types != 1 && source_types != 4) { *error = 3; *width = *height = 0; return NULL; }
|
||||||
|
return open_persist(restore, NULL, save_token, save_data, cancelled, cancel_data, prefer_dmabuf, error, width, height, source_types);
|
||||||
}
|
}
|
||||||
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height) {
|
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);
|
return uu_portal_open_persist(restore, NULL, NULL, error, width, height);
|
||||||
|
|||||||
@@ -5,6 +5,9 @@
|
|||||||
#include "dmabuf_frame.h"
|
#include "dmabuf_frame.h"
|
||||||
struct uu_portal;
|
struct uu_portal;
|
||||||
typedef int (*uu_portal_cancelled_fn)(void *data);
|
typedef int (*uu_portal_cancelled_fn)(void *data);
|
||||||
|
typedef void (*uu_portal_save_detailed_fn)(const char *token, void *data, int has_position, int32_t x, int32_t y);
|
||||||
|
/* Reports the granted position alongside the rotated token before video initialization. */
|
||||||
|
struct uu_portal *uu_portal_open_detailed(const char *restore, uu_portal_save_detailed_fn save_token, void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data, int prefer_dmabuf, uint32_t source_types, int *error, uint32_t *width, uint32_t *height);
|
||||||
/* One object owns one consent/session and one PipeWire stream. */
|
/* One object owns one consent/session and one PipeWire stream. */
|
||||||
struct uu_portal *uu_portal_open(int *error, uint32_t *width, uint32_t *height);
|
struct uu_portal *uu_portal_open(int *error, uint32_t *width, uint32_t *height);
|
||||||
/* Saves the rotated token immediately after Start, before video initialization. */
|
/* Saves the rotated token immediately after Start, before video initialization. */
|
||||||
|
|||||||
+73
-39
@@ -41,6 +41,7 @@ mod native {
|
|||||||
fn uu_portal_open_virtual(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
fn uu_portal_open_virtual(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
||||||
fn uu_portal_open_persist_cancellable(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), save_data: *mut c_void, cancelled: extern "C" fn(*mut c_void) -> i32, cancel_data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
fn uu_portal_open_persist_cancellable(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), save_data: *mut c_void, cancelled: extern "C" fn(*mut c_void) -> i32, cancel_data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
||||||
fn uu_portal_open_persist_capture(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), save_data: *mut c_void, cancelled: Option<extern "C" fn(*mut c_void) -> i32>, cancel_data: *mut c_void, prefer_dmabuf: i32, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
fn uu_portal_open_persist_capture(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), save_data: *mut c_void, cancelled: Option<extern "C" fn(*mut c_void) -> i32>, cancel_data: *mut c_void, prefer_dmabuf: i32, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
||||||
|
fn uu_portal_open_detailed(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void, i32, i32, i32), save_data: *mut c_void, cancelled: Option<extern "C" fn(*mut c_void) -> i32>, cancel_data: *mut c_void, prefer_dmabuf: i32, source_types: u32, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
||||||
fn uu_portal_clipboard_supported(portal: *mut c_void) -> i32;
|
fn uu_portal_clipboard_supported(portal: *mut c_void) -> i32;
|
||||||
fn uu_portal_clipboard_ready(portal: *mut c_void) -> i32;
|
fn uu_portal_clipboard_ready(portal: *mut c_void) -> i32;
|
||||||
fn uu_portal_clipboard_set(portal: *mut c_void, text: *const u8, length: usize) -> i32;
|
fn uu_portal_clipboard_set(portal: *mut c_void, text: *const u8, length: usize) -> i32;
|
||||||
@@ -153,6 +154,7 @@ mod native {
|
|||||||
let directory = super::permissions::directory();
|
let directory = super::permissions::directory();
|
||||||
let key = super::permissions::desktop_key();
|
let key = super::permissions::desktop_key();
|
||||||
let wanted = if virtual_output { None } else { monitor.map(str::to_owned).or_else(crate::wayland::portal_monitor) };
|
let wanted = if virtual_output { None } else { monitor.map(str::to_owned).or_else(crate::wayland::portal_monitor) };
|
||||||
|
let wanted_key = wanted.as_deref().map(super::permissions::monitor_key);
|
||||||
let virtual_key = format!("{key}-virtual");
|
let virtual_key = format!("{key}-virtual");
|
||||||
// One opener at a time: consent tokens are single-use, and two concurrent openers
|
// One opener at a time: consent tokens are single-use, and two concurrent openers
|
||||||
// (privacy virtual-output switch and closure recovery after a quick reconnect) each
|
// (privacy virtual-output switch and closure recovery after a quick reconnect) each
|
||||||
@@ -163,66 +165,97 @@ mod native {
|
|||||||
let taken = directory.as_deref().filter(|_| !fresh).and_then(|dir| {
|
let taken = directory.as_deref().filter(|_| !fresh).and_then(|dir| {
|
||||||
let take = |k: String| super::permissions::take(dir, &k).map(|v| (k, v));
|
let take = |k: String| super::permissions::take(dir, &k).map(|v| (k, v));
|
||||||
if virtual_output { return take(virtual_key.clone()); }
|
if virtual_output { return take(virtual_key.clone()); }
|
||||||
wanted.as_deref().and_then(|name| take(super::permissions::monitor_key(name))).or_else(|| take(key.to_owned()))
|
wanted_key.as_ref().and_then(|name| take(super::permissions::pending_key(name)).or_else(|| take(name.clone())))
|
||||||
|
.or_else(|| take(super::permissions::pending_key(key))).or_else(|| take(key.to_owned()))
|
||||||
});
|
});
|
||||||
let token = taken.as_ref().and_then(|(_, value)| std::ffi::CString::new(value.as_str()).ok());
|
let token = taken.as_ref().and_then(|(_, value)| std::ffi::CString::new(value.as_str()).ok());
|
||||||
// The new token is saved once the granted monitor is known (after open).
|
let requested_key = if virtual_output { &virtual_key } else { wanted_key.as_deref().unwrap_or(key) };
|
||||||
let mut cache: Option<String> = None;
|
struct TokenCache { rotation: super::permissions::Rotation, virtual_key: Option<String>, fresh: bool }
|
||||||
extern "C" fn save_token(token: *const libc::c_char, data: *mut c_void) {
|
let mut cache = TokenCache {
|
||||||
// Callback is synchronous during open; cache lives through the call.
|
rotation: super::permissions::Rotation::new(directory.clone(), requested_key),
|
||||||
|
virtual_key: virtual_output.then(|| virtual_key.clone()), fresh,
|
||||||
|
};
|
||||||
|
extern "C" fn save_detailed(token: *const libc::c_char, data: *mut c_void, has_position: i32, x: i32, y: i32) {
|
||||||
let result = std::panic::catch_unwind(|| unsafe {
|
let result = std::panic::catch_unwind(|| unsafe {
|
||||||
let cache = &mut *(data as *mut Option<String>);
|
let cache = &mut *(data as *mut TokenCache);
|
||||||
*cache = std::ffi::CStr::from_ptr(token).to_str().ok().map(str::to_owned);
|
if let Ok(token) = std::ffi::CStr::from_ptr(token).to_str() {
|
||||||
|
if cache.fresh { cache.rotation.remember(token); return; }
|
||||||
|
let granted = cache.virtual_key.clone().or_else(||
|
||||||
|
(has_position != 0).then(|| crate::wayland::portal_monitor_at(x, y)).flatten()
|
||||||
|
.map(|name| super::permissions::monitor_key(&name)));
|
||||||
|
if cache.rotation.record(token, granted.as_deref()).is_err() { log::warn!("Portal permission cache could not be saved"); }
|
||||||
|
}
|
||||||
});
|
});
|
||||||
if result.is_err() { log::warn!("Portal permission cache callback failed"); }
|
if result.is_err() { log::warn!("Portal permission cache callback failed"); }
|
||||||
}
|
}
|
||||||
|
// Older plugins lack the position-aware entry point; use the pending slot.
|
||||||
|
extern "C" fn save_token(token: *const libc::c_char, data: *mut c_void) {
|
||||||
|
save_detailed(token, data, 0, 0, 0);
|
||||||
|
}
|
||||||
let restore = token.as_ref().map_or(std::ptr::null(), |value| value.as_ptr());
|
let restore = token.as_ref().map_or(std::ptr::null(), |value| value.as_ptr());
|
||||||
// A failed open must not cost the saved consent: put the token back unless the
|
let failed = |code: i32, cache: &TokenCache| {
|
||||||
// portal handed out a new one. A stale token only makes the next open ask again.
|
if let (Some(dir), Some((k, value)), false) = (directory.as_deref(), taken.as_ref(), cache.rotation.received()) {
|
||||||
let failed = |code: i32, cache: &Option<String>| {
|
|
||||||
if let (Some(dir), Some((k, value)), None) = (directory.as_deref(), taken.as_ref(), cache.as_ref()) {
|
|
||||||
if super::permissions::save(dir, k, value).is_err() { log::warn!("Portal permission cache could not be restored"); }
|
if super::permissions::save(dir, k, value).is_err() { log::warn!("Portal permission cache could not be restored"); }
|
||||||
}
|
}
|
||||||
super::PortalError { code }
|
super::PortalError { code }
|
||||||
};
|
};
|
||||||
|
// Release packages can temporarily have an older libuu-portal.so than the host.
|
||||||
|
#[cfg(feature = "engine-plugins")]
|
||||||
|
let detailed = crate::engine_plugin::symbol(crate::engine_plugin::Engine::Portal, "uu_portal_open_detailed\0") != 0;
|
||||||
|
#[cfg(not(feature = "engine-plugins"))]
|
||||||
|
let detailed = true;
|
||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
let mut pointer: *mut c_void = std::ptr::null_mut();
|
let mut prefer_dmabuf = false;
|
||||||
#[cfg(feature = "inproc-av")]
|
#[cfg(feature = "inproc-av")]
|
||||||
if !virtual_output && crate::video::native_engine_eligible(crate::video_select::selection().encoder, crate::video::NativeDisplayKind::Portal)
|
if !virtual_output && crate::video::native_engine_eligible(crate::video_select::selection().encoder, crate::video::NativeDisplayKind::Portal)
|
||||||
&& !std::env::var("UU_PORTAL_DMABUF").is_ok_and(|v| v == "0") {
|
&& !std::env::var("UU_PORTAL_DMABUF").is_ok_and(|v| v == "0") {
|
||||||
let callback = cancelled.map(|_| is_cancelled as extern "C" fn(*mut c_void) -> i32);
|
prefer_dmabuf = true;
|
||||||
let cancel_data = cancelled.map_or(std::ptr::null_mut(), |value| value as *const _ as *mut c_void);
|
|
||||||
pointer = unsafe { uu_portal_open_persist_capture(
|
|
||||||
restore, save_token, &mut cache as *mut _ as *mut c_void, callback, cancel_data, 1,
|
|
||||||
&mut error, &mut width, &mut height,
|
|
||||||
) };
|
|
||||||
if pointer.is_null() && error != 0 { return Err(failed(error, &cache)); }
|
|
||||||
if pointer.is_null() { log::warn!("Portal engine lacks DMA-BUF initialization; falling back to legacy initialization"); }
|
|
||||||
}
|
}
|
||||||
let pointer = if !pointer.is_null() { pointer } else { unsafe {
|
let pointer = if detailed {
|
||||||
if virtual_output {
|
let cancel = cancelled.filter(|_| !virtual_output);
|
||||||
uu_portal_open_virtual(restore, save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height)
|
let callback = cancel.map(|_| is_cancelled as extern "C" fn(*mut c_void) -> i32);
|
||||||
} else if let Some(cancelled) = cancelled {
|
let cancel_data = cancel.map_or(std::ptr::null_mut(), |value| value as *const _ as *mut c_void);
|
||||||
uu_portal_open_persist_cancellable(
|
unsafe { uu_portal_open_detailed(
|
||||||
restore, save_token, &mut cache as *mut _ as *mut c_void,
|
restore, save_detailed, &mut cache as *mut _ as *mut c_void, callback, cancel_data,
|
||||||
is_cancelled, cancelled as *const _ as *mut c_void,
|
i32::from(prefer_dmabuf), if virtual_output { 4 } else { 1 },
|
||||||
|
&mut error, &mut width, &mut height,
|
||||||
|
) }
|
||||||
|
} else {
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
let mut pointer: *mut c_void = std::ptr::null_mut();
|
||||||
|
#[cfg(feature = "inproc-av")]
|
||||||
|
if prefer_dmabuf {
|
||||||
|
let callback = cancelled.map(|_| is_cancelled as extern "C" fn(*mut c_void) -> i32);
|
||||||
|
let cancel_data = cancelled.map_or(std::ptr::null_mut(), |value| value as *const _ as *mut c_void);
|
||||||
|
pointer = unsafe { uu_portal_open_persist_capture(
|
||||||
|
restore, save_token, &mut cache as *mut _ as *mut c_void, callback, cancel_data, 1,
|
||||||
&mut error, &mut width, &mut height,
|
&mut error, &mut width, &mut height,
|
||||||
)
|
) };
|
||||||
} else {
|
if pointer.is_null() && error != 0 { return Err(failed(error, &cache)); }
|
||||||
uu_portal_open_persist(restore, save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height)
|
if pointer.is_null() { log::warn!("Portal engine lacks DMA-BUF initialization; falling back to legacy initialization"); }
|
||||||
}
|
}
|
||||||
} };
|
if !pointer.is_null() { pointer } else { unsafe {
|
||||||
let Some(handle) = NonNull::new(pointer) else { return Err(failed(error, &cache)) };
|
if virtual_output {
|
||||||
|
uu_portal_open_virtual(restore, save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height)
|
||||||
|
} else if let Some(cancelled) = cancelled {
|
||||||
|
uu_portal_open_persist_cancellable(restore, save_token, &mut cache as *mut _ as *mut c_void,
|
||||||
|
is_cancelled, cancelled as *const _ as *mut c_void, &mut error, &mut width, &mut height)
|
||||||
|
} else {
|
||||||
|
uu_portal_open_persist(restore, save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height)
|
||||||
|
}
|
||||||
|
} }
|
||||||
|
};
|
||||||
|
let Some(handle) = NonNull::new(pointer) else { return Err(failed(if error == 0 { 15 } else { error }, &cache)) };
|
||||||
let (mut x, mut y) = (0, 0);
|
let (mut x, mut y) = (0, 0);
|
||||||
let granted = (!virtual_output && unsafe { uu_portal_stream_position(handle.as_ptr(), &mut x, &mut y) } == 0)
|
let granted = (!virtual_output && unsafe { uu_portal_stream_position(handle.as_ptr(), &mut x, &mut y) } == 0)
|
||||||
.then(|| crate::wayland::portal_monitor_at(x, y)).flatten();
|
.then(|| crate::wayland::portal_monitor_at(x, y)).flatten();
|
||||||
if let (Some(dir), Some(token)) = (directory.as_deref(), cache.take()) {
|
let saved = match &granted {
|
||||||
let saved = match &granted {
|
_ if virtual_output => virtual_key.clone(),
|
||||||
_ if virtual_output => virtual_key.clone(),
|
Some(name) => super::permissions::monitor_key(name),
|
||||||
Some(name) => super::permissions::monitor_key(name),
|
None => key.to_owned(),
|
||||||
None => key.to_owned(),
|
};
|
||||||
};
|
if cache.rotation.finish(&saved, fresh || virtual_output || granted.is_some()).is_err() {
|
||||||
if super::permissions::save(dir, &saved, &token).is_err() { log::warn!("Portal permission cache could not be saved"); }
|
log::warn!("Portal permission cache could not be saved");
|
||||||
}
|
}
|
||||||
if wanted.is_some() && granted != wanted { log::warn!("Portal granted a different monitor than requested"); }
|
if wanted.is_some() && granted != wanted { log::warn!("Portal granted a different monitor than requested"); }
|
||||||
// Keep a freshly granted compositor frame regardless of lock state. Discarding it
|
// Keep a freshly granted compositor frame regardless of lock state. Discarding it
|
||||||
@@ -880,6 +913,7 @@ impl PortalSession {
|
|||||||
static ACTIVE: std::sync::Mutex<Option<std::sync::Weak<PortalSession>>> = std::sync::Mutex::new(None);
|
static ACTIVE: std::sync::Mutex<Option<std::sync::Weak<PortalSession>>> = std::sync::Mutex::new(None);
|
||||||
pub fn permissions_directory() -> Option<std::path::PathBuf> { permissions::directory() }
|
pub fn permissions_directory() -> Option<std::path::PathBuf> { permissions::directory() }
|
||||||
pub fn monitor_key(connector: &str) -> String { permissions::monitor_key(connector) }
|
pub fn monitor_key(connector: &str) -> String { permissions::monitor_key(connector) }
|
||||||
|
pub fn permission_exists(dir: &std::path::Path, key: &str) -> bool { permissions::exists(dir, key) }
|
||||||
pub fn register(session: &Arc<PortalSession>) {
|
pub fn register(session: &Arc<PortalSession>) {
|
||||||
*ACTIVE.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::downgrade(session));
|
*ACTIVE.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::downgrade(session));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,58 @@ pub fn desktop_key() -> &'static str {
|
|||||||
/// Consent for one monitor (Mutter connector); connectors are checked to be file-name safe.
|
/// Consent for one monitor (Mutter connector); connectors are checked to be file-name safe.
|
||||||
pub fn monitor_key(connector: &str) -> String { format!("{}@{}", desktop_key(), connector) }
|
pub fn monitor_key(connector: &str) -> String { format!("{}@{}", desktop_key(), connector) }
|
||||||
|
|
||||||
|
/// An unclassified grant is kept separately until its monitor is known.
|
||||||
|
pub fn pending_key(key: &str) -> String { format!("{key}.pending") }
|
||||||
|
|
||||||
|
pub fn exists(dir: &Path, key: &str) -> bool {
|
||||||
|
dir.join(key).exists() || dir.join(pending_key(key)).exists()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Rotation {
|
||||||
|
directory: Option<PathBuf>,
|
||||||
|
pending: String,
|
||||||
|
saved: Option<String>,
|
||||||
|
token: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Rotation {
|
||||||
|
pub fn new(directory: Option<PathBuf>, requested_key: &str) -> Self {
|
||||||
|
Self { directory, pending: pending_key(requested_key), saved: None, token: None }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn received(&self) -> bool { self.token.is_some() }
|
||||||
|
|
||||||
|
/// A fresh request has not consumed the old grant; keep it until the new session opens.
|
||||||
|
pub fn remember(&mut self, token: &str) { self.token = Some(token.to_owned()); }
|
||||||
|
|
||||||
|
/// Persist before PipeWire starts; `known_key` comes from the actual granted source.
|
||||||
|
pub fn record(&mut self, token: &str, known_key: Option<&str>) -> std::io::Result<()> {
|
||||||
|
self.remember(token);
|
||||||
|
let Some(dir) = self.directory.as_deref() else { return Ok(()); };
|
||||||
|
let destination = known_key.unwrap_or(&self.pending);
|
||||||
|
save(dir, destination, token)?;
|
||||||
|
self.saved = Some(destination.to_owned());
|
||||||
|
if let Some(key) = known_key { let _ = fs::remove_file(dir.join(pending_key(key))); }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Move, rather than copy, so a single-use token never has two cache entries.
|
||||||
|
pub fn finish(&mut self, destination: &str, replace: bool) -> std::io::Result<()> {
|
||||||
|
let (Some(dir), Some(token)) = (self.directory.as_deref(), self.token.as_deref()) else { return Ok(()); };
|
||||||
|
if self.saved.as_deref() == Some(destination) { return Ok(()); }
|
||||||
|
if !replace && dir.join(destination).exists() { return Ok(()); }
|
||||||
|
if let Some(source) = self.saved.as_deref() {
|
||||||
|
fs::rename(dir.join(source), dir.join(destination))?;
|
||||||
|
fs::File::open(dir)?.sync_all()?;
|
||||||
|
} else {
|
||||||
|
save(dir, destination, token)?;
|
||||||
|
}
|
||||||
|
self.saved = Some(destination.to_owned());
|
||||||
|
let _ = fs::remove_file(dir.join(pending_key(destination)));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn take(dir: &Path, key: &str) -> Option<String> {
|
pub fn take(dir: &Path, key: &str) -> Option<String> {
|
||||||
let path = dir.join(key);
|
let path = dir.join(key);
|
||||||
let mut file = OpenOptions::new().read(true).custom_flags(libc::O_NOFOLLOW).open(&path).ok()?;
|
let mut file = OpenOptions::new().read(true).custom_flags(libc::O_NOFOLLOW).open(&path).ok()?;
|
||||||
@@ -71,4 +123,61 @@ mod tests {
|
|||||||
assert!(save(&dir, "gnome", &"x".repeat(4097)).is_err());
|
assert!(save(&dir, "gnome", &"x".repeat(4097)).is_err());
|
||||||
fs::remove_dir_all(dir).unwrap();
|
fs::remove_dir_all(dir).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rotated_grant_survives_failure_and_is_moved_after_success() {
|
||||||
|
let dir = std::env::temp_dir().join(format!("uu-rotation-{}", std::process::id()));
|
||||||
|
fs::create_dir_all(&dir).unwrap();
|
||||||
|
save(&dir, "gnome@other", "other-grant").unwrap();
|
||||||
|
let mut rotation = Rotation::new(Some(dir.clone()), "gnome@wanted");
|
||||||
|
rotation.record("new-grant", None).unwrap();
|
||||||
|
// Simulate capture failure: no finish callback runs.
|
||||||
|
assert!(exists(&dir, "gnome@wanted"));
|
||||||
|
assert_eq!(take(&dir, &pending_key("gnome@wanted")).as_deref(), Some("new-grant"));
|
||||||
|
assert!(!exists(&dir, "gnome@wanted"));
|
||||||
|
assert_eq!(take(&dir, "gnome@other").as_deref(), Some("other-grant"));
|
||||||
|
|
||||||
|
let mut rotation = Rotation::new(Some(dir.clone()), "gnome@wanted");
|
||||||
|
rotation.record("newer-grant", None).unwrap();
|
||||||
|
rotation.finish("gnome@actual", true).unwrap();
|
||||||
|
assert!(!dir.join(pending_key("gnome@wanted")).exists());
|
||||||
|
assert_eq!(take(&dir, "gnome@actual").as_deref(), Some("newer-grant"));
|
||||||
|
fs::remove_dir_all(dir).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn known_grants_replace_only_their_monitor_and_unknown_grants_do_not_overwrite() {
|
||||||
|
let dir = std::env::temp_dir().join(format!("uu-rotation-known-{}", std::process::id()));
|
||||||
|
fs::create_dir_all(&dir).unwrap();
|
||||||
|
save(&dir, "gnome", "existing-grant").unwrap();
|
||||||
|
save(&dir, "gnome@other", "other-grant").unwrap();
|
||||||
|
let mut unknown = Rotation::new(Some(dir.clone()), "gnome@wanted");
|
||||||
|
unknown.record("unknown-grant", None).unwrap();
|
||||||
|
unknown.finish("gnome", false).unwrap();
|
||||||
|
assert_eq!(take(&dir, "gnome").as_deref(), Some("existing-grant"));
|
||||||
|
assert_eq!(take(&dir, &pending_key("gnome@wanted")).as_deref(), Some("unknown-grant"));
|
||||||
|
let mut known = Rotation::new(Some(dir.clone()), "gnome@wanted");
|
||||||
|
known.record("known-grant", Some("gnome@actual")).unwrap();
|
||||||
|
assert_eq!(take(&dir, "gnome@actual").as_deref(), Some("known-grant"));
|
||||||
|
assert_eq!(take(&dir, "gnome@other").as_deref(), Some("other-grant"));
|
||||||
|
let mut no_token = Rotation::new(Some(dir.clone()), "gnome");
|
||||||
|
no_token.finish("gnome", true).unwrap();
|
||||||
|
assert!(!no_token.received());
|
||||||
|
assert!(!dir.join("gnome").exists());
|
||||||
|
fs::remove_dir_all(dir).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fresh_request_keeps_existing_grant_until_open_succeeds() {
|
||||||
|
let dir = std::env::temp_dir().join(format!("uu-rotation-fresh-{}", std::process::id()));
|
||||||
|
fs::create_dir_all(&dir).unwrap();
|
||||||
|
save(&dir, "gnome", "existing-grant").unwrap();
|
||||||
|
let mut fresh = Rotation::new(Some(dir.clone()), "gnome");
|
||||||
|
fresh.remember("fresh-grant");
|
||||||
|
assert_eq!(fs::read_to_string(dir.join("gnome")).unwrap(), "existing-grant");
|
||||||
|
assert!(!dir.join(pending_key("gnome")).exists());
|
||||||
|
fresh.finish("gnome", true).unwrap();
|
||||||
|
assert_eq!(take(&dir, "gnome").as_deref(), Some("fresh-grant"));
|
||||||
|
fs::remove_dir_all(dir).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -261,7 +261,7 @@ fn portal_outputs()->Vec<Output> {
|
|||||||
let mut outputs:Vec<Output>=monitors.iter().map(|m|Output {name:m.connector.clone(),width:m.width,height:m.height,fps,x:m.x,y:m.y}).collect();
|
let mut outputs:Vec<Output>=monitors.iter().map(|m|Output {name:m.connector.clone(),width:m.width,height:m.height,fps,x:m.x,y:m.y}).collect();
|
||||||
// Screen 0 is the one opened first: the monitor whose consent is already saved, else the primary.
|
// Screen 0 is the one opened first: the monitor whose consent is already saved, else the primary.
|
||||||
let dir=crate::portal::permissions_directory();
|
let dir=crate::portal::permissions_directory();
|
||||||
let granted=|name:&str|dir.as_deref().is_some_and(|d|d.join(crate::portal::monitor_key(name)).exists());
|
let granted=|name:&str|dir.as_deref().is_some_and(|d|crate::portal::permission_exists(d,&crate::portal::monitor_key(name)));
|
||||||
let primary=monitors.iter().find(|m|m.primary).map(|m|m.connector.clone()).unwrap_or_default();
|
let primary=monitors.iter().find(|m|m.primary).map(|m|m.connector.clone()).unwrap_or_default();
|
||||||
outputs.sort_by_key(|o|(!granted(&o.name),o.name!=primary,o.x,o.y));
|
outputs.sort_by_key(|o|(!granted(&o.name),o.name!=primary,o.x,o.y));
|
||||||
outputs.truncate(16);
|
outputs.truncate(16);
|
||||||
|
|||||||
@@ -419,7 +419,24 @@ static void authorization_still_required(void) {
|
|||||||
g_variant_unref(reply);
|
g_variant_unref(reply);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
static void saved_token_with_position(const char *token, void *data, int has_position, int32_t x, int32_t y) {
|
||||||
|
int *calls = data;
|
||||||
|
assert(!strcmp(token, "synthetic-grant"));
|
||||||
|
assert(has_position && x == 25 && y == 50);
|
||||||
|
(*calls)++;
|
||||||
|
}
|
||||||
|
static void rotated_token_callback(void) {
|
||||||
|
struct uu_portal p = {0};
|
||||||
|
int calls = 0;
|
||||||
|
save_restore_token(&p, NULL, saved_token_with_position, &calls);
|
||||||
|
assert(calls == 0);
|
||||||
|
p.restore_token = "synthetic-grant";
|
||||||
|
p.has_position = 1; p.position_x = 25; p.position_y = 50;
|
||||||
|
save_restore_token(&p, NULL, saved_token_with_position, &calls);
|
||||||
|
assert(calls == 1);
|
||||||
|
}
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
rotated_token_callback();
|
||||||
portal_renegotiation_timeout();
|
portal_renegotiation_timeout();
|
||||||
portal_renegotiation_recovery();
|
portal_renegotiation_recovery();
|
||||||
portal_renegotiation_event_order_and_healthy_states();
|
portal_renegotiation_event_order_and_healthy_states();
|
||||||
|
|||||||
Reference in new issue
Block a user