hal/cocoa/CocoaSensor.mm
author Kartikaya Gupta <kgupta@mozilla.com>
Fri, 13 Apr 2012 10:29:14 -0400
changeset 91612 84351dbcd62d12769fb601e11cc098849faee073
parent 90025 41f85ded0197c8cf5af119fe31edc7dbec336cb4
child 100844 b5c4b792f3f2a047e3517472d72842a76afb77cd
permissions -rw-r--r--
Bug 742115 - Make the java panning constants preffable; original patch stolen from :joe. r=Cwiiis

/* 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/. */

#include "Hal.h"
#include "nsITimer.h"

#include "smslib.h"
#define MEAN_GRAVITY 9.80665
#define DEFAULT_SENSOR_POLL 100

using namespace mozilla::hal;

namespace mozilla {
namespace hal_impl {

static nsITimer* sUpdateTimer = nsnull;

void UpdateHandler(nsITimer *aTimer, void *aClosure)
{
  sms_acceleration accel;
  smsGetData(&accel);

  InfallibleTArray<float> values;
  values.AppendElement(accel.x * MEAN_GRAVITY);
  values.AppendElement(accel.y * MEAN_GRAVITY);
  values.AppendElement(accel.z * MEAN_GRAVITY);
  hal::SensorData sdata(hal::SENSOR_ACCELERATION,
			PR_Now(),
			values,
			hal::SENSOR_ACCURACY_UNKNOWN);
  hal::NotifySensorChange(sdata);
}

void
EnableSensorNotifications(SensorType aSensor)
{
  if (aSensor != SENSOR_ACCELERATION)
    return;

  if (sUpdateTimer)
    return;

  smsStartup(nil, nil);
  smsLoadCalibration();

  CallCreateInstance("@mozilla.org/timer;1", &sUpdateTimer);
  if (sUpdateTimer)
    sUpdateTimer->InitWithFuncCallback(UpdateHandler,
                                       NULL,
                                       DEFAULT_SENSOR_POLL,
                                       nsITimer::TYPE_REPEATING_SLACK);
}

void
DisableSensorNotifications(SensorType aSensor)
{
  if (aSensor != SENSOR_ACCELERATION)
    return;

  if (sUpdateTimer) {
    sUpdateTimer->Cancel();
    NS_RELEASE(sUpdateTimer);
  }
  smsShutdown();
}

} // hal_impl
} // mozilla