Skip to content

Commit 95271db

Browse files
authored
HapticFeedback: fix broken haptic for Release builds + refactori… (#198)
HapticFeedback: fix broken haptic for Release builds + refactoring
2 parents 77846d0 + 67aaa2a commit 95271db

4 files changed

Lines changed: 38 additions & 33 deletions

File tree

MTMR/CustomButtonTouchBarItem.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
1212
var tapClosure: (() -> Void)?
1313
var longTapClosure: (() -> Void)?
1414

15-
private let hf: HapticFeedback = HapticFeedback()
1615
private var button: NSButton!
1716
private var singleClick: HapticClickGestureRecognizer!
1817
private var longClick: NSPressGestureRecognizer!
@@ -118,10 +117,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
118117
switch gr.state {
119118
case .began:
120119
if let closure = self.longTapClosure {
121-
hf.tap(strong: 2)
120+
HapticFeedback.shared.tap(strong: 2)
122121
closure()
123122
} else if let closure = self.tapClosure {
124-
hf.tap(strong: 6)
123+
HapticFeedback.shared.tap(strong: 6)
125124
closure()
126125
print("long click")
127126
}
@@ -171,15 +170,13 @@ class CustomButtonCell: NSButtonCell {
171170
}
172171

173172
class HapticClickGestureRecognizer: NSClickGestureRecognizer {
174-
let hf: HapticFeedback = HapticFeedback()
175-
176173
override func touchesBegan(with event: NSEvent) {
177-
hf.tap(strong: 2)
174+
HapticFeedback.shared.tap(strong: 2)
178175
super.touchesBegan(with: event)
179176
}
180177

181178
override func touchesEnded(with event: NSEvent) {
182-
hf.tap(strong: 1)
179+
HapticFeedback.shared.tap(strong: 1)
183180
super.touchesEnded(with: event)
184181
}
185182
}

MTMR/HapticFeedback.swift

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import IOKit
1010

1111
class HapticFeedback {
12-
private var correctDeviceId: UInt64?
12+
static let shared = HapticFeedback()
1313

1414
// Here we have list of possible IDs for Haptic Generator Device. They are not constant
1515
// To find deviceID, you will need IORegistryExplorer app from Additional Tools for Xcode dmg
@@ -20,16 +20,11 @@ class HapticFeedback {
2020
0x200_0000_0100_0000, // MacBook Pro 2016/2017
2121
0x300000080500000 // MacBook Pro 2019 (possibly 2018 as well)
2222
]
23+
private var correctDeviceID: UInt64?
24+
private var actuatorRef: CFTypeRef?
2325

2426
init() {
25-
// Let's find our Haptic device
26-
possibleDeviceIDs.forEach {(deviceID) in
27-
guard correctDeviceId == nil else {return}
28-
let actuatorRef: CFTypeRef? = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue()
29-
if actuatorRef != nil {
30-
correctDeviceId = deviceID
31-
}
32-
}
27+
recreateDevice()
3328
}
3429

3530
// Don't know how to do strong is enum one of
@@ -44,15 +39,8 @@ class HapticFeedback {
4439
// 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`
4540

4641
func tap(strong: Int32) {
47-
guard correctDeviceId != nil else {
48-
print("guard correctDeviceId == nil (no haptic device found?)")
49-
return
50-
}
51-
52-
let actuatorRef: CFTypeRef? = MTActuatorCreateFromDeviceID(correctDeviceId!).takeRetainedValue()
53-
54-
guard actuatorRef != nil else {
55-
print("guard actuatorRef == nil")
42+
guard correctDeviceID != nil, actuatorRef != nil else {
43+
print("guard actuatorRef == nil (no haptic device found?)")
5644
return
5745
}
5846

@@ -61,10 +49,11 @@ class HapticFeedback {
6149
result = MTActuatorOpen(actuatorRef!)
6250
guard result == kIOReturnSuccess else {
6351
print("guard MTActuatorOpen")
52+
recreateDevice()
6453
return
6554
}
6655

67-
result = MTActuatorActuate(actuatorRef!, strong, 0, 0.0, 0.0)
56+
result = MTActuatorActuate(actuatorRef!, strong, 0, 0, 0)
6857
guard result == kIOReturnSuccess else {
6958
print("guard MTActuatorActuate")
7059
return
@@ -76,4 +65,25 @@ class HapticFeedback {
7665
return
7766
}
7867
}
68+
69+
private func recreateDevice() {
70+
if let actuatorRef = actuatorRef {
71+
MTActuatorClose(actuatorRef)
72+
self.actuatorRef = nil // just in case %)
73+
}
74+
75+
if let correctDeviceID = correctDeviceID {
76+
actuatorRef = MTActuatorCreateFromDeviceID(correctDeviceID).takeRetainedValue()
77+
} else {
78+
// Let's find our Haptic device
79+
possibleDeviceIDs.forEach {(deviceID) in
80+
guard correctDeviceID == nil else {return}
81+
actuatorRef = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue()
82+
83+
if actuatorRef != nil {
84+
correctDeviceID = deviceID
85+
}
86+
}
87+
}
88+
}
7989
}

MTMR/Widgets/AppScrubberTouchBarItem.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import Cocoa
1010
class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrubberDataSource {
1111
private var scrubber: NSScrubber!
1212

13-
private let hf: HapticFeedback = HapticFeedback()
14-
1513
private var timer: Timer!
1614
private var ticks: Int = 0
1715
private let minTicks: Int = 5
@@ -148,12 +146,12 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
148146
ticks += 1
149147

150148
if ticks == minTicks {
151-
hf.tap(strong: 2)
149+
HapticFeedback.shared.tap(strong: 2)
152150
}
153151

154152
if ticks > maxTicks {
155153
stopTimer()
156-
hf.tap(strong: 6)
154+
HapticFeedback.shared.tap(strong: 6)
157155
}
158156
}
159157

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

@@ -203,7 +201,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
203201
}
204202
}
205203
} else {
206-
hf.tap(strong: 6)
204+
HapticFeedback.shared.tap(strong: 6)
207205
if let index = self.persistentAppIdentifiers.index(of: bundleIdentifier!) {
208206
persistentAppIdentifiers.remove(at: index)
209207
} else {

MTMR/Widgets/CurrencyBarItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
123123
}
124124

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

0 commit comments

Comments
 (0)