Skip to content

Commit a6fa853

Browse files
committed
Add check NVIDIA module for NVIDIA EGL prime logic
Add SHARUN_NO_NVIDIA_EGL_PRIME Disables NVIDIA EGL prime logic Minor fixes
1 parent a2e2a03 commit a6fa853

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cargo-features = ["panic-immediate-abort"]
22

33
[package]
44
name = "sharun"
5-
version = "0.7.7"
5+
version = "0.7.8"
66
readme = "README.md"
77
license = "MIT"
88
repository = "https://github.com/VHSgunzo/sharun"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ cp ./target/$(uname -m)-unknown-linux-musl/release/sharun .
4848
SHARUN_ALLOW_SYS_VKICD=1 Enables breaking system vulkan/icd.d for vulkan loader
4949
SHARUN_ALLOW_LD_PRELOAD=1 Enables breaking LD_PRELOAD env variable
5050
SHARUN_ALLOW_QT_PLUGIN_PATH=1 Enables breaking QT_PLUGIN_PATH env variable
51+
SHARUN_NO_NVIDIA_EGL_PRIME=1 Disables NVIDIA EGL prime logic
5152
SHARUN_PRINTENV=1 Print environment variables to stderr
5253
SHARUN_LDNAME=ld.so Specifies the name of the interpreter
5354
SHARUN_EXTRA_LIBRARY_PATH Extra library directories with highest priority

src/main.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@ fn add_to_env<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, val: V) {
282282
let old_val = get_env_var(key);
283283
if old_val.is_empty() {
284284
env::set_var(key, val)
285-
} else if !old_val.contains(val) {
285+
} else if old_val != val &&
286+
!old_val.starts_with(&format!("{val}:")) &&
287+
!old_val.ends_with(&format!(":{val}")) &&
288+
!old_val.contains(&format!(":{val}:")) {
286289
env::set_var(key, format!("{val}:{old_val}"))
287290
}
288291
}
@@ -350,6 +353,20 @@ fn gen_library_path(library_path: &str, lib_path_file: &String) {
350353
}
351354
}
352355

356+
fn collect_json_files(dir: &Path) -> Vec<PathBuf> {
357+
let mut json_paths = Vec::new();
358+
if dir.exists() {
359+
if let Ok(entries) = dir.read_dir() {
360+
for entry in entries.flatten() {
361+
let path = entry.path();
362+
if path.extension().is_some_and(|ext| ext == "json") &&
363+
path.exists() { json_paths.push(path) }
364+
}
365+
}
366+
}
367+
json_paths
368+
}
369+
353370
fn print_usage() {
354371
println!("[ {} ]
355372
@@ -373,6 +390,7 @@ fn print_usage() {
373390
SHARUN_ALLOW_SYS_VKICD=1 Enables breaking system vulkan/icd.d for vulkan loader
374391
SHARUN_ALLOW_LD_PRELOAD=1 Enables breaking LD_PRELOAD env variable
375392
SHARUN_ALLOW_QT_PLUGIN_PATH=1 Enables breaking QT_PLUGIN_PATH env variable
393+
SHARUN_NO_NVIDIA_EGL_PRIME=1 Disables NVIDIA EGL prime logic
376394
SHARUN_PRINTENV=1 Print environment variables to stderr
377395
SHARUN_LDNAME=ld.so Specifies the name of the interpreter
378396
SHARUN_EXTRA_LIBRARY_PATH Extra library directories with highest priority
@@ -832,15 +850,34 @@ fn main() {
832850
let name = entry.file_name();
833851
match name.to_str().unwrap_or_default() {
834852
"glvnd" => {
835-
if get_env_var("__EGL_VENDOR_LIBRARY_FILENAMES").is_empty() {
836-
for xdg_data_dir in xdg_data_dirs.rsplit(":") {
837-
let nvidia_json_path = Path::new(xdg_data_dir).join("glvnd/egl_vendor.d/10_nvidia.json");
838-
if nvidia_json_path.exists() {
839-
env::set_var("__EGL_VENDOR_LIBRARY_FILENAMES", &nvidia_json_path);
840-
break
841-
}
842-
}
843-
}
853+
if get_env_var("SHARUN_NO_NVIDIA_EGL_PRIME") != "1" &&
854+
Path::new("/sys/module/nvidia/version").exists() &&
855+
get_env_var("__EGL_VENDOR_LIBRARY_FILENAMES").is_empty() {
856+
let mut xdg_json_paths = Vec::new();
857+
for xdg_data_dir in xdg_data_dirs.split(":") {
858+
let egl_vendor = Path::new(xdg_data_dir).join("glvnd/egl_vendor.d");
859+
let mut paths = collect_json_files(&egl_vendor);
860+
xdg_json_paths.append(&mut paths)
861+
}
862+
let nvidia_json = xdg_json_paths.iter()
863+
.find(|p| p.file_name().unwrap_or_default().to_string_lossy().contains("nvidia"));
864+
if let Some(nvidia_path) = nvidia_json {
865+
let mut all_paths = Vec::new();
866+
all_paths.push(nvidia_path.clone());
867+
for path in xdg_json_paths.iter() {
868+
if !path.file_name().unwrap_or_default().to_string_lossy().contains("nvidia") {
869+
all_paths.push(path.clone())
870+
}
871+
}
872+
if !all_paths.is_empty() {
873+
let paths_str = all_paths.iter()
874+
.map(|p| p.to_string_lossy())
875+
.collect::<Vec<_>>()
876+
.join(":");
877+
env::set_var("__EGL_VENDOR_LIBRARY_FILENAMES", &paths_str)
878+
}
879+
}
880+
}
844881
add_to_xdg_data_env(xdg_data_dirs,
845882
"__EGL_VENDOR_LIBRARY_DIRS", "glvnd/egl_vendor.d")
846883
}

0 commit comments

Comments
 (0)