Show More
@@ -6,7 +6,6 b' import ctypes.util' | |||
|
6 | 6 | |
|
7 | 7 | objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc')) |
|
8 | 8 | CoreFoundation = ctypes.cdll.LoadLibrary(ctypes.util.find_library('CoreFoundation')) |
|
9 | # Cocoa = ctypes.cdll.LoadLibrary(ctypes.util.find_library('Cocoa')) | |
|
10 | 9 | |
|
11 | 10 | void_p = ctypes.c_void_p |
|
12 | 11 | |
@@ -24,7 +23,7 b' def _utf8(s):' | |||
|
24 | 23 | return s |
|
25 | 24 | |
|
26 | 25 | def n(name): |
|
27 | """create a selector name (for methods)""" | |
|
26 | """create a selector name (for ObjC methods)""" | |
|
28 | 27 | return objc.sel_registerName(_utf8(name)) |
|
29 | 28 | |
|
30 | 29 | def C(classname): |
@@ -63,7 +62,6 b' CFFileDescriptorInvalidate = CoreFoundation.CFFileDescriptorInvalidate' | |||
|
63 | 62 | CFFileDescriptorInvalidate.restype = None |
|
64 | 63 | CFFileDescriptorInvalidate.argtypes = [void_p] |
|
65 | 64 | |
|
66 | ||
|
67 | 65 | # From CFFileDescriptor.h |
|
68 | 66 | kCFFileDescriptorReadCallBack = 1 |
|
69 | 67 | kCFRunLoopCommonModes = void_p.in_dll(CoreFoundation, 'kCFRunLoopCommonModes') |
@@ -72,19 +70,37 b' def _NSApp():' | |||
|
72 | 70 | """Return the global NSApplication instance (NSApp)""" |
|
73 | 71 | return msg(C('NSApplication'), n('sharedApplication')) |
|
74 | 72 | |
|
73 | def _wake(NSApp): | |
|
74 | """Wake the Application""" | |
|
75 | event = msg(C('NSEvent'), | |
|
76 | n('otherEventWithType:location:modifierFlags:' | |
|
77 | 'timestamp:windowNumber:context:subtype:data1:data2:'), | |
|
78 | 15, # Type | |
|
79 | 0, # location | |
|
80 | 0, # flags | |
|
81 | 0, # timestamp | |
|
82 | 0, # window | |
|
83 | None, # context | |
|
84 | 0, # subtype | |
|
85 | 0, # data1 | |
|
86 | 0, # data2 | |
|
87 | ) | |
|
88 | msg(NSApp, n('postEvent:atStart:'), void_p(event), True) | |
|
89 | ||
|
75 | 90 | def _input_callback(fdref, flags, info): |
|
76 | 91 | """Callback to fire when there's input to be read""" |
|
77 | 92 | CFFileDescriptorInvalidate(fdref) |
|
78 | 93 | CFRelease(fdref) |
|
79 | 94 | NSApp = _NSApp() |
|
80 | 95 | msg(NSApp, n('stop:'), NSApp) |
|
96 | _wake(NSApp) | |
|
81 | 97 | |
|
82 | 98 | _c_callback_func_type = ctypes.CFUNCTYPE(None, void_p, void_p, void_p) |
|
83 | _c_callback = _c_callback_func_type(_input_callback) | |
|
99 | _c_input_callback = _c_callback_func_type(_input_callback) | |
|
84 | 100 | |
|
85 | 101 | def _stop_on_read(fd): |
|
86 | 102 | """Register callback to stop eventloop when there's data on fd""" |
|
87 | fdref = CFFileDescriptorCreate(None, fd, False, _c_callback, None) | |
|
103 | fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None) | |
|
88 | 104 | CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack) |
|
89 | 105 | source = CFFileDescriptorCreateRunLoopSource(None, fdref, 0) |
|
90 | 106 | loop = CFRunLoopGetCurrent() |
@@ -102,3 +118,4 b' def inputhook(context):' | |||
|
102 | 118 | return |
|
103 | 119 | _stop_on_read(context.fileno()) |
|
104 | 120 | msg(NSApp, n('run')) |
|
121 |
General Comments 0
You need to be logged in to leave comments.
Login now