Skip to content

Commit efe41c9

Browse files
committed
Use the serial as an ID for the arm macos component
1 parent 070d8b6 commit efe41c9

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/unix/apple/macos/component/arm.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::sys::inner::ffi::{
1111
HID_DEVICE_PROPERTY_PRODUCT, IOHIDEventFieldBase, IOHIDEventGetFloatValue,
1212
IOHIDEventSystemClientCreate, IOHIDEventSystemClientSetMatching, IOHIDServiceClientCopyEvent,
1313
kHIDPage_AppleVendor, kHIDUsage_AppleVendor_TemperatureSensor, kIOHIDEventTypeTemperature,
14+
kIOHIDSerialNumberKey,
1415
};
1516

1617
pub(crate) struct ComponentsInner {
@@ -89,11 +90,17 @@ impl ComponentsInner {
8990
let services = CFRetained::cast_unchecked::<CFArray<IOHIDServiceClient>>(services);
9091

9192
let key = CFString::from_static_str(HID_DEVICE_PROPERTY_PRODUCT);
93+
let serial_key = CFString::from_static_str(kIOHIDSerialNumberKey);
9294

9395
for service in services {
9496
let Some(name) = service.property(&key) else {
9597
continue;
9698
};
99+
let serial = service
100+
.property(&serial_key)
101+
.and_then(|value| value.downcast::<CFString>().ok())
102+
.as_deref()
103+
.map(CFString::to_string);
97104
let name = name.downcast::<CFString>().unwrap();
98105
let name_str = name.to_string();
99106

@@ -107,7 +114,7 @@ impl ComponentsInner {
107114
continue;
108115
}
109116

110-
let mut component = ComponentInner::new(name_str, None, None, service);
117+
let mut component = ComponentInner::new(serial, name_str, None, None, service);
111118
component.refresh();
112119

113120
self.components.push(Component { inner: component });
@@ -117,6 +124,7 @@ impl ComponentsInner {
117124
}
118125

119126
pub(crate) struct ComponentInner {
127+
id: Option<String>,
120128
service: CFRetained<IOHIDServiceClient>,
121129
temperature: Option<f32>,
122130
label: String,
@@ -132,12 +140,14 @@ unsafe impl Sync for ComponentInner {}
132140

133141
impl ComponentInner {
134142
pub(crate) fn new(
143+
id: Option<String>,
135144
label: String,
136145
max: Option<f32>,
137146
critical: Option<f32>,
138147
service: CFRetained<IOHIDServiceClient>,
139148
) -> Self {
140149
Self {
150+
id,
141151
service,
142152
label,
143153
max: max.unwrap_or(0.),
@@ -164,7 +174,7 @@ impl ComponentInner {
164174
}
165175

166176
pub(crate) fn id(&self) -> Option<&str> {
167-
None
177+
self.id.as_deref()
168178
}
169179

170180
pub(crate) fn refresh(&mut self) {

src/unix/apple/macos/ffi.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ mod private {
153153

154154
pub(crate) const HID_DEVICE_PROPERTY_PRODUCT: &str = "Product";
155155

156+
#[allow(non_upper_case_globals)]
157+
pub(crate) const kIOHIDSerialNumberKey: &str = "SerialNumber";
158+
156159
pub(crate) const HID_DEVICE_PROPERTY_PRIMARY_USAGE: &str = "PrimaryUsage";
157160
pub(crate) const HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE: &str = "PrimaryUsagePage";
158161

src/unix/freebsd/component.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ impl ComponentsInner {
122122

123123
#[cfg(test)]
124124
mod tests {
125-
use crate::unix::freebsd::{ComponentInner, ComponentsInner};
126125
use crate::Component;
126+
use crate::unix::freebsd::{ComponentInner, ComponentsInner};
127127

128128
#[test]
129129
fn test_components() {

0 commit comments

Comments
 (0)