Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 84 additions & 21 deletions crates/cordial-shell/src/vr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,13 @@ impl Places {
config_dirs: dirs("XDG_CONFIG_DIRS", "/etc/xdg"),
data_home: under_home("XDG_DATA_HOME", ".local/share"),
data_dirs: dirs("XDG_DATA_DIRS", "/usr/local/share:/usr/share"),
wivrn_flatpak: wivrn_flatpak_location()
.or_else(|| flatpak_install_on_disk(home.as_deref(), Path::new("/var/lib/flatpak"), WIVRN_FLATPAK)),
wivrn_flatpak: wivrn_flatpak_location().or_else(|| {
flatpak_install_on_disk(
home.as_deref(),
Path::new("/var/lib/flatpak"),
WIVRN_FLATPAK,
)
}),
sandboxed: Path::new("/.flatpak-info").exists(),
runtime_dir: std::env::var_os("XDG_RUNTIME_DIR").map(PathBuf::from),
home,
Expand Down Expand Up @@ -248,14 +253,17 @@ pub fn manifest_name(path: &Path) -> String {

/// The library a manifest names, resolved as the loader resolves it: an
/// absolute path as given, a relative one against the manifest's directory.
/// The manifest is canonicalized first, so a symlinked active_runtime.json
/// (common in ~/.config/openxr/1) resolves relative paths against the target
/// manifest's directory, not the symlink's.
pub fn manifest_library(manifest: &Path) -> Option<PathBuf> {
let v = read_manifest(manifest)?;
let lib = PathBuf::from(v.pointer("/runtime/library_path")?.as_str()?);
Some(if lib.is_absolute() {
lib
} else {
manifest.parent()?.join(lib)
})
if lib.is_absolute() {
return Some(lib);
}
let base = std::fs::canonicalize(manifest).unwrap_or_else(|_| manifest.to_path_buf());
Some(base.parent()?.join(lib))
}

/// What a launch should use, from the setting's value.
Expand Down Expand Up @@ -370,7 +378,8 @@ pub fn wivrn_start_command(places: &Places) -> String {

/// What Settings → VR's Play in VR row says when no Quest build has been imported, which is the
/// first thing missing on every machine that has never been set up for VR.
pub const NO_QUEST_BUILD: &str = "Import the Quest build of Roblox from your headset in Settings → VR.";
pub const NO_QUEST_BUILD: &str =
"Import the Quest build of Roblox from your headset in Settings → VR.";

/// Everything Play in VR needs, gathered once.
#[derive(Debug, Clone)]
Expand All @@ -387,13 +396,18 @@ impl Readiness {
pub fn gather(setting: Option<&str>) -> Self {
let places = Places::of_this_machine();
let (sandboxed, runtime_dir) = (places.sandboxed, places.runtime_dir.clone());
Self::gather_in(setting, &places, cordial_update::quest::current(), move || {
if sandboxed {
wivrn_socket_present(runtime_dir.as_deref())
} else {
wivrn_server_running()
}
})
Self::gather_in(
setting,
&places,
cordial_update::quest::current(),
move || {
if sandboxed {
wivrn_socket_present(runtime_dir.as_deref())
} else {
wivrn_server_running()
}
},
)
}

pub fn gather_in(
Expand Down Expand Up @@ -525,6 +539,36 @@ mod tests {
}
}

#[test]
fn a_symlinked_active_runtime_resolves_relative_library_paths_against_the_target() {
let root = scratch("symlink-runtime");
let p = places(&root);
// The real manifest lives under /usr/share, like a distro-packaged
// runtime, and names its library with a relative path.
manifest(
&root.join("usr/share/openxr/1/openxr_wivrn.json"),
"WiVRn",
"../../../lib/wivrn/libopenxr_wivrn.so",
);
library(&root.join("usr/lib/wivrn/libopenxr_wivrn.so"));
// The user's active runtime is a symlink to that manifest, which is how
// WiVRn and others register themselves in ~/.config/openxr/1.
let link = root.join("home/.config/openxr/1/active_runtime.json");
std::fs::create_dir_all(link.parent().unwrap()).unwrap();
#[cfg(unix)]
std::os::unix::fs::symlink(root.join("usr/share/openxr/1/openxr_wivrn.json"), &link)
.unwrap();

match resolve(None, &p) {
Chosen::System(r) => {
assert_eq!(r.name, "WiVRn");
assert_eq!(r.manifest, link);
}
other => panic!("{other:?}"),
}
let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn the_users_active_runtime_wins_over_the_systems() {
let root = scratch("active");
Expand Down Expand Up @@ -671,7 +715,11 @@ mod tests {
// user's ~/.config beside it, which does.
p.config_home = Some(root.join("home/.var/app/io.github.luohoa97.Cordial/config"));
library(&root.join("lib/w.so"));
manifest(&root.join("home/.config/openxr/1/active_runtime.json"), "WiVRn", &root.join("lib/w.so").to_string_lossy());
manifest(
&root.join("home/.config/openxr/1/active_runtime.json"),
"WiVRn",
&root.join("lib/w.so").to_string_lossy(),
);
let users = root.join("home/.config/openxr/1/active_runtime.json");
assert_ne!(
system_active(&p).map(|r| r.manifest),
Expand Down Expand Up @@ -712,13 +760,28 @@ mod tests {
let root = scratch("fpdisk");
let home = root.join("home");
let system = root.join("var/lib/flatpak");
assert_eq!(flatpak_install_on_disk(Some(&home), &system, WIVRN_FLATPAK), None);
let sys = system.join("app").join(WIVRN_FLATPAK).join("current/active");
assert_eq!(
flatpak_install_on_disk(Some(&home), &system, WIVRN_FLATPAK),
None
);
let sys = system
.join("app")
.join(WIVRN_FLATPAK)
.join("current/active");
std::fs::create_dir_all(sys.join("files")).unwrap();
assert_eq!(flatpak_install_on_disk(Some(&home), &system, WIVRN_FLATPAK), Some(sys));
let user = home.join(".local/share/flatpak/app").join(WIVRN_FLATPAK).join("current/active");
assert_eq!(
flatpak_install_on_disk(Some(&home), &system, WIVRN_FLATPAK),
Some(sys)
);
let user = home
.join(".local/share/flatpak/app")
.join(WIVRN_FLATPAK)
.join("current/active");
std::fs::create_dir_all(user.join("files")).unwrap();
assert_eq!(flatpak_install_on_disk(Some(&home), &system, WIVRN_FLATPAK), Some(user));
assert_eq!(
flatpak_install_on_disk(Some(&home), &system, WIVRN_FLATPAK),
Some(user)
);
let _ = std::fs::remove_dir_all(&root);
}

Expand Down