Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Unified Diff: device/gamepad/gamepad_provider.h

Issue 2129003002: Refactored gamepad polling to support dynamic sources (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed unit test issue and Mac XBoxDataFetcher constructor Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/gamepad/gamepad_provider.h
diff --git a/device/gamepad/gamepad_provider.h b/device/gamepad/gamepad_provider.h
index 5d2e2b2eb9b744f646705cb2945d58ba4a98433b..3adf81439e5c039cd2ae0cd1176503d17a46f1bc 100644
--- a/device/gamepad/gamepad_provider.h
+++ b/device/gamepad/gamepad_provider.h
@@ -5,6 +5,8 @@
#ifndef DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_
#define DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_
+#include <stdint.h>
+
#include <memory>
#include <utility>
#include <vector>
@@ -13,11 +15,11 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/shared_memory.h"
-#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "base/system_monitor/system_monitor.h"
#include "device/gamepad/gamepad_export.h"
#include "device/gamepad/gamepad_shared_buffer.h"
+#include "device/gamepad/gamepad_standard_mappings.h"
#include "third_party/WebKit/public/platform/WebGamepads.h"
namespace base {
@@ -29,6 +31,57 @@ namespace device {
class GamepadDataFetcher;
+enum GamepadSource {
+ GAMEPAD_SOURCE_NONE = 0,
+ GAMEPAD_SOURCE_ANDROID,
+ GAMEPAD_SOURCE_LINUX_UDEV,
+ GAMEPAD_SOURCE_MAC_HID,
+ GAMEPAD_SOURCE_MAC_XBOX,
+ GAMEPAD_SOURCE_TEST,
+ GAMEPAD_SOURCE_WIN_XINPUT,
+ GAMEPAD_SOURCE_WIN_RAW,
+};
+
+enum GamepadActiveState {
+ GAMEPAD_INACTIVE = 0,
+ GAMEPAD_ACTIVE,
+ GAMEPAD_NEWLY_ACTIVE,
+};
+
+struct PadState {
+ // Which data fetcher provided this gamepad's data.
+ GamepadSource source;
+ // Data fetcher-specific identifier for this gamepad.
+ int source_id;
+
+ // Indicates whether or not the gamepad is actively being updated
+ GamepadActiveState active_state;
+
+ // Gamepad data, unmapped.
+ blink::WebGamepad data;
+
+ // Functions to map from device data to standard layout, if available. May
+ // be null if no mapping is available or needed.
+ GamepadStandardMappingFunction mapper;
+
+ // Sanitization masks
+ // axis_mask and button_mask are bitfields that represent the reset state of
+ // each input. If a button or axis has ever reported 0 in the past the
+ // corresponding bit will be set to 1.
+
+ // If we ever increase the max axis count this will need to be updated.
+ static_assert(blink::WebGamepad::axesLengthCap <=
+ std::numeric_limits<uint32_t>::digits,
+ "axis_mask is not large enough");
+ uint32_t axis_mask;
+
+ // If we ever increase the max button count this will need to be updated.
+ static_assert(blink::WebGamepad::buttonsLengthCap <=
+ std::numeric_limits<uint32_t>::digits,
+ "button_mask is not large enough");
+ uint32_t button_mask;
+};
+
class DEVICE_GAMEPAD_EXPORT GamepadConnectionChangeClient {
public:
virtual void OnGamepadConnectionChange(bool connected,
@@ -56,6 +109,9 @@ class DEVICE_GAMEPAD_EXPORT GamepadProvider
base::SharedMemoryHandle GetSharedMemoryHandleForProcess(
base::ProcessHandle renderer_process);
+ void AddGamepadDataFetcher(GamepadDataFetcher* fetcher);
+ void RemoveGamepadDataFetcher(GamepadDataFetcher* fetcher);
+
void GetCurrentGamepadData(blink::WebGamepads* data);
// Pause and resume the background polling thread. Can be called from any
@@ -70,12 +126,26 @@ class DEVICE_GAMEPAD_EXPORT GamepadProvider
// base::SystemMonitor::DevicesChangedObserver implementation.
void OnDevicesChanged(base::SystemMonitor::DeviceType type) override;
+ // Add a gamepad data fetcher. Takes ownership of |fetcher|.
+ void AddGamepadDataFetcher(std::unique_ptr<GamepadDataFetcher> fetcher);
+
+ // Remove gamepad data fetchers with the given source.
+ void RemoveSourceGamepadDataFetcher(GamepadSource source);
+
+ // Gets a PadState object for the given source and id. If the device hasn't
+ // been encountered before one of the remaining slots will be reserved for it.
+ // If no slots are available will return NULL.
+ PadState* GetPadState(GamepadSource source, int source_id);
+
+ void SetSanitizationEnabled(bool sanitize) { sanitize_ = sanitize; }
+
private:
void Initialize(std::unique_ptr<GamepadDataFetcher> fetcher);
// Method for setting up the platform-specific data fetcher. Takes ownership
// of |fetcher|.
- void DoInitializePollingThread(std::unique_ptr<GamepadDataFetcher> fetcher);
+ void DoAddGamepadDataFetcher(std::unique_ptr<GamepadDataFetcher> fetcher);
+ void DoRemoveSourceGamepadDataFetcher(GamepadSource source);
// Method for sending pause hints to the low-level data fetcher. Runs on
// polling_thread_.
@@ -91,6 +161,9 @@ class DEVICE_GAMEPAD_EXPORT GamepadProvider
// Checks the gamepad state to see if the user has interacted with it.
void CheckForUserGesture();
+ void ClearPadState(PadState& state);
+
+ void MapAndSanitizeGamepadData(PadState* pad_state, blink::WebGamepad* pad);
enum { kDesiredSamplingIntervalMs = 16 };
@@ -131,31 +204,14 @@ class DEVICE_GAMEPAD_EXPORT GamepadProvider
bool devices_changed_;
bool ever_had_user_gesture_;
+ bool sanitize_;
- class PadState {
- public:
- PadState() { SetDisconnected(); }
-
- bool Match(const blink::WebGamepad& pad) const;
- void SetPad(const blink::WebGamepad& pad);
- void SetDisconnected();
- void AsWebGamepad(blink::WebGamepad* pad);
-
- bool connected() const { return connected_; }
-
- private:
- bool connected_;
- unsigned axes_length_;
- unsigned buttons_length_;
- blink::WebUChar id_[blink::WebGamepad::idLengthCap];
- blink::WebUChar mapping_[blink::WebGamepad::mappingLengthCap];
- };
-
- // Used to detect connections and disconnections.
+ // Tracks the state of each gamepad slot.
std::unique_ptr<PadState[]> pad_states_;
// Only used on the polling thread.
- std::unique_ptr<GamepadDataFetcher> data_fetcher_;
+ typedef std::vector<std::unique_ptr<GamepadDataFetcher>> GamepadFetcherVector;
+ GamepadFetcherVector data_fetchers_;
base::Lock shared_memory_lock_;
std::unique_ptr<GamepadSharedBuffer> gamepad_shared_buffer_;
@@ -165,8 +221,6 @@ class DEVICE_GAMEPAD_EXPORT GamepadProvider
GamepadConnectionChangeClient* connection_change_client_;
- static GamepadProvider* instance_;
-
DISALLOW_COPY_AND_ASSIGN(GamepadProvider);
};

Powered by Google App Engine
This is Rietveld 408576698