On Sat, 2008-10-18 at 22:51 +0100, Emmanuele Bassi wrote:

> you can create an instance of a C GObject using the ctypes module and
> possibly some syntactic sugar using a module; but no: you cannot mix a
> pygobject based binding (like pyclutter) and a ctypes-based one. so your
> application would either be completely using ctypes or completely using
> pyclutter.

That's not strictly true. It is possible to do it with some voodoo that
I don't really understand. Please see this FAQ:

http://faq.pygtk.org/index.py?req=show&file=faq23.041.htp

Attached is an example using that to create a ClutterGLXTexturePixmap
and manipulate it as if it were a clutter.Actor

- Neil
# Ctypes helper code from
# http://faq.pygtk.org/index.py?req=show&file=faq23.041.htp
import ctypes
import sys
import clutter

import gobject

class _PyGObject_Functions(ctypes.Structure):
   _fields_ = [
       ('register_class',
        ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p,
                          ctypes.c_int, ctypes.py_object,
                          ctypes.py_object)),
       ('register_wrapper',
        ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.py_object)),
       ('register_sinkfunc',
        ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
       ('lookupclass',
        ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_int)),
       ('newgobj',
        ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
       ]

class PyGObjectCPAI(object):
   def __init__(self):
       addr = ctypes.pythonapi.PyCObject_AsVoidPtr(
           ctypes.py_object(gobject._PyGObject_API))
       self._api = _PyGObject_Functions.from_address(addr)

   def pygobject_new(self, addr):
       return self._api.newgobj(addr)

# Function to make a ClutterX11TexturePixmap
def make_tpm(win):
    lib = ctypes.cdll.LoadLibrary(None)

    ptr = lib.clutter_glx_texture_pixmap_new_with_window(win)
    capi = PyGObjectCPAI()
    return capi.pygobject_new(ptr)

if len(sys.argv) < 2:
    print("Usage: " + sys.argv[0] + ": <winid>")
    exit(1)
win = int(sys.argv[1])

stage = clutter.Stage()

tpm = make_tpm(win)
# We can set properties using the regular pygobject API
tpm.set_property("automatic-updates", True)

stage.add(tpm)

# Python knows the object is an actor so we can call actor methods on it
tpm.set_x(80)
tpm.set_y(120)
print(tpm.get_gid())

stage.show()

clutter.main()

Reply via email to