inputhook.py
529 lines
| 17.7 KiB
| text/x-python
|
PythonLexer
|
r2363 | # coding: utf-8 | ||
|
r2066 | """ | ||
Inputhook management for GUI event loop integration. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
|
r5390 | # Copyright (C) 2008-2011 The IPython Development Team | ||
|
r2066 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r6100 | try: | ||
import ctypes | ||||
except ImportError: | ||||
ctypes = None | ||||
|
r4913 | import os | ||
|
r2195 | import sys | ||
|
r7688 | from distutils.version import LooseVersion as V | ||
|
r6100 | |||
from IPython.utils.warn import warn | ||||
|
r2066 | |||
#----------------------------------------------------------------------------- | ||||
|
r2214 | # Constants | ||
#----------------------------------------------------------------------------- | ||||
# Constants for identifying the GUI toolkits. | ||||
GUI_WX = 'wx' | ||||
|
r2363 | GUI_QT = 'qt' | ||
|
r2214 | GUI_QT4 = 'qt4' | ||
GUI_GTK = 'gtk' | ||||
GUI_TK = 'tk' | ||||
|
r3462 | GUI_OSX = 'osx' | ||
|
r4806 | GUI_GLUT = 'glut' | ||
|
r4692 | GUI_PYGLET = 'pyglet' | ||
|
r6459 | GUI_GTK3 = 'gtk3' | ||
|
r4943 | GUI_NONE = 'none' # i.e. disable | ||
|
r2214 | |||
#----------------------------------------------------------------------------- | ||||
|
r4913 | # Utilities | ||
|
r2066 | #----------------------------------------------------------------------------- | ||
|
r4930 | def _stdin_ready_posix(): | ||
"""Return True if there's something to read on stdin (posix version).""" | ||||
infds, outfds, erfds = select.select([sys.stdin],[],[],0) | ||||
return bool(infds) | ||||
def _stdin_ready_nt(): | ||||
"""Return True if there's something to read on stdin (nt version).""" | ||||
return msvcrt.kbhit() | ||||
def _stdin_ready_other(): | ||||
"""Return True, assuming there's something to read on stdin.""" | ||||
return True # | ||||
|
r4944 | |||
def _ignore_CTRL_C_posix(): | ||||
"""Ignore CTRL+C (SIGINT).""" | ||||
signal.signal(signal.SIGINT, signal.SIG_IGN) | ||||
def _allow_CTRL_C_posix(): | ||||
"""Take CTRL+C into account (SIGINT).""" | ||||
signal.signal(signal.SIGINT, signal.default_int_handler) | ||||
def _ignore_CTRL_C_other(): | ||||
"""Ignore CTRL+C (not implemented).""" | ||||
pass | ||||
def _allow_CTRL_C_other(): | ||||
"""Take CTRL+C into account (not implemented).""" | ||||
pass | ||||
|
r4930 | if os.name == 'posix': | ||
import select | ||||
|
r4944 | import signal | ||
|
r4930 | stdin_ready = _stdin_ready_posix | ||
|
r4944 | ignore_CTRL_C = _ignore_CTRL_C_posix | ||
allow_CTRL_C = _allow_CTRL_C_posix | ||||
|
r4930 | elif os.name == 'nt': | ||
import msvcrt | ||||
stdin_ready = _stdin_ready_nt | ||||
|
r4944 | ignore_CTRL_C = _ignore_CTRL_C_other | ||
allow_CTRL_C = _allow_CTRL_C_other | ||||
|
r4930 | else: | ||
stdin_ready = _stdin_ready_other | ||||
|
r4944 | ignore_CTRL_C = _ignore_CTRL_C_other | ||
allow_CTRL_C = _allow_CTRL_C_other | ||||
|
r4913 | |||
|
r2213 | |||
|
r2214 | #----------------------------------------------------------------------------- | ||
# Main InputHookManager class | ||||
#----------------------------------------------------------------------------- | ||||
|
r2210 | |||
|
r2066 | class InputHookManager(object): | ||
|
r2197 | """Manage PyOS_InputHook for different GUI toolkits. | ||
This class installs various hooks under ``PyOSInputHook`` to handle | ||||
GUI event loop integration. | ||||
""" | ||||
|
r6467 | |||
|
r2066 | def __init__(self): | ||
|
r6100 | if ctypes is None: | ||
|
r8223 | warn("IPython GUI event loop requires ctypes, %gui will not be available") | ||
|
r6100 | return | ||
|
r2066 | self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int) | ||
|
r2208 | self._apps = {} | ||
|
r2066 | self._reset() | ||
def _reset(self): | ||||
self._callback_pyfunctype = None | ||||
self._callback = None | ||||
self._installed = False | ||||
|
r2197 | self._current_gui = None | ||
|
r2066 | |||
def get_pyos_inputhook(self): | ||||
|
r2209 | """Return the current PyOS_InputHook as a ctypes.c_void_p.""" | ||
|
r2066 | return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook") | ||
def get_pyos_inputhook_as_func(self): | ||||
|
r2209 | """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE.""" | ||
|
r2066 | return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook") | ||
|
r2069 | def set_inputhook(self, callback): | ||
|
r2209 | """Set PyOS_InputHook to callback and return the previous one.""" | ||
|
r4944 | # On platforms with 'readline' support, it's all too likely to | ||
# have a KeyboardInterrupt signal delivered *even before* an | ||||
# initial ``try:`` clause in the callback can be executed, so | ||||
# we need to disable CTRL+C in this situation. | ||||
ignore_CTRL_C() | ||||
|
r2066 | self._callback = callback | ||
self._callback_pyfunctype = self.PYFUNC(callback) | ||||
pyos_inputhook_ptr = self.get_pyos_inputhook() | ||||
original = self.get_pyos_inputhook_as_func() | ||||
pyos_inputhook_ptr.value = \ | ||||
ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value | ||||
self._installed = True | ||||
return original | ||||
|
r2363 | def clear_inputhook(self, app=None): | ||
"""Set PyOS_InputHook to NULL and return the previous one. | ||||
Parameters | ||||
---------- | ||||
app : optional, ignored | ||||
This parameter is allowed only so that clear_inputhook() can be | ||||
called with a similar interface as all the ``enable_*`` methods. But | ||||
the actual value of the parameter is ignored. This uniform interface | ||||
makes it easier to have user-level entry points in the main IPython | ||||
app like :meth:`enable_gui`.""" | ||||
|
r2066 | pyos_inputhook_ptr = self.get_pyos_inputhook() | ||
original = self.get_pyos_inputhook_as_func() | ||||
pyos_inputhook_ptr.value = ctypes.c_void_p(None).value | ||||
|
r4944 | allow_CTRL_C() | ||
|
r2066 | self._reset() | ||
return original | ||||
|
r2208 | def clear_app_refs(self, gui=None): | ||
"""Clear IPython's internal reference to an application instance. | ||||
|
r2209 | Whenever we create an app for a user on qt4 or wx, we hold a | ||
reference to the app. This is needed because in some cases bad things | ||||
can happen if a user doesn't hold a reference themselves. This | ||||
method is provided to clear the references we are holding. | ||||
|
r2208 | Parameters | ||
---------- | ||||
gui : None or str | ||||
If None, clear all app references. If ('wx', 'qt4') clear | ||||
the app for that toolkit. References are not held for gtk or tk | ||||
as those toolkits don't have the notion of an app. | ||||
""" | ||||
if gui is None: | ||||
self._apps = {} | ||||
|
r7859 | elif gui in self._apps: | ||
|
r2208 | del self._apps[gui] | ||
|
r4419 | def enable_wx(self, app=None): | ||
|
r2069 | """Enable event loop integration with wxPython. | ||
|
r2197 | |||
Parameters | ||||
---------- | ||||
|
r4419 | app : WX Application, optional. | ||
Running application to use. If not given, we probe WX for an | ||||
existing application object, and create a new one if none is found. | ||||
|
r2197 | |||
Notes | ||||
----- | ||||
|
r2209 | This methods sets the ``PyOS_InputHook`` for wxPython, which allows | ||
|
r2069 | the wxPython to integrate with terminal based applications like | ||
IPython. | ||||
|
r2209 | |||
|
r4419 | If ``app`` is not given we probe for an existing one, and return it if | ||
found. If no existing app is found, we create an :class:`wx.App` as | ||||
follows:: | ||||
|
r2209 | |||
import wx | ||||
app = wx.App(redirect=False, clearSigInt=False) | ||||
|
r2069 | """ | ||
|
r7688 | import wx | ||
wx_version = V(wx.__version__).version | ||||
if wx_version < [2, 8]: | ||||
raise ValueError("requires wxPython >= 2.8, but you have %s" % wx.__version__) | ||||
|
r2068 | from IPython.lib.inputhookwx import inputhook_wx | ||
|
r2066 | self.set_inputhook(inputhook_wx) | ||
|
r2214 | self._current_gui = GUI_WX | ||
|
r2918 | import wx | ||
|
r4419 | if app is None: | ||
app = wx.GetApp() | ||||
|
r2918 | if app is None: | ||
app = wx.App(redirect=False, clearSigInt=False) | ||||
app._in_event_loop = True | ||||
self._apps[GUI_WX] = app | ||||
return app | ||||
|
r2066 | |||
def disable_wx(self): | ||||
|
r2069 | """Disable event loop integration with wxPython. | ||
|
r2209 | |||
|
r2069 | This merely sets PyOS_InputHook to NULL. | ||
""" | ||||
|
r7859 | if GUI_WX in self._apps: | ||
|
r2918 | self._apps[GUI_WX]._in_event_loop = False | ||
|
r2066 | self.clear_inputhook() | ||
|
r4419 | def enable_qt4(self, app=None): | ||
|
r2069 | """Enable event loop integration with PyQt4. | ||
|
r6467 | |||
|
r2197 | Parameters | ||
---------- | ||||
|
r4419 | app : Qt Application, optional. | ||
Running application to use. If not given, we probe Qt for an | ||||
existing application object, and create a new one if none is found. | ||||
|
r2197 | |||
Notes | ||||
----- | ||||
|
r2209 | This methods sets the PyOS_InputHook for PyQt4, which allows | ||
|
r2069 | the PyQt4 to integrate with terminal based applications like | ||
IPython. | ||||
|
r2209 | |||
|
r4419 | If ``app`` is not given we probe for an existing one, and return it if | ||
found. If no existing app is found, we create an :class:`QApplication` | ||||
as follows:: | ||||
|
r2209 | |||
from PyQt4 import QtCore | ||||
app = QtGui.QApplication(sys.argv) | ||||
|
r2069 | """ | ||
|
r4931 | from IPython.lib.inputhookqt4 import create_inputhook_qt4 | ||
app, inputhook_qt4 = create_inputhook_qt4(self, app) | ||||
|
r4915 | self.set_inputhook(inputhook_qt4) | ||
self._current_gui = GUI_QT4 | ||||
|
r2918 | app._in_event_loop = True | ||
self._apps[GUI_QT4] = app | ||||
return app | ||||
|
r2066 | |||
def disable_qt4(self): | ||||
|
r2069 | """Disable event loop integration with PyQt4. | ||
|
r2209 | |||
|
r2069 | This merely sets PyOS_InputHook to NULL. | ||
""" | ||||
|
r7859 | if GUI_QT4 in self._apps: | ||
|
r2918 | self._apps[GUI_QT4]._in_event_loop = False | ||
|
r2066 | self.clear_inputhook() | ||
|
r4419 | def enable_gtk(self, app=None): | ||
|
r2069 | """Enable event loop integration with PyGTK. | ||
|
r2197 | |||
Parameters | ||||
---------- | ||||
|
r4419 | app : ignored | ||
Ignored, it's only a placeholder to keep the call signature of all | ||||
gui activation methods consistent, which simplifies the logic of | ||||
supporting magics. | ||||
|
r2197 | |||
Notes | ||||
----- | ||||
|
r2069 | This methods sets the PyOS_InputHook for PyGTK, which allows | ||
the PyGTK to integrate with terminal based applications like | ||||
IPython. | ||||
""" | ||||
|
r2066 | import gtk | ||
try: | ||||
gtk.set_interactive(True) | ||||
|
r2214 | self._current_gui = GUI_GTK | ||
|
r2066 | except AttributeError: | ||
# For older versions of gtk, use our own ctypes version | ||||
|
r2068 | from IPython.lib.inputhookgtk import inputhook_gtk | ||
|
r2207 | self.set_inputhook(inputhook_gtk) | ||
|
r2214 | self._current_gui = GUI_GTK | ||
|
r2066 | |||
def disable_gtk(self): | ||||
|
r2069 | """Disable event loop integration with PyGTK. | ||
|
r6467 | |||
|
r2069 | This merely sets PyOS_InputHook to NULL. | ||
""" | ||||
|
r2066 | self.clear_inputhook() | ||
|
r4419 | def enable_tk(self, app=None): | ||
|
r2197 | """Enable event loop integration with Tk. | ||
Parameters | ||||
---------- | ||||
|
r4419 | app : toplevel :class:`Tkinter.Tk` widget, optional. | ||
|
r4421 | Running toplevel widget to use. If not given, we probe Tk for an | ||
existing one, and create a new one if none is found. | ||||
|
r2197 | |||
Notes | ||||
----- | ||||
|
r4419 | If you have already created a :class:`Tkinter.Tk` object, the only | ||
thing done by this method is to register with the | ||||
:class:`InputHookManager`, since creating that object automatically | ||||
|
r2197 | sets ``PyOS_InputHook``. | ||
""" | ||||
|
r2214 | self._current_gui = GUI_TK | ||
|
r4419 | if app is None: | ||
|
r2214 | import Tkinter | ||
app = Tkinter.Tk() | ||||
app.withdraw() | ||||
self._apps[GUI_TK] = app | ||||
return app | ||||
|
r2066 | |||
def disable_tk(self): | ||||
|
r2069 | """Disable event loop integration with Tkinter. | ||
|
r6467 | |||
|
r2069 | This merely sets PyOS_InputHook to NULL. | ||
""" | ||||
|
r2066 | self.clear_inputhook() | ||
|
r4819 | |||
|
r4806 | def enable_glut(self, app=None): | ||
|
r4819 | """ Enable event loop integration with GLUT. | ||
|
r4692 | |||
Parameters | ||||
---------- | ||||
|
r4819 | |||
|
r4692 | app : ignored | ||
|
r4819 | Ignored, it's only a placeholder to keep the call signature of all | ||
gui activation methods consistent, which simplifies the logic of | ||||
supporting magics. | ||||
|
r4692 | |||
Notes | ||||
----- | ||||
|
r4812 | This methods sets the PyOS_InputHook for GLUT, which allows the GLUT to | ||
integrate with terminal based applications like IPython. Due to GLUT | ||||
limitations, it is currently not possible to start the event loop | ||||
without first creating a window. You should thus not create another | ||||
window but use instead the created one. See 'gui-glut.py' in the | ||||
docs/examples/lib directory. | ||||
|
r6467 | |||
|
r4812 | The default screen mode is set to: | ||
|
r4819 | glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH | ||
|
r4806 | """ | ||
|
r4812 | |||
|
r4831 | import OpenGL.GLUT as glut | ||
|
r4834 | from IPython.lib.inputhookglut import glut_display_mode, \ | ||
glut_close, glut_display, \ | ||||
glut_idle, inputhook_glut | ||||
|
r4806 | |||
|
r7859 | if GUI_GLUT not in self._apps: | ||
|
r4834 | glut.glutInit( sys.argv ) | ||
glut.glutInitDisplayMode( glut_display_mode ) | ||||
|
r4831 | # This is specific to freeglut | ||
if bool(glut.glutSetOption): | ||||
|
r4834 | glut.glutSetOption( glut.GLUT_ACTION_ON_WINDOW_CLOSE, | ||
glut.GLUT_ACTION_GLUTMAINLOOP_RETURNS ) | ||||
glut.glutCreateWindow( sys.argv[0] ) | ||||
|
r4831 | glut.glutReshapeWindow( 1, 1 ) | ||
|
r4834 | glut.glutHideWindow( ) | ||
glut.glutWMCloseFunc( glut_close ) | ||||
glut.glutDisplayFunc( glut_display ) | ||||
glut.glutIdleFunc( glut_idle ) | ||||
|
r4806 | else: | ||
|
r4834 | glut.glutWMCloseFunc( glut_close ) | ||
glut.glutDisplayFunc( glut_display ) | ||||
|
r4831 | glut.glutIdleFunc( glut_idle) | ||
|
r4834 | self.set_inputhook( inputhook_glut ) | ||
|
r4806 | self._current_gui = GUI_GLUT | ||
self._apps[GUI_GLUT] = True | ||||
|
r4819 | |||
|
r4806 | def disable_glut(self): | ||
"""Disable event loop integration with glut. | ||||
|
r6467 | |||
|
r4806 | This sets PyOS_InputHook to NULL and set the display function to a | ||
dummy one and set the timer to a dummy timer that will be triggered | ||||
very far in the future. | ||||
""" | ||||
|
r4834 | import OpenGL.GLUT as glut | ||
from glut_support import glutMainLoopEvent | ||||
|
r4812 | |||
glut.glutHideWindow() # This is an event to be processed below | ||||
glutMainLoopEvent() | ||||
|
r4692 | self.clear_inputhook() | ||
|
r4812 | |||
def enable_pyglet(self, app=None): | ||||
"""Enable event loop integration with pyglet. | ||||
Parameters | ||||
---------- | ||||
app : ignored | ||||
Ignored, it's only a placeholder to keep the call signature of all | ||||
gui activation methods consistent, which simplifies the logic of | ||||
supporting magics. | ||||
|
r4692 | |||
|
r4812 | Notes | ||
----- | ||||
This methods sets the ``PyOS_InputHook`` for pyglet, which allows | ||||
pyglet to integrate with terminal based applications like | ||||
IPython. | ||||
""" | ||||
import pyglet | ||||
from IPython.lib.inputhookpyglet import inputhook_pyglet | ||||
self.set_inputhook(inputhook_pyglet) | ||||
self._current_gui = GUI_PYGLET | ||||
return app | ||||
def disable_pyglet(self): | ||||
"""Disable event loop integration with pyglet. | ||||
This merely sets PyOS_InputHook to NULL. | ||||
""" | ||||
self.clear_inputhook() | ||||
|
r4692 | |||
|
r6459 | def enable_gtk3(self, app=None): | ||
"""Enable event loop integration with Gtk3 (gir bindings). | ||||
Parameters | ||||
---------- | ||||
app : ignored | ||||
Ignored, it's only a placeholder to keep the call signature of all | ||||
gui activation methods consistent, which simplifies the logic of | ||||
supporting magics. | ||||
Notes | ||||
----- | ||||
This methods sets the PyOS_InputHook for Gtk3, which allows | ||||
the Gtk3 to integrate with terminal based applications like | ||||
IPython. | ||||
""" | ||||
from IPython.lib.inputhookgtk3 import inputhook_gtk3 | ||||
self.set_inputhook(inputhook_gtk3) | ||||
self._current_gui = GUI_GTK | ||||
def disable_gtk3(self): | ||||
"""Disable event loop integration with PyGTK. | ||||
This merely sets PyOS_InputHook to NULL. | ||||
""" | ||||
self.clear_inputhook() | ||||
|
r2197 | def current_gui(self): | ||
"""Return a string indicating the currently active GUI or None.""" | ||||
return self._current_gui | ||||
|
r2066 | inputhook_manager = InputHookManager() | ||
enable_wx = inputhook_manager.enable_wx | ||||
disable_wx = inputhook_manager.disable_wx | ||||
enable_qt4 = inputhook_manager.enable_qt4 | ||||
disable_qt4 = inputhook_manager.disable_qt4 | ||||
enable_gtk = inputhook_manager.enable_gtk | ||||
disable_gtk = inputhook_manager.disable_gtk | ||||
|
r2068 | enable_tk = inputhook_manager.enable_tk | ||
|
r2069 | disable_tk = inputhook_manager.disable_tk | ||
|
r4806 | enable_glut = inputhook_manager.enable_glut | ||
disable_glut = inputhook_manager.disable_glut | ||||
|
r4812 | enable_pyglet = inputhook_manager.enable_pyglet | ||
disable_pyglet = inputhook_manager.disable_pyglet | ||||
|
r6459 | enable_gtk3 = inputhook_manager.enable_gtk3 | ||
disable_gtk3 = inputhook_manager.disable_gtk3 | ||||
|
r2069 | clear_inputhook = inputhook_manager.clear_inputhook | ||
|
r2197 | set_inputhook = inputhook_manager.set_inputhook | ||
|
r2208 | current_gui = inputhook_manager.current_gui | ||
clear_app_refs = inputhook_manager.clear_app_refs | ||||
|
r2363 | |||
# Convenience function to switch amongst them | ||||
|
r4419 | def enable_gui(gui=None, app=None): | ||
|
r2363 | """Switch amongst GUI input hooks by name. | ||
This is just a utility wrapper around the methods of the InputHookManager | ||||
object. | ||||
Parameters | ||||
---------- | ||||
gui : optional, string or None | ||||
|
r4943 | If None (or 'none'), clears input hook, otherwise it must be one | ||
of the recognized GUI names (see ``GUI_*`` constants in module). | ||||
|
r2363 | |||
|
r4419 | app : optional, existing application object. | ||
For toolkits that have the concept of a global app, you can supply an | ||||
existing one. If not given, the toolkit will be probed for one, and if | ||||
none is found, a new one will be created. Note that GTK does not have | ||||
this concept, and passing an app if `gui`=="GTK" will raise an error. | ||||
|
r2363 | |||
Returns | ||||
------- | ||||
The output of the underlying gui switch routine, typically the actual | ||||
PyOS_InputHook wrapper object or the GUI toolkit app created, if there was | ||||
one. | ||||
""" | ||||
guis = {None: clear_inputhook, | ||||
|
r4943 | GUI_NONE: clear_inputhook, | ||
|
r3462 | GUI_OSX: lambda app=False: None, | ||
|
r2363 | GUI_TK: enable_tk, | ||
GUI_GTK: enable_gtk, | ||||
GUI_WX: enable_wx, | ||||
GUI_QT: enable_qt4, # qt3 not supported | ||||
|
r4692 | GUI_QT4: enable_qt4, | ||
|
r4812 | GUI_GLUT: enable_glut, | ||
|
r4692 | GUI_PYGLET: enable_pyglet, | ||
|
r6460 | GUI_GTK3: enable_gtk3, | ||
|
r4692 | } | ||
|
r2363 | try: | ||
gui_hook = guis[gui] | ||||
except KeyError: | ||||
|
r4419 | e = "Invalid GUI request %r, valid ones are:%s" % (gui, guis.keys()) | ||
|
r2363 | raise ValueError(e) | ||
|
r4419 | return gui_hook(app) | ||
|
r2918 | |||