diff --git a/CHANGES.md b/CHANGES.md index 02f9e1ac6..247f927a5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,7 +7,8 @@ Next Release (5.17.0) Features -------- - +* [#1658](https://github.com/java-native-access/jna/pull/1658): Add win32 power event constants, types, and functions - [@eranl](https://github.com/eranl). + Bug Fixes --------- 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 29e539f50..22e5a7236 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java @@ -4420,4 +4420,6 @@ public IO_COUNTERS(Pointer memory) { class TOKEN_ELEVATION extends Structure { public int TokenIsElevated; } + + Guid.GUID GUID_CONSOLE_DISPLAY_STATE = new Guid.GUID("6FE69556-704A-47A0-8F24-C28D936FDA47"); } diff --git a/contrib/platform/src/com/sun/jna/platform/win32/WinUser.java b/contrib/platform/src/com/sun/jna/platform/win32/WinUser.java index 9728a36f9..d24523d37 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/WinUser.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/WinUser.java @@ -30,7 +30,6 @@ import com.sun.jna.Structure.FieldOrder; import com.sun.jna.Union; import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR; -import com.sun.jna.platform.win32.WinDef.HKL; import com.sun.jna.platform.win32.WinNT.HANDLE; import com.sun.jna.win32.StdCallLibrary.StdCallCallback; import com.sun.jna.win32.W32APITypeMapper; @@ -2088,4 +2087,79 @@ public String toString() { * Bitmask for the RESERVED2 key modifier. */ int MODIFIER_RESERVED2_MASK = 32; + + class HPOWERNOTIFY extends PVOID { + public HPOWERNOTIFY() { + } + + public HPOWERNOTIFY(Pointer pointer) { + super(pointer); + } + } + + /** + * Registers the application to receive power setting notifications for the specific power setting event. + * @param hRecipient Handle indicating where the power setting notifications are to be sent. + * For interactive applications, the Flags parameter should be DEVICE_NOTIFY_WINDOW_HANDLE, + * and the hRecipient parameter should be a window handle. + * For services, the Flags parameter should be DEVICE_NOTIFY_SERVICE_HANDLE, and the hRecipient + * parameter should be a SERVICE_STATUS_HANDLE as returned from RegisterServiceCtrlHandlerEx. + * @param PowerSettingGuid The GUID of the power setting for which notifications are to be sent. + * @param Flags + *
  • DEVICE_NOTIFY_WINDOW_HANDLE - Notifications are sent using WM_POWERBROADCAST messages + * with a wParam parameter of PBT_POWERSETTINGCHANGE. + *
  • DEVICE_NOTIFY_SERVICE_HANDLE - Notifications are sent to the HandlerEx callback function with a dwControl + * parameter of SERVICE_CONTROL_POWEREVENT and a dwEventType of PBT_POWERSETTINGCHANGE. + * @return a notification handle for unregistering for power notifications. If the function fails, the return + * value is NULL. To get extended error information, call GetLastError. + */ + HPOWERNOTIFY RegisterPowerSettingNotification( + /*[in]*/ HANDLE hRecipient, + /*[in]*/ Guid.GUID PowerSettingGuid, + /*[in]*/ int Flags); + + /** + * Unregisters the power setting notification. + * @param Handle The handle returned from the RegisterPowerSettingNotification function. + * @return If the function succeeds, the return value is nonzero. + * If the function fails, the return value is zero. To get extended error information, call GetLastError. + */ + BOOL UnregisterPowerSettingNotification( + /*[in]*/ HPOWERNOTIFY Handle + ); + + int WM_POWERBROADCAST = 0x0218; + + int PBT_APMQUERYSUSPEND = 0x0000; + int PBT_APMQUERYSTANDBY = 0x0001; + int PBT_APMQUERYSUSPENDFAILED = 0x0002; + int PBT_APMQUERYSTANDBYFAILED = 0x0003; + int PBT_APMSUSPEND = 0x0004; + int PBT_APMSTANDBY = 0x0005; + int PBT_APMRESUMECRITICAL = 0x0006; + int PBT_APMRESUMESUSPEND = 0x0007; + int PBT_APMRESUMESTANDBY = 0x0008; + int PBT_APMBATTERYLOW = 0x0009; + int PBT_APMPOWERSTATUSCHANGE = 0x000A; + int PBT_APMOEMEVENT = 0x000B; + int PBT_APMRESUMEAUTOMATIC = 0x0012; + int PBT_POWERSETTINGCHANGE = 0x8013; + + @FieldOrder({"PowerSetting", "DataLength", "Data"}) + class POWERBROADCAST_SETTING extends Structure { + public Guid.GUID PowerSetting; + public int DataLength; + public byte Data[] = new byte[1]; + + public POWERBROADCAST_SETTING(Pointer p) { + super(p); + read(); + } + + @Override + public final void read() { + Data = new byte[getPointer().getInt(fieldOffset("DataLength"))]; + super.read(); + } + } } \ No newline at end of file diff --git a/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java b/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java index 7cb6708ee..38f82beaa 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/User32Test.java @@ -596,4 +596,12 @@ public void testToUnicodeEx() { // which is in an undefined state at test execution and may change arbitrarily // during the test. } + + @Test + public void testRegisterPowerSettingNotification() { + WinUser.HPOWERNOTIFY hpowernotify = INSTANCE.RegisterPowerSettingNotification( + new HWND(), WinNT.GUID_CONSOLE_DISPLAY_STATE, User32.DEVICE_NOTIFY_WINDOW_HANDLE); + assertNotNull(hpowernotify); + assertNotEquals(0, INSTANCE.UnregisterPowerSettingNotification(hpowernotify)); + } }