2009-01-15 15:32:30 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* FILE: dll/win32/kernel32/misc/power.c
|
|
|
|
* PURPOSE: Power Management Functions
|
|
|
|
* PROGRAMMER: Dmitry Chapyshev <dmitry@reactos.org>
|
|
|
|
*
|
|
|
|
* UPDATE HISTORY:
|
|
|
|
* 01/15/2009 Created
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <k32.h>
|
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/* PUBLIC FUNCTIONS ***********************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
2011-07-24 16:50:03 +00:00
|
|
|
GetSystemPowerStatus(IN LPSYSTEM_POWER_STATUS PowerStatus)
|
2009-01-15 15:32:30 +00:00
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
2011-07-24 16:50:03 +00:00
|
|
|
SYSTEM_BATTERY_STATE BattState;
|
|
|
|
ULONG Max, Current;
|
2009-01-15 15:32:30 +00:00
|
|
|
|
|
|
|
Status = NtPowerInformation(SystemBatteryState,
|
|
|
|
NULL,
|
|
|
|
0,
|
2011-07-24 16:50:03 +00:00
|
|
|
&BattState,
|
2009-01-15 15:32:30 +00:00
|
|
|
sizeof(SYSTEM_BATTERY_STATE));
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2011-07-23 18:54:29 +00:00
|
|
|
BaseSetLastNTError(Status);
|
2009-01-15 15:32:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-09-20 13:53:25 +00:00
|
|
|
RtlZeroMemory(PowerStatus, sizeof(SYSTEM_POWER_STATUS));
|
2009-01-15 15:32:30 +00:00
|
|
|
|
|
|
|
PowerStatus->BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
|
|
|
|
PowerStatus->BatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
|
|
|
|
PowerStatus->BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
|
2011-07-24 16:50:03 +00:00
|
|
|
PowerStatus->ACLineStatus = AC_LINE_ONLINE;
|
|
|
|
|
|
|
|
Max = BattState.MaxCapacity;
|
|
|
|
Current = BattState.RemainingCapacity;
|
|
|
|
if (Max)
|
2009-01-15 15:32:30 +00:00
|
|
|
{
|
2011-07-24 16:50:03 +00:00
|
|
|
if (Current <= Max)
|
|
|
|
{
|
|
|
|
PowerStatus->BatteryLifePercent = (100 * Current + Max / 2) / Max;
|
|
|
|
}
|
2009-01-15 15:32:30 +00:00
|
|
|
else
|
2011-07-24 16:50:03 +00:00
|
|
|
{
|
|
|
|
PowerStatus->BatteryLifePercent = 100;
|
|
|
|
}
|
2009-01-15 15:32:30 +00:00
|
|
|
|
2011-07-24 16:50:03 +00:00
|
|
|
if (PowerStatus->BatteryLifePercent <= 32) PowerStatus->BatteryFlag |= BATTERY_FLAG_LOW;
|
|
|
|
if (PowerStatus->BatteryLifePercent >= 67) PowerStatus->BatteryFlag |= BATTERY_FLAG_HIGH;
|
2009-01-15 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
2011-07-24 16:50:03 +00:00
|
|
|
if (!BattState.BatteryPresent) PowerStatus->BatteryFlag |= BATTERY_FLAG_NO_BATTERY;
|
2009-01-15 15:32:30 +00:00
|
|
|
|
2011-07-24 16:50:03 +00:00
|
|
|
if (BattState.Charging) PowerStatus->BatteryFlag |= BATTERY_FLAG_CHARGING;
|
2009-01-15 15:32:30 +00:00
|
|
|
|
2011-07-24 16:50:03 +00:00
|
|
|
if (!(BattState.AcOnLine) && (BattState.BatteryPresent)) PowerStatus->ACLineStatus = AC_LINE_OFFLINE;
|
2009-01-15 15:32:30 +00:00
|
|
|
|
2011-07-24 16:50:03 +00:00
|
|
|
if (BattState.EstimatedTime) PowerStatus->BatteryLifeTime = BattState.EstimatedTime;
|
2009-01-15 15:32:30 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-01-16 06:33:19 +00:00
|
|
|
* @implemented
|
2009-01-15 15:32:30 +00:00
|
|
|
*/
|
2011-07-24 16:50:03 +00:00
|
|
|
BOOL
|
|
|
|
WINAPI
|
|
|
|
SetSystemPowerState(IN BOOL fSuspend,
|
|
|
|
IN BOOL fForce)
|
2009-01-15 15:32:30 +00:00
|
|
|
{
|
2009-01-16 06:33:19 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Status = NtInitiatePowerAction(PowerActionSleep,
|
2011-07-24 16:50:03 +00:00
|
|
|
(fSuspend != FALSE) ?
|
|
|
|
PowerSystemSleeping1 : PowerSystemHibernate,
|
|
|
|
fForce != TRUE,
|
2009-01-16 06:33:19 +00:00
|
|
|
FALSE);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2011-07-23 18:54:29 +00:00
|
|
|
BaseSetLastNTError(Status);
|
2009-01-16 06:33:19 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2009-01-15 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-01-15 16:09:03 +00:00
|
|
|
* @implemented
|
2009-01-15 15:32:30 +00:00
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
2011-07-24 16:50:03 +00:00
|
|
|
GetDevicePowerState(IN HANDLE hDevice,
|
|
|
|
OUT BOOL *pfOn)
|
2009-01-15 15:32:30 +00:00
|
|
|
{
|
2009-01-15 16:09:03 +00:00
|
|
|
DEVICE_POWER_STATE DevicePowerState;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Status = NtGetDevicePowerState(hDevice, &DevicePowerState);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
2011-07-24 16:50:03 +00:00
|
|
|
*pfOn = (DevicePowerState == PowerDeviceUnspecified) ||
|
|
|
|
(DevicePowerState == PowerDeviceD0);
|
2009-01-15 16:09:03 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:54:29 +00:00
|
|
|
BaseSetLastNTError(Status);
|
2009-01-15 16:09:03 +00:00
|
|
|
return FALSE;
|
2009-01-15 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-01-16 06:44:40 +00:00
|
|
|
* @implemented
|
2009-01-15 15:32:30 +00:00
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
2011-07-24 16:50:03 +00:00
|
|
|
RequestDeviceWakeup(IN HANDLE hDevice)
|
2009-01-15 15:32:30 +00:00
|
|
|
{
|
2009-01-16 06:44:40 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Status = NtRequestDeviceWakeup(hDevice);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2011-07-23 18:54:29 +00:00
|
|
|
BaseSetLastNTError(Status);
|
2009-01-16 06:44:40 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2009-01-15 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-01-15 16:09:03 +00:00
|
|
|
* @implemented
|
2009-01-15 15:32:30 +00:00
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
2011-07-24 16:50:03 +00:00
|
|
|
RequestWakeupLatency(IN LATENCY_TIME latency)
|
2009-01-15 15:32:30 +00:00
|
|
|
{
|
2009-01-15 16:09:03 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Status = NtRequestWakeupLatency(latency);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2011-07-23 18:54:29 +00:00
|
|
|
BaseSetLastNTError(Status);
|
2009-01-15 16:09:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2009-01-15 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-01-16 06:44:40 +00:00
|
|
|
* @implemented
|
2009-01-15 15:32:30 +00:00
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
2011-07-24 16:50:03 +00:00
|
|
|
CancelDeviceWakeupRequest(IN HANDLE hDevice)
|
2009-01-15 15:32:30 +00:00
|
|
|
{
|
2009-01-16 06:44:40 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Status = NtCancelDeviceWakeupRequest(hDevice);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2011-07-23 18:54:29 +00:00
|
|
|
BaseSetLastNTError(Status);
|
2009-01-16 06:44:40 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2009-01-15 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-01-16 06:44:40 +00:00
|
|
|
* @implemented
|
2009-01-15 15:32:30 +00:00
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
|
|
|
IsSystemResumeAutomatic(VOID)
|
|
|
|
{
|
2011-07-24 16:50:03 +00:00
|
|
|
return (BOOL)NtIsSystemResumeAutomatic();
|
2009-01-15 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-07-23 22:03:03 +00:00
|
|
|
* @implemented
|
2009-01-15 15:32:30 +00:00
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
2011-07-23 22:03:03 +00:00
|
|
|
SetMessageWaitingIndicator(IN HANDLE hMsgIndicator,
|
|
|
|
IN ULONG ulMsgCount)
|
2009-01-15 15:32:30 +00:00
|
|
|
{
|
2011-07-24 16:50:03 +00:00
|
|
|
/* This is the correct Windows implementation */
|
2009-01-16 06:33:19 +00:00
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
2009-01-15 15:32:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-01-15 17:07:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
EXECUTION_STATE
|
|
|
|
WINAPI
|
|
|
|
SetThreadExecutionState(EXECUTION_STATE esFlags)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
2011-07-24 16:50:03 +00:00
|
|
|
Status = NtSetThreadExecutionState(esFlags, &esFlags);
|
2009-01-15 17:07:01 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2011-07-23 18:54:29 +00:00
|
|
|
BaseSetLastNTError(Status);
|
2009-01-15 17:07:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-24 16:50:03 +00:00
|
|
|
return esFlags;
|
2009-01-15 17:07:01 +00:00
|
|
|
}
|