Support POWER events suspend(ed)/resume in wxWdgets. (PR #25778)

23 views
Skip to first unread message

Tijs Van Buggenhout

unread,
Sep 9, 2025, 11:25:53 AM (5 days ago) Sep 9
to wx-...@googlegroups.com, Subscribed

Register observers with notifications center, with callbacks for NSWorkspaceWillSleepNotification and NSWorkspaceDidWakeNotification notifications.

These callbacks inform the top level window to handle POWER SUSPENDED and/or RESUME.


You can view, comment on, or merge this pull request online at:

  https://github.com/wxWidgets/wxWidgets/pull/25778

Commit Summary

  • 722d11a Add wxCocoaPowerModule and CocoaPowerEventsObserver

File Changes

(2 files)

Patch Links:


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/25778@github.com>

VZ

unread,
Sep 9, 2025, 11:36:46 AM (5 days ago) Sep 9
to wx-...@googlegroups.com, Subscribed

@vadz commented on this pull request.

Nice, thanks for implementing this!

The documentation in interface/wx/power.h should be updated to mention that this functionality is now available under macOS too.

I also wonder if we could/should delay CocoaPowerEventsObserver construction until wxPowerResourceBlocker is instantiated: this is required to get these events under Linux and even if it isn't under macOS, it could be nice to encourage writing more portable code. What do you think?


In src/osx/cocoa/power.mm:

> @@ -105,3 +110,81 @@ bool UpdatePowerResourceUsage(wxPowerResourceKind kind, const wxString& reason)
             break;
     }
 }
+
+#import <Cocoa/Cocoa.h>
+#import "wx/power.h" // Assuming you have wxPowerEvent defined or a custom event
+
+@interface CocoaPowerEventsObserver : NSObject

This probably should be called wxCocoaPowerEventsObserver for consistency and also maybe to avoid any conflicts with application-defined identifier when using static linking (I'm not sure what are the visibility rules for Objective C interfaces, but using "wx" prefix can never hurt).


In src/osx/cocoa/power.mm:

> +namespace {
+// add power observers on init and remove on exit
+class wxCocoaPowerModule : public wxModule
+{
+wxDECLARE_DYNAMIC_CLASS(wxCocoaPowerModule);
+
+public:
+    bool OnInit() override
+    {
+         m_powerObserver = [[CocoaPowerEventsObserver alloc] init];
+         return true;
+    }
+
+    void OnExit() override
+    {
+         if (m_powerObserver != nullptr) {

Minor style pick, wx style puts braces on their own lines:

⬇️ Suggested change
-         if (m_powerObserver != nullptr) {
+         if (m_powerObserver != nullptr)
+         {


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/25778/review/3202192040@github.com>

Tijs Van Buggenhout

unread,
Sep 10, 2025, 6:38:48 AM (4 days ago) Sep 10
to wx-...@googlegroups.com, Subscribed

@tisj commented on this pull request.


In src/osx/cocoa/power.mm:

> @@ -105,3 +110,81 @@ bool UpdatePowerResourceUsage(wxPowerResourceKind kind, const wxString& reason)
             break;
     }
 }
+
+#import <Cocoa/Cocoa.h>
+#import "wx/power.h" // Assuming you have wxPowerEvent defined or a custom event
+
+@interface CocoaPowerEventsObserver : NSObject

I didn't use wx prefix here since it is a local symbol.

These are the symbols in an application binary:

00000001002fa570 t -[CocoaPowerEventsObserver dealloc]
00000001002fa6d0 t -[CocoaPowerEventsObserver handleDidWake:]
00000001002fa5e0 t -[CocoaPowerEventsObserver handleWillSleep:]
00000001002fa4b0 t -[CocoaPowerEventsObserver init]
000000010077c420 S OBJC_CLASS$_CocoaPowerEventsObserver
000000010077c448 S OBJC_METACLASS$_CocoaPowerEventsObserver
00000001007791d8 s _OBJC$_INSTANCE_METHODS_CocoaPowerEventsObserver
0000000100779240 s _OBJC_CLASS_RO$_CocoaPowerEventsObserver
0000000100779190 s _OBJC_METACLASS_RO$_CocoaPowerEventsObserver
00000001002ca30c t -[CocoaPowerEventsObserver dealloc]
00000001002ca468 t -[CocoaPowerEventsObserver handleDidWake:]
00000001002ca37c t -[CocoaPowerEventsObserver handleWillSleep:]
00000001002ca244 t -[CocoaPowerEventsObserver init]
0000000100713b98 S OBJC_CLASS$_CocoaPowerEventsObserver
0000000100713bc0 S OBJC_METACLASS$_CocoaPowerEventsObserver
00000001004cd798 s _OBJC$_INSTANCE_METHODS_CocoaPowerEventsObserver
0000000100710ab0 s _OBJC_CLASS_RO$_CocoaPowerEventsObserver
0000000100710a68 s _OBJC_METACLASS_RO$_CocoaPowerEventsObserver

However wx modules also seem local..

0000000100787be8 b __ZN12_GLOBAL__N_118wxCocoaPowerModule12ms_classInfoE
00000001002fa790 t __ZN12_GLOBAL__N_118wxCocoaPowerModule14wxCreateObjectEv
0000000100787c18 b __ZN12_GLOBAL__N_118wxCocoaPowerModule15m_powerObserverE
00000001002fa850 t __ZN12_GLOBAL__N_118wxCocoaPowerModule6OnExitEv
00000001002fa820 t __ZN12_GLOBAL__N_118wxCocoaPowerModule6OnInitEv
00000001002fa7e0 t __ZN12_GLOBAL__N_118wxCocoaPowerModuleD0Ev
00000001002fa7d0 t __ZN12_GLOBAL__N_118wxCocoaPowerModuleD1Ev
00000001002fa810 t __ZNK12_GLOBAL__N_118wxCocoaPowerModule12GetClassInfoEv
0000000100718750 s __ZTIN12_GLOBAL__N_118wxCocoaPowerModuleE
00000001005949a8 s __ZTSN12_GLOBAL__N_118wxCocoaPowerModuleE
0000000100718708 s __ZTVN12_GLOBAL__N_118wxCocoaPowerModuleE
000000010071f220 b __ZN12_GLOBAL__N_118wxCocoaPowerModule12ms_classInfoE
00000001002ca51c t __ZN12_GLOBAL__N_118wxCocoaPowerModule14wxCreateObjectEv
000000010071f250 b __ZN12_GLOBAL__N_118wxCocoaPowerModule15m_powerObserverE
00000001002ca5b0 t __ZN12_GLOBAL__N_118wxCocoaPowerModule6OnExitEv
00000001002ca57c t __ZN12_GLOBAL__N_118wxCocoaPowerModule6OnInitEv
00000001002ca558 t __ZN12_GLOBAL__N_118wxCocoaPowerModuleD0Ev
00000001002ca554 t __ZN12_GLOBAL__N_118wxCocoaPowerModuleD1Ev
00000001002ca570 t __ZNK12_GLOBAL__N_118wxCocoaPowerModule12GetClassInfoEv
00000001006b0500 s __ZTIN12_GLOBAL__N_118wxCocoaPowerModuleE
0000000100536800 s __ZTSN12_GLOBAL__N_118wxCocoaPowerModuleE
00000001006b04b8 s __ZTVN12_GLOBAL__N_118wxCocoaPowerModuleE

We actually had a problem using the power management in static builds, as the module and observer were not getting linked in automatically. Only when we used the (wxPowerResource) acquire and release, all symbols in the same file got included (and thus the module is registered with wx).


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/25778/review/3205541523@github.com>

Tijs Van Buggenhout

unread,
Sep 10, 2025, 6:40:47 AM (4 days ago) Sep 10
to wx-...@googlegroups.com, Push

@tisj pushed 1 commit.

  • bfc9ebb Add wxCocoaPowerModule and CocoaPowerEventsObserver


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/25778/before/722d11a69645fa7cf9072b8e0efe23fc6071db03/after/bfc9ebbeb4e38b0ae35d070fb2044f9274c3c1e2@github.com>

Tijs Van Buggenhout

unread,
Sep 10, 2025, 6:51:22 AM (4 days ago) Sep 10
to wx-...@googlegroups.com, Subscribed
tisj left a comment (wxWidgets/wxWidgets#25778)

If i understand it correctly the wxResourcePowerBlocker is defined in include/wx/power.h and gets instantiated when the application includes this file (directly or indirectly)?

In Linux these events are observed on the DBus system bus via login manager, but that implementation resides in src/gtk/power.cpp - and thus part of wx library.

No sure how the magic is happening here ;)


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/25778/c3274406405@github.com>

VZ

unread,
Sep 10, 2025, 8:28:31 AM (4 days ago) Sep 10
to wx-...@googlegroups.com, Subscribed

@vadz commented on this pull request.


In src/osx/cocoa/power.mm:

> @@ -105,3 +110,81 @@ bool UpdatePowerResourceUsage(wxPowerResourceKind kind, const wxString& reason)
             break;
     }
 }
+
+#import <Cocoa/Cocoa.h>
+#import "wx/power.h" // Assuming you have wxPowerEvent defined or a custom event
+
+@interface CocoaPowerEventsObserver : NSObject

Just to confirm: are you sure that if the application defines an interface called CocoaPowerEventsObserver it doesn't conflict with this one? I think Objective C uses strings for matching somewhere, so I'd really prefer to just prefer this type to have a wx prefix than to think carefully about whether it is or not a problem.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/25778/review/3205928941@github.com>

VZ

unread,
Sep 10, 2025, 8:33:08 AM (4 days ago) Sep 10
to wx-...@googlegroups.com, Subscribed
vadz left a comment (wxWidgets/wxWidgets#25778)

If i understand it correctly the wxResourcePowerBlocker is defined in include/wx/power.h and gets instantiated when the application includes this file (directly or indirectly)?

No, it's just a normal class, there is no magic involved. The application needs to create an object of this class in order to receive power events under Linux/GTK, see the example in wxPowerEvent documentation.

My thinking is that we could also only send these events in macOS implementation if/after wxPowerResourceBlocker was/is created, even if this is unnecessary under this platform. But by forcing the application to do it we would

  1. Avoid creating the observer unnecessarily (for the applications that don't care about such events).
  2. Ensure that the code using these events developed under macOS also works under Linux (instead of silently doing nothing there).

P.S. Please don't force push until and unless you're pushing the very final version of the PR, force pushing makes it difficult to see what has changed, so please use "fixup" commits as created by git commit --fixup or --squash and then force push just once at the end. TIA!


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/25778/c3274757451@github.com>

Reply all
Reply to author
Forward
0 new messages