main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "input_common/udp/udp.h"
  12. #ifdef HAVE_SDL2
  13. #include "input_common/sdl/sdl.h"
  14. #endif
  15. namespace InputCommon {
  16. static std::shared_ptr<Keyboard> keyboard;
  17. static std::shared_ptr<MotionEmu> motion_emu;
  18. #ifdef HAVE_SDL2
  19. static std::unique_ptr<SDL::State> sdl;
  20. #endif
  21. static std::unique_ptr<CemuhookUDP::State> udp;
  22. void Init() {
  23. keyboard = std::make_shared<Keyboard>();
  24. Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard);
  25. Input::RegisterFactory<Input::AnalogDevice>("analog_from_button",
  26. std::make_shared<AnalogFromButton>());
  27. motion_emu = std::make_shared<MotionEmu>();
  28. Input::RegisterFactory<Input::MotionDevice>("motion_emu", motion_emu);
  29. #ifdef HAVE_SDL2
  30. sdl = SDL::Init();
  31. #endif
  32. udp = CemuhookUDP::Init();
  33. }
  34. void Shutdown() {
  35. Input::UnregisterFactory<Input::ButtonDevice>("keyboard");
  36. keyboard.reset();
  37. Input::UnregisterFactory<Input::AnalogDevice>("analog_from_button");
  38. Input::UnregisterFactory<Input::MotionDevice>("motion_emu");
  39. motion_emu.reset();
  40. #ifdef HAVE_SDL2
  41. sdl.reset();
  42. #endif
  43. udp.reset();
  44. }
  45. Keyboard* GetKeyboard() {
  46. return keyboard.get();
  47. }
  48. MotionEmu* GetMotionEmu() {
  49. return motion_emu.get();
  50. }
  51. std::string GenerateKeyboardParam(int key_code) {
  52. Common::ParamPackage param{
  53. {"engine", "keyboard"},
  54. {"code", std::to_string(key_code)},
  55. };
  56. return param.Serialize();
  57. }
  58. std::string GenerateAnalogParamFromKeys(int key_up, int key_down, int key_left, int key_right,
  59. int key_modifier, float modifier_scale) {
  60. Common::ParamPackage circle_pad_param{
  61. {"engine", "analog_from_button"},
  62. {"up", GenerateKeyboardParam(key_up)},
  63. {"down", GenerateKeyboardParam(key_down)},
  64. {"left", GenerateKeyboardParam(key_left)},
  65. {"right", GenerateKeyboardParam(key_right)},
  66. {"modifier", GenerateKeyboardParam(key_modifier)},
  67. {"modifier_scale", std::to_string(modifier_scale)},
  68. };
  69. return circle_pad_param.Serialize();
  70. }
  71. namespace Polling {
  72. std::vector<std::unique_ptr<DevicePoller>> GetPollers(DeviceType type) {
  73. std::vector<std::unique_ptr<DevicePoller>> pollers;
  74. #ifdef HAVE_SDL2
  75. pollers = sdl->GetPollers(type);
  76. #endif
  77. return pollers;
  78. }
  79. } // namespace Polling
  80. } // namespace InputCommon