2017-02-02 06:30:58 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/GamepadHapticActuator.h"
|
|
|
|
#include "mozilla/dom/GamepadManager.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(GamepadHapticActuator)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(GamepadHapticActuator)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GamepadHapticActuator)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GamepadHapticActuator, mParent)
|
|
|
|
|
2018-11-30 10:46:48 +00:00
|
|
|
GamepadHapticActuator::GamepadHapticActuator(nsISupports* aParent,
|
|
|
|
uint32_t aGamepadId,
|
2017-02-02 06:30:58 +00:00
|
|
|
uint32_t aIndex)
|
2018-11-30 10:46:48 +00:00
|
|
|
: mParent(aParent),
|
|
|
|
mGamepadId(aGamepadId),
|
|
|
|
mType(GamepadHapticActuatorType::Vibration),
|
|
|
|
mIndex(aIndex) {}
|
2017-02-02 06:30:58 +00:00
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* virtual */
|
|
|
|
JSObject* GamepadHapticActuator::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-25 21:20:54 +00:00
|
|
|
return GamepadHapticActuator_Binding::Wrap(aCx, this, aGivenProto);
|
2017-02-02 06:30:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 10:46:48 +00:00
|
|
|
nsISupports* GamepadHapticActuator::GetParentObject() const { return mParent; }
|
2017-02-02 06:30:58 +00:00
|
|
|
|
2018-11-30 10:46:48 +00:00
|
|
|
#define CLAMP(f, min, max) (((f) < min) ? min : (((f) > max) ? max : (f)))
|
2017-02-02 06:30:58 +00:00
|
|
|
|
2018-11-30 10:46:48 +00:00
|
|
|
already_AddRefed<Promise> GamepadHapticActuator::Pulse(double aValue,
|
|
|
|
double aDuration,
|
|
|
|
ErrorResult& aRv) {
|
2017-02-02 06:30:58 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
|
|
|
RefPtr<GamepadManager> gamepadManager(GamepadManager::GetService());
|
|
|
|
MOZ_ASSERT(gamepadManager);
|
|
|
|
|
|
|
|
// Clamp intensity aValue to be 0~1.
|
|
|
|
double value = CLAMP(aValue, 0, 1);
|
|
|
|
// aDuration should be always positive.
|
|
|
|
double duration = CLAMP(aDuration, 0, aDuration);
|
|
|
|
|
|
|
|
switch (mType) {
|
2018-11-30 10:46:48 +00:00
|
|
|
case GamepadHapticActuatorType::Vibration: {
|
|
|
|
RefPtr<Promise> promise = gamepadManager->VibrateHaptic(
|
2017-02-02 06:30:58 +00:00
|
|
|
mGamepadId, mIndex, value, duration, global, aRv);
|
|
|
|
if (!promise) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return promise.forget();
|
|
|
|
}
|
2018-11-30 10:46:48 +00:00
|
|
|
default: {
|
2017-02-02 06:30:58 +00:00
|
|
|
// We need to implement other types of haptic
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 10:46:48 +00:00
|
|
|
GamepadHapticActuatorType GamepadHapticActuator::Type() const { return mType; }
|
2017-02-02 06:30:58 +00:00
|
|
|
|
2018-11-30 10:46:48 +00:00
|
|
|
void GamepadHapticActuator::Set(const GamepadHapticActuator* aOther) {
|
2017-02-02 06:30:58 +00:00
|
|
|
mGamepadId = aOther->mGamepadId;
|
|
|
|
mType = aOther->mType;
|
|
|
|
mIndex = aOther->mIndex;
|
|
|
|
}
|
|
|
|
|
2018-11-30 10:46:48 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|