gamepad_data_fetcher.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) 2011 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_GAMEPAD_DATA_FETCHER_H_
  5. #define DEVICE_GAMEPAD_GAMEPAD_DATA_FETCHER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/task/sequenced_task_runner.h"
  8. #include "device/gamepad/gamepad_data_fetcher_manager.h"
  9. #include "device/gamepad/gamepad_export.h"
  10. #include "device/gamepad/gamepad_pad_state_provider.h"
  11. #include "device/gamepad/public/cpp/gamepad.h"
  12. #include "device/gamepad/public/mojom/gamepad.mojom-forward.h"
  13. #include "mojo/public/cpp/bindings/pending_receiver.h"
  14. #include "services/device/public/mojom/hid.mojom.h"
  15. namespace device {
  16. // Abstract interface for implementing platform- (and test-) specific behavior
  17. // for getting the gamepad data.
  18. class DEVICE_GAMEPAD_EXPORT GamepadDataFetcher {
  19. public:
  20. GamepadDataFetcher();
  21. virtual ~GamepadDataFetcher();
  22. virtual void GetGamepadData(bool devices_changed_hint) = 0;
  23. virtual void PauseHint(bool paused) {}
  24. virtual void PlayEffect(
  25. int source_id,
  26. mojom::GamepadHapticEffectType,
  27. mojom::GamepadEffectParametersPtr,
  28. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback,
  29. scoped_refptr<base::SequencedTaskRunner>);
  30. virtual void ResetVibration(
  31. int source_id,
  32. mojom::GamepadHapticsManager::ResetVibrationActuatorCallback,
  33. scoped_refptr<base::SequencedTaskRunner>);
  34. virtual GamepadSource source() = 0;
  35. // Shuts down the gamepad with given |source_id| and removes it from the data
  36. // fetchers list of devices. Default implementation used on data fetchers for
  37. // recognized gamepads because it should never be called on those gamepads.
  38. // Returns a boolean that is true if the gamepad was successfully
  39. // disconnected.
  40. virtual bool DisconnectUnrecognizedGamepad(int source_id);
  41. GamepadPadStateProvider* provider() { return provider_; }
  42. PadState* GetPadState(int source_id, bool new_pad_recognized = true) {
  43. if (!provider_)
  44. return nullptr;
  45. return provider_->GetPadState(source(), source_id, new_pad_recognized);
  46. }
  47. // Returns the current time value in microseconds. Data fetchers should use
  48. // the value returned by this method to update the |timestamp| gamepad member.
  49. static int64_t CurrentTimeInMicroseconds();
  50. // Converts a TimeTicks value to a timestamp in microseconds, as used for
  51. // the |timestamp| gamepad member.
  52. static int64_t TimeInMicroseconds(base::TimeTicks update_time);
  53. // Perform one-time string initialization on the gamepad state in |pad|.
  54. static void UpdateGamepadStrings(const std::string& product_name,
  55. uint16_t vendor_id,
  56. uint16_t product_id,
  57. bool has_standard_mapping,
  58. Gamepad& pad);
  59. // Call a vibration callback on the same sequence that the vibration command
  60. // was issued on.
  61. static void RunVibrationCallback(
  62. base::OnceCallback<void(mojom::GamepadHapticsResult)> callback,
  63. scoped_refptr<base::SequencedTaskRunner> callback_runner,
  64. mojom::GamepadHapticsResult result);
  65. // Sets a global callback for GamepadProviders to use when binding a
  66. // HidManager interface.
  67. using HidManagerBinder =
  68. base::RepeatingCallback<void(mojo::PendingReceiver<mojom::HidManager>)>;
  69. static void SetHidManagerBinder(HidManagerBinder binder);
  70. protected:
  71. friend GamepadPadStateProvider;
  72. // To be called by the GamepadPadStateProvider on the polling thread.
  73. void InitializeProvider(GamepadPadStateProvider* provider);
  74. // Binds a HidManager interface.
  75. void BindHidManager(mojo::PendingReceiver<mojom::HidManager> receiver);
  76. // This call will happen on the gamepad polling thread. Any initialization
  77. // that needs to happen on that thread should be done here, not in the
  78. // constructor.
  79. virtual void OnAddedToProvider() {}
  80. private:
  81. // GamepadPadStateProvider is the base class of GamepadProvider, which owns
  82. // this data fetcher.
  83. raw_ptr<GamepadPadStateProvider> provider_ = nullptr;
  84. };
  85. // Factory class for creating a GamepadDataFetcher. Used by the
  86. // GamepadDataFetcherManager.
  87. class DEVICE_GAMEPAD_EXPORT GamepadDataFetcherFactory {
  88. public:
  89. GamepadDataFetcherFactory();
  90. virtual ~GamepadDataFetcherFactory() {}
  91. virtual std::unique_ptr<GamepadDataFetcher> CreateDataFetcher() = 0;
  92. virtual GamepadSource source() = 0;
  93. };
  94. // Basic factory implementation for GamepadDataFetchers without a complex
  95. // constructor.
  96. template <typename DataFetcherType, GamepadSource DataFetcherSource>
  97. class GamepadDataFetcherFactoryImpl : public GamepadDataFetcherFactory {
  98. public:
  99. ~GamepadDataFetcherFactoryImpl() override {}
  100. std::unique_ptr<GamepadDataFetcher> CreateDataFetcher() override {
  101. return std::unique_ptr<GamepadDataFetcher>(new DataFetcherType());
  102. }
  103. GamepadSource source() override { return DataFetcherSource; }
  104. static GamepadSource static_source() { return DataFetcherSource; }
  105. };
  106. } // namespace device
  107. #endif // DEVICE_GAMEPAD_GAMEPAD_DATA_FETCHER_H_