Gamepad.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "Gamepad.h"
  6. #include "nsPIDOMWindow.h"
  7. #include "nsTArray.h"
  8. #include "nsVariant.h"
  9. #include "mozilla/dom/GamepadBinding.h"
  10. namespace mozilla {
  11. namespace dom {
  12. NS_IMPL_CYCLE_COLLECTING_ADDREF(Gamepad)
  13. NS_IMPL_CYCLE_COLLECTING_RELEASE(Gamepad)
  14. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Gamepad)
  15. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  16. NS_INTERFACE_MAP_ENTRY(nsISupports)
  17. NS_INTERFACE_MAP_END
  18. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Gamepad, mParent, mButtons, mPose)
  19. void
  20. Gamepad::UpdateTimestamp()
  21. {
  22. nsCOMPtr<nsPIDOMWindowInner> newWindow(do_QueryInterface(mParent));
  23. if(newWindow) {
  24. Performance* perf = newWindow->GetPerformance();
  25. if (perf) {
  26. mTimestamp = perf->Now();
  27. }
  28. }
  29. }
  30. Gamepad::Gamepad(nsISupports* aParent,
  31. const nsAString& aID, uint32_t aIndex,
  32. GamepadMappingType aMapping,
  33. uint32_t aNumButtons, uint32_t aNumAxes)
  34. : mParent(aParent),
  35. mID(aID),
  36. mIndex(aIndex),
  37. mMapping(aMapping),
  38. mConnected(true),
  39. mButtons(aNumButtons),
  40. mAxes(aNumAxes),
  41. mTimestamp(0)
  42. {
  43. for (unsigned i = 0; i < aNumButtons; i++) {
  44. mButtons.InsertElementAt(i, new GamepadButton(mParent));
  45. }
  46. mAxes.InsertElementsAt(0, aNumAxes, 0.0f);
  47. mPose = new GamepadPose(aParent);
  48. UpdateTimestamp();
  49. }
  50. void
  51. Gamepad::SetIndex(uint32_t aIndex)
  52. {
  53. mIndex = aIndex;
  54. }
  55. void
  56. Gamepad::SetConnected(bool aConnected)
  57. {
  58. mConnected = aConnected;
  59. }
  60. void
  61. Gamepad::SetButton(uint32_t aButton, bool aPressed, double aValue)
  62. {
  63. MOZ_ASSERT(aButton < mButtons.Length());
  64. mButtons[aButton]->SetPressed(aPressed);
  65. mButtons[aButton]->SetValue(aValue);
  66. UpdateTimestamp();
  67. }
  68. void
  69. Gamepad::SetAxis(uint32_t aAxis, double aValue)
  70. {
  71. MOZ_ASSERT(aAxis < mAxes.Length());
  72. if (mAxes[aAxis] != aValue) {
  73. mAxes[aAxis] = aValue;
  74. GamepadBinding::ClearCachedAxesValue(this);
  75. }
  76. UpdateTimestamp();
  77. }
  78. void
  79. Gamepad::SetPose(const GamepadPoseState& aPose)
  80. {
  81. mPose->SetPoseState(aPose);
  82. }
  83. void
  84. Gamepad::SyncState(Gamepad* aOther)
  85. {
  86. const char* kGamepadExtEnabledPref = "dom.gamepad.extensions.enabled";
  87. if (mButtons.Length() != aOther->mButtons.Length() ||
  88. mAxes.Length() != aOther->mAxes.Length()) {
  89. return;
  90. }
  91. mConnected = aOther->mConnected;
  92. for (uint32_t i = 0; i < mButtons.Length(); ++i) {
  93. mButtons[i]->SetPressed(aOther->mButtons[i]->Pressed());
  94. mButtons[i]->SetValue(aOther->mButtons[i]->Value());
  95. }
  96. bool changed = false;
  97. for (uint32_t i = 0; i < mAxes.Length(); ++i) {
  98. changed = changed || (mAxes[i] != aOther->mAxes[i]);
  99. mAxes[i] = aOther->mAxes[i];
  100. }
  101. if (changed) {
  102. GamepadBinding::ClearCachedAxesValue(this);
  103. }
  104. if (Preferences::GetBool(kGamepadExtEnabledPref)) {
  105. MOZ_ASSERT(aOther->GetPose());
  106. mPose->SetPoseState(aOther->GetPose()->GetPoseState());
  107. }
  108. UpdateTimestamp();
  109. }
  110. already_AddRefed<Gamepad>
  111. Gamepad::Clone(nsISupports* aParent)
  112. {
  113. RefPtr<Gamepad> out =
  114. new Gamepad(aParent, mID, mIndex, mMapping,
  115. mButtons.Length(), mAxes.Length());
  116. out->SyncState(this);
  117. return out.forget();
  118. }
  119. /* virtual */ JSObject*
  120. Gamepad::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  121. {
  122. return GamepadBinding::Wrap(aCx, this, aGivenProto);
  123. }
  124. } // namespace dom
  125. } // namespace mozilla