Skip to content

Instantly share code, notes, and snippets.

@mkottman
Created December 22, 2011 21:19

Revisions

  1. mkottman created this gist Dec 22, 2011.
    50 changes: 50 additions & 0 deletions multitouch.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    -- Apple Multitouch MacBook trackpad in Lua using LuaJIT FFI, 2011 Michal Kottman
    -- definitions from http://www.steike.com/code/multitouch/

    local ffi = require 'ffi'

    ffi.cdef [[
    typedef struct { float x,y; } mtPoint;
    typedef struct { mtPoint pos,vel; } mtReadout;

    typedef struct {
    int frame;
    double timestamp;
    int identifier, state, foo3, foo4;
    mtReadout normalized;
    float size;
    int zero1;
    float angle, majorAxis, minorAxis; // ellipsoid
    mtReadout mm;
    int zero2[2];
    float unk2;
    } Finger;

    typedef void *MTDeviceRef;
    typedef int (*MTContactCallbackFunction)(int,Finger*,int,double,int);

    MTDeviceRef MTDeviceCreateDefault();
    void MTRegisterContactFrameCallback(MTDeviceRef, MTContactCallbackFunction);
    void MTDeviceStart(MTDeviceRef, int);

    typedef int (*MTCallback)(int device, Finger *data, int nFingers, double timestamp, int frame);

    unsigned int sleep(unsigned int seconds);
    ]]

    local mt = ffi.load('/System/Library/PrivateFrameworks/MultitouchSupport.framework/Versions/Current/MultitouchSupport')

    local callback = ffi.cast('MTCallback', function(device, data, nFingers, timestamp, frame)
    print('=== '..frame..', '..nFingers..' fingers ===')
    for i=0,nFingers-1 do
    local finger = data[i]
    local pos = finger.normalized.pos
    print(i, finger.identifier, pos.x, pos.y, finger.size)
    end
    return 0
    end)

    local dev = mt.MTDeviceCreateDefault()
    mt.MTRegisterContactFrameCallback(dev, callback)
    mt.MTDeviceStart(dev, 0);
    ffi.C.sleep(0xFFFFFFFF)