abstract_haptic_gamepad.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2017 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_ABSTRACT_HAPTIC_GAMEPAD_H_
  5. #define DEVICE_GAMEPAD_ABSTRACT_HAPTIC_GAMEPAD_H_
  6. #include "base/memory/scoped_refptr.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/task/sequenced_task_runner.h"
  9. #include "base/threading/thread_checker.h"
  10. #include "device/gamepad/gamepad_export.h"
  11. #include "device/gamepad/public/mojom/gamepad.mojom.h"
  12. namespace device {
  13. // AbstractHapticGamepad is a base class for gamepads that support dual-rumble
  14. // vibration effects. To use it, override the SetVibration method so that it
  15. // sets the vibration intensity on the device. Then, calling PlayEffect or
  16. // ResetVibration will call your SetVibration method at the appropriate times
  17. // to produce the desired vibration effect. When the effect is complete, or when
  18. // it has been preempted by another effect, the callback is invoked with a
  19. // result code.
  20. //
  21. // By default, SetZeroVibration simply calls SetVibration with both parameters
  22. // set to zero. You may optionally override SetZeroVibration if the device has a
  23. // more efficient means of stopping an ongoing effect.
  24. class DEVICE_GAMEPAD_EXPORT AbstractHapticGamepad {
  25. public:
  26. AbstractHapticGamepad();
  27. virtual ~AbstractHapticGamepad();
  28. // Start playing a haptic effect of type |type|, described by |params|. When
  29. // the effect is complete, or if it encounters an error, the result code is
  30. // passed back to the caller on its own sequence by calling |callback| using
  31. // |callback_runner|.
  32. void PlayEffect(
  33. mojom::GamepadHapticEffectType type,
  34. mojom::GamepadEffectParametersPtr params,
  35. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback callback,
  36. scoped_refptr<base::SequencedTaskRunner> callback_runner);
  37. // Reset vibration on the gamepad, perhaps interrupting an ongoing effect. A
  38. // result code is passed back to the caller on its own sequence by calling
  39. // |callback| using |callback_runner|.
  40. void ResetVibration(
  41. mojom::GamepadHapticsManager::ResetVibrationActuatorCallback callback,
  42. scoped_refptr<base::SequencedTaskRunner> callback_runner);
  43. // Stop vibration effects, run callbacks, and release held resources. Must be
  44. // called exactly once before the device is destroyed.
  45. void Shutdown();
  46. // Returns true if Shutdown() has been called.
  47. bool IsShuttingDown() { return is_shutting_down_; }
  48. // Set the vibration magnitude for the strong and weak vibration actuators.
  49. virtual void SetVibration(mojom::GamepadEffectParametersPtr params) = 0;
  50. // Set the vibration magnitude for both actuators to zero.
  51. virtual void SetZeroVibration();
  52. // The maximum effect duration supported by this device. Long-running effects
  53. // must be divided into effects of this duration or less.
  54. virtual double GetMaxEffectDurationMillis();
  55. virtual base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() = 0;
  56. private:
  57. // Override to perform additional shutdown actions after vibration effects
  58. // are halted and callbacks are issued.
  59. virtual void DoShutdown() {}
  60. void PlayVibrationEffect(int sequence_id,
  61. mojom::GamepadEffectParametersPtr params);
  62. void StartVibration(int sequence_id,
  63. double duration,
  64. mojom::GamepadEffectParametersPtr params);
  65. void FinishEffect(int sequence_id);
  66. bool is_shutting_down_ = false;
  67. bool is_shut_down_ = false;
  68. int sequence_id_ = 0;
  69. mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback
  70. playing_effect_callback_;
  71. scoped_refptr<base::SequencedTaskRunner> callback_runner_;
  72. THREAD_CHECKER(thread_checker_);
  73. };
  74. } // namespace device
  75. #endif // DEVICE_GAMEPAD_ABSTRACT_HAPTIC_GAMEPAD_H_