Bug 1313581 - Part 1: Support hand attribute in GamepadAPI; r?lenzak800, kip, qdot draft
authorDaosheng Mu <daoshengmu@gmail.com>
Mon, 14 Nov 2016 17:28:48 +0800
changeset 448074 6cf2a0aa67f56eb438ba4f420a4fd53896fb6818
parent 447934 d8f63b2935af0915a6a24b3ea8e27d9a09f66416
child 448075 4524cbf3937e1fce07a070e287d2007ad339f29a
push id38254
push userbmo:dmu@mozilla.com
push dateFri, 09 Dec 2016 02:25:14 +0000
reviewerslenzak800, kip, qdot
bugs1313581
milestone53.0a1
Bug 1313581 - Part 1: Support hand attribute in GamepadAPI; r?lenzak800, kip, qdot MozReview-Commit-ID: 9n48LGaqOP2
dom/gamepad/Gamepad.cpp
dom/gamepad/Gamepad.h
dom/gamepad/ipc/GamepadEventTypes.ipdlh
dom/webidl/Gamepad.webidl
--- a/dom/gamepad/Gamepad.cpp
+++ b/dom/gamepad/Gamepad.cpp
@@ -33,21 +33,23 @@ Gamepad::UpdateTimestamp()
       mTimestamp =  perf->Now();
     }
   }
 }
 
 Gamepad::Gamepad(nsISupports* aParent,
                  const nsAString& aID, uint32_t aIndex,
                  GamepadMappingType aMapping,
+                 GamepadHand aHand,
                  uint32_t aNumButtons, uint32_t aNumAxes)
   : mParent(aParent),
     mID(aID),
     mIndex(aIndex),
     mMapping(aMapping),
+    mHand(aHand),
     mConnected(true),
     mButtons(aNumButtons),
     mAxes(aNumAxes),
     mTimestamp(0)
 {
   for (unsigned i = 0; i < aNumButtons; i++) {
     mButtons.InsertElementAt(i, new GamepadButton(mParent));
   }
@@ -117,27 +119,28 @@ Gamepad::SyncState(Gamepad* aOther)
   }
   if (changed) {
     GamepadBinding::ClearCachedAxesValue(this);
   }
 
   if (Preferences::GetBool(kGamepadExtEnabledPref)) {
     MOZ_ASSERT(aOther->GetPose());
     mPose->SetPoseState(aOther->GetPose()->GetPoseState());
+    mHand = aOther->Hand();
   }
 
   UpdateTimestamp();
 }
 
 already_AddRefed<Gamepad>
 Gamepad::Clone(nsISupports* aParent)
 {
   RefPtr<Gamepad> out =
     new Gamepad(aParent, mID, mIndex, mMapping,
-                mButtons.Length(), mAxes.Length());
+                mHand, mButtons.Length(), mAxes.Length());
   out->SyncState(this);
   return out.forget();
 }
 
 /* virtual */ JSObject*
 Gamepad::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return GamepadBinding::Wrap(aCx, this, aGivenProto);
--- a/dom/gamepad/Gamepad.h
+++ b/dom/gamepad/Gamepad.h
@@ -36,17 +36,17 @@ const int kRightStickYAxis = 3;
 
 
 class Gamepad final : public nsISupports,
                       public nsWrapperCache
 {
 public:
   Gamepad(nsISupports* aParent,
           const nsAString& aID, uint32_t aIndex,
-          GamepadMappingType aMapping,
+          GamepadMappingType aMapping, GamepadHand aHand,
           uint32_t aNumButtons, uint32_t aNumAxes);
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Gamepad)
 
   void SetConnected(bool aConnected);
   void SetButton(uint32_t aButton, bool aPressed, double aValue);
   void SetAxis(uint32_t aAxis, double aValue);
   void SetIndex(uint32_t aIndex);
@@ -76,16 +76,21 @@ public:
      return mTimestamp;
   }
 
   GamepadMappingType Mapping()
   {
     return mMapping;
   }
 
+  GamepadHand Hand()
+  {
+    return mHand;
+  }
+
   bool Connected() const
   {
     return mConnected;
   }
 
   uint32_t Index() const
   {
     return mIndex;
@@ -112,16 +117,17 @@ private:
 
 protected:
   nsCOMPtr<nsISupports> mParent;
   nsString mID;
   uint32_t mIndex;
 
   // The mapping in use.
   GamepadMappingType mMapping;
+  GamepadHand mHand;
 
   // true if this gamepad is currently connected.
   bool mConnected;
 
   // Current state of buttons, axes.
   nsTArray<RefPtr<GamepadButton>> mButtons;
   nsTArray<double> mAxes;
   DOMHighResTimeStamp mTimestamp;
--- a/dom/gamepad/ipc/GamepadEventTypes.ipdlh
+++ b/dom/gamepad/ipc/GamepadEventTypes.ipdlh
@@ -11,16 +11,20 @@ namespace dom {
 
 struct GamepadAdded {
   nsString id;
   uint32_t index;
   // Ideally, mapping should be a GamepadMappingType
   // But, we have dependency problems in non MOZ_GAMEPAD
   // platforms. Therefore, we make it as an uint32_t here.
   uint32_t mapping;
+  // Ideally, hand should be a GamepadHand
+  // But, we have dependency problems in non MOZ_GAMEPAD
+  // platforms. Therefore, we make it as an uint32_t here.
+  uint32_t hand;
   GamepadServiceType service_type;
   uint32_t num_buttons;
   uint32_t num_axes;
 };
 
 struct GamepadRemoved {
   uint32_t index;
   GamepadServiceType service_type;
--- a/dom/webidl/Gamepad.webidl
+++ b/dom/webidl/Gamepad.webidl
@@ -1,19 +1,30 @@
 /* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * The origin of this IDL file is
+ * https://w3c.github.io/gamepad/
+ * https://w3c.github.io/gamepad/extensions.html
+ */
 
 [Pref="dom.gamepad.enabled"]
 interface GamepadButton {
   readonly    attribute boolean pressed;
   readonly    attribute double  value;
 };
 
+enum GamepadHand {
+  "",
+  "left",
+  "right"
+};
+
 enum GamepadMappingType {
   "",
   "standard"
 };
 
 [Pref="dom.gamepad.enabled"]
 interface Gamepad {
   /**
@@ -29,16 +40,23 @@ interface Gamepad {
 
   /**
    * The mapping in use for this device. The empty string
    * indicates that no mapping is in use.
    */
   readonly attribute GamepadMappingType mapping;
 
   /**
+   * The hand in use for this device. The empty string
+   * indicates that unknown, both hands, or not applicable
+   */
+  [Pref="dom.gamepad.extensions.enabled"]
+  readonly attribute GamepadHand hand;
+
+  /**
    * true if this gamepad is currently connected to the system.
    */
   readonly attribute boolean connected;
 
   /**
    * The current state of all buttons on the device, an
    * array of GamepadButton.
    */