2009-08-21 07:42:37 -07:00
|
|
|
/* ***** 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 "nsAccelerometer.h"
|
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMEventTarget.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIPrivateDOMEvent.h"
|
2011-04-29 16:49:20 -07:00
|
|
|
#include "nsIDOMDeviceOrientationEvent.h"
|
2009-08-26 16:57:08 -07:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIPrefService.h"
|
2009-08-21 07:42:37 -07:00
|
|
|
|
|
|
|
class nsAcceleration : public nsIAcceleration
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIACCELERATION
|
|
|
|
|
2011-04-29 16:49:20 -07:00
|
|
|
nsAcceleration(double alpha, double beta, double gamma);
|
2009-08-21 07:42:37 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
~nsAcceleration();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/* additional members */
|
2011-04-29 16:49:20 -07:00
|
|
|
double mAlpha, mBeta, mGamma;
|
2009-08-21 07:42:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Implementation file */
|
|
|
|
NS_IMPL_ISUPPORTS1(nsAcceleration, nsIAcceleration)
|
|
|
|
|
2011-04-29 16:49:20 -07:00
|
|
|
nsAcceleration::nsAcceleration(double alpha, double beta, double gamma)
|
|
|
|
:mAlpha(alpha), mBeta(beta), mGamma(gamma)
|
2009-08-21 07:42:37 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAcceleration::~nsAcceleration()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-04-29 16:49:20 -07:00
|
|
|
NS_IMETHODIMP nsAcceleration::GetAlpha(double *aAlpha)
|
2009-08-21 07:42:37 -07:00
|
|
|
{
|
2011-04-29 16:49:20 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aAlpha);
|
|
|
|
*aAlpha = mAlpha;
|
2009-08-21 07:42:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-04-29 16:49:20 -07:00
|
|
|
NS_IMETHODIMP nsAcceleration::GetBeta(double *aBeta)
|
2009-08-21 07:42:37 -07:00
|
|
|
{
|
2011-04-29 16:49:20 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aBeta);
|
|
|
|
*aBeta = mBeta;
|
2009-08-21 07:42:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-04-29 16:49:20 -07:00
|
|
|
NS_IMETHODIMP nsAcceleration::GetGamma(double *aGamma)
|
2009-08-21 07:42:37 -07:00
|
|
|
{
|
2011-04-29 16:49:20 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aGamma);
|
|
|
|
*aGamma = mGamma;
|
2009-08-21 07:42:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-05 10:43:13 -07:00
|
|
|
NS_IMPL_ISUPPORTS2(nsAccelerometer, nsIAccelerometer, nsIAccelerometerUpdate)
|
2009-08-21 07:42:37 -07:00
|
|
|
|
|
|
|
nsAccelerometer::nsAccelerometer()
|
2011-04-29 16:49:20 -07:00
|
|
|
: mLastAlpha(-200), /* initialize to values that can't be possible */
|
|
|
|
mLastBeta(-200),
|
|
|
|
mLastGamma(-200),
|
2009-08-26 16:57:08 -07:00
|
|
|
mStarted(PR_FALSE),
|
|
|
|
mNewListener(PR_FALSE),
|
2010-04-19 12:00:46 -07:00
|
|
|
mUpdateInterval(50), /* default to 50 ms */
|
|
|
|
mEnabled(PR_TRUE)
|
2009-08-21 07:42:37 -07:00
|
|
|
{
|
2009-08-26 16:57:08 -07:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefSrv = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (prefSrv) {
|
2010-03-08 07:33:24 -08:00
|
|
|
PRInt32 value;
|
2009-08-26 16:57:08 -07:00
|
|
|
nsresult rv = prefSrv->GetIntPref("accelerometer.update.interval", &value);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mUpdateInterval = value;
|
2010-03-08 07:33:24 -08:00
|
|
|
|
|
|
|
PRBool bvalue;
|
|
|
|
rv = prefSrv->GetBoolPref("accelerometer.enabled", &bvalue);
|
|
|
|
if (NS_SUCCEEDED(rv) && bvalue == PR_FALSE)
|
|
|
|
mEnabled = PR_FALSE;
|
2009-08-26 16:57:08 -07:00
|
|
|
}
|
2009-08-21 07:42:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAccelerometer::~nsAccelerometer()
|
|
|
|
{
|
2009-08-26 16:57:08 -07:00
|
|
|
if (mTimeoutTimer)
|
|
|
|
mTimeoutTimer->Cancel();
|
2009-08-21 07:42:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-26 16:57:08 -07:00
|
|
|
nsAccelerometer::StartDisconnectTimer()
|
2009-08-21 07:42:37 -07:00
|
|
|
{
|
2009-08-26 16:57:08 -07:00
|
|
|
if (mTimeoutTimer)
|
|
|
|
mTimeoutTimer->Cancel();
|
|
|
|
|
|
|
|
mTimeoutTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
if (mTimeoutTimer)
|
|
|
|
mTimeoutTimer->InitWithFuncCallback(TimeoutHandler,
|
|
|
|
this,
|
|
|
|
2000,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2009-08-21 07:42:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsAccelerometer::TimeoutHandler(nsITimer *aTimer, void *aClosure)
|
|
|
|
{
|
|
|
|
// the reason that we use self, instead of just using nsITimerCallback or nsIObserver
|
|
|
|
// is so that subclasses are free to use timers without worry about the base classes's
|
|
|
|
// usage.
|
|
|
|
nsAccelerometer *self = reinterpret_cast<nsAccelerometer *>(aClosure);
|
|
|
|
if (!self) {
|
|
|
|
NS_ERROR("no self");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// what about listeners that don't clean up properly? they will leak
|
|
|
|
if (self->mListeners.Count() == 0 && self->mWindowListeners.Count() == 0) {
|
|
|
|
self->Shutdown();
|
|
|
|
self->mStarted = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsAccelerometer::AddListener(nsIAccelerationListener *aListener)
|
|
|
|
{
|
2011-01-03 17:52:00 -08:00
|
|
|
if (mListeners.IndexOf(aListener) >= 0)
|
|
|
|
return NS_OK; // already exists
|
|
|
|
|
2009-08-21 07:42:37 -07:00
|
|
|
if (mStarted == PR_FALSE) {
|
|
|
|
mStarted = PR_TRUE;
|
2009-08-26 16:57:08 -07:00
|
|
|
mNewListener = PR_TRUE;
|
|
|
|
Startup();
|
2009-08-21 07:42:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.AppendObject(aListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsAccelerometer::RemoveListener(nsIAccelerationListener *aListener)
|
|
|
|
{
|
2011-01-03 17:52:00 -08:00
|
|
|
if (mListeners.IndexOf(aListener) < 0)
|
|
|
|
return NS_OK; // doesn't exist
|
|
|
|
|
2009-08-21 07:42:37 -07:00
|
|
|
mListeners.RemoveObject(aListener);
|
2009-08-26 16:57:08 -07:00
|
|
|
StartDisconnectTimer();
|
2009-08-21 07:42:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsAccelerometer::AddWindowListener(nsIDOMWindow *aWindow)
|
|
|
|
{
|
2011-01-03 17:52:00 -08:00
|
|
|
if (mWindowListeners.IndexOf(aWindow) >= 0)
|
|
|
|
return NS_OK; // already exists
|
|
|
|
|
2009-08-21 07:42:37 -07:00
|
|
|
if (mStarted == PR_FALSE) {
|
2009-08-26 16:57:08 -07:00
|
|
|
mStarted = PR_TRUE;
|
|
|
|
mNewListener = PR_TRUE;
|
|
|
|
Startup();
|
2009-08-21 07:42:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mWindowListeners.AppendObject(aWindow);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsAccelerometer::RemoveWindowListener(nsIDOMWindow *aWindow)
|
|
|
|
{
|
2011-01-03 17:52:00 -08:00
|
|
|
if (mWindowListeners.IndexOf(aWindow) < 0)
|
|
|
|
return NS_OK; // doesn't exist
|
|
|
|
|
2009-08-21 07:42:37 -07:00
|
|
|
mWindowListeners.RemoveObject(aWindow);
|
2009-08-26 16:57:08 -07:00
|
|
|
StartDisconnectTimer();
|
|
|
|
|
2009-08-21 07:42:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-05 10:43:13 -07:00
|
|
|
NS_IMETHODIMP
|
2011-04-29 16:49:20 -07:00
|
|
|
nsAccelerometer::AccelerationChanged(double alpha, double beta, double gamma)
|
2009-08-21 07:42:37 -07:00
|
|
|
{
|
2010-03-08 07:33:24 -08:00
|
|
|
if (!mEnabled)
|
2010-11-05 10:43:13 -07:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2010-03-08 07:33:24 -08:00
|
|
|
|
2011-04-29 16:49:20 -07:00
|
|
|
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;
|
2009-08-26 16:57:08 -07:00
|
|
|
|
|
|
|
if (!mNewListener) {
|
2011-04-29 16:49:20 -07:00
|
|
|
if (PR_ABS(mLastAlpha - alpha) < 1 &&
|
|
|
|
PR_ABS(mLastBeta - beta) < 1 &&
|
|
|
|
PR_ABS(mLastGamma - gamma) < 1)
|
2010-11-05 10:43:13 -07:00
|
|
|
return NS_OK;
|
2009-08-26 16:57:08 -07:00
|
|
|
}
|
|
|
|
|
2011-04-29 16:49:20 -07:00
|
|
|
mLastAlpha = alpha;
|
|
|
|
mLastBeta = beta;
|
|
|
|
mLastGamma = gamma;
|
2009-08-26 16:57:08 -07:00
|
|
|
mNewListener = PR_FALSE;
|
|
|
|
|
2009-08-21 07:42:37 -07:00
|
|
|
for (PRUint32 i = mListeners.Count(); i > 0 ; ) {
|
|
|
|
--i;
|
2011-04-29 16:49:20 -07:00
|
|
|
nsRefPtr<nsIAcceleration> a = new nsAcceleration(alpha, beta, gamma);
|
2009-08-21 07:42:37 -07:00
|
|
|
mListeners[i]->OnAccelerationChange(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (PRUint32 i = mWindowListeners.Count(); i > 0 ; ) {
|
|
|
|
--i;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
|
|
|
mWindowListeners[i]->GetDocument(getter_AddRefs(domdoc));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
|
|
|
|
PRBool defaultActionEnabled = PR_TRUE;
|
|
|
|
|
2011-05-23 09:46:36 -07:00
|
|
|
if (domdoc) {
|
|
|
|
domdoc->CreateEvent(NS_LITERAL_STRING("DeviceOrientationEvent"), getter_AddRefs(event));
|
2009-08-21 07:42:37 -07:00
|
|
|
|
2011-04-29 16:49:20 -07:00
|
|
|
nsCOMPtr<nsIDOMDeviceOrientationEvent> oe = do_QueryInterface(event);
|
2009-08-21 07:42:37 -07:00
|
|
|
|
|
|
|
if (event) {
|
2011-04-29 16:49:20 -07:00
|
|
|
oe->InitDeviceOrientationEvent(NS_LITERAL_STRING("deviceorientation"),
|
|
|
|
PR_TRUE,
|
|
|
|
PR_FALSE,
|
|
|
|
alpha,
|
|
|
|
beta,
|
|
|
|
gamma,
|
|
|
|
PR_TRUE);
|
2009-08-21 07:42:37 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
|
|
|
|
if (privateEvent)
|
|
|
|
privateEvent->SetTrusted(PR_TRUE);
|
|
|
|
|
2011-04-29 16:49:20 -07:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mWindowListeners[i]);
|
2009-08-21 07:42:37 -07:00
|
|
|
target->DispatchEvent(event, &defaultActionEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-05 10:43:13 -07:00
|
|
|
return NS_OK;
|
2009-08-21 07:42:37 -07:00
|
|
|
}
|