FrontendCommon: Use SDL_InitSubSystem() for lazy initialization

This commit is contained in:
Connor McLaughlin 2020-02-16 00:15:05 +09:00
parent 78a6666439
commit 06f4d72631
10 changed files with 60 additions and 20 deletions

View file

@ -783,7 +783,7 @@ void QtHostInterface::threadEntryPoint()
m_worker_thread_event_loop = new QEventLoop(); m_worker_thread_event_loop = new QEventLoop();
// set up controller interface and immediate poll to pick up the controller attached events // set up controller interface and immediate poll to pick up the controller attached events
g_sdl_controller_interface.Initialize(this, true); g_sdl_controller_interface.Initialize(this);
g_sdl_controller_interface.PumpSDLEvents(); g_sdl_controller_interface.PumpSDLEvents();
doUpdateInputMap(); doUpdateInputMap();

View file

@ -2,18 +2,12 @@
#include "common/log.h" #include "common/log.h"
#include "core/system.h" #include "core/system.h"
#include "sdl_host_interface.h" #include "sdl_host_interface.h"
#include "frontend-common/sdl_initializer.h"
#include <SDL.h> #include <SDL.h>
#include <cstdio> #include <cstdio>
static int Run(int argc, char* argv[]) static int Run(int argc, char* argv[])
{ {
// init sdl
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) < 0)
{
Panic("SDL initialization failed");
return -1;
}
// parameters // parameters
std::optional<s32> state_index; std::optional<s32> state_index;
const char* boot_filename = nullptr; const char* boot_filename = nullptr;
@ -82,6 +76,8 @@ int main(int argc, char* argv[])
// Log::SetFilterLevel(LOGLEVEL_DEV); // Log::SetFilterLevel(LOGLEVEL_DEV);
#endif #endif
FrontendCommon::EnsureSDLInitialized();
// return NoGUITest(); // return NoGUITest();
return Run(argc, argv); return Run(argc, argv);
} }

View file

@ -7,6 +7,8 @@ add_library(frontend-common
sdl_audio_stream.h sdl_audio_stream.h
sdl_controller_interface.cpp sdl_controller_interface.cpp
sdl_controller_interface.h sdl_controller_interface.h
sdl_initializer.cpp
sdl_initializer.h
) )
target_include_directories(frontend-common PRIVATE ${SDL2_INCLUDE_DIRS}) target_include_directories(frontend-common PRIVATE ${SDL2_INCLUDE_DIRS})

View file

@ -50,12 +50,14 @@
<ClCompile Include="imgui_styles.cpp" /> <ClCompile Include="imgui_styles.cpp" />
<ClCompile Include="sdl_audio_stream.cpp" /> <ClCompile Include="sdl_audio_stream.cpp" />
<ClCompile Include="sdl_controller_interface.cpp" /> <ClCompile Include="sdl_controller_interface.cpp" />
<ClCompile Include="sdl_initializer.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="icon.h" /> <ClInclude Include="icon.h" />
<ClInclude Include="imgui_styles.h" /> <ClInclude Include="imgui_styles.h" />
<ClInclude Include="sdl_audio_stream.h" /> <ClInclude Include="sdl_audio_stream.h" />
<ClInclude Include="sdl_controller_interface.h" /> <ClInclude Include="sdl_controller_interface.h" />
<ClInclude Include="sdl_initializer.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="font_roboto_regular.inl" /> <None Include="font_roboto_regular.inl" />

View file

@ -5,12 +5,14 @@
<ClCompile Include="imgui_styles.cpp" /> <ClCompile Include="imgui_styles.cpp" />
<ClCompile Include="sdl_audio_stream.cpp" /> <ClCompile Include="sdl_audio_stream.cpp" />
<ClCompile Include="sdl_controller_interface.cpp" /> <ClCompile Include="sdl_controller_interface.cpp" />
<ClCompile Include="sdl_initializer.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="icon.h" /> <ClInclude Include="icon.h" />
<ClInclude Include="imgui_styles.h" /> <ClInclude Include="imgui_styles.h" />
<ClInclude Include="sdl_audio_stream.h" /> <ClInclude Include="sdl_audio_stream.h" />
<ClInclude Include="sdl_controller_interface.h" /> <ClInclude Include="sdl_controller_interface.h" />
<ClInclude Include="sdl_initializer.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="font_roboto_regular.inl" /> <None Include="font_roboto_regular.inl" />

View file

@ -1,4 +1,5 @@
#include "sdl_audio_stream.h" #include "sdl_audio_stream.h"
#include "sdl_initializer.h"
#include "common/assert.h" #include "common/assert.h"
#include "common/log.h" #include "common/log.h"
#include <SDL.h> #include <SDL.h>
@ -21,6 +22,14 @@ bool SDLAudioStream::OpenDevice()
{ {
DebugAssert(!m_is_open); DebugAssert(!m_is_open);
FrontendCommon::EnsureSDLInitialized();
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
{
Log_ErrorPrintf("SDL_InitSubSystem(SDL_INIT_AUDIO) failed");
return false;
}
SDL_AudioSpec spec = {}; SDL_AudioSpec spec = {};
spec.freq = m_output_sample_rate; spec.freq = m_output_sample_rate;
spec.channels = static_cast<Uint8>(m_channels); spec.channels = static_cast<Uint8>(m_channels);
@ -32,6 +41,7 @@ bool SDLAudioStream::OpenDevice()
if (SDL_OpenAudio(&spec, nullptr) < 0) if (SDL_OpenAudio(&spec, nullptr) < 0)
{ {
Log_ErrorPrintf("SDL_OpenAudio failed"); Log_ErrorPrintf("SDL_OpenAudio failed");
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return false; return false;
} }
@ -48,6 +58,7 @@ void SDLAudioStream::CloseDevice()
{ {
DebugAssert(m_is_open); DebugAssert(m_is_open);
SDL_CloseAudio(); SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
m_is_open = false; m_is_open = false;
} }

View file

@ -4,6 +4,7 @@
#include "core/controller.h" #include "core/controller.h"
#include "core/host_interface.h" #include "core/host_interface.h"
#include "core/system.h" #include "core/system.h"
#include "sdl_initializer.h"
#include <SDL.h> #include <SDL.h>
Log_SetChannel(SDLControllerInterface); Log_SetChannel(SDLControllerInterface);
@ -16,19 +17,19 @@ SDLControllerInterface::~SDLControllerInterface()
Assert(m_controllers.empty()); Assert(m_controllers.empty());
} }
bool SDLControllerInterface::Initialize(HostInterface* host_interface, bool init_sdl) bool SDLControllerInterface::Initialize(HostInterface* host_interface)
{ {
if (init_sdl) FrontendCommon::EnsureSDLInitialized();
if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) < 0)
{ {
if (SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) < 0) Log_ErrorPrintf("SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) failed");
{
Log_ErrorPrintf("SDL_Init() failed");
return false; return false;
} }
}
// we should open the controllers as the connected events come in, so no need to do any more here // we should open the controllers as the connected events come in, so no need to do any more here
m_host_interface = host_interface; m_host_interface = host_interface;
m_initialized = true;
return true; return true;
} }
@ -37,10 +38,10 @@ void SDLControllerInterface::Shutdown()
while (!m_controllers.empty()) while (!m_controllers.empty())
CloseGameController(m_controllers.begin()->first); CloseGameController(m_controllers.begin()->first);
if (m_sdl_initialized_by_us) if (m_initialized)
{ {
SDL_Quit(); SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC);
m_sdl_initialized_by_us = false; m_initialized = false;
} }
m_host_interface = nullptr; m_host_interface = nullptr;

View file

@ -26,7 +26,7 @@ public:
SDLControllerInterface(); SDLControllerInterface();
~SDLControllerInterface(); ~SDLControllerInterface();
bool Initialize(HostInterface* host_interface, bool init_sdl); bool Initialize(HostInterface* host_interface);
void Shutdown(); void Shutdown();
// Removes all bindings. Call before setting new bindings. // Removes all bindings. Call before setting new bindings.
@ -101,7 +101,7 @@ private:
std::mutex m_event_intercept_mutex; std::mutex m_event_intercept_mutex;
Hook::Callback m_event_intercept_callback; Hook::Callback m_event_intercept_callback;
bool m_sdl_initialized_by_us = false; bool m_initialized = false;
}; };
extern SDLControllerInterface g_sdl_controller_interface; extern SDLControllerInterface g_sdl_controller_interface;

View file

@ -0,0 +1,21 @@
#include "sdl_initializer.h"
#include "common/assert.h"
#include <SDL.h>
namespace FrontendCommon {
static bool s_sdl_initialized = false;
void EnsureSDLInitialized()
{
if (s_sdl_initialized)
return;
if (SDL_Init(0) < 0)
{
Panic("SDL_Init(0) failed");
return;
}
s_sdl_initialized = true;
}
} // namespace FrontendCommon

View file

@ -0,0 +1,5 @@
#pragma once
namespace FrontendCommon {
void EnsureSDLInitialized();
}