diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /hal/windows | |
parent | Initial commit. (diff) | |
download | firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'hal/windows')
-rw-r--r-- | hal/windows/WindowsBattery.cpp | 115 | ||||
-rw-r--r-- | hal/windows/WindowsProcessPriority.cpp | 85 | ||||
-rw-r--r-- | hal/windows/WindowsScreenConfiguration.cpp | 103 | ||||
-rw-r--r-- | hal/windows/WindowsSensor.cpp | 170 |
4 files changed, 473 insertions, 0 deletions
diff --git a/hal/windows/WindowsBattery.cpp b/hal/windows/WindowsBattery.cpp new file mode 100644 index 0000000000..fba786c8a2 --- /dev/null +++ b/hal/windows/WindowsBattery.cpp @@ -0,0 +1,115 @@ +/* -*- 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 "mozilla/Preferences.h" +#include "mozilla/dom/battery/Constants.h" + +#include <windows.h> + +using namespace mozilla::dom::battery; + +namespace mozilla { +namespace hal_impl { + +static HPOWERNOTIFY sPowerHandle = nullptr; +static HPOWERNOTIFY sCapacityHandle = nullptr; +static HWND sHWnd = nullptr; + +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() { + // 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 = RegisterPowerSettingNotification( + sHWnd, &GUID_ACDC_POWER_SOURCE, DEVICE_NOTIFY_WINDOW_HANDLE); + sCapacityHandle = RegisterPowerSettingNotification( + sHWnd, &GUID_BATTERY_PERCENTAGE_REMAINING, DEVICE_NOTIFY_WINDOW_HANDLE); +} + +void DisableBatteryNotifications() { + if (sPowerHandle) { + UnregisterPowerSettingNotification(sPowerHandle); + sPowerHandle = nullptr; + } + + if (sCapacityHandle) { + UnregisterPowerSettingNotification(sCapacityHandle); + sCapacityHandle = nullptr; + } + + if (sHWnd) { + DestroyWindow(sHWnd); + sHWnd = 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; + } +} + +} // namespace hal_impl +} // namespace mozilla diff --git a/hal/windows/WindowsProcessPriority.cpp b/hal/windows/WindowsProcessPriority.cpp new file mode 100644 index 0000000000..3f2153f569 --- /dev/null +++ b/hal/windows/WindowsProcessPriority.cpp @@ -0,0 +1,85 @@ +/* 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/. */ + +// SetProcessInformation is only defined for Win8 and newer. +#if defined(_WIN32_WINNT) +# undef _WIN32_WINNT +# define _WIN32_WINNT _WIN32_WINNT_WIN8 +#endif // defined(_WIN32_WINNT) + +#include "Hal.h" +#include "HalLog.h" +#include "nsWindowsHelpers.h" // for nsAutoHandle and nsModuleHandle +#include "mozilla/StaticPrefs_dom.h" + +#include <windows.h> + +using namespace mozilla::hal; + +namespace mozilla { +namespace hal_impl { + +void SetProcessPriority(int aPid, ProcessPriority aPriority) { + HAL_LOG("WindowsProcessPriority - SetProcessPriority(%d, %s)\n", aPid, + ProcessPriorityToString(aPriority)); + + nsAutoHandle processHandle( + ::OpenProcess(PROCESS_SET_INFORMATION, FALSE, aPid)); + if (processHandle) { + DWORD priority = NORMAL_PRIORITY_CLASS; + if (aPriority == PROCESS_PRIORITY_BACKGROUND) { + priority = IDLE_PRIORITY_CLASS; + } else if (aPriority == PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE) { + priority = BELOW_NORMAL_PRIORITY_CLASS; + } + + if (::SetPriorityClass(processHandle, priority)) { + HAL_LOG("WindowsProcessPriority - priority set to %d for pid %d\n", + aPriority, aPid); + } + + // Set the process into or out of EcoQoS. + static bool alreadyInitialized = false; + static decltype(::SetProcessInformation)* setProcessInformation = nullptr; + if (!alreadyInitialized) { + if (aPriority == PROCESS_PRIORITY_PARENT_PROCESS || + !StaticPrefs::dom_ipc_processPriorityManager_backgroundUsesEcoQoS()) { + return; + } + + alreadyInitialized = true; + // SetProcessInformation only exists on Windows 8 and later. + nsModuleHandle module(LoadLibrary(L"Kernel32.dll")); + if (module) { + setProcessInformation = + (decltype(::SetProcessInformation)*)GetProcAddress( + module, "SetProcessInformation"); + } + } + if (!setProcessInformation) { + return; + } + + PROCESS_POWER_THROTTLING_STATE PowerThrottling; + RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling)); + PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION; + PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED; + PowerThrottling.StateMask = + (aPriority == PROCESS_PRIORITY_BACKGROUND) && + StaticPrefs:: + dom_ipc_processPriorityManager_backgroundUsesEcoQoS() + ? PROCESS_POWER_THROTTLING_EXECUTION_SPEED + : 0; + if (setProcessInformation(processHandle, ProcessPowerThrottling, + &PowerThrottling, sizeof(PowerThrottling))) { + HAL_LOG("SetProcessInformation(%d, %s)\n", aPid, + aPriority == PROCESS_PRIORITY_BACKGROUND ? "eco" : "normal"); + } else { + HAL_LOG("SetProcessInformation failed for %d\n", aPid); + } + } +} + +} // namespace hal_impl +} // namespace mozilla diff --git a/hal/windows/WindowsScreenConfiguration.cpp b/hal/windows/WindowsScreenConfiguration.cpp new file mode 100644 index 0000000000..c971054ff2 --- /dev/null +++ b/hal/windows/WindowsScreenConfiguration.cpp @@ -0,0 +1,103 @@ +/* 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/. */ + +#if defined(WINVER) +# undef WINVER +# define WINVER 0x0602 +#endif + +#include "Hal.h" +#include "mozilla/WindowsVersion.h" +#include "mozilla/widget/ScreenManager.h" +#include "nsIWindowsUIUtils.h" +#include "WinUtils.h" + +#include <windows.h> + +namespace mozilla { +namespace hal_impl { + +static decltype(SetDisplayAutoRotationPreferences)* + sSetDisplayAutoRotationPreferences = nullptr; + +RefPtr<GenericNonExclusivePromise> LockScreenOrientation( + const hal::ScreenOrientation& aOrientation) { + // SetDisplayAutoRotationPreferences requires Win8, tablet mode and device + // support. + if (!IsWin8OrLater()) { + return GenericNonExclusivePromise::CreateAndReject( + NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__); + } + AR_STATE state; + if (!widget::WinUtils::GetAutoRotationState(&state)) { + return GenericNonExclusivePromise::CreateAndReject( + NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__); + } + + if (state & (AR_DISABLED | AR_REMOTESESSION | AR_MULTIMON | AR_NOSENSOR | + AR_NOT_SUPPORTED | AR_LAPTOP | AR_DOCKED)) { + return GenericNonExclusivePromise::CreateAndReject( + NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__); + } + + if (!sSetDisplayAutoRotationPreferences) { + HMODULE user32dll = GetModuleHandleW(L"user32.dll"); + if (user32dll) { + sSetDisplayAutoRotationPreferences = + (decltype(SetDisplayAutoRotationPreferences)*)GetProcAddress( + user32dll, "SetDisplayAutoRotationPreferences"); + } + if (!sSetDisplayAutoRotationPreferences) { + return GenericNonExclusivePromise::CreateAndReject( + NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__); + } + } + + ORIENTATION_PREFERENCE orientation = ORIENTATION_PREFERENCE_NONE; + + if (aOrientation == hal::ScreenOrientation::Default) { + // Actually, current screen is single and tablet mode according to + // GetAutoRotationState. So get primary screen data for natural orientation. + RefPtr<widget::Screen> screen = + widget::ScreenManager::GetSingleton().GetPrimaryScreen(); + hal::ScreenOrientation defaultOrientation = + screen->GetDefaultOrientationType(); + if (defaultOrientation == hal::ScreenOrientation::LandscapePrimary) { + orientation = ORIENTATION_PREFERENCE_LANDSCAPE; + } else { + orientation = ORIENTATION_PREFERENCE_PORTRAIT; + } + } else { + if (aOrientation & hal::ScreenOrientation::LandscapePrimary) { + orientation |= ORIENTATION_PREFERENCE_LANDSCAPE; + } + if (aOrientation & hal::ScreenOrientation::LandscapeSecondary) { + orientation |= ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED; + } + if (aOrientation & hal::ScreenOrientation::PortraitPrimary) { + orientation |= ORIENTATION_PREFERENCE_PORTRAIT; + } + if (aOrientation & hal::ScreenOrientation::PortraitSecondary) { + orientation |= ORIENTATION_PREFERENCE_PORTRAIT_FLIPPED; + } + } + + if (!sSetDisplayAutoRotationPreferences(orientation)) { + return GenericNonExclusivePromise::CreateAndReject(NS_ERROR_DOM_ABORT_ERR, + __func__); + } + + return GenericNonExclusivePromise::CreateAndResolve(true, __func__); +} + +void UnlockScreenOrientation() { + if (!sSetDisplayAutoRotationPreferences) { + return; + } + // This does nothing if the device doesn't support orientation lock + sSetDisplayAutoRotationPreferences(ORIENTATION_PREFERENCE_NONE); +} + +} // namespace hal_impl +} // namespace mozilla diff --git a/hal/windows/WindowsSensor.cpp b/hal/windows/WindowsSensor.cpp new file mode 100644 index 0000000000..73bd8bc57c --- /dev/null +++ b/hal/windows/WindowsSensor.cpp @@ -0,0 +1,170 @@ +/* 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; + nsTArray<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::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; + } +} + +} // namespace hal_impl +} // namespace mozilla |