gamepad_pad_state_provider.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2016 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_PAD_STATE_PROVIDER_H_
  5. #define DEVICE_GAMEPAD_GAMEPAD_PAD_STATE_PROVIDER_H_
  6. #include <stdint.h>
  7. #include <limits>
  8. #include <memory>
  9. #include "device/gamepad/gamepad_export.h"
  10. #include "device/gamepad/gamepad_standard_mappings.h"
  11. #include "device/gamepad/public/cpp/gamepad.h"
  12. namespace device {
  13. class GamepadDataFetcher;
  14. // These values are logged to UMA. Entries should not be renumbered and
  15. // numeric values should never be reused. Please keep in sync with
  16. // "GamepadSource" in src/tools/metrics/histograms/enums.xml.
  17. enum GamepadSource {
  18. GAMEPAD_SOURCE_NONE = 0,
  19. GAMEPAD_SOURCE_ANDROID,
  20. GAMEPAD_SOURCE_GVR,
  21. GAMEPAD_SOURCE_CARDBOARD,
  22. GAMEPAD_SOURCE_LINUX_UDEV,
  23. GAMEPAD_SOURCE_MAC_GC,
  24. GAMEPAD_SOURCE_MAC_HID,
  25. GAMEPAD_SOURCE_MAC_XBOX,
  26. GAMEPAD_SOURCE_NINTENDO,
  27. GAMEPAD_SOURCE_OCULUS,
  28. GAMEPAD_SOURCE_OPENVR,
  29. GAMEPAD_SOURCE_TEST,
  30. GAMEPAD_SOURCE_WIN_XINPUT,
  31. GAMEPAD_SOURCE_WIN_RAW,
  32. GAMEPAD_SOURCE_WIN_MR,
  33. GAMEPAD_SOURCE_OPENXR,
  34. GAMEPAD_SOURCE_WIN_WGI,
  35. kMaxValue = GAMEPAD_SOURCE_WIN_WGI,
  36. };
  37. struct PadState {
  38. PadState();
  39. ~PadState();
  40. // Which data fetcher provided this gamepad's data.
  41. GamepadSource source;
  42. // Data fetcher-specific identifier for this gamepad.
  43. int source_id;
  44. // Indicates whether this gamepad is actively receiving input. |is_active| is
  45. // initialized to false on each polling cycle and must is set to true when new
  46. // data is received.
  47. bool is_active;
  48. // True if the gamepad is newly connected but notifications have not yet been
  49. // sent.
  50. bool is_newly_active;
  51. // Set by the data fetcher to indicate that one-time initialization for this
  52. // gamepad has been completed.
  53. bool is_initialized;
  54. // Set by the data fetcher to indicate whether this gamepad's ids are
  55. // recognized as a specific gamepad. It is then used to prioritize recognized
  56. // gamepads when finding an empty slot for any new gamepads when activated.
  57. bool is_recognized;
  58. // Gamepad data, unmapped.
  59. Gamepad data;
  60. // Functions to map from device data to standard layout, if available. May
  61. // be null if no mapping is available or needed.
  62. GamepadStandardMappingFunction mapper;
  63. // Sanitization masks
  64. // axis_mask and button_mask are bitfields that represent the reset state of
  65. // each input. If a button or axis has ever reported 0 in the past the
  66. // corresponding bit will be set to 1.
  67. // If we ever increase the max axis count this will need to be updated.
  68. static_assert(Gamepad::kAxesLengthCap <=
  69. std::numeric_limits<uint32_t>::digits,
  70. "axis_mask is not large enough");
  71. uint32_t axis_mask;
  72. // If we ever increase the max button count this will need to be updated.
  73. static_assert(Gamepad::kButtonsLengthCap <=
  74. std::numeric_limits<uint32_t>::digits,
  75. "button_mask is not large enough");
  76. uint32_t button_mask;
  77. };
  78. class DEVICE_GAMEPAD_EXPORT GamepadPadStateProvider {
  79. public:
  80. GamepadPadStateProvider();
  81. virtual ~GamepadPadStateProvider();
  82. // Gets a PadState object for the given source and id. If the device hasn't
  83. // been encountered before one of the remaining slots will be reserved for it.
  84. // If no slots are available this returns nullptr. However, if one of those
  85. // slots contains an unrecognized gamepad and |new_gamepad_recognized| is true
  86. // that slot will be reset and returned.
  87. PadState* GetPadState(GamepadSource source,
  88. int source_id,
  89. bool new_gamepad_recognized);
  90. // Gets a PadState object for a connected gamepad by specifying its index in
  91. // the pad_states_ array. Returns NULL if there is no connected gamepad at
  92. // that index.
  93. PadState* GetConnectedPadState(uint32_t pad_index);
  94. protected:
  95. void ClearPadState(PadState& state);
  96. void InitializeDataFetcher(GamepadDataFetcher* fetcher);
  97. void MapAndSanitizeGamepadData(PadState* pad_state,
  98. Gamepad* pad,
  99. bool sanitize);
  100. // Tracks the state of each gamepad slot.
  101. std::unique_ptr<PadState[]> pad_states_;
  102. private:
  103. // Calls the DisconnectUnrecognizedGamepad method on the data fetcher
  104. // associated with the given |source|. The actual implementation is always
  105. // in the |gamepad_provider|.
  106. virtual void DisconnectUnrecognizedGamepad(GamepadSource source,
  107. int source_id) = 0;
  108. };
  109. } // namespace device
  110. #endif // DEVICE_GAMEPAD_GAMEPAD_PAD_STATE_PROVIDER_H_