I need this for a MacBook with the M1/M2 chips. I write a launch daemon (in C++) that needs to know when a laptop lid is closed (and opened.) And also have an ability to disable the OS to process the lid opening/closing events.
But I'm having a hard time finding any information on how to do it in macOS.
So far I found out that if run the following in Terminal:
ioreg -r -k AppleClamshellState -d 4
It gives me the information that I need in the AppleClamshellState
and AppleClamshellCausesSleep
parameters.
I guess I can run that as a separate process and grep that info out of its output. But I was hoping to see what does ioreg
use internally (or which lower level function does it call) to get that info, as I think this is terribly inefficient to invoke a whole separate process for that. So this is my first question.
The second one is how to get a notification when someone closes or opens the lid?
PS. I did some research and found the IOPMrootDomain
class (?) or interface, that seems to prove the IOPMrootDomain::sendClientClamshellNotification
method (source here.) But I don't really know how to use it in my C++ daemon.
PS2. I am relatively new to programming under Mac. But I have an extensive experiencing programming under Windows. So I apologize in advance if I'm asking something trivial.