From 17fd1aea29791eb8d1a94faa4b059293700bfc00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 23 Sep 2020 17:19:56 +0200 Subject: [PATCH] [ion] Haptics implementation are on all simulators but Ion::Haptics::isEnabled is per platform. --- ion/src/simulator/android/Makefile | 3 +- ion/src/simulator/android/src/cpp/haptics.cpp | 46 ------------------- .../android/src/cpp/haptics_enabled.cpp | 20 ++++++++ ion/src/simulator/ios/Makefile | 3 +- ion/src/simulator/linux/Makefile | 3 +- ion/src/simulator/macos/Makefile | 3 +- ion/src/simulator/shared/dummy/haptics.cpp | 19 -------- .../shared/dummy/haptics_enabled.cpp | 15 ++++++ ion/src/simulator/shared/events.cpp | 2 +- ion/src/simulator/shared/haptics.cpp | 35 ++++++++++++++ ion/src/simulator/shared/haptics.h | 3 +- ion/src/simulator/web/Makefile | 3 +- ion/src/simulator/windows/Makefile | 3 +- 13 files changed, 85 insertions(+), 73 deletions(-) delete mode 100644 ion/src/simulator/android/src/cpp/haptics.cpp create mode 100644 ion/src/simulator/android/src/cpp/haptics_enabled.cpp delete mode 100644 ion/src/simulator/shared/dummy/haptics.cpp create mode 100644 ion/src/simulator/shared/dummy/haptics_enabled.cpp create mode 100644 ion/src/simulator/shared/haptics.cpp diff --git a/ion/src/simulator/android/Makefile b/ion/src/simulator/android/Makefile index eeba19dea..2fb97edbb 100644 --- a/ion/src/simulator/android/Makefile +++ b/ion/src/simulator/android/Makefile @@ -1,11 +1,12 @@ ion_src += $(addprefix ion/src/simulator/android/src/cpp/, \ images.cpp \ - haptics.cpp \ + haptics_enabled.cpp \ ) ion_src += $(addprefix ion/src/simulator/shared/, \ dummy/callback.cpp \ dummy/language.cpp \ + haptics.cpp \ ) ion_src += ion/src/shared/collect_registers.cpp diff --git a/ion/src/simulator/android/src/cpp/haptics.cpp b/ion/src/simulator/android/src/cpp/haptics.cpp deleted file mode 100644 index 0c1eedfce..000000000 --- a/ion/src/simulator/android/src/cpp/haptics.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "../../../shared/haptics.h" -#include -#include - -namespace Ion { -namespace Simulator { -namespace Haptics { - -static SDL_Haptic * sSDLHaptic = nullptr; - -void init() { - if (SDL_Init(SDL_INIT_HAPTIC) == 0) { - sSDLHaptic = SDL_HapticOpen(0); - if (sSDLHaptic) { - if (SDL_HapticRumbleInit(sSDLHaptic) != 0) { - sSDLHaptic = nullptr; - } - } - } -} - -void shutdown() { - if (sSDLHaptic) { - SDL_HapticClose(sSDLHaptic); - } -} - -void perform() { - // Retrieve system setting: is haptic feedback enabled? - JNIEnv * env = static_cast(SDL_AndroidGetJNIEnv()); - jobject activity = static_cast(SDL_AndroidGetActivity()); - jclass j_class = env->FindClass("com/numworks/calculator/EpsilonActivity"); - jmethodID j_methodId = env->GetMethodID(j_class,"hapticFeedbackIsEnabled", "()Z"); - - bool feedbackEnabled = env->CallObjectMethod(activity, j_methodId); - - if (feedbackEnabled && sSDLHaptic) { - SDL_HapticRumblePlay(sSDLHaptic, 1.0, 40); - } -} - - -} -} -} - diff --git a/ion/src/simulator/android/src/cpp/haptics_enabled.cpp b/ion/src/simulator/android/src/cpp/haptics_enabled.cpp new file mode 100644 index 000000000..3f2e77487 --- /dev/null +++ b/ion/src/simulator/android/src/cpp/haptics_enabled.cpp @@ -0,0 +1,20 @@ +#include "../../../shared/haptics.h" +#include +#include + +namespace Ion { +namespace Simulator { +namespace Haptics { + +bool isEnabled() { + JNIEnv * env = static_cast(SDL_AndroidGetJNIEnv()); + jobject activity = static_cast(SDL_AndroidGetActivity()); + jclass j_class = env->FindClass("com/numworks/calculator/EpsilonActivity"); + jmethodID j_methodId = env->GetMethodID(j_class,"hapticFeedbackIsEnabled", "()Z"); + + return env->CallObjectMethod(activity, j_methodId); +} + +} +} +} diff --git a/ion/src/simulator/ios/Makefile b/ion/src/simulator/ios/Makefile index cdc1a03f6..e3496ac80 100644 --- a/ion/src/simulator/ios/Makefile +++ b/ion/src/simulator/ios/Makefile @@ -5,7 +5,8 @@ ion_src += $(addprefix ion/src/simulator/ios/, \ ion_src += $(addprefix ion/src/simulator/shared/, \ apple/language.m \ dummy/callback.cpp \ - dummy/haptics.cpp \ + dummy/haptics_enabled.cpp \ + haptics.cpp \ ) ion_src += ion/src/shared/collect_registers.cpp diff --git a/ion/src/simulator/linux/Makefile b/ion/src/simulator/linux/Makefile index 4eec77717..256685745 100644 --- a/ion/src/simulator/linux/Makefile +++ b/ion/src/simulator/linux/Makefile @@ -15,9 +15,10 @@ ion_src += $(addprefix ion/src/simulator/linux/, \ ion_src += $(addprefix ion/src/simulator/shared/, \ dummy/callback.cpp \ - dummy/haptics.cpp \ + dummy/haptics_enabled.cpp \ collect_registers_x86_64.s \ collect_registers.cpp \ + haptics.cpp \ ) ifeq ($(EPSILON_TELEMETRY),1) diff --git a/ion/src/simulator/macos/Makefile b/ion/src/simulator/macos/Makefile index 4fc46018e..8171ffff2 100644 --- a/ion/src/simulator/macos/Makefile +++ b/ion/src/simulator/macos/Makefile @@ -5,9 +5,10 @@ ion_src += $(addprefix ion/src/simulator/macos/, \ ion_src += $(addprefix ion/src/simulator/shared/, \ apple/language.m \ dummy/callback.cpp \ - dummy/haptics.cpp \ + dummy/haptics_enabled.cpp \ collect_registers_x86_64.s \ collect_registers.cpp \ + haptics.cpp \ ) ifeq ($(EPSILON_TELEMETRY),1) diff --git a/ion/src/simulator/shared/dummy/haptics.cpp b/ion/src/simulator/shared/dummy/haptics.cpp deleted file mode 100644 index 1d148beb1..000000000 --- a/ion/src/simulator/shared/dummy/haptics.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "../haptics.h" - -namespace Ion { -namespace Simulator { -namespace Haptics { - -void init() { -} - -void shutdown() { -} - -void perform() { -} - - -} -} -} diff --git a/ion/src/simulator/shared/dummy/haptics_enabled.cpp b/ion/src/simulator/shared/dummy/haptics_enabled.cpp new file mode 100644 index 000000000..ee9e48eb8 --- /dev/null +++ b/ion/src/simulator/shared/dummy/haptics_enabled.cpp @@ -0,0 +1,15 @@ +#include "../haptics.h" + +namespace Ion { +namespace Simulator { +namespace Haptics { + +bool isEnabled() { + /* Dummy default to false as failsafe. + * Avoid to get haptics feedback that are not desactivable. */ + return false; +} + +} +} +} diff --git a/ion/src/simulator/shared/events.cpp b/ion/src/simulator/shared/events.cpp index 04e35f9c8..a522fe7dd 100644 --- a/ion/src/simulator/shared/events.cpp +++ b/ion/src/simulator/shared/events.cpp @@ -6,7 +6,7 @@ namespace Ion { namespace Events { void didPressNewKey() { - Simulator::Haptics::perform(); + Simulator::Haptics::rumble(); } } diff --git a/ion/src/simulator/shared/haptics.cpp b/ion/src/simulator/shared/haptics.cpp new file mode 100644 index 000000000..f0b61b2ea --- /dev/null +++ b/ion/src/simulator/shared/haptics.cpp @@ -0,0 +1,35 @@ +#include "haptics.h" +#include + +namespace Ion { +namespace Simulator { +namespace Haptics { + +static SDL_Haptic * sSDLHaptic = nullptr; + +void init() { + if (SDL_Init(SDL_INIT_HAPTIC) == 0) { + sSDLHaptic = SDL_HapticOpen(0); + if (sSDLHaptic) { + if (SDL_HapticRumbleInit(sSDLHaptic) != 0) { + sSDLHaptic = nullptr; + } + } + } +} + +void shutdown() { + if (sSDLHaptic) { + SDL_HapticClose(sSDLHaptic); + } +} + +void rumble() { + if (isEnabled() && sSDLHaptic) { + SDL_HapticRumblePlay(sSDLHaptic, 1.0, 40); + } +} + +} +} +} diff --git a/ion/src/simulator/shared/haptics.h b/ion/src/simulator/shared/haptics.h index 1a8f88378..2adc20ede 100644 --- a/ion/src/simulator/shared/haptics.h +++ b/ion/src/simulator/shared/haptics.h @@ -6,7 +6,8 @@ namespace Simulator { namespace Haptics { void init(); -void perform(); +bool isEnabled(); +void rumble(); void shutdown(); } diff --git a/ion/src/simulator/web/Makefile b/ion/src/simulator/web/Makefile index 66f8d8e80..e8c729371 100644 --- a/ion/src/simulator/web/Makefile +++ b/ion/src/simulator/web/Makefile @@ -20,7 +20,8 @@ ion_src += $(addprefix ion/src/simulator/web/, \ ion_src += $(addprefix ion/src/simulator/shared/, \ dummy/language.cpp \ - dummy/haptics.cpp \ + dummy/haptics_enabled.cpp \ + haptics.cpp \ ) ion_src += ion/src/shared/collect_registers.cpp diff --git a/ion/src/simulator/windows/Makefile b/ion/src/simulator/windows/Makefile index 3fd21fc20..a057924f5 100644 --- a/ion/src/simulator/windows/Makefile +++ b/ion/src/simulator/windows/Makefile @@ -6,7 +6,8 @@ ion_src += $(addprefix ion/src/simulator/windows/, \ ion_src += $(addprefix ion/src/simulator/shared/, \ dummy/callback.cpp \ - dummy/haptics.cpp \ + dummy/haptics_enabled.cpp \ + haptics.cpp \ ) ion_src += ion/src/shared/collect_registers.cpp