From c21b3a8fe994e34b618f6811d34db06fcf907869 Mon Sep 17 00:00:00 2001 From: Serge Poltavski Date: Tue, 7 May 2024 12:11:11 +0300 Subject: [PATCH] sysinfo updates --- ceammc/ext/ceammc_objects.txt | 1 - ceammc/extra/rust/hw/.vscode/settings.json | 1 + ceammc/extra/rust/hw/Cargo.toml | 1 - ceammc/extra/rust/hw/hw_rust.hpp | 26 +-- ceammc/extra/rust/hw/src/lib.rs | 1 - ceammc/extra/rust/hw/src/sysinfo.rs | 146 ---------------- .../extra/rust/system/.vscode/settings.json | 3 +- ceammc/extra/rust/system/Cargo.toml | 1 + ceammc/extra/rust/system/src/lib.rs | 14 ++ ceammc/extra/rust/system/src/sysinfo.rs | 158 ++++++++++++++++++ ceammc/extra/rust/system/system_rust.hpp | 21 +++ 11 files changed, 204 insertions(+), 169 deletions(-) delete mode 100644 ceammc/extra/rust/hw/src/sysinfo.rs create mode 100644 ceammc/extra/rust/system/src/sysinfo.rs diff --git a/ceammc/ext/ceammc_objects.txt b/ceammc/ext/ceammc_objects.txt index f28a545717..1ed58595d0 100644 --- a/ceammc/ext/ceammc_objects.txt +++ b/ceammc/ext/ceammc_objects.txt @@ -634,7 +634,6 @@ http.send hw.apple_smc hw.apple_sms hw.arduino -hw.cpu_temp hw.display hw.gamepad hw.kbd_light diff --git a/ceammc/extra/rust/hw/.vscode/settings.json b/ceammc/extra/rust/hw/.vscode/settings.json index fd37514886..2c9287a767 100644 --- a/ceammc/extra/rust/hw/.vscode/settings.json +++ b/ceammc/extra/rust/hw/.vscode/settings.json @@ -9,6 +9,7 @@ "gamepads", "gilrs", "listdev", + "pdfium", "powerinfo", "powerstate", "repr", diff --git a/ceammc/extra/rust/hw/Cargo.toml b/ceammc/extra/rust/hw/Cargo.toml index 5a3112b120..27b09cbf64 100644 --- a/ceammc/extra/rust/hw/Cargo.toml +++ b/ceammc/extra/rust/hw/Cargo.toml @@ -13,7 +13,6 @@ c_str_macro = "1.0.3" env_logger = "0.11.3" gilrs = "0.10.5" log = "0.4.21" -sysinfo = "0.30.12" tokio = { version = "1.36", features = ["rt", "rt-multi-thread", "signal", "sync", "macros", "time"] } [target.'cfg(windows)'.dependencies] diff --git a/ceammc/extra/rust/hw/hw_rust.hpp b/ceammc/extra/rust/hw/hw_rust.hpp index 8f51ca76de..48c32147c3 100644 --- a/ceammc/extra/rust/hw/hw_rust.hpp +++ b/ceammc/extra/rust/hw/hw_rust.hpp @@ -70,18 +70,9 @@ enum class ceammc_hw_printer_state { UNKNOWN, }; -struct ceammc_hw_cputemp; - /// gamepad opaque type struct ceammc_hw_gamepad; -struct ceammc_hw_notify_cb { - /// dispatcher ID - size_t id; - /// dispatcher callback (not NULL!) - void (*f)(size_t id); -}; - struct ceammc_gamepad_err_cb { /// pointer to user data void *user; @@ -132,6 +123,13 @@ struct ceammc_gamepad_listdev_cb { void (*cb)(void *user, const ceammc_gamepad_dev_info *info); }; +struct ceammc_hw_notify_cb { + /// dispatcher ID + size_t id; + /// dispatcher callback (not NULL!) + void (*f)(size_t id); +}; + struct ceammc_hw_printer_info { const char *name; const char *system_name; @@ -163,16 +161,6 @@ struct ceammc_hw_error_cb { extern "C" { -ceammc_hw_cputemp *ceammc_hw_cputemp_create(void *user, - void (*cb_temp)(void *user, const char *label, float cpu_temp), - ceammc_hw_notify_cb cb_notify); - -void ceammc_hw_cputemp_free(ceammc_hw_cputemp *cpu_temp); - -bool ceammc_hw_cputemp_get(ceammc_hw_cputemp *cpu_temp); - -bool ceammc_hw_cputemp_process(ceammc_hw_cputemp *cpu_temp); - /// free gamepad /// @param gp - pointer to gp void ceammc_hw_gamepad_free(ceammc_hw_gamepad *gp); diff --git a/ceammc/extra/rust/hw/src/lib.rs b/ceammc/extra/rust/hw/src/lib.rs index f05e793e65..3692f548c3 100644 --- a/ceammc/extra/rust/hw/src/lib.rs +++ b/ceammc/extra/rust/hw/src/lib.rs @@ -19,7 +19,6 @@ impl hw_notify_cb { } pub mod gamepad; -pub mod sysinfo; pub mod printers; #[repr(C)] diff --git a/ceammc/extra/rust/hw/src/sysinfo.rs b/ceammc/extra/rust/hw/src/sysinfo.rs deleted file mode 100644 index f3bb965a6e..0000000000 --- a/ceammc/extra/rust/hw/src/sysinfo.rs +++ /dev/null @@ -1,146 +0,0 @@ -use std::{ - ffi::CString, - os::raw::{c_char, c_void}, - time::Duration, -}; - -use sysinfo::Components; - -use crate::hw_notify_cb; - -enum CpuTempReply { - Temp(CString, f32), -} - -enum CpuTempRequest { - GetTemp, - Quit, -} - -#[allow(non_camel_case_types)] -pub struct hw_cputemp { - rx: std::sync::mpsc::Receiver, - tx: std::sync::mpsc::Sender, - user: *mut c_void, - cb_temp: Option, -} - -impl Drop for hw_cputemp { - fn drop(&mut self) { - if let Err(err) = self.tx.send(CpuTempRequest::Quit) { - log::error!("send error: {err}"); - } - } -} - -#[no_mangle] -pub extern "C" fn ceammc_hw_cputemp_create( - user: *mut c_void, - cb_temp: Option, - cb_notify: hw_notify_cb, -) -> *mut hw_cputemp { - let (reply_tx, reply_rx) = std::sync::mpsc::channel::(); - let (req_tx, req_rx) = std::sync::mpsc::channel::(); - - std::thread::spawn(move || { - let mut components = Components::new_with_refreshed_list(); - loop { - match req_rx.recv_timeout(Duration::from_millis(100)) { - Ok(data) => match data { - CpuTempRequest::Quit => { - log::debug!("[worker] quit"); - break; - } - CpuTempRequest::GetTemp => { - for component in &mut components.iter_mut() { - component.refresh(); - - let str = component.label(); - - #[cfg(target_os = "macos")] - { - if !str.starts_with("GPU") && !str.starts_with("pACC") && !str.starts_with("eACC") { - continue; - } - } - - let lbl = CString::new(str).unwrap_or_default(); - - let temp = component.temperature(); - if temp.is_nan() { // skip - continue; - } - if let Err(err) = reply_tx.send(CpuTempReply::Temp(lbl, temp)) { - log::error!("[worker] send error: {err}"); - break; - } - - cb_notify.notify(); - } - } - }, - Err(err) => match err { - std::sync::mpsc::RecvTimeoutError::Timeout => continue, - std::sync::mpsc::RecvTimeoutError::Disconnected => break, - }, - } - } - }); - - Box::into_raw(Box::new(hw_cputemp { - rx: reply_rx, - tx: req_tx, - user, - cb_temp, - })) -} - -#[no_mangle] -pub extern "C" fn ceammc_hw_cputemp_get(cpu_temp: *mut hw_cputemp) -> bool { - if cpu_temp.is_null() { - log::error!("NULL cpu temp pointer"); - return false; - } - - let cpu_temp = unsafe { &mut *cpu_temp }; - match cpu_temp.tx.send(CpuTempRequest::GetTemp) { - Ok(_) => true, - Err(err) => { - log::error!("[owner] send error: {err}"); - false - } - } -} - -#[no_mangle] -pub extern "C" fn ceammc_hw_cputemp_process(cpu_temp: *mut hw_cputemp) -> bool { - if cpu_temp.is_null() { - log::error!("NULL cpu temp pointer"); - return false; - } - - let cpu_temp = unsafe { &mut *cpu_temp }; - loop { - match cpu_temp.rx.try_recv() { - Ok(reply) => match reply { - CpuTempReply::Temp(label, value) => cpu_temp - .cb_temp - .map(|f| f(cpu_temp.user, label.as_ptr(), value)), - }, - Err(err) => match err { - std::sync::mpsc::TryRecvError::Empty => return true, - std::sync::mpsc::TryRecvError::Disconnected => { - log::error!("[owner] process results: {err}"); - return false; - } - }, - }; - } -} - -#[no_mangle] -pub extern "C" fn ceammc_hw_cputemp_free(cpu_temp: *mut hw_cputemp) { - if !cpu_temp.is_null() { - drop(unsafe { Box::from_raw(cpu_temp) }); - } -} diff --git a/ceammc/extra/rust/system/.vscode/settings.json b/ceammc/extra/rust/system/.vscode/settings.json index 61f7095d56..3d89b6faff 100644 --- a/ceammc/extra/rust/system/.vscode/settings.json +++ b/ceammc/extra/rust/system/.vscode/settings.json @@ -1,5 +1,6 @@ { "cSpell.words": [ - "FUDI" + "FUDI", + "sysinfo" ] } \ No newline at end of file diff --git a/ceammc/extra/rust/system/Cargo.toml b/ceammc/extra/rust/system/Cargo.toml index 7edf410173..e00de5c032 100644 --- a/ceammc/extra/rust/system/Cargo.toml +++ b/ceammc/extra/rust/system/Cargo.toml @@ -15,6 +15,7 @@ lazy_static = "1.4.0" log = "0.4.21" logging = "0.1.0" regex = "1.10.4" +sysinfo = "0.30.12" # c_str_macro = "1.0.3" # gilrs = "0.10.5" diff --git a/ceammc/extra/rust/system/src/lib.rs b/ceammc/extra/rust/system/src/lib.rs index 9fe79612b6..314d6f0201 100644 --- a/ceammc/extra/rust/system/src/lib.rs +++ b/ceammc/extra/rust/system/src/lib.rs @@ -1 +1,15 @@ pub mod command; +pub mod sysinfo; + +#[allow(non_camel_case_types)] +#[repr(C)] +pub struct system_notify_cb { + client_id: usize, + cb: Option, +} + +impl system_notify_cb { + fn notify(&self) { + self.cb.map(|f| f(self.client_id)); + } +} diff --git a/ceammc/extra/rust/system/src/sysinfo.rs b/ceammc/extra/rust/system/src/sysinfo.rs new file mode 100644 index 0000000000..ac017748b0 --- /dev/null +++ b/ceammc/extra/rust/system/src/sysinfo.rs @@ -0,0 +1,158 @@ +use std::{ + ffi::CString, + os::raw::{c_char, c_void}, + time::Duration, +}; + +use sysinfo::Components; + +use crate::system_notify_cb; + +#[allow(non_camel_case_types)] +#[repr(C)] +pub struct sysinfo_temp_cb { + user: *mut c_void, + cb: Option, +} + +impl sysinfo_temp_cb { + fn exec(&self, label: &CString, value: f32) { + self.cb.map(|f| f(self.user, label.as_ptr(), value)); + } +} + +enum SysInfoReply { + Temperature(CString, f32), +} + +enum SysInfoRequest { + GetTemp, + Quit, +} + +#[allow(non_camel_case_types)] +pub struct system_info { + rx: std::sync::mpsc::Receiver, + tx: std::sync::mpsc::Sender, + cb_temp: sysinfo_temp_cb, +} + +impl system_info { + fn request(&self, req: SysInfoRequest) -> bool { + match self.tx.send(req) { + Ok(_) => true, + Err(err) => { + log::error!("[owner] send error: {err}"); + false + } + } + } + + fn ptr_request(sysinfo: *mut Self, req: SysInfoRequest) -> bool { + if sysinfo.is_null() { + log::error!("NULL cpu temp pointer"); + return false; + } + + let sysinfo = unsafe { &mut *sysinfo }; + sysinfo.request(req) + } +} + +impl Drop for system_info { + fn drop(&mut self) { + if let Err(err) = self.tx.send(SysInfoRequest::Quit) { + log::error!("send error: {err}"); + } + } +} + +#[no_mangle] +pub extern "C" fn ceammc_sysinfo_create( + cb_temp: sysinfo_temp_cb, + cb_notify: system_notify_cb, +) -> *mut system_info { + let (reply_tx, reply_rx) = std::sync::mpsc::channel::(); + let (req_tx, req_rx) = std::sync::mpsc::channel::(); + + std::thread::spawn(move || { + let mut components = Components::new_with_refreshed_list(); + loop { + match req_rx.recv_timeout(Duration::from_millis(100)) { + Ok(data) => match data { + SysInfoRequest::Quit => { + log::debug!("[worker] quit"); + break; + } + SysInfoRequest::GetTemp => { + log::debug!("[worker] get temperature"); + for component in &mut components.iter_mut() { + component.refresh(); + + let str = component.label(); + let lbl = CString::new(str).unwrap_or_default(); + + let temp = component.temperature(); + if temp.is_nan() { + // skip + continue; + } + if let Err(err) = reply_tx.send(SysInfoReply::Temperature(lbl, temp)) { + log::error!("[worker] send error: {err}"); + break; + } + + cb_notify.notify(); + } + } + }, + Err(err) => match err { + std::sync::mpsc::RecvTimeoutError::Timeout => continue, + std::sync::mpsc::RecvTimeoutError::Disconnected => break, + }, + } + } + }); + + Box::into_raw(Box::new(system_info { + rx: reply_rx, + tx: req_tx, + cb_temp, + })) +} + +#[no_mangle] +pub extern "C" fn ceammc_sysinfo_get_temperature(sysinfo: *mut system_info) -> bool { + system_info::ptr_request(sysinfo, SysInfoRequest::GetTemp) +} + +#[no_mangle] +pub extern "C" fn ceammc_sysinfo_process(sysinfo: *mut system_info) -> bool { + if sysinfo.is_null() { + log::error!("NULL cpu temp pointer"); + return false; + } + + let sysinfo = unsafe { &mut *sysinfo }; + loop { + match sysinfo.rx.try_recv() { + Ok(reply) => match reply { + SysInfoReply::Temperature(label, value) => sysinfo.cb_temp.exec(&label, value), + }, + Err(err) => match err { + std::sync::mpsc::TryRecvError::Empty => return true, + std::sync::mpsc::TryRecvError::Disconnected => { + log::error!("[owner] process results: {err}"); + return false; + } + }, + }; + } +} + +#[no_mangle] +pub extern "C" fn ceammc_sysinfo_free(sysinfo: *mut system_info) { + if !sysinfo.is_null() { + drop(unsafe { Box::from_raw(sysinfo) }); + } +} diff --git a/ceammc/extra/rust/system/system_rust.hpp b/ceammc/extra/rust/system/system_rust.hpp index 739b5bcbf1..a9c9a0e3a2 100644 --- a/ceammc/extra/rust/system/system_rust.hpp +++ b/ceammc/extra/rust/system/system_rust.hpp @@ -20,8 +20,20 @@ enum class ceammc_system_process_state { Ready, }; +struct ceammc_system_info; + struct ceammc_system_process; +struct ceammc_sysinfo_temp_cb { + void *user; + void (*cb)(void *user, const char *label, float cpu_temp); +}; + +struct ceammc_system_notify_cb { + size_t client_id; + void (*cb)(size_t client_id); +}; + struct ceammc_system_process_cmd { const char *prog; const char *const *argv; @@ -31,6 +43,15 @@ struct ceammc_system_process_cmd { extern "C" { +ceammc_system_info *ceammc_sysinfo_create(ceammc_sysinfo_temp_cb cb_temp, + ceammc_system_notify_cb cb_notify); + +void ceammc_sysinfo_free(ceammc_system_info *sysinfo); + +bool ceammc_sysinfo_get_temperature(ceammc_system_info *sysinfo); + +bool ceammc_sysinfo_process(ceammc_system_info *sysinfo); + /// clear current system command (kill if running) /// @param proc - system command pointer bool ceammc_system_process_clear(ceammc_system_process *proc);