internal/uidriver/mobile: make Vibrate async

This commit is contained in:
Hajime Hoshi 2022-01-26 22:23:12 +09:00
parent 8f1f9e3a5f
commit eabd82cda0
2 changed files with 20 additions and 13 deletions

View File

@ -105,9 +105,11 @@ static void vibrateOneShot(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx,
import "C"
func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) {
_ = app.RunOnJVM(func(vm, env, ctx uintptr) error {
// TODO: This might be crash when this is called from init(). How can we detect this?
C.vibrateOneShot(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx), C.int64_t(duration/time.Millisecond), C.double(magnitude))
return nil
})
go func() {
_ = app.RunOnJVM(func(vm, env, ctx uintptr) error {
// TODO: This might be crash when this is called from init(). How can we detect this?
C.vibrateOneShot(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx), C.int64_t(duration/time.Millisecond), C.double(magnitude))
return nil
})
}()
}

View File

@ -20,6 +20,7 @@ package mobile
// #cgo LDFLAGS: -framework CoreHaptics
//
// #import <CoreHaptics/CoreHaptics.h>
// #include <dispatch/dispatch.h>
//
// static id initializeHapticEngine(void) {
// if (@available(iOS 13.0, *)) {
@ -30,11 +31,13 @@ package mobile
// NSError* error = nil;
// CHHapticEngine* engine = [[CHHapticEngine alloc] initAndReturnError:&error];
// if (error) {
// NSLog(@"CHHapticEngine::initAndReturnError failed: %@", error);
// return nil;
// }
//
// [engine startAndReturnError:&error];
// if (error) {
// NSLog(@"CHHapticEngine::startAndReturnError failed %@", error);
// return nil;
// }
// return engine;
@ -42,7 +45,7 @@ package mobile
// return nil;
// }
//
// static void vibrate(double duration, double intensity) {
// static void vibrateOnMainThread(double duration, double intensity) {
// if (@available(iOS 13.0, *)) {
// static BOOL initializeHapticEngineCalled = NO;
// static CHHapticEngine* engine = nil;
@ -93,18 +96,20 @@ package mobile
// }
// }
// }
//
// static void vibrate(double duration, double intensity) {
// dispatch_async(dispatch_get_main_queue(), ^{
// vibrateOnMainThread(duration, intensity);
// });
// }
import "C"
import (
"sync"
"time"
)
var vibrationM sync.Mutex
func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) {
vibrationM.Lock()
defer vibrationM.Unlock()
C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude))
go func() {
C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude))
}()
}