osx.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """Inputhook for OS X
  2. Calls NSApp / CoreFoundation APIs via ctypes.
  3. """
  4. # obj-c boilerplate from appnope, used under BSD 2-clause
  5. import ctypes
  6. import ctypes.util
  7. from threading import Event
  8. objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc'))
  9. void_p = ctypes.c_void_p
  10. objc.objc_getClass.restype = void_p
  11. objc.sel_registerName.restype = void_p
  12. objc.objc_msgSend.restype = void_p
  13. objc.objc_msgSend.argtypes = [void_p, void_p]
  14. msg = objc.objc_msgSend
  15. def _utf8(s):
  16. """ensure utf8 bytes"""
  17. if not isinstance(s, bytes):
  18. s = s.encode('utf8')
  19. return s
  20. def n(name):
  21. """create a selector name (for ObjC methods)"""
  22. return objc.sel_registerName(_utf8(name))
  23. def C(classname):
  24. """get an ObjC Class by name"""
  25. return objc.objc_getClass(_utf8(classname))
  26. # end obj-c boilerplate from appnope
  27. # CoreFoundation C-API calls we will use:
  28. CoreFoundation = ctypes.cdll.LoadLibrary(ctypes.util.find_library('CoreFoundation'))
  29. CFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate
  30. CFFileDescriptorCreate.restype = void_p
  31. CFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p]
  32. CFFileDescriptorGetNativeDescriptor = CoreFoundation.CFFileDescriptorGetNativeDescriptor
  33. CFFileDescriptorGetNativeDescriptor.restype = ctypes.c_int
  34. CFFileDescriptorGetNativeDescriptor.argtypes = [void_p]
  35. CFFileDescriptorEnableCallBacks = CoreFoundation.CFFileDescriptorEnableCallBacks
  36. CFFileDescriptorEnableCallBacks.restype = None
  37. CFFileDescriptorEnableCallBacks.argtypes = [void_p, ctypes.c_ulong]
  38. CFFileDescriptorCreateRunLoopSource = CoreFoundation.CFFileDescriptorCreateRunLoopSource
  39. CFFileDescriptorCreateRunLoopSource.restype = void_p
  40. CFFileDescriptorCreateRunLoopSource.argtypes = [void_p, void_p, void_p]
  41. CFRunLoopGetCurrent = CoreFoundation.CFRunLoopGetCurrent
  42. CFRunLoopGetCurrent.restype = void_p
  43. CFRunLoopAddSource = CoreFoundation.CFRunLoopAddSource
  44. CFRunLoopAddSource.restype = None
  45. CFRunLoopAddSource.argtypes = [void_p, void_p, void_p]
  46. CFRelease = CoreFoundation.CFRelease
  47. CFRelease.restype = None
  48. CFRelease.argtypes = [void_p]
  49. CFFileDescriptorInvalidate = CoreFoundation.CFFileDescriptorInvalidate
  50. CFFileDescriptorInvalidate.restype = None
  51. CFFileDescriptorInvalidate.argtypes = [void_p]
  52. # From CFFileDescriptor.h
  53. kCFFileDescriptorReadCallBack = 1
  54. kCFRunLoopCommonModes = void_p.in_dll(CoreFoundation, 'kCFRunLoopCommonModes')
  55. def _NSApp():
  56. """Return the global NSApplication instance (NSApp)"""
  57. return msg(C('NSApplication'), n('sharedApplication'))
  58. def _wake(NSApp):
  59. """Wake the Application"""
  60. event = msg(C('NSEvent'),
  61. n('otherEventWithType:location:modifierFlags:'
  62. 'timestamp:windowNumber:context:subtype:data1:data2:'),
  63. 15, # Type
  64. 0, # location
  65. 0, # flags
  66. 0, # timestamp
  67. 0, # window
  68. None, # context
  69. 0, # subtype
  70. 0, # data1
  71. 0, # data2
  72. )
  73. msg(NSApp, n('postEvent:atStart:'), void_p(event), True)
  74. _triggered = Event()
  75. def _input_callback(fdref, flags, info):
  76. """Callback to fire when there's input to be read"""
  77. _triggered.set()
  78. CFFileDescriptorInvalidate(fdref)
  79. CFRelease(fdref)
  80. NSApp = _NSApp()
  81. msg(NSApp, n('stop:'), NSApp)
  82. _wake(NSApp)
  83. _c_callback_func_type = ctypes.CFUNCTYPE(None, void_p, void_p, void_p)
  84. _c_input_callback = _c_callback_func_type(_input_callback)
  85. def _stop_on_read(fd):
  86. """Register callback to stop eventloop when there's data on fd"""
  87. _triggered.clear()
  88. fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None)
  89. CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack)
  90. source = CFFileDescriptorCreateRunLoopSource(None, fdref, 0)
  91. loop = CFRunLoopGetCurrent()
  92. CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes)
  93. CFRelease(source)
  94. def inputhook(context):
  95. """Inputhook for Cocoa (NSApp)"""
  96. NSApp = _NSApp()
  97. _stop_on_read(context.fileno())
  98. msg(NSApp, n('run'))
  99. if not _triggered.is_set():
  100. # app closed without firing callback,
  101. # probably due to last window being closed.
  102. # Run the loop manually in this case,
  103. # since there may be events still to process (#9734)
  104. CoreFoundation.CFRunLoopRun()