2011-12-05 14:34:27 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +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/. */
|
2011-12-05 14:34:27 +04:00
|
|
|
|
|
|
|
#include "Hal.h"
|
2011-12-18 01:04:51 +04:00
|
|
|
#include "HalImpl.h"
|
2011-12-05 14:34:27 +04:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/dom/battery/Constants.h"
|
2013-09-11 00:56:05 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2011-12-05 14:34:27 +04:00
|
|
|
|
|
|
|
#include <windows.h>
|
2013-10-18 22:14:31 +04:00
|
|
|
#include "mozilla/WindowsVersion.h"
|
2011-12-05 14:34:27 +04:00
|
|
|
|
|
|
|
using namespace mozilla::dom::battery;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace hal_impl {
|
|
|
|
|
|
|
|
static nsCOMPtr<nsITimer> sUpdateTimer;
|
|
|
|
|
|
|
|
/* Power Event API is Vista or later */
|
2014-02-18 02:32:52 +04:00
|
|
|
static decltype(RegisterPowerSettingNotification)* sRegisterPowerSettingNotification = nullptr;
|
|
|
|
static decltype(UnregisterPowerSettingNotification)* sUnregisterPowerSettingNotification = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
static HPOWERNOTIFY sPowerHandle = nullptr;
|
|
|
|
static HPOWERNOTIFY sCapacityHandle = nullptr;
|
|
|
|
static HWND sHWnd = nullptr;
|
2011-12-05 14:34:27 +04:00
|
|
|
|
|
|
|
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(¤tInfo);
|
|
|
|
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(¤tInfo);
|
|
|
|
|
|
|
|
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)
|
2014-02-18 02:32:52 +04:00
|
|
|
sRegisterPowerSettingNotification = (decltype(RegisterPowerSettingNotification)*)
|
2011-12-05 14:34:27 +04:00
|
|
|
GetProcAddress(hUser32, "RegisterPowerSettingNotification");
|
|
|
|
if (!sUnregisterPowerSettingNotification)
|
2014-02-18 02:32:52 +04:00
|
|
|
sUnregisterPowerSettingNotification = (decltype(UnregisterPowerSettingNotification)*)
|
2011-12-05 14:34:27 +04:00
|
|
|
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.
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (sHWnd == nullptr) {
|
2011-12-05 14:34:27 +04:00
|
|
|
WNDCLASSW wc;
|
2012-07-30 18:20:58 +04:00
|
|
|
HMODULE hSelf = GetModuleHandle(nullptr);
|
2011-12-05 14:34:27 +04:00
|
|
|
|
|
|
|
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,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, nullptr, hSelf, nullptr);
|
2011-12-05 14:34:27 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (sHWnd == nullptr) {
|
2011-12-05 14:34:27 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sPowerHandle =
|
|
|
|
sRegisterPowerSettingNotification(sHWnd,
|
|
|
|
&GUID_ACDC_POWER_SOURCE,
|
|
|
|
DEVICE_NOTIFY_WINDOW_HANDLE);
|
|
|
|
sCapacityHandle =
|
|
|
|
sRegisterPowerSettingNotification(sHWnd,
|
|
|
|
&GUID_BATTERY_PERCENTAGE_REMAINING,
|
|
|
|
DEVICE_NOTIFY_WINDOW_HANDLE);
|
|
|
|
} else
|
|
|
|
{
|
2012-02-23 18:53:55 +04:00
|
|
|
// for Windows XP. If we remove Windows XP support,
|
2011-12-05 14:34:27 +04:00
|
|
|
// we should remove timer-based power notification
|
|
|
|
sUpdateTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
if (sUpdateTimer) {
|
|
|
|
sUpdateTimer->InitWithFuncCallback(UpdateHandler,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2011-12-05 14:34:27 +04:00
|
|
|
Preferences::GetInt("dom.battery.timer",
|
|
|
|
30000 /* 30s */),
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableBatteryNotifications()
|
|
|
|
{
|
|
|
|
if (IsVistaOrLater()) {
|
|
|
|
if (sPowerHandle) {
|
|
|
|
sUnregisterPowerSettingNotification(sPowerHandle);
|
2012-07-30 18:20:58 +04:00
|
|
|
sPowerHandle = nullptr;
|
2011-12-05 14:34:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sCapacityHandle) {
|
|
|
|
sUnregisterPowerSettingNotification(sCapacityHandle);
|
2012-07-30 18:20:58 +04:00
|
|
|
sCapacityHandle = nullptr;
|
2011-12-05 14:34:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sHWnd) {
|
|
|
|
DestroyWindow(sHWnd);
|
2012-07-30 18:20:58 +04:00
|
|
|
sHWnd = nullptr;
|
2011-12-05 14:34:27 +04:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (sUpdateTimer) {
|
|
|
|
sUpdateTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
sUpdateTimer = nullptr;
|
2011-12-05 14:34:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2011-12-19 07:23:13 +04:00
|
|
|
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;
|
|
|
|
}
|
2011-12-05 14:34:27 +04:00
|
|
|
} else {
|
|
|
|
aBatteryInfo->remainingTime() =
|
|
|
|
status.BatteryLifeTime == (DWORD)-1 ? kUnknownRemainingTime
|
|
|
|
: status.BatteryLifeTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // hal_impl
|
|
|
|
} // mozilla
|