Skip to content

Commit

Permalink
remove unused code and check pointer return from c code
Browse files Browse the repository at this point in the history
  • Loading branch information
sangluo committed Jul 10, 2022
1 parent b17f534 commit f7a9eb0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 75 deletions.
40 changes: 27 additions & 13 deletions src/apple/macos/component/arm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::ptr::null;
use std::ffi::CStr;

use core_foundation_sys::base::kCFAllocatorDefault;
use core_foundation_sys::mach_port::CFIndex;
use core_foundation_sys::base::Boolean;
use core_foundation_sys::string::CFStringCreateWithBytes;
use core_foundation_sys::string::CFStringGetCStringPtr;
use core_foundation_sys::string::kCFStringEncodingUTF8;
Expand All @@ -32,30 +29,47 @@ pub(crate) fn temperatures() -> Vec<Component> {

unsafe {
let matches = matching(kHIDPage_AppleVendor, kHIDUsage_AppleVendor_TemperatureSensor);
if matches.is_null() {
return components;
}

let client = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
if client.is_null() {
return components;
}

let _ = IOHIDEventSystemClientSetMatching(client, matches);

let services = IOHIDEventSystemClientCopyServices(client);
if services.is_null() {
return components;
}

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

let count = CFArrayGetCount(services);

for i in 0..count {
let service = CFArrayGetValueAtIndex(services, i);
if service.is_null() {
continue;
}

let name = IOHIDServiceClientCopyProperty(service as *const _, key_ref);
if name != null() {
// CFArrayAppendValue(array, name as *const _);
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);
components.push(component);
}
if name.is_null() {
continue;
}

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);
components.push(component);
}
}

Expand Down Expand Up @@ -112,7 +126,7 @@ impl ComponentExt for Component {
fn refresh(&mut self) {
unsafe {
let event = IOHIDServiceClientCopyEvent(self.service as *const _, kIOHIDEventTypeTemperature, 0, 0);
if event != null() {
if !event.is_null() {
self.temperature = IOHIDEventGetFloatValue(event, IOHIDEventFieldBase(kIOHIDEventTypeTemperature)) as f32;
}
}
Expand Down
70 changes: 9 additions & 61 deletions src/apple/macos/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::ptr::null;
use std::ffi::CStr;
use std::ffi::CString;

use core_foundation_sys::base::CFAllocatorRef;
use core_foundation_sys::base::kCFAllocatorDefault;
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::CFStringCreateWithBytes;
use core_foundation_sys::string::kCFStringEncodingUTF8;
use core_foundation_sys::string::CFStringGetCStringPtr;
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::array::CFArrayGetCount;
use core_foundation_sys::array::CFArrayGetValueAtIndex;
use core_foundation_sys::base::Boolean;
use core_foundation_sys::mach_port::CFIndex;
use core_foundation_sys::number::CFNumberCreate;
use core_foundation_sys::number::kCFNumberSInt32Type;
Expand Down Expand Up @@ -190,8 +181,7 @@ pub type IOHIDEventRef = *const __IOHIDEvent;

pub const kIOHIDEventTypeTemperature: i64 = 15;

pub const kIOHIDEventTypePower: i64 = 15;

#[inline]
pub fn IOHIDEventFieldBase(event_type: i64) -> i64 {
event_type << 16
}
Expand Down Expand Up @@ -219,32 +209,25 @@ extern "C" {
pub fn CFArrayAppendValue(the_array: CFMutableArrayRef, value: *const libc::c_void);
}

pub(crate) const HID_DEVICE_PROPERTY_VENDOR_ID: &str = "VendorId";
pub(crate) const HID_DEVICE_PROPERTY_PRODUCT_ID: &str = "ProductID";
pub(crate) const HID_DEVICE_PROPERTY_PRODUCT: &str = "Product";
pub(crate) const HID_DEVICE_PROPERTY_PRIMARY_USAGE: &str = "PrimaryUsage";
pub(crate) const HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE: &str = "PrimaryUsagePage";
pub(crate) const HID_DEVICE_PROPERTY_MAX_INPUT_REPORT_SIZE: &str = "MaxInputReportSize";
pub(crate) const HID_DEVICE_PROPERTY_MAX_OUTPUT_REPORT_SIZE: &str = "MaxOutputReportSize";
pub(crate) const HID_DEVICE_PROPERTY_REPORT_ID: &str = "ReportID";
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 primary_usage_page = CString::new(HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE).unwrap();
let primary_usage = CString::new(HID_DEVICE_PROPERTY_PRIMARY_USAGE).unwrap();

let keys = [
CFStringCreateWithCString(
null() as *const _,
primary_usage_page.as_ptr(),
HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE.as_ptr() as *const _,
0
),
CFStringCreateWithCString(
null() as *const _,
primary_usage.as_ptr(),
HID_DEVICE_PROPERTY_PRIMARY_USAGE.as_ptr() as *const _,
0
),
];
Expand All @@ -253,12 +236,12 @@ pub(crate) fn matching(page: i32, usage: i32) -> CFDictionaryRef {
CFNumberCreate(
null(),
kCFNumberSInt32Type,
&page as *const _ as *const libc::c_void
&page as *const _ as *const _
),
CFNumberCreate(
null(),
kCFNumberSInt32Type,
&usage as *const _ as *const libc::c_void
&usage as *const _ as *const _
),
];

Expand All @@ -272,38 +255,3 @@ pub(crate) fn matching(page: i32, usage: i32) -> CFDictionaryRef {
)
}
}

pub fn get_product_names(sensors: CFDictionaryRef) -> Vec<String> {
unsafe {
let system = IOHIDEventSystemClientCreate(kCFAllocatorDefault);

let _rv = IOHIDEventSystemClientSetMatching(system, sensors);

let services = IOHIDEventSystemClientCopyServices(system);

let count = CFArrayGetCount(services);

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

let mut names = Vec::with_capacity(count as usize);

for i in 0..count {
let service = CFArrayGetValueAtIndex(services, i);
let name = IOHIDServiceClientCopyProperty(service as *const _, key_ref);
if name != null() {
// CFArrayAppendValue(array, name as *const _);
let name_ptr = CFStringGetCStringPtr(name as *const _, kCFStringEncodingUTF8);
let name = CStr::from_ptr(name_ptr).to_string_lossy();
names.push(name);
}
}

names.into_iter().map(|i| i.to_string()).collect::<Vec<_>>()
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ mod test {

// Ensure that the CPUs frequency isn't retrieved until we ask for it.
#[test]
#[cfg(all(not(target_os = "freebsd"), not(target_arch = "aarch64")))] // In a VM, it'll fail.
#[cfg(not(target_os = "freebsd"))] // In a VM, it'll fail.
fn check_cpu_frequency() {
if !System::IS_SUPPORTED {
return;
Expand Down

0 comments on commit f7a9eb0

Please sign in to comment.