Skip to content

Instantly share code, notes, and snippets.

@mkottman
Created December 22, 2011 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkottman/1511905 to your computer and use it in GitHub Desktop.
Save mkottman/1511905 to your computer and use it in GitHub Desktop.
Apple Multitouch MacBook trackpad in Lua using LuaJIT FFI
-- 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment