@@ -13,7 +13,12 @@ func MTActuatorOpen(_ actuator: OpaquePointer) -> Int32 |
13 | 13 | func MTActuatorClose(_ actuator: OpaquePointer) -> Int32 |
14 | 14 |
|
15 | 15 | @_silgen_name("MTActuatorActuate") |
16 | | -func MTActuatorActuate(_ actuator: OpaquePointer, _ actuationID: Int32, _ unknown1: UInt32, _ unknown2: Float, _ intensity: Float) -> Int32 |
| 16 | +func MTActuatorActuate(_ actuator: OpaquePointer, _ actuationID: Int32, _ unknown1: UInt32, _ unknown2: Float, _ unknown3: Float) -> Int32 |
| 17 | + |
| 18 | +// MARK: - Persistent Actuator State |
| 19 | + |
| 20 | +var persistentActuator: OpaquePointer? = nil |
| 21 | +var persistentDeviceID: UInt64 = 0 |
17 | 22 |
|
18 | 23 | // MARK: - IOKit Device Discovery |
19 | 24 |
|
@@ -49,6 +54,18 @@ func findTrackpadDeviceID() -> UInt64? { |
49 | 54 | continue |
50 | 55 | } |
51 | 56 |
|
| 57 | + var isBuiltIn = false |
| 58 | + if let builtInRef = IORegistryEntryCreateCFProperty( |
| 59 | + service, |
| 60 | + "MT Built-In" as CFString, |
| 61 | + kCFAllocatorDefault, |
| 62 | + 0 |
| 63 | + )?.takeRetainedValue() { |
| 64 | + if CFGetTypeID(builtInRef) == CFBooleanGetTypeID() { |
| 65 | + isBuiltIn = CFBooleanGetValue((builtInRef as! CFBoolean)) |
| 66 | + } |
| 67 | + } |
| 68 | + |
52 | 69 | guard let deviceIDRef = IORegistryEntryCreateCFProperty( |
53 | 70 | service, |
54 | 71 | "Multitouch ID" as CFString, |
@@ -65,37 +82,101 @@ func findTrackpadDeviceID() -> UInt64? { |
65 | 82 | var deviceID: Int64 = 0 |
66 | 83 | CFNumberGetValue((deviceIDRef as! CFNumber), .sInt64Type, &deviceID) |
67 | 84 |
|
68 | | - return UInt64(bitPattern: deviceID) |
| 85 | + let id = UInt64(bitPattern: deviceID) |
| 86 | + |
| 87 | + if isBuiltIn { |
| 88 | + return id |
| 89 | + } |
| 90 | + |
| 91 | + if persistentDeviceID == 0 { |
| 92 | + persistentDeviceID = id |
| 93 | + } |
69 | 94 | } |
70 | 95 |
|
71 | | - return nil |
| 96 | + return persistentDeviceID != 0 ? persistentDeviceID : nil |
72 | 97 | } |
73 | 98 |
|
74 | 99 | // MARK: - Haptic Feedback |
75 | 100 |
|
76 | | -/// Actuation IDs: 1=very weak, 3=weak, 4=medium, 5=medium-strong, 6=strong, 15=very strong |
77 | | -let ACTUATION_STRONG: Int32 = 15 |
78 | | -let ACTUATION_WEAK: Int32 = 6 |
| 101 | +let ACTUATION_STRONG: Int32 = 6 |
| 102 | +let ACTUATION_MEDIUM: Int32 = 4 |
| 103 | +let ACTUATION_WEAK: Int32 = 3 |
79 | 104 |
|
80 | | -func triggerHaptic(actuationID: Int32, intensity: Float) -> Bool { |
| 105 | +func openActuator() -> Bool { |
| 106 | + if persistentActuator != nil { |
| 107 | + return true |
| 108 | + } |
| 109 | + |
81 | 110 | guard let deviceID = findTrackpadDeviceID() else { |
| 111 | + if ProcessInfo.processInfo.environment["DEBUG"] != nil { |
| 112 | + fputs("DEBUG: No trackpad found\n", stderr) |
| 113 | + } |
82 | 114 | return false |
83 | 115 | } |
84 | 116 |
|
| 117 | + persistentDeviceID = deviceID |
| 118 | + |
85 | 119 | guard let actuator = MTActuatorCreateFromDeviceID(deviceID) else { |
| 120 | + if ProcessInfo.processInfo.environment["DEBUG"] != nil { |
| 121 | + fputs("DEBUG: Failed to create actuator for device \(deviceID)\n", stderr) |
| 122 | + } |
86 | 123 | return false |
87 | 124 | } |
88 | 125 |
|
89 | 126 | let openResult = MTActuatorOpen(actuator) |
90 | 127 | if openResult != 0 { |
| 128 | + if ProcessInfo.processInfo.environment["DEBUG"] != nil { |
| 129 | + fputs("DEBUG: MTActuatorOpen failed with \(openResult)\n", stderr) |
| 130 | + } |
| 131 | + return false |
| 132 | + } |
| 133 | + |
| 134 | + persistentActuator = actuator |
| 135 | + |
| 136 | + if ProcessInfo.processInfo.environment["DEBUG"] != nil { |
| 137 | + fputs("DEBUG: Actuator opened for device \(deviceID)\n", stderr) |
| 138 | + } |
| 139 | + |
| 140 | + return true |
| 141 | +} |
| 142 | + |
| 143 | +func closeActuator() { |
| 144 | + if let actuator = persistentActuator { |
91 | 145 | _ = MTActuatorClose(actuator) |
| 146 | + persistentActuator = nil |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func triggerHaptic(actuationID: Int32, intensity: Float) -> Bool { |
| 151 | + if !openActuator() { |
92 | 152 | return false |
93 | 153 | } |
94 | 154 |
|
95 | | - let clampedActuationID = max(0, min(15, actuationID)) |
| 155 | + guard let actuator = persistentActuator else { |
| 156 | + return false |
| 157 | + } |
| 158 | + |
| 159 | + let clampedActuationID = max(1, min(16, actuationID)) |
96 | 160 | let clampedIntensity = max(0.0, min(2.0, intensity)) |
97 | | - let actuateResult = MTActuatorActuate(actuator, clampedActuationID, 0, 0.0, clampedIntensity) |
98 | | - _ = MTActuatorClose(actuator) |
| 161 | + |
| 162 | + var actuateResult = MTActuatorActuate(actuator, clampedActuationID, 0, 0.0, clampedIntensity) |
| 163 | + |
| 164 | + if ProcessInfo.processInfo.environment["DEBUG"] != nil { |
| 165 | + fputs("DEBUG: MTActuatorActuate(id=\(clampedActuationID), intensity=\(clampedIntensity)) = \(actuateResult)\n", stderr) |
| 166 | + } |
| 167 | + |
| 168 | + if actuateResult != 0 { |
| 169 | + if ProcessInfo.processInfo.environment["DEBUG"] != nil { |
| 170 | + fputs("DEBUG: Retrying after close/reopen\n", stderr) |
| 171 | + } |
| 172 | + closeActuator() |
| 173 | + if openActuator(), let retryActuator = persistentActuator { |
| 174 | + actuateResult = MTActuatorActuate(retryActuator, clampedActuationID, 0, 0.0, clampedIntensity) |
| 175 | + if ProcessInfo.processInfo.environment["DEBUG"] != nil { |
| 176 | + fputs("DEBUG: Retry result = \(actuateResult)\n", stderr) |
| 177 | + } |
| 178 | + } |
| 179 | + } |
99 | 180 |
|
100 | 181 | return actuateResult == 0 |
101 | 182 | } |
|