mirror of
				https://github.com/RetroDECK/Duckstation.git
				synced 2025-04-10 19:15:14 +00:00 
			
		
		
		
	FrontendCommon: Make SDL2 dependency optional
This commit is contained in:
		
							parent
							
								
									f35970fcac
								
							
						
					
					
						commit
						9ab5212833
					
				|  | @ -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") | ||||
|  |  | |||
|  | @ -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) | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 <cstring> | ||||
| Log_SetChannel(CommonHostInterface); | ||||
| 
 | ||||
|  | @ -35,8 +37,10 @@ std::unique_ptr<AudioStream> 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<std::string> 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<int> 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<int> controller_index = StringUtil::FromChars<int>(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<int> controller_index = StringUtil::FromChars<int>(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() | ||||
|  |  | |||
|  | @ -1,14 +1,14 @@ | |||
| #pragma once | ||||
| #include "core/host_interface.h" | ||||
| #include "common/string.h" | ||||
| #include "core/host_interface.h" | ||||
| #include <atomic> | ||||
| #include <functional> | ||||
| #include <map> | ||||
| #include <memory> | ||||
| #include <mutex> | ||||
| #include <optional> | ||||
| #include <string_view> | ||||
| #include <utility> | ||||
| #include <optional> | ||||
| #include <vector> | ||||
| 
 | ||||
| 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; | ||||
| 
 | ||||
|  |  | |||
|  | @ -202,7 +202,7 @@ | |||
|       </PrecompiledHeader> | ||||
|       <WarningLevel>Level3</WarningLevel> | ||||
|       <Optimization>Disabled</Optimization> | ||||
|       <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <PreprocessorDefinitions>WITH_SDL2=1;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> | ||||
|       <AdditionalIncludeDirectories>$(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||||
|  | @ -229,7 +229,7 @@ | |||
|       </PrecompiledHeader> | ||||
|       <WarningLevel>Level3</WarningLevel> | ||||
|       <Optimization>Disabled</Optimization> | ||||
|       <PreprocessorDefinitions>_ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <PreprocessorDefinitions>WITH_SDL2=1;_ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> | ||||
|       <AdditionalIncludeDirectories>$(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||||
|  | @ -259,7 +259,7 @@ | |||
|       </PrecompiledHeader> | ||||
|       <WarningLevel>Level3</WarningLevel> | ||||
|       <Optimization>Disabled</Optimization> | ||||
|       <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <PreprocessorDefinitions>WITH_SDL2=1;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> | ||||
|       <AdditionalIncludeDirectories>$(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||||
|  | @ -286,7 +286,7 @@ | |||
|       </PrecompiledHeader> | ||||
|       <WarningLevel>Level3</WarningLevel> | ||||
|       <Optimization>Disabled</Optimization> | ||||
|       <PreprocessorDefinitions>_ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <PreprocessorDefinitions>WITH_SDL2=1;_ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> | ||||
|       <AdditionalIncludeDirectories>$(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||||
|  | @ -318,7 +318,7 @@ | |||
|       <Optimization>MaxSpeed</Optimization> | ||||
|       <FunctionLevelLinking>true</FunctionLevelLinking> | ||||
|       <IntrinsicFunctions>true</IntrinsicFunctions> | ||||
|       <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <PreprocessorDefinitions>WITH_SDL2=1;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <AdditionalIncludeDirectories>$(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||||
|       <MultiProcessorCompilation>true</MultiProcessorCompilation> | ||||
|  | @ -347,7 +347,7 @@ | |||
|       <Optimization>MaxSpeed</Optimization> | ||||
|       <FunctionLevelLinking>true</FunctionLevelLinking> | ||||
|       <IntrinsicFunctions>true</IntrinsicFunctions> | ||||
|       <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <PreprocessorDefinitions>WITH_SDL2=1;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <AdditionalIncludeDirectories>$(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||||
|       <OmitFramePointers>true</OmitFramePointers> | ||||
|  | @ -378,7 +378,7 @@ | |||
|       <Optimization>MaxSpeed</Optimization> | ||||
|       <FunctionLevelLinking>true</FunctionLevelLinking> | ||||
|       <IntrinsicFunctions>true</IntrinsicFunctions> | ||||
|       <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <PreprocessorDefinitions>WITH_SDL2=1;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <AdditionalIncludeDirectories>$(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||||
|       <MultiProcessorCompilation>true</MultiProcessorCompilation> | ||||
|  | @ -407,7 +407,7 @@ | |||
|       <Optimization>MaxSpeed</Optimization> | ||||
|       <FunctionLevelLinking>true</FunctionLevelLinking> | ||||
|       <IntrinsicFunctions>true</IntrinsicFunctions> | ||||
|       <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <PreprocessorDefinitions>WITH_SDL2=1;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <AdditionalIncludeDirectories>$(SolutionDir)dep\msvc\include\SDL;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\imgui\include;$(SolutionDir)src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||||
|       <OmitFramePointers>true</OmitFramePointers> | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Connor McLaughlin
						Connor McLaughlin