main.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <thread>
  6. #include "common/param_package.h"
  7. #include "input_common/analog_from_button.h"
  8. #include "input_common/keyboard.h"
  9. #include "input_common/main.h"
  10. #include "input_common/motion_emu.h"
  11. #ifdef HAVE_SDL2
  12. #include "input_common/sdl/sdl.h"
  13. #endif
  14. namespace InputCommon {
  15. static std::shared_ptr<Keyboard> keyboard;
  16. static std::shared_ptr<MotionEmu> motion_emu;
  17. #ifdef HAVE_SDL2
  18. static std::thread poll_thread;
  19. #endif
  20. void Init() {
  21. keyboard = std::make_shared<Keyboard>();
  22. Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard);
  23. Input::RegisterFactory<Input::AnalogDevice>("analog_from_button",
  24. std::make_shared<AnalogFromButton>());
  25. motion_emu = std::make_shared<MotionEmu>();
  26. Input::RegisterFactory<Input::MotionDevice>("motion_emu", motion_emu);
  27. #ifdef HAVE_SDL2
  28. SDL::Init();
  29. #endif
  30. }
  31. void StartJoystickEventHandler() {
  32. #ifdef HAVE_SDL2
  33. poll_thread = std::thread(SDL::PollLoop);
  34. #endif
  35. }
  36. void Shutdown() {
  37. Input::UnregisterFactory<Input::ButtonDevice>("keyboard");
  38. keyboard.reset();
  39. Input::UnregisterFactory<Input::AnalogDevice>("analog_from_button");
  40. Input::UnregisterFactory<Input::MotionDevice>("motion_emu");
  41. motion_emu.reset();
  42. #ifdef HAVE_SDL2
  43. SDL::Shutdown();
  44. poll_thread.join();
  45. #endif
  46. }
  47. Keyboard* GetKeyboard() {
  48. return keyboard.get();
  49. }
  50. MotionEmu* GetMotionEmu() {
  51. return motion_emu.get();
  52. }
  53. std::string GenerateKeyboardParam(int key_code) {
  54. Common::ParamPackage param{
  55. {"engine", "keyboard"},
  56. {"code", std::to_string(key_code)},
  57. };
  58. return param.Serialize();
  59. }
  60. std::string GenerateAnalogParamFromKeys(int key_up, int key_down, int key_left, int key_right,
  61. int key_modifier, float modifier_scale) {
  62. Common::ParamPackage circle_pad_param{
  63. {"engine", "analog_from_button"},
  64. {"up", GenerateKeyboardParam(key_up)},
  65. {"down", GenerateKeyboardParam(key_down)},
  66. {"left", GenerateKeyboardParam(key_left)},
  67. {"right", GenerateKeyboardParam(key_right)},
  68. {"modifier", GenerateKeyboardParam(key_modifier)},
  69. {"modifier_scale", std::to_string(modifier_scale)},
  70. };
  71. return circle_pad_param.Serialize();
  72. }
  73. namespace Polling {
  74. std::vector<std::unique_ptr<DevicePoller>> GetPollers(DeviceType type) {
  75. #ifdef HAVE_SDL2
  76. return SDL::Polling::GetPollers(type);
  77. #else
  78. return {};
  79. #endif
  80. }
  81. } // namespace Polling
  82. } // namespace InputCommon