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