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

Unified Diff: content/browser/gamepad/gamepad_provider.h

Issue 1586663006: Refactoring gamepad polling to support dynamically added sources (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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: content/browser/gamepad/gamepad_provider.h
diff --git a/content/browser/gamepad/gamepad_provider.h b/content/browser/gamepad/gamepad_provider.h
index f2dcad245bbef162bf847f520b4f83f274df9400..7245bf1dd07c43753f7a2a3ec3de1859ae86d1f3 100644
--- a/content/browser/gamepad/gamepad_provider.h
+++ b/content/browser/gamepad/gamepad_provider.h
@@ -5,6 +5,7 @@
#ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_
#define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_
+#include <stdint.h>
#include <utility>
#include <vector>
@@ -13,9 +14,9 @@
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.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 "content/browser/gamepad/gamepad_standard_mappings.h"
#include "content/common/content_export.h"
#include "third_party/WebKit/public/platform/WebGamepads.h"
@@ -29,6 +30,57 @@ namespace content {
class GamepadDataFetcher;
struct GamepadHardwareBuffer;
+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.
scottmg 2016/01/18 22:54:51 Blank before // line.
+ 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 CONTENT_EXPORT GamepadProvider :
public base::SystemMonitor::DevicesChangedObserver {
public:
@@ -58,12 +110,20 @@ class CONTENT_EXPORT GamepadProvider :
// base::SystemMonitor::DevicesChangedObserver implementation.
void OnDevicesChanged(base::SystemMonitor::DeviceType type) override;
+ // Add a gamepad data fetcher. Takes ownership of |fetcher|.
+ void AddGamepadDataFetcher(scoped_ptr<GamepadDataFetcher> fetcher);
+
+ // 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(scoped_ptr<GamepadDataFetcher> fetcher);
- // Method for setting up the platform-specific data fetcher. Takes ownership
- // of |fetcher|.
- void DoInitializePollingThread(scoped_ptr<GamepadDataFetcher> fetcher);
+ void DoAddGamepadDataFetcher(scoped_ptr<GamepadDataFetcher> fetcher);
// Method for sending pause hints to the low-level data fetcher. Runs on
// polling_thread_.
@@ -84,6 +144,10 @@ class CONTENT_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 };
@@ -123,33 +187,14 @@ class CONTENT_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.
scoped_ptr<PadState[]> pad_states_;
// Only used on the polling thread.
- scoped_ptr<GamepadDataFetcher> data_fetcher_;
+ typedef std::vector<scoped_ptr<GamepadDataFetcher>> GamepadFetcherVector;
+ GamepadFetcherVector data_fetchers_;
base::Lock shared_memory_lock_;
base::SharedMemory gamepad_shared_memory_;
@@ -157,7 +202,7 @@ class CONTENT_EXPORT GamepadProvider :
// Polling is done on this background thread.
scoped_ptr<base::Thread> polling_thread_;
- static GamepadProvider* instance_;
+ //static GamepadProvider* instance_;
scottmg 2016/01/18 22:54:51 Delete.
DISALLOW_COPY_AND_ASSIGN(GamepadProvider);
};

Powered by Google App Engine
This is Rietveld 408576698