[tor-commits] [tor-browser] 33/73: Bug 1776655 - Allocate input report context for each gamepad. r=mccr8, a=RyanVM

gitolite role git at cupani.torproject.org
Wed Sep 21 20:17:26 UTC 2022


This is an automated email from the git hooks/post-receive script.

richard pushed a commit to branch geckoview-102.3.0esr-12.0-1
in repository tor-browser.

commit cfa8596ba261cc51b39d842a4c0a692b28d7b09d
Author: Chris Martin <cmartin at mozilla.com>
AuthorDate: Mon Aug 29 19:12:58 2022 +0000

    Bug 1776655 - Allocate input report context for each gamepad. r=mccr8, a=RyanVM
    
    Differential Revision: https://phabricator.services.mozilla.com/D152938
---
 dom/gamepad/cocoa/CocoaGamepad.cpp | 48 ++++++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 18 deletions(-)

diff --git a/dom/gamepad/cocoa/CocoaGamepad.cpp b/dom/gamepad/cocoa/CocoaGamepad.cpp
index f9f0feb7e5983..8a813b24542a1 100644
--- a/dom/gamepad/cocoa/CocoaGamepad.cpp
+++ b/dom/gamepad/cocoa/CocoaGamepad.cpp
@@ -28,7 +28,6 @@ namespace {
 
 using namespace mozilla;
 using namespace mozilla::dom;
-using std::vector;
 class DarwinGamepadService;
 
 DarwinGamepadService* gService = nullptr;
@@ -80,6 +79,11 @@ const unsigned kBackUsage = 0x224;
 // 50ms is arbitrarily chosen.
 const uint32_t kDarwinGamepadPollInterval = 50;
 
+struct GamepadInputReportContext {
+  DarwinGamepadService* service;
+  size_t gamepadSlot;
+};
+
 class Gamepad {
  private:
   IOHIDDeviceRef mDevice;
@@ -120,7 +124,8 @@ class Gamepad {
 
   GamepadHandle mHandle;
   RefPtr<GamepadRemapper> mRemapper;
-  std::vector<uint8_t> mInputReport;
+  nsTArray<uint8_t> mInputReport;
+  UniquePtr<GamepadInputReportContext> mInputReportContext;
 };
 
 void Gamepad::init(IOHIDDeviceRef aDevice, bool aDefaultRemapper) {
@@ -179,7 +184,7 @@ void Gamepad::init(IOHIDDeviceRef aDevice, bool aDefaultRemapper) {
 class DarwinGamepadService {
  private:
   IOHIDManagerRef mManager;
-  vector<Gamepad> mGamepads;
+  nsTArray<Gamepad> mGamepads;
 
   nsCOMPtr<nsIThread> mMonitorThread;
   nsCOMPtr<nsIThread> mBackgroundThread;
@@ -272,14 +277,14 @@ void DarwinGamepadService::DeviceAdded(IOHIDDeviceRef device) {
   }
 
   size_t slot = size_t(-1);
-  for (size_t i = 0; i < mGamepads.size(); i++) {
+  for (size_t i = 0; i < mGamepads.Length(); i++) {
     if (mGamepads[i] == device) return;
     if (slot == size_t(-1) && mGamepads[i].empty()) slot = i;
   }
 
   if (slot == size_t(-1)) {
-    slot = mGamepads.size();
-    mGamepads.push_back(Gamepad());
+    slot = mGamepads.Length();
+    mGamepads.AppendElement(Gamepad());
   }
 
   // Gather some identifying information
@@ -322,13 +327,15 @@ void DarwinGamepadService::DeviceAdded(IOHIDDeviceRef device) {
   }
 
   mGamepads[slot].mHandle = handle;
-  mGamepads[slot].mInputReport.resize(remapper->GetMaxInputReportLength());
+  mGamepads[slot].mInputReport.SetLength(remapper->GetMaxInputReportLength());
+  mGamepads[slot].mInputReportContext = UniquePtr<GamepadInputReportContext>(
+      new GamepadInputReportContext{this, slot});
   mGamepads[slot].mRemapper = remapper.forget();
 
   IOHIDDeviceRegisterInputReportCallback(
-      device, mGamepads[slot].mInputReport.data(),
-      mGamepads[slot].mInputReport.size(), ReportChangedCallback,
-      &mGamepads[slot]);
+      device, mGamepads[slot].mInputReport.Elements(),
+      mGamepads[slot].mInputReport.Length(), ReportChangedCallback,
+      mGamepads[slot].mInputReportContext.get());
 }
 
 void DarwinGamepadService::DeviceRemoved(IOHIDDeviceRef device) {
@@ -337,13 +344,16 @@ void DarwinGamepadService::DeviceRemoved(IOHIDDeviceRef device) {
   if (!service) {
     return;
   }
-  for (size_t i = 0; i < mGamepads.size(); i++) {
-    if (mGamepads[i] == device) {
+  for (Gamepad& gamepad : mGamepads) {
+    if (gamepad == device) {
       IOHIDDeviceRegisterInputReportCallback(
-          device, mGamepads[i].mInputReport.data(), 0, NULL, &mGamepads[i]);
+          device, gamepad.mInputReport.Elements(), 0, NULL,
+          gamepad.mInputReportContext.get());
+
+      gamepad.mInputReportContext.reset();
 
-      service->RemoveGamepad(mGamepads[i].mHandle);
-      mGamepads[i].clear();
+      service->RemoveGamepad(gamepad.mHandle);
+      gamepad.clear();
       return;
     }
   }
@@ -354,7 +364,10 @@ void DarwinGamepadService::ReportChangedCallback(
     void* context, IOReturn result, void* sender, IOHIDReportType report_type,
     uint32_t report_id, uint8_t* report, CFIndex report_length) {
   if (context && report_type == kIOHIDReportTypeInput && report_length) {
-    reinterpret_cast<Gamepad*>(context)->ReportChanged(report, report_length);
+    auto reportContext = static_cast<GamepadInputReportContext*>(context);
+    DarwinGamepadService* service = reportContext->service;
+    service->mGamepads[reportContext->gamepadSlot].ReportChanged(report,
+                                                                 report_length);
   }
 }
 
@@ -388,8 +401,7 @@ void DarwinGamepadService::InputValueChanged(IOHIDValueRef value) {
   IOHIDElementRef element = IOHIDValueGetElement(value);
   IOHIDDeviceRef device = IOHIDElementGetDevice(element);
 
-  for (unsigned i = 0; i < mGamepads.size(); i++) {
-    Gamepad& gamepad = mGamepads[i];
+  for (Gamepad& gamepad : mGamepads) {
     if (gamepad == device) {
       // Axis elements represent axes and d-pads.
       if (Axis* axis = gamepad.lookupAxis(element)) {

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the tor-commits mailing list