2012-05-21 04:12:37 -07: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-19 22:36:17 -07:00
|
|
|
|
2012-03-20 23:36:17 -07:00
|
|
|
#include "mozilla/Hal.h"
|
|
|
|
#include "mozilla/HalSensor.h"
|
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
#include "nsDeviceSensors.h"
|
2011-06-19 22:36:17 -07:00
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2011-08-31 19:39:49 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2011-06-19 22:36:17 -07:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2012-07-20 09:42:08 -07:00
|
|
|
#include "GeneratedEvents.h"
|
2012-04-03 21:09:20 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-06-14 19:31:55 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-04-29 10:27:26 -07:00
|
|
|
#include "mozilla/Services.h"
|
2012-10-23 19:30:10 -07:00
|
|
|
#include "nsIPermissionManager.h"
|
2013-09-14 11:59:50 -07:00
|
|
|
#include "mozilla/dom/DeviceLightEvent.h"
|
2013-09-11 15:35:51 -07:00
|
|
|
#include "mozilla/dom/DeviceProximityEvent.h"
|
2013-09-14 11:59:51 -07:00
|
|
|
#include "mozilla/dom/UserProximityEvent.h"
|
2012-03-09 10:40:43 -08:00
|
|
|
|
2012-03-20 23:36:17 -07:00
|
|
|
using namespace mozilla;
|
2013-04-19 15:18:33 -07:00
|
|
|
using namespace mozilla::dom;
|
2012-03-20 23:36:17 -07:00
|
|
|
using namespace hal;
|
2012-03-13 09:59:24 -07:00
|
|
|
|
2012-05-16 13:27:54 -07:00
|
|
|
#undef near
|
|
|
|
|
2012-03-13 16:57:09 -07:00
|
|
|
// also see sDefaultSensorHint in mobile/android/base/GeckoAppShell.java
|
|
|
|
#define DEFAULT_SENSOR_POLL 100
|
|
|
|
|
2011-08-09 22:36:00 -07:00
|
|
|
static const nsTArray<nsIDOMWindow*>::index_type NoIndex =
|
2012-03-24 17:29:49 -07:00
|
|
|
nsTArray<nsIDOMWindow*>::NoIndex;
|
2011-07-26 09:27:11 -07:00
|
|
|
|
2012-06-14 19:31:55 -07:00
|
|
|
class nsDeviceSensorData MOZ_FINAL : public nsIDeviceSensorData
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2012-03-24 17:29:57 -07:00
|
|
|
NS_DECL_NSIDEVICESENSORDATA
|
2011-06-19 22:36:17 -07:00
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
nsDeviceSensorData(unsigned long type, double x, double y, double z);
|
2011-06-19 22:36:17 -07:00
|
|
|
|
|
|
|
private:
|
2012-03-24 17:29:57 -07:00
|
|
|
~nsDeviceSensorData();
|
2011-06-19 22:36:17 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
unsigned long mType;
|
|
|
|
double mX, mY, mZ;
|
|
|
|
};
|
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
nsDeviceSensorData::nsDeviceSensorData(unsigned long type, double x, double y, double z)
|
2011-06-19 22:36:17 -07:00
|
|
|
: mType(type), mX(x), mY(y), mZ(z)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDeviceSensorData)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDeviceSensorData)
|
2011-06-19 22:36:17 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
NS_IMPL_ADDREF(nsDeviceSensorData)
|
|
|
|
NS_IMPL_RELEASE(nsDeviceSensorData)
|
2011-06-19 22:36:17 -07:00
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
nsDeviceSensorData::~nsDeviceSensorData()
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHODIMP nsDeviceSensorData::GetType(uint32_t *aType)
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
|
|
|
*aType = mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
NS_IMETHODIMP nsDeviceSensorData::GetX(double *aX)
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aX);
|
|
|
|
*aX = mX;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
NS_IMETHODIMP nsDeviceSensorData::GetY(double *aY)
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aY);
|
|
|
|
*aY = mY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
NS_IMETHODIMP nsDeviceSensorData::GetZ(double *aZ)
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aZ);
|
|
|
|
*aZ = mZ;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsDeviceSensors, nsIDeviceSensors)
|
2011-06-19 22:36:17 -07:00
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
nsDeviceSensors::nsDeviceSensors()
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
2012-05-16 13:27:54 -07:00
|
|
|
mIsUserProximityNear = false;
|
2012-03-20 23:36:17 -07:00
|
|
|
mLastDOMMotionEventTime = TimeStamp::Now();
|
2012-05-02 09:43:45 -07:00
|
|
|
mEnabled = Preferences::GetBool("device.sensors.enabled", true);
|
2012-03-20 23:36:17 -07:00
|
|
|
|
2012-03-24 17:29:49 -07:00
|
|
|
for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
|
|
|
|
nsTArray<nsIDOMWindow*> *windows = new nsTArray<nsIDOMWindow*>();
|
|
|
|
mWindowListeners.AppendElement(windows);
|
|
|
|
}
|
2011-06-19 22:36:17 -07:00
|
|
|
|
2012-03-24 17:29:49 -07:00
|
|
|
mLastDOMMotionEventTime = TimeStamp::Now();
|
2011-06-19 22:36:17 -07:00
|
|
|
}
|
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
nsDeviceSensors::~nsDeviceSensors()
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
2012-03-24 17:29:49 -07:00
|
|
|
for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
|
|
|
|
if (IsSensorEnabled(i))
|
|
|
|
UnregisterSensorObserver((SensorType)i, this);
|
2011-06-19 22:36:17 -07:00
|
|
|
}
|
2012-03-24 17:29:49 -07:00
|
|
|
|
|
|
|
for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
|
|
|
|
delete mWindowListeners[i];
|
2011-06-19 22:36:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-20 21:40:56 -07:00
|
|
|
NS_IMETHODIMP nsDeviceSensors::HasWindowListener(uint32_t aType, nsIDOMWindow *aWindow, bool *aRetVal)
|
2012-06-10 16:44:50 -07:00
|
|
|
{
|
2012-10-20 21:40:56 -07:00
|
|
|
if (!mEnabled)
|
|
|
|
*aRetVal = false;
|
|
|
|
else
|
|
|
|
*aRetVal = mWindowListeners[aType]->IndexOf(aWindow) != NoIndex;
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHODIMP nsDeviceSensors::AddWindowListener(uint32_t aType, nsIDOMWindow *aWindow)
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
2012-05-02 09:43:45 -07:00
|
|
|
if (!mEnabled)
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-03-24 17:29:49 -07:00
|
|
|
if (mWindowListeners[aType]->IndexOf(aWindow) != NoIndex)
|
|
|
|
return NS_OK;
|
2011-06-19 22:36:17 -07:00
|
|
|
|
2012-03-24 17:29:49 -07:00
|
|
|
if (!IsSensorEnabled(aType)) {
|
|
|
|
RegisterSensorObserver((SensorType)aType, this);
|
2011-06-19 22:36:17 -07:00
|
|
|
}
|
|
|
|
|
2012-03-24 17:29:49 -07:00
|
|
|
mWindowListeners[aType]->AppendElement(aWindow);
|
2011-06-19 22:36:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHODIMP nsDeviceSensors::RemoveWindowListener(uint32_t aType, nsIDOMWindow *aWindow)
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
2012-03-24 17:29:49 -07:00
|
|
|
if (mWindowListeners[aType]->IndexOf(aWindow) == NoIndex)
|
|
|
|
return NS_OK;
|
2011-06-19 22:36:17 -07:00
|
|
|
|
2012-03-24 17:29:49 -07:00
|
|
|
mWindowListeners[aType]->RemoveElement(aWindow);
|
2012-03-24 17:30:03 -07:00
|
|
|
|
|
|
|
if (mWindowListeners[aType]->Length() == 0)
|
|
|
|
UnregisterSensorObserver((SensorType)aType, this);
|
|
|
|
|
2011-06-19 22:36:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-24 17:29:57 -07:00
|
|
|
NS_IMETHODIMP nsDeviceSensors::RemoveWindowAsListener(nsIDOMWindow *aWindow)
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
2012-03-24 17:29:49 -07:00
|
|
|
for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
|
2012-03-24 17:30:03 -07:00
|
|
|
RemoveWindowListener((SensorType)i, aWindow);
|
2011-06-19 22:36:17 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-23 19:30:10 -07:00
|
|
|
static bool
|
|
|
|
WindowCannotReceiveSensorEvent (nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
// 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 00:47:37 -08:00
|
|
|
if (!aWindow || !aWindow->IsCurrentInnerWindow()) {
|
2012-10-23 19:30:10 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWindow->GetOuterWindow()->IsBackground()) {
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
2014-04-29 10:27:26 -07:00
|
|
|
services::GetPermissionManager();
|
2012-10-23 19:30:10 -07:00
|
|
|
NS_ENSURE_TRUE(permMgr, false);
|
|
|
|
uint32_t permission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
permMgr->TestPermissionFromWindow(aWindow, "background-sensors", &permission);
|
|
|
|
return permission != nsIPermissionManager::ALLOW_ACTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-24 17:29:57 -07:00
|
|
|
nsDeviceSensors::Notify(const mozilla::hal::SensorData& aSensorData)
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t type = aSensorData.sensor();
|
2012-03-20 23:36:17 -07:00
|
|
|
|
2012-06-05 14:41:22 -07: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;
|
2012-03-20 23:36:17 -07:00
|
|
|
|
2012-03-09 08:41:25 -08:00
|
|
|
nsCOMArray<nsIDOMWindow> windowListeners;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mWindowListeners[type]->Length(); i++) {
|
2012-03-24 17:29:49 -07:00
|
|
|
windowListeners.AppendObject(mWindowListeners[type]->SafeElementAt(i));
|
2011-06-19 22:36:17 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = windowListeners.Count(); i > 0 ; ) {
|
2011-06-19 22:36:17 -07:00
|
|
|
--i;
|
|
|
|
|
2012-03-09 08:41:25 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> pwindow = do_QueryInterface(windowListeners[i]);
|
2012-10-23 19:30:10 -07:00
|
|
|
if (WindowCannotReceiveSensorEvent(pwindow)) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-31 19:39:49 -07:00
|
|
|
|
2011-06-19 22:36:17 -07:00
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
2012-03-09 08:41:25 -08:00
|
|
|
windowListeners[i]->GetDocument(getter_AddRefs(domdoc));
|
2011-06-19 22:36:17 -07:00
|
|
|
|
|
|
|
if (domdoc) {
|
2013-03-09 03:34:29 -08:00
|
|
|
nsCOMPtr<mozilla::dom::EventTarget> target = do_QueryInterface(windowListeners[i]);
|
2013-04-19 15:18:33 -07:00
|
|
|
if (type == nsIDeviceSensorData::TYPE_ACCELERATION ||
|
|
|
|
type == nsIDeviceSensorData::TYPE_LINEAR_ACCELERATION ||
|
2012-03-24 17:29:57 -07:00
|
|
|
type == nsIDeviceSensorData::TYPE_GYROSCOPE)
|
2012-03-13 16:57:51 -07:00
|
|
|
FireDOMMotionEvent(domdoc, target, type, x, y, z);
|
2012-03-24 17:29:57 -07:00
|
|
|
else if (type == nsIDeviceSensorData::TYPE_ORIENTATION)
|
2011-06-19 22:36:17 -07:00
|
|
|
FireDOMOrientationEvent(domdoc, target, x, y, z);
|
2012-05-02 09:43:45 -07:00
|
|
|
else if (type == nsIDeviceSensorData::TYPE_PROXIMITY)
|
|
|
|
FireDOMProximityEvent(target, x, y, z);
|
2012-05-02 09:43:45 -07:00
|
|
|
else if (type == nsIDeviceSensorData::TYPE_LIGHT)
|
|
|
|
FireDOMLightEvent(target, x);
|
|
|
|
|
2011-06-19 22:36:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 09:43:45 -07:00
|
|
|
void
|
2013-03-09 03:34:29 -08:00
|
|
|
nsDeviceSensors::FireDOMLightEvent(mozilla::dom::EventTarget* aTarget,
|
|
|
|
double aValue)
|
2012-05-02 09:43:45 -07:00
|
|
|
{
|
2013-10-09 09:05:22 -07:00
|
|
|
DeviceLightEventInit init;
|
2013-09-14 11:59:50 -07:00
|
|
|
init.mBubbles = true;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mValue = aValue;
|
|
|
|
nsRefPtr<DeviceLightEvent> event =
|
|
|
|
DeviceLightEvent::Constructor(aTarget, NS_LITERAL_STRING("devicelight"), init);
|
2012-05-02 09:43:45 -07:00
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
event->SetTrusted(true);
|
2012-05-02 09:43:45 -07:00
|
|
|
|
|
|
|
bool defaultActionEnabled;
|
|
|
|
aTarget->DispatchEvent(event, &defaultActionEnabled);
|
|
|
|
}
|
|
|
|
|
2012-05-02 09:43:45 -07:00
|
|
|
void
|
2013-03-09 03:34:29 -08:00
|
|
|
nsDeviceSensors::FireDOMProximityEvent(mozilla::dom::EventTarget* aTarget,
|
2012-05-02 09:43:45 -07:00
|
|
|
double aValue,
|
|
|
|
double aMin,
|
|
|
|
double aMax)
|
|
|
|
{
|
2013-10-09 09:05:22 -07:00
|
|
|
DeviceProximityEventInit init;
|
2013-09-11 15:35:51 -07:00
|
|
|
init.mBubbles = true;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mValue = aValue;
|
|
|
|
init.mMin = aMin;
|
|
|
|
init.mMax = aMax;
|
|
|
|
nsRefPtr<DeviceProximityEvent> event =
|
|
|
|
DeviceProximityEvent::Constructor(aTarget,
|
|
|
|
NS_LITERAL_STRING("deviceproximity"),
|
|
|
|
init);
|
2012-06-10 16:44:50 -07:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2012-05-02 09:43:45 -07:00
|
|
|
bool defaultActionEnabled;
|
|
|
|
aTarget->DispatchEvent(event, &defaultActionEnabled);
|
2012-05-16 13:27:54 -07: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 03:34:29 -08:00
|
|
|
nsDeviceSensors::FireDOMUserProximityEvent(mozilla::dom::EventTarget* aTarget,
|
|
|
|
bool aNear)
|
2012-05-16 13:27:54 -07:00
|
|
|
{
|
2013-10-09 09:05:22 -07:00
|
|
|
UserProximityEventInit init;
|
2013-09-14 11:59:51 -07:00
|
|
|
init.mBubbles = true;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mNear = aNear;
|
|
|
|
nsRefPtr<UserProximityEvent> event =
|
|
|
|
UserProximityEvent::Constructor(aTarget,
|
|
|
|
NS_LITERAL_STRING("userproximity"),
|
|
|
|
init);
|
2012-05-16 13:27:54 -07:00
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2012-05-16 13:27:54 -07:00
|
|
|
bool defaultActionEnabled;
|
|
|
|
aTarget->DispatchEvent(event, &defaultActionEnabled);
|
2012-05-02 09:43:45 -07:00
|
|
|
}
|
|
|
|
|
2011-06-19 22:36:17 -07:00
|
|
|
void
|
2013-04-19 15:18:33 -07:00
|
|
|
nsDeviceSensors::FireDOMOrientationEvent(nsIDOMDocument* domdoc,
|
|
|
|
EventTarget* target,
|
2012-05-16 13:27:54 -07:00
|
|
|
double alpha,
|
|
|
|
double beta,
|
|
|
|
double gamma)
|
2011-06-19 22:36:17 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool defaultActionEnabled = true;
|
2011-06-19 22:36:17 -07:00
|
|
|
domdoc->CreateEvent(NS_LITERAL_STRING("DeviceOrientationEvent"), getter_AddRefs(event));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDeviceOrientationEvent> oe = do_QueryInterface(event);
|
|
|
|
|
|
|
|
if (!oe) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
oe->InitDeviceOrientationEvent(NS_LITERAL_STRING("deviceorientation"),
|
2011-10-17 07:59:28 -07:00
|
|
|
true,
|
|
|
|
false,
|
2011-06-19 22:36:17 -07:00
|
|
|
alpha,
|
|
|
|
beta,
|
|
|
|
gamma,
|
2011-10-17 07:59:28 -07:00
|
|
|
true);
|
2011-06-19 22:36:17 -07:00
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
event->SetTrusted(true);
|
2013-04-19 15:18:33 -07:00
|
|
|
|
2011-06-19 22:36:17 -07:00
|
|
|
target->DispatchEvent(event, &defaultActionEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-03-24 17:29:57 -07:00
|
|
|
nsDeviceSensors::FireDOMMotionEvent(nsIDOMDocument *domdoc,
|
2013-04-19 15:18:33 -07:00
|
|
|
EventTarget* target,
|
|
|
|
uint32_t type,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
double z)
|
|
|
|
{
|
2012-03-09 10:40:43 -08:00
|
|
|
// Attempt to coalesce events
|
|
|
|
bool fireEvent = TimeStamp::Now() > mLastDOMMotionEventTime + TimeDuration::FromMilliseconds(DEFAULT_SENSOR_POLL);
|
2012-03-13 16:57:51 -07:00
|
|
|
|
|
|
|
switch (type) {
|
2012-03-24 17:29:57 -07:00
|
|
|
case nsIDeviceSensorData::TYPE_LINEAR_ACCELERATION:
|
2014-01-01 14:52:47 -08:00
|
|
|
if (mLastAcceleration.empty()) {
|
|
|
|
mLastAcceleration.construct();
|
|
|
|
}
|
|
|
|
mLastAcceleration.ref().mX.SetValue(x);
|
|
|
|
mLastAcceleration.ref().mY.SetValue(y);
|
|
|
|
mLastAcceleration.ref().mZ.SetValue(z);
|
2012-03-24 17:29:49 -07:00
|
|
|
break;
|
2012-03-24 17:29:57 -07:00
|
|
|
case nsIDeviceSensorData::TYPE_ACCELERATION:
|
2014-01-01 14:52:47 -08:00
|
|
|
if (mLastAccelerationIncluduingGravity.empty()) {
|
|
|
|
mLastAccelerationIncluduingGravity.construct();
|
|
|
|
}
|
|
|
|
mLastAccelerationIncluduingGravity.ref().mX.SetValue(x);
|
|
|
|
mLastAccelerationIncluduingGravity.ref().mY.SetValue(y);
|
|
|
|
mLastAccelerationIncluduingGravity.ref().mZ.SetValue(z);
|
2012-03-24 17:29:49 -07:00
|
|
|
break;
|
2012-03-24 17:29:57 -07:00
|
|
|
case nsIDeviceSensorData::TYPE_GYROSCOPE:
|
2014-01-01 14:52:47 -08:00
|
|
|
if (mLastRotationRate.empty()) {
|
|
|
|
mLastRotationRate.construct();
|
|
|
|
}
|
|
|
|
mLastRotationRate.ref().mAlpha.SetValue(x);
|
|
|
|
mLastRotationRate.ref().mBeta.SetValue(y);
|
|
|
|
mLastRotationRate.ref().mGamma.SetValue(z);
|
2012-03-24 17:29:49 -07:00
|
|
|
break;
|
2012-03-13 16:57:51 -07:00
|
|
|
}
|
|
|
|
|
2014-01-05 00:36:19 -08:00
|
|
|
if (fireEvent) {
|
|
|
|
if (mLastAcceleration.empty()) {
|
|
|
|
mLastAcceleration.construct();
|
|
|
|
}
|
|
|
|
if (mLastAccelerationIncluduingGravity.empty()) {
|
|
|
|
mLastAccelerationIncluduingGravity.construct();
|
|
|
|
}
|
|
|
|
if (mLastRotationRate.empty()) {
|
|
|
|
mLastRotationRate.construct();
|
|
|
|
}
|
|
|
|
} else if (mLastAcceleration.empty() ||
|
|
|
|
mLastAccelerationIncluduingGravity.empty() ||
|
|
|
|
mLastRotationRate.empty()) {
|
2012-03-24 17:29:49 -07:00
|
|
|
return;
|
2012-03-09 10:40:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
domdoc->CreateEvent(NS_LITERAL_STRING("DeviceMotionEvent"), getter_AddRefs(event));
|
|
|
|
|
2014-02-27 02:51:12 -08:00
|
|
|
DeviceMotionEvent* me = static_cast<DeviceMotionEvent*>(event.get());
|
2011-06-19 22:36:17 -07:00
|
|
|
|
2014-01-01 14:52:47 -08:00
|
|
|
ErrorResult rv;
|
2011-06-19 22:36:17 -07:00
|
|
|
me->InitDeviceMotionEvent(NS_LITERAL_STRING("devicemotion"),
|
2011-10-17 07:59:28 -07:00
|
|
|
true,
|
|
|
|
false,
|
2014-01-01 14:52:47 -08:00
|
|
|
mLastAcceleration.ref(),
|
|
|
|
mLastAccelerationIncluduingGravity.ref(),
|
|
|
|
mLastRotationRate.ref(),
|
|
|
|
Nullable<double>(DEFAULT_SENSOR_POLL),
|
|
|
|
rv);
|
2011-06-19 22:36:17 -07:00
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
event->SetTrusted(true);
|
2012-03-24 17:29:49 -07:00
|
|
|
|
2012-03-09 10:40:43 -08:00
|
|
|
bool defaultActionEnabled = true;
|
2011-06-19 22:36:17 -07:00
|
|
|
target->DispatchEvent(event, &defaultActionEnabled);
|
2012-03-09 10:40:43 -08:00
|
|
|
|
2014-01-01 14:52:47 -08:00
|
|
|
mLastRotationRate.destroy();
|
|
|
|
mLastAccelerationIncluduingGravity.destroy();
|
|
|
|
mLastAcceleration.destroy();
|
2012-03-09 10:40:43 -08:00
|
|
|
mLastDOMMotionEventTime = TimeStamp::Now();
|
2012-03-20 23:36:17 -07:00
|
|
|
}
|