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