1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "nsWrapperCache.h"
8 
9 #include "mozilla/HoldDropJSObjects.h"
10 #include "mozilla/dom/GamepadPoseBinding.h"
11 #include "mozilla/dom/GamepadPose.h"
12 
13 namespace mozilla {
14 namespace dom {
15 
GamepadPose(nsISupports * aParent,const GamepadPoseState & aState)16 GamepadPose::GamepadPose(nsISupports* aParent, const GamepadPoseState& aState)
17     : Pose(aParent), mPoseState(aState) {
18   mozilla::HoldJSObjects(this);
19 }
20 
GamepadPose(nsISupports * aParent)21 GamepadPose::GamepadPose(nsISupports* aParent) : Pose(aParent) {
22   mozilla::HoldJSObjects(this);
23   mPoseState.Clear();
24 }
25 
~GamepadPose()26 GamepadPose::~GamepadPose() { mozilla::DropJSObjects(this); }
27 
WrapObject(JSContext * aJSContext,JS::Handle<JSObject * > aGivenProto)28 /* virtual */ JSObject* GamepadPose::WrapObject(
29     JSContext* aJSContext, JS::Handle<JSObject*> aGivenProto) {
30   return GamepadPoseBinding::Wrap(aJSContext, this, aGivenProto);
31 }
32 
HasOrientation() const33 bool GamepadPose::HasOrientation() const {
34   return bool(mPoseState.flags & GamepadCapabilityFlags::Cap_Position);
35 }
36 
HasPosition() const37 bool GamepadPose::HasPosition() const {
38   return bool(mPoseState.flags & GamepadCapabilityFlags::Cap_Orientation);
39 }
40 
GetPosition(JSContext * aJSContext,JS::MutableHandle<JSObject * > aRetval,ErrorResult & aRv)41 void GamepadPose::GetPosition(JSContext* aJSContext,
42                               JS::MutableHandle<JSObject*> aRetval,
43                               ErrorResult& aRv) {
44   SetFloat32Array(aJSContext, aRetval, mPosition,
45                   mPoseState.isPositionValid ? mPoseState.position : nullptr, 3,
46                   bool(mPoseState.flags & GamepadCapabilityFlags::Cap_Position),
47                   aRv);
48 }
49 
GetLinearVelocity(JSContext * aJSContext,JS::MutableHandle<JSObject * > aRetval,ErrorResult & aRv)50 void GamepadPose::GetLinearVelocity(JSContext* aJSContext,
51                                     JS::MutableHandle<JSObject*> aRetval,
52                                     ErrorResult& aRv) {
53   SetFloat32Array(
54       aJSContext, aRetval, mLinearVelocity,
55       mPoseState.isPositionValid ? mPoseState.linearVelocity : nullptr, 3,
56       bool(mPoseState.flags & GamepadCapabilityFlags::Cap_Position), aRv);
57 }
58 
GetLinearAcceleration(JSContext * aJSContext,JS::MutableHandle<JSObject * > aRetval,ErrorResult & aRv)59 void GamepadPose::GetLinearAcceleration(JSContext* aJSContext,
60                                         JS::MutableHandle<JSObject*> aRetval,
61                                         ErrorResult& aRv) {
62   SetFloat32Array(
63       aJSContext, aRetval, mLinearAcceleration,
64       mPoseState.isPositionValid ? mPoseState.linearAcceleration : nullptr, 3,
65       bool(mPoseState.flags & GamepadCapabilityFlags::Cap_LinearAcceleration),
66       aRv);
67 }
68 
GetOrientation(JSContext * aJSContext,JS::MutableHandle<JSObject * > aRetval,ErrorResult & aRv)69 void GamepadPose::GetOrientation(JSContext* aJSContext,
70                                  JS::MutableHandle<JSObject*> aRetval,
71                                  ErrorResult& aRv) {
72   SetFloat32Array(
73       aJSContext, aRetval, mOrientation,
74       mPoseState.isOrientationValid ? mPoseState.orientation : nullptr, 4,
75       bool(mPoseState.flags & GamepadCapabilityFlags::Cap_Orientation), aRv);
76 }
77 
GetAngularVelocity(JSContext * aJSContext,JS::MutableHandle<JSObject * > aRetval,ErrorResult & aRv)78 void GamepadPose::GetAngularVelocity(JSContext* aJSContext,
79                                      JS::MutableHandle<JSObject*> aRetval,
80                                      ErrorResult& aRv) {
81   SetFloat32Array(
82       aJSContext, aRetval, mAngularVelocity,
83       mPoseState.isOrientationValid ? mPoseState.angularVelocity : nullptr, 3,
84       bool(mPoseState.flags & GamepadCapabilityFlags::Cap_Orientation), aRv);
85 }
86 
GetAngularAcceleration(JSContext * aJSContext,JS::MutableHandle<JSObject * > aRetval,ErrorResult & aRv)87 void GamepadPose::GetAngularAcceleration(JSContext* aJSContext,
88                                          JS::MutableHandle<JSObject*> aRetval,
89                                          ErrorResult& aRv) {
90   SetFloat32Array(
91       aJSContext, aRetval, mAngularAcceleration,
92       mPoseState.isOrientationValid ? mPoseState.angularAcceleration : nullptr,
93       3,
94       bool(mPoseState.flags & GamepadCapabilityFlags::Cap_AngularAcceleration),
95       aRv);
96 }
97 
SetPoseState(const GamepadPoseState & aPose)98 void GamepadPose::SetPoseState(const GamepadPoseState& aPose) {
99   mPoseState = aPose;
100 }
101 
GetPoseState()102 const GamepadPoseState& GamepadPose::GetPoseState() { return mPoseState; }
103 
104 }  // namespace dom
105 }  // namespace mozilla
106