// Copyright (C) 2025, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #nullable enable using System; using System.Threading; using Duplicati.Library.Interface; using Vanara.PInvoke; using static Vanara.PInvoke.User32; namespace Duplicati.Library.WindowsModules; /// /// Implementation of a power mode provider using a hidden window to receive power broadcast messages /// class PowerManagementModule : IPowerModeProvider { /// /// The message loop thread /// private readonly Thread _messageThread; /// /// Event to signal that initialization is complete /// private readonly ManualResetEvent _init = new(false); /// /// The window handle for the hidden window /// private HWND _hwnd; /// /// Event that is triggered when the system is resuming from suspend /// public Action? OnResume { get; set; } /// /// Event that is triggered when the system is suspending /// public Action? OnSuspend { get; set; } /// /// Initializes a new instance of the class. /// public PowerManagementModule() { _messageThread = new Thread(MessageLoop) { IsBackground = true }; _messageThread.Start(); _init.WaitOne(); } /// /// The message loop for the hidden window /// private void MessageLoop() { _hwnd = CreateWindowEx( 0, "STATIC", "PowerMonitorWnd", WindowStyles.WS_OVERLAPPED, 0, 0, 0, 0, HWND.NULL, HMENU.NULL, HINSTANCE.NULL, IntPtr.Zero); _init.Set(); MSG msg; while (GetMessage(out msg, HWND.NULL, 0, 0) > 0) { if (msg.message == (uint)WindowMessage.WM_POWERBROADCAST) { switch ((PowerBroadcastType)msg.wParam) { case PowerBroadcastType.PBT_APMSUSPEND: OnSuspend?.Invoke(); break; case PowerBroadcastType.PBT_APMRESUMEAUTOMATIC: case PowerBroadcastType.PBT_APMRESUMESUSPEND: OnResume?.Invoke(); break; } } TranslateMessage(in msg); DispatchMessage(in msg); } } /// /// Disposes the power mode provider and stops listening for events /// public void Dispose() { if (_hwnd != HWND.NULL) { PostMessage(_hwnd, (uint)WindowMessage.WM_CLOSE, IntPtr.Zero, IntPtr.Zero); _messageThread.Join(); } } }