Show More
@@ -1,56 +1,59 b'' | |||||
1 | # Code borrowed from python-prompt-toolkit examples |
|
1 | # Code borrowed from python-prompt-toolkit examples | |
2 | # https://github.com/jonathanslenders/python-prompt-toolkit/blob/77cdcfbc7f4b4c34a9d2f9a34d422d7152f16209/examples/inputhook.py |
|
2 | # https://github.com/jonathanslenders/python-prompt-toolkit/blob/77cdcfbc7f4b4c34a9d2f9a34d422d7152f16209/examples/inputhook.py | |
3 |
|
3 | |||
4 | # Copyright (c) 2014, Jonathan Slenders |
|
4 | # Copyright (c) 2014, Jonathan Slenders | |
5 | # All rights reserved. |
|
5 | # All rights reserved. | |
6 | # |
|
6 | # | |
7 | # Redistribution and use in source and binary forms, with or without modification, |
|
7 | # Redistribution and use in source and binary forms, with or without modification, | |
8 | # are permitted provided that the following conditions are met: |
|
8 | # are permitted provided that the following conditions are met: | |
9 | # |
|
9 | # | |
10 | # * Redistributions of source code must retain the above copyright notice, this |
|
10 | # * Redistributions of source code must retain the above copyright notice, this | |
11 | # list of conditions and the following disclaimer. |
|
11 | # list of conditions and the following disclaimer. | |
12 | # |
|
12 | # | |
13 | # * Redistributions in binary form must reproduce the above copyright notice, this |
|
13 | # * Redistributions in binary form must reproduce the above copyright notice, this | |
14 | # list of conditions and the following disclaimer in the documentation and/or |
|
14 | # list of conditions and the following disclaimer in the documentation and/or | |
15 | # other materials provided with the distribution. |
|
15 | # other materials provided with the distribution. | |
16 | # |
|
16 | # | |
17 | # * Neither the name of the {organization} nor the names of its |
|
17 | # * Neither the name of the {organization} nor the names of its | |
18 | # contributors may be used to endorse or promote products derived from |
|
18 | # contributors may be used to endorse or promote products derived from | |
19 | # this software without specific prior written permission. |
|
19 | # this software without specific prior written permission. | |
20 | # |
|
20 | # | |
21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
23 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
|
24 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | |
25 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
25 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
26 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
|
27 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
28 | # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 |
|
31 | |||
32 | """ |
|
32 | """ | |
33 | PyGTK input hook for prompt_toolkit. |
|
33 | PyGTK input hook for prompt_toolkit. | |
34 |
|
34 | |||
35 | Listens on the pipe prompt_toolkit sets up for a notification that it should |
|
35 | Listens on the pipe prompt_toolkit sets up for a notification that it should | |
36 | return control to the terminal event loop. |
|
36 | return control to the terminal event loop. | |
37 | """ |
|
37 | """ | |
38 | from __future__ import absolute_import |
|
38 | from __future__ import absolute_import | |
39 |
|
39 | |||
40 | import gtk, gobject |
|
40 | import gtk, gobject | |
41 |
|
41 | |||
|
42 | # Enable threading in GTK. (Otherwise, GTK will keep the GIL.) | |||
|
43 | gtk.gdk.threads_init() | |||
|
44 | ||||
42 | def inputhook(context): |
|
45 | def inputhook(context): | |
43 | """ |
|
46 | """ | |
44 | When the eventloop of prompt-toolkit is idle, call this inputhook. |
|
47 | When the eventloop of prompt-toolkit is idle, call this inputhook. | |
45 |
|
48 | |||
46 | This will run the GTK main loop until the file descriptor |
|
49 | This will run the GTK main loop until the file descriptor | |
47 | `context.fileno()` becomes ready. |
|
50 | `context.fileno()` becomes ready. | |
48 |
|
51 | |||
49 | :param context: An `InputHookContext` instance. |
|
52 | :param context: An `InputHookContext` instance. | |
50 | """ |
|
53 | """ | |
51 | def _main_quit(*a, **kw): |
|
54 | def _main_quit(*a, **kw): | |
52 | gtk.main_quit() |
|
55 | gtk.main_quit() | |
53 | return False |
|
56 | return False | |
54 |
|
57 | |||
55 | gobject.io_add_watch(context.fileno(), gobject.IO_IN, _main_quit) |
|
58 | gobject.io_add_watch(context.fileno(), gobject.IO_IN, _main_quit) | |
56 | gtk.main() |
|
59 | gtk.main() |
General Comments 0
You need to be logged in to leave comments.
Login now