From 8c7e09dd08540a96aa4750fa18fe03a347ea8695 Mon Sep 17 00:00:00 2001 From: sangluo Date: Sun, 10 Jul 2022 22:16:05 +0800 Subject: [PATCH] fix fmt and clippy --- src/apple/macos/component/arm.rs | 63 +++++---- src/apple/macos/component/mod.rs | 6 +- src/apple/macos/ffi.rs | 211 ++++++++++++++++++------------- src/apple/system.rs | 38 +++++- 4 files changed, 197 insertions(+), 121 deletions(-) diff --git a/src/apple/macos/component/arm.rs b/src/apple/macos/component/arm.rs index b5983f4b2..39a39792f 100644 --- a/src/apple/macos/component/arm.rs +++ b/src/apple/macos/component/arm.rs @@ -2,33 +2,36 @@ use std::ffi::CStr; +use core_foundation_sys::array::CFArrayGetCount; +use core_foundation_sys::array::CFArrayGetValueAtIndex; use core_foundation_sys::base::kCFAllocatorDefault; +use core_foundation_sys::string::kCFStringEncodingUTF8; use core_foundation_sys::string::CFStringCreateWithBytes; use core_foundation_sys::string::CFStringGetCStringPtr; -use core_foundation_sys::string::kCFStringEncodingUTF8; -use core_foundation_sys::array::CFArrayGetCount; -use core_foundation_sys::array::CFArrayGetValueAtIndex; -use crate::ComponentExt; +use crate::apple::inner::ffi::kHIDPage_AppleVendor; +use crate::apple::inner::ffi::kHIDUsage_AppleVendor_TemperatureSensor; +use crate::apple::inner::ffi::kIOHIDEventTypeTemperature; +use crate::apple::inner::ffi::matching; +use crate::apple::inner::ffi::IOHIDEventFieldBase; +use crate::apple::inner::ffi::IOHIDEventGetFloatValue; use crate::apple::inner::ffi::IOHIDEventSystemClientCopyServices; use crate::apple::inner::ffi::IOHIDEventSystemClientCreate; use crate::apple::inner::ffi::IOHIDEventSystemClientSetMatching; +use crate::apple::inner::ffi::IOHIDServiceClientCopyEvent; use crate::apple::inner::ffi::IOHIDServiceClientCopyProperty; use crate::apple::inner::ffi::IOHIDServiceClientRef; -use crate::apple::inner::ffi::IOHIDServiceClientCopyEvent; -use crate::apple::inner::ffi::IOHIDEventGetFloatValue; -use crate::apple::inner::ffi::kHIDPage_AppleVendor; -use crate::apple::inner::ffi::kHIDUsage_AppleVendor_TemperatureSensor; -use crate::apple::inner::ffi::kIOHIDEventTypeTemperature; -use crate::apple::inner::ffi::IOHIDEventFieldBase; -use crate::apple::inner::ffi::matching; use crate::apple::inner::ffi::HID_DEVICE_PROPERTY_PRODUCT; +use crate::ComponentExt; pub(crate) fn temperatures(components: &mut Vec) -> &mut Vec { components.clear(); unsafe { - let matches = matching(kHIDPage_AppleVendor, kHIDUsage_AppleVendor_TemperatureSensor); + let matches = matching( + kHIDPage_AppleVendor, + kHIDUsage_AppleVendor_TemperatureSensor, + ); if matches.is_null() { return components; } @@ -46,11 +49,11 @@ pub(crate) fn temperatures(components: &mut Vec) -> &mut Vec) -> &mut Vec + critical: Option, } -impl Component { +impl Component { pub(crate) fn new( label: String, max: Option, critical: Option, - service: IOHIDServiceClientRef + service: IOHIDServiceClientRef, ) -> Self { Self { service, label, max: max.unwrap_or(0.), critical, - temperature: 0. + temperature: 0., } } } @@ -125,10 +133,17 @@ impl ComponentExt for Component { fn refresh(&mut self) { unsafe { - let event = IOHIDServiceClientCopyEvent(self.service as *const _, kIOHIDEventTypeTemperature, 0, 0); + let event = IOHIDServiceClientCopyEvent( + self.service as *const _, + kIOHIDEventTypeTemperature, + 0, + 0, + ); if !event.is_null() { - self.temperature = IOHIDEventGetFloatValue(event, IOHIDEventFieldBase(kIOHIDEventTypeTemperature)) as f32; + self.temperature = + IOHIDEventGetFloatValue(event, IOHIDEventFieldBase(kIOHIDEventTypeTemperature)) + as f32; } } } -} \ No newline at end of file +} diff --git a/src/apple/macos/component/mod.rs b/src/apple/macos/component/mod.rs index 6cf8e8ff9..50b359e61 100644 --- a/src/apple/macos/component/mod.rs +++ b/src/apple/macos/component/mod.rs @@ -1,13 +1,13 @@ // Take a look at the license at the top of the repository in the LICENSE file. -#[cfg(target_arch = "x86")] +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] pub(crate) mod x86; -#[cfg(target_arch = "x86")] +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] pub use self::x86::*; #[cfg(target_arch = "aarch64")] pub(crate) mod arm; #[cfg(target_arch = "aarch64")] -pub use self::arm::*; \ No newline at end of file +pub use self::arm::*; diff --git a/src/apple/macos/ffi.rs b/src/apple/macos/ffi.rs index 4d5b991a4..34121bc17 100644 --- a/src/apple/macos/ffi.rs +++ b/src/apple/macos/ffi.rs @@ -1,30 +1,23 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use std::ptr::null; - use core_foundation_sys::base::CFAllocatorRef; use core_foundation_sys::dictionary::CFMutableDictionaryRef; -use core_foundation_sys::dictionary::CFDictionaryRef; -use core_foundation_sys::dictionary::CFDictionaryCreate; -use core_foundation_sys::dictionary::kCFTypeDictionaryKeyCallBacks; -use core_foundation_sys::dictionary::kCFTypeDictionaryValueCallBacks; use core_foundation_sys::string::CFStringEncoding; use core_foundation_sys::string::CFStringRef; -use core_foundation_sys::string::CFStringCreateWithCString; -use core_foundation_sys::array::__CFArray; -use core_foundation_sys::array::CFArrayCallBacks; -use core_foundation_sys::array::CFArrayRef; -use core_foundation_sys::mach_port::CFIndex; -use core_foundation_sys::number::CFNumberCreate; -use core_foundation_sys::number::kCFNumberSInt32Type; use libc::{c_char, c_void}; -#[cfg(not(feature = "apple-sandbox"))] +#[cfg(all( + not(feature = "apple-sandbox"), + any(target_arch = "x86", target_arch = "x86_64") +))] use libc::{mach_port_t, size_t}; pub(crate) use crate::sys::ffi::*; -#[cfg(not(feature = "apple-sandbox"))] +#[cfg(all( + not(feature = "apple-sandbox"), + any(target_arch = "x86", target_arch = "x86_64") +))] extern "C" { // The proc_* PID functions are internal Apple APIs which are not // allowed in App store releases as Apple blocks any binary using them. @@ -49,6 +42,7 @@ extern "C" { pub fn IOServiceClose(a: io_connect_t) -> i32; + #[allow(dead_code)] pub fn IOConnectCallStructMethod( connection: mach_port_t, selector: u32, @@ -104,7 +98,10 @@ pub struct SessionWrap(pub DASessionRef); unsafe impl Send for SessionWrap {} unsafe impl Sync for SessionWrap {} -#[cfg(not(feature = "apple-sandbox"))] +#[cfg(all( + not(feature = "apple-sandbox"), + any(target_arch = "x86", target_arch = "x86_64") +))] mod io_service { use super::mach_port_t; @@ -157,8 +154,13 @@ mod io_service { pub bytes: [i8; 32], // SMCBytes_t } + #[allow(dead_code)] pub const KERNEL_INDEX_SMC: i32 = 2; + + #[allow(dead_code)] pub const SMC_CMD_READ_KEYINFO: u8 = 9; + + #[allow(dead_code)] pub const SMC_CMD_READ_BYTES: u8 = 5; pub const KIO_RETURN_SUCCESS: i32 = 0; @@ -167,91 +169,126 @@ mod io_service { #[cfg(feature = "apple-sandbox")] mod io_service {} +#[cfg(all( + not(feature = "apple-sandbox"), + any(target_arch = "x86", target_arch = "x86_64") +))] pub use io_service::*; -#[repr(C)] -pub struct __IOHIDServiceClient(libc::c_void); - -pub type IOHIDServiceClientRef = *const __IOHIDServiceClient; +#[cfg(all(not(feature = "apple-sandbox"), target_arch = "aarch64"))] +mod io_service { + use std::ptr::null; + + use core_foundation_sys::array::CFArrayRef; + use core_foundation_sys::base::CFAllocatorRef; + use core_foundation_sys::dictionary::kCFTypeDictionaryKeyCallBacks; + use core_foundation_sys::dictionary::kCFTypeDictionaryValueCallBacks; + use core_foundation_sys::dictionary::CFDictionaryCreate; + use core_foundation_sys::dictionary::CFDictionaryRef; + use core_foundation_sys::number::kCFNumberSInt32Type; + use core_foundation_sys::number::CFNumberCreate; + use core_foundation_sys::string::CFStringCreateWithCString; + use core_foundation_sys::string::CFStringRef; -#[repr(C)] -pub struct __IOHIDEvent(libc::c_void); + #[repr(C)] + pub struct __IOHIDServiceClient(libc::c_void); -pub type IOHIDEventRef = *const __IOHIDEvent; + pub type IOHIDServiceClientRef = *const __IOHIDServiceClient; -pub const kIOHIDEventTypeTemperature: i64 = 15; + #[repr(C)] + pub struct __IOHIDEvent(libc::c_void); -#[inline] -pub fn IOHIDEventFieldBase(event_type: i64) -> i64 { - event_type << 16 -} + pub type IOHIDEventRef = *const __IOHIDEvent; -#[cfg(not(feature = "apple-sandbox"))] -extern "C" { - pub fn IOHIDEventSystemClientCreate(allocator: CFAllocatorRef) -> *mut libc::c_void; - - pub fn IOHIDEventSystemClientSetMatching(client: *mut libc::c_void, matches: CFDictionaryRef) -> i32; + #[allow(non_upper_case_globals)] + pub const kIOHIDEventTypeTemperature: i64 = 15; - pub fn IOHIDEventSystemClientCopyServices(client: *mut libc::c_void) -> CFArrayRef; + #[inline] + #[allow(non_snake_case)] + pub fn IOHIDEventFieldBase(event_type: i64) -> i64 { + event_type << 16 + } - pub fn IOHIDServiceClientCopyProperty(service: IOHIDServiceClientRef, key: CFStringRef) -> CFStringRef; + #[cfg(not(feature = "apple-sandbox"))] + extern "C" { + pub fn IOHIDEventSystemClientCreate(allocator: CFAllocatorRef) -> *mut libc::c_void; - pub fn IOHIDServiceClientCopyEvent(service: IOHIDServiceClientRef, v0: i64, v1: i32, v2: i64) -> IOHIDEventRef; + pub fn IOHIDEventSystemClientSetMatching( + client: *mut libc::c_void, + matches: CFDictionaryRef, + ) -> i32; - pub fn IOHIDEventGetFloatValue(event: IOHIDEventRef, field: i64) -> f64; -} + pub fn IOHIDEventSystemClientCopyServices(client: *mut libc::c_void) -> CFArrayRef; -pub type CFMutableArrayRef = *mut __CFArray; + pub fn IOHIDServiceClientCopyProperty( + service: IOHIDServiceClientRef, + key: CFStringRef, + ) -> CFStringRef; -extern "C" { - pub fn CFArrayCreateMutable(allocator: CFAllocatorRef, capacity: CFIndex, callbacks: *const CFArrayCallBacks) -> CFMutableArrayRef; + pub fn IOHIDServiceClientCopyEvent( + service: IOHIDServiceClientRef, + v0: i64, + v1: i32, + v2: i64, + ) -> IOHIDEventRef; - pub fn CFArrayAppendValue(the_array: CFMutableArrayRef, value: *const libc::c_void); -} + pub fn IOHIDEventGetFloatValue(event: IOHIDEventRef, field: i64) -> f64; + } -pub(crate) const HID_DEVICE_PROPERTY_PRODUCT: &[u8] = b"Product\0"; - -pub(crate) const HID_DEVICE_PROPERTY_PRIMARY_USAGE: &[u8] = b"PrimaryUsage\0"; -pub(crate) const HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE: &[u8] = b"PrimaryUsagePage\0"; - -pub(crate) const kHIDPage_AppleVendor: i32 = 0xff00; -pub(crate) const kHIDUsage_AppleVendor_TemperatureSensor: i32 = 0x0005; - -pub(crate) fn matching(page: i32, usage: i32) -> CFDictionaryRef { - unsafe { - let keys = [ - CFStringCreateWithCString( - null() as *const _, - HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE.as_ptr() as *const _, - 0 - ), - CFStringCreateWithCString( - null() as *const _, - HID_DEVICE_PROPERTY_PRIMARY_USAGE.as_ptr() as *const _, - 0 - ), - ]; - - let nums = [ - CFNumberCreate( - null(), - kCFNumberSInt32Type, - &page as *const _ as *const _ - ), - CFNumberCreate( - null(), - kCFNumberSInt32Type, - &usage as *const _ as *const _ - ), - ]; - - CFDictionaryCreate( - null(), - &keys as *const _ as *const _, - &nums as *const _ as *const _, - 2, - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks - ) + // pub type CFMutableArrayRef = *mut __CFArray; + + // extern "C" { + // pub fn CFArrayCreateMutable( + // allocator: CFAllocatorRef, + // capacity: CFIndex, + // callbacks: *const CFArrayCallBacks, + // ) -> CFMutableArrayRef; + + // pub fn CFArrayAppendValue(the_array: CFMutableArrayRef, value: *const libc::c_void); + // } + + pub(crate) const HID_DEVICE_PROPERTY_PRODUCT: &[u8] = b"Product\0"; + + pub(crate) const HID_DEVICE_PROPERTY_PRIMARY_USAGE: &[u8] = b"PrimaryUsage\0"; + pub(crate) const HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE: &[u8] = b"PrimaryUsagePage\0"; + + #[allow(non_upper_case_globals)] + pub(crate) const kHIDPage_AppleVendor: i32 = 0xff00; + + #[allow(non_upper_case_globals)] + pub(crate) const kHIDUsage_AppleVendor_TemperatureSensor: i32 = 0x0005; + + pub(crate) fn matching(page: i32, usage: i32) -> CFDictionaryRef { + unsafe { + let keys = [ + CFStringCreateWithCString( + null() as *const _, + HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE.as_ptr() as *const _, + 0, + ), + CFStringCreateWithCString( + null() as *const _, + HID_DEVICE_PROPERTY_PRIMARY_USAGE.as_ptr() as *const _, + 0, + ), + ]; + + let nums = [ + CFNumberCreate(null(), kCFNumberSInt32Type, &page as *const _ as *const _), + CFNumberCreate(null(), kCFNumberSInt32Type, &usage as *const _ as *const _), + ]; + + CFDictionaryCreate( + null(), + &keys as *const _ as *const _, + &nums as *const _ as *const _, + 2, + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks, + ) + } } } + +#[cfg(all(not(feature = "apple-sandbox"), target_arch = "aarch64"))] +pub use io_service::*; diff --git a/src/apple/system.rs b/src/apple/system.rs index 2ccd20448..2331ccbf4 100644 --- a/src/apple/system.rs +++ b/src/apple/system.rs @@ -87,12 +87,16 @@ pub struct System { global_cpu: Cpu, cpus: Vec, page_size_kb: u64, - #[cfg(all(target_os = "macos", target_arch = "x86"))] + #[cfg(all(target_os = "macos", any(target_arch = "x86", target_arch = "x86_64")))] components: Vec, #[cfg(all(target_os = "macos", target_arch = "aarch64"))] components: Vec, // Used to get CPU information, not supported on iOS, or inside the default macOS sandbox. - #[cfg(all(target_os = "macos", not(feature = "apple-sandbox")))] + #[cfg(all( + target_os = "macos", + not(feature = "apple-sandbox"), + any(target_arch = "x86", target_arch = "x86_64") + ))] connection: Option, disks: Vec, networks: Networks, @@ -112,7 +116,11 @@ impl Drop for System { fn drop(&mut self) { #[cfg(target_os = "macos")] unsafe { - #[cfg(all(target_os = "macos", not(feature = "apple-sandbox")))] + #[cfg(all( + target_os = "macos", + not(feature = "apple-sandbox"), + any(target_arch = "x86", target_arch = "x86_64") + ))] if let Some(conn) = self.connection { ffi::IOServiceClose(conn); } @@ -187,7 +195,11 @@ impl SystemExt for System { cpus: Vec::new(), page_size_kb: sysconf(_SC_PAGESIZE) as u64 / 1_000, components: Vec::with_capacity(2), - #[cfg(all(target_os = "macos", not(feature = "apple-sandbox")))] + #[cfg(all( + target_os = "macos", + not(feature = "apple-sandbox"), + any(target_arch = "x86", target_arch = "x86_64") + ))] connection: get_io_service_connection(), disks: Vec::with_capacity(1), networks: Networks::new(), @@ -268,7 +280,11 @@ impl SystemExt for System { #[cfg(any(target_os = "ios", feature = "apple-sandbox"))] fn refresh_components_list(&mut self) {} - #[cfg(all(target_os = "macos", not(feature = "apple-sandbox"), target_arch = "x86"))] + #[cfg(all( + target_os = "macos", + not(feature = "apple-sandbox"), + any(target_arch = "x86", target_arch = "x86_64") + ))] fn refresh_components_list(&mut self) { if let Some(con) = self.connection { self.components.clear(); @@ -286,7 +302,11 @@ impl SystemExt for System { } } - #[cfg(all(target_os = "macos", not(feature = "apple-sandbox"), target_arch = "aarch64"))] + #[cfg(all( + target_os = "macos", + not(feature = "apple-sandbox"), + target_arch = "aarch64" + ))] fn refresh_components_list(&mut self) { crate::apple::component::temperatures(&mut self.components); } @@ -632,7 +652,11 @@ impl Default for System { // code from https://github.com/Chris911/iStats // Not supported on iOS, or in the default macOS -#[cfg(all(target_os = "macos", not(feature = "apple-sandbox")))] +#[cfg(all( + target_os = "macos", + not(feature = "apple-sandbox"), + any(target_arch = "x86", target_arch = "x86_64") +))] fn get_io_service_connection() -> Option { let mut master_port: mach_port_t = 0; let mut iterator: ffi::io_iterator_t = 0;