2012-05-01 07:04:30 +04: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/. */
|
|
|
|
|
|
|
|
#include "Hal.h"
|
|
|
|
|
|
|
|
#include <sensorsapi.h>
|
|
|
|
#include <sensors.h>
|
|
|
|
#include <portabledevicetypes.h>
|
|
|
|
|
|
|
|
#define MEAN_GRAVITY 9.80665
|
|
|
|
#define DEFAULT_SENSOR_POLL 100
|
|
|
|
|
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace hal_impl {
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
static RefPtr<ISensor> sAccelerometer;
|
2012-05-01 07:04:30 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class SensorEvent final : public ISensorEvents {
|
2012-05-01 07:04:30 +04:00
|
|
|
public:
|
|
|
|
SensorEvent() : mCount(0) {}
|
|
|
|
|
|
|
|
// IUnknown interface
|
|
|
|
|
|
|
|
STDMETHODIMP_(ULONG) AddRef() { return InterlockedIncrement(&mCount); }
|
|
|
|
|
|
|
|
STDMETHODIMP_(ULONG) Release() {
|
|
|
|
ULONG count = InterlockedDecrement(&mCount);
|
|
|
|
if (!count) {
|
|
|
|
delete this;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
STDMETHODIMP QueryInterface(REFIID iid, void** ppv) {
|
|
|
|
if (iid == IID_IUnknown) {
|
|
|
|
*ppv = static_cast<IUnknown*>(this);
|
|
|
|
} else if (iid == IID_ISensorEvents) {
|
|
|
|
*ppv = static_cast<ISensorEvents*>(this);
|
|
|
|
} else {
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
AddRef();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ISensorEvents interface
|
|
|
|
|
|
|
|
STDMETHODIMP OnEvent(ISensor* aSensor, REFGUID aId,
|
|
|
|
IPortableDeviceValues* aData) {
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
STDMETHODIMP OnLeave(REFSENSOR_ID aId) { return S_OK; }
|
|
|
|
|
|
|
|
STDMETHODIMP OnStateChanged(ISensor* aSensor, SensorState state) {
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
STDMETHODIMP OnDataUpdated(ISensor* aSensor, ISensorDataReport* aReport) {
|
|
|
|
PROPVARIANT v;
|
|
|
|
HRESULT hr;
|
2019-07-10 06:30:52 +03:00
|
|
|
nsTArray<float> values;
|
2012-05-01 07:04:30 +04:00
|
|
|
|
|
|
|
// X-axis acceleration in g's
|
|
|
|
hr = aReport->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_X_G, &v);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
values.AppendElement(float(-v.dblVal * MEAN_GRAVITY));
|
|
|
|
|
|
|
|
// Y-axis acceleration in g's
|
|
|
|
hr = aReport->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Y_G, &v);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
values.AppendElement(float(-v.dblVal * MEAN_GRAVITY));
|
|
|
|
|
|
|
|
// Z-axis acceleration in g's
|
|
|
|
hr = aReport->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Z_G, &v);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
values.AppendElement(float(-v.dblVal * MEAN_GRAVITY));
|
|
|
|
|
|
|
|
hal::SensorData sdata(hal::SENSOR_ACCELERATION, PR_Now(), values);
|
|
|
|
hal::NotifySensorChange(sdata);
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ULONG mCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
void EnableSensorNotifications(SensorType aSensor) {
|
|
|
|
if (aSensor != SENSOR_ACCELERATION) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sAccelerometer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ISensorManager> manager;
|
2013-08-23 23:51:15 +04:00
|
|
|
if (FAILED(CoCreateInstance(CLSID_SensorManager, nullptr,
|
|
|
|
CLSCTX_INPROC_SERVER, IID_ISensorManager,
|
|
|
|
getter_AddRefs(manager)))) {
|
2012-05-01 07:04:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// accelerometer event
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ISensorCollection> collection;
|
2012-05-01 07:04:30 +04:00
|
|
|
if (FAILED(manager->GetSensorsByType(SENSOR_TYPE_ACCELEROMETER_3D,
|
|
|
|
getter_AddRefs(collection)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ULONG count = 0;
|
|
|
|
collection->GetCount(&count);
|
|
|
|
if (!count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ISensor> sensor;
|
2012-05-01 07:04:30 +04:00
|
|
|
collection->GetAt(0, getter_AddRefs(sensor));
|
|
|
|
if (!sensor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set report interval to 100ms if possible.
|
|
|
|
// Default value depends on drivers.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IPortableDeviceValues> values;
|
2013-08-23 23:51:15 +04:00
|
|
|
if (SUCCEEDED(CoCreateInstance(
|
|
|
|
CLSID_PortableDeviceValues, nullptr, CLSCTX_INPROC_SERVER,
|
2012-05-01 07:04:30 +04:00
|
|
|
IID_IPortableDeviceValues, getter_AddRefs(values)))) {
|
|
|
|
if (SUCCEEDED(values->SetUnsignedIntegerValue(
|
|
|
|
SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, DEFAULT_SENSOR_POLL))) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IPortableDeviceValues> returns;
|
2012-05-01 07:04:30 +04:00
|
|
|
sensor->SetProperties(values, getter_AddRefs(returns));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SensorEvent> event = new SensorEvent();
|
|
|
|
RefPtr<ISensorEvents> sensorEvents;
|
2012-05-01 07:04:30 +04:00
|
|
|
if (FAILED(event->QueryInterface(IID_ISensorEvents,
|
|
|
|
getter_AddRefs(sensorEvents)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(sensor->SetEventSink(sensorEvents))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sAccelerometer = sensor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisableSensorNotifications(SensorType aSensor) {
|
|
|
|
if (aSensor == SENSOR_ACCELERATION && sAccelerometer) {
|
2013-08-23 23:51:15 +04:00
|
|
|
sAccelerometer->SetEventSink(nullptr);
|
2012-07-30 18:20:58 +04:00
|
|
|
sAccelerometer = nullptr;
|
2012-05-01 07:04:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace hal_impl
|
|
|
|
} // namespace mozilla
|