Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import os, sys, pipes
- import objc
- from Foundation import NSObject
- from CoreFoundation import (
- CFRunLoopRun, CFRunLoopStop, CFFileDescriptorCreate,
- CFFileDescriptorCreateRunLoopSource, CFRunLoopAddSource,
- kCFRunLoopDefaultMode, CFRunLoopGetCurrent,
- CFFileDescriptorEnableCallBacks, kCFFileDescriptorReadCallBack
- )
- from AppKit import NSNotificationCenter, NSDistributedNotificationCenter
- class Locked(NSObject):
- @objc.selectorFor(NSNotificationCenter.addObserver_selector_name_object_)
- def observe_(self, notification):
- command = ' '.join(map(pipes.quote, sys.argv[1:]))
- print("Sleep: running ", command)
- os.system(command)
- nsdnc = NSDistributedNotificationCenter.defaultCenter()
- nsdnc.addObserver_selector_name_object_(
- Locked.alloc().init().retain(), "observe:", "com.apple.screenIsLocked",
- None,
- )
- def handle_signals():
- def stop(cffd, cbt, info):
- CFRunLoopStop(CFRunLoopGetCurrent())
- r, w = os.pipe()
- cffd = CFFileDescriptorCreate(None, r, False, stop, None)
- CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack)
- cfrlsrc = CFFileDescriptorCreateRunLoopSource(None, cffd, 0)
- CFRunLoopAddSource(CFRunLoopGetCurrent(), cfrlsrc, kCFRunLoopDefaultMode)
- def nop(signum, stackframe):
- pass
- import signal
- signal.set_wakeup_fd(w)
- signal.signal(signal.SIGINT, nop)
- signal.signal(signal.SIGTERM, nop)
- handle_signals()
- CFRunLoopRun()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement