##// END OF EJS Templates
Added code for the GLUT interactive session
Nicolas Rougier -
Show More
@@ -19,6 +19,7 b' from IPython.lib.inputhook import ('
19 enable_gtk, disable_gtk,
19 enable_gtk, disable_gtk,
20 enable_qt4, disable_qt4,
20 enable_qt4, disable_qt4,
21 enable_tk, disable_tk,
21 enable_tk, disable_tk,
22 enable_glut, disable_glut,
22 enable_pyglet, disable_pyglet,
23 enable_pyglet, disable_pyglet,
23 set_inputhook, clear_inputhook,
24 set_inputhook, clear_inputhook,
24 current_gui
25 current_gui
@@ -29,6 +29,7 b" GUI_QT4 = 'qt4'"
29 GUI_GTK = 'gtk'
29 GUI_GTK = 'gtk'
30 GUI_TK = 'tk'
30 GUI_TK = 'tk'
31 GUI_OSX = 'osx'
31 GUI_OSX = 'osx'
32 GUI_GLUT = 'glut'
32 GUI_PYGLET = 'pyglet'
33 GUI_PYGLET = 'pyglet'
33
34
34 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
@@ -285,9 +286,14 b' class InputHookManager(object):'
285 self.clear_inputhook()
286 self.clear_inputhook()
286
287
287
288
289 <<<<<<< HEAD
288
290
289 def enable_pyglet(self, app=None):
291 def enable_pyglet(self, app=None):
290 """Enable event loop integration with pyglet.
292 """Enable event loop integration with pyglet.
293 =======
294 def enable_glut(self, app=None):
295 """Enable event loop integration with GLUT.
296 >>>>>>> Added code for the GLUT interactive session
291
297
292 Parameters
298 Parameters
293 ----------
299 ----------
@@ -298,6 +304,7 b' class InputHookManager(object):'
298
304
299 Notes
305 Notes
300 -----
306 -----
307 <<<<<<< HEAD
301 This methods sets the ``PyOS_InputHook`` for pyglet, which allows
308 This methods sets the ``PyOS_InputHook`` for pyglet, which allows
302 pyglet to integrate with terminal based applications like
309 pyglet to integrate with terminal based applications like
303 IPython.
310 IPython.
@@ -314,6 +321,129 b' class InputHookManager(object):'
314
321
315 This merely sets PyOS_InputHook to NULL.
322 This merely sets PyOS_InputHook to NULL.
316 """
323 """
324 =======
325 This methods sets the PyOS_InputHook for GLUT, which allows
326 the GLUT to integrate with terminal based applications like
327 IPython.
328
329 GLUT is a quite old library and it is difficult to ensure proper
330 integration within IPython since original GLUT does not allow to handle
331 events one by one. Instead, it requires for the mainloop to be entered
332 and never returned (there is not event a function to exit he
333 mainloop). Fortunately, there are alternatives such as freeglut
334 (avaialble for linux and windows) and the OSX implementation gives
335 access to a glutCheckLoop() function that blocks itself until a new
336 event is received. This means we have to setup a default timer to
337 ensure we got at least one event that will unblock the function.
338
339 Furthermore, it is not possible to install these handlers wihtout a
340 window being first created. We choose to make this window visible for
341 the user to realize that it does not need to create a new one (or this
342 will bring troubles). But, display mode options are then set here and
343 it won't be possible for the user to change them without modifying the
344 code or this has to be made availble via IPython options system.
345
346 Script integration
347 ------------------
348
349 ::
350
351 interactive = False
352 if glut.glutGetWindow() > 0:
353 interactive = True
354 else:
355 glut.glutInit(sys.argv)
356 glut.glutInitDisplayMode( glut.GLUT_DOUBLE |
357 glut.GLUT_RGBA |
358 glut.GLUT_DEPTH )
359 ...
360 if not interactive:
361 glut.glutMainLoop()
362 """
363 import OpenGL.GLUT as glut
364 import OpenGL.platform as platform
365
366 def timer_none(fps):
367 ''' Dummy timer function '''
368 pass
369
370 def display():
371 ''' Dummy display function '''
372 pass
373
374 def timer(fps):
375 # We should normally set the active window to 1 and post a
376 # redisplay for each window. The problem is that we do not know
377 # how much active windows we have and there is no function in glut
378 # to get that number.
379 # glut.glutSetWindow(1)
380 glut.glutTimerFunc( int(1000.0/fps), timer, fps)
381 glut.glutPostRedisplay()
382
383 glutMainLoopEvent = None
384 if sys.platform == 'darwin':
385 try:
386 glutCheckLoop = platform.createBaseFunction(
387 'glutCheckLoop', dll=platform.GLUT, resultType=None,
388 argTypes=[],
389 doc='glutCheckLoop( ) -> None',
390 argNames=(),
391 )
392 except AttributeError:
393 raise RuntimeError,\
394 'Your glut implementation does not allow interactive sessions' \
395 'Consider installing freeglut.'
396 glutMainLoopEvent = glutCheckLoop
397 elif glut.HAVE_FREEGLUT:
398 glutMainLoopEvent = glut.glutMainLoopEvent
399 else:
400 raise RuntimeError,\
401 'Your glut implementation does not allow interactive sessions. ' \
402 'Consider installing freeglut.'
403
404 def inputhook_glut():
405 """ Process pending GLUT events only. """
406 # We need to protect against a user pressing Control-C when IPython is
407 # idle and this is running. We trap KeyboardInterrupt and pass.
408 try:
409 glutMainLoopEvent()
410 except KeyboardInterrupt:
411 pass
412 return 0
413
414 # Frame per second : 60
415 # Should be probably an IPython option
416 fps = 60
417 if not self._apps.has_key(GUI_GLUT):
418 glut.glutInit(sys.argv)
419
420 # Display mode shoudl be also an Ipython option since user won't be able
421 # to change it later
422 glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH)
423 glut.glutCreateWindow(sys.argv[0])
424 glut.glutReshapeWindow(1,1)
425 glut.glutHideWindow()
426 glut.glutDisplayFunc(display)
427 glut.glutTimerFunc( int(1000.0/fps), timer, fps)
428 else:
429 glut.glutDisplayFunc(display)
430 glut.glutTimerFunc( int(1000.0/fps), timer, fps)
431
432 self.set_inputhook(inputhook_glut)
433 self._current_gui = GUI_GLUT
434 self._apps[GUI_GLUT] = True
435
436
437 def disable_glut(self):
438 """Disable event loop integration with glut.
439
440 This sets PyOS_InputHook to NULL and set the display function to a
441 dummy one and set the timer to a dummy timer that will be triggered
442 very far in the future.
443 """
444 glut.HideWindow()
445 glut.glutTimerFunc( sys.maxint-1, null_timer_none, 0)
446 >>>>>>> Added code for the GLUT interactive session
317 self.clear_inputhook()
447 self.clear_inputhook()
318
448
319
449
@@ -331,8 +461,13 b' enable_gtk = inputhook_manager.enable_gtk'
331 disable_gtk = inputhook_manager.disable_gtk
461 disable_gtk = inputhook_manager.disable_gtk
332 enable_tk = inputhook_manager.enable_tk
462 enable_tk = inputhook_manager.enable_tk
333 disable_tk = inputhook_manager.disable_tk
463 disable_tk = inputhook_manager.disable_tk
464 <<<<<<< HEAD
334 enable_pyglet = inputhook_manager.enable_pyglet
465 enable_pyglet = inputhook_manager.enable_pyglet
335 disable_pyglet = inputhook_manager.disable_pyglet
466 disable_pyglet = inputhook_manager.disable_pyglet
467 =======
468 enable_glut = inputhook_manager.enable_glut
469 disable_glut = inputhook_manager.disable_glut
470 >>>>>>> Added code for the GLUT interactive session
336 clear_inputhook = inputhook_manager.clear_inputhook
471 clear_inputhook = inputhook_manager.clear_inputhook
337 set_inputhook = inputhook_manager.set_inputhook
472 set_inputhook = inputhook_manager.set_inputhook
338 current_gui = inputhook_manager.current_gui
473 current_gui = inputhook_manager.current_gui
@@ -371,8 +506,12 b' def enable_gui(gui=None, app=None):'
371 GUI_WX: enable_wx,
506 GUI_WX: enable_wx,
372 GUI_QT: enable_qt4, # qt3 not supported
507 GUI_QT: enable_qt4, # qt3 not supported
373 GUI_QT4: enable_qt4,
508 GUI_QT4: enable_qt4,
509 <<<<<<< HEAD
374 GUI_PYGLET: enable_pyglet,
510 GUI_PYGLET: enable_pyglet,
375 }
511 }
512 =======
513 GUI_GLUT: enable_glut}
514 >>>>>>> Added code for the GLUT interactive session
376 try:
515 try:
377 gui_hook = guis[gui]
516 gui_hook = guis[gui]
378 except KeyError:
517 except KeyError:
General Comments 0
You need to be logged in to leave comments. Login now