From 25d2148ae46d3275c18163980b6ffd8826624f55 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 5 Aug 2022 17:05:18 +1000 Subject: [PATCH] Misc: Necessary emucore changes for Android --- src/common/file_system.cpp | 6 ++- src/core/settings.h | 5 ++ src/frontend-common/common_host.cpp | 4 ++ src/frontend-common/input_manager.cpp | 66 +++++++++++++++++++++++++-- src/frontend-common/input_manager.h | 12 +++++ src/frontend-common/input_source.h | 3 ++ 6 files changed, 91 insertions(+), 5 deletions(-) diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index d01ba099a..79d9c79fd 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -48,6 +48,8 @@ Log_SetChannel(FileSystem); +#ifndef __ANDROID__ + #ifdef _WIN32 static std::time_t ConvertFileTimeToUnixTime(const FILETIME& ft) { @@ -753,6 +755,8 @@ int FileSystem::OpenFDFile(const char* filename, int flags, int mode) #endif } +#endif + FileSystem::ManagedCFilePtr FileSystem::OpenManagedCFile(const char* filename, const char* mode) { return ManagedCFilePtr(OpenCFile(filename, mode), [](std::FILE* fp) { std::fclose(fp); }); @@ -1625,7 +1629,7 @@ bool FileSystem::SetPathCompression(const char* path, bool enable) return result; } -#else +#elif !defined(__ANDROID__) static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, const char* Path, const char* Pattern, u32 Flags, FileSystem::FindResultsArray* pResults) diff --git a/src/core/settings.h b/src/core/settings.h index 34bd5310e..e972cfaba 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -410,8 +410,13 @@ struct Settings static constexpr LOGLEVEL DEFAULT_LOG_LEVEL = LOGLEVEL_INFO; +#ifndef __ANDROID__ static constexpr u32 DEFAULT_AUDIO_BUFFER_MS = 50; static constexpr u32 DEFAULT_AUDIO_OUTPUT_LATENCY_MS = 20; +#else + static constexpr u32 DEFAULT_AUDIO_BUFFER_MS = 100; + static constexpr u32 DEFAULT_AUDIO_OUTPUT_LATENCY_MS = 20; +#endif static constexpr AudioStretchMode DEFAULT_AUDIO_STRETCH_MODE = AudioStretchMode::TimeStretch; // Enable console logging by default on Linux platforms. diff --git a/src/frontend-common/common_host.cpp b/src/frontend-common/common_host.cpp index 70bb7ba52..794382754 100644 --- a/src/frontend-common/common_host.cpp +++ b/src/frontend-common/common_host.cpp @@ -131,6 +131,8 @@ void CommonHost::ReleaseHostDisplayResources() SaveStateSelectorUI::DestroyTextures(); } +#ifndef __ANDROID__ + std::unique_ptr Host::CreateAudioStream(AudioBackend backend, u32 sample_rate, u32 channels, u32 buffer_ms, u32 latency_ms, AudioStretchMode stretch) { @@ -154,6 +156,8 @@ std::unique_ptr Host::CreateAudioStream(AudioBackend backend, u32 s } } +#endif + void CommonHost::UpdateLogSettings() { Log::SetFilterLevel(g_settings.log_level); diff --git a/src/frontend-common/input_manager.cpp b/src/frontend-common/input_manager.cpp index 2933d85c7..57401b7b7 100644 --- a/src/frontend-common/input_manager.cpp +++ b/src/frontend-common/input_manager.cpp @@ -101,6 +101,8 @@ static std::optional ParseHostKeyboardKey(const std::string_vie const std::string_view& sub_binding); static std::optional ParsePointerKey(const std::string_view& source, const std::string_view& sub_binding); +static std::optional ParseSensorKey(const std::string_view& source, + const std::string_view& sub_binding); static std::vector SplitChord(const std::string_view& binding); static bool SplitBinding(const std::string_view& binding, std::string_view* source, std::string_view* sub_binding); @@ -120,7 +122,6 @@ static bool PreprocessEvent(InputBindingKey key, float value, GenericInputBindin static void LoadMacroButtonConfig(SettingsInterface& si, const std::string& section, u32 pad, const Controller::ControllerInfo* cinfo); -static void SetMacroButtonState(u32 pad, u32 index, bool state); static void ApplyMacroButton(u32 pad, const MacroButton& mb); static void UpdateMacroButtons(); } // namespace InputManager @@ -162,6 +163,7 @@ static constexpr const std::array(InputPointerAxis: {"X", "Y", "WheelX", "WheelY"}}; static constexpr const std::array s_pointer_button_names = { {"LeftButton", "RightButton", "MiddleButton"}}; +static constexpr const std::array s_sensor_accelerometer_names = {{"Turn", "Tilt", "Rotate"}}; struct PointerAxisState { @@ -239,6 +241,10 @@ std::optional InputManager::ParseInputBindingKey(const std::str { return ParsePointerKey(source, sub_binding); } + else if (StringUtil::StartsWith(source, "Sensor")) + { + return ParseSensorKey(source, sub_binding); + } else { for (u32 i = FIRST_EXTERNAL_INPUT_SOURCE; i < LAST_EXTERNAL_INPUT_SOURCE; i++) @@ -301,6 +307,11 @@ std::string InputManager::ConvertInputBindingKeyToString(InputBindingKey key) key.negative ? '-' : '+'); } } + else if (key.source_type == InputSourceType::Sensor) + { + if (key.source_subtype == InputSubclass::SensorAccelerometer && key.data < s_sensor_accelerometer_names.size()) + return fmt::format("Sensor/{}", s_sensor_accelerometer_names[key.data]); + } else if (key.source_type < InputSourceType::Count && s_input_sources[static_cast(key.source_type)]) { return s_input_sources[static_cast(key.source_type)]->ConvertKeyToString(key); @@ -403,23 +414,35 @@ InputBindingKey InputManager::MakePointerAxisKey(u32 index, InputPointerAxis axi return key; } +InputBindingKey InputManager::MakeSensorAxisKey(InputSubclass sensor, u32 axis) +{ + InputBindingKey key = {}; + key.data = static_cast(axis); + key.source_index = 0; + key.source_type = InputSourceType::Sensor; + key.source_subtype = sensor; + return key; +} + // ------------------------------------------------------------------------ // Bind Encoders // ------------------------------------------------------------------------ static std::array(InputSourceType::Count)> s_input_class_names = {{ "Keyboard", - "Mouse", + "Pointer", + "Sensor", #ifdef _WIN32 "DInput", "XInput", -#endif -#if defined(_WIN32) "RawInput", #endif #ifdef WITH_SDL2 "SDL", #endif +#ifdef __ANDROID__ + "Android", +#endif }}; InputSource* InputManager::GetInputSourceInterface(InputSourceType type) @@ -513,6 +536,38 @@ std::optional InputManager::ParsePointerKey(const std::string_v return std::nullopt; } +std::optional InputManager::ParseSensorKey(const std::string_view& source, + const std::string_view& sub_binding) +{ + if (source != "Sensor") + return std::nullopt; + + InputBindingKey key = {}; + key.source_type = InputSourceType::Sensor; + key.source_index = 0; + + for (u32 i = 0; i < s_sensor_accelerometer_names.size(); i++) + { + if (StringUtil::StartsWith(sub_binding, s_sensor_accelerometer_names[i])) + { + key.source_subtype = InputSubclass::SensorAccelerometer; + key.data = i; + + const std::string_view dir_part(sub_binding.substr(std::strlen(s_pointer_axis_names[i]))); + if (dir_part == "+") + key.negative = false; + else if (dir_part == "-") + key.negative = true; + else + return std::nullopt; + + return key; + } + } + + return std::nullopt; +} + // ------------------------------------------------------------------------ // Binding Enumeration // ------------------------------------------------------------------------ @@ -1470,4 +1525,7 @@ void InputManager::ReloadSources(SettingsInterface& si, std::unique_lock ParseInputBindingKey(const std::string_view& binding); @@ -243,6 +252,9 @@ void UpdatePointerAbsolutePosition(u32 index, float x, float y); /// reporting. void UpdatePointerRelativeDelta(u32 index, InputPointerAxis axis, float d, bool raw_input = false); +/// Sets the state of the specified macro button. +void SetMacroButtonState(u32 pad, u32 index, bool state); + /// Returns true if the raw input source is being used. bool IsUsingRawInput(); diff --git a/src/frontend-common/input_source.h b/src/frontend-common/input_source.h index 5806caeb8..b769d1b01 100644 --- a/src/frontend-common/input_source.h +++ b/src/frontend-common/input_source.h @@ -69,4 +69,7 @@ public: #ifdef WITH_SDL2 static std::unique_ptr CreateSDLSource(); #endif +#ifdef __ANDROID__ + static std::unique_ptr CreateAndroidSource(); +#endif };