Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for get temperature on m1 #792

Merged
merged 8 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions src/apple/macos/component/arm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::ffi::CStr;

use core_foundation_sys::array::{CFArrayGetCount, CFArrayGetValueAtIndex};
use core_foundation_sys::base::kCFAllocatorDefault;
use core_foundation_sys::string::{
kCFStringEncodingUTF8, CFStringCreateWithBytes, CFStringGetCStringPtr,
};

use super::super::CFReleaser;
use crate::apple::inner::ffi::{
kHIDPage_AppleVendor, kHIDUsage_AppleVendor_TemperatureSensor, kIOHIDEventTypeTemperature,
matching, IOHIDEventFieldBase, IOHIDEventGetFloatValue, IOHIDEventSystemClientCopyServices,
IOHIDEventSystemClientCreate, IOHIDEventSystemClientSetMatching, IOHIDServiceClientCopyEvent,
IOHIDServiceClientCopyProperty, __IOHIDEventSystemClient, __IOHIDServiceClient,
HID_DEVICE_PROPERTY_PRODUCT,
};
use crate::ComponentExt;

pub(crate) struct Components {
pub inner: Vec<Component>,
client: Option<CFReleaser<__IOHIDEventSystemClient>>,
}

impl Components {
pub(crate) fn new() -> Self {
Self {
inner: vec![],
client: None,
}
}

pub(crate) fn refresh(&mut self) {
self.inner.clear();

let client = || -> Option<CFReleaser<_>> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand why you needed to make this into a closure. Why not a simple block instead?

unsafe {
let matches = CFReleaser::new(matching(
kHIDPage_AppleVendor,
kHIDUsage_AppleVendor_TemperatureSensor,
))?;

let client = CFReleaser::new(IOHIDEventSystemClientCreate(kCFAllocatorDefault))?;

let _ = IOHIDEventSystemClientSetMatching(client.inner(), matches.inner());
Some(client)
}
}();

if client.is_none() {
return;
}
let client = client.unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if client.is_none() {
return;
}
let client = client.unwrap();
let client = match client {
Some(c) => c,
None => return,
};


unsafe {
let services = IOHIDEventSystemClientCopyServices(client.inner());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

services needs to be freed using CFRelease. And before every return too. The best thing you can do is wrapping it into a struct which implements Drop (which calls CFRelease automatically).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you already added CFReleaser so you already have the needed struct. :)

if services.is_null() {
return;
}

let key_ref = CFReleaser::new(CFStringCreateWithBytes(
kCFAllocatorDefault,
HID_DEVICE_PROPERTY_PRODUCT.as_ptr(),
HID_DEVICE_PROPERTY_PRODUCT.len() as _,
kCFStringEncodingUTF8,
false as _,
));
if key_ref.is_none() {
return;
}
let key_ref = key_ref.unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if key_ref.is_none() {
return;
}
let key_ref = key_ref.unwrap();
let key_ref = match key_ref {
Some(k) => k,
None => return,
};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that you could match directly on CFReleaser::new(...).


let count = CFArrayGetCount(services);

for i in 0..count {
let service = CFReleaser::new(CFArrayGetValueAtIndex(services, i) as *const _);
if service.is_none() {
continue;
}
let service = service.unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let service = CFReleaser::new(CFArrayGetValueAtIndex(services, i) as *const _);
if service.is_none() {
continue;
}
let service = service.unwrap();
let service = match CFReleaser::new(CFArrayGetValueAtIndex(services, i) as *const _) {
Some(s) => s,
None => continue,
};


let name = CFReleaser::new(IOHIDServiceClientCopyProperty(
service.inner(),
key_ref.inner(),
));
if name.is_none() {
continue;
}
let name = name.unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let name = CFReleaser::new(IOHIDServiceClientCopyProperty(
service.inner(),
key_ref.inner(),
));
if name.is_none() {
continue;
}
let name = name.unwrap();
let name = match CFReleaser::new(IOHIDServiceClientCopyProperty(
service.inner(),
key_ref.inner(),
)) {
Some(n) => n,
None => continue,
};


let name_ptr =
CFStringGetCStringPtr(name.inner() as *const _, kCFStringEncodingUTF8);
let name_str = CStr::from_ptr(name_ptr).to_string_lossy().to_string();

self.inner
.push(Component::new(name_str, None, None, service));
}

self.client.replace(client);
}
}
}

unsafe impl Send for Components {}

unsafe impl Sync for Components {}

#[doc = include_str!("../../../../md_doc/component.md")]
pub struct Component {
service: CFReleaser<__IOHIDServiceClient>,
temperature: f32,
label: String,
max: f32,
critical: Option<f32>,
}

impl Component {
pub(crate) fn new(
label: String,
max: Option<f32>,
critical: Option<f32>,
service: CFReleaser<__IOHIDServiceClient>,
) -> Self {
Self {
service,
label,
max: max.unwrap_or(0.),
critical,
temperature: 0.,
}
}
}

unsafe impl Send for Component {}

unsafe impl Sync for Component {}

impl ComponentExt for Component {
fn temperature(&self) -> f32 {
self.temperature
}

fn max(&self) -> f32 {
self.max
}

fn critical(&self) -> Option<f32> {
self.critical
}

fn label(&self) -> &str {
&self.label
}

fn refresh(&mut self) {
unsafe {
let event = CFReleaser::new(IOHIDServiceClientCopyEvent(
self.service.inner() as *const _,
kIOHIDEventTypeTemperature,
0,
0,
));
if event.is_none() {
return;
}
let event = event.unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.


self.temperature = IOHIDEventGetFloatValue(
event.inner(),
IOHIDEventFieldBase(kIOHIDEventTypeTemperature),
) as f32;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) as f32;
) as _;

}
}
}
13 changes: 13 additions & 0 deletions src/apple/macos/component/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub(crate) mod x86;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub use self::x86::*;

#[cfg(target_arch = "aarch64")]
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) mod arm;

#[cfg(target_arch = "aarch64")]
pub use self::arm::*;
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl ComponentFFI {
}
}

#[doc = include_str!("../../../md_doc/component.md")]
#[doc = include_str!("../../../../md_doc/component.md")]
pub struct Component {
temperature: f32,
max: f32,
Expand Down
26 changes: 1 addition & 25 deletions src/apple/macos/disk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use super::CFReleaser;
use crate::sys::{ffi, utils};
use crate::utils::to_cpath;
use crate::{Disk, DiskType};
Expand Down Expand Up @@ -33,31 +34,6 @@ fn to_path(mount_path: &[c_char]) -> Option<PathBuf> {
}
}

#[repr(transparent)]
struct CFReleaser<T>(*const T);

impl<T> CFReleaser<T> {
fn new(ptr: *const T) -> Option<Self> {
if ptr.is_null() {
None
} else {
Some(Self(ptr))
}
}

fn inner(&self) -> *const T {
self.0
}
}

impl<T> Drop for CFReleaser<T> {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe { CFRelease(self.0 as _) }
}
}
}

pub(crate) fn get_disks(session: ffi::DASessionRef) -> Vec<Disk> {
if session.is_null() {
return Vec::new();
Expand Down
Loading