summaryrefslogtreecommitdiffstats
path: root/hal/windows
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /hal/windows
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'hal/windows')
-rw-r--r--hal/windows/WindowsBattery.cpp190
-rw-r--r--hal/windows/WindowsSensor.cpp184
2 files changed, 374 insertions, 0 deletions
diff --git a/hal/windows/WindowsBattery.cpp b/hal/windows/WindowsBattery.cpp
new file mode 100644
index 000000000..c1b9a31e6
--- /dev/null
+++ b/hal/windows/WindowsBattery.cpp
@@ -0,0 +1,190 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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 "HalImpl.h"
+#include "nsITimer.h"
+#include "mozilla/Preferences.h"
+#include "mozilla/dom/battery/Constants.h"
+#include "nsComponentManagerUtils.h"
+
+#include <windows.h>
+#include "mozilla/WindowsVersion.h"
+
+using namespace mozilla::dom::battery;
+
+namespace mozilla {
+namespace hal_impl {
+
+static nsCOMPtr<nsITimer> sUpdateTimer;
+
+/* Power Event API is Vista or later */
+static decltype(RegisterPowerSettingNotification)* sRegisterPowerSettingNotification = nullptr;
+static decltype(UnregisterPowerSettingNotification)* sUnregisterPowerSettingNotification = nullptr;
+static HPOWERNOTIFY sPowerHandle = nullptr;
+static HPOWERNOTIFY sCapacityHandle = nullptr;
+static HWND sHWnd = nullptr;
+
+static void
+UpdateHandler(nsITimer* aTimer, void* aClosure) {
+ NS_ASSERTION(!IsVistaOrLater(),
+ "We shouldn't call this function for Vista or later version!");
+
+ static hal::BatteryInformation sLastInfo;
+ hal::BatteryInformation currentInfo;
+
+ hal_impl::GetCurrentBatteryInformation(&currentInfo);
+ if (sLastInfo.level() != currentInfo.level() ||
+ sLastInfo.charging() != currentInfo.charging() ||
+ sLastInfo.remainingTime() != currentInfo.remainingTime()) {
+ hal::NotifyBatteryChange(currentInfo);
+ sLastInfo = currentInfo;
+ }
+}
+
+static
+LRESULT CALLBACK
+BatteryWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
+ if (msg != WM_POWERBROADCAST || wParam != PBT_POWERSETTINGCHANGE) {
+ return DefWindowProc(hwnd, msg, wParam, lParam);
+ }
+
+ hal::BatteryInformation currentInfo;
+
+ // Since we need update remainingTime, we cannot use LPARAM.
+ hal_impl::GetCurrentBatteryInformation(&currentInfo);
+
+ hal::NotifyBatteryChange(currentInfo);
+ return TRUE;
+}
+
+void
+EnableBatteryNotifications()
+{
+ if (IsVistaOrLater()) {
+ // RegisterPowerSettingNotification is from Vista or later.
+ // Use this API if available.
+ HMODULE hUser32 = GetModuleHandleW(L"USER32.DLL");
+ if (!sRegisterPowerSettingNotification)
+ sRegisterPowerSettingNotification = (decltype(RegisterPowerSettingNotification)*)
+ GetProcAddress(hUser32, "RegisterPowerSettingNotification");
+ if (!sUnregisterPowerSettingNotification)
+ sUnregisterPowerSettingNotification = (decltype(UnregisterPowerSettingNotification)*)
+ GetProcAddress(hUser32, "UnregisterPowerSettingNotification");
+
+ if (!sRegisterPowerSettingNotification ||
+ !sUnregisterPowerSettingNotification) {
+ NS_ASSERTION(false, "Canot find PowerSettingNotification functions.");
+ return;
+ }
+
+ // Create custom window to watch battery event
+ // If we can get Gecko's window handle, this is unnecessary.
+
+ if (sHWnd == nullptr) {
+ WNDCLASSW wc;
+ HMODULE hSelf = GetModuleHandle(nullptr);
+
+ if (!GetClassInfoW(hSelf, L"MozillaBatteryClass", &wc)) {
+ ZeroMemory(&wc, sizeof(WNDCLASSW));
+ wc.hInstance = hSelf;
+ wc.lpfnWndProc = BatteryWindowProc;
+ wc.lpszClassName = L"MozillaBatteryClass";
+ RegisterClassW(&wc);
+ }
+
+ sHWnd = CreateWindowW(L"MozillaBatteryClass", L"Battery Watcher",
+ 0, 0, 0, 0, 0,
+ nullptr, nullptr, hSelf, nullptr);
+ }
+
+ if (sHWnd == nullptr) {
+ return;
+ }
+
+ sPowerHandle =
+ sRegisterPowerSettingNotification(sHWnd,
+ &GUID_ACDC_POWER_SOURCE,
+ DEVICE_NOTIFY_WINDOW_HANDLE);
+ sCapacityHandle =
+ sRegisterPowerSettingNotification(sHWnd,
+ &GUID_BATTERY_PERCENTAGE_REMAINING,
+ DEVICE_NOTIFY_WINDOW_HANDLE);
+ } else
+ {
+ // for Windows XP. If we remove Windows XP support,
+ // we should remove timer-based power notification
+ sUpdateTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
+ if (sUpdateTimer) {
+ sUpdateTimer->InitWithFuncCallback(UpdateHandler,
+ nullptr,
+ Preferences::GetInt("dom.battery.timer",
+ 30000 /* 30s */),
+ nsITimer::TYPE_REPEATING_SLACK);
+ }
+ }
+}
+
+void
+DisableBatteryNotifications()
+{
+ if (IsVistaOrLater()) {
+ if (sPowerHandle) {
+ sUnregisterPowerSettingNotification(sPowerHandle);
+ sPowerHandle = nullptr;
+ }
+
+ if (sCapacityHandle) {
+ sUnregisterPowerSettingNotification(sCapacityHandle);
+ sCapacityHandle = nullptr;
+ }
+
+ if (sHWnd) {
+ DestroyWindow(sHWnd);
+ sHWnd = nullptr;
+ }
+ } else
+ {
+ if (sUpdateTimer) {
+ sUpdateTimer->Cancel();
+ sUpdateTimer = nullptr;
+ }
+ }
+}
+
+void
+GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo)
+{
+ SYSTEM_POWER_STATUS status;
+ if (!GetSystemPowerStatus(&status)) {
+ aBatteryInfo->level() = kDefaultLevel;
+ aBatteryInfo->charging() = kDefaultCharging;
+ aBatteryInfo->remainingTime() = kDefaultRemainingTime;
+ return;
+ }
+
+ aBatteryInfo->level() =
+ status.BatteryLifePercent == 255 ? kDefaultLevel
+ : ((double)status.BatteryLifePercent) / 100.0;
+ aBatteryInfo->charging() = (status.ACLineStatus != 0);
+ if (status.ACLineStatus != 0) {
+ if (aBatteryInfo->level() == 1.0) {
+ // GetSystemPowerStatus API may returns -1 for BatteryFullLifeTime.
+ // So, if battery is 100%, set kDefaultRemainingTime at force.
+ aBatteryInfo->remainingTime() = kDefaultRemainingTime;
+ } else {
+ aBatteryInfo->remainingTime() =
+ status.BatteryFullLifeTime == (DWORD)-1 ? kUnknownRemainingTime
+ : status.BatteryFullLifeTime;
+ }
+ } else {
+ aBatteryInfo->remainingTime() =
+ status.BatteryLifeTime == (DWORD)-1 ? kUnknownRemainingTime
+ : status.BatteryLifeTime;
+ }
+}
+
+} // hal_impl
+} // mozilla
diff --git a/hal/windows/WindowsSensor.cpp b/hal/windows/WindowsSensor.cpp
new file mode 100644
index 000000000..302ad7c48
--- /dev/null
+++ b/hal/windows/WindowsSensor.cpp
@@ -0,0 +1,184 @@
+/* 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 {
+
+static RefPtr<ISensor> sAccelerometer;
+
+class SensorEvent final : public ISensorEvents {
+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;
+ InfallibleTArray<float> values;
+
+ // 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::SENSOR_ACCURACY_UNKNOWN);
+ hal::NotifySensorChange(sdata);
+
+ return S_OK;
+ }
+
+private:
+ ULONG mCount;
+};
+
+void
+EnableSensorNotifications(SensorType aSensor)
+{
+ if (aSensor != SENSOR_ACCELERATION) {
+ return;
+ }
+
+ if (sAccelerometer) {
+ return;
+ }
+
+ RefPtr<ISensorManager> manager;
+ if (FAILED(CoCreateInstance(CLSID_SensorManager, nullptr,
+ CLSCTX_INPROC_SERVER,
+ IID_ISensorManager,
+ getter_AddRefs(manager)))) {
+ return;
+ }
+
+ // accelerometer event
+
+ RefPtr<ISensorCollection> collection;
+ if (FAILED(manager->GetSensorsByType(SENSOR_TYPE_ACCELEROMETER_3D,
+ getter_AddRefs(collection)))) {
+ return;
+ }
+
+ ULONG count = 0;
+ collection->GetCount(&count);
+ if (!count) {
+ return;
+ }
+
+ RefPtr<ISensor> sensor;
+ collection->GetAt(0, getter_AddRefs(sensor));
+ if (!sensor) {
+ return;
+ }
+
+ // Set report interval to 100ms if possible.
+ // Default value depends on drivers.
+ RefPtr<IPortableDeviceValues> values;
+ if (SUCCEEDED(CoCreateInstance(CLSID_PortableDeviceValues, nullptr,
+ CLSCTX_INPROC_SERVER,
+ IID_IPortableDeviceValues,
+ getter_AddRefs(values)))) {
+ if (SUCCEEDED(values->SetUnsignedIntegerValue(
+ SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL,
+ DEFAULT_SENSOR_POLL))) {
+ RefPtr<IPortableDeviceValues> returns;
+ sensor->SetProperties(values, getter_AddRefs(returns));
+ }
+ }
+
+ RefPtr<SensorEvent> event = new SensorEvent();
+ RefPtr<ISensorEvents> sensorEvents;
+ 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) {
+ sAccelerometer->SetEventSink(nullptr);
+ sAccelerometer = nullptr;
+ }
+}
+
+} // hal_impl
+} // mozilla