Skip to content

Instantly share code, notes, and snippets.

@niw
Last active December 15, 2022 14:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niw/b120b460e854ec10cefeeb60784f5dbb to your computer and use it in GitHub Desktop.
Save niw/b120b460e854ec10cefeeb60784f5dbb to your computer and use it in GitHub Desktop.
A small sample code to trigger a haptic feedback on recent MacBook trackpad.
//
// MTActuatorTest.m
// MTActuatorTest
//
// Created by Yoshimasa Niwa on 12/2/17.
// Copyright © 2017 Yoshimasa Niwa. All rights reserved.
//
/*
MTActuatorTest
==============
This is a small sample code to trigger a haptic feedback on recent MacBook trackpad.
To build binary, use next command.
$ clang -g -fmodules -fobjc-arc -F "$(xcrun --show-sdk-platform-path)/Developer/SDKs/MacOSX.sdk/System/Library/PrivateFrameworks" -framework MultitouchSupport MTActuatorTest.m
License
-------
Copyright (c) 2017 Yoshimasa Niwa
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
@import Foundation;
@import IOKit;
CF_EXPORT CFTypeRef MTActuatorCreateFromDeviceID(UInt64 deviceID);
CF_EXPORT IOReturn MTActuatorOpen(CFTypeRef actuatorRef);
CF_EXPORT IOReturn MTActuatorClose(CFTypeRef actuatorRef);
// NOTE: There are unknown arguments.
// unknown1, unknown2, and unknown 3 are used to calcutate waveform.
// unknown1 looks like a 32bit bit fields and passed to 4th arguments of MTActuationCalculateWaveform().
// Give 0 or 0.0 for these arguments should be okay.
CF_EXPORT IOReturn MTActuatorActuate(CFTypeRef actuatorRef, SInt32 actuationID, UInt32 unknown1, Float32 unknown2, Float32 unknown3);
CF_EXPORT bool MTActuatorIsOpen(CFTypeRef actuatorRef);
static void release(CFTypeRef *typeRefRef) {
CFTypeRef typeRef = *typeRefRef;
if (typeRef) {
CFRelease(typeRef);
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// By using IORegistoryExploere, which is in Additional Tools for Xcode,
// Find `AppleMultitouchDevice` which has `Multitouch ID`.
// Probably this is a fixed value.
CFTypeRef actuatorRef __attribute__((cleanup(release))) = MTActuatorCreateFromDeviceID(0x200000001000000);
if (!actuatorRef) {
NSLog(@"MTActuatorCreateFromDeviceID failed.");
return 1;
}
IOReturn error;
error = MTActuatorOpen(actuatorRef);
if (error != kIOReturnSuccess) {
NSLog(@"MTActuatorOpen failed: 0x%04x", error);
return 1;
}
// To find predefiend actuation ID, run next command.
// $ otool -s __TEXT __tpad_act_plist /System/Library/PrivateFrameworks/MultitouchSupport.framework/Versions/Current/MultitouchSupport|tail -n +3|awk -F'\t' '{print $2}'|xxd -r -p
// This show a embeded property list file in MultitouchSupport framework.
// There are default 1, 2, 3, 4, 5, 6, 15, and 16 actuation IDs.
error = MTActuatorActuate(actuatorRef, 6, 0, 0.0, 0.0);
if (error != kIOReturnSuccess) {
NSLog(@"MTActuatorActuate failed: 0x%04x", error);
return 1;
}
error = MTActuatorClose(actuatorRef);
if (error != kIOReturnSuccess) {
NSLog(@"MTActuatorClose failed: 0x%04x", error);
return 1;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment