From 9ab5212833e36bae0ebc28a7d1685402a9247534 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 28 Feb 2020 17:00:12 +1000 Subject: [PATCH] FrontendCommon: Make SDL2 dependency optional --- CMakeLists.txt | 1 + src/frontend-common/CMakeLists.txt | 23 ++-- src/frontend-common/common_host_interface.cpp | 122 +++++++++++------- src/frontend-common/common_host_interface.h | 10 +- src/frontend-common/frontend-common.vcxproj | 16 +-- 5 files changed, 102 insertions(+), 70 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 95b5a1cef..d944b88b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ endif() # Common include/library directories on Windows. if(WIN32) + set(SDL2_FOUND TRUE) set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/dep/msvc/sdl2/include") if(CMAKE_SIZEOF_VOID_P EQUAL 8) set(SDL2_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/dep/msvc/sdl2/lib64/SDL2.lib") diff --git a/src/frontend-common/CMakeLists.txt b/src/frontend-common/CMakeLists.txt index 20e87e3dc..6ad3eb2fb 100644 --- a/src/frontend-common/CMakeLists.txt +++ b/src/frontend-common/CMakeLists.txt @@ -5,14 +5,21 @@ add_library(frontend-common icon.h imgui_styles.cpp imgui_styles.h - sdl_audio_stream.cpp - sdl_audio_stream.h - sdl_controller_interface.cpp - sdl_controller_interface.h - sdl_initializer.cpp - sdl_initializer.h ) -target_include_directories(frontend-common PRIVATE ${SDL2_INCLUDE_DIRS}) -target_link_libraries(frontend-common PRIVATE core common imgui ${SDL2_LIBRARIES}) +if(SDL2_FOUND) + target_sources(frontend-common PRIVATE + sdl_audio_stream.cpp + sdl_audio_stream.h + sdl_controller_interface.cpp + sdl_controller_interface.h + sdl_initializer.cpp + sdl_initializer.h + ) + target_compile_definitions(frontend-common PRIVATE "WITH_SDL2=1") + target_include_directories(frontend-common PRIVATE ${SDL2_INCLUDE_DIRS}) + target_link_libraries(frontend-common PRIVATE ${SDL2_LIBRARIES}) +endif() + +target_link_libraries(frontend-common PRIVATE core common imgui) diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index 7668ea06b..fd0266c71 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -7,8 +7,10 @@ #include "core/game_list.h" #include "core/gpu.h" #include "core/system.h" +#ifdef WITH_SDL2 #include "sdl_audio_stream.h" #include "sdl_controller_interface.h" +#endif #include Log_SetChannel(CommonHostInterface); @@ -35,8 +37,10 @@ std::unique_ptr CommonHostInterface::CreateAudioStream(AudioBackend case AudioBackend::Cubeb: return AudioStream::CreateCubebAudioStream(); +#ifdef WITH_SDL2 case AudioBackend::SDL: return SDLAudioStream::Create(); +#endif default: return nullptr; @@ -102,12 +106,28 @@ bool CommonHostInterface::HandleHostKeyEvent(HostKeyCode key, bool pressed) void CommonHostInterface::UpdateInputMap(SettingsInterface& si) { m_keyboard_input_handlers.clear(); +#ifdef WITH_SDL2 g_sdl_controller_interface.ClearControllerBindings(); +#endif UpdateControllerInputMap(si); UpdateHotkeyInputMap(si); } +static bool SplitBinding(const std::string& binding, std::string_view* device, std::string_view* sub_binding) +{ + const std::string::size_type slash_pos = binding.find('/'); + if (slash_pos == std::string::npos) + { + Log_WarningPrintf("Malformed binding: '%s'", binding.c_str()); + return false; + } + + *device = std::string_view(binding).substr(0, slash_pos); + *sub_binding = std::string_view(binding).substr(slash_pos + 1); + return true; +} + void CommonHostInterface::UpdateControllerInputMap(SettingsInterface& si) { for (u32 controller_index = 0; controller_index < 2; controller_index++) @@ -127,7 +147,11 @@ void CommonHostInterface::UpdateControllerInputMap(SettingsInterface& si) si.GetStringList(category, TinyString::FromFormat("Button%s", button_name.c_str())); for (const std::string& binding : bindings) { - AddButtonToInputMap(binding, [this, controller_index, button_code](bool pressed) { + std::string_view device, button; + if (!SplitBinding(binding, &device, &button)) + continue; + + AddButtonToInputMap(binding, device, button, [this, controller_index, button_code](bool pressed) { if (!m_system) return; @@ -148,7 +172,11 @@ void CommonHostInterface::UpdateControllerInputMap(SettingsInterface& si) si.GetStringList(category, TinyString::FromFormat("Axis%s", axis_name.c_str())); for (const std::string& binding : bindings) { - AddAxisToInputMap(binding, [this, controller_index, axis_code](float value) { + std::string_view device, axis; + if (!SplitBinding(binding, &device, &axis)) + continue; + + AddAxisToInputMap(binding, device, axis, [this, controller_index, axis_code](float value) { if (!m_system) return; @@ -167,39 +195,40 @@ void CommonHostInterface::UpdateHotkeyInputMap(SettingsInterface& si) { const std::vector bindings = si.GetStringList("Hotkeys", hi.name); for (const std::string& binding : bindings) - AddButtonToInputMap(binding, hi.handler); + { + std::string_view device, button; + if (!SplitBinding(binding, &device, &button)) + continue; + + AddButtonToInputMap(binding, device, button, hi.handler); + } } } -void CommonHostInterface::AddButtonToInputMap(const std::string& binding, InputButtonHandler handler) +bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const std::string_view& device, + const std::string_view& button, InputButtonHandler handler) { - const std::string::size_type slash_pos = binding.find('/'); - if (slash_pos == std::string::npos) - { - Log_WarningPrintf("Malformed button binding: '%s'", binding.c_str()); - return; - } - - const auto device = std::string_view(binding).substr(0, slash_pos); - const auto button = std::string_view(binding).substr(slash_pos + 1); if (device == "Keyboard") { std::optional key_id = GetHostKeyCode(button); if (!key_id.has_value()) { Log_WarningPrintf("Unknown keyboard key in binding '%s'", binding.c_str()); - return; + return false; } m_keyboard_input_handlers.emplace(key_id.value(), std::move(handler)); + return true; } - else if (device == "Controller") + +#ifdef WITH_SDL2 + if (device == "Controller") { const std::optional controller_index = StringUtil::FromChars(device.substr(10)); if (!controller_index || *controller_index < 0) { Log_WarningPrintf("Invalid controller index in button binding '%s'", binding.c_str()); - return; + return false; } if (button.find_first_of("Button") == 0) @@ -209,8 +238,10 @@ void CommonHostInterface::AddButtonToInputMap(const std::string& binding, InputB !g_sdl_controller_interface.BindControllerButton(*controller_index, *button_index, std::move(handler))) { Log_WarningPrintf("Failed to bind controller button '%s' to button", binding.c_str()); - return; + return false; } + + return true; } else if (button.find_first_of("+Axis") == 0 || button.find_first_of("-Axis")) { @@ -220,40 +251,32 @@ void CommonHostInterface::AddButtonToInputMap(const std::string& binding, InputB positive, std::move(handler))) { Log_WarningPrintf("Failed to bind controller axis '%s' to button", binding.c_str()); - return; + return false; } + + return true; } - else - { - Log_WarningPrintf("Malformed controller binding '%s' in button", binding.c_str()); - return; - } - } - else - { - Log_WarningPrintf("Unknown input device in button binding '%s'", binding.c_str()); - return; + + Log_WarningPrintf("Malformed controller binding '%s' in button", binding.c_str()); + return false; } +#endif + + Log_WarningPrintf("Unknown input device in button binding '%s'", binding.c_str()); + return false; } -void CommonHostInterface::AddAxisToInputMap(const std::string& binding, InputAxisHandler handler) +bool CommonHostInterface::AddAxisToInputMap(const std::string& binding, const std::string_view& device, + const std::string_view& axis, InputAxisHandler handler) { - const std::string::size_type slash_pos = binding.find('/'); - if (slash_pos == std::string::npos) - { - Log_WarningPrintf("Malformed axis binding: '%s'", binding.c_str()); - return; - } - - const auto device = std::string_view(binding).substr(0, slash_pos); - const auto axis = std::string_view(binding).substr(slash_pos + 1); +#ifdef WITH_SDL2 if (device == "Controller") { const std::optional controller_index = StringUtil::FromChars(device.substr(10)); if (!controller_index || *controller_index < 0) { Log_WarningPrintf("Invalid controller index in axis binding '%s'", binding.c_str()); - return; + return false; } if (axis.find_first_of("Axis") == 0) @@ -263,20 +286,19 @@ void CommonHostInterface::AddAxisToInputMap(const std::string& binding, InputAxi !g_sdl_controller_interface.BindControllerAxis(*controller_index, *axis_index, std::move(handler))) { Log_WarningPrintf("Failed to bind controller axis '%s' to axi", binding.c_str()); - return; + return false; } + + return true; } - else - { - Log_WarningPrintf("Malformed controller binding '%s' in button", binding.c_str()); - return; - } - } - else - { - Log_WarningPrintf("Unknown input device in axis binding '%s'", binding.c_str()); - return; + + Log_WarningPrintf("Malformed controller binding '%s' in button", binding.c_str()); + return false; } +#endif + + Log_WarningPrintf("Unknown input device in axis binding '%s'", binding.c_str()); + return false; } void CommonHostInterface::RegisterGeneralHotkeys() diff --git a/src/frontend-common/common_host_interface.h b/src/frontend-common/common_host_interface.h index e45fddc4f..54437765c 100644 --- a/src/frontend-common/common_host_interface.h +++ b/src/frontend-common/common_host_interface.h @@ -1,14 +1,14 @@ #pragma once -#include "core/host_interface.h" #include "common/string.h" +#include "core/host_interface.h" #include #include #include #include #include +#include #include #include -#include #include class CommonHostInterface : public HostInterface @@ -55,8 +55,10 @@ private: void RegisterSaveStateHotkeys(); void UpdateControllerInputMap(SettingsInterface& si); void UpdateHotkeyInputMap(SettingsInterface& si); - void AddButtonToInputMap(const std::string& binding, InputButtonHandler handler); - void AddAxisToInputMap(const std::string& binding, InputAxisHandler handler); + virtual bool AddButtonToInputMap(const std::string& binding, const std::string_view& device, + const std::string_view& button, InputButtonHandler handler); + virtual bool AddAxisToInputMap(const std::string& binding, const std::string_view& device, + const std::string_view& axis, InputAxisHandler handler); HotkeyInfoList m_hotkeys; diff --git a/src/frontend-common/frontend-common.vcxproj b/src/frontend-common/frontend-common.vcxproj index 6c909c739..c8adcaf33 100644 --- a/src/frontend-common/frontend-common.vcxproj +++ b/src/frontend-common/frontend-common.vcxproj @@ -202,7 +202,7 @@ Level3 Disabled - WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + WITH_SDL2=1;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase $(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) @@ -229,7 +229,7 @@ Level3 Disabled - _ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions) + WITH_SDL2=1;_ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase $(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) @@ -259,7 +259,7 @@ Level3 Disabled - WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + WITH_SDL2=1;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase $(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) @@ -286,7 +286,7 @@ Level3 Disabled - _ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions) + WITH_SDL2=1;_ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase $(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) @@ -318,7 +318,7 @@ MaxSpeed true true - WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + WITH_SDL2=1;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true $(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true @@ -347,7 +347,7 @@ MaxSpeed true true - WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + WITH_SDL2=1;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true $(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true @@ -378,7 +378,7 @@ MaxSpeed true true - WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + WITH_SDL2=1;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true $(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true @@ -407,7 +407,7 @@ MaxSpeed true true - WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + WITH_SDL2=1;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true $(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true