Apple Silicon MacBooks have an accelerometer and a gyroscope in the base. macOS gives apps no documented way to read them, and the common advice is that you need root to touch the sensor at all.
You do not. I needed the sensor for ChillDesk, a living winter scene behind the desktop icons: shake the laptop and a blizzard kicks up, tilt it and the villagers slide downhill. Here is how it works from a normal user process, with no root, no entitlement and no privacy prompt.
1. The sensors are asleep until you ask
The accelerometer and gyro sit behind a driver service called AppleSPUHIDDriver. Out of the box they report nothing. The trick is that a normal process may set three properties on that service in the I/O Registry, and that wakes the sensors up:
/// Asks every SPU sensor service to power up and report at our interval. fn wake_sensors() { let props: [(&CStr, i32); 3] = [ (c"SensorPropertyReportingState", 1), (c"SensorPropertyPowerState", 1), (c"ReportInterval", 20_000), // microseconds: 50 Hz ]; let matching = IOServiceMatching(c"AppleSPUHIDDriver".as_ptr()); // for every matching service: // IORegistryEntrySetCFProperty(service, key, value); } 50 Hz is plenty for shakes and tilts and keeps the CPU cost tiny.
2. Open them as HID devices
Once awake, each sensor is an ordinary HID device on Apple's vendor usage page 0xFF00. The accelerometer is usage 3 and the gyroscope is usage 9. IOHIDManager finds them:
const VENDOR_USAGE_PAGE: i32 = 0xFF00; const ACCEL_USAGE: i32 = 3; const GYRO_USAGE: i32 = 9; // matching dictionary: PrimaryUsagePage = 0xFF00, PrimaryUsage = usage IOHIDManagerSetDeviceMatching(manager, matching); let devices = IOHIDManagerCopyDevices(manager); // keep the ones that IOHIDDeviceOpen(device, 0) == 0 accepts On a Mac without these devices (desktop Macs, older laptops) the set is simply empty, and the feature switches itself off. No error dialog, nothing to configure.
3. Decode the 22-byte report
Each device streams input reports through IOHIDDeviceRegisterInputReportCallback. A report is 22 bytes, with x, y and z as little-endian i32 at byte offsets 6, 10 and 14, in units of 1/65536:
const SCALE: f32 = 1.0 / 65_536.0; fn xyz(report: &[u8]) -> Option<[f32; 3]> { if report.len() < 18 { return None; } let at = |i: usize| i32::from_le_bytes([report[i], report[i + 1], report[i + 2], report[i + 3]]) as f32 * SCALE; Some([at(6), at(10), at(14)]) } For the accelerometer that is in g (z is about -1 when the laptop lies flat), x points to the user's right and y away from the user. For the gyroscope it is degrees per second.
One detail that matters for an app that wants to sleep: a dedicated thread owns the HID devices and runs their callbacks on its own run loop. The 50 reports a second never wake the main event loop.
4. From raw samples to "something happened"
Fifty samples a second is not what a renderer wants. A small pure filter (in a platform-free crate, unit-tested on any machine) turns them into rare messages:
- a 0.25 s low-pass of the accelerometer is the tilt (the direction of gravity);
- what is left over, integrated, is the impulse of a jolt, leaking away over 0.3 s, with a small deadband so that typing does not count;
- the RMS energy of the motion is the blizzard level, decaying over 3 s;
- the gyro about the screen's normal is a spin.
A laptop at rest sends about one message every 8 seconds. That is what lets the scene stay at 0 frames per second while you work and still react the moment you pick the laptop up.
5. One physics model instead of effects
The fun part: everything in the scene feels the same apparent gravity, gravity minus the laptop's own acceleration. Tilt the laptop and gravity leans, so snow and villagers drift downhill. Jolt it and free snow flies the opposite way, the way it would in a real snow globe. Villagers lean on a damped spring and slip with plain Coulomb friction. No special case per effect, and it looks right because it is the same rule everywhere.
Takeaways
- The MacBook motion sensors are readable from user space: wake
AppleSPUHIDDriverwith three registry properties, then read HID usage page0xFF00, usages 3 and 9. - Treat a missing device as "feature off", not as an error.
- Read sensors on their own thread and run loop, and send the rest of the app only rare, meaningful messages.
- Keep the filter pure so you can test it without the hardware.
The result is in ChillDesk: shake your MacBook and the vale gets a blizzard. Questions about IOKit or the filter are welcome in the comments.
