diff options
Diffstat (limited to 'src/input_common/main.cpp')
-rw-r--r-- | src/input_common/main.cpp | 468 |
1 files changed, 258 insertions, 210 deletions
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp index f3907c65a..940744c5f 100644 --- a/src/input_common/main.cpp +++ b/src/input_common/main.cpp @@ -4,146 +4,173 @@ #include <memory> #include <thread> +#include "common/input.h" #include "common/param_package.h" -#include "common/settings.h" -#include "input_common/analog_from_button.h" -#include "input_common/gcadapter/gc_adapter.h" -#include "input_common/gcadapter/gc_poller.h" -#include "input_common/keyboard.h" +#include "input_common/drivers/gc_adapter.h" +#include "input_common/drivers/keyboard.h" +#include "input_common/drivers/mouse.h" +#include "input_common/drivers/tas_input.h" +#include "input_common/drivers/touch_screen.h" +#include "input_common/drivers/udp_client.h" +#include "input_common/helpers/stick_from_buttons.h" +#include "input_common/helpers/touch_from_buttons.h" +#include "input_common/input_engine.h" +#include "input_common/input_mapping.h" +#include "input_common/input_poller.h" #include "input_common/main.h" -#include "input_common/motion_from_button.h" -#include "input_common/mouse/mouse_input.h" -#include "input_common/mouse/mouse_poller.h" -#include "input_common/tas/tas_input.h" -#include "input_common/tas/tas_poller.h" -#include "input_common/touch_from_button.h" -#include "input_common/udp/client.h" -#include "input_common/udp/udp.h" #ifdef HAVE_SDL2 -#include "input_common/sdl/sdl.h" +#include "input_common/drivers/sdl_driver.h" #endif namespace InputCommon { struct InputSubsystem::Impl { void Initialize() { - gcadapter = std::make_shared<GCAdapter::Adapter>(); - gcbuttons = std::make_shared<GCButtonFactory>(gcadapter); - Input::RegisterFactory<Input::ButtonDevice>("gcpad", gcbuttons); - gcanalog = std::make_shared<GCAnalogFactory>(gcadapter); - Input::RegisterFactory<Input::AnalogDevice>("gcpad", gcanalog); - gcvibration = std::make_shared<GCVibrationFactory>(gcadapter); - Input::RegisterFactory<Input::VibrationDevice>("gcpad", gcvibration); - - keyboard = std::make_shared<Keyboard>(); - Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard); - Input::RegisterFactory<Input::AnalogDevice>("analog_from_button", - std::make_shared<AnalogFromButton>()); - Input::RegisterFactory<Input::MotionDevice>("keyboard", - std::make_shared<MotionFromButton>()); - Input::RegisterFactory<Input::TouchDevice>("touch_from_button", - std::make_shared<TouchFromButtonFactory>()); + mapping_factory = std::make_shared<MappingFactory>(); + MappingCallback mapping_callback{[this](MappingData data) { RegisterInput(data); }}; + + keyboard = std::make_shared<Keyboard>("keyboard"); + keyboard->SetMappingCallback(mapping_callback); + keyboard_factory = std::make_shared<InputFactory>(keyboard); + keyboard_output_factory = std::make_shared<OutputFactory>(keyboard); + Common::Input::RegisterFactory<Common::Input::InputDevice>(keyboard->GetEngineName(), + keyboard_factory); + Common::Input::RegisterFactory<Common::Input::OutputDevice>(keyboard->GetEngineName(), + keyboard_output_factory); + + mouse = std::make_shared<Mouse>("mouse"); + mouse->SetMappingCallback(mapping_callback); + mouse_factory = std::make_shared<InputFactory>(mouse); + mouse_output_factory = std::make_shared<OutputFactory>(mouse); + Common::Input::RegisterFactory<Common::Input::InputDevice>(mouse->GetEngineName(), + mouse_factory); + Common::Input::RegisterFactory<Common::Input::OutputDevice>(mouse->GetEngineName(), + mouse_output_factory); + + touch_screen = std::make_shared<TouchScreen>("touch"); + touch_screen_factory = std::make_shared<InputFactory>(touch_screen); + Common::Input::RegisterFactory<Common::Input::InputDevice>(touch_screen->GetEngineName(), + touch_screen_factory); + + gcadapter = std::make_shared<GCAdapter>("gcpad"); + gcadapter->SetMappingCallback(mapping_callback); + gcadapter_input_factory = std::make_shared<InputFactory>(gcadapter); + gcadapter_output_factory = std::make_shared<OutputFactory>(gcadapter); + Common::Input::RegisterFactory<Common::Input::InputDevice>(gcadapter->GetEngineName(), + gcadapter_input_factory); + Common::Input::RegisterFactory<Common::Input::OutputDevice>(gcadapter->GetEngineName(), + gcadapter_output_factory); + + udp_client = std::make_shared<CemuhookUDP::UDPClient>("cemuhookudp"); + udp_client->SetMappingCallback(mapping_callback); + udp_client_input_factory = std::make_shared<InputFactory>(udp_client); + udp_client_output_factory = std::make_shared<OutputFactory>(udp_client); + Common::Input::RegisterFactory<Common::Input::InputDevice>(udp_client->GetEngineName(), + udp_client_input_factory); + Common::Input::RegisterFactory<Common::Input::OutputDevice>(udp_client->GetEngineName(), + udp_client_output_factory); + + tas_input = std::make_shared<TasInput::Tas>("tas"); + tas_input->SetMappingCallback(mapping_callback); + tas_input_factory = std::make_shared<InputFactory>(tas_input); + tas_output_factory = std::make_shared<OutputFactory>(tas_input); + Common::Input::RegisterFactory<Common::Input::InputDevice>(tas_input->GetEngineName(), + tas_input_factory); + Common::Input::RegisterFactory<Common::Input::OutputDevice>(tas_input->GetEngineName(), + tas_output_factory); #ifdef HAVE_SDL2 - sdl = SDL::Init(); + sdl = std::make_shared<SDLDriver>("sdl"); + sdl->SetMappingCallback(mapping_callback); + sdl_input_factory = std::make_shared<InputFactory>(sdl); + sdl_output_factory = std::make_shared<OutputFactory>(sdl); + Common::Input::RegisterFactory<Common::Input::InputDevice>(sdl->GetEngineName(), + sdl_input_factory); + Common::Input::RegisterFactory<Common::Input::OutputDevice>(sdl->GetEngineName(), + sdl_output_factory); #endif - udp = std::make_shared<InputCommon::CemuhookUDP::Client>(); - udpmotion = std::make_shared<UDPMotionFactory>(udp); - Input::RegisterFactory<Input::MotionDevice>("cemuhookudp", udpmotion); - udptouch = std::make_shared<UDPTouchFactory>(udp); - Input::RegisterFactory<Input::TouchDevice>("cemuhookudp", udptouch); - - mouse = std::make_shared<MouseInput::Mouse>(); - mousebuttons = std::make_shared<MouseButtonFactory>(mouse); - Input::RegisterFactory<Input::ButtonDevice>("mouse", mousebuttons); - mouseanalog = std::make_shared<MouseAnalogFactory>(mouse); - Input::RegisterFactory<Input::AnalogDevice>("mouse", mouseanalog); - mousemotion = std::make_shared<MouseMotionFactory>(mouse); - Input::RegisterFactory<Input::MotionDevice>("mouse", mousemotion); - mousetouch = std::make_shared<MouseTouchFactory>(mouse); - Input::RegisterFactory<Input::TouchDevice>("mouse", mousetouch); - - tas = std::make_shared<TasInput::Tas>(); - tasbuttons = std::make_shared<TasButtonFactory>(tas); - Input::RegisterFactory<Input::ButtonDevice>("tas", tasbuttons); - tasanalog = std::make_shared<TasAnalogFactory>(tas); - Input::RegisterFactory<Input::AnalogDevice>("tas", tasanalog); + Common::Input::RegisterFactory<Common::Input::InputDevice>( + "touch_from_button", std::make_shared<TouchFromButton>()); + Common::Input::RegisterFactory<Common::Input::InputDevice>( + "analog_from_button", std::make_shared<StickFromButton>()); } void Shutdown() { - Input::UnregisterFactory<Input::ButtonDevice>("keyboard"); - Input::UnregisterFactory<Input::MotionDevice>("keyboard"); + Common::Input::UnregisterFactory<Common::Input::InputDevice>(keyboard->GetEngineName()); + Common::Input::UnregisterFactory<Common::Input::OutputDevice>(keyboard->GetEngineName()); keyboard.reset(); - Input::UnregisterFactory<Input::AnalogDevice>("analog_from_button"); - Input::UnregisterFactory<Input::TouchDevice>("touch_from_button"); -#ifdef HAVE_SDL2 - sdl.reset(); -#endif - Input::UnregisterFactory<Input::ButtonDevice>("gcpad"); - Input::UnregisterFactory<Input::AnalogDevice>("gcpad"); - Input::UnregisterFactory<Input::VibrationDevice>("gcpad"); - gcbuttons.reset(); - gcanalog.reset(); - gcvibration.reset(); + Common::Input::UnregisterFactory<Common::Input::InputDevice>(mouse->GetEngineName()); + Common::Input::UnregisterFactory<Common::Input::OutputDevice>(mouse->GetEngineName()); + mouse.reset(); - Input::UnregisterFactory<Input::MotionDevice>("cemuhookudp"); - Input::UnregisterFactory<Input::TouchDevice>("cemuhookudp"); + Common::Input::UnregisterFactory<Common::Input::InputDevice>(touch_screen->GetEngineName()); + touch_screen.reset(); - udpmotion.reset(); - udptouch.reset(); + Common::Input::UnregisterFactory<Common::Input::InputDevice>(gcadapter->GetEngineName()); + Common::Input::UnregisterFactory<Common::Input::OutputDevice>(gcadapter->GetEngineName()); + gcadapter.reset(); - Input::UnregisterFactory<Input::ButtonDevice>("mouse"); - Input::UnregisterFactory<Input::AnalogDevice>("mouse"); - Input::UnregisterFactory<Input::MotionDevice>("mouse"); - Input::UnregisterFactory<Input::TouchDevice>("mouse"); + Common::Input::UnregisterFactory<Common::Input::InputDevice>(udp_client->GetEngineName()); + Common::Input::UnregisterFactory<Common::Input::OutputDevice>(udp_client->GetEngineName()); + udp_client.reset(); - mousebuttons.reset(); - mouseanalog.reset(); - mousemotion.reset(); - mousetouch.reset(); + Common::Input::UnregisterFactory<Common::Input::InputDevice>(tas_input->GetEngineName()); + Common::Input::UnregisterFactory<Common::Input::OutputDevice>(tas_input->GetEngineName()); + tas_input.reset(); - Input::UnregisterFactory<Input::ButtonDevice>("tas"); - Input::UnregisterFactory<Input::AnalogDevice>("tas"); +#ifdef HAVE_SDL2 + Common::Input::UnregisterFactory<Common::Input::InputDevice>(sdl->GetEngineName()); + Common::Input::UnregisterFactory<Common::Input::OutputDevice>(sdl->GetEngineName()); + sdl.reset(); +#endif - tasbuttons.reset(); - tasanalog.reset(); + Common::Input::UnregisterFactory<Common::Input::InputDevice>("touch_from_button"); + Common::Input::UnregisterFactory<Common::Input::InputDevice>("analog_from_button"); } [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const { std::vector<Common::ParamPackage> devices = { - Common::ParamPackage{{"display", "Any"}, {"class", "any"}}, - Common::ParamPackage{{"display", "Keyboard/Mouse"}, {"class", "keyboard"}}, + Common::ParamPackage{{"display", "Any"}, {"engine", "any"}}, }; - if (Settings::values.tas_enable) { - devices.emplace_back( - Common::ParamPackage{{"display", "TAS Controller"}, {"class", "tas"}}); - } + + auto keyboard_devices = keyboard->GetInputDevices(); + devices.insert(devices.end(), keyboard_devices.begin(), keyboard_devices.end()); + auto mouse_devices = mouse->GetInputDevices(); + devices.insert(devices.end(), mouse_devices.begin(), mouse_devices.end()); + auto gcadapter_devices = gcadapter->GetInputDevices(); + devices.insert(devices.end(), gcadapter_devices.begin(), gcadapter_devices.end()); + auto udp_devices = udp_client->GetInputDevices(); + devices.insert(devices.end(), udp_devices.begin(), udp_devices.end()); #ifdef HAVE_SDL2 auto sdl_devices = sdl->GetInputDevices(); devices.insert(devices.end(), sdl_devices.begin(), sdl_devices.end()); #endif - auto udp_devices = udp->GetInputDevices(); - devices.insert(devices.end(), udp_devices.begin(), udp_devices.end()); - auto gcpad_devices = gcadapter->GetInputDevices(); - devices.insert(devices.end(), gcpad_devices.begin(), gcpad_devices.end()); + return devices; } [[nodiscard]] AnalogMapping GetAnalogMappingForDevice( const Common::ParamPackage& params) const { - if (!params.Has("class") || params.Get("class", "") == "any") { + if (!params.Has("engine") || params.Get("engine", "") == "any") { return {}; } - if (params.Get("class", "") == "gcpad") { + const std::string engine = params.Get("engine", ""); + if (engine == mouse->GetEngineName()) { + return mouse->GetAnalogMappingForDevice(params); + } + if (engine == gcadapter->GetEngineName()) { return gcadapter->GetAnalogMappingForDevice(params); } - if (params.Get("class", "") == "tas") { - return tas->GetAnalogMappingForDevice(params); + if (engine == udp_client->GetEngineName()) { + return udp_client->GetAnalogMappingForDevice(params); + } + if (engine == tas_input->GetEngineName()) { + return tas_input->GetAnalogMappingForDevice(params); } #ifdef HAVE_SDL2 - if (params.Get("class", "") == "sdl") { + if (engine == sdl->GetEngineName()) { return sdl->GetAnalogMappingForDevice(params); } #endif @@ -152,17 +179,21 @@ struct InputSubsystem::Impl { [[nodiscard]] ButtonMapping GetButtonMappingForDevice( const Common::ParamPackage& params) const { - if (!params.Has("class") || params.Get("class", "") == "any") { + if (!params.Has("engine") || params.Get("engine", "") == "any") { return {}; } - if (params.Get("class", "") == "gcpad") { + const std::string engine = params.Get("engine", ""); + if (engine == gcadapter->GetEngineName()) { return gcadapter->GetButtonMappingForDevice(params); } - if (params.Get("class", "") == "tas") { - return tas->GetButtonMappingForDevice(params); + if (engine == udp_client->GetEngineName()) { + return udp_client->GetButtonMappingForDevice(params); + } + if (engine == tas_input->GetEngineName()) { + return tas_input->GetButtonMappingForDevice(params); } #ifdef HAVE_SDL2 - if (params.Get("class", "") == "sdl") { + if (engine == sdl->GetEngineName()) { return sdl->GetButtonMappingForDevice(params); } #endif @@ -171,40 +202,119 @@ struct InputSubsystem::Impl { [[nodiscard]] MotionMapping GetMotionMappingForDevice( const Common::ParamPackage& params) const { - if (!params.Has("class") || params.Get("class", "") == "any") { + if (!params.Has("engine") || params.Get("engine", "") == "any") { return {}; } - if (params.Get("class", "") == "cemuhookudp") { - // TODO return the correct motion device - return {}; + const std::string engine = params.Get("engine", ""); + if (engine == udp_client->GetEngineName()) { + return udp_client->GetMotionMappingForDevice(params); } #ifdef HAVE_SDL2 - if (params.Get("class", "") == "sdl") { + if (engine == sdl->GetEngineName()) { return sdl->GetMotionMappingForDevice(params); } #endif return {}; } + Common::Input::ButtonNames GetButtonName(const Common::ParamPackage& params) const { + if (!params.Has("engine") || params.Get("engine", "") == "any") { + return Common::Input::ButtonNames::Undefined; + } + const std::string engine = params.Get("engine", ""); + if (engine == mouse->GetEngineName()) { + return mouse->GetUIName(params); + } + if (engine == gcadapter->GetEngineName()) { + return gcadapter->GetUIName(params); + } + if (engine == udp_client->GetEngineName()) { + return udp_client->GetUIName(params); + } + if (engine == tas_input->GetEngineName()) { + return tas_input->GetUIName(params); + } +#ifdef HAVE_SDL2 + if (engine == sdl->GetEngineName()) { + return sdl->GetUIName(params); + } +#endif + return Common::Input::ButtonNames::Invalid; + } + + bool IsController(const Common::ParamPackage& params) { + const std::string engine = params.Get("engine", ""); + if (engine == mouse->GetEngineName()) { + return true; + } + if (engine == gcadapter->GetEngineName()) { + return true; + } + if (engine == udp_client->GetEngineName()) { + return true; + } + if (engine == tas_input->GetEngineName()) { + return true; + } +#ifdef HAVE_SDL2 + if (engine == sdl->GetEngineName()) { + return true; + } +#endif + return false; + } + + void BeginConfiguration() { + keyboard->BeginConfiguration(); + mouse->BeginConfiguration(); + gcadapter->BeginConfiguration(); + udp_client->BeginConfiguration(); +#ifdef HAVE_SDL2 + sdl->BeginConfiguration(); +#endif + } + + void EndConfiguration() { + keyboard->EndConfiguration(); + mouse->EndConfiguration(); + gcadapter->EndConfiguration(); + udp_client->EndConfiguration(); +#ifdef HAVE_SDL2 + sdl->EndConfiguration(); +#endif + } + + void RegisterInput(MappingData data) { + mapping_factory->RegisterInput(data); + } + + std::shared_ptr<MappingFactory> mapping_factory; + std::shared_ptr<Keyboard> keyboard; + std::shared_ptr<Mouse> mouse; + std::shared_ptr<GCAdapter> gcadapter; + std::shared_ptr<TouchScreen> touch_screen; + std::shared_ptr<TasInput::Tas> tas_input; + std::shared_ptr<CemuhookUDP::UDPClient> udp_client; + + std::shared_ptr<InputFactory> keyboard_factory; + std::shared_ptr<InputFactory> mouse_factory; + std::shared_ptr<InputFactory> gcadapter_input_factory; + std::shared_ptr<InputFactory> touch_screen_factory; + std::shared_ptr<InputFactory> udp_client_input_factory; + std::shared_ptr<InputFactory> tas_input_factory; + + std::shared_ptr<OutputFactory> keyboard_output_factory; + std::shared_ptr<OutputFactory> mouse_output_factory; + std::shared_ptr<OutputFactory> gcadapter_output_factory; + std::shared_ptr<OutputFactory> udp_client_output_factory; + std::shared_ptr<OutputFactory> tas_output_factory; + #ifdef HAVE_SDL2 - std::unique_ptr<SDL::State> sdl; + std::shared_ptr<SDLDriver> sdl; + std::shared_ptr<InputFactory> sdl_input_factory; + std::shared_ptr<OutputFactory> sdl_output_factory; #endif - std::shared_ptr<GCButtonFactory> gcbuttons; - std::shared_ptr<GCAnalogFactory> gcanalog; - std::shared_ptr<GCVibrationFactory> gcvibration; - std::shared_ptr<UDPMotionFactory> udpmotion; - std::shared_ptr<UDPTouchFactory> udptouch; - std::shared_ptr<MouseButtonFactory> mousebuttons; - std::shared_ptr<MouseAnalogFactory> mouseanalog; - std::shared_ptr<MouseMotionFactory> mousemotion; - std::shared_ptr<MouseTouchFactory> mousetouch; - std::shared_ptr<TasButtonFactory> tasbuttons; - std::shared_ptr<TasAnalogFactory> tasanalog; - std::shared_ptr<CemuhookUDP::Client> udp; - std::shared_ptr<GCAdapter::Adapter> gcadapter; - std::shared_ptr<MouseInput::Mouse> mouse; - std::shared_ptr<TasInput::Tas> tas; }; InputSubsystem::InputSubsystem() : impl{std::make_unique<Impl>()} {} @@ -227,20 +337,28 @@ const Keyboard* InputSubsystem::GetKeyboard() const { return impl->keyboard.get(); } -MouseInput::Mouse* InputSubsystem::GetMouse() { +Mouse* InputSubsystem::GetMouse() { return impl->mouse.get(); } -const MouseInput::Mouse* InputSubsystem::GetMouse() const { +const Mouse* InputSubsystem::GetMouse() const { return impl->mouse.get(); } +TouchScreen* InputSubsystem::GetTouchScreen() { + return impl->touch_screen.get(); +} + +const TouchScreen* InputSubsystem::GetTouchScreen() const { + return impl->touch_screen.get(); +} + TasInput::Tas* InputSubsystem::GetTas() { - return impl->tas.get(); + return impl->tas_input.get(); } const TasInput::Tas* InputSubsystem::GetTas() const { - return impl->tas.get(); + return impl->tas_input.get(); } std::vector<Common::ParamPackage> InputSubsystem::GetInputDevices() const { @@ -259,100 +377,30 @@ MotionMapping InputSubsystem::GetMotionMappingForDevice(const Common::ParamPacka return impl->GetMotionMappingForDevice(device); } -GCAnalogFactory* InputSubsystem::GetGCAnalogs() { - return impl->gcanalog.get(); -} - -const GCAnalogFactory* InputSubsystem::GetGCAnalogs() const { - return impl->gcanalog.get(); -} - -GCButtonFactory* InputSubsystem::GetGCButtons() { - return impl->gcbuttons.get(); -} - -const GCButtonFactory* InputSubsystem::GetGCButtons() const { - return impl->gcbuttons.get(); -} - -UDPMotionFactory* InputSubsystem::GetUDPMotions() { - return impl->udpmotion.get(); -} - -const UDPMotionFactory* InputSubsystem::GetUDPMotions() const { - return impl->udpmotion.get(); -} - -UDPTouchFactory* InputSubsystem::GetUDPTouch() { - return impl->udptouch.get(); -} - -const UDPTouchFactory* InputSubsystem::GetUDPTouch() const { - return impl->udptouch.get(); -} - -MouseButtonFactory* InputSubsystem::GetMouseButtons() { - return impl->mousebuttons.get(); +Common::Input::ButtonNames InputSubsystem::GetButtonName(const Common::ParamPackage& params) const { + return impl->GetButtonName(params); } -const MouseButtonFactory* InputSubsystem::GetMouseButtons() const { - return impl->mousebuttons.get(); +bool InputSubsystem::IsController(const Common::ParamPackage& params) const { + return impl->IsController(params); } -MouseAnalogFactory* InputSubsystem::GetMouseAnalogs() { - return impl->mouseanalog.get(); -} - -const MouseAnalogFactory* InputSubsystem::GetMouseAnalogs() const { - return impl->mouseanalog.get(); -} - -MouseMotionFactory* InputSubsystem::GetMouseMotions() { - return impl->mousemotion.get(); -} - -const MouseMotionFactory* InputSubsystem::GetMouseMotions() const { - return impl->mousemotion.get(); -} - -MouseTouchFactory* InputSubsystem::GetMouseTouch() { - return impl->mousetouch.get(); -} - -const MouseTouchFactory* InputSubsystem::GetMouseTouch() const { - return impl->mousetouch.get(); -} - -TasButtonFactory* InputSubsystem::GetTasButtons() { - return impl->tasbuttons.get(); -} - -const TasButtonFactory* InputSubsystem::GetTasButtons() const { - return impl->tasbuttons.get(); -} - -TasAnalogFactory* InputSubsystem::GetTasAnalogs() { - return impl->tasanalog.get(); +void InputSubsystem::ReloadInputDevices() { + impl->udp_client.get()->ReloadSockets(); } -const TasAnalogFactory* InputSubsystem::GetTasAnalogs() const { - return impl->tasanalog.get(); +void InputSubsystem::BeginMapping(Polling::InputType type) { + impl->BeginConfiguration(); + impl->mapping_factory->BeginMapping(type); } -void InputSubsystem::ReloadInputDevices() { - if (!impl->udp) { - return; - } - impl->udp->ReloadSockets(); +const Common::ParamPackage InputSubsystem::GetNextInput() const { + return impl->mapping_factory->GetNextInput(); } -std::vector<std::unique_ptr<Polling::DevicePoller>> InputSubsystem::GetPollers( - [[maybe_unused]] Polling::DeviceType type) const { -#ifdef HAVE_SDL2 - return impl->sdl->GetPollers(type); -#else - return {}; -#endif +void InputSubsystem::StopMapping() const { + impl->EndConfiguration(); + impl->mapping_factory->StopMapping(); } std::string GenerateKeyboardParam(int key_code) { |