2015-05-03 22:32:37 +03: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: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2011-06-20 09:36:17 +04:00
|
|
|
|
2012-03-21 10:36:17 +04:00
|
|
|
#include "mozilla/Hal.h"
|
|
|
|
#include "mozilla/HalSensor.h"
|
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
#include "nsDeviceSensors.h"
|
2011-06-20 09:36:17 +04:00
|
|
|
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2011-09-01 06:39:49 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2011-06-20 09:36:17 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
2016-02-24 20:43:07 +03:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2011-06-20 09:36:17 +04:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2012-04-04 08:09:20 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-06-15 06:31:55 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-04-29 21:27:26 +04:00
|
|
|
#include "mozilla/Services.h"
|
2012-10-24 06:30:10 +04:00
|
|
|
#include "nsIPermissionManager.h"
|
2013-09-14 22:59:50 +04:00
|
|
|
#include "mozilla/dom/DeviceLightEvent.h"
|
2014-07-01 03:02:03 +04:00
|
|
|
#include "mozilla/dom/DeviceOrientationEvent.h"
|
2013-09-12 02:35:51 +04:00
|
|
|
#include "mozilla/dom/DeviceProximityEvent.h"
|
2013-09-14 22:59:51 +04:00
|
|
|
#include "mozilla/dom/UserProximityEvent.h"
|
2012-03-09 22:40:43 +04:00
|
|
|
|
2015-12-15 04:03:56 +03:00
|
|
|
#include <cmath>
|
|
|
|
|
2012-03-21 10:36:17 +04:00
|
|
|
using namespace mozilla;
|
2013-04-20 02:18:33 +04:00
|
|
|
using namespace mozilla::dom;
|
2012-03-21 10:36:17 +04:00
|
|
|
using namespace hal;
|
2012-03-13 20:59:24 +04:00
|
|
|
|
2012-05-17 00:27:54 +04:00
|
|
|
#undef near
|
|
|
|
|
2012-03-14 03:57:09 +04:00
|
|
|
#define DEFAULT_SENSOR_POLL 100
|
|
|
|
|
2011-08-10 09:36:00 +04:00
|
|
|
static const nsTArray<nsIDOMWindow*>::index_type NoIndex =
|
2012-03-25 04:29:49 +04:00
|
|
|
nsTArray<nsIDOMWindow*>::NoIndex;
|
2011-07-26 20:27:11 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsDeviceSensorData final : public nsIDeviceSensorData
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2012-03-25 04:29:57 +04:00
|
|
|
NS_DECL_NSIDEVICESENSORDATA
|
2011-06-20 09:36:17 +04:00
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
nsDeviceSensorData(unsigned long type, double x, double y, double z);
|
2011-06-20 09:36:17 +04:00
|
|
|
|
|
|
|
private:
|
2012-03-25 04:29:57 +04:00
|
|
|
~nsDeviceSensorData();
|
2011-06-20 09:36:17 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
unsigned long mType;
|
|
|
|
double mX, mY, mZ;
|
|
|
|
};
|
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
nsDeviceSensorData::nsDeviceSensorData(unsigned long type, double x, double y, double z)
|
2011-06-20 09:36:17 +04:00
|
|
|
: mType(type), mX(x), mY(y), mZ(z)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDeviceSensorData)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDeviceSensorData)
|
2011-06-20 09:36:17 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
NS_IMPL_ADDREF(nsDeviceSensorData)
|
|
|
|
NS_IMPL_RELEASE(nsDeviceSensorData)
|
2011-06-20 09:36:17 +04:00
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
nsDeviceSensorData::~nsDeviceSensorData()
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsDeviceSensorData::GetType(uint32_t *aType)
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
|
|
|
*aType = mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
NS_IMETHODIMP nsDeviceSensorData::GetX(double *aX)
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aX);
|
|
|
|
*aX = mX;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
NS_IMETHODIMP nsDeviceSensorData::GetY(double *aY)
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aY);
|
|
|
|
*aY = mY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
NS_IMETHODIMP nsDeviceSensorData::GetZ(double *aZ)
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aZ);
|
|
|
|
*aZ = mZ;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsDeviceSensors, nsIDeviceSensors)
|
2011-06-20 09:36:17 +04:00
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
nsDeviceSensors::nsDeviceSensors()
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
2012-05-17 00:27:54 +04:00
|
|
|
mIsUserProximityNear = false;
|
2012-03-21 10:36:17 +04:00
|
|
|
mLastDOMMotionEventTime = TimeStamp::Now();
|
2012-05-02 20:43:45 +04:00
|
|
|
mEnabled = Preferences::GetBool("device.sensors.enabled", true);
|
2012-03-21 10:36:17 +04:00
|
|
|
|
2012-03-25 04:29:49 +04:00
|
|
|
for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
|
|
|
|
nsTArray<nsIDOMWindow*> *windows = new nsTArray<nsIDOMWindow*>();
|
|
|
|
mWindowListeners.AppendElement(windows);
|
|
|
|
}
|
2011-06-20 09:36:17 +04:00
|
|
|
|
2012-03-25 04:29:49 +04:00
|
|
|
mLastDOMMotionEventTime = TimeStamp::Now();
|
2011-06-20 09:36:17 +04:00
|
|
|
}
|
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
nsDeviceSensors::~nsDeviceSensors()
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
2012-03-25 04:29:49 +04:00
|
|
|
for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
|
2014-11-08 07:48:38 +03:00
|
|
|
if (IsSensorEnabled(i))
|
2012-03-25 04:29:49 +04:00
|
|
|
UnregisterSensorObserver((SensorType)i, this);
|
2011-06-20 09:36:17 +04:00
|
|
|
}
|
2012-03-25 04:29:49 +04:00
|
|
|
|
|
|
|
for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
|
|
|
|
delete mWindowListeners[i];
|
2011-06-20 09:36:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-21 08:40:56 +04:00
|
|
|
NS_IMETHODIMP nsDeviceSensors::HasWindowListener(uint32_t aType, nsIDOMWindow *aWindow, bool *aRetVal)
|
2012-06-11 03:44:50 +04:00
|
|
|
{
|
2012-10-21 08:40:56 +04:00
|
|
|
if (!mEnabled)
|
|
|
|
*aRetVal = false;
|
|
|
|
else
|
|
|
|
*aRetVal = mWindowListeners[aType]->IndexOf(aWindow) != NoIndex;
|
2012-06-11 03:44:50 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class DeviceSensorTestEvent : public Runnable
|
2016-02-24 20:43:07 +03:00
|
|
|
{
|
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
DeviceSensorTestEvent(nsDeviceSensors* aTarget, uint32_t aType)
|
|
|
|
: mozilla::Runnable("DeviceSensorTestEvent")
|
|
|
|
, mTarget(aTarget)
|
|
|
|
, mType(aType)
|
2016-02-24 20:43:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2016-02-24 20:43:07 +03:00
|
|
|
{
|
|
|
|
SensorData sensorData;
|
|
|
|
sensorData.sensor() = static_cast<SensorType>(mType);
|
|
|
|
sensorData.timestamp() = PR_Now();
|
|
|
|
sensorData.values().AppendElement(0.5f);
|
|
|
|
sensorData.values().AppendElement(0.5f);
|
|
|
|
sensorData.values().AppendElement(0.5f);
|
|
|
|
sensorData.values().AppendElement(0.5f);
|
|
|
|
sensorData.accuracy() = SENSOR_ACCURACY_UNRELIABLE;
|
|
|
|
mTarget->Notify(sensorData);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<nsDeviceSensors> mTarget;
|
|
|
|
uint32_t mType;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool sTestSensorEvents = false;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsDeviceSensors::AddWindowListener(uint32_t aType, nsIDOMWindow *aWindow)
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
2012-05-02 20:43:45 +04:00
|
|
|
if (!mEnabled)
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-03-25 04:29:49 +04:00
|
|
|
if (mWindowListeners[aType]->IndexOf(aWindow) != NoIndex)
|
|
|
|
return NS_OK;
|
2011-06-20 09:36:17 +04:00
|
|
|
|
2012-03-25 04:29:49 +04:00
|
|
|
if (!IsSensorEnabled(aType)) {
|
|
|
|
RegisterSensorObserver((SensorType)aType, this);
|
2011-06-20 09:36:17 +04:00
|
|
|
}
|
|
|
|
|
2012-03-25 04:29:49 +04:00
|
|
|
mWindowListeners[aType]->AppendElement(aWindow);
|
2016-02-24 20:43:07 +03:00
|
|
|
|
|
|
|
static bool sPrefCacheInitialized = false;
|
|
|
|
if (!sPrefCacheInitialized) {
|
|
|
|
sPrefCacheInitialized = true;
|
|
|
|
Preferences::AddBoolVarCache(&sTestSensorEvents,
|
|
|
|
"device.sensors.test.events",
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sTestSensorEvents) {
|
|
|
|
nsCOMPtr<nsIRunnable> event = new DeviceSensorTestEvent(this, aType);
|
|
|
|
NS_DispatchToCurrentThread(event);
|
|
|
|
}
|
|
|
|
|
2011-06-20 09:36:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsDeviceSensors::RemoveWindowListener(uint32_t aType, nsIDOMWindow *aWindow)
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
2012-03-25 04:29:49 +04:00
|
|
|
if (mWindowListeners[aType]->IndexOf(aWindow) == NoIndex)
|
|
|
|
return NS_OK;
|
2011-06-20 09:36:17 +04:00
|
|
|
|
2012-03-25 04:29:49 +04:00
|
|
|
mWindowListeners[aType]->RemoveElement(aWindow);
|
2012-03-25 04:30:03 +04:00
|
|
|
|
|
|
|
if (mWindowListeners[aType]->Length() == 0)
|
|
|
|
UnregisterSensorObserver((SensorType)aType, this);
|
|
|
|
|
2011-06-20 09:36:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-25 04:29:57 +04:00
|
|
|
NS_IMETHODIMP nsDeviceSensors::RemoveWindowAsListener(nsIDOMWindow *aWindow)
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
2012-03-25 04:29:49 +04:00
|
|
|
for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
|
2012-03-25 04:30:03 +04:00
|
|
|
RemoveWindowListener((SensorType)i, aWindow);
|
2011-06-20 09:36:17 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-24 06:30:10 +04:00
|
|
|
static bool
|
2016-01-30 20:05:36 +03:00
|
|
|
WindowCannotReceiveSensorEvent (nsPIDOMWindowInner* aWindow)
|
2012-10-24 06:30:10 +04:00
|
|
|
{
|
|
|
|
// Check to see if this window is in the background. If
|
|
|
|
// it is and it does not have the "background-sensors" permission,
|
|
|
|
// don't send any device motion events to it.
|
2013-11-13 12:47:37 +04:00
|
|
|
if (!aWindow || !aWindow->IsCurrentInnerWindow()) {
|
2012-10-24 06:30:10 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-24 20:43:07 +03:00
|
|
|
bool disabled = aWindow->GetOuterWindow()->IsBackground() ||
|
|
|
|
!aWindow->IsTopLevelWindowActive();
|
|
|
|
if (!disabled) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> top = aWindow->GetScriptableTop();
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aWindow);
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> topSop = do_QueryInterface(top);
|
|
|
|
if (!sop || !topSop) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal* principal = sop->GetPrincipal();
|
|
|
|
nsIPrincipal* topPrincipal = topSop->GetPrincipal();
|
|
|
|
if (!principal || !topPrincipal) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
disabled = !principal->Subsumes(topPrincipal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disabled) {
|
2012-10-24 06:30:10 +04:00
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
2014-04-29 21:27:26 +04:00
|
|
|
services::GetPermissionManager();
|
2016-02-24 20:43:07 +03:00
|
|
|
NS_ENSURE_TRUE(permMgr, true);
|
2012-10-24 06:30:10 +04:00
|
|
|
uint32_t permission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
permMgr->TestPermissionFromWindow(aWindow, "background-sensors", &permission);
|
|
|
|
return permission != nsIPermissionManager::ALLOW_ACTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-15 04:03:56 +03:00
|
|
|
// Holds the device orientation in Euler angle degrees (azimuth, pitch, roll).
|
|
|
|
struct Orientation
|
|
|
|
{
|
2015-12-17 23:49:44 +03:00
|
|
|
enum OrientationReference
|
|
|
|
{
|
|
|
|
kRelative = 0,
|
|
|
|
kAbsolute
|
|
|
|
};
|
|
|
|
|
2015-12-15 04:03:56 +03:00
|
|
|
static Orientation RadToDeg(const Orientation& aOrient)
|
|
|
|
{
|
|
|
|
const static double kRadToDeg = 180.0 / M_PI;
|
|
|
|
return { aOrient.alpha * kRadToDeg,
|
|
|
|
aOrient.beta * kRadToDeg,
|
|
|
|
aOrient.gamma * kRadToDeg };
|
|
|
|
}
|
|
|
|
|
|
|
|
double alpha;
|
|
|
|
double beta;
|
|
|
|
double gamma;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Orientation
|
|
|
|
RotationVectorToOrientation(double aX, double aY, double aZ, double aW)
|
|
|
|
{
|
|
|
|
static const double kFuzzyOne = 1.0 - 1e-6;
|
|
|
|
static const double kCircleRad = 2.0 * M_PI;
|
|
|
|
|
|
|
|
Orientation orient = { 2.0 * std::atan2(aY, aW),
|
|
|
|
M_PI_2,
|
|
|
|
0.0 };
|
|
|
|
|
|
|
|
const double sqX = aX * aX;
|
|
|
|
const double sqY = aY * aY;
|
|
|
|
const double sqZ = aZ * aZ;
|
|
|
|
const double sqW = aW * aW;
|
|
|
|
const double unitLength = sqX + sqY + sqZ + sqW;
|
|
|
|
const double xwyz = 2.0 * (aX * aW + aY * aZ) / unitLength;
|
|
|
|
|
|
|
|
if (xwyz < -kFuzzyOne) {
|
|
|
|
orient.alpha *= -1.0;
|
|
|
|
orient.beta *= -1.0;
|
|
|
|
} else if (xwyz <= kFuzzyOne) {
|
|
|
|
const double gammaX = -sqX - sqY + sqZ + sqW;
|
|
|
|
const double gammaY = 2.0 * (aY * aW - aX * aZ);
|
|
|
|
const double alphaX = -sqX + sqY - sqZ + sqW;
|
|
|
|
const double alphaY = 2.0 * (aZ * aW - aX * aY);
|
|
|
|
const double fac = gammaX > 0 ? 1.0 : -1.0;
|
|
|
|
|
|
|
|
orient.alpha = std::fmod(kCircleRad + std::atan2(fac * alphaY, fac * alphaX),
|
|
|
|
kCircleRad);
|
|
|
|
orient.beta = fac * std::asin(xwyz);
|
|
|
|
orient.gamma = std::atan2(fac * gammaY, fac * gammaX);
|
|
|
|
if (fac < 0.0) {
|
|
|
|
orient.beta = fmod(M_PI + orient.beta, M_PI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Orientation::RadToDeg(orient);
|
|
|
|
}
|
|
|
|
|
2012-10-24 06:30:10 +04:00
|
|
|
void
|
2012-03-25 04:29:57 +04:00
|
|
|
nsDeviceSensors::Notify(const mozilla::hal::SensorData& aSensorData)
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t type = aSensorData.sensor();
|
2012-03-21 10:36:17 +04:00
|
|
|
|
2012-06-06 01:41:22 +04:00
|
|
|
const InfallibleTArray<float>& values = aSensorData.values();
|
|
|
|
size_t len = values.Length();
|
|
|
|
double x = len > 0 ? values[0] : 0.0;
|
|
|
|
double y = len > 1 ? values[1] : 0.0;
|
|
|
|
double z = len > 2 ? values[2] : 0.0;
|
2015-12-15 04:03:56 +03:00
|
|
|
double w = len > 3 ? values[3] : 0.0;
|
2016-04-04 20:22:59 +03:00
|
|
|
PRTime timestamp = aSensorData.timestamp();
|
2012-03-21 10:36:17 +04:00
|
|
|
|
2012-03-09 20:41:25 +04:00
|
|
|
nsCOMArray<nsIDOMWindow> windowListeners;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mWindowListeners[type]->Length(); i++) {
|
2012-03-25 04:29:49 +04:00
|
|
|
windowListeners.AppendObject(mWindowListeners[type]->SafeElementAt(i));
|
2011-06-20 09:36:17 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = windowListeners.Count(); i > 0 ; ) {
|
2011-06-20 09:36:17 +04:00
|
|
|
--i;
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> pwindow = do_QueryInterface(windowListeners[i]);
|
2012-10-24 06:30:10 +04:00
|
|
|
if (WindowCannotReceiveSensorEvent(pwindow)) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-01 06:39:49 +04:00
|
|
|
|
2015-10-27 00:37:32 +03:00
|
|
|
if (nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(pwindow->GetDoc())) {
|
2013-03-09 15:34:29 +04:00
|
|
|
nsCOMPtr<mozilla::dom::EventTarget> target = do_QueryInterface(windowListeners[i]);
|
2013-04-20 02:18:33 +04:00
|
|
|
if (type == nsIDeviceSensorData::TYPE_ACCELERATION ||
|
|
|
|
type == nsIDeviceSensorData::TYPE_LINEAR_ACCELERATION ||
|
2015-12-15 04:03:56 +03:00
|
|
|
type == nsIDeviceSensorData::TYPE_GYROSCOPE) {
|
2016-04-04 20:22:59 +03:00
|
|
|
FireDOMMotionEvent(domDoc, target, type, timestamp, x, y, z);
|
2015-12-15 04:03:56 +03:00
|
|
|
} else if (type == nsIDeviceSensorData::TYPE_ORIENTATION) {
|
2015-12-17 23:49:44 +03:00
|
|
|
FireDOMOrientationEvent(target, x, y, z, Orientation::kAbsolute);
|
2015-12-15 04:03:56 +03:00
|
|
|
} else if (type == nsIDeviceSensorData::TYPE_ROTATION_VECTOR) {
|
|
|
|
const Orientation orient = RotationVectorToOrientation(x, y, z, w);
|
2015-12-17 23:49:44 +03:00
|
|
|
FireDOMOrientationEvent(target, orient.alpha, orient.beta, orient.gamma,
|
|
|
|
Orientation::kAbsolute);
|
|
|
|
} else if (type == nsIDeviceSensorData::TYPE_GAME_ROTATION_VECTOR) {
|
|
|
|
const Orientation orient = RotationVectorToOrientation(x, y, z, w);
|
|
|
|
FireDOMOrientationEvent(target, orient.alpha, orient.beta, orient.gamma,
|
|
|
|
Orientation::kRelative);
|
2015-12-15 04:03:56 +03:00
|
|
|
} else if (type == nsIDeviceSensorData::TYPE_PROXIMITY) {
|
2012-05-02 20:43:45 +04:00
|
|
|
FireDOMProximityEvent(target, x, y, z);
|
2015-12-15 04:03:56 +03:00
|
|
|
} else if (type == nsIDeviceSensorData::TYPE_LIGHT) {
|
2012-05-02 20:43:45 +04:00
|
|
|
FireDOMLightEvent(target, x);
|
2015-12-15 04:03:56 +03:00
|
|
|
}
|
2011-06-20 09:36:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 20:43:45 +04:00
|
|
|
void
|
2013-03-09 15:34:29 +04:00
|
|
|
nsDeviceSensors::FireDOMLightEvent(mozilla::dom::EventTarget* aTarget,
|
|
|
|
double aValue)
|
2012-05-02 20:43:45 +04:00
|
|
|
{
|
2013-10-09 20:05:22 +04:00
|
|
|
DeviceLightEventInit init;
|
2013-09-14 22:59:50 +04:00
|
|
|
init.mBubbles = true;
|
|
|
|
init.mCancelable = false;
|
2016-09-08 09:07:59 +03:00
|
|
|
init.mValue = round(aValue);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DeviceLightEvent> event =
|
2013-09-14 22:59:50 +04:00
|
|
|
DeviceLightEvent::Constructor(aTarget, NS_LITERAL_STRING("devicelight"), init);
|
2012-05-02 20:43:45 +04:00
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
event->SetTrusted(true);
|
2012-05-02 20:43:45 +04:00
|
|
|
|
|
|
|
bool defaultActionEnabled;
|
|
|
|
aTarget->DispatchEvent(event, &defaultActionEnabled);
|
|
|
|
}
|
|
|
|
|
2012-05-02 20:43:45 +04:00
|
|
|
void
|
2013-03-09 15:34:29 +04:00
|
|
|
nsDeviceSensors::FireDOMProximityEvent(mozilla::dom::EventTarget* aTarget,
|
2012-05-02 20:43:45 +04:00
|
|
|
double aValue,
|
|
|
|
double aMin,
|
|
|
|
double aMax)
|
|
|
|
{
|
2013-10-09 20:05:22 +04:00
|
|
|
DeviceProximityEventInit init;
|
2013-09-12 02:35:51 +04:00
|
|
|
init.mBubbles = true;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mValue = aValue;
|
|
|
|
init.mMin = aMin;
|
|
|
|
init.mMax = aMax;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DeviceProximityEvent> event =
|
2013-09-12 02:35:51 +04:00
|
|
|
DeviceProximityEvent::Constructor(aTarget,
|
|
|
|
NS_LITERAL_STRING("deviceproximity"),
|
|
|
|
init);
|
2012-06-11 03:44:50 +04:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2012-05-02 20:43:45 +04:00
|
|
|
bool defaultActionEnabled;
|
|
|
|
aTarget->DispatchEvent(event, &defaultActionEnabled);
|
2012-05-17 00:27:54 +04:00
|
|
|
|
|
|
|
// Some proximity sensors only support a binary near or
|
|
|
|
// far measurement. In this case, the sensor should report
|
|
|
|
// its maximum range value in the far state and a lesser
|
|
|
|
// value in the near state.
|
|
|
|
|
|
|
|
bool near = (aValue < aMax);
|
|
|
|
if (mIsUserProximityNear != near) {
|
|
|
|
mIsUserProximityNear = near;
|
|
|
|
FireDOMUserProximityEvent(aTarget, mIsUserProximityNear);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-09 15:34:29 +04:00
|
|
|
nsDeviceSensors::FireDOMUserProximityEvent(mozilla::dom::EventTarget* aTarget,
|
|
|
|
bool aNear)
|
2012-05-17 00:27:54 +04:00
|
|
|
{
|
2013-10-09 20:05:22 +04:00
|
|
|
UserProximityEventInit init;
|
2013-09-14 22:59:51 +04:00
|
|
|
init.mBubbles = true;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mNear = aNear;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UserProximityEvent> event =
|
2013-09-14 22:59:51 +04:00
|
|
|
UserProximityEvent::Constructor(aTarget,
|
|
|
|
NS_LITERAL_STRING("userproximity"),
|
|
|
|
init);
|
2012-05-17 00:27:54 +04:00
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2012-05-17 00:27:54 +04:00
|
|
|
bool defaultActionEnabled;
|
|
|
|
aTarget->DispatchEvent(event, &defaultActionEnabled);
|
2012-05-02 20:43:45 +04:00
|
|
|
}
|
|
|
|
|
2011-06-20 09:36:17 +04:00
|
|
|
void
|
2014-07-01 03:02:03 +04:00
|
|
|
nsDeviceSensors::FireDOMOrientationEvent(EventTarget* aTarget,
|
|
|
|
double aAlpha,
|
|
|
|
double aBeta,
|
2015-12-17 23:49:44 +03:00
|
|
|
double aGamma,
|
|
|
|
bool aIsAbsolute)
|
2011-06-20 09:36:17 +04:00
|
|
|
{
|
2014-07-01 03:02:03 +04:00
|
|
|
DeviceOrientationEventInit init;
|
|
|
|
init.mBubbles = true;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mAlpha.SetValue(aAlpha);
|
|
|
|
init.mBeta.SetValue(aBeta);
|
|
|
|
init.mGamma.SetValue(aGamma);
|
2015-12-17 23:49:44 +03:00
|
|
|
init.mAbsolute = aIsAbsolute;
|
2014-07-01 03:02:03 +04:00
|
|
|
|
2015-12-18 01:14:51 +03:00
|
|
|
auto Dispatch = [&](EventTarget* aEventTarget, const nsAString& aType)
|
|
|
|
{
|
|
|
|
RefPtr<DeviceOrientationEvent> event =
|
|
|
|
DeviceOrientationEvent::Constructor(aEventTarget, aType, init);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
bool dummy;
|
|
|
|
aEventTarget->DispatchEvent(event, &dummy);
|
|
|
|
};
|
2013-04-20 02:18:33 +04:00
|
|
|
|
2015-12-18 01:14:51 +03:00
|
|
|
Dispatch(aTarget, aIsAbsolute ? NS_LITERAL_STRING("absolutedeviceorientation") :
|
|
|
|
NS_LITERAL_STRING("deviceorientation"));
|
|
|
|
|
|
|
|
// This is used to determine whether relative events have been dispatched
|
|
|
|
// during the current session, in which case we don't dispatch the additional
|
|
|
|
// compatibility events.
|
|
|
|
static bool sIsDispatchingRelativeEvents = false;
|
|
|
|
sIsDispatchingRelativeEvents = sIsDispatchingRelativeEvents || !aIsAbsolute;
|
|
|
|
|
|
|
|
// Android devices with SENSOR_GAME_ROTATION_VECTOR support dispatch
|
|
|
|
// relative events for "deviceorientation" by default, while other platforms
|
|
|
|
// and devices without such support dispatch absolute events by default.
|
|
|
|
if (aIsAbsolute && !sIsDispatchingRelativeEvents) {
|
|
|
|
// For absolute events on devices without support for relative events,
|
|
|
|
// we need to additionally dispatch type "deviceorientation" to keep
|
|
|
|
// backwards-compatibility.
|
|
|
|
Dispatch(aTarget, NS_LITERAL_STRING("deviceorientation"));
|
|
|
|
}
|
2011-06-20 09:36:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-25 04:29:57 +04:00
|
|
|
nsDeviceSensors::FireDOMMotionEvent(nsIDOMDocument *domdoc,
|
2013-04-20 02:18:33 +04:00
|
|
|
EventTarget* target,
|
|
|
|
uint32_t type,
|
2016-04-04 20:22:59 +03:00
|
|
|
PRTime timestamp,
|
2013-04-20 02:18:33 +04:00
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
double z)
|
|
|
|
{
|
2012-03-09 22:40:43 +04:00
|
|
|
// Attempt to coalesce events
|
2016-02-24 20:43:07 +03:00
|
|
|
TimeDuration sensorPollDuration =
|
|
|
|
TimeDuration::FromMilliseconds(DEFAULT_SENSOR_POLL);
|
|
|
|
bool fireEvent =
|
|
|
|
(TimeStamp::Now() > mLastDOMMotionEventTime + sensorPollDuration) ||
|
|
|
|
sTestSensorEvents;
|
2012-03-14 03:57:51 +04:00
|
|
|
|
|
|
|
switch (type) {
|
2012-03-25 04:29:57 +04:00
|
|
|
case nsIDeviceSensorData::TYPE_LINEAR_ACCELERATION:
|
2014-08-14 02:39:41 +04:00
|
|
|
if (!mLastAcceleration) {
|
|
|
|
mLastAcceleration.emplace();
|
2014-01-02 02:52:47 +04:00
|
|
|
}
|
2014-08-14 02:39:41 +04:00
|
|
|
mLastAcceleration->mX.SetValue(x);
|
|
|
|
mLastAcceleration->mY.SetValue(y);
|
|
|
|
mLastAcceleration->mZ.SetValue(z);
|
2012-03-25 04:29:49 +04:00
|
|
|
break;
|
2012-03-25 04:29:57 +04:00
|
|
|
case nsIDeviceSensorData::TYPE_ACCELERATION:
|
2014-11-08 07:48:38 +03:00
|
|
|
if (!mLastAccelerationIncludingGravity) {
|
|
|
|
mLastAccelerationIncludingGravity.emplace();
|
2014-01-02 02:52:47 +04:00
|
|
|
}
|
2014-11-08 07:48:38 +03:00
|
|
|
mLastAccelerationIncludingGravity->mX.SetValue(x);
|
|
|
|
mLastAccelerationIncludingGravity->mY.SetValue(y);
|
|
|
|
mLastAccelerationIncludingGravity->mZ.SetValue(z);
|
2012-03-25 04:29:49 +04:00
|
|
|
break;
|
2012-03-25 04:29:57 +04:00
|
|
|
case nsIDeviceSensorData::TYPE_GYROSCOPE:
|
2014-08-14 02:39:41 +04:00
|
|
|
if (!mLastRotationRate) {
|
|
|
|
mLastRotationRate.emplace();
|
2014-01-02 02:52:47 +04:00
|
|
|
}
|
2014-08-14 02:39:41 +04:00
|
|
|
mLastRotationRate->mAlpha.SetValue(x);
|
|
|
|
mLastRotationRate->mBeta.SetValue(y);
|
|
|
|
mLastRotationRate->mGamma.SetValue(z);
|
2012-03-25 04:29:49 +04:00
|
|
|
break;
|
2012-03-14 03:57:51 +04:00
|
|
|
}
|
|
|
|
|
2014-01-05 12:36:19 +04:00
|
|
|
if (fireEvent) {
|
2014-08-14 02:39:41 +04:00
|
|
|
if (!mLastAcceleration) {
|
|
|
|
mLastAcceleration.emplace();
|
2014-01-05 12:36:19 +04:00
|
|
|
}
|
2014-11-08 07:48:38 +03:00
|
|
|
if (!mLastAccelerationIncludingGravity) {
|
|
|
|
mLastAccelerationIncludingGravity.emplace();
|
2014-01-05 12:36:19 +04:00
|
|
|
}
|
2014-08-14 02:39:41 +04:00
|
|
|
if (!mLastRotationRate) {
|
|
|
|
mLastRotationRate.emplace();
|
2014-01-05 12:36:19 +04:00
|
|
|
}
|
2014-08-14 02:39:41 +04:00
|
|
|
} else if (!mLastAcceleration ||
|
2014-11-08 07:48:38 +03:00
|
|
|
!mLastAccelerationIncludingGravity ||
|
2014-08-14 02:39:41 +04:00
|
|
|
!mLastRotationRate) {
|
2012-03-25 04:29:49 +04:00
|
|
|
return;
|
2012-03-09 22:40:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
domdoc->CreateEvent(NS_LITERAL_STRING("DeviceMotionEvent"), getter_AddRefs(event));
|
|
|
|
|
2014-02-27 14:51:12 +04:00
|
|
|
DeviceMotionEvent* me = static_cast<DeviceMotionEvent*>(event.get());
|
2011-06-20 09:36:17 +04:00
|
|
|
|
|
|
|
me->InitDeviceMotionEvent(NS_LITERAL_STRING("devicemotion"),
|
2011-10-17 18:59:28 +04:00
|
|
|
true,
|
|
|
|
false,
|
2014-08-14 02:39:41 +04:00
|
|
|
*mLastAcceleration,
|
2014-11-08 07:48:38 +03:00
|
|
|
*mLastAccelerationIncludingGravity,
|
2014-08-14 02:39:41 +04:00
|
|
|
*mLastRotationRate,
|
2016-04-04 20:22:59 +03:00
|
|
|
Nullable<double>(DEFAULT_SENSOR_POLL),
|
|
|
|
Nullable<uint64_t>(timestamp));
|
2011-06-20 09:36:17 +04:00
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
event->SetTrusted(true);
|
2012-03-25 04:29:49 +04:00
|
|
|
|
2012-03-09 22:40:43 +04:00
|
|
|
bool defaultActionEnabled = true;
|
2011-06-20 09:36:17 +04:00
|
|
|
target->DispatchEvent(event, &defaultActionEnabled);
|
2012-03-09 22:40:43 +04:00
|
|
|
|
2014-08-14 02:39:41 +04:00
|
|
|
mLastRotationRate.reset();
|
2014-11-08 07:48:38 +03:00
|
|
|
mLastAccelerationIncludingGravity.reset();
|
2014-08-14 02:39:41 +04:00
|
|
|
mLastAcceleration.reset();
|
2012-03-09 22:40:43 +04:00
|
|
|
mLastDOMMotionEventTime = TimeStamp::Now();
|
2012-03-21 10:36:17 +04:00
|
|
|
}
|