Author: ion
Date: Sun Jul 24 16:50:03 2011
New Revision: 52841
URL:
http://svn.reactos.org/svn/reactos?rev=52841&view=rev
Log:
[NDK]: Add missing native power functions.
[KERNEL32]: Cleanup the power APIs. Fix calculation of battery percentage in
GetSystemPowerStatus.
Modified:
trunk/reactos/dll/win32/kernel32/client/power.c
trunk/reactos/include/ndk/pofuncs.h
Modified: trunk/reactos/dll/win32/kernel32/client/power.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/client/power.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/client/power.c [iso-8859-1] Sun Jul 24 16:50:03 2011
@@ -15,59 +15,6 @@
#define NDEBUG
#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 ***********************************************************/
/*
@@ -75,15 +22,16 @@
*/
BOOL
WINAPI
-GetSystemPowerStatus(LPSYSTEM_POWER_STATUS PowerStatus)
-{
- NTSTATUS Status;
- SYSTEM_BATTERY_STATE SysBatState;
+GetSystemPowerStatus(IN LPSYSTEM_POWER_STATUS PowerStatus)
+{
+ NTSTATUS Status;
+ SYSTEM_BATTERY_STATE BattState;
+ ULONG Max, Current;
Status = NtPowerInformation(SystemBatteryState,
NULL,
0,
- &SysBatState,
+ &BattState,
sizeof(SYSTEM_BATTERY_STATE));
if (!NT_SUCCESS(Status))
@@ -96,83 +44,77 @@
PowerStatus->BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
PowerStatus->BatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
-
PowerStatus->BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
- if (SysBatState.MaxCapacity)
- {
- if (SysBatState.MaxCapacity >= SysBatState.RemainingCapacity)
- PowerStatus->BatteryLifePercent = (SysBatState.RemainingCapacity /
SysBatState.MaxCapacity) * 100;
+ PowerStatus->ACLineStatus = AC_LINE_ONLINE;
+
+ Max = BattState.MaxCapacity;
+ Current = BattState.RemainingCapacity;
+ if (Max)
+ {
+ if (Current <= Max)
+ {
+ PowerStatus->BatteryLifePercent = (100 * Current + Max / 2) / Max;
+ }
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;
-
- if (SysBatState.EstimatedTime)
- PowerStatus->BatteryLifeTime = SysBatState.EstimatedTime;
-
- return TRUE;
-}
-
-/*
- * @implemented
- */
-BOOL WINAPI
-SetSystemPowerState(BOOL fSuspend, BOOL fForce)
-{
- SYSTEM_POWER_STATE MinSystemState = (!fSuspend ? PowerSystemHibernate :
PowerSystemSleeping1);
- ULONG Flags = (!fForce ? POWER_ACTION_QUERY_ALLOWED : 0);
+ {
+ 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;
+}
+
+/*
+ * @implemented
+ */
+BOOL
+WINAPI
+SetSystemPowerState(IN BOOL fSuspend,
+ IN BOOL fForce)
+{
NTSTATUS Status;
Status = NtInitiatePowerAction(PowerActionSleep,
- MinSystemState,
- Flags,
+ (fSuspend != FALSE) ?
+ PowerSystemSleeping1 : PowerSystemHibernate,
+ fForce != TRUE,
FALSE);
-
- if (!NT_SUCCESS(Status))
- {
- BaseSetLastNTError(Status);
- return FALSE;
- }
-
- return TRUE;
-}
-
-/*
- * @implemented
- */
-BOOL
-WINAPI
-GetDevicePowerState(HANDLE hDevice, BOOL *pfOn)
+ if (!NT_SUCCESS(Status))
+ {
+ BaseSetLastNTError(Status);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/*
+ * @implemented
+ */
+BOOL
+WINAPI
+GetDevicePowerState(IN HANDLE hDevice,
+ OUT BOOL *pfOn)
{
DEVICE_POWER_STATE DevicePowerState;
NTSTATUS Status;
Status = NtGetDevicePowerState(hDevice, &DevicePowerState);
-
if (NT_SUCCESS(Status))
{
- if ((DevicePowerState != PowerDeviceUnspecified) &&
- (DevicePowerState != PowerDeviceD0))
- *pfOn = FALSE;
- else
- *pfOn = TRUE;
-
+ *pfOn = (DevicePowerState == PowerDeviceUnspecified) ||
+ (DevicePowerState == PowerDeviceD0);
return TRUE;
}
@@ -185,52 +127,49 @@
*/
BOOL
WINAPI
-RequestDeviceWakeup(HANDLE hDevice)
+RequestDeviceWakeup(IN HANDLE hDevice)
{
NTSTATUS Status;
Status = NtRequestDeviceWakeup(hDevice);
-
- if (!NT_SUCCESS(Status))
- {
- BaseSetLastNTError(Status);
- return FALSE;
- }
-
- return TRUE;
-}
-
-/*
- * @implemented
- */
-BOOL
-WINAPI
-RequestWakeupLatency(LATENCY_TIME latency)
+ if (!NT_SUCCESS(Status))
+ {
+ BaseSetLastNTError(Status);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/*
+ * @implemented
+ */
+BOOL
+WINAPI
+RequestWakeupLatency(IN LATENCY_TIME latency)
{
NTSTATUS Status;
Status = NtRequestWakeupLatency(latency);
-
- if (!NT_SUCCESS(Status))
- {
- BaseSetLastNTError(Status);
- return FALSE;
- }
-
- return TRUE;
-}
-
-/*
- * @implemented
- */
-BOOL
-WINAPI
-CancelDeviceWakeupRequest(HANDLE hDevice)
+ if (!NT_SUCCESS(Status))
+ {
+ BaseSetLastNTError(Status);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/*
+ * @implemented
+ */
+BOOL
+WINAPI
+CancelDeviceWakeupRequest(IN HANDLE hDevice)
{
NTSTATUS Status;
Status = NtCancelDeviceWakeupRequest(hDevice);
-
if (!NT_SUCCESS(Status))
{
BaseSetLastNTError(Status);
@@ -247,7 +186,7 @@
WINAPI
IsSystemResumeAutomatic(VOID)
{
- return NtIsSystemResumeAutomatic();
+ return (BOOL)NtIsSystemResumeAutomatic();
}
/*
@@ -258,6 +197,7 @@
SetMessageWaitingIndicator(IN HANDLE hMsgIndicator,
IN ULONG ulMsgCount)
{
+ /* This is the correct Windows implementation */
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return 0;
}
@@ -269,16 +209,14 @@
WINAPI
SetThreadExecutionState(EXECUTION_STATE esFlags)
{
- EXECUTION_STATE OldFlags;
- NTSTATUS Status;
-
- Status = NtSetThreadExecutionState(esFlags, &OldFlags);
-
+ NTSTATUS Status;
+
+ Status = NtSetThreadExecutionState(esFlags, &esFlags);
if (!NT_SUCCESS(Status))
{
BaseSetLastNTError(Status);
return 0;
}
- return OldFlags;
-}
+ return esFlags;
+}
Modified: trunk/reactos/include/ndk/pofuncs.h
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/include/ndk/pofuncs.h?rev=…
==============================================================================
--- trunk/reactos/include/ndk/pofuncs.h [iso-8859-1] (original)
+++ trunk/reactos/include/ndk/pofuncs.h [iso-8859-1] Sun Jul 24 16:50:03 2011
@@ -60,6 +60,58 @@
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
+NTSTATUS
+NTAPI
ZwInitiatePowerAction(
POWER_ACTION SystemAction,
SYSTEM_POWER_STATE MinSystemState,