Skip to content

Commit 646b23a

Browse files
Eran Leshemdbwiddis
Eran Leshem
authored andcommitted
Add power event constants, types, and functions
1 parent 3eee725 commit 646b23a

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Next Release (5.17.0)
77

88
Features
99
--------
10-
10+
* [#1658](https://github.com/java-native-access/jna/pull/1658): Add win32 power event constants, types, and functions - [@eranl](https://github.com/eranl).
11+
1112
Bug Fixes
1213
---------
1314

contrib/platform/src/com/sun/jna/platform/win32/WinNT.java

+2
Original file line numberDiff line numberDiff line change
@@ -4420,4 +4420,6 @@ public IO_COUNTERS(Pointer memory) {
44204420
class TOKEN_ELEVATION extends Structure {
44214421
public int TokenIsElevated;
44224422
}
4423+
4424+
Guid.GUID GUID_CONSOLE_DISPLAY_STATE = new Guid.GUID("6FE69556-704A-47A0-8F24-C28D936FDA47");
44234425
}

contrib/platform/src/com/sun/jna/platform/win32/WinUser.java

+75-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.sun.jna.Structure.FieldOrder;
3131
import com.sun.jna.Union;
3232
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR;
33-
import com.sun.jna.platform.win32.WinDef.HKL;
3433
import com.sun.jna.platform.win32.WinNT.HANDLE;
3534
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
3635
import com.sun.jna.win32.W32APITypeMapper;
@@ -2088,4 +2087,79 @@ public String toString() {
20882087
* Bitmask for the RESERVED2 key modifier.
20892088
*/
20902089
int MODIFIER_RESERVED2_MASK = 32;
2090+
2091+
class HPOWERNOTIFY extends PVOID {
2092+
public HPOWERNOTIFY() {
2093+
}
2094+
2095+
public HPOWERNOTIFY(Pointer pointer) {
2096+
super(pointer);
2097+
}
2098+
}
2099+
2100+
/**
2101+
* Registers the application to receive power setting notifications for the specific power setting event.
2102+
* @param hRecipient Handle indicating where the power setting notifications are to be sent.
2103+
* For interactive applications, the Flags parameter should be DEVICE_NOTIFY_WINDOW_HANDLE,
2104+
* and the hRecipient parameter should be a window handle.
2105+
* For services, the Flags parameter should be DEVICE_NOTIFY_SERVICE_HANDLE, and the hRecipient
2106+
* parameter should be a SERVICE_STATUS_HANDLE as returned from RegisterServiceCtrlHandlerEx.
2107+
* @param PowerSettingGuid The GUID of the power setting for which notifications are to be sent.
2108+
* @param Flags
2109+
* <li>DEVICE_NOTIFY_WINDOW_HANDLE - Notifications are sent using WM_POWERBROADCAST messages
2110+
* with a wParam parameter of PBT_POWERSETTINGCHANGE.
2111+
* <li>DEVICE_NOTIFY_SERVICE_HANDLE - Notifications are sent to the HandlerEx callback function with a dwControl
2112+
* parameter of SERVICE_CONTROL_POWEREVENT and a dwEventType of PBT_POWERSETTINGCHANGE.
2113+
* @return a notification handle for unregistering for power notifications. If the function fails, the return
2114+
* value is NULL. To get extended error information, call GetLastError.
2115+
*/
2116+
HPOWERNOTIFY RegisterPowerSettingNotification(
2117+
/*[in]*/ HANDLE hRecipient,
2118+
/*[in]*/ Guid.GUID PowerSettingGuid,
2119+
/*[in]*/ int Flags);
2120+
2121+
/**
2122+
* Unregisters the power setting notification.
2123+
* @param Handle The handle returned from the RegisterPowerSettingNotification function.
2124+
* @return If the function succeeds, the return value is nonzero.
2125+
* If the function fails, the return value is zero. To get extended error information, call GetLastError.
2126+
*/
2127+
BOOL UnregisterPowerSettingNotification(
2128+
/*[in]*/ HPOWERNOTIFY Handle
2129+
);
2130+
2131+
int WM_POWERBROADCAST = 0x0218;
2132+
2133+
int PBT_APMQUERYSUSPEND = 0x0000;
2134+
int PBT_APMQUERYSTANDBY = 0x0001;
2135+
int PBT_APMQUERYSUSPENDFAILED = 0x0002;
2136+
int PBT_APMQUERYSTANDBYFAILED = 0x0003;
2137+
int PBT_APMSUSPEND = 0x0004;
2138+
int PBT_APMSTANDBY = 0x0005;
2139+
int PBT_APMRESUMECRITICAL = 0x0006;
2140+
int PBT_APMRESUMESUSPEND = 0x0007;
2141+
int PBT_APMRESUMESTANDBY = 0x0008;
2142+
int PBT_APMBATTERYLOW = 0x0009;
2143+
int PBT_APMPOWERSTATUSCHANGE = 0x000A;
2144+
int PBT_APMOEMEVENT = 0x000B;
2145+
int PBT_APMRESUMEAUTOMATIC = 0x0012;
2146+
int PBT_POWERSETTINGCHANGE = 0x8013;
2147+
2148+
@FieldOrder({"PowerSetting", "DataLength", "Data"})
2149+
class POWERBROADCAST_SETTING extends Structure {
2150+
public Guid.GUID PowerSetting;
2151+
public int DataLength;
2152+
public byte Data[] = new byte[1];
2153+
2154+
public POWERBROADCAST_SETTING(Pointer p) {
2155+
super(p);
2156+
read();
2157+
}
2158+
2159+
@Override
2160+
public final void read() {
2161+
Data = new byte[getPointer().getInt(fieldOffset("DataLength"))];
2162+
super.read();
2163+
}
2164+
}
20912165
}

contrib/platform/test/com/sun/jna/platform/win32/User32Test.java

+8
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,12 @@ public void testToUnicodeEx() {
596596
// which is in an undefined state at test execution and may change arbitrarily
597597
// during the test.
598598
}
599+
600+
@Test
601+
public void testRegisterPowerSettingNotification() {
602+
WinUser.HPOWERNOTIFY hpowernotify = INSTANCE.RegisterPowerSettingNotification(
603+
new HWND(), WinNT.GUID_CONSOLE_DISPLAY_STATE, User32.DEVICE_NOTIFY_WINDOW_HANDLE);
604+
assertNotNull(hpowernotify);
605+
assertNotEquals(0, INSTANCE.UnregisterPowerSettingNotification(hpowernotify));
606+
}
599607
}

0 commit comments

Comments
 (0)