From 4c06e76982c881dc49f59a632a5eb3cbf69039fd Mon Sep 17 00:00:00 2001 From: beerpsi Date: Tue, 26 Dec 2023 11:57:51 +0700 Subject: [PATCH] turns out, vscode does properly dim inactive code --- .gitignore | 1 - Cargo.lock | 223 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 + build.rs | 9 +++ src/lib.rs | 94 ++++++++-------------- 5 files changed, 266 insertions(+), 64 deletions(-) create mode 100644 Cargo.lock create mode 100644 build.rs diff --git a/.gitignore b/.gitignore index c98f1e9..4b89f14 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /target -/Cargo.lock # Created by https://www.toptal.com/developers/gitignore/api/jetbrains # Edit at https://www.toptal.com/developers/gitignore?templates=jetbrains diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..36a28ce --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,223 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59d2a3357dde987206219e78ecfbbb6e8dad06cbb65292758d3270e6254f7355" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cfg_aliases" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77e53693616d3075149f4ead59bdeecd204ac6b8192d8969757601b74bddf00f" + +[[package]] +name = "chuniio-yubideck" +version = "0.1.0" +dependencies = [ + "anyhow", + "cfg_aliases", + "env_logger", + "lazy_static", + "log", + "rusb", + "shared_memory", + "winapi", +] + +[[package]] +name = "env_logger" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" +dependencies = [ + "log", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" + +[[package]] +name = "libusb1-sys" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d0e2afce4245f2c9a418511e5af8718bcaf2fa408aefb259504d1a9cb25f27" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "memoffset" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] + +[[package]] +name = "nix" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f3790c00a0150112de0f4cd161e3d7fc4b2d8a5542ffc35f099a2562aecb35c" +dependencies = [ + "bitflags", + "cc", + "cfg-if", + "libc", + "memoffset", +] + +[[package]] +name = "pkg-config" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a" + +[[package]] +name = "rusb" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45fff149b6033f25e825cbb7b2c625a11ee8e6dac09264d49beb125e39aa97bf" +dependencies = [ + "libc", + "libusb1-sys", +] + +[[package]] +name = "shared_memory" +version = "0.12.4-chuniio" +dependencies = [ + "cfg-if", + "libc", + "nix", + "win-sys", +] + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "win-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b7b128a98c1cfa201b09eb49ba285887deb3cbe7466a98850eb1adabb452be5" +dependencies = [ + "windows", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45296b64204227616fdbf2614cefa4c236b98ee64dfaaaa435207ed99fe7829f" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17cffbe740121affb56fad0fc0e421804adf0ae00891205213b5cecd30db881d" + +[[package]] +name = "windows_i686_gnu" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2564fde759adb79129d9b4f54be42b32c89970c18ebf93124ca8870a498688ed" + +[[package]] +name = "windows_i686_msvc" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cd9d32ba70453522332c14d38814bceeb747d80b3958676007acadd7e166956" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfce6deae227ee8d356d19effc141a509cc503dfd1f850622ec4b0f84428e1f4" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d19538ccc21819d01deaf88d6a17eae6596a12e9aafdbb97916fb49896d89de9" diff --git a/Cargo.toml b/Cargo.toml index 63752cb..c7c1b0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,6 @@ log = "0.4.20" rusb = "0.9.3" shared_memory = { path = "vendor/shared_memory" } winapi = { version = "0.3.9", features = ["minwindef", "winnt", "winerror", "debugapi", "winbase", "winuser"] } + +[build-dependencies] +cfg_aliases = "0.2.0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..4333d2e --- /dev/null +++ b/build.rs @@ -0,0 +1,9 @@ +use cfg_aliases::cfg_aliases; + +fn main() { + cfg_aliases! { + chuni: { not(feature = "chusan") }, + amdaemon: { all(feature = "chusan", target_pointer_width = "64") }, + chusanapp: { all(feature = "chusan", target_pointer_width = "32") }, + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 37fa296..6a1a828 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,23 +1,7 @@ -// I swear the code down below looks way better in a proper editor -// that greys out disabled code. -// -// While I'd love to make use of something like cfg_aliases, IDEs -// are not intelligent enough yet. -// -// To aid reading this code, I have also adapted a convention of -// having the original function first, then the stub function after. -// -// Legend of common cfg flags: -// - any(all(feature = "chusan", target_arch = "x86_64"), not(feature = "chusan")): For amdaemon usage -// - any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")): For chusanapp usage - mod configuration; mod log; -#[cfg(any( - all(feature = "chusan", target_arch = "x86_64"), - not(feature = "chusan") -))] +#[cfg(any(chuni, amdaemon))] use std::{ffi::c_int, sync::atomic::AtomicU16}; use std::{ ffi::{c_void, CString}, @@ -27,7 +11,7 @@ use std::{ Arc, }, }; -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] use std::{ sync::{Mutex, OnceLock}, thread::{self, JoinHandle}, @@ -38,10 +22,7 @@ use ::log::error; use anyhow::{anyhow, Result}; use lazy_static::lazy_static; use shared_memory::{Shmem, ShmemConf, ShmemError}; -#[cfg(any( - all(feature = "chusan", target_arch = "x86_64"), - not(feature = "chusan") -))] +#[cfg(any(chuni, amdaemon))] use winapi::um::winuser::GetAsyncKeyState; use winapi::{ shared::{ @@ -53,7 +34,7 @@ use winapi::{ winnt::{DLL_PROCESS_ATTACH, HRESULT}, }, }; -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] use { ::log::info, rusb::{DeviceHandle, GlobalContext}, @@ -63,33 +44,28 @@ use crate::{configuration::Configuration, log::init_logger}; static mut INPUT_SHMEM: Option> = None; -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] type SliderCallbackFn = unsafe extern "C" fn(data: *const u8); -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] static DEVICE: OnceLock> = OnceLock::new(); -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] static mut SLIDER_THREAD: OnceLock> = OnceLock::new(); -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] static SLIDER_ACTIVE: AtomicBool = AtomicBool::new(false); -#[cfg(any( - all(feature = "chusan", target_arch = "x86_64"), - not(feature = "chusan") -))] +#[cfg(any(chuni, amdaemon))] static COIN_COUNT: AtomicU16 = AtomicU16::new(0); -#[cfg(any( - all(feature = "chusan", target_arch = "x86_64"), - not(feature = "chusan") -))] +#[cfg(any(chuni, amdaemon))] static COIN_PRESSED: AtomicBool = AtomicBool::new(false); -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] static mut OUTPUT_SHMEM: Option>> = None; +#[cfg(any(chuni, amdaemon))] lazy_static! { static ref CONFIGURATION: Configuration = { let io3 = CString::new("io3").unwrap(); @@ -139,10 +115,7 @@ pub extern "C" fn chuni_io_get_api_version() -> u16 { } #[no_mangle] -#[cfg(any( - all(feature = "chusan", target_arch = "x86_64"), - not(feature = "chusan") -))] +#[cfg(any(chuni, amdaemon))] pub extern "C" fn chuni_io_jvs_init() -> HRESULT { #[cfg(not(feature = "chusan"))] { @@ -156,16 +129,13 @@ pub extern "C" fn chuni_io_jvs_init() -> HRESULT { } #[no_mangle] -#[cfg(all(feature = "chusan", target_arch = "x86"))] +#[cfg(chusanapp)] pub extern "C" fn chuni_io_jvs_init() -> HRESULT { S_OK } #[no_mangle] -#[cfg(any( - all(feature = "chusan", target_arch = "x86_64"), - not(feature = "chusan") -))] +#[cfg(any(chuni, amdaemon))] pub extern "C" fn chuni_io_jvs_poll(opbtn: *mut u8, beams: *mut u8) { if opbtn.is_null() || beams.is_null() { return; @@ -193,14 +163,11 @@ pub extern "C" fn chuni_io_jvs_poll(opbtn: *mut u8, beams: *mut u8) { } #[no_mangle] -#[cfg(all(feature = "chusan", target_arch = "x86"))] +#[cfg(chusanapp)] pub extern "C" fn chuni_io_jvs_poll(_opbtn: *mut u8, _beams: *mut u8) {} #[no_mangle] -#[cfg(any( - all(feature = "chusan", target_arch = "x86_64"), - not(feature = "chusan") -))] +#[cfg(any(chuni, amdaemon))] pub extern "C" fn chuni_io_jvs_read_coin_counter(total: *mut u16) { if total.is_null() { return; @@ -227,11 +194,12 @@ pub extern "C" fn chuni_io_jvs_read_coin_counter(total: *mut u16) { } #[no_mangle] -#[cfg(all(feature = "chusan", target_arch = "x86"))] +#[cfg(chusanapp)] pub extern "C" fn chuni_io_jvs_read_coin_counter(_total: *mut u16) {} #[no_mangle] -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[allow(unreachable_code)] +#[cfg(any(chuni, chusanapp))] pub unsafe extern "C" fn chuni_io_slider_init() -> HRESULT { match create_shared_memory("Local\\YubideckOutput", 122, true) { Ok(s) => OUTPUT_SHMEM = Some(Arc::new(Mutex::new(s))), @@ -256,13 +224,13 @@ pub unsafe extern "C" fn chuni_io_slider_init() -> HRESULT { } #[no_mangle] -#[cfg(all(feature = "chusan", target_arch = "x86_64"))] +#[cfg(amdaemon)] pub extern "C" fn chuni_io_slider_init(_callback: *const c_void) -> HRESULT { S_OK } #[no_mangle] -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] pub unsafe extern "C" fn chuni_io_slider_start(callback: *const c_void) { if callback.is_null() { return; @@ -298,11 +266,11 @@ pub unsafe extern "C" fn chuni_io_slider_start(callback: *const c_void) { } #[no_mangle] -#[cfg(all(feature = "chusan", target_arch = "x86_64"))] +#[cfg(amdaemon)] pub extern "C" fn chuni_io_slider_start(_callback: *const c_void) {} #[no_mangle] -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] pub extern "C" fn chuni_io_slider_stop() { let Some(thread) = (unsafe { SLIDER_THREAD.take() }) else { return; @@ -314,11 +282,11 @@ pub extern "C" fn chuni_io_slider_stop() { } #[no_mangle] -#[cfg(all(feature = "chusan", target_arch = "x86_64"))] +#[cfg(amdaemon)] pub extern "C" fn chuni_io_slider_stop() {} #[no_mangle] -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] pub unsafe extern "C" fn chuni_io_slider_set_leds(rgb: *const u8) { if rgb.is_null() { return; @@ -367,7 +335,7 @@ pub unsafe extern "C" fn chuni_io_slider_set_leds(rgb: *const u8) { } #[no_mangle] -#[cfg(all(feature = "chusan", target_arch = "x86_64"))] +#[cfg(amdaemon)] pub extern "C" fn chuni_io_slider_set_leds(_rgb: *const u8) {} #[no_mangle] @@ -376,7 +344,7 @@ pub extern "C" fn chuni_io_led_init() -> HRESULT { } #[no_mangle] -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] pub unsafe extern "C" fn chuni_io_led_set_colors(board: u8, rgb: *const u8) { let Some(device) = DEVICE.get() else { return }; let Some(out_shmem) = &OUTPUT_SHMEM else { @@ -414,7 +382,7 @@ pub unsafe extern "C" fn chuni_io_led_set_colors(board: u8, rgb: *const u8) { } #[no_mangle] -#[cfg(all(feature = "chusan", target_arch = "x86_64"))] +#[cfg(amdaemon)] pub extern "C" fn chuni_io_led_set_colors(_rgb: *const u8) {} fn create_input_shared_memory() -> HRESULT { @@ -453,7 +421,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")))] +#[cfg(any(chuni, chusanapp))] fn yubideck_init() -> Result<()> { let Some(mut device) = rusb::open_device_with_vid_pid(0x1973, 0x2001) else { return Err(anyhow!("YubiDeck not found.")); @@ -470,7 +438,7 @@ fn yubideck_init() -> Result<()> { Ok(()) } -#[cfg(any(all(feature = "chusan", target_arch = "x86"), not(feature = "chusan")))] +#[cfg(any(chuni, chusanapp))] fn input_thread_proc() { info!("Input thread started");