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