mirror of
https://github.com/reactos/reactos.git
synced 2024-08-21 10:53:15 +00:00
[NDK]: Add missing native power functions.
[KERNEL32]: Cleanup the power APIs. Fix calculation of battery percentage in GetSystemPowerStatus. svn path=/trunk/; revision=52841
This commit is contained in:
parent
3989ede5bb
commit
078cdde168
|
@ -15,59 +15,6 @@
|
||||||
#define NDEBUG
|
#define NDEBUG
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
|
||||||
|
|
||||||
NTSYSAPI
|
|
||||||
NTSTATUS
|
|
||||||
NTAPI
|
|
||||||
NtGetDevicePowerState(
|
|
||||||
IN HANDLE Device,
|
|
||||||
IN PDEVICE_POWER_STATE PowerState
|
|
||||||
);
|
|
||||||
|
|
||||||
NTSYSAPI
|
|
||||||
NTSTATUS
|
|
||||||
NTAPI
|
|
||||||
NtRequestWakeupLatency(
|
|
||||||
IN LATENCY_TIME latency
|
|
||||||
);
|
|
||||||
|
|
||||||
NTSYSAPI
|
|
||||||
BOOLEAN
|
|
||||||
NTAPI
|
|
||||||
NtIsSystemResumeAutomatic(VOID);
|
|
||||||
|
|
||||||
NTSYSAPI
|
|
||||||
NTSTATUS
|
|
||||||
NTAPI
|
|
||||||
NtSetThreadExecutionState(
|
|
||||||
IN EXECUTION_STATE esFlags,
|
|
||||||
OUT EXECUTION_STATE *PreviousFlags
|
|
||||||
);
|
|
||||||
|
|
||||||
NTSYSAPI
|
|
||||||
NTSTATUS
|
|
||||||
NTAPI
|
|
||||||
NtInitiatePowerAction(
|
|
||||||
IN POWER_ACTION SystemAction,
|
|
||||||
IN SYSTEM_POWER_STATE MinSystemState,
|
|
||||||
IN ULONG Flags,
|
|
||||||
IN BOOLEAN Asynchronous
|
|
||||||
);
|
|
||||||
|
|
||||||
NTSYSAPI
|
|
||||||
NTSTATUS
|
|
||||||
NTAPI
|
|
||||||
NtRequestDeviceWakeup(
|
|
||||||
IN HANDLE Device
|
|
||||||
);
|
|
||||||
|
|
||||||
NTSYSAPI
|
|
||||||
NTSTATUS
|
|
||||||
NTAPI
|
|
||||||
NtCancelDeviceWakeupRequest(
|
|
||||||
IN HANDLE Device
|
|
||||||
);
|
|
||||||
|
|
||||||
/* PUBLIC FUNCTIONS ***********************************************************/
|
/* PUBLIC FUNCTIONS ***********************************************************/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -75,15 +22,16 @@ NtCancelDeviceWakeupRequest(
|
||||||
*/
|
*/
|
||||||
BOOL
|
BOOL
|
||||||
WINAPI
|
WINAPI
|
||||||
GetSystemPowerStatus(LPSYSTEM_POWER_STATUS PowerStatus)
|
GetSystemPowerStatus(IN LPSYSTEM_POWER_STATUS PowerStatus)
|
||||||
{
|
{
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
SYSTEM_BATTERY_STATE SysBatState;
|
SYSTEM_BATTERY_STATE BattState;
|
||||||
|
ULONG Max, Current;
|
||||||
|
|
||||||
Status = NtPowerInformation(SystemBatteryState,
|
Status = NtPowerInformation(SystemBatteryState,
|
||||||
NULL,
|
NULL,
|
||||||
0,
|
0,
|
||||||
&SysBatState,
|
&BattState,
|
||||||
sizeof(SYSTEM_BATTERY_STATE));
|
sizeof(SYSTEM_BATTERY_STATE));
|
||||||
|
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
|
@ -96,35 +44,33 @@ GetSystemPowerStatus(LPSYSTEM_POWER_STATUS PowerStatus)
|
||||||
|
|
||||||
PowerStatus->BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
|
PowerStatus->BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
|
||||||
PowerStatus->BatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
|
PowerStatus->BatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
|
||||||
|
|
||||||
PowerStatus->BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
|
PowerStatus->BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
|
||||||
if (SysBatState.MaxCapacity)
|
|
||||||
{
|
|
||||||
if (SysBatState.MaxCapacity >= SysBatState.RemainingCapacity)
|
|
||||||
PowerStatus->BatteryLifePercent = (SysBatState.RemainingCapacity / SysBatState.MaxCapacity) * 100;
|
|
||||||
else
|
|
||||||
PowerStatus->BatteryLifePercent = 100; /* 100% */
|
|
||||||
|
|
||||||
if (PowerStatus->BatteryLifePercent <= 32)
|
|
||||||
PowerStatus->BatteryFlag |= BATTERY_FLAG_LOW;
|
|
||||||
|
|
||||||
if (PowerStatus->BatteryLifePercent >= 67)
|
|
||||||
PowerStatus->BatteryFlag |= BATTERY_FLAG_HIGH;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!SysBatState.BatteryPresent)
|
|
||||||
PowerStatus->BatteryFlag |= BATTERY_FLAG_NO_BATTERY;
|
|
||||||
|
|
||||||
if (SysBatState.Charging)
|
|
||||||
PowerStatus->BatteryFlag |= BATTERY_FLAG_CHARGING;
|
|
||||||
|
|
||||||
if (!SysBatState.AcOnLine && SysBatState.BatteryPresent)
|
|
||||||
PowerStatus->ACLineStatus = AC_LINE_OFFLINE;
|
|
||||||
else
|
|
||||||
PowerStatus->ACLineStatus = AC_LINE_ONLINE;
|
PowerStatus->ACLineStatus = AC_LINE_ONLINE;
|
||||||
|
|
||||||
if (SysBatState.EstimatedTime)
|
Max = BattState.MaxCapacity;
|
||||||
PowerStatus->BatteryLifeTime = SysBatState.EstimatedTime;
|
Current = BattState.RemainingCapacity;
|
||||||
|
if (Max)
|
||||||
|
{
|
||||||
|
if (Current <= Max)
|
||||||
|
{
|
||||||
|
PowerStatus->BatteryLifePercent = (100 * Current + Max / 2) / Max;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PowerStatus->BatteryLifePercent = 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PowerStatus->BatteryLifePercent <= 32) PowerStatus->BatteryFlag |= BATTERY_FLAG_LOW;
|
||||||
|
if (PowerStatus->BatteryLifePercent >= 67) PowerStatus->BatteryFlag |= BATTERY_FLAG_HIGH;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!BattState.BatteryPresent) PowerStatus->BatteryFlag |= BATTERY_FLAG_NO_BATTERY;
|
||||||
|
|
||||||
|
if (BattState.Charging) PowerStatus->BatteryFlag |= BATTERY_FLAG_CHARGING;
|
||||||
|
|
||||||
|
if (!(BattState.AcOnLine) && (BattState.BatteryPresent)) PowerStatus->ACLineStatus = AC_LINE_OFFLINE;
|
||||||
|
|
||||||
|
if (BattState.EstimatedTime) PowerStatus->BatteryLifeTime = BattState.EstimatedTime;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
@ -132,18 +78,18 @@ GetSystemPowerStatus(LPSYSTEM_POWER_STATUS PowerStatus)
|
||||||
/*
|
/*
|
||||||
* @implemented
|
* @implemented
|
||||||
*/
|
*/
|
||||||
BOOL WINAPI
|
BOOL
|
||||||
SetSystemPowerState(BOOL fSuspend, BOOL fForce)
|
WINAPI
|
||||||
|
SetSystemPowerState(IN BOOL fSuspend,
|
||||||
|
IN BOOL fForce)
|
||||||
{
|
{
|
||||||
SYSTEM_POWER_STATE MinSystemState = (!fSuspend ? PowerSystemHibernate : PowerSystemSleeping1);
|
|
||||||
ULONG Flags = (!fForce ? POWER_ACTION_QUERY_ALLOWED : 0);
|
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
|
|
||||||
Status = NtInitiatePowerAction(PowerActionSleep,
|
Status = NtInitiatePowerAction(PowerActionSleep,
|
||||||
MinSystemState,
|
(fSuspend != FALSE) ?
|
||||||
Flags,
|
PowerSystemSleeping1 : PowerSystemHibernate,
|
||||||
|
fForce != TRUE,
|
||||||
FALSE);
|
FALSE);
|
||||||
|
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
BaseSetLastNTError(Status);
|
BaseSetLastNTError(Status);
|
||||||
|
@ -158,21 +104,17 @@ SetSystemPowerState(BOOL fSuspend, BOOL fForce)
|
||||||
*/
|
*/
|
||||||
BOOL
|
BOOL
|
||||||
WINAPI
|
WINAPI
|
||||||
GetDevicePowerState(HANDLE hDevice, BOOL *pfOn)
|
GetDevicePowerState(IN HANDLE hDevice,
|
||||||
|
OUT BOOL *pfOn)
|
||||||
{
|
{
|
||||||
DEVICE_POWER_STATE DevicePowerState;
|
DEVICE_POWER_STATE DevicePowerState;
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
|
|
||||||
Status = NtGetDevicePowerState(hDevice, &DevicePowerState);
|
Status = NtGetDevicePowerState(hDevice, &DevicePowerState);
|
||||||
|
|
||||||
if (NT_SUCCESS(Status))
|
if (NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
if ((DevicePowerState != PowerDeviceUnspecified) &&
|
*pfOn = (DevicePowerState == PowerDeviceUnspecified) ||
|
||||||
(DevicePowerState != PowerDeviceD0))
|
(DevicePowerState == PowerDeviceD0);
|
||||||
*pfOn = FALSE;
|
|
||||||
else
|
|
||||||
*pfOn = TRUE;
|
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,12 +127,11 @@ GetDevicePowerState(HANDLE hDevice, BOOL *pfOn)
|
||||||
*/
|
*/
|
||||||
BOOL
|
BOOL
|
||||||
WINAPI
|
WINAPI
|
||||||
RequestDeviceWakeup(HANDLE hDevice)
|
RequestDeviceWakeup(IN HANDLE hDevice)
|
||||||
{
|
{
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
|
|
||||||
Status = NtRequestDeviceWakeup(hDevice);
|
Status = NtRequestDeviceWakeup(hDevice);
|
||||||
|
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
BaseSetLastNTError(Status);
|
BaseSetLastNTError(Status);
|
||||||
|
@ -205,12 +146,11 @@ RequestDeviceWakeup(HANDLE hDevice)
|
||||||
*/
|
*/
|
||||||
BOOL
|
BOOL
|
||||||
WINAPI
|
WINAPI
|
||||||
RequestWakeupLatency(LATENCY_TIME latency)
|
RequestWakeupLatency(IN LATENCY_TIME latency)
|
||||||
{
|
{
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
|
|
||||||
Status = NtRequestWakeupLatency(latency);
|
Status = NtRequestWakeupLatency(latency);
|
||||||
|
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
BaseSetLastNTError(Status);
|
BaseSetLastNTError(Status);
|
||||||
|
@ -225,12 +165,11 @@ RequestWakeupLatency(LATENCY_TIME latency)
|
||||||
*/
|
*/
|
||||||
BOOL
|
BOOL
|
||||||
WINAPI
|
WINAPI
|
||||||
CancelDeviceWakeupRequest(HANDLE hDevice)
|
CancelDeviceWakeupRequest(IN HANDLE hDevice)
|
||||||
{
|
{
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
|
|
||||||
Status = NtCancelDeviceWakeupRequest(hDevice);
|
Status = NtCancelDeviceWakeupRequest(hDevice);
|
||||||
|
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
BaseSetLastNTError(Status);
|
BaseSetLastNTError(Status);
|
||||||
|
@ -247,7 +186,7 @@ BOOL
|
||||||
WINAPI
|
WINAPI
|
||||||
IsSystemResumeAutomatic(VOID)
|
IsSystemResumeAutomatic(VOID)
|
||||||
{
|
{
|
||||||
return NtIsSystemResumeAutomatic();
|
return (BOOL)NtIsSystemResumeAutomatic();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -258,6 +197,7 @@ WINAPI
|
||||||
SetMessageWaitingIndicator(IN HANDLE hMsgIndicator,
|
SetMessageWaitingIndicator(IN HANDLE hMsgIndicator,
|
||||||
IN ULONG ulMsgCount)
|
IN ULONG ulMsgCount)
|
||||||
{
|
{
|
||||||
|
/* This is the correct Windows implementation */
|
||||||
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -269,16 +209,14 @@ EXECUTION_STATE
|
||||||
WINAPI
|
WINAPI
|
||||||
SetThreadExecutionState(EXECUTION_STATE esFlags)
|
SetThreadExecutionState(EXECUTION_STATE esFlags)
|
||||||
{
|
{
|
||||||
EXECUTION_STATE OldFlags;
|
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
|
|
||||||
Status = NtSetThreadExecutionState(esFlags, &OldFlags);
|
Status = NtSetThreadExecutionState(esFlags, &esFlags);
|
||||||
|
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
BaseSetLastNTError(Status);
|
BaseSetLastNTError(Status);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return OldFlags;
|
return esFlags;
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,58 @@ NtSetSystemPowerState(
|
||||||
IN ULONG Flags
|
IN ULONG Flags
|
||||||
);
|
);
|
||||||
|
|
||||||
|
NTSYSAPI
|
||||||
|
NTSTATUS
|
||||||
|
NTAPI
|
||||||
|
NtGetDevicePowerState(
|
||||||
|
IN HANDLE Device,
|
||||||
|
IN PDEVICE_POWER_STATE PowerState
|
||||||
|
);
|
||||||
|
|
||||||
|
NTSYSAPI
|
||||||
|
NTSTATUS
|
||||||
|
NTAPI
|
||||||
|
NtRequestWakeupLatency(
|
||||||
|
IN LATENCY_TIME latency
|
||||||
|
);
|
||||||
|
|
||||||
|
NTSYSAPI
|
||||||
|
BOOLEAN
|
||||||
|
NTAPI
|
||||||
|
NtIsSystemResumeAutomatic(VOID);
|
||||||
|
|
||||||
|
NTSYSAPI
|
||||||
|
NTSTATUS
|
||||||
|
NTAPI
|
||||||
|
NtSetThreadExecutionState(
|
||||||
|
IN EXECUTION_STATE esFlags,
|
||||||
|
OUT EXECUTION_STATE *PreviousFlags
|
||||||
|
);
|
||||||
|
|
||||||
|
NTSYSAPI
|
||||||
|
NTSTATUS
|
||||||
|
NTAPI
|
||||||
|
NtInitiatePowerAction(
|
||||||
|
IN POWER_ACTION SystemAction,
|
||||||
|
IN SYSTEM_POWER_STATE MinSystemState,
|
||||||
|
IN ULONG Flags,
|
||||||
|
IN BOOLEAN Asynchronous
|
||||||
|
);
|
||||||
|
|
||||||
|
NTSYSAPI
|
||||||
|
NTSTATUS
|
||||||
|
NTAPI
|
||||||
|
NtRequestDeviceWakeup(
|
||||||
|
IN HANDLE Device
|
||||||
|
);
|
||||||
|
|
||||||
|
NTSYSAPI
|
||||||
|
NTSTATUS
|
||||||
|
NTAPI
|
||||||
|
NtCancelDeviceWakeupRequest(
|
||||||
|
IN HANDLE Device
|
||||||
|
);
|
||||||
|
|
||||||
NTSYSAPI
|
NTSYSAPI
|
||||||
NTSTATUS
|
NTSTATUS
|
||||||
NTAPI
|
NTAPI
|
||||||
|
|
Loading…
Reference in a new issue