Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import os, sys, pipes
  4.  
  5. import objc
  6. from Foundation import NSObject
  7.  
  8. from CoreFoundation import (
  9. CFRunLoopRun, CFRunLoopStop, CFFileDescriptorCreate,
  10. CFFileDescriptorCreateRunLoopSource, CFRunLoopAddSource,
  11. kCFRunLoopDefaultMode, CFRunLoopGetCurrent,
  12. CFFileDescriptorEnableCallBacks, kCFFileDescriptorReadCallBack
  13. )
  14.  
  15. from AppKit import NSNotificationCenter, NSDistributedNotificationCenter
  16.  
  17. class Locked(NSObject):
  18. @objc.selectorFor(NSNotificationCenter.addObserver_selector_name_object_)
  19. def observe_(self, notification):
  20. command = ' '.join(map(pipes.quote, sys.argv[1:]))
  21. print("Sleep: running ", command)
  22. os.system(command)
  23.  
  24. nsdnc = NSDistributedNotificationCenter.defaultCenter()
  25. nsdnc.addObserver_selector_name_object_(
  26. Locked.alloc().init().retain(), "observe:", "com.apple.screenIsLocked",
  27. None,
  28. )
  29.  
  30. def handle_signals():
  31. def stop(cffd, cbt, info):
  32. CFRunLoopStop(CFRunLoopGetCurrent())
  33.  
  34. r, w = os.pipe()
  35.  
  36. cffd = CFFileDescriptorCreate(None, r, False, stop, None)
  37. CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack)
  38. cfrlsrc = CFFileDescriptorCreateRunLoopSource(None, cffd, 0)
  39. CFRunLoopAddSource(CFRunLoopGetCurrent(), cfrlsrc, kCFRunLoopDefaultMode)
  40.  
  41. def nop(signum, stackframe):
  42. pass
  43.  
  44. import signal
  45. signal.set_wakeup_fd(w)
  46. signal.signal(signal.SIGINT, nop)
  47. signal.signal(signal.SIGTERM, nop)
  48.  
  49. handle_signals()
  50. CFRunLoopRun()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement