Skip to content

Instantly share code, notes, and snippets.

@S2Ler
Last active May 13, 2016 05:16
Show Gist options
  • Save S2Ler/ede3626405764ef854d6 to your computer and use it in GitHub Desktop.
Save S2Ler/ede3626405764ef854d6 to your computer and use it in GitHub Desktop.
bool isLidClosed(void)
{
bool isClosed = false;
io_registry_entry_t rootDomain;
mach_port_t masterPort;
CFTypeRef clamShellStateRef = NULL;
// Retrieve the IOKit's master port so a notification port can be created
IOReturn ioReturn = IOMasterPort(MACH_PORT_NULL, &masterPort);
// Check to see if the "AppleClamshellClosed" property is in the PM root domain:
rootDomain = IORegistryEntryFromPath(masterPort, kIOPowerPlane ":/IOPowerConnection/IOPMrootDomain");
clamShellStateRef = IORegistryEntryCreateCFProperty( rootDomain,CFSTR(kAppleClamshellStateKey), kCFAllocatorDefault, 0);
if (clamShellStateRef == NULL)
{
if ( rootDomain )
IOObjectRelease(rootDomain);
ioReturn = kIOReturnNoResources;
}
if ( CFBooleanGetValue( (CFBooleanRef)(clamShellStateRef) ) == true )
{
printf("closed!\n");
isClosed = true;
}
if ( rootDomain )
IOObjectRelease(rootDomain);
if (clamShellStateRef)
CFRelease(clamShellStateRef);
return isClosed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment