From 1ddf98ea91c333bfd049fc06b317de69877b92aa Mon Sep 17 00:00:00 2001 From: beerpsi Date: Sun, 24 Dec 2023 04:05:51 +0700 Subject: [PATCH] more separation --- src/lib.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c686185..2b691b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,18 +24,19 @@ use std::{ fmt::Display, sync::{ atomic::{AtomicBool, Ordering}, - Arc, OnceLock, + Arc, }, - thread, - time::Duration, }; #[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] -use std::{sync::Mutex, thread::JoinHandle}; +use std::{ + sync::{Mutex, OnceLock}, + thread::{self, JoinHandle}, + time::Duration, +}; -use ::log::{error, info}; +use ::log::error; use anyhow::{anyhow, Result}; use lazy_static::lazy_static; -use rusb::{DeviceHandle, GlobalContext}; use shared_memory::{Shmem, ShmemConf, ShmemError}; #[cfg(any( all(feature = "chusan", target_arch = "x86_64"), @@ -52,14 +53,21 @@ use winapi::{ winnt::{DLL_PROCESS_ATTACH, HRESULT}, }, }; +#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +use { + ::log::info, + rusb::{DeviceHandle, GlobalContext}, +}; use crate::{configuration::Configuration, log::init_logger}; +static mut INPUT_SHMEM: Option> = None; + #[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] type SliderCallbackFn = unsafe extern "C" fn(data: *const u8); +#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] static DEVICE: OnceLock> = OnceLock::new(); -static mut INPUT_SHMEM: Option> = None; #[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] static mut SLIDER_THREAD: OnceLock> = OnceLock::new(); @@ -136,7 +144,8 @@ pub extern "C" fn chuni_io_get_api_version() -> u16 { not(feature = "chusan") ))] pub extern "C" fn chuni_io_jvs_init() -> HRESULT { - if cfg!(not(feature = "chusan")) { + #[cfg(not(feature = "chusan"))] + { if let Err(e) = yubideck_init() { error!("Could not initialize YubiDeck: {e:#?}"); return E_FAIL; @@ -232,7 +241,8 @@ pub unsafe extern "C" fn chuni_io_slider_init() -> HRESULT { } } - if cfg!(not(feature = "chusan")) { + #[cfg(not(feature = "chusan"))] + { // Already initialized in chuni_io_jvs_init() return S_OK; } @@ -443,6 +453,7 @@ where .map_err(|e| anyhow!("Failed to create/open shared memory {os_id}: {e:#}")) } +#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] fn yubideck_init() -> Result<()> { let Some(mut device) = rusb::open_device_with_vid_pid(0x1973, 0x2001) else { return Err(anyhow!("YubiDeck not found.")); @@ -459,6 +470,7 @@ fn yubideck_init() -> Result<()> { Ok(()) } +#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] fn input_thread_proc() { info!("Input thread started");