gtk.py 2.4 KB

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