pub use crate::error::Result;
use crate::error::{Error, ErrorKind, MutexExt, MutexType};
use crate::hal::check::check_gx_status;
use crate::raw::gx_interface::*;
use std::sync::{Arc, LazyLock, Mutex};
pub static GXI: LazyLock<Arc<Mutex<Option<GXInstance>>>> =
LazyLock::new(|| Arc::new(Mutex::new(None)));
pub fn gxi_check<T, F>(func: F) -> Result<T>
where
F: FnOnce(&GXInstance) -> Result<T>,
{
let gxi = GXI.lock_safe(MutexType::Gxi)?;
if let Some(gxi) = gxi.as_ref() {
func(gxi)
} else {
Err(Error::new(ErrorKind::GxiError(
"GXI is None while gxi_check(). Please check your gxci_init situation.".to_string(),
)))
}
}
pub fn gxci_init(dll_path: &str) -> Result<()> {
println!("Initializing GXI with DLL path: {}", dll_path);
let mut gxi = GXI.lock_safe(MutexType::Gxi)?;
if gxi.is_some() {
println!("Warning: GXI is already initialized. Reinitializing.");
}
*gxi = Some(GXInstance::new(dll_path)?);
let status = gxi.as_ref().unwrap().gx_init_lib()?;
check_gx_status(status)?;
Ok(())
}
pub fn gxci_init_default() -> Result<()> {
let dll_path = std::env::var("GXCI_DLL_PATH").unwrap_or_else(|_| {
if cfg!(windows) {
"C:\\Program Files\\Daheng Imaging\\GalaxySDK\\APIDll\\Win64\\GxIAPI.dll".to_string()
} else if cfg!(unix) {
"/usr/lib/libgxiapi.so".to_string()
} else {
panic!("Unsupported platform: no default DLL path. Set GXCI_DLL_PATH env var.");
}
});
gxci_init(&dll_path)
}
pub fn gxci_close() -> Result<()> {
gxi_check(|gxi| gxi.gx_close_lib())?;
let mut gxi = GXI.lock_safe(MutexType::Gxi)?;
*gxi = None;
Ok(())
}