Skip to content

Commit

Permalink
fix: windows and macos build
Browse files Browse the repository at this point in the history
  • Loading branch information
koriwi committed Nov 2, 2022
1 parent e1b8478 commit c574dd9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
23 changes: 14 additions & 9 deletions src-tauri/src/lib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
#[cfg(target_os = "macos")]
pub fn get_current_window<F: FnOnce(&str) -> Option<PathBuf>>(
resolve_resource: F,
) -> Option<String> {
) -> Result<String> {
use std::process::Command;
let script_location = resolve_resource("./src/assets/macos_active_window.as").unwrap();
let mut command = Command::new("sh");
Expand All @@ -18,10 +18,9 @@ pub fn get_current_window<F: FnOnce(&str) -> Option<PathBuf>>(
let success = result.trim().len() > 0;

if success {
return Some(result);
return Ok(result);
}
println!("failed to get active window, remove and readd freedeck-configurator to accessibility list?");
return None;
Err(anyhow!("failed to get active window, remove and readd freedeck-configurator to accessibility list?"))
}

#[cfg(target_os = "linux")]
Expand All @@ -48,19 +47,25 @@ pub fn get_current_window<F: FnOnce(&str) -> Option<PathBuf>>(
#[cfg(target_os = "windows")]
pub fn get_current_window<F: FnOnce(&str) -> Option<PathBuf>>(
_resolve_resource: F,
) -> Option<String> {
) -> Result<String> {
use std::{ffi::OsString, os::windows::prelude::OsStringExt};
use winapi::um::winuser::{GetForegroundWindow, GetWindowTextW};
unsafe {
let window = GetForegroundWindow();
let mut text: [u16; 512] = [0; 512];
let _result: usize = GetWindowTextW(window, text.as_mut_ptr(), text.len() as i32) as usize;
let (short, _) = text.split_at(_result);
let result = OsString::from_wide(&short).to_str().unwrap().to_string();
let success = result.trim().len() > 0;
let result = OsString::from_wide(short)
.to_str()
.expect("os string conversion error")
.to_string();
let success = !result.trim().is_empty();
if success {
return Some(OsString::from_wide(&short).to_str().unwrap().to_string());
return Ok(OsString::from_wide(short)
.to_str()
.expect("os string conversion error")
.to_string());
}
}
None
Err(anyhow!("windows did an oopsy"))
}
51 changes: 31 additions & 20 deletions src-tauri/src/lib/system/win.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{collections::HashMap, sync::Arc};

use anyhow::Result;
use std::collections::HashMap;
use wmi::*;

// struct SaveCOM(COMLibrary);
Expand All @@ -10,48 +10,59 @@ pub struct SystemInfo {
}

impl SystemInfo {
pub fn new() -> Option<SystemInfo> {
match WMIConnection::with_namespace_path(
"root\\OpenHardwareMonitor",
COMLibrary::new().unwrap(),
) {
Ok(sys) => Some(SystemInfo { sys }),
Err(_) => None,
}
pub fn new() -> Result<SystemInfo> {
let lib = COMLibrary::new()?;
let connection = WMIConnection::with_namespace_path("root\\OpenHardwareMonitor", lib)?;
Ok(SystemInfo { sys: connection })
}
pub fn cpu_temp(&mut self) -> f32 {
let cpu_results: Vec<HashMap<String, Variant>> =
self.sys.raw_query("SELECT * FROM Sensor where Identifier = '/amdcpu/0/temperature/0' or Identifier = '/intelcpu/0/temperature/0' AND SensorType = 'Temperature'").unwrap();
if cpu_results.len() == 0 {
let cpu_results: Result<Vec<HashMap<String, Variant>>, WMIError> =
self.sys.raw_query("SELECT * FROM Sensor where Identifier = '/amdcpu/0/temperature/0' or Identifier = '/intelcpu/0/temperature/0' AND SensorType = 'Temperature'");

let cpu_results = match cpu_results {
Ok(result) => result,
Err(_e) => return 0.0,
};

if cpu_results.is_empty() {
0.0
} else {
let cpu = &cpu_results[0];
if let Some(Variant::R4(value)) = cpu.get("Value") {
value.clone()
value.to_owned()
} else {
0.0
}
}
}
pub fn gpu_temp(&mut self) -> f32 {
let gpu_results: Vec<HashMap<String, Variant>> =
self.sys.raw_query("SELECT * FROM Sensor where Identifier = '/atigpu/0/temperature/0' or Identifier = '/nvidiagpu/0/temperature/0' AND SensorType = 'Temperature'").unwrap();
if gpu_results.len() == 0 {
let gpu_results: Result<Vec<HashMap<String, Variant>>, WMIError> =
self.sys.raw_query("SELECT * FROM Sensor where Identifier = '/atigpu/0/temperature/0' or Identifier = '/nvidiagpu/0/temperature/0' AND SensorType = 'Temperature'");

let gpu_results = match gpu_results {
Ok(result) => result,
Err(_e) => return 0.0,
};

if gpu_results.is_empty() {
0.0
} else {
let cpu = &gpu_results[0];
if let Some(Variant::R4(value)) = cpu.get("Value") {
value.clone()
value.to_owned()
} else {
0.0
}
}
}
pub fn list_sensors(&mut self) -> Vec<String> {
let sensor_list_result: Vec<HashMap<String, Variant>> = self
let sensor_list_result: Vec<HashMap<String, Variant>> = match self
.sys
.raw_query("SELECT * FROM Sensor where SensorType='Temperature'")
.unwrap();
{
Ok(result) => result,
Err(e) => return vec![e.to_string()],
};

sensor_list_result
.iter()
Expand Down

0 comments on commit c574dd9

Please sign in to comment.