Show More
@@ -0,0 +1,148 b'' | |||
|
1 | """Enable wxPython to be used interacively in prompt_toolkit | |
|
2 | """ | |
|
3 | from __future__ import absolute_import | |
|
4 | ||
|
5 | import sys | |
|
6 | import signal | |
|
7 | import time | |
|
8 | from timeit import default_timer as clock | |
|
9 | import wx | |
|
10 | ||
|
11 | ||
|
12 | def inputhook_wx1(context): | |
|
13 | """Run the wx event loop by processing pending events only. | |
|
14 | ||
|
15 | This approach seems to work, but its performance is not great as it | |
|
16 | relies on having PyOS_InputHook called regularly. | |
|
17 | """ | |
|
18 | try: | |
|
19 | app = wx.GetApp() | |
|
20 | if app is not None: | |
|
21 | assert wx.Thread_IsMain() | |
|
22 | ||
|
23 | # Make a temporary event loop and process system events until | |
|
24 | # there are no more waiting, then allow idle events (which | |
|
25 | # will also deal with pending or posted wx events.) | |
|
26 | evtloop = wx.EventLoop() | |
|
27 | ea = wx.EventLoopActivator(evtloop) | |
|
28 | while evtloop.Pending(): | |
|
29 | evtloop.Dispatch() | |
|
30 | app.ProcessIdle() | |
|
31 | del ea | |
|
32 | except KeyboardInterrupt: | |
|
33 | pass | |
|
34 | return 0 | |
|
35 | ||
|
36 | class EventLoopTimer(wx.Timer): | |
|
37 | ||
|
38 | def __init__(self, func): | |
|
39 | self.func = func | |
|
40 | wx.Timer.__init__(self) | |
|
41 | ||
|
42 | def Notify(self): | |
|
43 | self.func() | |
|
44 | ||
|
45 | class EventLoopRunner(object): | |
|
46 | ||
|
47 | def Run(self, time, input_is_ready): | |
|
48 | self.input_is_ready = input_is_ready | |
|
49 | self.evtloop = wx.EventLoop() | |
|
50 | self.timer = EventLoopTimer(self.check_stdin) | |
|
51 | self.timer.Start(time) | |
|
52 | self.evtloop.Run() | |
|
53 | ||
|
54 | def check_stdin(self): | |
|
55 | if self.input_is_ready(): | |
|
56 | self.timer.Stop() | |
|
57 | self.evtloop.Exit() | |
|
58 | ||
|
59 | def inputhook_wx2(context): | |
|
60 | """Run the wx event loop, polling for stdin. | |
|
61 | ||
|
62 | This version runs the wx eventloop for an undetermined amount of time, | |
|
63 | during which it periodically checks to see if anything is ready on | |
|
64 | stdin. If anything is ready on stdin, the event loop exits. | |
|
65 | ||
|
66 | The argument to elr.Run controls how often the event loop looks at stdin. | |
|
67 | This determines the responsiveness at the keyboard. A setting of 1000 | |
|
68 | enables a user to type at most 1 char per second. I have found that a | |
|
69 | setting of 10 gives good keyboard response. We can shorten it further, | |
|
70 | but eventually performance would suffer from calling select/kbhit too | |
|
71 | often. | |
|
72 | """ | |
|
73 | try: | |
|
74 | app = wx.GetApp() | |
|
75 | if app is not None: | |
|
76 | assert wx.Thread_IsMain() | |
|
77 | elr = EventLoopRunner() | |
|
78 | # As this time is made shorter, keyboard response improves, but idle | |
|
79 | # CPU load goes up. 10 ms seems like a good compromise. | |
|
80 | elr.Run(time=10, # CHANGE time here to control polling interval | |
|
81 | input_is_ready=context.input_is_ready) | |
|
82 | except KeyboardInterrupt: | |
|
83 | pass | |
|
84 | return 0 | |
|
85 | ||
|
86 | def inputhook_wx3(context): | |
|
87 | """Run the wx event loop by processing pending events only. | |
|
88 | ||
|
89 | This is like inputhook_wx1, but it keeps processing pending events | |
|
90 | until stdin is ready. After processing all pending events, a call to | |
|
91 | time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. | |
|
92 | This sleep time should be tuned though for best performance. | |
|
93 | """ | |
|
94 | # We need to protect against a user pressing Control-C when IPython is | |
|
95 | # idle and this is running. We trap KeyboardInterrupt and pass. | |
|
96 | try: | |
|
97 | app = wx.GetApp() | |
|
98 | if app is not None: | |
|
99 | assert wx.Thread_IsMain() | |
|
100 | ||
|
101 | # The import of wx on Linux sets the handler for signal.SIGINT | |
|
102 | # to 0. This is a bug in wx or gtk. We fix by just setting it | |
|
103 | # back to the Python default. | |
|
104 | if not callable(signal.getsignal(signal.SIGINT)): | |
|
105 | signal.signal(signal.SIGINT, signal.default_int_handler) | |
|
106 | ||
|
107 | evtloop = wx.EventLoop() | |
|
108 | ea = wx.EventLoopActivator(evtloop) | |
|
109 | t = clock() | |
|
110 | while not context.input_is_ready(): | |
|
111 | while evtloop.Pending(): | |
|
112 | t = clock() | |
|
113 | evtloop.Dispatch() | |
|
114 | app.ProcessIdle() | |
|
115 | # We need to sleep at this point to keep the idle CPU load | |
|
116 | # low. However, if sleep to long, GUI response is poor. As | |
|
117 | # a compromise, we watch how often GUI events are being processed | |
|
118 | # and switch between a short and long sleep time. Here are some | |
|
119 | # stats useful in helping to tune this. | |
|
120 | # time CPU load | |
|
121 | # 0.001 13% | |
|
122 | # 0.005 3% | |
|
123 | # 0.01 1.5% | |
|
124 | # 0.05 0.5% | |
|
125 | used_time = clock() - t | |
|
126 | if used_time > 10.0: | |
|
127 | # print 'Sleep for 1 s' # dbg | |
|
128 | time.sleep(1.0) | |
|
129 | elif used_time > 0.1: | |
|
130 | # Few GUI events coming in, so we can sleep longer | |
|
131 | # print 'Sleep for 0.05 s' # dbg | |
|
132 | time.sleep(0.05) | |
|
133 | else: | |
|
134 | # Many GUI events coming in, so sleep only very little | |
|
135 | time.sleep(0.001) | |
|
136 | del ea | |
|
137 | except KeyboardInterrupt: | |
|
138 | pass | |
|
139 | return 0 | |
|
140 | ||
|
141 | if sys.platform == 'darwin': | |
|
142 | # On OSX, evtloop.Pending() always returns True, regardless of there being | |
|
143 | # any events pending. As such we can't use implementations 1 or 3 of the | |
|
144 | # inputhook as those depend on a pending/dispatch loop. | |
|
145 | inputhook = inputhook_wx2 | |
|
146 | else: | |
|
147 | # This is our default implementation | |
|
148 | inputhook = inputhook_wx3 |
General Comments 0
You need to be logged in to leave comments.
Login now