From 7b853b5d47599e84eca790154e30ba240a379c9a Mon Sep 17 00:00:00 2001 From: bobrosoft Date: Wed, 17 Jul 2019 12:17:35 +0400 Subject: [PATCH 1/2] HapticFeedback: fix recovery from the long sleep --- MTMR/HapticFeedback.swift | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/MTMR/HapticFeedback.swift b/MTMR/HapticFeedback.swift index e9d8752..8fc3ae1 100644 --- a/MTMR/HapticFeedback.swift +++ b/MTMR/HapticFeedback.swift @@ -9,14 +9,14 @@ import IOKit class HapticFeedback { - private var actuatorRef: CFTypeRef? + private var correctDeviceId: UInt64? // Here we have list of possible IDs for Haptic Generator Device. They are not constant // To find deviceID, you will need IORegistryExplorer app from Additional Tools for Xcode dmg // which you can download from https://developer.apple.com/download/more/?=Additional%20Tools // Open IORegistryExplorer app, search for AppleMultitouchDevice and get "Multitouch ID" // There should be programmatic way to get it but I can't find, no docs for macOS :( - private var possibleDeviceIDs: [UInt64] = [ + private let possibleDeviceIDs: [UInt64] = [ 0x200_0000_0100_0000, // MacBook Pro 2016/2017 0x300000080500000 // MacBook Pro 2019 (possibly 2018 as well) ] @@ -24,8 +24,11 @@ class HapticFeedback { init() { // Let's find and init Haptic device possibleDeviceIDs.forEach {(deviceID) in - guard actuatorRef == nil else {return} - actuatorRef = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue() + guard correctDeviceId == nil else {return} + let actuatorRef: CFTypeRef? = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue() + if actuatorRef != nil { + correctDeviceId = deviceID + } } } @@ -41,8 +44,15 @@ class HapticFeedback { // you can get a plist `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` func tap(strong: Int32) { + guard correctDeviceId != nil else { + print("guard correctDeviceId == nil (no haptic device found?)") + return + } + + let actuatorRef: CFTypeRef? = MTActuatorCreateFromDeviceID(correctDeviceId!).takeRetainedValue() + guard actuatorRef != nil else { - print("guard actuatorRef == nil (can't find proper haptic device?)") + print("guard actuatorRef == nil") return } From efc52293a86767f049fa5fccbd36fcb832380a6c Mon Sep 17 00:00:00 2001 From: bobrosoft Date: Wed, 17 Jul 2019 12:21:51 +0400 Subject: [PATCH 2/2] HapticFeedback: fix recovery from the long sleep (minor comment changes) --- MTMR/HapticFeedback.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MTMR/HapticFeedback.swift b/MTMR/HapticFeedback.swift index 8fc3ae1..cd4c0c1 100644 --- a/MTMR/HapticFeedback.swift +++ b/MTMR/HapticFeedback.swift @@ -22,7 +22,7 @@ class HapticFeedback { ] init() { - // Let's find and init Haptic device + // Let's find our Haptic device possibleDeviceIDs.forEach {(deviceID) in guard correctDeviceId == nil else {return} let actuatorRef: CFTypeRef? = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue()