2022-08-10 01:56:22 +01:00
|
|
|
use std::convert::Into;
|
|
|
|
use std::path::PathBuf;
|
2022-11-10 03:09:05 +00:00
|
|
|
//use std::sync::{Arc, Mutex};
|
2022-07-30 21:33:31 +01:00
|
|
|
|
2022-08-10 01:56:22 +01:00
|
|
|
use super::{Battery, Cpu, Gpu};
|
|
|
|
use super::{OnResume, OnSet, SettingError};
|
|
|
|
use crate::persist::{CpuJson, SettingsJson};
|
2022-11-10 03:09:05 +00:00
|
|
|
//use crate::utility::unwrap_lock;
|
2022-07-30 21:33:31 +01:00
|
|
|
|
|
|
|
const LATEST_VERSION: u64 = 0;
|
|
|
|
|
2022-08-10 01:56:22 +01:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum SettingVariant {
|
|
|
|
Battery,
|
|
|
|
Cpu,
|
|
|
|
Gpu,
|
|
|
|
General,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for SettingVariant {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Battery => write!(f, "Battery"),
|
|
|
|
Self::Cpu => write!(f, "CPU"),
|
|
|
|
Self::Gpu => write!(f, "GPU"),
|
|
|
|
Self::General => write!(f, "General"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 21:33:31 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2022-08-10 01:56:22 +01:00
|
|
|
pub struct General {
|
2022-07-30 21:33:31 +01:00
|
|
|
pub persistent: bool,
|
2022-08-10 01:56:22 +01:00
|
|
|
pub path: PathBuf,
|
|
|
|
pub name: String,
|
2022-07-30 21:33:31 +01:00
|
|
|
}
|
|
|
|
|
2022-08-26 22:00:43 +01:00
|
|
|
impl OnSet for General {
|
|
|
|
fn on_set(&mut self) -> Result<(), SettingError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 01:56:22 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Settings {
|
2022-11-10 03:09:05 +00:00
|
|
|
pub general: General,
|
|
|
|
pub cpus: Vec<Cpu>,
|
|
|
|
pub gpu: Gpu,
|
|
|
|
pub battery: Battery,
|
2022-08-10 01:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OnSet for Settings {
|
|
|
|
fn on_set(&mut self) -> Result<(), SettingError> {
|
2022-11-10 03:09:05 +00:00
|
|
|
self.battery.on_set()?;
|
|
|
|
for cpu in self.cpus.iter_mut() {
|
|
|
|
cpu.on_set()?;
|
2022-08-10 01:56:22 +01:00
|
|
|
}
|
2022-11-10 03:09:05 +00:00
|
|
|
self.gpu.on_set()?;
|
|
|
|
self.general.on_set()?;
|
2022-08-10 01:56:22 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
2022-07-30 21:33:31 +01:00
|
|
|
#[inline]
|
2022-08-10 01:56:22 +01:00
|
|
|
pub fn from_json(other: SettingsJson, json_path: PathBuf) -> Self {
|
2022-07-30 21:33:31 +01:00
|
|
|
match other.version {
|
|
|
|
0 => Self {
|
2022-11-10 03:09:05 +00:00
|
|
|
general: General {
|
2022-08-10 01:56:22 +01:00
|
|
|
persistent: other.persistent,
|
|
|
|
path: json_path,
|
|
|
|
name: other.name,
|
2022-11-10 03:09:05 +00:00
|
|
|
},
|
|
|
|
cpus: Self::convert_cpus(other.cpus, other.version),
|
|
|
|
gpu: Gpu::from_json(other.gpu, other.version),
|
|
|
|
battery: Battery::from_json(other.battery, other.version),
|
2022-07-30 21:33:31 +01:00
|
|
|
},
|
|
|
|
_ => Self {
|
2022-11-10 03:09:05 +00:00
|
|
|
general: General {
|
2022-08-10 01:56:22 +01:00
|
|
|
persistent: other.persistent,
|
|
|
|
path: json_path,
|
|
|
|
name: other.name,
|
2022-11-10 03:09:05 +00:00
|
|
|
},
|
|
|
|
cpus: Self::convert_cpus(other.cpus, other.version),
|
|
|
|
gpu: Gpu::from_json(other.gpu, other.version),
|
|
|
|
battery: Battery::from_json(other.battery, other.version),
|
2022-08-10 01:56:22 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn convert_cpus(mut cpus: Vec<CpuJson>, version: u64) -> Vec<Cpu> {
|
|
|
|
let mut result = Vec::with_capacity(cpus.len());
|
2022-09-06 02:56:15 +01:00
|
|
|
let max_cpus = Cpu::cpu_count();
|
2022-08-10 01:56:22 +01:00
|
|
|
for (i, cpu) in cpus.drain(..).enumerate() {
|
2022-09-06 02:56:15 +01:00
|
|
|
// prevent having more CPUs than available
|
|
|
|
if let Some(max_cpus) = max_cpus {
|
|
|
|
if i == max_cpus {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 01:56:22 +01:00
|
|
|
result.push(Cpu::from_json(cpu, version, i));
|
|
|
|
}
|
2022-09-06 02:56:15 +01:00
|
|
|
if let Some(max_cpus) = max_cpus {
|
|
|
|
if result.len() != max_cpus {
|
|
|
|
let mut sys_cpus = Cpu::system_default();
|
|
|
|
for i in result.len()..sys_cpus.len() {
|
|
|
|
result.push(sys_cpus.remove(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 01:56:22 +01:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn system_default(json_path: PathBuf) -> Self {
|
|
|
|
Self {
|
2022-11-10 03:09:05 +00:00
|
|
|
general: General {
|
2022-08-10 01:56:22 +01:00
|
|
|
persistent: false,
|
|
|
|
path: json_path,
|
2022-09-01 01:18:15 +01:00
|
|
|
name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
|
2022-11-10 03:09:05 +00:00
|
|
|
},
|
|
|
|
cpus: Cpu::system_default(),
|
|
|
|
gpu: Gpu::system_default(),
|
|
|
|
battery: Battery::system_default(),
|
2022-08-10 01:56:22 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-11 22:38:20 +01:00
|
|
|
|
2022-11-10 03:09:05 +00:00
|
|
|
pub fn load_system_default(&mut self) {
|
|
|
|
self.cpus = Cpu::system_default();
|
|
|
|
self.gpu = Gpu::system_default();
|
|
|
|
self.battery = Battery::system_default();
|
2022-10-11 22:38:20 +01:00
|
|
|
}
|
2022-08-26 22:00:43 +01:00
|
|
|
|
2022-11-10 03:09:05 +00:00
|
|
|
pub fn load_file(&mut self, filename: PathBuf, name: String, system_defaults: bool) -> Result<bool, SettingError> {
|
2022-09-05 19:24:01 +01:00
|
|
|
let json_path = crate::utility::settings_dir().join(filename);
|
2022-11-10 03:09:05 +00:00
|
|
|
//let mut general_lock = unwrap_lock(self.general.lock(), "general");
|
2022-08-26 22:00:43 +01:00
|
|
|
if json_path.exists() {
|
|
|
|
let settings_json = SettingsJson::open(&json_path).map_err(|e| SettingError {
|
|
|
|
msg: e.to_string(),
|
|
|
|
setting: SettingVariant::General,
|
|
|
|
})?;
|
2022-10-05 01:49:02 +01:00
|
|
|
if !settings_json.persistent {
|
2022-10-11 22:38:20 +01:00
|
|
|
log::warn!("Loaded persistent config `{}` ({}) with persistent=false", &settings_json.name, json_path.display());
|
2022-11-10 03:09:05 +00:00
|
|
|
self.general.persistent = false;
|
|
|
|
self.general.name = name;
|
2022-10-11 22:38:20 +01:00
|
|
|
} else {
|
2022-11-10 03:09:05 +00:00
|
|
|
self.cpus = Self::convert_cpus(settings_json.cpus, settings_json.version);
|
|
|
|
self.gpu = Gpu::from_json(settings_json.gpu, settings_json.version);
|
|
|
|
self.battery = Battery::from_json(settings_json.battery, settings_json.version);
|
|
|
|
self.general.persistent = true;
|
|
|
|
self.general.name = settings_json.name;
|
2022-10-05 01:49:02 +01:00
|
|
|
}
|
2022-08-26 22:00:43 +01:00
|
|
|
} else {
|
2022-10-11 22:38:20 +01:00
|
|
|
if system_defaults {
|
|
|
|
self.load_system_default();
|
|
|
|
}
|
2022-11-10 03:09:05 +00:00
|
|
|
self.general.persistent = false;
|
|
|
|
self.general.name = name;
|
2022-08-26 22:00:43 +01:00
|
|
|
}
|
2022-11-10 03:09:05 +00:00
|
|
|
self.general.path = json_path;
|
|
|
|
Ok(self.general.persistent)
|
2022-08-26 22:00:43 +01:00
|
|
|
}
|
2022-08-10 01:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OnResume for Settings {
|
|
|
|
fn on_resume(&self) -> Result<(), SettingError> {
|
2022-11-10 03:09:05 +00:00
|
|
|
log::debug!("Applying settings for on_resume");
|
|
|
|
self.battery.on_resume()?;
|
|
|
|
log::debug!("Resumed battery");
|
|
|
|
for cpu in self.cpus.iter() {
|
|
|
|
cpu.on_resume()?;
|
2022-07-30 21:33:31 +01:00
|
|
|
}
|
2022-11-10 03:09:05 +00:00
|
|
|
log::debug!("Resumed CPUs");
|
|
|
|
self.gpu.on_resume()?;
|
|
|
|
log::debug!("Resumed GPU");
|
2022-08-10 01:56:22 +01:00
|
|
|
Ok(())
|
2022-07-30 21:33:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<SettingsJson> for Settings {
|
|
|
|
#[inline]
|
2022-08-10 01:56:22 +01:00
|
|
|
fn into(self) -> SettingsJson {
|
2022-11-10 03:09:05 +00:00
|
|
|
log::debug!("Converting into json");
|
2022-07-30 21:33:31 +01:00
|
|
|
SettingsJson {
|
|
|
|
version: LATEST_VERSION,
|
2022-11-10 03:09:05 +00:00
|
|
|
name: self.general.name.clone(),
|
|
|
|
persistent: self.general.persistent,
|
|
|
|
cpus: self.cpus
|
2022-08-10 01:56:22 +01:00
|
|
|
.clone()
|
|
|
|
.drain(..)
|
|
|
|
.map(|cpu| cpu.into())
|
|
|
|
.collect(),
|
2022-11-10 03:09:05 +00:00
|
|
|
gpu: self.gpu.clone().into(),
|
|
|
|
battery: self.battery.clone().into(),
|
2022-07-30 21:33:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|