A minimal C program to read the physical lid angle of a MacBook using IOKit and CoreFoundation frameworks.
Note
I coded this strictly for practice. It is not intended for any production use.
Full credit goes to Sam Henri Gold for the original discovery and implementation. Here is the original project in Objective-C & Swift.
Unfortunately, the raw hardware sensor can only output a whole integer of the degree (e.g., 67, 69). In Sam's original project, he uses an Exponential Moving Average to calculate a smoothed value with 2 decimal precision.
smoothedAngle = Self.angleSmoothingFactor * rawAngle + (1 - Self.angleSmoothingFactor) * smoothedAngleI am too lazy to implement that math here, so this C program will only print out the raw integer value.
The lid angle sensor was first introduced with the 2019 16-inch MacBook Pro.
- While this code should theoretically work with any MacBook released after that time, there are known issues with some specific models.
- Since Sam developed the project with a M4 MacBook Pro, so it is guaranteed to work on the M4 MacBook Pro.
- A C99 or newer C compiler (
clangorgcc) is strictly required (the code uses the C99 Variable Length Arrays & inline variable declarations)
clang main.c -framework IOKit -framework CoreFoundation -o main
./mainExample Output:
Current Lid Angle: 67 degrees