mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-09 03:15:11 +00:00
Bug 615597 - Implement the W3C DeviceOrientation event. r=smaug,blassey
This commit is contained in:
parent
7c7432c3be
commit
3c5959f2f9
@ -1698,7 +1698,7 @@ GK_ATOM(onMozTouchMove, "onMozTouchMove")
|
||||
GK_ATOM(onMozTouchUp, "onMozTouchUp")
|
||||
|
||||
// orientation support
|
||||
GK_ATOM(onMozOrientation, "onMozOrientation")
|
||||
GK_ATOM(ondeviceorientation, "ondeviceorientation")
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Special atoms
|
||||
|
@ -90,7 +90,7 @@ NS_NewDOMMutationEvent(nsIDOMEvent** aResult NS_OUTPARAM, nsPresContext* aPresCo
|
||||
nsresult
|
||||
NS_NewDOMPopupBlockedEvent(nsIDOMEvent** aResult, nsPresContext* aPresContext, nsEvent* aEvent);
|
||||
nsresult
|
||||
NS_NewDOMOrientationEvent(nsIDOMEvent** aResult, nsPresContext* aPresContext, nsEvent* aEvent);
|
||||
NS_NewDOMDeviceOrientationEvent(nsIDOMEvent** aResult, nsPresContext* aPresContext, nsEvent* aEvent);
|
||||
nsresult
|
||||
NS_NewDOMTextEvent(nsIDOMEvent** aResult, nsPresContext* aPresContext, class nsTextEvent* aEvent);
|
||||
nsresult
|
||||
|
@ -40,59 +40,75 @@
|
||||
NS_IMPL_ADDREF_INHERITED(nsDOMOrientationEvent, nsDOMEvent)
|
||||
NS_IMPL_RELEASE_INHERITED(nsDOMOrientationEvent, nsDOMEvent)
|
||||
|
||||
DOMCI_DATA(OrientationEvent, nsDOMOrientationEvent)
|
||||
DOMCI_DATA(DeviceOrientationEvent, nsDOMOrientationEvent)
|
||||
|
||||
NS_INTERFACE_MAP_BEGIN(nsDOMOrientationEvent)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIDOMOrientationEvent)
|
||||
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(OrientationEvent)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIDOMDeviceOrientationEvent)
|
||||
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DeviceOrientationEvent)
|
||||
NS_INTERFACE_MAP_END_INHERITING(nsDOMEvent)
|
||||
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::InitOrientationEvent(const nsAString & eventTypeArg,
|
||||
PRBool canBubbleArg,
|
||||
PRBool cancelableArg,
|
||||
double x,
|
||||
double y,
|
||||
double z)
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::InitDeviceOrientationEvent(const nsAString & aEventTypeArg,
|
||||
PRBool aCanBubbleArg,
|
||||
PRBool aCancelableArg,
|
||||
double aAlpha,
|
||||
double aBeta,
|
||||
double aGamma,
|
||||
PRBool aAbsolute)
|
||||
{
|
||||
nsresult rv = nsDOMEvent::InitEvent(eventTypeArg, canBubbleArg, cancelableArg);
|
||||
nsresult rv = nsDOMEvent::InitEvent(aEventTypeArg, aCanBubbleArg, aCancelableArg);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
mX = x;
|
||||
mY = y;
|
||||
mZ = z;
|
||||
mAlpha = aAlpha;
|
||||
mBeta = aBeta;
|
||||
mGamma = aGamma;
|
||||
mAbsolute = aAbsolute;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::GetX(double *aX)
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::GetAlpha(double *aAlpha)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aX);
|
||||
NS_ENSURE_ARG_POINTER(aAlpha);
|
||||
|
||||
*aX = mX;
|
||||
*aAlpha = mAlpha;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::GetY(double *aY)
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::GetBeta(double *aBeta)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aY);
|
||||
NS_ENSURE_ARG_POINTER(aBeta);
|
||||
|
||||
*aY = mY;
|
||||
*aBeta = mBeta;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::GetZ(double *aZ)
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::GetGamma(double *aGamma)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aZ);
|
||||
NS_ENSURE_ARG_POINTER(aGamma);
|
||||
|
||||
*aZ = mZ;
|
||||
*aGamma = mGamma;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::GetAbsolute(PRBool *aAbsolute)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aAbsolute);
|
||||
|
||||
nsresult NS_NewDOMOrientationEvent(nsIDOMEvent** aInstancePtrResult,
|
||||
nsPresContext* aPresContext,
|
||||
nsEvent *aEvent)
|
||||
*aAbsolute = mAbsolute;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsDOMOrientationEvent::GetCompassCalibrated(PRBool *aCompassCalibrated)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aCompassCalibrated);
|
||||
|
||||
*aCompassCalibrated = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult NS_NewDOMDeviceOrientationEvent(nsIDOMEvent** aInstancePtrResult,
|
||||
nsPresContext* aPresContext,
|
||||
nsEvent *aEvent)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aInstancePtrResult);
|
||||
|
||||
|
@ -37,19 +37,20 @@
|
||||
#ifndef nsDOMOrientationEvent_h__
|
||||
#define nsDOMOrientationEvent_h__
|
||||
|
||||
#include "nsIDOMOrientationEvent.h"
|
||||
#include "nsIDOMDeviceOrientationEvent.h"
|
||||
#include "nsDOMEvent.h"
|
||||
|
||||
class nsDOMOrientationEvent : public nsDOMEvent,
|
||||
public nsIDOMOrientationEvent
|
||||
public nsIDOMDeviceOrientationEvent
|
||||
{
|
||||
public:
|
||||
|
||||
nsDOMOrientationEvent(nsPresContext* aPresContext, nsEvent* aEvent)
|
||||
: nsDOMEvent(aPresContext, aEvent),
|
||||
mX(0),
|
||||
mY(0),
|
||||
mZ(0) {}
|
||||
mAlpha(0),
|
||||
mBeta(0),
|
||||
mGamma(0),
|
||||
mAbsolute(PR_TRUE) {}
|
||||
|
||||
NS_DECL_ISUPPORTS_INHERITED
|
||||
|
||||
@ -57,10 +58,11 @@ public:
|
||||
NS_FORWARD_TO_NSDOMEVENT
|
||||
|
||||
// nsIDOMOrientationEvent Interface
|
||||
NS_DECL_NSIDOMORIENTATIONEVENT
|
||||
NS_DECL_NSIDOMDEVICEORIENTATIONEVENT
|
||||
|
||||
protected:
|
||||
double mX, mY, mZ;
|
||||
double mAlpha, mBeta, mGamma;
|
||||
PRBool mAbsolute;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -812,8 +812,8 @@ nsEventDispatcher::CreateEvent(nsPresContext* aPresContext,
|
||||
return NS_NewDOMTextEvent(aDOMEvent, aPresContext, nsnull);
|
||||
if (aEventType.LowerCaseEqualsLiteral("popupblockedevents"))
|
||||
return NS_NewDOMPopupBlockedEvent(aDOMEvent, aPresContext, nsnull);
|
||||
if (aEventType.LowerCaseEqualsLiteral("orientation"))
|
||||
return NS_NewDOMOrientationEvent(aDOMEvent, aPresContext, nsnull);
|
||||
if (aEventType.LowerCaseEqualsLiteral("deviceorientationevent"))
|
||||
return NS_NewDOMDeviceOrientationEvent(aDOMEvent, aPresContext, nsnull);
|
||||
if (aEventType.LowerCaseEqualsLiteral("uievent") ||
|
||||
aEventType.LowerCaseEqualsLiteral("uievents"))
|
||||
return NS_NewDOMUIEvent(aDOMEvent, aPresContext, nsnull);
|
||||
|
@ -503,7 +503,7 @@ nsEventListenerManager::AddEventListener(nsIDOMEventListener *aListener,
|
||||
kAllMutationBits :
|
||||
MutationBitForEventType(aType));
|
||||
}
|
||||
} else if (aTypeAtom == nsGkAtoms::onMozOrientation) {
|
||||
} else if (aTypeAtom == nsGkAtoms::ondeviceorientation) {
|
||||
nsPIDOMWindow* window = GetInnerWindowForTarget();
|
||||
if (window)
|
||||
window->SetHasOrientationEventListener();
|
||||
|
@ -101,6 +101,7 @@ _TEST_FILES = \
|
||||
test_bug624127.html \
|
||||
test_bug641477.html \
|
||||
test_bug648573.html \
|
||||
test_bug615597.html \
|
||||
$(NULL)
|
||||
|
||||
#bug 585630
|
||||
|
43
content/events/test/test_bug615597.html
Normal file
43
content/events/test/test_bug615597.html
Normal file
@ -0,0 +1,43 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<!--
|
||||
https://bugzilla.mozilla.org/show_bug.cgi?id=615597
|
||||
-->
|
||||
<head>
|
||||
<title>Test for Bug 615597</title>
|
||||
<script type="application/javascript" src="/MochiKit/packed.js"></script>
|
||||
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=615597">Mozilla Bug 615597</a>
|
||||
<p id="display"></p>
|
||||
<div id="content" style="display: none">
|
||||
|
||||
</div>
|
||||
<pre id="test">
|
||||
<script type="application/javascript">
|
||||
|
||||
/** Test for Bug 615597 **/
|
||||
|
||||
window.addEventListener("deviceorientation", function(event) {
|
||||
is(event.alpha, 1.5);
|
||||
is(event.beta, 2.25);
|
||||
is(event.gamma, 3.667);
|
||||
is(event.absolute, true);
|
||||
is(event.compassCalibrated, true);
|
||||
SimpleTest.finish();
|
||||
}, true);
|
||||
|
||||
var event = DeviceOrientationEvent;
|
||||
ok(!!event, "Should have seen DeviceOrientationEvent!");
|
||||
|
||||
event = document.createEvent("DeviceOrientationEvent");
|
||||
event.initDeviceOrientationEvent('deviceorientation', true, true, 1.5, 2.25, 3.667, true);
|
||||
window.dispatchEvent(event);
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
</script>
|
||||
</pre>
|
||||
</body>
|
||||
</html>
|
@ -338,7 +338,7 @@
|
||||
#include "nsIDOMCSSStyleRule.h"
|
||||
#include "nsIDOMCSSStyleSheet.h"
|
||||
#include "nsDOMCSSValueList.h"
|
||||
#include "nsIDOMOrientationEvent.h"
|
||||
#include "nsIDOMDeviceOrientationEvent.h"
|
||||
#include "nsIDOMRange.h"
|
||||
#include "nsIDOMNSRange.h"
|
||||
#include "nsIDOMRangeException.h"
|
||||
@ -748,7 +748,7 @@ static nsDOMClassInfoData sClassInfoData[] = {
|
||||
DOM_DEFAULT_SCRIPTABLE_FLAGS)
|
||||
NS_DEFINE_CLASSINFO_DATA(PopupBlockedEvent, nsDOMGenericSH,
|
||||
DOM_DEFAULT_SCRIPTABLE_FLAGS)
|
||||
NS_DEFINE_CLASSINFO_DATA(OrientationEvent, nsDOMGenericSH,
|
||||
NS_DEFINE_CLASSINFO_DATA(DeviceOrientationEvent, nsDOMGenericSH,
|
||||
DOM_DEFAULT_SCRIPTABLE_FLAGS)
|
||||
|
||||
// Misc HTML classes
|
||||
@ -2574,8 +2574,8 @@ nsDOMClassInfo::Init()
|
||||
DOM_CLASSINFO_EVENT_MAP_ENTRIES
|
||||
DOM_CLASSINFO_MAP_END
|
||||
|
||||
DOM_CLASSINFO_MAP_BEGIN(OrientationEvent, nsIDOMOrientationEvent)
|
||||
DOM_CLASSINFO_MAP_ENTRY(nsIDOMOrientationEvent)
|
||||
DOM_CLASSINFO_MAP_BEGIN(DeviceOrientationEvent, nsIDOMDeviceOrientationEvent)
|
||||
DOM_CLASSINFO_MAP_ENTRY(nsIDOMDeviceOrientationEvent)
|
||||
DOM_CLASSINFO_EVENT_MAP_ENTRIES
|
||||
DOM_CLASSINFO_MAP_END
|
||||
|
||||
|
@ -76,7 +76,7 @@ DOMCI_CLASS(MouseScrollEvent)
|
||||
DOMCI_CLASS(DragEvent)
|
||||
DOMCI_CLASS(KeyboardEvent)
|
||||
DOMCI_CLASS(PopupBlockedEvent)
|
||||
DOMCI_CLASS(OrientationEvent)
|
||||
DOMCI_CLASS(DeviceOrientationEvent)
|
||||
|
||||
// HTML classes
|
||||
DOMCI_CLASS(HTMLDocument)
|
||||
|
@ -82,7 +82,7 @@ XPIDLSRCS = \
|
||||
nsIDOMSimpleGestureEvent.idl \
|
||||
nsIDOMNSMouseEvent.idl \
|
||||
nsIDOMMozTouchEvent.idl \
|
||||
nsIDOMOrientationEvent.idl \
|
||||
nsIDOMDeviceOrientationEvent.idl \
|
||||
nsIDOMScrollAreaEvent.idl \
|
||||
nsIDOMTransitionEvent.idl \
|
||||
nsIDOMPopStateEvent.idl \
|
||||
|
76
dom/interfaces/events/nsIDOMDeviceOrientationEvent.idl
Normal file
76
dom/interfaces/events/nsIDOMDeviceOrientationEvent.idl
Normal file
@ -0,0 +1,76 @@
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation
|
||||
* Portions created by the Initial Developer are Copyright (C) 2011
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsIDOMEvent.idl"
|
||||
|
||||
[scriptable, uuid(daf2d570-0ecc-4aa0-aba4-26f60dfcba6a)]
|
||||
interface nsIDOMDeviceOrientationEvent : nsIDOMEvent
|
||||
{
|
||||
void initDeviceOrientationEvent(in DOMString eventTypeArg,
|
||||
in boolean canBubbleArg,
|
||||
in boolean cancelableArg,
|
||||
in double alpha,
|
||||
in double beta,
|
||||
in double gamma,
|
||||
in boolean absolute);
|
||||
|
||||
/*
|
||||
* W3C specification values: http://dev.w3.org/geo/api/spec-source-orientation.html
|
||||
*
|
||||
* Alpha, beta and gamma are Tait-Bryan angles (Z-X'-Y''), that is they correspond to
|
||||
* yaw, pitch and roll in flight coordinates. More specifically, assume the device
|
||||
* is resting face up on a flat surface (its screen is pointing straight up). Then
|
||||
* the X axis points to the right when looking at the screen, the Y axis points up
|
||||
* when looking at the screen, and the Z axis points toward you when looking at
|
||||
* the screen. Alpha/beta/gamma then define the device's orientation by applying
|
||||
* the following rotations to the device in order:
|
||||
*
|
||||
* 1) Rotate it by alpha degrees around its Z axis (like a compass, or like changing
|
||||
* the yaw of an aircraft, assuming the Y axis is the nose of the craft); alpha
|
||||
* is in [0,360).
|
||||
* 2) Rotate it by beta degrees around its X axis (tilting the top of the device
|
||||
* towards or away from you, like pitching an aircraft); beta is in [-180,180).
|
||||
* 3) Rotate it by gamma degrees around its Y axis (tilting it sideways, like
|
||||
* rolling an aircraft); gamma is in [-90,90).
|
||||
*/
|
||||
|
||||
readonly attribute double alpha;
|
||||
readonly attribute double beta;
|
||||
readonly attribute double gamma;
|
||||
readonly attribute boolean absolute;
|
||||
readonly attribute boolean compassCalibrated;
|
||||
};
|
||||
|
@ -1,59 +0,0 @@
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Doug Turner <dougt@dougt.org>
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsIDOMEvent.idl"
|
||||
|
||||
[scriptable, uuid(1618546a-c176-40a2-9086-2d973acceeb1)]
|
||||
interface nsIDOMOrientationEvent : nsIDOMEvent
|
||||
{
|
||||
void initOrientationEvent(in DOMString eventTypeArg,
|
||||
in boolean canBubbleArg,
|
||||
in boolean cancelableArg,
|
||||
in double x,
|
||||
in double y,
|
||||
in double z);
|
||||
/*
|
||||
* Axis values are between -1 to 1 where 0 is the balance point.
|
||||
*
|
||||
* For example, x = 0 would mean that the device, along the x axis, is parallel to the earth
|
||||
* x decreasing would mean that the device is undergoing a counterclock-wise rotation on the x axis
|
||||
* x increasing would mean that the device is undergoing a clock-wise rotation on the x axis.
|
||||
*/
|
||||
|
||||
readonly attribute double x;
|
||||
readonly attribute double y;
|
||||
readonly attribute double z;
|
||||
};
|
@ -1104,12 +1104,12 @@ ContentParent::RecvScriptError(const nsString& aMessage,
|
||||
NS_IMETHODIMP
|
||||
ContentParent::OnAccelerationChange(nsIAcceleration *aAcceleration)
|
||||
{
|
||||
double x, y, z;
|
||||
aAcceleration->GetX(&x);
|
||||
aAcceleration->GetY(&y);
|
||||
aAcceleration->GetZ(&z);
|
||||
double alpha, beta, gamma;
|
||||
aAcceleration->GetAlpha(&alpha);
|
||||
aAcceleration->GetBeta(&beta);
|
||||
aAcceleration->GetGamma(&gamma);
|
||||
|
||||
unused << SendAccelerationChanged(x, y, z);
|
||||
unused << SendAccelerationChanged(alpha, beta, gamma);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIPrivateDOMEvent.h"
|
||||
#include "nsIDOMDocumentEvent.h"
|
||||
#include "nsIDOMOrientationEvent.h"
|
||||
#include "nsIDOMDeviceOrientationEvent.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIPrefService.h"
|
||||
|
||||
@ -54,21 +54,21 @@ public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIACCELERATION
|
||||
|
||||
nsAcceleration(double x, double y, double z);
|
||||
nsAcceleration(double alpha, double beta, double gamma);
|
||||
|
||||
private:
|
||||
~nsAcceleration();
|
||||
|
||||
protected:
|
||||
/* additional members */
|
||||
double mX, mY, mZ;
|
||||
double mAlpha, mBeta, mGamma;
|
||||
};
|
||||
|
||||
/* Implementation file */
|
||||
NS_IMPL_ISUPPORTS1(nsAcceleration, nsIAcceleration)
|
||||
|
||||
nsAcceleration::nsAcceleration(double x, double y, double z)
|
||||
:mX(x), mY(y), mZ(z)
|
||||
nsAcceleration::nsAcceleration(double alpha, double beta, double gamma)
|
||||
:mAlpha(alpha), mBeta(beta), mGamma(gamma)
|
||||
{
|
||||
}
|
||||
|
||||
@ -76,33 +76,33 @@ nsAcceleration::~nsAcceleration()
|
||||
{
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsAcceleration::GetX(double *aX)
|
||||
NS_IMETHODIMP nsAcceleration::GetAlpha(double *aAlpha)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aX);
|
||||
*aX = mX;
|
||||
NS_ENSURE_ARG_POINTER(aAlpha);
|
||||
*aAlpha = mAlpha;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsAcceleration::GetY(double *aY)
|
||||
NS_IMETHODIMP nsAcceleration::GetBeta(double *aBeta)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aY);
|
||||
*aY = mY;
|
||||
NS_ENSURE_ARG_POINTER(aBeta);
|
||||
*aBeta = mBeta;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsAcceleration::GetZ(double *aZ)
|
||||
NS_IMETHODIMP nsAcceleration::GetGamma(double *aGamma)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aZ);
|
||||
*aZ = mZ;
|
||||
NS_ENSURE_ARG_POINTER(aGamma);
|
||||
*aGamma = mGamma;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS2(nsAccelerometer, nsIAccelerometer, nsIAccelerometerUpdate)
|
||||
|
||||
nsAccelerometer::nsAccelerometer()
|
||||
: mLastX(10), /* initialize to values that can't be possible */
|
||||
mLastY(10),
|
||||
mLastZ(10),
|
||||
: mLastAlpha(-200), /* initialize to values that can't be possible */
|
||||
mLastBeta(-200),
|
||||
mLastGamma(-200),
|
||||
mStarted(PR_FALSE),
|
||||
mNewListener(PR_FALSE),
|
||||
mUpdateInterval(50), /* default to 50 ms */
|
||||
@ -213,39 +213,41 @@ NS_IMETHODIMP nsAccelerometer::RemoveWindowListener(nsIDOMWindow *aWindow)
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsAccelerometer::AccelerationChanged(double x, double y, double z)
|
||||
nsAccelerometer::AccelerationChanged(double alpha, double beta, double gamma)
|
||||
{
|
||||
if (!mEnabled)
|
||||
return NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (x > 1)
|
||||
x = 1;
|
||||
if (y > 1)
|
||||
y = 1;
|
||||
if (z > 1)
|
||||
z = 1;
|
||||
if (x < -1)
|
||||
x = -1;
|
||||
if (y < -1)
|
||||
y = -1;
|
||||
if (z < -1)
|
||||
z = -1;
|
||||
if (alpha > 360)
|
||||
alpha = 360;
|
||||
if (alpha < 0)
|
||||
alpha = 0;
|
||||
|
||||
if (beta > 180)
|
||||
beta = 180;
|
||||
if (beta < -180)
|
||||
beta = -180;
|
||||
|
||||
if (gamma > 90)
|
||||
gamma = 90;
|
||||
if (gamma < -90)
|
||||
gamma = -90;
|
||||
|
||||
if (!mNewListener) {
|
||||
if (PR_ABS(mLastX - x) < .01 &&
|
||||
PR_ABS(mLastY - y) < .01 &&
|
||||
PR_ABS(mLastZ - z) < .01)
|
||||
if (PR_ABS(mLastAlpha - alpha) < 1 &&
|
||||
PR_ABS(mLastBeta - beta) < 1 &&
|
||||
PR_ABS(mLastGamma - gamma) < 1)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
mLastX = x;
|
||||
mLastY = y;
|
||||
mLastZ = z;
|
||||
mLastAlpha = alpha;
|
||||
mLastBeta = beta;
|
||||
mLastGamma = gamma;
|
||||
mNewListener = PR_FALSE;
|
||||
|
||||
for (PRUint32 i = mListeners.Count(); i > 0 ; ) {
|
||||
--i;
|
||||
nsRefPtr<nsIAcceleration> a = new nsAcceleration(x, y, z);
|
||||
nsRefPtr<nsIAcceleration> a = new nsAcceleration(alpha, beta, gamma);
|
||||
mListeners[i]->OnAccelerationChange(a);
|
||||
}
|
||||
|
||||
@ -261,23 +263,24 @@ nsAccelerometer::AccelerationChanged(double x, double y, double z)
|
||||
PRBool defaultActionEnabled = PR_TRUE;
|
||||
|
||||
if (docevent) {
|
||||
docevent->CreateEvent(NS_LITERAL_STRING("orientation"), getter_AddRefs(event));
|
||||
docevent->CreateEvent(NS_LITERAL_STRING("DeviceOrientationEvent"), getter_AddRefs(event));
|
||||
|
||||
nsCOMPtr<nsIDOMOrientationEvent> oe = do_QueryInterface(event);
|
||||
nsCOMPtr<nsIDOMDeviceOrientationEvent> oe = do_QueryInterface(event);
|
||||
|
||||
if (event) {
|
||||
oe->InitOrientationEvent(NS_LITERAL_STRING("MozOrientation"),
|
||||
PR_TRUE,
|
||||
PR_FALSE,
|
||||
x,
|
||||
y,
|
||||
z);
|
||||
oe->InitDeviceOrientationEvent(NS_LITERAL_STRING("deviceorientation"),
|
||||
PR_TRUE,
|
||||
PR_FALSE,
|
||||
alpha,
|
||||
beta,
|
||||
gamma,
|
||||
PR_TRUE);
|
||||
|
||||
nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
|
||||
if (privateEvent)
|
||||
privateEvent->SetTrusted(PR_TRUE);
|
||||
|
||||
nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(mWindowListeners[i]));
|
||||
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mWindowListeners[i]);
|
||||
target->DispatchEvent(event, &defaultActionEnabled);
|
||||
}
|
||||
}
|
||||
|
@ -61,9 +61,9 @@ public:
|
||||
|
||||
virtual ~nsAccelerometer();
|
||||
|
||||
double mLastX;
|
||||
double mLastY;
|
||||
double mLastZ;
|
||||
double mLastAlpha;
|
||||
double mLastBeta;
|
||||
double mLastGamma;
|
||||
|
||||
private:
|
||||
nsCOMArray<nsIAccelerationListener> mListeners;
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* -*- Mode: Java; tab-width: 20; indent-tabs-mode: nil; -*-
|
||||
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; -*-
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
@ -45,6 +45,7 @@ import android.graphics.*;
|
||||
import android.widget.*;
|
||||
import android.hardware.*;
|
||||
import android.location.*;
|
||||
import android.util.FloatMath;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
@ -95,7 +96,7 @@ public class GeckoEvent {
|
||||
public long mTime;
|
||||
public Point mP0, mP1;
|
||||
public Rect mRect;
|
||||
public float mX, mY, mZ;
|
||||
public double mAlpha, mBeta, mGamma;
|
||||
|
||||
public int mMetaState, mFlags;
|
||||
public int mKeyCode, mUnicodeChar;
|
||||
@ -140,9 +141,17 @@ public class GeckoEvent {
|
||||
|
||||
public GeckoEvent(SensorEvent s) {
|
||||
mType = SENSOR_EVENT;
|
||||
mX = s.values[0] / SensorManager.GRAVITY_EARTH;
|
||||
mY = s.values[1] / SensorManager.GRAVITY_EARTH;
|
||||
mZ = s.values[2] / SensorManager.GRAVITY_EARTH;
|
||||
// We interpret the accelerometer readings as the direction of gravity.
|
||||
// Note that we will be mistaken if there is any additional
|
||||
// acceleration on the device. We can detect that to some degree
|
||||
// by comparing the magnitude to SensorManager.GRAVITY_EARTH, but
|
||||
// don't have any easy way to use that information...
|
||||
float magnitude = FloatMath.sqrt(s.values[0] * s.values[0] +
|
||||
s.values[1] * s.values[1] +
|
||||
s.values[2] * s.values[2]);
|
||||
mAlpha = 0; // This should be null; we do not have enough info to calculate it
|
||||
mBeta = Math.toDegrees(Math.asin(s.values[1] / magnitude));
|
||||
mGamma = -Math.toDegrees(Math.asin(s.values[0] / magnitude));
|
||||
}
|
||||
|
||||
public GeckoEvent(Location l, Address a) {
|
||||
|
@ -46,9 +46,9 @@ jfieldID AndroidGeckoEvent::jTypeField = 0;
|
||||
jfieldID AndroidGeckoEvent::jTimeField = 0;
|
||||
jfieldID AndroidGeckoEvent::jP0Field = 0;
|
||||
jfieldID AndroidGeckoEvent::jP1Field = 0;
|
||||
jfieldID AndroidGeckoEvent::jXField = 0;
|
||||
jfieldID AndroidGeckoEvent::jYField = 0;
|
||||
jfieldID AndroidGeckoEvent::jZField = 0;
|
||||
jfieldID AndroidGeckoEvent::jAlphaField = 0;
|
||||
jfieldID AndroidGeckoEvent::jBetaField = 0;
|
||||
jfieldID AndroidGeckoEvent::jGammaField = 0;
|
||||
jfieldID AndroidGeckoEvent::jRectField = 0;
|
||||
jfieldID AndroidGeckoEvent::jNativeWindowField = 0;
|
||||
|
||||
@ -142,9 +142,9 @@ AndroidGeckoEvent::InitGeckoEventClass(JNIEnv *jEnv)
|
||||
jTimeField = getField("mTime", "J");
|
||||
jP0Field = getField("mP0", "Landroid/graphics/Point;");
|
||||
jP1Field = getField("mP1", "Landroid/graphics/Point;");
|
||||
jXField = getField("mX", "F");
|
||||
jYField = getField("mY", "F");
|
||||
jZField = getField("mZ", "F");
|
||||
jAlphaField = getField("mAlpha", "D");
|
||||
jBetaField = getField("mBeta", "D");
|
||||
jGammaField = getField("mGamma", "D");
|
||||
jRectField = getField("mRect", "Landroid/graphics/Rect;");
|
||||
|
||||
jCharactersField = getField("mCharacters", "Ljava/lang/String;");
|
||||
@ -399,9 +399,9 @@ AndroidGeckoEvent::Init(JNIEnv *jenv, jobject jobj)
|
||||
break;
|
||||
|
||||
case SENSOR_EVENT:
|
||||
mX = jenv->GetFloatField(jobj, jXField);
|
||||
mY = jenv->GetFloatField(jobj, jYField);
|
||||
mZ = jenv->GetFloatField(jobj, jZField);
|
||||
mAlpha = jenv->GetDoubleField(jobj, jAlphaField);
|
||||
mBeta = jenv->GetDoubleField(jobj, jBetaField);
|
||||
mGamma = jenv->GetDoubleField(jobj, jGammaField);
|
||||
break;
|
||||
|
||||
case LOCATION_EVENT: {
|
||||
|
@ -392,9 +392,9 @@ public:
|
||||
int64_t Time() { return mTime; }
|
||||
const nsIntPoint& P0() { return mP0; }
|
||||
const nsIntPoint& P1() { return mP1; }
|
||||
float X() { return mX; }
|
||||
float Y() { return mY; }
|
||||
float Z() { return mZ; }
|
||||
double Alpha() { return mAlpha; }
|
||||
double Beta() { return mBeta; }
|
||||
double Gamma() { return mGamma; }
|
||||
const nsIntRect& Rect() { return mRect; }
|
||||
nsAString& Characters() { return mCharacters; }
|
||||
int KeyCode() { return mKeyCode; }
|
||||
@ -422,7 +422,7 @@ protected:
|
||||
int mOffset, mCount;
|
||||
int mRangeType, mRangeStyles;
|
||||
int mRangeForeColor, mRangeBackColor;
|
||||
float mX, mY, mZ;
|
||||
double mAlpha, mBeta, mGamma;
|
||||
nsString mCharacters;
|
||||
nsRefPtr<nsGeoPosition> mGeoPosition;
|
||||
nsRefPtr<nsGeoPositionAddress> mGeoAddress;
|
||||
@ -438,9 +438,9 @@ protected:
|
||||
static jfieldID jTimeField;
|
||||
static jfieldID jP0Field;
|
||||
static jfieldID jP1Field;
|
||||
static jfieldID jXField;
|
||||
static jfieldID jYField;
|
||||
static jfieldID jZField;
|
||||
static jfieldID jAlphaField;
|
||||
static jfieldID jBetaField;
|
||||
static jfieldID jGammaField;
|
||||
static jfieldID jRectField;
|
||||
static jfieldID jNativeWindowField;
|
||||
|
||||
|
@ -315,7 +315,7 @@ nsAppShell::ProcessNextNativeEvent(PRBool mayWait)
|
||||
break;
|
||||
|
||||
case AndroidGeckoEvent::SENSOR_EVENT:
|
||||
gAccel->AccelerationChanged(-curEvent->X(), curEvent->Y(), curEvent->Z());
|
||||
gAccel->AccelerationChanged(-curEvent->Alpha(), curEvent->Beta(), curEvent->Gamma());
|
||||
break;
|
||||
|
||||
case AndroidGeckoEvent::LOCATION_EVENT: {
|
||||
|
@ -38,12 +38,12 @@
|
||||
|
||||
interface nsIDOMWindow;
|
||||
|
||||
[scriptable, uuid(1B406E32-CF42-471E-A470-6FD600BF4C7B)]
|
||||
[scriptable, uuid(0ec7ed95-dc9e-4d20-a5e2-8fc6a03bce67)]
|
||||
interface nsIAcceleration : nsISupports
|
||||
{
|
||||
readonly attribute double x;
|
||||
readonly attribute double y;
|
||||
readonly attribute double z;
|
||||
readonly attribute double alpha;
|
||||
readonly attribute double beta;
|
||||
readonly attribute double gamma;
|
||||
};
|
||||
|
||||
[scriptable, uuid(3386BED8-7393-4704-8FFC-1EB2C35432FF)]
|
||||
@ -70,5 +70,5 @@ interface nsIAccelerometer : nsISupports
|
||||
interface nsIAccelerometerUpdate : nsIAccelerometer
|
||||
{
|
||||
/* must be called on the main thread or else */
|
||||
void accelerationChanged(in double x, in double y, in double z);
|
||||
void accelerationChanged(in double alpha, in double beta, in double gamma);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user