Skip to content

Commit

Permalink
fix fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
sangluo committed Jul 10, 2022
1 parent 447095b commit 246c542
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 121 deletions.
63 changes: 39 additions & 24 deletions src/apple/macos/component/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Component>) -> &mut Vec<Component> {
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;
}
Expand All @@ -46,11 +49,11 @@ pub(crate) fn temperatures(components: &mut Vec<Component>) -> &mut Vec<Componen
}

let key_ref = CFStringCreateWithBytes(
kCFAllocatorDefault,
HID_DEVICE_PROPERTY_PRODUCT.as_ptr(),
HID_DEVICE_PROPERTY_PRODUCT.len() as _,
kCFAllocatorDefault,
HID_DEVICE_PROPERTY_PRODUCT.as_ptr(),
HID_DEVICE_PROPERTY_PRODUCT.len() as _,
kCFStringEncodingUTF8,
false as _
false as _,
);

let count = CFArrayGetCount(services);
Expand All @@ -68,7 +71,12 @@ pub(crate) fn temperatures(components: &mut Vec<Component>) -> &mut Vec<Componen

let name_ptr = CFStringGetCStringPtr(name as *const _, kCFStringEncodingUTF8);
let name = CStr::from_ptr(name_ptr).to_string_lossy();
let component = Component::new(name.to_string(), None, None, service as IOHIDServiceClientRef);
let component = Component::new(
name.to_string(),
None,
None,
service as IOHIDServiceClientRef,
);
components.push(component);
}
}
Expand All @@ -82,22 +90,22 @@ pub struct Component {
temperature: f32,
label: String,
max: f32,
critical: Option<f32>
critical: Option<f32>,
}

impl Component {
impl Component {
pub(crate) fn new(
label: String,
max: Option<f32>,
critical: Option<f32>,
service: IOHIDServiceClientRef
service: IOHIDServiceClientRef,
) -> Self {
Self {
service,
label,
max: max.unwrap_or(0.),
critical,
temperature: 0.
temperature: 0.,
}
}
}
Expand Down Expand Up @@ -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;
}
}
}
}
}
6 changes: 3 additions & 3 deletions src/apple/macos/component/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
pub use self::arm::*;
Loading

0 comments on commit 246c542

Please sign in to comment.