Show More
@@ -1,3 +1,7 b'' | |||||
|
1 | """Inputhook for OS X | |||
|
2 | ||||
|
3 | Calls NSApp / CoreFoundation APIs via ctypes. | |||
|
4 | """ | |||
1 |
|
5 | |||
2 | # obj-c boilerplate from appnope, used under BSD 2-clause |
|
6 | # obj-c boilerplate from appnope, used under BSD 2-clause | |
3 |
|
7 | |||
@@ -5,7 +9,6 b' import ctypes' | |||||
5 | import ctypes.util |
|
9 | import ctypes.util | |
6 |
|
10 | |||
7 | objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc')) |
|
11 | objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc')) | |
8 | CoreFoundation = ctypes.cdll.LoadLibrary(ctypes.util.find_library('CoreFoundation')) |
|
|||
9 |
|
12 | |||
10 | void_p = ctypes.c_void_p |
|
13 | void_p = ctypes.c_void_p | |
11 |
|
14 | |||
@@ -30,7 +33,11 b' def C(classname):' | |||||
30 | """get an ObjC Class by name""" |
|
33 | """get an ObjC Class by name""" | |
31 | return objc.objc_getClass(_utf8(classname)) |
|
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 | CFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate |
|
41 | CFFileDescriptorCreate = CoreFoundation.CFFileDescriptorCreate | |
35 | CFFileDescriptorCreate.restype = void_p |
|
42 | CFFileDescriptorCreate.restype = void_p | |
36 | CFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p] |
|
43 | CFFileDescriptorCreate.argtypes = [void_p, ctypes.c_int, ctypes.c_bool, void_p] | |
@@ -66,10 +73,12 b' CFFileDescriptorInvalidate.argtypes = [void_p]' | |||||
66 | kCFFileDescriptorReadCallBack = 1 |
|
73 | kCFFileDescriptorReadCallBack = 1 | |
67 | kCFRunLoopCommonModes = void_p.in_dll(CoreFoundation, 'kCFRunLoopCommonModes') |
|
74 | kCFRunLoopCommonModes = void_p.in_dll(CoreFoundation, 'kCFRunLoopCommonModes') | |
68 |
|
75 | |||
|
76 | ||||
69 | def _NSApp(): |
|
77 | def _NSApp(): | |
70 | """Return the global NSApplication instance (NSApp)""" |
|
78 | """Return the global NSApplication instance (NSApp)""" | |
71 | return msg(C('NSApplication'), n('sharedApplication')) |
|
79 | return msg(C('NSApplication'), n('sharedApplication')) | |
72 |
|
80 | |||
|
81 | ||||
73 | def _wake(NSApp): |
|
82 | def _wake(NSApp): | |
74 | """Wake the Application""" |
|
83 | """Wake the Application""" | |
75 | event = msg(C('NSEvent'), |
|
84 | event = msg(C('NSEvent'), | |
@@ -87,6 +96,7 b' def _wake(NSApp):' | |||||
87 | ) |
|
96 | ) | |
88 | msg(NSApp, n('postEvent:atStart:'), void_p(event), True) |
|
97 | msg(NSApp, n('postEvent:atStart:'), void_p(event), True) | |
89 |
|
98 | |||
|
99 | ||||
90 | def _input_callback(fdref, flags, info): |
|
100 | def _input_callback(fdref, flags, info): | |
91 | """Callback to fire when there's input to be read""" |
|
101 | """Callback to fire when there's input to be read""" | |
92 | CFFileDescriptorInvalidate(fdref) |
|
102 | CFFileDescriptorInvalidate(fdref) | |
@@ -98,6 +108,7 b' def _input_callback(fdref, flags, info):' | |||||
98 | _c_callback_func_type = ctypes.CFUNCTYPE(None, void_p, void_p, void_p) |
|
108 | _c_callback_func_type = ctypes.CFUNCTYPE(None, void_p, void_p, void_p) | |
99 | _c_input_callback = _c_callback_func_type(_input_callback) |
|
109 | _c_input_callback = _c_callback_func_type(_input_callback) | |
100 |
|
110 | |||
|
111 | ||||
101 | def _stop_on_read(fd): |
|
112 | def _stop_on_read(fd): | |
102 | """Register callback to stop eventloop when there's data on fd""" |
|
113 | """Register callback to stop eventloop when there's data on fd""" | |
103 | fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None) |
|
114 | fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None) | |
@@ -107,6 +118,7 b' def _stop_on_read(fd):' | |||||
107 | CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes) |
|
118 | CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes) | |
108 | CFRelease(source) |
|
119 | CFRelease(source) | |
109 |
|
120 | |||
|
121 | ||||
110 | def inputhook(context): |
|
122 | def inputhook(context): | |
111 | """Inputhook for Cocoa (NSApp)""" |
|
123 | """Inputhook for Cocoa (NSApp)""" | |
112 | NSApp = _NSApp() |
|
124 | NSApp = _NSApp() |
General Comments 0
You need to be logged in to leave comments.
Login now