gamepad.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 COMPONENTS_EXO_GAMEPAD_H_
  5. #define COMPONENTS_EXO_GAMEPAD_H_
  6. #include <vector>
  7. #include "base/observer_list.h"
  8. #include "base/timer/timer.h"
  9. #include "components/exo/gamepad_delegate.h"
  10. #include "components/exo/gamepad_observer.h"
  11. #include "ui/events/devices/gamepad_device.h"
  12. #include "ui/events/ozone/gamepad/gamepad_event.h"
  13. #include "ui/ozone/public/input_controller.h"
  14. #include "ui/ozone/public/ozone_platform.h"
  15. namespace exo {
  16. // Maximum force feedback duration supported by Linux.
  17. constexpr int64_t kMaxDurationMillis = 0xFFFF;
  18. // This class represents one gamepad. It allows control over the gamepad's
  19. // vibration and provides focus tracking for the gamepad.
  20. class Gamepad {
  21. public:
  22. explicit Gamepad(const ui::GamepadDevice& gamepad_device);
  23. Gamepad(const Gamepad& other) = delete;
  24. Gamepad& operator=(const Gamepad& other) = delete;
  25. // The destructor also informs GamepadObservers and GamepadDelegate when a
  26. // gamepad has been disconnected.
  27. virtual ~Gamepad();
  28. // Controls vibration effects on the gamepad.
  29. // The duration_millis/amplitude pairs determine the duration and strength of
  30. // the vibration. Note that the two vectors have to be the same size.
  31. // The repeat value determines the index of the duration_millis (or
  32. // amplitudes) vector at which the pattern to repeat begins. If repeat is
  33. // enabled, the vibration pattern will repeat indefinitely until the vibration
  34. // event is canceled. A repeat value of -1 disables repeat.
  35. // The user does not have to explicitly call CancelVibration() at the end of
  36. // every vibration call. However, if Vibrate() is called when there is an
  37. // ongoing vibration, the existing vibration is automatically interrupted and
  38. // canceled. The gamepad has to be focused in order for the gamepad to
  39. // vibrate. If focus is lost when there is an ongoing vibration, the vibration
  40. // is canceled automatically.
  41. void Vibrate(const std::vector<int64_t>& duration_millis,
  42. const std::vector<uint8_t>& amplitudes,
  43. int32_t repeat);
  44. void CancelVibration();
  45. // The GamepadDelegate is not owned by Gamepad. The delegate must stay alive
  46. // until OnRemoved is called.
  47. void SetDelegate(std::unique_ptr<GamepadDelegate> delegate);
  48. // Manages the GamepadObserver list. GamepadObservers are notified when the
  49. // gamepad is being destroyed.
  50. void AddObserver(GamepadObserver* observer);
  51. bool HasObserver(GamepadObserver* observer) const;
  52. void RemoveObserver(GamepadObserver* observer);
  53. // Informs the gamepad when window focus changes; focus changes determine
  54. // whether a gamepad is allowed to vibrate at any given time.
  55. void OnGamepadFocused();
  56. void OnGamepadFocusLost();
  57. // Forwards gamepad events to the corresponding GamepadDelegate calls.
  58. void OnGamepadEvent(const ui::GamepadEvent& event);
  59. const ui::GamepadDevice device;
  60. private:
  61. // Private method for handling vibration patterns. Handles repeat and
  62. // breaking down of vibration events by iterating through duration/amplitude
  63. // vectors. Also provides handling for a vibration event that exceeds the
  64. // maximum force feedback duration supported by Linux.
  65. void HandleVibrate(const std::vector<int64_t>& duration_millis,
  66. const std::vector<uint8_t>& amplitudes,
  67. int32_t repeat,
  68. size_t start_index,
  69. int64_t duration_already_vibrated);
  70. // These methods forward vibration calls to |input_controller_|.
  71. // They are virtual for testing purposes.
  72. virtual void SendVibrate(uint8_t amplitude, int64_t duration_millis);
  73. virtual void SendCancelVibration();
  74. // Keeps track of whether the gamepad is allowed to vibrate at any given
  75. // time.
  76. bool can_vibrate_ = false;
  77. std::unique_ptr<GamepadDelegate> delegate_;
  78. base::ObserverList<GamepadObserver>::Unchecked observer_list_;
  79. // Methods to control gamepad vibration are routed through InputController.
  80. ui::InputController* input_controller_;
  81. // A timer to keep track of vibration requests.
  82. base::OneShotTimer vibration_timer_;
  83. };
  84. } // namespace exo
  85. #endif // COMPONENTS_EXO_GAMEPAD_H_