|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 | + * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +#include "mozilla/dom/GamepadFunctions.h" |
| 6 | +#include "mozilla/dom/GamepadService.h" |
| 7 | +#include "nsHashKeys.h" |
| 8 | +#include "mozilla/dom/ContentParent.h" |
| 9 | +#include "mozilla/unused.h" |
| 10 | + |
| 11 | +namespace mozilla { |
| 12 | +namespace dom { |
| 13 | +namespace GamepadFunctions { |
| 14 | + |
| 15 | +namespace { |
| 16 | +// Increments as gamepads are added |
| 17 | +uint32_t gGamepadIndex = 0; |
| 18 | +} |
| 19 | + |
| 20 | +template<class T> |
| 21 | +void |
| 22 | +NotifyGamepadChange(const T& aInfo) |
| 23 | +{ |
| 24 | + MOZ_ASSERT(NS_IsMainThread()); |
| 25 | + MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); |
| 26 | + GamepadChangeEvent e(aInfo); |
| 27 | + nsTArray<ContentParent*> t; |
| 28 | + ContentParent::GetAll(t); |
| 29 | + for(uint32_t i = 0; i < t.Length(); ++i) { |
| 30 | + unused << t[i]->SendGamepadUpdate(e); |
| 31 | + } |
| 32 | + // If we have a GamepadService in the main process, send directly to it. |
| 33 | + if (GamepadService::IsServiceRunning()) { |
| 34 | + nsRefPtr<GamepadService> svc = GamepadService::GetService(); |
| 35 | + svc->Update(e); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +uint32_t |
| 40 | +AddGamepad(const char* aID, |
| 41 | + GamepadMappingType aMapping, |
| 42 | + uint32_t aNumButtons, uint32_t aNumAxes) |
| 43 | +{ |
| 44 | + MOZ_ASSERT(NS_IsMainThread()); |
| 45 | + MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); |
| 46 | + |
| 47 | + int index = gGamepadIndex; |
| 48 | + gGamepadIndex++; |
| 49 | + GamepadAdded a(NS_ConvertUTF8toUTF16(nsDependentCString(aID)), index, |
| 50 | + (uint32_t)aMapping, aNumButtons, aNumAxes); |
| 51 | + gGamepadIndex++; |
| 52 | + NotifyGamepadChange<GamepadAdded>(a); |
| 53 | + return index; |
| 54 | +} |
| 55 | + |
| 56 | +void |
| 57 | +RemoveGamepad(uint32_t aIndex) |
| 58 | +{ |
| 59 | + MOZ_ASSERT(NS_IsMainThread()); |
| 60 | + MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); |
| 61 | + GamepadRemoved a(aIndex); |
| 62 | + NotifyGamepadChange<GamepadRemoved>(a); |
| 63 | +} |
| 64 | + |
| 65 | +void |
| 66 | +NewButtonEvent(uint32_t aIndex, uint32_t aButton, |
| 67 | + bool aPressed, double aValue) |
| 68 | +{ |
| 69 | + MOZ_ASSERT(NS_IsMainThread()); |
| 70 | + MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); |
| 71 | + GamepadButtonInformation a(aIndex, aButton, aPressed, aValue); |
| 72 | + NotifyGamepadChange<GamepadButtonInformation>(a); |
| 73 | +} |
| 74 | + |
| 75 | +void |
| 76 | +NewButtonEvent(uint32_t aIndex, uint32_t aButton, |
| 77 | + bool aPressed) |
| 78 | +{ |
| 79 | + MOZ_ASSERT(NS_IsMainThread()); |
| 80 | + MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); |
| 81 | + // When only a digital button is available the value will be synthesized. |
| 82 | + NewButtonEvent(aIndex, aButton, aPressed, aPressed ? 1.0L : 0.0L); |
| 83 | +} |
| 84 | + |
| 85 | +void |
| 86 | +NewAxisMoveEvent(uint32_t aIndex, uint32_t aAxis, |
| 87 | + double aValue) |
| 88 | +{ |
| 89 | + MOZ_ASSERT(NS_IsMainThread()); |
| 90 | + MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); |
| 91 | + GamepadAxisInformation a(aIndex, aAxis, aValue); |
| 92 | + NotifyGamepadChange<GamepadAxisInformation>(a); |
| 93 | +} |
| 94 | + |
| 95 | +void |
| 96 | +ResetGamepadIndexes() |
| 97 | +{ |
| 98 | + MOZ_ASSERT(NS_IsMainThread()); |
| 99 | + MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); |
| 100 | + gGamepadIndex = 0; |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace GamepadFunctions |
| 104 | +} // namespace dom |
| 105 | +} // namespace mozilla |
0 commit comments