use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActiveAppProfile {
pub pid: u32,
pub name: String,
pub exe_path: Option<String>,
pub has_nvidia_drs_profile: bool,
}
impl ActiveAppProfile {
pub fn has_any_profile(&self) -> bool {
self.has_nvidia_drs_profile
}
}
pub fn active_profiles_for_processes() -> Vec<ActiveAppProfile> {
let nvidia_set: std::collections::BTreeSet<String> = {
#[cfg(windows)]
{
super::nvidia_drs::drs_exe_set()
}
#[cfg(not(windows))]
{
std::collections::BTreeSet::new()
}
};
let processes = match crate::process_monitor::ProcessMonitor::new() {
Ok(mut m) => m.processes().unwrap_or_default(),
Err(_) => return Vec::new(),
};
let mut seen = std::collections::HashSet::new();
let mut out = Vec::new();
for p in processes {
let lower = p.name.to_ascii_lowercase();
let key = if lower.ends_with(".exe") {
lower.clone()
} else {
format!("{}.exe", lower)
};
if !seen.insert((p.pid, key.clone())) {
continue;
}
let has_nvidia = nvidia_set.contains(&key);
out.push(ActiveAppProfile {
pid: p.pid,
name: p.name.clone(),
exe_path: None, has_nvidia_drs_profile: has_nvidia,
});
}
out.sort_by(|a, b| {
b.has_any_profile().cmp(&a.has_any_profile()).then_with(|| {
a.name
.to_ascii_lowercase()
.cmp(&b.name.to_ascii_lowercase())
})
});
out
}
pub fn matched_active_profiles() -> Vec<ActiveAppProfile> {
active_profiles_for_processes()
.into_iter()
.filter(ActiveAppProfile::has_any_profile)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn smoke() {
let _ = active_profiles_for_processes();
let _ = matched_active_profiles();
}
#[test]
fn matched_subset_of_full() {
let all = active_profiles_for_processes();
let matched = matched_active_profiles();
assert!(matched.len() <= all.len());
for m in &matched {
assert!(m.has_any_profile());
}
}
}