gamepad_provider.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (c) 2012 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_PROVIDER_H_
  5. #define DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_
  6. #include <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/read_only_shared_memory_region.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/synchronization/lock.h"
  14. #include "base/system/system_monitor.h"
  15. #include "base/time/time.h"
  16. #include "device/gamepad/gamepad_export.h"
  17. #include "device/gamepad/gamepad_pad_state_provider.h"
  18. #include "device/gamepad/gamepad_shared_buffer.h"
  19. #include "device/gamepad/public/cpp/gamepads.h"
  20. #include "device/gamepad/public/mojom/gamepad.mojom.h"
  21. #include "mojo/public/cpp/system/buffer.h"
  22. namespace base {
  23. class SingleThreadTaskRunner;
  24. class Thread;
  25. } // namespace base
  26. namespace device {
  27. class GamepadDataFetcher;
  28. class DEVICE_GAMEPAD_EXPORT GamepadChangeClient {
  29. public:
  30. virtual void OnGamepadConnectionChange(bool connected,
  31. uint32_t index,
  32. const Gamepad& pad) = 0;
  33. virtual void OnGamepadChange(mojom::GamepadChangesPtr changes) = 0;
  34. };
  35. class DEVICE_GAMEPAD_EXPORT GamepadProvider
  36. : public GamepadPadStateProvider,
  37. public base::SystemMonitor::DevicesChangedObserver {
  38. public:
  39. explicit GamepadProvider(GamepadChangeClient* gamepad_change_client);
  40. // Manually specifies the data fetcher and polling thread. The polling thread
  41. // will be created normally if |polling_thread| is nullptr. Used for testing.
  42. GamepadProvider(GamepadChangeClient* gamepad_change_client,
  43. std::unique_ptr<GamepadDataFetcher> fetcher,
  44. std::unique_ptr<base::Thread> polling_thread);
  45. GamepadProvider(const GamepadProvider&) = delete;
  46. GamepadProvider& operator=(const GamepadProvider&) = delete;
  47. ~GamepadProvider() override;
  48. // Returns a duplicate of the shared memory region of the gamepad data.
  49. base::ReadOnlySharedMemoryRegion DuplicateSharedMemoryRegion();
  50. void GetCurrentGamepadData(Gamepads* data);
  51. void PlayVibrationEffectOnce(
  52. uint32_t pad_index,
  53. mojom::GamepadHapticEffectType,
  54. mojom::GamepadEffectParametersPtr,
  55. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback);
  56. void ResetVibrationActuator(
  57. uint32_t pad_index,
  58. mojom::GamepadHapticsManager::ResetVibrationActuatorCallback);
  59. // Pause and resume the background polling thread. Can be called from any
  60. // thread.
  61. void Pause();
  62. void Resume();
  63. // Registers the given closure for calling when the user has interacted with
  64. // the device. This callback will only be issued once.
  65. void RegisterForUserGesture(base::OnceClosure closure);
  66. // base::SystemMonitor::DevicesChangedObserver implementation.
  67. void OnDevicesChanged(base::SystemMonitor::DeviceType type) override;
  68. // Add a gamepad data fetcher. Takes ownership of |fetcher|.
  69. void AddGamepadDataFetcher(std::unique_ptr<GamepadDataFetcher> fetcher);
  70. // Remove gamepad data fetchers with the given source.
  71. void RemoveSourceGamepadDataFetcher(GamepadSource source);
  72. void SetSanitizationEnabled(bool sanitize) { sanitize_ = sanitize; }
  73. private:
  74. void Initialize(std::unique_ptr<GamepadDataFetcher> fetcher);
  75. // Method for setting up the platform-specific data fetcher. Takes ownership
  76. // of |fetcher|.
  77. void DoAddGamepadDataFetcher(std::unique_ptr<GamepadDataFetcher> fetcher);
  78. void DoRemoveSourceGamepadDataFetcher(GamepadSource source);
  79. GamepadDataFetcher* GetSourceGamepadDataFetcher(GamepadSource source);
  80. // Method for sending pause hints to the low-level data fetcher. Runs on
  81. // polling_thread_.
  82. void SendPauseHint(bool paused);
  83. // Method for polling a GamepadDataFetcher. Runs on the polling_thread_.
  84. void DoPoll();
  85. void ScheduleDoPoll();
  86. void SendChangeEvents(mojom::GamepadChangesPtr changes);
  87. void OnGamepadConnectionChange(bool connected,
  88. uint32_t index,
  89. const Gamepad& pad);
  90. // Checks the gamepad state to see if the user has interacted with it. Returns
  91. // true if any user gesture observers were notified.
  92. bool CheckForUserGesture();
  93. // GamepadPadStateProvider implementation.
  94. void DisconnectUnrecognizedGamepad(GamepadSource source,
  95. int source_id) override;
  96. void PlayEffectOnPollingThread(
  97. uint32_t pad_index,
  98. mojom::GamepadHapticEffectType,
  99. mojom::GamepadEffectParametersPtr,
  100. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback,
  101. scoped_refptr<base::SequencedTaskRunner>);
  102. void ResetVibrationOnPollingThread(
  103. uint32_t pad_index,
  104. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback,
  105. scoped_refptr<base::SequencedTaskRunner>);
  106. // The duration of the delay between iterations of DoPoll.
  107. base::TimeDelta sampling_interval_delta_;
  108. // Keeps track of when the background thread is paused. Access to is_paused_
  109. // must be guarded by is_paused_lock_.
  110. base::Lock is_paused_lock_;
  111. bool is_paused_ = true;
  112. // Keep track of when a polling task is schedlued, so as to prevent us from
  113. // accidentally scheduling more than one at any time, when rapidly toggling
  114. // |is_paused_|.
  115. bool have_scheduled_do_poll_ = false;
  116. // Lists all observers registered for user gestures, and the thread which
  117. // to issue the callbacks on. Since we always issue the callback on the
  118. // thread which the registration happened, and this class lives on the I/O
  119. // thread, the message loop proxies will normally just be the I/O thread.
  120. // However, this will be the main thread for unit testing.
  121. base::Lock user_gesture_lock_;
  122. using ClosureAndThread =
  123. std::pair<base::OnceClosure, scoped_refptr<base::SingleThreadTaskRunner>>;
  124. using UserGestureObserverVector = std::vector<ClosureAndThread>;
  125. UserGestureObserverVector user_gesture_observers_;
  126. // Updated based on notification from SystemMonitor when the system devices
  127. // have been updated, and this notification is passed on to the data fetcher
  128. // to enable it to avoid redundant (and possibly expensive) is-connected
  129. // tests. Access to devices_changed_ must be guarded by
  130. // devices_changed_lock_.
  131. base::Lock devices_changed_lock_;
  132. bool devices_changed_ = true;
  133. bool ever_had_user_gesture_ = false;
  134. bool sanitize_ = true;
  135. // Only used on the polling thread.
  136. using GamepadFetcherVector = std::vector<std::unique_ptr<GamepadDataFetcher>>;
  137. GamepadFetcherVector data_fetchers_;
  138. base::Lock shared_memory_lock_;
  139. std::unique_ptr<GamepadSharedBuffer> gamepad_shared_buffer_;
  140. // Polling is done on this background thread.
  141. std::unique_ptr<base::Thread> polling_thread_;
  142. scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
  143. raw_ptr<GamepadChangeClient> gamepad_change_client_;
  144. };
  145. } // namespace device
  146. #endif // DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_