Skip to content

Commit

Permalink
HapticFeedback: fix broken haptic for Release builds + refactori… (#198)
Browse files Browse the repository at this point in the history
HapticFeedback: fix broken haptic for Release builds + refactoring
  • Loading branch information
Toxblh committed Jul 27, 2019
2 parents 77846d0 + 67aaa2a commit 95271db
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
11 changes: 4 additions & 7 deletions MTMR/CustomButtonTouchBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
var tapClosure: (() -> Void)?
var longTapClosure: (() -> Void)?

private let hf: HapticFeedback = HapticFeedback()
private var button: NSButton!
private var singleClick: HapticClickGestureRecognizer!
private var longClick: NSPressGestureRecognizer!
Expand Down Expand Up @@ -118,10 +117,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
switch gr.state {
case .began:
if let closure = self.longTapClosure {
hf.tap(strong: 2)
HapticFeedback.shared.tap(strong: 2)
closure()
} else if let closure = self.tapClosure {
hf.tap(strong: 6)
HapticFeedback.shared.tap(strong: 6)
closure()
print("long click")
}
Expand Down Expand Up @@ -171,15 +170,13 @@ class CustomButtonCell: NSButtonCell {
}

class HapticClickGestureRecognizer: NSClickGestureRecognizer {
let hf: HapticFeedback = HapticFeedback()

override func touchesBegan(with event: NSEvent) {
hf.tap(strong: 2)
HapticFeedback.shared.tap(strong: 2)
super.touchesBegan(with: event)
}

override func touchesEnded(with event: NSEvent) {
hf.tap(strong: 1)
HapticFeedback.shared.tap(strong: 1)
super.touchesEnded(with: event)
}
}
Expand Down
48 changes: 29 additions & 19 deletions MTMR/HapticFeedback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import IOKit

class HapticFeedback {
private var correctDeviceId: UInt64?
static let shared = HapticFeedback()

// 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
Expand All @@ -20,16 +20,11 @@ class HapticFeedback {
0x200_0000_0100_0000, // MacBook Pro 2016/2017
0x300000080500000 // MacBook Pro 2019 (possibly 2018 as well)
]
private var correctDeviceID: UInt64?
private var actuatorRef: CFTypeRef?

init() {
// Let's find our Haptic device
possibleDeviceIDs.forEach {(deviceID) in
guard correctDeviceId == nil else {return}
let actuatorRef: CFTypeRef? = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue()
if actuatorRef != nil {
correctDeviceId = deviceID
}
}
recreateDevice()
}

// Don't know how to do strong is enum one of
Expand All @@ -44,15 +39,8 @@ 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")
guard correctDeviceID != nil, actuatorRef != nil else {
print("guard actuatorRef == nil (no haptic device found?)")
return
}

Expand All @@ -61,10 +49,11 @@ class HapticFeedback {
result = MTActuatorOpen(actuatorRef!)
guard result == kIOReturnSuccess else {
print("guard MTActuatorOpen")
recreateDevice()
return
}

result = MTActuatorActuate(actuatorRef!, strong, 0, 0.0, 0.0)
result = MTActuatorActuate(actuatorRef!, strong, 0, 0, 0)
guard result == kIOReturnSuccess else {
print("guard MTActuatorActuate")
return
Expand All @@ -76,4 +65,25 @@ class HapticFeedback {
return
}
}

private func recreateDevice() {
if let actuatorRef = actuatorRef {
MTActuatorClose(actuatorRef)
self.actuatorRef = nil // just in case %)
}

if let correctDeviceID = correctDeviceID {
actuatorRef = MTActuatorCreateFromDeviceID(correctDeviceID).takeRetainedValue()
} else {
// Let's find our Haptic device
possibleDeviceIDs.forEach {(deviceID) in
guard correctDeviceID == nil else {return}
actuatorRef = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue()

if actuatorRef != nil {
correctDeviceID = deviceID
}
}
}
}
}
10 changes: 4 additions & 6 deletions MTMR/Widgets/AppScrubberTouchBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import Cocoa
class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrubberDataSource {
private var scrubber: NSScrubber!

private let hf: HapticFeedback = HapticFeedback()

private var timer: Timer!
private var ticks: Int = 0
private let minTicks: Int = 5
Expand Down Expand Up @@ -148,12 +146,12 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
ticks += 1

if ticks == minTicks {
hf.tap(strong: 2)
HapticFeedback.shared.tap(strong: 2)
}

if ticks > maxTicks {
stopTimer()
hf.tap(strong: 6)
HapticFeedback.shared.tap(strong: 6)
}
}

Expand Down Expand Up @@ -184,7 +182,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
NSWorkspace.shared.openFile(bundleIdentifier!.replacingOccurrences(of: "file://", with: ""))
} else {
NSWorkspace.shared.launchApplication(withBundleIdentifier: bundleIdentifier!, options: [.default], additionalEventParamDescriptor: nil, launchIdentifier: nil)
hf.tap(strong: 6)
HapticFeedback.shared.tap(strong: 6)
}
updateRunningApplication()

Expand All @@ -203,7 +201,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
}
}
} else {
hf.tap(strong: 6)
HapticFeedback.shared.tap(strong: 6)
if let index = self.persistentAppIdentifiers.index(of: bundleIdentifier!) {
persistentAppIdentifiers.remove(at: index)
} else {
Expand Down
2 changes: 1 addition & 1 deletion MTMR/Widgets/CurrencyBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
}

let regularFont = attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15)
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont])
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont, .baselineOffset: 1])
newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
attributedTitle = newTitle
}
Expand Down

0 comments on commit 95271db

Please sign in to comment.