From b5b0873982ea1e4e47198bde1dc8606f2e2ab922 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Mon, 4 Feb 2019 21:08:37 -0800 Subject: [PATCH 1/5] Add CallNTPowerInformation --- CHANGES.md | 1 + .../com/sun/jna/platform/win32/NTStatus.java | 9 + .../com/sun/jna/platform/win32/PowrProf.java | 161 ++++++++++++++++++ .../sun/jna/platform/win32/PowrProfTest.java | 67 ++++++++ 4 files changed, 238 insertions(+) create mode 100644 contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java create mode 100644 contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java diff --git a/CHANGES.md b/CHANGES.md index 4ab7bcb770..1ca55536ed 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ Features * [#1058](https://github.com/java-native-access/jna/pull/1058): Add selectable timeout to stopService() and improve timeout handling - [@keithharp](https://github.com/keithharp). * [#1050](https://github.com/java-native-access/jna/pull/1050): Add `c.s.j.p.win32.VersionHelpers` and supporting functions - [@dbwiddis](https://github.com/dbwiddis). * [#1061](https://github.com/java-native-access/jna/pull/1061): replace toArray(new T[size]) with toArray(new T[0]) for better performance - [@hc-codersatlas](https://github.com/hc-codersatlas). +* [#1065](https://github.com/java-native-access/jna/pull/1065): Add `c.s.j.p.win32.PowrProf#CallNTPowerInformation` and some supporting structures - [@dbwiddis](https://github.com/dbwiddis). Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/win32/NTStatus.java b/contrib/platform/src/com/sun/jna/platform/win32/NTStatus.java index 37111794ea..dac89621f5 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/NTStatus.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/NTStatus.java @@ -105,5 +105,14 @@ public interface NTStatus { // Indicates a particular Security ID may not be assigned as the owner of an object. // int STATUS_INVALID_OWNER = 0xC000005A; + + // MessageId: STATUS_ACCESS_DENIED + // + // MessageText: + // + // A process has requested access to an object, but has not been granted + // those access rights. + // + int STATUS_ACCESS_DENIED = 0xC0000022; } diff --git a/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java new file mode 100644 index 0000000000..d964dea2d2 --- /dev/null +++ b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java @@ -0,0 +1,161 @@ +/* Copyright (c) 2019 Daniel Widdis, All Rights Reserved + * + * The contents of this file is dual-licensed under 2 + * alternative Open Source/Free licenses: LGPL 2.1 or later and + * Apache License 2.0. (starting with JNA version 4.0.0). + * + * You can freely decide which license you want to apply to + * the project. + * + * You may obtain a copy of the LGPL License at: + * + * http://www.gnu.org/licenses/licenses.html + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "LGPL2.1". + * + * You may obtain a copy of the Apache License at: + * + * http://www.apache.org/licenses/ + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "AL2.0". + */ +package com.sun.jna.platform.win32; + +import com.sun.jna.Library; +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import com.sun.jna.Structure; +import com.sun.jna.Structure.FieldOrder; + +/** + * Functions used with power management. + */ +public interface PowrProf extends Library { + PowrProf INSTANCE = Native.load("PowrProf", PowrProf.class); + + /** + * Indicates power level information. + */ + public interface POWER_INFORMATION_LEVEL { + int LAST_SLEEP_TIME = 15; + int LAST_WAKE_TIME = 14; + int PROCESSOR_INFORMATION = 11; + int SYSTEM_BATTERY_STATE = 5; + int SYSTEM_EXECUTION_STATE = 16; + int SYSTEM_POWER_CAPABILITIES = 4; + int SYSTEM_POWER_INFORMATION = 12; + int SYSTEM_POWER_POLICY_AC = 0; + int SYSTEM_POWER_POLICY_CURRENT = 8; + int SYSTEM_POWER_POLICY_DC = 1; + int SYSTEM_RESERVE_HIBER_FILE = 10; + } + + /** + * Contains information about the current state of the system battery. + */ + @FieldOrder({ "acOnLine", "batteryPresent", "charging", "discharging", "spare1", "tag", "maxCapacity", + "remainingCapacity", "rate", "estimatedTime", "defaultAlert1", "defaultAlert2" }) + class SystemBatteryState extends Structure { + public byte acOnLine; + public byte batteryPresent; + public byte charging; + public byte discharging; + public byte[] spare1 = new byte[3]; + public byte tag; + public int maxCapacity; + public int remainingCapacity; + public int rate; + public int estimatedTime; + public int defaultAlert1; + public int defaultAlert2; + + public SystemBatteryState(Pointer p) { + super(p); + read(); + } + + public SystemBatteryState() { + super(); + } + } + + /** + * Contains information about a processor. + */ + @FieldOrder({ "number", "maxMhz", "currentMhz", "mhzLimit", "maxIdleState", "currentIdleState" }) + class ProcessorPowerInformation extends Structure { + public int number; + public int maxMhz; + public int currentMhz; + public int mhzLimit; + public int maxIdleState; + public int currentIdleState; + + public ProcessorPowerInformation(Pointer p) { + super(p); + read(); + } + + public ProcessorPowerInformation() { + super(); + } + } + + /** + * Sets or retrieves power information. + *

+ * Changes made to the current system power policy using + * {@link #CallNtPowerInformation()} are immediate, but they are not + * persistent; that is, the changes are not stored as part of a power + * scheme. Any changes to system power policy made with + * {@link #CallNtPowerInformation()} may be overwritten by changes to a + * policy scheme made by the user in the Power Options control panel + * program, or by subsequent calls to {@code WritePwrScheme}, + * {@code SetActivePwrScheme}, or other power scheme functions. + * + * @param informationLevel + * The information level requested. This value indicates the + * specific power information to be set or retrieved. This + * parameter must be one of the following + * {@link POWER_INFORMATION_LEVEL} enumeration type values: + * {@link POWER_INFORMATION_LEVEL#LAST_SLEEP_TIME}, + * {@link POWER_INFORMATION_LEVEL#LAST_WAKE_TIME}, + * {@link POWER_INFORMATION_LEVEL#PROCESSOR_INFORMATION}, + * {@link POWER_INFORMATION_LEVEL#SYSTEM_BATTERY_STATE}, + * {@link POWER_INFORMATION_LEVEL#SYSTEM_EXECUTION_STATE}, + * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_CAPABILITIES}, + * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_INFORMATION}, + * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_POLICY_AC}, + * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_POLICY_CURRENT}, + * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_POLICY_DC}, or + * {@link POWER_INFORMATION_LEVEL#SYSTEM_RESERVE_HIBER_FILE}. + * @param lpInputBuffer + * A pointer to an optional input buffer. The data type of this + * buffer depends on the information level requested in the + * {@code informationLevel} parameter. + * @param nInputBufferSize + * The size of the input buffer, in bytes. + * @param lpOutputBuffer + * A pointer to an optional output buffer. The data type of this + * buffer depends on the information level requested in the + * {@code informationLevel} parameter. If the buffer is too small + * to contain the information, the function returns + * {@link NTStatus#STATUS_BUFFER_TOO_SMALL}. + * @param nOutputBufferSize + * The size of the output buffer, in bytes. Depending on the + * information level requested, this may be a variably sized + * buffer. + * @return If the function succeeds, the return value is + * {@link NTStatus#STATUS_SUCCESS}. If the function fails, the + * return value can be one the following status codes: + * {@link NTStatus#STATUS_BUFFER_TOO_SMALL} if the output buffer is + * of insufficient size to contain the data to be returned. + * {@link NTStatus#STATUS_ACCESS_DENIED} if the caller had + * insufficient access rights to perform the requested action. + */ + int CallNtPowerInformation(int informationLevel, Pointer lpInputBuffer, int nInputBufferSize, + Pointer lpOutputBuffer, int nOutputBufferSize); +} + diff --git a/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java b/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java new file mode 100644 index 0000000000..1459718bc4 --- /dev/null +++ b/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java @@ -0,0 +1,67 @@ +/* Copyright (c) 2019 Daniel Widdis, All Rights Reserved + * + * The contents of this file is dual-licensed under 2 + * alternative Open Source/Free licenses: LGPL 2.1 or later and + * Apache License 2.0. (starting with JNA version 4.0.0). + * + * You can freely decide which license you want to apply to + * the project. + * + * You may obtain a copy of the LGPL License at: + * + * http://www.gnu.org/licenses/licenses.html + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "LGPL2.1". + * + * You may obtain a copy of the Apache License at: + * + * http://www.apache.org/licenses/ + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "AL2.0". + */ +package com.sun.jna.platform.win32; + +import com.sun.jna.Memory; +import com.sun.jna.platform.win32.PowrProf.POWER_INFORMATION_LEVEL; +import com.sun.jna.platform.win32.PowrProf.ProcessorPowerInformation; +import com.sun.jna.platform.win32.PowrProf.SystemBatteryState; +import com.sun.jna.platform.win32.WinBase.SYSTEM_INFO; + +import junit.framework.TestCase; + +public class PowrProfTest extends TestCase { + + public void testProcessorPowerInformation() { + SYSTEM_INFO info = new SYSTEM_INFO(); + Kernel32.INSTANCE.GetSystemInfo(info); + int numProcs = info.dwNumberOfProcessors.intValue(); + + ProcessorPowerInformation ppi = new ProcessorPowerInformation(); + long[] freqs = new long[numProcs]; + int bufferSize = ppi.size() * numProcs; + Memory mem = new Memory(bufferSize); + assertEquals(NTStatus.STATUS_SUCCESS, PowrProf.INSTANCE + .CallNtPowerInformation(POWER_INFORMATION_LEVEL.PROCESSOR_INFORMATION, null, 0, + mem, bufferSize)); + for (int i = 0; i < freqs.length; i++) { + ppi = new ProcessorPowerInformation(mem.share(i * (long) ppi.size())); + assertTrue(ppi.currentMhz <= ppi.maxMhz); + } + } + + public void testSystemBatteryState() { + int size = new SystemBatteryState().size(); + Memory mem = new Memory(size); + assertEquals(NTStatus.STATUS_SUCCESS, + PowrProf.INSTANCE.CallNtPowerInformation(POWER_INFORMATION_LEVEL.SYSTEM_BATTERY_STATE, null, 0, mem, size)); + SystemBatteryState batteryState = new SystemBatteryState(mem); + if (batteryState.batteryPresent > 0) { + if (batteryState.acOnLine == 0 && batteryState.charging == 0 && batteryState.discharging > 0) { + assertTrue(batteryState.estimatedTime >= 0); + } + assertTrue(batteryState.remainingCapacity <= batteryState.maxCapacity); + } + } +} From 7d44574e4f3d9530b3d3145e8f63edc0c9fdad06 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Sun, 10 Feb 2019 11:30:08 -0800 Subject: [PATCH 2/5] Conform structure field names to Windows names --- .../com/sun/jna/platform/win32/PowrProf.java | 42 +++++++++---------- .../sun/jna/platform/win32/PowrProfTest.java | 15 ++++--- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java index d964dea2d2..47964332c2 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java @@ -55,21 +55,21 @@ public interface POWER_INFORMATION_LEVEL { /** * Contains information about the current state of the system battery. */ - @FieldOrder({ "acOnLine", "batteryPresent", "charging", "discharging", "spare1", "tag", "maxCapacity", - "remainingCapacity", "rate", "estimatedTime", "defaultAlert1", "defaultAlert2" }) + @FieldOrder({ "AcOnLine", "BatteryPresent", "Charging", "Discharging", "Spare1", "Tag", "MaxCapacity", + "RemainingCapacity", "Rate", "EstimatedTime", "DefaultAlert1", "DefaultAlert2" }) class SystemBatteryState extends Structure { - public byte acOnLine; - public byte batteryPresent; - public byte charging; - public byte discharging; - public byte[] spare1 = new byte[3]; - public byte tag; - public int maxCapacity; - public int remainingCapacity; - public int rate; - public int estimatedTime; - public int defaultAlert1; - public int defaultAlert2; + public byte AcOnLine; + public byte BatteryPresent; + public byte Charging; + public byte Discharging; + public byte[] Spare1 = new byte[3]; + public byte Tag; + public int MaxCapacity; + public int RemainingCapacity; + public int Rate; + public int EstimatedTime; + public int DefaultAlert1; + public int DefaultAlert2; public SystemBatteryState(Pointer p) { super(p); @@ -84,14 +84,14 @@ public SystemBatteryState() { /** * Contains information about a processor. */ - @FieldOrder({ "number", "maxMhz", "currentMhz", "mhzLimit", "maxIdleState", "currentIdleState" }) + @FieldOrder({ "Number", "MaxMhz", "CurrentMhz", "MhzLimit", "MaxIdleState", "CurrentIdleState" }) class ProcessorPowerInformation extends Structure { - public int number; - public int maxMhz; - public int currentMhz; - public int mhzLimit; - public int maxIdleState; - public int currentIdleState; + public int Number; + public int MaxMhz; + public int CurrentMhz; + public int MhzLimit; + public int MaxIdleState; + public int CurrentIdleState; public ProcessorPowerInformation(Pointer p) { super(p); diff --git a/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java b/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java index 1459718bc4..03237155fb 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java @@ -34,6 +34,11 @@ public class PowrProfTest extends TestCase { public void testProcessorPowerInformation() { + // MSDN docs for CallNTPowerInformation specify use of GetSystemInfo to + // count logical processors for this InformationLevel. The GetSystemInfo + // function only counts logical processors on the current Processor + // Group. However, the array of results from ProcessorPowerInformation + // also are restricted to the current Processor Group. SYSTEM_INFO info = new SYSTEM_INFO(); Kernel32.INSTANCE.GetSystemInfo(info); int numProcs = info.dwNumberOfProcessors.intValue(); @@ -47,7 +52,7 @@ public void testProcessorPowerInformation() { mem, bufferSize)); for (int i = 0; i < freqs.length; i++) { ppi = new ProcessorPowerInformation(mem.share(i * (long) ppi.size())); - assertTrue(ppi.currentMhz <= ppi.maxMhz); + assertTrue(ppi.CurrentMhz <= ppi.MaxMhz); } } @@ -57,11 +62,11 @@ public void testSystemBatteryState() { assertEquals(NTStatus.STATUS_SUCCESS, PowrProf.INSTANCE.CallNtPowerInformation(POWER_INFORMATION_LEVEL.SYSTEM_BATTERY_STATE, null, 0, mem, size)); SystemBatteryState batteryState = new SystemBatteryState(mem); - if (batteryState.batteryPresent > 0) { - if (batteryState.acOnLine == 0 && batteryState.charging == 0 && batteryState.discharging > 0) { - assertTrue(batteryState.estimatedTime >= 0); + if (batteryState.BatteryPresent > 0) { + if (batteryState.AcOnLine == 0 && batteryState.Charging == 0 && batteryState.Discharging > 0) { + assertTrue(batteryState.EstimatedTime >= 0); } - assertTrue(batteryState.remainingCapacity <= batteryState.maxCapacity); + assertTrue(batteryState.RemainingCapacity <= batteryState.MaxCapacity); } } } From 6f5096c15fef1867ba1b9ce161b75331b4731932 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Sat, 16 Feb 2019 15:58:29 -0800 Subject: [PATCH 3/5] Fix case and add structures --- .../com/sun/jna/platform/win32/PowrProf.java | 346 ++++++++++++++++-- .../src/com/sun/jna/platform/win32/WinNT.java | 29 ++ .../sun/jna/platform/win32/PowrProfTest.java | 77 +++- 3 files changed, 407 insertions(+), 45 deletions(-) diff --git a/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java index 47964332c2..8ffdb39a62 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java @@ -35,21 +35,142 @@ public interface PowrProf extends Library { PowrProf INSTANCE = Native.load("PowrProf", PowrProf.class); + int NUM_DISCHARGE_POLICIES = 4; + /** - * Indicates power level information. + * Enum which indicates the power information level requested from + * {@link #CallNtPowerInformation()}. This value indicates the specific + * power information to be set or retrieved. */ public interface POWER_INFORMATION_LEVEL { - int LAST_SLEEP_TIME = 15; - int LAST_WAKE_TIME = 14; - int PROCESSOR_INFORMATION = 11; - int SYSTEM_BATTERY_STATE = 5; - int SYSTEM_EXECUTION_STATE = 16; - int SYSTEM_POWER_CAPABILITIES = 4; - int SYSTEM_POWER_INFORMATION = 12; - int SYSTEM_POWER_POLICY_AC = 0; - int SYSTEM_POWER_POLICY_CURRENT = 8; - int SYSTEM_POWER_POLICY_DC = 1; - int SYSTEM_RESERVE_HIBER_FILE = 10; + /** + * The {@code lpInBuffer} parameter must be {@code NULL}; otherwise, the + * function returns {@link WinError#ERROR_INVALID_PARAMETER}. + *

+ * The {@code lpOutputBuffer} buffer receives a {@link WinDef#ULONGLONG} + * that specifies the interrupt-time count, in 100-nanosecond units, at + * the last system sleep time. + */ + int LastSleepTime = 15; + + /** + * The {@code lpInBuffer} parameter must be {@code NULL}; otherwise, the + * function returns {@link WinError#ERROR_INVALID_PARAMETER}. + *

+ * The {@code lpOutputBuffer} buffer receives a {@link WinDef#ULONGLONG} + * that specifies the interrupt-time count, in 100-nanosecond units, at + * the last system wake time. + */ + int LastWakeTime = 14; + + /** + * The {@code lpInBuffer} parameter must be {@code NULL}; otherwise, the + * function returns {@link WinError#ERROR_INVALID_PARAMETER}. + *

+ * The {@code lpOutputBuffer} buffer receives one + * {@link PROCESSOR_POWER_INFORMATION} structure for each processor that + * is installed on the system. Use the {@link Kernel32#GetSystemInfo()} + * function to retrieve the number of processors, which will be the + * number of logical processors on the current Processor Group. + */ + int ProcessorInformation = 11; + + /** + * The {@code lpInBuffer} parameter must be {@code NULL}; otherwise, the + * function returns {@link WinError#ERROR_INVALID_PARAMETER}. + *

+ * The {@code lpOutputBuffer} buffer receives a + * {@link SYSTEM_BATTERY_STATE} structure containing information about + * the current system battery. + */ + int SystemBatteryState = 5; + + /** + * The {@code lpInBuffer} parameter must be {@code NULL}; otherwise, the + * function returns {@link WinError#ERROR_INVALID_PARAMETER}. + *

+ * The {@code lpOutputBuffer} buffer receives a {@link WinDef#ULONG} + * value containing the system execution state buffer. This value may + * contain any combination of the following values: + * {@link WinBase#ES_SYSTEM_REQUIRED}, + * {@link WinBase#ES_DISPLAY_REQUIRED}, or + * {@link WinBase#ES_USER_PRESENT}. For more information, see the + * {@link {@link Kernel32#SetThreadExecutionState()} function. + */ + int SystemExecutionState = 16; + + /** + * The {@code lpInBuffer} parameter must be {@code NULL}; otherwise, the + * function returns {@link WinError#ERROR_INVALID_PARAMETER}. + *

+ * The {@code lpOutputBuffer} buffer receives a + * {@link SYSTEM_POWER_CAPABILITIES} structure containing the current + * system power capabilities. + *

+ * This information represents the currently supported power + * capabilities. It may change as drivers are installed in the system. + * For example, installation of legacy device drivers that do not + * support power management disables all system sleep states. + */ + int SystemPowerCapabilities = 4; + + /** + * The {@code lpInBuffer} parameter must be {@code NULL}; otherwise, the + * function returns {@link WinError#ERROR_INVALID_PARAMETER}. + *

+ * The {@code lpOutputBuffer} buffer receives a + * {@link SYSTEM_POWER_INFORMATION} structure. + *

+ * Applications can use this level to retrieve information about the + * idleness of the system. + */ + int SystemPowerInformation = 12; + + /** + * If {@code lpInBuffer} is not {@code NULL}, the function applies the + * {@link SYSTEM_POWER_POLICY} values passed in {@code lpInBuffer} to + * the current system power policy used while the system is running on + * AC (utility) power. + *

+ * The {@code lpOutputBuffer} buffer receives a + * {@link SYSTEM_POWER_POLICY} structure containing the current system + * power policy used while the system is running on AC (utility) power. + */ + int SystemPowerPolicyAc = 0; + + /** + * The {@code lpInBuffer} parameter must be {@code NULL}; otherwise, the + * function returns {@link WinError#ERROR_INVALID_PARAMETER}. + *

+ * The {@code lpOutputBuffer} buffer receives a + * {@link SYSTEM_POWER_POLICY} structure containing the current system + * power policy used while the system is running on AC (utility) power. + */ + int SystemPowerPolicyCurrent = 8; + + /** + * If {@code lpInBuffer} is not {@code NULL}, the function applies the + * {@link SYSTEM_POWER_POLICY} values passed in {@code lpInBuffer} to + * the current system power policy used while the system is running on + * battery power. + *

+ * The {@code lpOutputBuffer} buffer receives a + * {@link SYSTEM_POWER_POLICY} structure containing the current system + * power policy used while the system is running on battery power. + */ + int SystemPowerPolicyDc = 1; + + /** + * If {@code lpInBuffer} is not {@code NULL} and the current user has + * sufficient privileges, the function commits or decommits the storage + * required to hold the hibernation image on the boot volume. + *

+ * The lpInBuffer parameter must point to a {@code BOOLEAN} value + * indicating the desired request. If the value is {@code TRUE}, the + * hibernation file is reserved; if the value is {@code FALSE}, the + * hibernation file is removed. + */ + int SystemReserveHiberFile = 10; } /** @@ -57,7 +178,7 @@ public interface POWER_INFORMATION_LEVEL { */ @FieldOrder({ "AcOnLine", "BatteryPresent", "Charging", "Discharging", "Spare1", "Tag", "MaxCapacity", "RemainingCapacity", "Rate", "EstimatedTime", "DefaultAlert1", "DefaultAlert2" }) - class SystemBatteryState extends Structure { + class SYSTEM_BATTERY_STATE extends Structure { public byte AcOnLine; public byte BatteryPresent; public byte Charging; @@ -71,12 +192,12 @@ class SystemBatteryState extends Structure { public int DefaultAlert1; public int DefaultAlert2; - public SystemBatteryState(Pointer p) { + public SYSTEM_BATTERY_STATE(Pointer p) { super(p); read(); } - public SystemBatteryState() { + public SYSTEM_BATTERY_STATE() { super(); } } @@ -85,7 +206,7 @@ public SystemBatteryState() { * Contains information about a processor. */ @FieldOrder({ "Number", "MaxMhz", "CurrentMhz", "MhzLimit", "MaxIdleState", "CurrentIdleState" }) - class ProcessorPowerInformation extends Structure { + class PROCESSOR_POWER_INFORMATION extends Structure { public int Number; public int MaxMhz; public int CurrentMhz; @@ -93,12 +214,179 @@ class ProcessorPowerInformation extends Structure { public int MaxIdleState; public int CurrentIdleState; - public ProcessorPowerInformation(Pointer p) { + public PROCESSOR_POWER_INFORMATION(Pointer p) { + super(p); + read(); + } + + public PROCESSOR_POWER_INFORMATION() { + super(); + } + } + + /** + * Contains the granularity of the battery capacity. + */ + @FieldOrder({ "Granularity", "Capacity" }) + class BATTERY_REPORTING_SCALE extends Structure { + public int Granularity; + public int Capacity; + } + + /** + * Contains information about the power capabilities of the system. + */ + @FieldOrder({ "PowerButtonPresent", "SleepButtonPresent", "LidPresent", "SystemS1", "SystemS2", "SystemS3", + "SystemS4", "SystemS5", "HiberFilePresent", "FullWake", "VideoDimPresent", "ApmPresent", "UpsPresent", + "ThermalControl", "ProcessorThrottle", "ProcessorMinThrottle", "ProcessorMaxThrottle", "FastSystemS4", + "Hiberboot", "WakeAlarmPresent", "AoAc", "DiskSpinDown", "HiberFileType", "AoAcConnectivitySupported", + "spare3", "SystemBatteriesPresent", "BatteriesAreShortTerm", "BatteryScale", "AcOnLineWake", "SoftLidWake", + "RtcWake", "MinDeviceWakeState", "DefaultLowLatencyWake" }) + class SYSTEM_POWER_CAPABILITIES extends Structure { + // Misc supported system features + public byte PowerButtonPresent; + public byte SleepButtonPresent; + public byte LidPresent; + public byte SystemS1; + public byte SystemS2; + public byte SystemS3; + public byte SystemS4; // hibernate + public byte SystemS5; // off + public byte HiberFilePresent; + public byte FullWake; + public byte VideoDimPresent; + public byte ApmPresent; + public byte UpsPresent; + + // Processors + public byte ThermalControl; + public byte ProcessorThrottle; + public byte ProcessorMinThrottle; + + // Prior to WinXP, next 5 bytes are ProcessorThrottleScale + // followed by 4 spare bytes + public byte ProcessorMaxThrottle; + public byte FastSystemS4; + public byte Hiberboot; + public byte WakeAlarmPresent; + public byte AoAc; + + // Disk + public byte DiskSpinDown; + + // HiberFile (Pre-Win10 next 2 bytes are spare) + public byte HiberFileType; + public byte AoAcConnectivitySupported; + public byte[] spare3 = new byte[6]; + + // System Battery + public byte SystemBatteriesPresent; + public byte BatteriesAreShortTerm; + public BATTERY_REPORTING_SCALE[] BatteryScale = new BATTERY_REPORTING_SCALE[3]; + + // Wake + public int /* SYSTEM_POWER_STATE */ AcOnLineWake; + public int /* SYSTEM_POWER_STATE */ SoftLidWake; + public int /* SYSTEM_POWER_STATE */ RtcWake; + public int /* SYSTEM_POWER_STATE */ MinDeviceWakeState; + public int /* SYSTEM_POWER_STATE */ DefaultLowLatencyWake; + + public SYSTEM_POWER_CAPABILITIES(Pointer p) { + super(p); + read(); + } + + public SYSTEM_POWER_CAPABILITIES() { + super(); + } + } + + /** + * Contains information about the idleness of the system. + */ + @FieldOrder({ "MaxIdlenessAllowed", "Idleness", "TimeRemaining", "CoolingMode" }) + class SYSTEM_POWER_INFORMATION extends Structure { + public int MaxIdlenessAllowed; + public int Idleness; + public int TimeRemaining; + public byte CoolingMode; + + public SYSTEM_POWER_INFORMATION(Pointer p) { + super(p); + read(); + } + + public SYSTEM_POWER_INFORMATION() { + super(); + } + } + + /** + * Contains information used to set the system power state. + */ + @FieldOrder({ "Action", "Flags", "EventCode" }) + class POWER_ACTION_POLICY extends Structure { + public int /* POWER_ACTION */ Action; + public int Flags; + public int EventCode; + } + + /** + * Contains information about system battery drain policy settings. + */ + @FieldOrder({ "Enable", "Spare", "BatteryLevel", "PowerPolicy", "MinSystemState" }) + class SYSTEM_POWER_LEVEL extends Structure { + public byte Enable; + public byte[] Spare = new byte[3]; + public int BatteryLevel; + public POWER_ACTION_POLICY PowerPolicy; + public int /* SYSTEM_POWER_STATE */ MinSystemState; + } + + /** + * Contains information about the current system power policy. + */ + @FieldOrder({ "Revision", "PowerButton", "SleepButton", "LidClose", "LidOpenWake", "Reserved", "Idle", + "IdleTimeout", "IdleSensitivity", "DynamicThrottle", "Spare2", "MinSleep", "MaxSleep", + "ReducedLatencySleep", "WinLogonFlags", "Spare3", "DozeS4Timeout", "BroadcastCapacityResolution", + "DischargePolicy", "VideoTimeout", "VideoDimDisplay", "VideoReserved", "SpindownTimeout", + "OptimizeForPower", "FanThrottleTolerance", "ForcedThrottle", "MinThrottle", "OverThrottled" }) + class SYSTEM_POWER_POLICY extends Structure { + public int Revision; + public POWER_ACTION_POLICY PowerButton; + public POWER_ACTION_POLICY SleepButton; + public POWER_ACTION_POLICY LidClose; + public int /* SYSTEM_POWER_STATE */ LidOpenWake; + public int Reserved; + public POWER_ACTION_POLICY Idle; + public int IdleTimeout; + public byte IdleSensitivity; + public byte DynamicThrottle; + public byte[] Spare2 = new byte[2]; + public int /* SYSTEM_POWER_STATE */ MinSleep; + public int /* SYSTEM_POWER_STATE */ MaxSleep; + public int /* SYSTEM_POWER_STATE */ ReducedLatencySleep; + public int WinLogonFlags; + public int Spare3; + public int DozeS4Timeout; + public int BroadcastCapacityResolution; + public SYSTEM_POWER_LEVEL[] DischargePolicy = new SYSTEM_POWER_LEVEL[NUM_DISCHARGE_POLICIES]; + public int VideoTimeout; + public byte VideoDimDisplay; + public int[] VideoReserved = new int[3]; + public int SpindownTimeout; + public byte OptimizeForPower; + public byte FanThrottleTolerance; + public byte ForcedThrottle; + public byte MinThrottle; + public POWER_ACTION_POLICY OverThrottled; + + public SYSTEM_POWER_POLICY(Pointer p) { super(p); read(); } - public ProcessorPowerInformation() { + public SYSTEM_POWER_POLICY() { super(); } } @@ -120,17 +408,17 @@ public ProcessorPowerInformation() { * specific power information to be set or retrieved. This * parameter must be one of the following * {@link POWER_INFORMATION_LEVEL} enumeration type values: - * {@link POWER_INFORMATION_LEVEL#LAST_SLEEP_TIME}, - * {@link POWER_INFORMATION_LEVEL#LAST_WAKE_TIME}, - * {@link POWER_INFORMATION_LEVEL#PROCESSOR_INFORMATION}, - * {@link POWER_INFORMATION_LEVEL#SYSTEM_BATTERY_STATE}, - * {@link POWER_INFORMATION_LEVEL#SYSTEM_EXECUTION_STATE}, - * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_CAPABILITIES}, - * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_INFORMATION}, - * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_POLICY_AC}, - * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_POLICY_CURRENT}, - * {@link POWER_INFORMATION_LEVEL#SYSTEM_POWER_POLICY_DC}, or - * {@link POWER_INFORMATION_LEVEL#SYSTEM_RESERVE_HIBER_FILE}. + * {@link POWER_INFORMATION_LEVEL#LastSleepTime}, + * {@link POWER_INFORMATION_LEVEL#LastWakeTime}, + * {@link POWER_INFORMATION_LEVEL#ProcessorInformation}, + * {@link POWER_INFORMATION_LEVEL#SystemBatteryState}, + * {@link POWER_INFORMATION_LEVEL#SystemExecutionState}, + * {@link POWER_INFORMATION_LEVEL#SystemPowerCapabilities}, + * {@link POWER_INFORMATION_LEVEL#SystemPowerInformation}, + * {@link POWER_INFORMATION_LEVEL#SystemPowerPolicyAc}, + * {@link POWER_INFORMATION_LEVEL#SystemPowerPolicyCurrent}, + * {@link POWER_INFORMATION_LEVEL#SystemPowerPolicyDc}, or + * {@link POWER_INFORMATION_LEVEL#SystemReserveHiberFile}. * @param lpInputBuffer * A pointer to an optional input buffer. The data type of this * buffer depends on the information level requested in the diff --git a/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java b/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java index ab129b5e56..399f2c798e 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java @@ -3395,6 +3395,35 @@ public static abstract class PROCESSOR_CACHE_TYPE { public static int CacheTrace = 3; } + /** + * Defines values that are used to specify system power action types. + */ + public interface POWER_ACTION { + int PowerActionNone = 0; + int PowerActionReserved = 1; + int PowerActionSleep = 2; + int PowerActionHibernate = 3; + int PowerActionShutdown = 4; + int PowerActionShutdownReset = 5; + int PowerActionShutdownOff = 6; + int PowerActionWarmEject = 7; + int PowerActionDisplayOff = 8; + } + + /** + * Defines values that are used to specify system power states. + */ + public interface SYSTEM_POWER_STATE { + int PowerSystemUnspecified = 0; + int PowerSystemWorking = 1; + int PowerSystemSleeping1 = 2; + int PowerSystemSleeping2 = 3; + int PowerSystemSleeping3 = 4; + int PowerSystemHibernate = 5; // S4 + int PowerSystemShutdown = 6; // S5 + int PowerSystemMaximum = 7; + } + /** * Indicates committed pages for which physical storage has been allocated, either in memory or in the paging file on disk. */ diff --git a/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java b/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java index 03237155fb..5fa0fc9d54 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java @@ -25,9 +25,14 @@ import com.sun.jna.Memory; import com.sun.jna.platform.win32.PowrProf.POWER_INFORMATION_LEVEL; -import com.sun.jna.platform.win32.PowrProf.ProcessorPowerInformation; -import com.sun.jna.platform.win32.PowrProf.SystemBatteryState; +import com.sun.jna.platform.win32.PowrProf.PROCESSOR_POWER_INFORMATION; +import com.sun.jna.platform.win32.PowrProf.SYSTEM_BATTERY_STATE; +import com.sun.jna.platform.win32.PowrProf.SYSTEM_POWER_CAPABILITIES; +import com.sun.jna.platform.win32.PowrProf.SYSTEM_POWER_INFORMATION; +import com.sun.jna.platform.win32.PowrProf.SYSTEM_POWER_POLICY; import com.sun.jna.platform.win32.WinBase.SYSTEM_INFO; +import com.sun.jna.platform.win32.WinNT.POWER_ACTION; +import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_STATE; import junit.framework.TestCase; @@ -36,37 +41,77 @@ public class PowrProfTest extends TestCase { public void testProcessorPowerInformation() { // MSDN docs for CallNTPowerInformation specify use of GetSystemInfo to // count logical processors for this InformationLevel. The GetSystemInfo - // function only counts logical processors on the current Processor - // Group. However, the array of results from ProcessorPowerInformation - // also are restricted to the current Processor Group. + // function and this function only count logical processors on the + // current Processor Group, so the array will never have more than 64 + // elements SYSTEM_INFO info = new SYSTEM_INFO(); Kernel32.INSTANCE.GetSystemInfo(info); int numProcs = info.dwNumberOfProcessors.intValue(); - ProcessorPowerInformation ppi = new ProcessorPowerInformation(); - long[] freqs = new long[numProcs]; - int bufferSize = ppi.size() * numProcs; + // Try with too small buffer + int bufferSize = 1; Memory mem = new Memory(bufferSize); + assertEquals(NTStatus.STATUS_BUFFER_TOO_SMALL, PowrProf.INSTANCE + .CallNtPowerInformation(POWER_INFORMATION_LEVEL.ProcessorInformation, null, 0, mem, bufferSize)); + + PROCESSOR_POWER_INFORMATION ppi = new PROCESSOR_POWER_INFORMATION(); + bufferSize = ppi.size() * numProcs; + mem = new Memory(bufferSize); assertEquals(NTStatus.STATUS_SUCCESS, PowrProf.INSTANCE - .CallNtPowerInformation(POWER_INFORMATION_LEVEL.PROCESSOR_INFORMATION, null, 0, - mem, bufferSize)); + .CallNtPowerInformation(POWER_INFORMATION_LEVEL.ProcessorInformation, null, 0, mem, bufferSize)); + + long[] freqs = new long[numProcs]; for (int i = 0; i < freqs.length; i++) { - ppi = new ProcessorPowerInformation(mem.share(i * (long) ppi.size())); + ppi = new PROCESSOR_POWER_INFORMATION(mem.share(i * (long) ppi.size())); assertTrue(ppi.CurrentMhz <= ppi.MaxMhz); } } - public void testSystemBatteryState() { - int size = new SystemBatteryState().size(); + public void testSystemBatteryStateAndPowerCapabilities() { + int size = new SYSTEM_BATTERY_STATE().size(); Memory mem = new Memory(size); - assertEquals(NTStatus.STATUS_SUCCESS, - PowrProf.INSTANCE.CallNtPowerInformation(POWER_INFORMATION_LEVEL.SYSTEM_BATTERY_STATE, null, 0, mem, size)); - SystemBatteryState batteryState = new SystemBatteryState(mem); + assertEquals(NTStatus.STATUS_SUCCESS, PowrProf.INSTANCE + .CallNtPowerInformation(POWER_INFORMATION_LEVEL.SystemBatteryState, null, 0, mem, size)); + SYSTEM_BATTERY_STATE batteryState = new SYSTEM_BATTERY_STATE(mem); if (batteryState.BatteryPresent > 0) { if (batteryState.AcOnLine == 0 && batteryState.Charging == 0 && batteryState.Discharging > 0) { assertTrue(batteryState.EstimatedTime >= 0); } assertTrue(batteryState.RemainingCapacity <= batteryState.MaxCapacity); } + + size = new SYSTEM_POWER_CAPABILITIES().size(); + mem = new Memory(size); + assertEquals(NTStatus.STATUS_SUCCESS, PowrProf.INSTANCE + .CallNtPowerInformation(POWER_INFORMATION_LEVEL.SystemPowerCapabilities, null, 0, mem, size)); + SYSTEM_POWER_CAPABILITIES powerCapabilities = new SYSTEM_POWER_CAPABILITIES(mem); + assertEquals(powerCapabilities.SystemBatteriesPresent > 0, batteryState.BatteryPresent > 0); + } + + public void testSystemPowerInformation() { + int size = new SYSTEM_POWER_INFORMATION().size(); + Memory mem = new Memory(size); + assertEquals(NTStatus.STATUS_SUCCESS, PowrProf.INSTANCE + .CallNtPowerInformation(POWER_INFORMATION_LEVEL.SystemPowerInformation, null, 0, mem, size)); + SYSTEM_POWER_INFORMATION powerInfo = new SYSTEM_POWER_INFORMATION(mem); + assertTrue(powerInfo.MaxIdlenessAllowed <= 100); + assertTrue(powerInfo.Idleness <= 100); + assertTrue(powerInfo.CoolingMode >= 0); // must be 0,1,2 + assertTrue(powerInfo.CoolingMode <= 2); + } + + public void testSystemPowerPolicy() { + int size = new SYSTEM_POWER_POLICY().size(); + Memory mem = new Memory(size); + assertEquals(NTStatus.STATUS_SUCCESS, PowrProf.INSTANCE + .CallNtPowerInformation(POWER_INFORMATION_LEVEL.SystemPowerPolicyCurrent, null, 0, mem, size)); + SYSTEM_POWER_POLICY powerPolicy = new SYSTEM_POWER_POLICY(mem); + + // Test selected elements including nested structures + assertTrue(powerPolicy.PowerButton.Action >= 0); + assertTrue(powerPolicy.PowerButton.Action <= POWER_ACTION.PowerActionDisplayOff); // Max + assertTrue(powerPolicy.MinSleep <= powerPolicy.MaxSleep); + assertTrue(powerPolicy.MaxSleep <= SYSTEM_POWER_STATE.PowerSystemMaximum); + assertTrue(powerPolicy.DischargePolicy[0].BatteryLevel <= 100); // percentage } } From fd5b5d80b39c9833752c95b51156b09d0d32359a Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Sat, 16 Feb 2019 16:23:55 -0800 Subject: [PATCH 4/5] Move structures to WinNT --- .../com/sun/jna/platform/win32/PowrProf.java | 227 +----------------- .../src/com/sun/jna/platform/win32/WinNT.java | 223 ++++++++++++++++- .../sun/jna/platform/win32/PowrProfTest.java | 13 +- 3 files changed, 234 insertions(+), 229 deletions(-) diff --git a/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java index 8ffdb39a62..120340d34d 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java @@ -26,8 +26,11 @@ import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Pointer; -import com.sun.jna.Structure; -import com.sun.jna.Structure.FieldOrder; +import com.sun.jna.platform.win32.WinNT.PROCESSOR_POWER_INFORMATION; +import com.sun.jna.platform.win32.WinNT.SYSTEM_BATTERY_STATE; +import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_CAPABILITIES; +import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_INFORMATION; +import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_POLICY; /** * Functions used with power management. @@ -35,8 +38,6 @@ public interface PowrProf extends Library { PowrProf INSTANCE = Native.load("PowrProf", PowrProf.class); - int NUM_DISCHARGE_POLICIES = 4; - /** * Enum which indicates the power information level requested from * {@link #CallNtPowerInformation()}. This value indicates the specific @@ -173,224 +174,6 @@ public interface POWER_INFORMATION_LEVEL { int SystemReserveHiberFile = 10; } - /** - * Contains information about the current state of the system battery. - */ - @FieldOrder({ "AcOnLine", "BatteryPresent", "Charging", "Discharging", "Spare1", "Tag", "MaxCapacity", - "RemainingCapacity", "Rate", "EstimatedTime", "DefaultAlert1", "DefaultAlert2" }) - class SYSTEM_BATTERY_STATE extends Structure { - public byte AcOnLine; - public byte BatteryPresent; - public byte Charging; - public byte Discharging; - public byte[] Spare1 = new byte[3]; - public byte Tag; - public int MaxCapacity; - public int RemainingCapacity; - public int Rate; - public int EstimatedTime; - public int DefaultAlert1; - public int DefaultAlert2; - - public SYSTEM_BATTERY_STATE(Pointer p) { - super(p); - read(); - } - - public SYSTEM_BATTERY_STATE() { - super(); - } - } - - /** - * Contains information about a processor. - */ - @FieldOrder({ "Number", "MaxMhz", "CurrentMhz", "MhzLimit", "MaxIdleState", "CurrentIdleState" }) - class PROCESSOR_POWER_INFORMATION extends Structure { - public int Number; - public int MaxMhz; - public int CurrentMhz; - public int MhzLimit; - public int MaxIdleState; - public int CurrentIdleState; - - public PROCESSOR_POWER_INFORMATION(Pointer p) { - super(p); - read(); - } - - public PROCESSOR_POWER_INFORMATION() { - super(); - } - } - - /** - * Contains the granularity of the battery capacity. - */ - @FieldOrder({ "Granularity", "Capacity" }) - class BATTERY_REPORTING_SCALE extends Structure { - public int Granularity; - public int Capacity; - } - - /** - * Contains information about the power capabilities of the system. - */ - @FieldOrder({ "PowerButtonPresent", "SleepButtonPresent", "LidPresent", "SystemS1", "SystemS2", "SystemS3", - "SystemS4", "SystemS5", "HiberFilePresent", "FullWake", "VideoDimPresent", "ApmPresent", "UpsPresent", - "ThermalControl", "ProcessorThrottle", "ProcessorMinThrottle", "ProcessorMaxThrottle", "FastSystemS4", - "Hiberboot", "WakeAlarmPresent", "AoAc", "DiskSpinDown", "HiberFileType", "AoAcConnectivitySupported", - "spare3", "SystemBatteriesPresent", "BatteriesAreShortTerm", "BatteryScale", "AcOnLineWake", "SoftLidWake", - "RtcWake", "MinDeviceWakeState", "DefaultLowLatencyWake" }) - class SYSTEM_POWER_CAPABILITIES extends Structure { - // Misc supported system features - public byte PowerButtonPresent; - public byte SleepButtonPresent; - public byte LidPresent; - public byte SystemS1; - public byte SystemS2; - public byte SystemS3; - public byte SystemS4; // hibernate - public byte SystemS5; // off - public byte HiberFilePresent; - public byte FullWake; - public byte VideoDimPresent; - public byte ApmPresent; - public byte UpsPresent; - - // Processors - public byte ThermalControl; - public byte ProcessorThrottle; - public byte ProcessorMinThrottle; - - // Prior to WinXP, next 5 bytes are ProcessorThrottleScale - // followed by 4 spare bytes - public byte ProcessorMaxThrottle; - public byte FastSystemS4; - public byte Hiberboot; - public byte WakeAlarmPresent; - public byte AoAc; - - // Disk - public byte DiskSpinDown; - - // HiberFile (Pre-Win10 next 2 bytes are spare) - public byte HiberFileType; - public byte AoAcConnectivitySupported; - public byte[] spare3 = new byte[6]; - - // System Battery - public byte SystemBatteriesPresent; - public byte BatteriesAreShortTerm; - public BATTERY_REPORTING_SCALE[] BatteryScale = new BATTERY_REPORTING_SCALE[3]; - - // Wake - public int /* SYSTEM_POWER_STATE */ AcOnLineWake; - public int /* SYSTEM_POWER_STATE */ SoftLidWake; - public int /* SYSTEM_POWER_STATE */ RtcWake; - public int /* SYSTEM_POWER_STATE */ MinDeviceWakeState; - public int /* SYSTEM_POWER_STATE */ DefaultLowLatencyWake; - - public SYSTEM_POWER_CAPABILITIES(Pointer p) { - super(p); - read(); - } - - public SYSTEM_POWER_CAPABILITIES() { - super(); - } - } - - /** - * Contains information about the idleness of the system. - */ - @FieldOrder({ "MaxIdlenessAllowed", "Idleness", "TimeRemaining", "CoolingMode" }) - class SYSTEM_POWER_INFORMATION extends Structure { - public int MaxIdlenessAllowed; - public int Idleness; - public int TimeRemaining; - public byte CoolingMode; - - public SYSTEM_POWER_INFORMATION(Pointer p) { - super(p); - read(); - } - - public SYSTEM_POWER_INFORMATION() { - super(); - } - } - - /** - * Contains information used to set the system power state. - */ - @FieldOrder({ "Action", "Flags", "EventCode" }) - class POWER_ACTION_POLICY extends Structure { - public int /* POWER_ACTION */ Action; - public int Flags; - public int EventCode; - } - - /** - * Contains information about system battery drain policy settings. - */ - @FieldOrder({ "Enable", "Spare", "BatteryLevel", "PowerPolicy", "MinSystemState" }) - class SYSTEM_POWER_LEVEL extends Structure { - public byte Enable; - public byte[] Spare = new byte[3]; - public int BatteryLevel; - public POWER_ACTION_POLICY PowerPolicy; - public int /* SYSTEM_POWER_STATE */ MinSystemState; - } - - /** - * Contains information about the current system power policy. - */ - @FieldOrder({ "Revision", "PowerButton", "SleepButton", "LidClose", "LidOpenWake", "Reserved", "Idle", - "IdleTimeout", "IdleSensitivity", "DynamicThrottle", "Spare2", "MinSleep", "MaxSleep", - "ReducedLatencySleep", "WinLogonFlags", "Spare3", "DozeS4Timeout", "BroadcastCapacityResolution", - "DischargePolicy", "VideoTimeout", "VideoDimDisplay", "VideoReserved", "SpindownTimeout", - "OptimizeForPower", "FanThrottleTolerance", "ForcedThrottle", "MinThrottle", "OverThrottled" }) - class SYSTEM_POWER_POLICY extends Structure { - public int Revision; - public POWER_ACTION_POLICY PowerButton; - public POWER_ACTION_POLICY SleepButton; - public POWER_ACTION_POLICY LidClose; - public int /* SYSTEM_POWER_STATE */ LidOpenWake; - public int Reserved; - public POWER_ACTION_POLICY Idle; - public int IdleTimeout; - public byte IdleSensitivity; - public byte DynamicThrottle; - public byte[] Spare2 = new byte[2]; - public int /* SYSTEM_POWER_STATE */ MinSleep; - public int /* SYSTEM_POWER_STATE */ MaxSleep; - public int /* SYSTEM_POWER_STATE */ ReducedLatencySleep; - public int WinLogonFlags; - public int Spare3; - public int DozeS4Timeout; - public int BroadcastCapacityResolution; - public SYSTEM_POWER_LEVEL[] DischargePolicy = new SYSTEM_POWER_LEVEL[NUM_DISCHARGE_POLICIES]; - public int VideoTimeout; - public byte VideoDimDisplay; - public int[] VideoReserved = new int[3]; - public int SpindownTimeout; - public byte OptimizeForPower; - public byte FanThrottleTolerance; - public byte ForcedThrottle; - public byte MinThrottle; - public POWER_ACTION_POLICY OverThrottled; - - public SYSTEM_POWER_POLICY(Pointer p) { - super(p); - read(); - } - - public SYSTEM_POWER_POLICY() { - super(); - } - } - /** * Sets or retrieves power information. *

diff --git a/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java b/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java index 399f2c798e..5c9bef85ab 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java @@ -3425,7 +3425,228 @@ public interface SYSTEM_POWER_STATE { } /** - * Indicates committed pages for which physical storage has been allocated, either in memory or in the paging file on disk. + * Contains information about the current state of the system battery. + */ + @FieldOrder({ "AcOnLine", "BatteryPresent", "Charging", "Discharging", "Spare1", "Tag", "MaxCapacity", + "RemainingCapacity", "Rate", "EstimatedTime", "DefaultAlert1", "DefaultAlert2" }) + class SYSTEM_BATTERY_STATE extends Structure { + public byte AcOnLine; + public byte BatteryPresent; + public byte Charging; + public byte Discharging; + public byte[] Spare1 = new byte[3]; + public byte Tag; + public int MaxCapacity; + public int RemainingCapacity; + public int Rate; + public int EstimatedTime; + public int DefaultAlert1; + public int DefaultAlert2; + + public SYSTEM_BATTERY_STATE(Pointer p) { + super(p); + read(); + } + + public SYSTEM_BATTERY_STATE() { + super(); + } + } + + + /** + * Contains the granularity of the battery capacity. + */ + @FieldOrder({ "Granularity", "Capacity" }) + class BATTERY_REPORTING_SCALE extends Structure { + public int Granularity; + public int Capacity; + } + + /** + * Contains information about a processor. + */ + @FieldOrder({ "Number", "MaxMhz", "CurrentMhz", "MhzLimit", "MaxIdleState", "CurrentIdleState" }) + class PROCESSOR_POWER_INFORMATION extends Structure { + public int Number; + public int MaxMhz; + public int CurrentMhz; + public int MhzLimit; + public int MaxIdleState; + public int CurrentIdleState; + + public PROCESSOR_POWER_INFORMATION(Pointer p) { + super(p); + read(); + } + + public PROCESSOR_POWER_INFORMATION() { + super(); + } + } + + /** + * Contains information about the idleness of the system. + */ + @FieldOrder({ "MaxIdlenessAllowed", "Idleness", "TimeRemaining", "CoolingMode" }) + class SYSTEM_POWER_INFORMATION extends Structure { + public int MaxIdlenessAllowed; + public int Idleness; + public int TimeRemaining; + public byte CoolingMode; + + public SYSTEM_POWER_INFORMATION(Pointer p) { + super(p); + read(); + } + + public SYSTEM_POWER_INFORMATION() { + super(); + } + } + + /** + * Contains information used to set the system power state. + */ + @FieldOrder({ "Action", "Flags", "EventCode" }) + class POWER_ACTION_POLICY extends Structure { + public int /* POWER_ACTION */ Action; + public int Flags; + public int EventCode; + } + + /** + * Contains information about system battery drain policy settings. + */ + @FieldOrder({ "Enable", "Spare", "BatteryLevel", "PowerPolicy", "MinSystemState" }) + class SYSTEM_POWER_LEVEL extends Structure { + public byte Enable; + public byte[] Spare = new byte[3]; + public int BatteryLevel; + public POWER_ACTION_POLICY PowerPolicy; + public int /* SYSTEM_POWER_STATE */ MinSystemState; + } + + int NUM_DISCHARGE_POLICIES = 4; + + /** + * Contains information about the current system power policy. + */ + @FieldOrder({ "Revision", "PowerButton", "SleepButton", "LidClose", "LidOpenWake", "Reserved", "Idle", + "IdleTimeout", "IdleSensitivity", "DynamicThrottle", "Spare2", "MinSleep", "MaxSleep", + "ReducedLatencySleep", "WinLogonFlags", "Spare3", "DozeS4Timeout", "BroadcastCapacityResolution", + "DischargePolicy", "VideoTimeout", "VideoDimDisplay", "VideoReserved", "SpindownTimeout", + "OptimizeForPower", "FanThrottleTolerance", "ForcedThrottle", "MinThrottle", "OverThrottled" }) + class SYSTEM_POWER_POLICY extends Structure { + public int Revision; + public POWER_ACTION_POLICY PowerButton; + public POWER_ACTION_POLICY SleepButton; + public POWER_ACTION_POLICY LidClose; + public int /* SYSTEM_POWER_STATE */ LidOpenWake; + public int Reserved; + public POWER_ACTION_POLICY Idle; + public int IdleTimeout; + public byte IdleSensitivity; + public byte DynamicThrottle; + public byte[] Spare2 = new byte[2]; + public int /* SYSTEM_POWER_STATE */ MinSleep; + public int /* SYSTEM_POWER_STATE */ MaxSleep; + public int /* SYSTEM_POWER_STATE */ ReducedLatencySleep; + public int WinLogonFlags; + public int Spare3; + public int DozeS4Timeout; + public int BroadcastCapacityResolution; + public SYSTEM_POWER_LEVEL[] DischargePolicy = new SYSTEM_POWER_LEVEL[NUM_DISCHARGE_POLICIES]; + public int VideoTimeout; + public byte VideoDimDisplay; + public int[] VideoReserved = new int[3]; + public int SpindownTimeout; + public byte OptimizeForPower; + public byte FanThrottleTolerance; + public byte ForcedThrottle; + public byte MinThrottle; + public POWER_ACTION_POLICY OverThrottled; + + public SYSTEM_POWER_POLICY(Pointer p) { + super(p); + read(); + } + + public SYSTEM_POWER_POLICY() { + super(); + } + } + + /** + * Contains information about the power capabilities of the system. + */ + @FieldOrder({ "PowerButtonPresent", "SleepButtonPresent", "LidPresent", "SystemS1", "SystemS2", "SystemS3", + "SystemS4", "SystemS5", "HiberFilePresent", "FullWake", "VideoDimPresent", "ApmPresent", "UpsPresent", + "ThermalControl", "ProcessorThrottle", "ProcessorMinThrottle", "ProcessorMaxThrottle", "FastSystemS4", + "Hiberboot", "WakeAlarmPresent", "AoAc", "DiskSpinDown", "HiberFileType", "AoAcConnectivitySupported", + "spare3", "SystemBatteriesPresent", "BatteriesAreShortTerm", "BatteryScale", "AcOnLineWake", "SoftLidWake", + "RtcWake", "MinDeviceWakeState", "DefaultLowLatencyWake" }) + class SYSTEM_POWER_CAPABILITIES extends Structure { + // Misc supported system features + public byte PowerButtonPresent; + public byte SleepButtonPresent; + public byte LidPresent; + public byte SystemS1; + public byte SystemS2; + public byte SystemS3; + public byte SystemS4; // hibernate + public byte SystemS5; // off + public byte HiberFilePresent; + public byte FullWake; + public byte VideoDimPresent; + public byte ApmPresent; + public byte UpsPresent; + + // Processors + public byte ThermalControl; + public byte ProcessorThrottle; + public byte ProcessorMinThrottle; + + // Prior to WinXP, next 5 bytes are ProcessorThrottleScale + // followed by 4 spare bytes + public byte ProcessorMaxThrottle; + public byte FastSystemS4; + public byte Hiberboot; + public byte WakeAlarmPresent; + public byte AoAc; + + // Disk + public byte DiskSpinDown; + + // HiberFile (Pre-Win10 next 2 bytes are spare) + public byte HiberFileType; + public byte AoAcConnectivitySupported; + public byte[] spare3 = new byte[6]; + + // System Battery + public byte SystemBatteriesPresent; + public byte BatteriesAreShortTerm; + public BATTERY_REPORTING_SCALE[] BatteryScale = new BATTERY_REPORTING_SCALE[3]; + + // Wake + public int /* SYSTEM_POWER_STATE */ AcOnLineWake; + public int /* SYSTEM_POWER_STATE */ SoftLidWake; + public int /* SYSTEM_POWER_STATE */ RtcWake; + public int /* SYSTEM_POWER_STATE */ MinDeviceWakeState; + public int /* SYSTEM_POWER_STATE */ DefaultLowLatencyWake; + + public SYSTEM_POWER_CAPABILITIES(Pointer p) { + super(p); + read(); + } + + public SYSTEM_POWER_CAPABILITIES() { + super(); + } + } + /** + * Indicates committed pages for which physical storage has been allocated, + * either in memory or in the paging file on disk. */ int MEM_COMMIT = 0x1000; diff --git a/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java b/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java index 5fa0fc9d54..6c3b6f3998 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/PowrProfTest.java @@ -25,13 +25,13 @@ import com.sun.jna.Memory; import com.sun.jna.platform.win32.PowrProf.POWER_INFORMATION_LEVEL; -import com.sun.jna.platform.win32.PowrProf.PROCESSOR_POWER_INFORMATION; -import com.sun.jna.platform.win32.PowrProf.SYSTEM_BATTERY_STATE; -import com.sun.jna.platform.win32.PowrProf.SYSTEM_POWER_CAPABILITIES; -import com.sun.jna.platform.win32.PowrProf.SYSTEM_POWER_INFORMATION; -import com.sun.jna.platform.win32.PowrProf.SYSTEM_POWER_POLICY; import com.sun.jna.platform.win32.WinBase.SYSTEM_INFO; import com.sun.jna.platform.win32.WinNT.POWER_ACTION; +import com.sun.jna.platform.win32.WinNT.PROCESSOR_POWER_INFORMATION; +import com.sun.jna.platform.win32.WinNT.SYSTEM_BATTERY_STATE; +import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_CAPABILITIES; +import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_INFORMATION; +import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_POLICY; import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_STATE; import junit.framework.TestCase; @@ -84,7 +84,8 @@ public void testSystemBatteryStateAndPowerCapabilities() { mem = new Memory(size); assertEquals(NTStatus.STATUS_SUCCESS, PowrProf.INSTANCE .CallNtPowerInformation(POWER_INFORMATION_LEVEL.SystemPowerCapabilities, null, 0, mem, size)); - SYSTEM_POWER_CAPABILITIES powerCapabilities = new SYSTEM_POWER_CAPABILITIES(mem); + SYSTEM_POWER_CAPABILITIES powerCapabilities = new SYSTEM_POWER_CAPABILITIES( + mem); assertEquals(powerCapabilities.SystemBatteriesPresent > 0, batteryState.BatteryPresent > 0); } From c5143b7d5581424ef772b5bc5e1f0cefc9a9659d Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Sat, 16 Feb 2019 16:27:44 -0800 Subject: [PATCH 5/5] Fix JavaDoc links --- CHANGES.md | 2 +- .../com/sun/jna/platform/win32/PowrProf.java | 51 +++++++++---------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 79cbf2aa68..633afe1b64 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,7 +11,7 @@ Features * [#1050](https://github.com/java-native-access/jna/pull/1050): Add `c.s.j.p.win32.VersionHelpers` and supporting functions - [@dbwiddis](https://github.com/dbwiddis). * [#1061](https://github.com/java-native-access/jna/pull/1061): replace toArray(new T[size]) with toArray(new T[0]) for better performance - [@hc-codersatlas](https://github.com/hc-codersatlas). * [#1064](https://github.com/java-native-access/jna/pull/1064): Add `c.s.j.p.win32.Kernel32.GetLogicalProcessorInformationEx` function, convenience Util method and supporting structures - [@dbwiddis](https://github.com/dbwiddis). -* [#1065](https://github.com/java-native-access/jna/pull/1065): Add `c.s.j.p.win32.PowrProf#CallNTPowerInformation` and some supporting structures - [@dbwiddis](https://github.com/dbwiddis). +* [#1065](https://github.com/java-native-access/jna/pull/1065): Add `c.s.j.p.win32.PowrProf#CallNTPowerInformation` and supporting structures - [@dbwiddis](https://github.com/dbwiddis). Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java index 120340d34d..46f6ace97e 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/PowrProf.java @@ -26,11 +26,6 @@ import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Pointer; -import com.sun.jna.platform.win32.WinNT.PROCESSOR_POWER_INFORMATION; -import com.sun.jna.platform.win32.WinNT.SYSTEM_BATTERY_STATE; -import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_CAPABILITIES; -import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_INFORMATION; -import com.sun.jna.platform.win32.WinNT.SYSTEM_POWER_POLICY; /** * Functions used with power management. @@ -69,10 +64,11 @@ public interface POWER_INFORMATION_LEVEL { * function returns {@link WinError#ERROR_INVALID_PARAMETER}. *

* The {@code lpOutputBuffer} buffer receives one - * {@link PROCESSOR_POWER_INFORMATION} structure for each processor that - * is installed on the system. Use the {@link Kernel32#GetSystemInfo()} - * function to retrieve the number of processors, which will be the - * number of logical processors on the current Processor Group. + * {@link WinNT#PROCESSOR_POWER_INFORMATION} structure for each + * processor that is installed on the system. Use the + * {@link Kernel32#GetSystemInfo()} function to retrieve the number of + * processors, which will be the number of logical processors on the + * current Processor Group. */ int ProcessorInformation = 11; @@ -81,8 +77,8 @@ public interface POWER_INFORMATION_LEVEL { * function returns {@link WinError#ERROR_INVALID_PARAMETER}. *

* The {@code lpOutputBuffer} buffer receives a - * {@link SYSTEM_BATTERY_STATE} structure containing information about - * the current system battery. + * {@link WinNT#SYSTEM_BATTERY_STATE} structure containing information + * about the current system battery. */ int SystemBatteryState = 5; @@ -105,8 +101,8 @@ public interface POWER_INFORMATION_LEVEL { * function returns {@link WinError#ERROR_INVALID_PARAMETER}. *

* The {@code lpOutputBuffer} buffer receives a - * {@link SYSTEM_POWER_CAPABILITIES} structure containing the current - * system power capabilities. + * {@link WinNT#SYSTEM_POWER_CAPABILITIES} structure containing the + * current system power capabilities. *

* This information represents the currently supported power * capabilities. It may change as drivers are installed in the system. @@ -120,7 +116,7 @@ public interface POWER_INFORMATION_LEVEL { * function returns {@link WinError#ERROR_INVALID_PARAMETER}. *

* The {@code lpOutputBuffer} buffer receives a - * {@link SYSTEM_POWER_INFORMATION} structure. + * {@link WinNT#SYSTEM_POWER_INFORMATION} structure. *

* Applications can use this level to retrieve information about the * idleness of the system. @@ -129,13 +125,14 @@ public interface POWER_INFORMATION_LEVEL { /** * If {@code lpInBuffer} is not {@code NULL}, the function applies the - * {@link SYSTEM_POWER_POLICY} values passed in {@code lpInBuffer} to - * the current system power policy used while the system is running on - * AC (utility) power. + * {@link WinNT#SYSTEM_POWER_POLICY} values passed in {@code lpInBuffer} + * to the current system power policy used while the system is running + * on AC (utility) power. *

* The {@code lpOutputBuffer} buffer receives a - * {@link SYSTEM_POWER_POLICY} structure containing the current system - * power policy used while the system is running on AC (utility) power. + * {@link WinNT#SYSTEM_POWER_POLICY} structure containing the current + * system power policy used while the system is running on AC (utility) + * power. */ int SystemPowerPolicyAc = 0; @@ -144,20 +141,22 @@ public interface POWER_INFORMATION_LEVEL { * function returns {@link WinError#ERROR_INVALID_PARAMETER}. *

* The {@code lpOutputBuffer} buffer receives a - * {@link SYSTEM_POWER_POLICY} structure containing the current system - * power policy used while the system is running on AC (utility) power. + * {@link WinNT#SYSTEM_POWER_POLICY} structure containing the current + * system power policy used while the system is running on AC (utility) + * power. */ int SystemPowerPolicyCurrent = 8; /** * If {@code lpInBuffer} is not {@code NULL}, the function applies the - * {@link SYSTEM_POWER_POLICY} values passed in {@code lpInBuffer} to - * the current system power policy used while the system is running on - * battery power. + * {@link WinNT#SYSTEM_POWER_POLICY} values passed in {@code lpInBuffer} + * to the current system power policy used while the system is running + * on battery power. *

* The {@code lpOutputBuffer} buffer receives a - * {@link SYSTEM_POWER_POLICY} structure containing the current system - * power policy used while the system is running on battery power. + * {@link WinNT#SYSTEM_POWER_POLICY} structure containing the current + * system power policy used while the system is running on battery + * power. */ int SystemPowerPolicyDc = 1;