gamepad_service.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_SERVICE_H_
  5. #define DEVICE_GAMEPAD_GAMEPAD_SERVICE_H_
  6. #include <memory>
  7. #include <set>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/bind.h"
  12. #include "base/callback_forward.h"
  13. #include "base/memory/read_only_shared_memory_region.h"
  14. #include "base/memory/singleton.h"
  15. #include "base/sequence_checker.h"
  16. #include "device/gamepad/gamepad_data_fetcher.h"
  17. #include "device/gamepad/gamepad_export.h"
  18. #include "device/gamepad/gamepad_provider.h"
  19. #include "device/gamepad/public/mojom/gamepad.mojom-forward.h"
  20. namespace device {
  21. class GamepadConsumer;
  22. class GamepadProvider;
  23. // Owns the GamepadProvider (the background polling thread) and keeps track of
  24. // the number of consumers currently using the data (and pausing the provider
  25. // when not in use).
  26. class DEVICE_GAMEPAD_EXPORT GamepadService : public GamepadChangeClient {
  27. public:
  28. // Returns the GamepadService singleton.
  29. static GamepadService* GetInstance();
  30. // Sets the GamepadService instance. Exposed for tests.
  31. static void SetInstance(GamepadService*);
  32. // Initializes the GamepadService. |hid_manager_binder| should be callable
  33. // from any sequence.
  34. void StartUp(GamepadDataFetcher::HidManagerBinder hid_manager_binder);
  35. // Increments the number of users of the provider. The Provider is running
  36. // when there's > 0 users, and is paused when the count drops to 0.
  37. // |consumer| is registered to listen for gamepad connections. If this is the
  38. // first time it is added to the set of consumers it will be treated
  39. // specially: it will not be informed about connections before a new user
  40. // gesture is observed at which point it will be notified for every connected
  41. // gamepads.
  42. //
  43. // Returns true on success. If |consumer| is already active, returns false and
  44. // exits without modifying the consumer set.
  45. //
  46. // Must be called on the I/O thread.
  47. bool ConsumerBecameActive(GamepadConsumer* consumer);
  48. // Decrements the number of users of the provider. |consumer| will not be
  49. // informed about connections until it's added back via ConsumerBecameActive.
  50. //
  51. // Returns true on success. If |consumer| is not in the consumer set or is
  52. // already inactive, returns false and exits without modifying the consumer
  53. // set.
  54. //
  55. // Must be called on the I/O thread.
  56. bool ConsumerBecameInactive(GamepadConsumer* consumer);
  57. // Decrements the number of users of the provider and removes |consumer| from
  58. // the set of consumers. Should be matched with a a ConsumerBecameActive
  59. // call.
  60. //
  61. // Returns true on success, or false if |consumer| was not in the consumer
  62. // set.
  63. //
  64. // Must be called on the I/O thread.
  65. bool RemoveConsumer(GamepadConsumer* consumer);
  66. // Registers the given closure for calling when the user has interacted with
  67. // the device. This callback will only be issued once. Should only be called
  68. // while a consumer is active.
  69. void RegisterForUserGesture(base::OnceClosure closure);
  70. // Returns a duplicate of the shared memory region of the gamepad data.
  71. base::ReadOnlySharedMemoryRegion DuplicateSharedMemoryRegion();
  72. // Stop/join with the background thread in GamepadProvider |provider_|.
  73. void Terminate();
  74. // Called on IO thread when a gamepad is connected.
  75. void OnGamepadConnected(uint32_t index, const Gamepad& pad);
  76. // Called on IO thread when a gamepad is disconnected.
  77. void OnGamepadDisconnected(uint32_t index, const Gamepad& pad);
  78. // Request playback of a haptic effect on the specified gamepad. Once effect
  79. // playback is complete or is preempted by a different effect, the callback
  80. // will be called.
  81. void PlayVibrationEffectOnce(
  82. uint32_t pad_index,
  83. mojom::GamepadHapticEffectType,
  84. mojom::GamepadEffectParametersPtr,
  85. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback);
  86. // Resets the state of the vibration actuator on the specified gamepad. If any
  87. // effects are currently being played, they are preempted and vibration is
  88. // stopped.
  89. void ResetVibrationActuator(
  90. uint32_t pad_index,
  91. mojom::GamepadHapticsManager::ResetVibrationActuatorCallback);
  92. // Constructor for testing. This specifies the data fetcher to use for a
  93. // provider, bypassing the default platform one.
  94. GamepadService(std::unique_ptr<GamepadDataFetcher> fetcher);
  95. GamepadService(const GamepadService&) = delete;
  96. GamepadService& operator=(const GamepadService&) = delete;
  97. virtual ~GamepadService();
  98. private:
  99. friend struct base::DefaultSingletonTraits<GamepadService>;
  100. friend class GamepadServiceTest;
  101. GamepadService();
  102. void OnUserGesture();
  103. // GamepadChangeClient implementation.
  104. void OnGamepadChange(mojom::GamepadChangesPtr change) override;
  105. void OnGamepadConnectionChange(bool connected,
  106. uint32_t index,
  107. const Gamepad& pad) override;
  108. void SetSanitizationEnabled(bool sanitize);
  109. struct ConsumerInfo {
  110. ConsumerInfo(GamepadConsumer* consumer) : consumer(consumer) {}
  111. bool operator<(const ConsumerInfo& other) const {
  112. return consumer < other.consumer;
  113. }
  114. GamepadConsumer* consumer;
  115. mutable bool is_active = false;
  116. mutable bool did_observe_user_gesture = false;
  117. };
  118. std::unique_ptr<GamepadProvider> provider_;
  119. SEQUENCE_CHECKER(sequence_checker_);
  120. typedef std::set<ConsumerInfo> ConsumerSet;
  121. ConsumerSet consumers_;
  122. typedef std::unordered_map<GamepadConsumer*, std::vector<bool>>
  123. ConsumerConnectedStateMap;
  124. ConsumerConnectedStateMap inactive_consumer_state_;
  125. // The number of active consumers in |consumers_|.
  126. int num_active_consumers_ = 0;
  127. bool gesture_callback_pending_ = false;
  128. };
  129. } // namespace device
  130. #endif // DEVICE_GAMEPAD_GAMEPAD_SERVICE_H_