Skip to content

Commit 435439e

Browse files
committed
Added Haptic feddback on click all buttons
1 parent bb0c81e commit 435439e

4 files changed

Lines changed: 85 additions & 8 deletions

File tree

MTMR.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
B082B25A205C7D8000BC04DC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B082B258205C7D8000BC04DC /* Main.storyboard */; };
1919
B082B266205C7D8000BC04DC /* MTMRTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B082B265205C7D8000BC04DC /* MTMRTests.swift */; };
2020
B082B271205C7D8000BC04DC /* MTMRUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B082B270205C7D8000BC04DC /* MTMRUITests.swift */; };
21+
B09EB1E4207C082000D5C1E0 /* HapticFeedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = B09EB1E3207C082000D5C1E0 /* HapticFeedback.swift */; };
22+
B09EB1E6207C0F8E00D5C1E0 /* MultitouchSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B09EB1E5207C0F8E00D5C1E0 /* MultitouchSupport.framework */; };
2123
B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */; };
2224
B0A8BF9E207B84160086F74D /* weather.scpt in Resources */ = {isa = PBXBuildFile; fileRef = B0A8BF9D207B84160086F74D /* weather.scpt */; };
2325
B0C1CFCA205C97D30021C862 /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0C1CFC9205C97D30021C862 /* WindowController.swift */; };
@@ -64,6 +66,8 @@
6466
B082B26C205C7D8000BC04DC /* MTMRUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MTMRUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
6567
B082B270205C7D8000BC04DC /* MTMRUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MTMRUITests.swift; sourceTree = "<group>"; };
6668
B082B272205C7D8000BC04DC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
69+
B09EB1E3207C082000D5C1E0 /* HapticFeedback.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HapticFeedback.swift; sourceTree = "<group>"; };
70+
B09EB1E5207C0F8E00D5C1E0 /* MultitouchSupport.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MultitouchSupport.framework; path = ../../../../../System/Library/PrivateFrameworks/MultitouchSupport.framework; sourceTree = "<group>"; };
6771
B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyPress.swift; sourceTree = "<group>"; };
6872
B0A8BF9D207B84160086F74D /* weather.scpt */ = {isa = PBXFileReference; lastKnownFileType = text; path = weather.scpt; sourceTree = "<group>"; };
6973
B0C1CFC9205C97D30021C862 /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = "<group>"; };
@@ -78,6 +82,7 @@
7882
buildActionMask = 2147483647;
7983
files = (
8084
B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */,
85+
B09EB1E6207C0F8E00D5C1E0 /* MultitouchSupport.framework in Frameworks */,
8186
);
8287
runOnlyForDeploymentPostprocessing = 0;
8388
};
@@ -101,6 +106,7 @@
101106
B059D62B205F11E8006E6B86 /* Frameworks */ = {
102107
isa = PBXGroup;
103108
children = (
109+
B09EB1E5207C0F8E00D5C1E0 /* MultitouchSupport.framework */,
104110
B059D62C205F11E8006E6B86 /* DFRFoundation.framework */,
105111
);
106112
name = Frameworks;
@@ -149,6 +155,7 @@
149155
B0F8771B207AC92700D6E430 /* TouchBarSupport.h */,
150156
B0F8771C207AD35400D6E430 /* battery.scpt */,
151157
B0A8BF9D207B84160086F74D /* weather.scpt */,
158+
B09EB1E3207C082000D5C1E0 /* HapticFeedback.swift */,
152159
);
153160
path = MTMR;
154161
sourceTree = "<group>";
@@ -318,6 +325,7 @@
318325
B0F8771A207AC1EA00D6E430 /* TouchBarSupport.m in Sources */,
319326
B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */,
320327
B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */,
328+
B09EB1E4207C082000D5C1E0 /* HapticFeedback.swift in Sources */,
321329
B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */,
322330
36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */,
323331
);

MTMR/HapticFeedback.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// HapticFeedback.swift
3+
// MTMR
4+
//
5+
// Created by Anton Palgunov on 09/04/2018.
6+
// Copyright © 2018 Anton Palgunov. All rights reserved.
7+
//
8+
9+
import IOKit
10+
11+
class HapticFeedback {
12+
private var actuatorRef: CFTypeRef?
13+
private var deviceID: UInt64 = 0x200000001000000
14+
private var error: IOReturn = 0
15+
16+
// Don't know how to do strong is enum one of
17+
// 1 like backClick
18+
// 2 (like Click)
19+
// 3 (week)
20+
// 4 (medium)
21+
// 5 (week medium)
22+
// 6 (strong)
23+
// 15 (nothing)
24+
// 16 (nothing)
25+
26+
func tap(strong:Int32) -> Void {
27+
let actuatorRef = MTActuatorCreateFromDeviceID(deviceID).takeRetainedValue()
28+
29+
// TODO: Need to fix warning
30+
guard actuatorRef != nil else {
31+
print("guard actuatorRef == nil")
32+
return
33+
}
34+
35+
error = MTActuatorOpen(actuatorRef)
36+
guard error == kIOReturnSuccess else {
37+
print("guard MTActuatorOpen")
38+
return
39+
}
40+
41+
error = MTActuatorActuate(actuatorRef, strong, 0, 0.0, 0.0)
42+
guard error == kIOReturnSuccess else {
43+
print("guard MTActuatorActuate")
44+
return
45+
}
46+
47+
error = MTActuatorClose(actuatorRef)
48+
guard error == kIOReturnSuccess else {
49+
print("guard MTActuatorClose")
50+
return
51+
}
52+
53+
return
54+
}
55+
}
56+
57+

MTMR/TouchBarItems.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,47 @@ import Cocoa
1111
extension NSTouchBarItem.Identifier {
1212
static let escButton = NSTouchBarItem.Identifier("com.toxblh.mtmr.escButton")
1313
static let dismissButton = NSTouchBarItem.Identifier("com.toxblh.mtmr.dismissButton")
14-
14+
1515
// Volume
1616
static let volumeUp = NSTouchBarItem.Identifier("com.toxblh.mtmr.volumeUp")
1717
static let volumeDown = NSTouchBarItem.Identifier("com.toxblh.mtmr.volumeDown")
18-
18+
1919
// Brightness
2020
static let brightUp = NSTouchBarItem.Identifier("com.toxblh.mtmr.brightUp")
2121
static let brightDown = NSTouchBarItem.Identifier("com.toxblh.mtmr.brightDown")
22-
22+
2323
// Music
2424
static let prev = NSTouchBarItem.Identifier("com.toxblh.mtmr.prev")
2525
static let next = NSTouchBarItem.Identifier("com.toxblh.mtmr.next")
2626
static let play = NSTouchBarItem.Identifier("com.toxblh.mtmr.play")
27-
27+
2828
// Plugins
2929
static let sleep = NSTouchBarItem.Identifier("com.toxblh.mtmr.sleep")
3030
static let weather = NSTouchBarItem.Identifier("com.toxblh.mtmr.weather")
3131
static let time = NSTouchBarItem.Identifier("com.toxblh.mtmr.time")
3232
static let battery = NSTouchBarItem.Identifier("com.toxblh.mtmr.battery")
3333
static let nowPlaying = NSTouchBarItem.Identifier("com.toxblh.mtmr.nowPlaying")
34-
34+
3535
static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip")
3636
}
3737

3838
class CustomButtonTouchBarItem: NSCustomTouchBarItem {
3939
let tapClosure: (NSCustomTouchBarItem) -> ()
40-
40+
4141
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping (NSCustomTouchBarItem) -> ()) {
4242
self.tapClosure = callback
4343
super.init(identifier: identifier)
4444
self.view = NSButton(title: title, target: self, action: #selector(didTapped))
4545
}
46-
46+
4747
required init?(coder: NSCoder) {
4848
fatalError("init(coder:) has not been implemented")
4949
}
50-
50+
5151
@objc func didTapped() {
5252
self.tapClosure(self)
53+
let hf: HapticFeedback = HapticFeedback()
54+
hf.tap(strong: 6)
5355
}
5456
}
5557

MTMR/TouchBarPrivateApi-Bridging.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@
88

99
#import "TouchBarPrivateApi.h"
1010
#import "TouchBarSupport.h"
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
CF_EXPORT CFTypeRef MTActuatorCreateFromDeviceID(UInt64 deviceID);
15+
CF_EXPORT IOReturn MTActuatorOpen(CFTypeRef actuatorRef);
16+
CF_EXPORT IOReturn MTActuatorClose(CFTypeRef actuatorRef);
17+
CF_EXPORT IOReturn MTActuatorActuate(CFTypeRef actuatorRef, SInt32 actuationID, UInt32 arg1, Float32 arg2, Float32 arg3);
18+
CF_EXPORT bool MTActuatorIsOpen(CFTypeRef actuatorRef);
19+
20+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)