wgi_data_fetcher_win.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef DEVICE_GAMEPAD_WGI_DATA_FETCHER_WIN_H_
  5. #define DEVICE_GAMEPAD_WGI_DATA_FETCHER_WIN_H_
  6. #include "device/gamepad/gamepad_data_fetcher.h"
  7. #include <Windows.Gaming.Input.h>
  8. #include <wrl/event.h>
  9. #include <memory>
  10. #include "base/callback_forward.h"
  11. #include "base/containers/flat_map.h"
  12. #include "base/memory/scoped_refptr.h"
  13. #include "base/no_destructor.h"
  14. #include "base/sequence_checker.h"
  15. #include "base/task/sequenced_task_runner.h"
  16. #include "device/gamepad/public/mojom/gamepad.mojom.h"
  17. #include "device/gamepad/wgi_gamepad_device.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace device {
  20. class DEVICE_GAMEPAD_EXPORT WgiDataFetcherWin final
  21. : public GamepadDataFetcher {
  22. public:
  23. enum class InitializationState {
  24. kUninitialized,
  25. kInitialized,
  26. kAddGamepadAddedFailed,
  27. kAddGamepadRemovedFailed,
  28. kRoGetActivationFactoryFailed,
  29. kCoreWinrtStringDelayLoadFailed,
  30. };
  31. using Factory =
  32. GamepadDataFetcherFactoryImpl<WgiDataFetcherWin, GAMEPAD_SOURCE_WIN_WGI>;
  33. // Define test hooks to use a fake WinRT RoGetActivationFactory
  34. // implementation to avoid dependencies on the OS for WGI testing.
  35. using GetActivationFactoryFunction = HRESULT (*)(HSTRING class_id,
  36. const IID& iid,
  37. void** out_factory);
  38. WgiDataFetcherWin();
  39. WgiDataFetcherWin(const WgiDataFetcherWin&) = delete;
  40. WgiDataFetcherWin& operator=(const WgiDataFetcherWin&) = delete;
  41. ~WgiDataFetcherWin() override;
  42. // GamepadDataFetcher implementation.
  43. GamepadSource source() override;
  44. void OnAddedToProvider() override;
  45. void GetGamepadData(bool devices_changed_hint) override;
  46. void PlayEffect(int source_id,
  47. mojom::GamepadHapticEffectType,
  48. mojom::GamepadEffectParametersPtr,
  49. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback,
  50. scoped_refptr<base::SequencedTaskRunner>) override;
  51. void ResetVibration(
  52. int source_id,
  53. mojom::GamepadHapticsManager::ResetVibrationActuatorCallback,
  54. scoped_refptr<base::SequencedTaskRunner>) override;
  55. // Set fake ActivationFunction for test to avoid dependencies on the OS API.
  56. using ActivationFactoryFunctionCallback =
  57. base::RepeatingCallback<GetActivationFactoryFunction()>;
  58. static void OverrideActivationFactoryFunctionForTesting(
  59. ActivationFactoryFunctionCallback callback);
  60. // Used to store gamepad devices indexed by its source id.
  61. using DeviceMap = base::flat_map<int, std::unique_ptr<WgiGamepadDevice>>;
  62. const DeviceMap& GetGamepadsForTesting() const { return devices_; }
  63. InitializationState GetInitializationState() const;
  64. private:
  65. // Set the state of the new connected gamepad to initialized, update
  66. // gamepad state connection status, and add a new controller mapping for
  67. // `gamepad` to `gamepads_` on gamepad polling thread.
  68. void OnGamepadAdded(IInspectable* /* sender */,
  69. ABI::Windows::Gaming::Input::IGamepad* gamepad);
  70. // Remove the corresponding controller mapping of `gamepad` in `gamepads_`
  71. // on gamepad polling thread.
  72. void OnGamepadRemoved(IInspectable* /* sender */,
  73. ABI::Windows::Gaming::Input::IGamepad* gamepad);
  74. static ActivationFactoryFunctionCallback&
  75. GetActivationFactoryFunctionCallback();
  76. std::u16string GetGamepadDisplayName(
  77. ABI::Windows::Gaming::Input::IGamepad* gamepad);
  78. Microsoft::WRL::ComPtr<ABI::Windows::Gaming::Input::IRawGameController>
  79. GetRawGameController(ABI::Windows::Gaming::Input::IGamepad* gamepad);
  80. void UnregisterEventHandlers();
  81. int next_source_id_ = 0;
  82. InitializationState initialization_state_ =
  83. InitializationState::kUninitialized;
  84. DeviceMap devices_;
  85. Microsoft::WRL::ComPtr<ABI::Windows::Gaming::Input::IGamepadStatics>
  86. gamepad_statics_;
  87. GetActivationFactoryFunction get_activation_factory_function_;
  88. absl::optional<EventRegistrationToken> added_event_token_;
  89. absl::optional<EventRegistrationToken> removed_event_token_;
  90. SEQUENCE_CHECKER(sequence_checker_);
  91. base::WeakPtrFactory<WgiDataFetcherWin> weak_factory_{this};
  92. };
  93. } // namespace device
  94. #endif // DEVICE_GAMEPAD_WGI_DATA_FETCHER_WIN_H_