gamepad.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2011, Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
  17. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_GAMEPAD_GAMEPAD_H_
  26. #define THIRD_PARTY_BLINK_RENDERER_MODULES_GAMEPAD_GAMEPAD_H_
  27. #include "device/gamepad/public/cpp/gamepad.h"
  28. #include "third_party/blink/renderer/core/dom/dom_high_res_time_stamp.h"
  29. #include "third_party/blink/renderer/modules/gamepad/gamepad_button.h"
  30. #include "third_party/blink/renderer/modules/gamepad/gamepad_haptic_actuator.h"
  31. #include "third_party/blink/renderer/modules/modules_export.h"
  32. #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
  33. #include "third_party/blink/renderer/platform/heap/handle.h"
  34. #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
  35. #include "third_party/blink/renderer/platform/wtf/vector.h"
  36. namespace blink {
  37. class MODULES_EXPORT Gamepad final : public ScriptWrappable {
  38. DEFINE_WRAPPERTYPEINFO();
  39. public:
  40. // Objects implementing this interface are garbage-collected.
  41. class Client : public GarbageCollectedMixin {
  42. public:
  43. virtual GamepadHapticActuator* GetVibrationActuatorForGamepad(
  44. const Gamepad&) = 0;
  45. virtual ~Client() = default;
  46. };
  47. Gamepad(Client* client,
  48. int index,
  49. base::TimeTicks time_origin,
  50. base::TimeTicks time_floor);
  51. ~Gamepad() override;
  52. void UpdateFromDeviceState(const device::Gamepad&);
  53. typedef Vector<double> DoubleVector;
  54. const String& id() const { return id_; }
  55. void SetId(const String& id) { id_ = id; }
  56. int index() const { return index_; }
  57. bool connected() const { return connected_; }
  58. void SetConnected(bool val) { connected_ = val; }
  59. DOMHighResTimeStamp timestamp() const { return timestamp_; }
  60. const String& mapping() const { return mapping_; }
  61. void SetMapping(device::GamepadMapping mapping);
  62. const DoubleVector& axes();
  63. void SetAxes(unsigned count, const double* data);
  64. bool isAxisDataDirty() const { return is_axis_data_dirty_; }
  65. const GamepadButtonVector& buttons();
  66. void SetButtons(unsigned count, const device::GamepadButton* data);
  67. bool isButtonDataDirty() const { return is_button_data_dirty_; }
  68. GamepadHapticActuator* vibrationActuator() const;
  69. void SetVibrationActuatorInfo(const device::GamepadHapticActuator&);
  70. bool HasVibrationActuator() const { return has_vibration_actuator_; }
  71. device::GamepadHapticActuatorType GetVibrationActuatorType() const {
  72. return vibration_actuator_type_;
  73. }
  74. void Trace(Visitor*) const override;
  75. private:
  76. void SetTimestamp(const device::Gamepad& device_gamepad);
  77. Member<Client> client_;
  78. // A string identifying the gamepad model.
  79. String id_;
  80. // The index of this gamepad within the GamepadList.
  81. const int index_;
  82. // True if this gamepad was still connected when gamepad state was captured.
  83. bool connected_;
  84. // The current time when the gamepad state was captured.
  85. DOMHighResTimeStamp timestamp_;
  86. // A string indicating whether the standard mapping is in use.
  87. String mapping_;
  88. // Snapshot of the axis state.
  89. DoubleVector axes_;
  90. // Snapshot of the button state.
  91. GamepadButtonVector buttons_;
  92. // True if the gamepad can produce haptic vibration effects.
  93. bool has_vibration_actuator_;
  94. // The type of haptic actuator used for vibration effects.
  95. device::GamepadHapticActuatorType vibration_actuator_type_;
  96. // True if the data in |axes_| has changed since the last time it was
  97. // accessed.
  98. bool is_axis_data_dirty_;
  99. // True if the data in |buttons_| has changed since the last time it was
  100. // accessed.
  101. bool is_button_data_dirty_;
  102. // Base time on which all relative timestamps are based.
  103. const base::TimeTicks time_origin_;
  104. // Minimum value to use for timestamps from the device.
  105. const base::TimeTicks time_floor_;
  106. };
  107. } // namespace blink
  108. #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_GAMEPAD_GAMEPAD_H_