MacBook Multitouch - in Python

I's gone an' outdone myself! Since the last post got so much attention, I realized more people were interested in reading the touch input than I thought.

So, I wondered how good ctypes is at using private frameworks from Apple. Turns out, pretty darn good.

This is an implementation of a simple multitouch listener which just prints the information:

@MTContactCallbackFunction
def my_callback(device, data_ptr, n_fingers, timestamp, frame):
    print device, timestamp, frame
    for i in xrange(n_fingers):
        data = data_ptr[i]
        d = "x=%.2f, y=%.2f" % (data.normalized.position.x * 100,
                                data.normalized.position.y * 100)
        print "%d: %s" % (i, d)
    return 0

devices = MultitouchSupport.MTDeviceCreateList()
num_devices = CFArrayGetCount(devices)
print "num_devices =", num_devices
for i in xrange(num_devices):
    device = CFArrayGetValueAtIndex(devices, i)
    print "device #%d: %016x" % (i, device)
    MTRegisterContactFrameCallback(device, my_callback)
    MTDeviceStart(device, 0)

# Why sleep instead of join? Ask David Beazley.
while threading.active_count():
    time.sleep(0.125)

See this paste for the whole body, including the ctypes definitions and mappings.

I also wondered how good pygame really is, so I decided to try it out. See this paste for the pygame-based thing. It uses only the SDL bindings, no SDL_* extras. To quit it (it's fullscreen), hold one finger still and swipe four fingers quickly downwards.


Comments
Posted by: Josh

Um, ludvig, you're on reddit. How's the fanclub?

2009-11-12 @ 16:23:14
Posted by: Ludvig Ericson

Oh, nice. It's doing alright.

2009-11-12 @ 18:02:00
URL: http://lericson.blogg.se/code/
Posted by: Oscar

The pasties does not work (for me).

Maybe you can put the project up somewhere? github for instance. People (like me) love to play with stuff like this.

2009-11-17 @ 05:48:34

Comment the entry:

Name:
Remember me (cookie)

E-mail: (never published, solely for reply)

URL:

Comment:

RSS 2.0