Debian Codegraph @

IPython/terminal/pt_inputhooks/glut.py

raw
1 """GLUT Input hook for interactive use with prompt_toolkit
2 """
3
4
5 # GLUT is quite an old library and it is difficult to ensure proper
6 # integration within IPython since original GLUT does not allow to handle
7 # events one by one. Instead, it requires for the mainloop to be entered
8 # and never returned (there is not even a function to exit he
9 # mainloop). Fortunately, there are alternatives such as freeglut
10 # (available for linux and windows) and the OSX implementation gives
11 # access to a glutCheckLoop() function that blocks itself until a new
12 # event is received. This means we have to setup the idle callback to
13 # ensure we got at least one event that will unblock the function.
14 #
15 # Furthermore, it is not possible to install these handlers without a window
16 # being first created. We choose to make this window invisible. This means that
17 # display mode options are set at this level and user won't be able to change
18 # them later without modifying the code. This should probably be made available
19 # via IPython options system.
20
21 import sys
22 import time
23 import signal
24 import OpenGL.GLUT as glut
25 import OpenGL.platform as platform
26 from timeit import default_timer as clock
27
28 # Frame per second : 60
29 # Should probably be an IPython option
30 glut_fps = 60
31
32 # Display mode : double buffeed + rgba + depth
33 # Should probably be an IPython option
34 glut_display_mode = (glut.GLUT_DOUBLE |
35 glut.GLUT_RGBA |
36 glut.GLUT_DEPTH)
37
38 glutMainLoopEvent = None
39 if sys.platform == 'darwin':
40 try:
41 glutCheckLoop = platform.createBaseFunction(
42 'glutCheckLoop', dll=platform.GLUT, resultType=None,
43 argTypes=[],
44 doc='glutCheckLoop( ) -> None',
45 argNames=(),
46 )
47 except AttributeError as e:
48 raise RuntimeError(
49 '''Your glut implementation does not allow interactive sessions. '''
50 '''Consider installing freeglut.''') from e
51 glutMainLoopEvent = glutCheckLoop
52 elif glut.HAVE_FREEGLUT:
53 glutMainLoopEvent = glut.glutMainLoopEvent
54 else:
55 raise RuntimeError(
56 '''Your glut implementation does not allow interactive sessions. '''
57 '''Consider installing freeglut.''')
58
59
60 def glut_display():
61 # Dummy display function
62 pass
63
64 def glut_idle():
65 # Dummy idle function
66 pass
67
68 def glut_close():
69 # Close function only hides the current window
70 glut.glutHideWindow()
71 glutMainLoopEvent()
72
73 def glut_int_handler(signum, frame):
74 # Catch sigint and print the defaultipyt message
75 signal.signal(signal.SIGINT, signal.default_int_handler)
76 print('\nKeyboardInterrupt')
77 # Need to reprint the prompt at this stage
78
79 # Initialisation code
80 glut.glutInit( sys.argv )
81 glut.glutInitDisplayMode( glut_display_mode )
82 # This is specific to freeglut
83 if bool(glut.glutSetOption):
84 glut.glutSetOption( glut.GLUT_ACTION_ON_WINDOW_CLOSE,
85 glut.GLUT_ACTION_GLUTMAINLOOP_RETURNS )
86 glut.glutCreateWindow( b'ipython' )
87 glut.glutReshapeWindow( 1, 1 )
88 glut.glutHideWindow( )
89 glut.glutWMCloseFunc( glut_close )
90 glut.glutDisplayFunc( glut_display )
91 glut.glutIdleFunc( glut_idle )
92
93
94 def inputhook(context):
95 """Run the pyglet event loop by processing pending events only.
96
97 This keeps processing pending events until stdin is ready. After
98 processing all pending events, a call to time.sleep is inserted. This is
99 needed, otherwise, CPU usage is at 100%. This sleep time should be tuned
100 though for best performance.
101 """
102 # We need to protect against a user pressing Control-C when IPython is
103 # idle and this is running. We trap KeyboardInterrupt and pass.
104
105 signal.signal(signal.SIGINT, glut_int_handler)
106
107 try:
108 t = clock()
109
110 # Make sure the default window is set after a window has been closed
111 if glut.glutGetWindow() == 0:
112 glut.glutSetWindow( 1 )
113 glutMainLoopEvent()
114 return 0
115
116 while not context.input_is_ready():
117 glutMainLoopEvent()
118 # We need to sleep at this point to keep the idle CPU load
119 # low. However, if sleep to long, GUI response is poor. As
120 # a compromise, we watch how often GUI events are being processed
121 # and switch between a short and long sleep time. Here are some
122 # stats useful in helping to tune this.
123 # time CPU load
124 # 0.001 13%
125 # 0.005 3%
126 # 0.01 1.5%
127 # 0.05 0.5%
128 used_time = clock() - t
129 if used_time > 10.0:
130 # print('Sleep for 1 s') # dbg
131 time.sleep(1.0)
132 elif used_time > 0.1:
133 # Few GUI events coming in, so we can sleep longer
134 # print('Sleep for 0.05 s') # dbg
135 time.sleep(0.05)
136 else:
137 # Many GUI events coming in, so sleep only very little
138 time.sleep(0.001)
139 except KeyboardInterrupt:
140 pass