Skip to content

Instantly share code, notes, and snippets.

@emilyst
Created March 19, 2019 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilyst/57e50dd0f4160ad673eebb2ca02d0f35 to your computer and use it in GitHub Desktop.
Save emilyst/57e50dd0f4160ad673eebb2ca02d0f35 to your computer and use it in GitHub Desktop.
This doesn't work either
#!/usr/bin/env python3
import objc
from Foundation import (
NSDate,
NSDefaultRunLoopMode,
NSLog,
NSObject,
NSRunLoop,
NSThread,
)
from PyObjCTools import AppHelper
from CoreFoundation import (
CFRunLoopAddSource,
CFRunLoopGetCurrent,
CFRunLoopRun,
kCFRunLoopDefaultMode,
)
kIOMessageCanSystemSleep = -536870288 # idle sleep
kIOMessageSystemWillSleep = -536870272 # forced sleep
kIOMessageSystemWillPowerOn = -536870112 # started waking up
kIOMessageSystemHasPoweredOn = -536870144 # finished waking up
bundle = objc.loadBundle(
'IOKit',
bundle_identifier='com.apple.framework.IOKit',
module_globals=globals()
)
objc.loadBundleFunctions(bundle, globals(), [
(
'IONotificationPortCreate',
b'^{IONotificationPort=}^{ipc_port=}',
'',
{
'arguments': {
1: {
'type': b'^{ipc_port=}',
'already_retained': True
},
},
}
),
(
'IORegisterForSystemPower',
b'^{OSObject=}^v^{IONotificationPort=}^?^^{OSObject}',
'',
{
'arguments': {
2: {
'callable': {
'arguments': {
0: { 'type': b'^v', },
1: { 'type': b'^{OSObject=}', },
2: { 'type': b'I', },
3: { 'type': b'^v', },
},
'retval': { 'type': b'v' },
},
'callable_retained': True,
},
3: { 'type_modifier': b'o', }
},
'retval': { 'type': b'^{OSObject=}' }
}
),
(
'IONotificationPortGetRunLoopSource',
b'i^{IONotificationPort=}'
),
])
objc.loadBundleVariables(bundle, globals(), [
('kIOMasterPortDefault', b'^{ipc_port=}'),
])
objc.createOpaquePointerType('IONotificationPortRef', b'^{IONotificationPort=}', '')
NSLog('%@', IONotificationPortCreate.__metadata__())
NSLog('%@', IORegisterForSystemPower.__metadata__())
NSLog('%@', IONotificationPortGetRunLoopSource.__metadata__())
@objc.callbackFor(IORegisterForSystemPower)
def sleep_wake_handler(refCon, service, messageType, messageArgument):
NSLog('handling message %@:', messageType)
portRef = IONotificationPortCreate(kIOMasterPortDefault)
root_port, notifier = IORegisterForSystemPower(None, portRef, sleep_wake_handler, None)
NSLog('notifier: %@', notifier)
NSLog('portRef: %@', portRef)
NSLog('root_port: %@', root_port)
CFRunLoopAddSource(
CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(portRef),
kCFRunLoopDefaultMode
);
CFRunLoopRun()
NSLog('%@', CFRunLoopGetCurrent())
# while not NSThread.currentThread().isCancelled():
# NSRunLoop.currentRunLoop().runMode_beforeDate_(NSDefaultRunLoopMode, NSDate.distantFuture())
# AppHelper.runConsoleEventLoop(installInterrupt=True)
## A quick guide to runtime name mangling:
##
## ObjC becomes Python
## [ obj method ] obj.method()
## [ obj method: arg ] obj.method_(arg)
## [ obj method: arg1 withOtherArgs: arg2 ]
## obj.method_withOtherArgs_( arg1, arg2 )
# class EventLoopThread(threading.Thread):
# def __init__(self):
# super(EventLoopThread, self).__init__()
# self.setDaemon(True)
# self.should_stop = False
# def run(self):
# NSLog('Starting event loop on background thread')
# AppHelper.runConsoleEventLoop(installInterrupt=True, mode=NSDefaultRunLoopMode)
# def stop(self):
# NSLog('Stopping the event loop on background thread')
# AppHelper.stopEventLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment