Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions IPython/kernel/zmq/eventloops.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ def loop_gtk(kernel):
gtk_kernel.start()


def loop_clutter(kernel):
"""Start the kernel, coordinating with the GTK event loop"""
from .gui.clutterembed import ClutterEmbed

clutter_kernel = ClutterEmbed(kernel)
clutter_kernel.start()


def loop_cocoa(kernel):
"""Start the kernel, coordinating with the Cocoa CFRunLoop event loop
via the matplotlib MacOSX backend.
Expand Down Expand Up @@ -233,6 +241,7 @@ def doi():
'wx' : loop_wx,
'tk' : loop_tk,
'gtk': loop_gtk,
'clutter': loop_clutter,
None : None,
}

Expand Down
87 changes: 87 additions & 0 deletions IPython/kernel/zmq/gui/clutterembed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""GUI support for the IPython ZeroMQ kernel - Clutter support.
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2010-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING.txt, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# stdlib
import sys

# Third-party
from gi.repository import Clutter, GObject

#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------


class ClutterEmbed(object):
"""A class to embed a kernel into the Clutter main event loop.
"""
def __init__(self, kernel):
self.kernel = kernel
# These two will later store the real Clutter functions when we hijack them
self.clutter_main = None
self.clutter_main_quit = None

def start(self):
"""Starts the Clutter main event loop and sets our kernel startup routine.
"""
# Register our function to initiate the kernel and start Clutter
GObject.idle_add(self._wire_kernel)
Clutter.init([])
Clutter.main()

def _wire_kernel(self):
"""Initializes the kernel inside Clutter.

This is meant to run only once at startup, so it does its job and
returns False to ensure it doesn't get run again by Clutter.
"""
self.clutter_main, self.clutter_main_quit = self._hijack_clutter()
GObject.timeout_add(int(1000*self.kernel._poll_interval),
self.iterate_kernel)
return False

def iterate_kernel(self):
"""Run one iteration of the kernel and return True.

Clutter timer functions must return True to be called again, so we make the
call to :meth:`do_one_iteration` and then return True for Clutter.
"""
self.kernel.do_one_iteration()
return True

def stop(self):
# FIXME: this one isn't getting called because we have no reliable
# kernel shutdown. We need to fix that: once the kernel has a
# shutdown mechanism, it can call this.
self.clutter_main_quit()
sys.exit()

def _hijack_clutter(self):
"""Hijack a few key functions in Clutter for IPython integration.

Modifies Clutters main and main_quit with a dummy so user code does not
block IPython. This allows us to use %run to run arbitrary Clutter
scripts from a long-lived IPython session, and when they attempt to
start or stop

Returns
-------
The original functions that have been hijacked:
- Clutter.main
- Clutter.main_quit
"""
def dummy(*args, **kw):
pass
# save and trap main and main_quit from Clutter
orig_main, Clutter.main = Clutter.main, dummy
orig_main_quit, Clutter.main_quit = Clutter.main_quit, dummy
return orig_main, orig_main_quit
31 changes: 31 additions & 0 deletions IPython/lib/inputhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
GUI_GLUT = 'glut'
GUI_PYGLET = 'pyglet'
GUI_GTK3 = 'gtk3'
GUI_CLUTTER = 'CLUTTER'
GUI_NONE = 'none' # i.e. disable

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -461,6 +462,33 @@ def disable_gtk3(self):
"""
self.clear_inputhook()

def enable_clutter(self, app=None):
"""Enable event loop integration with Clutter (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 Clutter, which allows
Clutter to integrate with terminal based applications like
IPython.
"""
from IPython.lib.inputhookclutter import inputhook_clutter
self.set_inputhook(inputhook_clutter)
self._current_gui = GUI_CLUTTER

def disable_clutter(self):
"""Disable event loop integration with disable_clutter.

This merely sets PyOS_InputHook to NULL.
"""
self.clear_inputhook()

def current_gui(self):
"""Return a string indicating the currently active GUI or None."""
return self._current_gui
Expand All @@ -481,6 +509,8 @@ def current_gui(self):
disable_pyglet = inputhook_manager.disable_pyglet
enable_gtk3 = inputhook_manager.enable_gtk3
disable_gtk3 = inputhook_manager.disable_gtk3
enable_clutter = inputhook_manager.enable_clutter
disable_clutter = inputhook_manager.disable_clutter
clear_inputhook = inputhook_manager.clear_inputhook
set_inputhook = inputhook_manager.set_inputhook
current_gui = inputhook_manager.current_gui
Expand All @@ -497,6 +527,7 @@ def current_gui(self):
GUI_GLUT: enable_glut,
GUI_PYGLET: enable_pyglet,
GUI_GTK3: enable_gtk3,
GUI_CLUTTER: enable_clutter,
}


Expand Down
35 changes: 35 additions & 0 deletions IPython/lib/inputhookclutter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# encoding: utf-8
"""
Enable Clutter to be used interacive by IPython.

Authors: Thomas Schüßler
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2012, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import sys
from gi.repository import Clutter, GLib

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------


def _main_quit(*args, **kwargs):
Clutter.main_quit()
return False


def inputhook_clutter():
GLib.io_add_watch(sys.stdin, GLib.IO_IN, _main_quit)
Clutter.main()
return 0
3 changes: 3 additions & 0 deletions IPython/testing/iptest.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def will_run(self):
# We do this unconditionally, so that the test suite doesn't import
# gtk, changing the default encoding and masking some unicode bugs.
sec.exclude('inputhookgtk')
# We do this unconditionally, so that the test suite doesn't import
# clutter.
sec.exclude('inputhookclutter')
# We also do this unconditionally, because wx can interfere with Unix signals.
# There are currently no tests for it anyway.
sec.exclude('inputhookwx')
Expand Down
47 changes: 47 additions & 0 deletions examples/IPython Kernel/gui/gui-clutter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python
"""Simple Clutter example to manually test event loop integration.

This is meant to run tests manually in ipython as:

In [1]: %gui clutter

In [2]: %run gui-clutter.py
"""

from gi.repository import Clutter


def on_click(*args):
print(args)


def stage_key(element, event):
if event.keyval == Clutter.Escape:
clutter_quit()


def clutter_quit(*args):
Clutter.main_quit()


Clutter.init([])
stage = Clutter.Stage()

stage.connect('destroy', clutter_quit)
stage.set_size(400, 400)

actor = Clutter.Actor()
actor.set_size(*stage.get_size())
actor.set_background_color(Clutter.Color.new(255, 147, 8, 255))
actor.connect('button-press-event', on_click)
actor.set_reactive(True)

stage.add_child(actor)
stage.connect('key-press-event', stage_key)
stage.show()

try:
from IPython.lib.inputhook import enable_clutter
enable_clutter()
except ImportError:
Clutter.main()