2021-10-23 22:02:12 +02:00
|
|
|
// Copyright 2021 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-02-08 06:39:21 +01:00
|
|
|
package vibrate
|
2021-10-23 22:02:12 +02:00
|
|
|
|
2022-02-04 15:29:09 +01:00
|
|
|
// #cgo CFLAGS: -x objective-c
|
|
|
|
// #cgo LDFLAGS: -framework AVFoundation -framework CoreHaptics -framework Foundation
|
2021-10-23 22:02:12 +02:00
|
|
|
//
|
2022-01-26 16:27:52 +01:00
|
|
|
// #import <AVFoundation/AVFoundation.h>
|
2021-10-23 22:02:12 +02:00
|
|
|
// #import <CoreHaptics/CoreHaptics.h>
|
2022-01-26 14:23:12 +01:00
|
|
|
// #include <dispatch/dispatch.h>
|
2021-10-23 22:02:12 +02:00
|
|
|
//
|
2021-11-10 18:28:02 +01:00
|
|
|
// static id initializeHapticEngine(void) {
|
|
|
|
// if (@available(iOS 13.0, *)) {
|
|
|
|
// if (!CHHapticEngine.capabilitiesForHardware.supportsHaptics) {
|
|
|
|
// return nil;
|
|
|
|
// }
|
2021-10-23 22:02:12 +02:00
|
|
|
//
|
2021-11-10 18:28:02 +01:00
|
|
|
// NSError* error = nil;
|
2022-01-26 15:52:41 +01:00
|
|
|
// // Specify the AVAudioSession's shared instance so that this won't affect
|
|
|
|
// // the result of `[[AVAudioSession sharedInstance] secondaryAudioShouldBeSilencedHint]` (#1976).
|
|
|
|
// CHHapticEngine* engine =
|
|
|
|
// [[CHHapticEngine alloc] initWithAudioSession:[AVAudioSession sharedInstance]
|
|
|
|
// error:&error];
|
2021-11-10 18:28:02 +01:00
|
|
|
// if (error) {
|
2022-01-26 14:23:12 +01:00
|
|
|
// NSLog(@"CHHapticEngine::initAndReturnError failed: %@", error);
|
2021-11-10 18:28:02 +01:00
|
|
|
// return nil;
|
|
|
|
// }
|
2021-10-23 22:02:12 +02:00
|
|
|
//
|
2021-11-10 18:28:02 +01:00
|
|
|
// [engine startAndReturnError:&error];
|
|
|
|
// if (error) {
|
2022-01-26 14:23:12 +01:00
|
|
|
// NSLog(@"CHHapticEngine::startAndReturnError failed %@", error);
|
2021-11-10 18:28:02 +01:00
|
|
|
// return nil;
|
|
|
|
// }
|
|
|
|
// return engine;
|
2021-10-23 22:02:12 +02:00
|
|
|
// }
|
2021-11-10 18:28:02 +01:00
|
|
|
// return nil;
|
2021-10-23 22:02:12 +02:00
|
|
|
// }
|
|
|
|
//
|
2022-01-26 14:23:12 +01:00
|
|
|
// static void vibrateOnMainThread(double duration, double intensity) {
|
2021-11-10 18:28:02 +01:00
|
|
|
// if (@available(iOS 13.0, *)) {
|
|
|
|
// static BOOL initializeHapticEngineCalled = NO;
|
|
|
|
// static CHHapticEngine* engine = nil;
|
|
|
|
// if (!initializeHapticEngineCalled) {
|
|
|
|
// engine = (CHHapticEngine*)initializeHapticEngine();
|
|
|
|
// initializeHapticEngineCalled = YES;
|
2021-10-23 22:02:12 +02:00
|
|
|
// }
|
2021-11-10 18:28:02 +01:00
|
|
|
// if (!engine) {
|
2021-10-23 22:02:12 +02:00
|
|
|
// return;
|
|
|
|
// }
|
2021-11-10 18:28:02 +01:00
|
|
|
// @autoreleasepool {
|
|
|
|
// NSDictionary* hapticDict = @{
|
|
|
|
// (id<NSCopying>)(CHHapticPatternKeyPattern): @[
|
|
|
|
// @{
|
|
|
|
// (id<NSCopying>)(CHHapticPatternKeyEvent): @{
|
|
|
|
// (id<NSCopying>)(CHHapticPatternKeyEventType):CHHapticEventTypeHapticContinuous,
|
|
|
|
// (id<NSCopying>)(CHHapticPatternKeyTime):@0.0,
|
|
|
|
// (id<NSCopying>)(CHHapticPatternKeyEventDuration):[NSNumber numberWithDouble:duration],
|
|
|
|
// (id<NSCopying>)(CHHapticPatternKeyEventParameters):@[
|
|
|
|
// @{
|
|
|
|
// (id<NSCopying>)(CHHapticPatternKeyParameterID): CHHapticEventParameterIDHapticIntensity,
|
2021-12-04 14:14:02 +01:00
|
|
|
// (id<NSCopying>)(CHHapticPatternKeyParameterValue): [NSNumber numberWithDouble:intensity],
|
2021-11-10 18:28:02 +01:00
|
|
|
// },
|
|
|
|
// ],
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// ],
|
|
|
|
// };
|
2021-10-23 22:02:12 +02:00
|
|
|
//
|
2021-11-10 18:28:02 +01:00
|
|
|
// NSError* error = nil;
|
|
|
|
// CHHapticPattern* pattern = [[CHHapticPattern alloc] initWithDictionary:hapticDict
|
|
|
|
// error:&error];
|
|
|
|
// if (error) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// id<CHHapticPatternPlayer> player = [engine createPlayerWithPattern:pattern
|
|
|
|
// error:&error];
|
|
|
|
// if (error) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// [player startAtTime:0 error:&error];
|
|
|
|
// if (error) {
|
|
|
|
// NSLog(@"3, %@", [error localizedDescription]);
|
|
|
|
// return;
|
|
|
|
// }
|
2021-10-23 22:02:12 +02:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2022-01-26 14:23:12 +01:00
|
|
|
//
|
|
|
|
// static void vibrate(double duration, double intensity) {
|
|
|
|
// dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
// vibrateOnMainThread(duration, intensity);
|
|
|
|
// });
|
|
|
|
// }
|
2021-10-23 22:02:12 +02:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-02-08 06:39:21 +01:00
|
|
|
func Vibrate(duration time.Duration, magnitude float64) {
|
2022-01-26 14:23:12 +01:00
|
|
|
go func() {
|
|
|
|
C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude))
|
|
|
|
}()
|
2021-10-23 22:02:12 +02:00
|
|
|
}
|