Building a lightweight application that functions perfectly across multiple operating systems is no small feat. In this article, we take a deep dive into the technical mechanics of the Charger Removal Alarm and how it integrates with native power state APIs on Windows, macOS, and Linux.
The Architecture: Native Bindings vs Polling
Many power-monitoring tools rely on constant polling—checking the battery status every few seconds. This is bad for battery life and CPU usage. Instead, Charger Removal Alarm uses a push-based architecture. We register callback functions directly with the kernel-level power APIs of each operating system so that we are only woken up when a change actually occurs.
1. Windows Integration (Win32 API)
On Windows, we register for power notifications using the Win32 API. Specifically, we call RegisterPowerSettingNotification with the target GUID set to GUID_ACDC_POWER_SOURCE.
When the user unplugs their AC adapter, Windows broadcasts a WM_POWERBROADCAST message to our hidden window handle, passing the parameter PBT_POWERSETTINGCHANGE. Our app intercepts this message instantly, triggering the alarm logic.
2. macOS Integration (IOKit & IOPowerSources)
Under macOS, the application hooks into the Apple IOKit framework. We register a notification port using IONotificationPortRef and hook into the power source notification loop.
We utilize the IOPSCreatePowerSourceRunLoopSource system API. This provides a run-loop callback whenever the system switch is made between "AC Power" and "Battery Power". Since this runs on Apple's native CoreFoundation run-loop, resource usage is practically zero.
3. Linux Integration (D-Bus & UPower)
On Linux, our service communicates via the system D-Bus. We listen for properties changes on the UPower service, which monitors power devices:
By subscribing to the PropertiesChanged signal, we receive an event the split-second the battery state flips to discharging. This supports all modern desktop environments, including GNOME, KDE Plasma, and XFCE.
Ready to protect your charger?
Join thousands of users who never forget their chargers anymore
Download Now