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