Skip to content

Commit

Permalink
Merge pull request #138 from sidit77/master
Browse files Browse the repository at this point in the history
Simplified conditional compilation
  • Loading branch information
ruabmbua authored Jan 25, 2024
2 parents 08d1f55 + e9e83b0 commit e33a939
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 85 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ windows-native = [

[dependencies]
libc = "0.2"
cfg-if = "1"

[target.'cfg(target_os = "linux")'.dependencies]
udev = { version = "0.7", optional = true }
Expand Down
118 changes: 49 additions & 69 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,10 @@
//! [`HidDevice`] handles can access the same physical device. For backward compatibility this is
//! an opt-in that can be enabled with the `macos-shared-device` feature flag.

extern crate libc;
#[cfg(all(feature = "linux-native", target_os = "linux"))]
extern crate nix;

#[cfg(target_os = "windows")]
use windows_sys::core::GUID;

mod error;
mod ffi;

#[cfg(hidapi)]
mod hidapi;

#[cfg(all(feature = "linux-native", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "linux-native", target_os = "linux"))))]
mod linux_native;
#[cfg(target_os = "macos")]
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
mod macos;
#[cfg(target_os = "windows")]
#[cfg_attr(docsrs, doc(cfg(target_os = "windows")))]
mod windows;

#[cfg(feature = "windows-native")]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "windows-native", target_os = "windows")))
)]
mod windows_native;

use cfg_if::cfg_if;
use libc::wchar_t;
use std::ffi::CStr;
use std::ffi::CString;
Expand All @@ -97,12 +71,54 @@ use std::sync::Mutex;

pub use error::HidError;

#[cfg(hidapi)]
use crate::hidapi::HidApiBackend;
#[cfg(all(feature = "linux-native", target_os = "linux"))]
use linux_native::HidApiBackend;
#[cfg(all(feature = "windows-native", target_os = "windows"))]
use windows_native::HidApiBackend;
cfg_if! {
if #[cfg(all(feature = "linux-native", target_os = "linux"))] {
//#[cfg_attr(docsrs, doc(cfg(all(feature = "linux-native", target_os = "linux"))))]
mod linux_native;
use linux_native::HidApiBackend;
} else if #[cfg(all(feature = "windows-native", target_os = "windows"))] {
//#[cfg_attr(docsrs, doc(cfg(all(feature = "windows-native", target_os = "windows"))))]
mod windows_native;
use windows_native::HidApiBackend;
} else if #[cfg(hidapi)] {
mod hidapi;
use hidapi::HidApiBackend;
} else {
compile_error!("No backend selected");
}
}

// Automatically implement the top trait
cfg_if! {
if #[cfg(target_os = "windows")] {
#[cfg_attr(docsrs, doc(cfg(target_os = "windows")))]
mod windows;
use windows::GUID;
/// A trait with the extra methods that are available on Windows
trait HidDeviceBackendWindows {
/// Get the container ID for a HID device
fn get_container_id(&self) -> HidResult<GUID>;
}
trait HidDeviceBackend: HidDeviceBackendBase + HidDeviceBackendWindows + Send {}
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + HidDeviceBackendWindows + Send {}
} else if #[cfg(target_os = "macos")] {
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
mod macos;
/// A trait with the extra methods that are available on macOS
trait HidDeviceBackendMacos {
/// Get the location ID for a [`HidDevice`] device.
fn get_location_id(&self) -> HidResult<u32>;

/// Check if the device was opened in exclusive mode.
fn is_open_exclusive(&self) -> HidResult<bool>;
}
trait HidDeviceBackend: HidDeviceBackendBase + HidDeviceBackendMacos + Send {}
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + HidDeviceBackendMacos + Send {}
} else {
trait HidDeviceBackend: HidDeviceBackendBase + Send {}
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + Send {}
}
}

pub type HidResult<T> = Result<T, HidError>;
pub const MAX_REPORT_DESCRIPTOR_SIZE: usize = 4096;
Expand Down Expand Up @@ -471,42 +487,6 @@ trait HidDeviceBackendBase {
}
}

/// A trait with the extra methods that are available on macOS
#[cfg(target_os = "macos")]
trait HidDeviceBackendMacos {
/// Get the location ID for a [`HidDevice`] device.
fn get_location_id(&self) -> HidResult<u32>;

/// Check if the device was opened in exclusive mode.
fn is_open_exclusive(&self) -> HidResult<bool>;
}

/// A trait with the extra methods that are available on macOS
#[cfg(target_os = "windows")]
trait HidDeviceBackendWindows {
/// Get the container ID for a HID device
fn get_container_id(&self) -> HidResult<GUID>;
}

#[cfg(not(any(target_os = "macos", target_os = "windows")))]
trait HidDeviceBackend: HidDeviceBackendBase + Send {}
#[cfg(target_os = "macos")]
trait HidDeviceBackend: HidDeviceBackendBase + HidDeviceBackendMacos + Send {}
#[cfg(target_os = "windows")]
trait HidDeviceBackend: HidDeviceBackendBase + HidDeviceBackendWindows + Send {}

/// Automatically implement the top trait
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + Send {}

/// Automatically implement the top trait
#[cfg(target_os = "macos")]
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + HidDeviceBackendMacos + Send {}

/// Automatically implement the top trait
#[cfg(target_os = "windows")]
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + HidDeviceBackendWindows + Send {}

pub struct HidDevice {
inner: Box<dyn HidDeviceBackend>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{HidDevice, HidResult};
use windows_sys::core::GUID;
pub use windows_sys::core::GUID;

impl HidDevice {
/// Get the container ID for a HID device.
Expand Down
1 change: 0 additions & 1 deletion src/windows_native/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod tests;
mod typedefs;
mod types;

use crate::ensure;
use crate::windows_native::descriptor::encoder::encode_descriptor;
use crate::windows_native::descriptor::typedefs::{Caps, HidpPreparsedData, LinkCollectionNode};
use crate::windows_native::descriptor::types::{
Expand Down
1 change: 0 additions & 1 deletion src/windows_native/dev_node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::ensure;
use crate::windows_native::error::{check_config, WinError, WinResult};
use crate::windows_native::string::U16Str;
use crate::windows_native::types::{DeviceProperty, PropertyKey};
Expand Down
1 change: 0 additions & 1 deletion src/windows_native/hid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::ensure;
use crate::windows_native::error::{check_boolean, WinError, WinResult};
use crate::windows_native::types::Handle;
use std::ffi::c_void;
Expand Down
1 change: 0 additions & 1 deletion src/windows_native/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::ensure;
use crate::windows_native::error::{check_config, WinError, WinResult};
use crate::windows_native::hid::get_interface_guid;
use crate::windows_native::string::{U16Str, U16StringList};
Expand Down
19 changes: 9 additions & 10 deletions src/windows_native/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
//! The implementation which uses the C library to perform operations
//! The implementation which uses the the raw win32 api to perform operations

macro_rules! ensure {
($cond:expr, $result:expr) => {
if !($cond) {
return $result;
}
};
}

mod descriptor;
mod dev_node;
Expand Down Expand Up @@ -42,15 +50,6 @@ use windows_sys::Win32::System::IO::{CancelIo, DeviceIoControl};

const STRING_BUF_LEN: usize = 128;

#[macro_export]
macro_rules! ensure {
($cond:expr, $result:expr) => {
if !($cond) {
return $result;
}
};
}

pub struct HidApiBackend;
impl HidApiBackend {
pub fn get_hid_device_info_vector(vid: u16, pid: u16) -> HidResult<Vec<DeviceInfo>> {
Expand Down
2 changes: 1 addition & 1 deletion src/windows_native/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::windows_native::error::{WinError, WinResult};
use crate::{ensure, BusType};
use crate::BusType;
use std::mem::{size_of, zeroed};
use std::ptr::null;
use windows_sys::core::GUID;
Expand Down

0 comments on commit e33a939

Please sign in to comment.