# HG changeset patch # User Stephane Lenclud # Date 1443127532 -7200 # Node ID de942d321cfb8ea515367e6f453286ce23169257 # Parent e7c8c2b500bdc74c767e2aa0dd44894604373903 Power Setting Notifier can now unregister from Monitor Power events. diff -r e7c8c2b500bd -r de942d321cfb PowerManager/PowerManager.cpp --- a/PowerManager/PowerManager.cpp Thu Sep 24 21:39:05 2015 +0200 +++ b/PowerManager/PowerManager.cpp Thu Sep 24 22:45:32 2015 +0200 @@ -9,44 +9,38 @@ namespace PowerManager { /// - PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle, Boolean aService) + SettingNotifier::SettingNotifier(IntPtr aHandle, Boolean aService) { Construct(aHandle, aService); } /// - PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle) + SettingNotifier::SettingNotifier(IntPtr aHandle) { //By default we assume we run as a Window Construct(aHandle, false); } /// - void PowerSettingNotifier::Construct(IntPtr aHandle, Boolean aService) + void SettingNotifier::Construct(IntPtr aHandle, Boolean aService) { iHandle = aHandle; iIsService = aService; iMonitorPowerOnDelegate = nullptr; iMonitorPowerOffDelegate = nullptr; iMonitorPowerObserverCount = 0; + iMonitorPowerHandle = NULL; } - /// - Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle, Boolean aService) + /// TODO: Make this generic by passing the HPOWERNOTIFY by reference and GUID as parameter too + HPOWERNOTIFY SettingNotifier::RegisterPowerSettingNotification(LPCGUID aGuid) { - HANDLE handle = aHandle.ToPointer(); - HPOWERNOTIFY res=::RegisterPowerSettingNotification(handle, &GUID_MONITOR_POWER_ON, (aService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE)); - return (res != NULL); - }; - - /// - Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle) - { - return RegisterPowerSettingNotification(aHandle,false); - }; + HANDLE handle = iHandle.ToPointer(); + return ::RegisterPowerSettingNotification(handle, aGuid, (iIsService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE)); + } /// - void PowerSettingNotifier::WndProc(Message% aMessage) + void SettingNotifier::WndProc(Message% aMessage) { POWERBROADCAST_SETTING* setting; @@ -72,32 +66,33 @@ } /// - void PowerSettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d) + void SettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d) { iMonitorPowerOnDelegate += d; iMonitorPowerObserverCount++; //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0) if (iMonitorPowerObserverCount == 1) { - //TODO: register - RegisterPowerSettingNotification(iHandle,iIsService); + //Register for monitor power notifications + iMonitorPowerHandle=RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON); } } /// - void PowerSettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d) + void SettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d) { iMonitorPowerOnDelegate -= d; iMonitorPowerObserverCount--; if (iMonitorPowerObserverCount==0) { - //TODO: unregister + //Unregister from corresponding power setting notification + UnregisterPowerSettingNotification(iMonitorPowerHandle); } } // - void PowerSettingNotifier::OnMonitorPowerOn::raise() + void SettingNotifier::OnMonitorPowerOn::raise() { if (iMonitorPowerOnDelegate != nullptr) { @@ -106,30 +101,31 @@ } /// - void PowerSettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d) + void SettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d) { iMonitorPowerOffDelegate += d; iMonitorPowerObserverCount++; if (iMonitorPowerObserverCount == 1) { - //TODO: register - RegisterPowerSettingNotification(iHandle, iIsService); + //Register for monitor power notifications + iMonitorPowerHandle = RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON); } } /// - void PowerSettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d) + void SettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d) { iMonitorPowerOffDelegate -= d; iMonitorPowerObserverCount--; if (iMonitorPowerObserverCount == 0) { - //TODO: unregister + //Unregister from corresponding power setting notification + UnregisterPowerSettingNotification(iMonitorPowerHandle); } } // - void PowerSettingNotifier::OnMonitorPowerOff::raise() + void SettingNotifier::OnMonitorPowerOff::raise() { if (iMonitorPowerOffDelegate != nullptr) { diff -r e7c8c2b500bd -r de942d321cfb PowerManager/PowerManager.h --- a/PowerManager/PowerManager.h Thu Sep 24 21:39:05 2015 +0200 +++ b/PowerManager/PowerManager.h Thu Sep 24 22:45:32 2015 +0200 @@ -9,11 +9,12 @@ { public delegate void PowerManagerDelegate(); - public ref class PowerSettingNotifier + public ref class SettingNotifier { public: - PowerSettingNotifier(IntPtr aHandle, Boolean aService); - PowerSettingNotifier(IntPtr aHandle); + //Constructors + SettingNotifier(IntPtr aHandle, Boolean aService); + SettingNotifier(IntPtr aHandle); // void WndProc(Message% aMessage); @@ -36,8 +37,7 @@ private: void Construct(IntPtr aHandle, Boolean aService); // - Boolean RegisterPowerSettingNotification(IntPtr aHandle, Boolean aService); - Boolean RegisterPowerSettingNotification(IntPtr aHandle); + HPOWERNOTIFY RegisterPowerSettingNotification(LPCGUID aGuid); private: PowerManagerDelegate^ iMonitorPowerOnDelegate; @@ -51,5 +51,6 @@ Boolean iIsService; /// int iMonitorPowerObserverCount; + HPOWERNOTIFY iMonitorPowerHandle; }; } diff -r e7c8c2b500bd -r de942d321cfb Server/MainForm.Hid.cs --- a/Server/MainForm.Hid.cs Thu Sep 24 21:39:05 2015 +0200 +++ b/Server/MainForm.Hid.cs Thu Sep 24 22:45:32 2015 +0200 @@ -32,7 +32,7 @@ private Hid.Handler iHidHandler; /// - private PowerManager.PowerSettingNotifier iPowerSettingNotifier; + private PowerManager.SettingNotifier iPowerSettingNotifier; /// /// Register HID devices so that we receive corresponding WM_INPUT messages. @@ -96,7 +96,7 @@ iHidHandler.OnHidEvent += HandleHidEventThreadSafe; //TODO: Move this some place else - iPowerSettingNotifier = new PowerManager.PowerSettingNotifier(Handle); + iPowerSettingNotifier = new PowerManager.SettingNotifier(Handle); iPowerSettingNotifier.OnMonitorPowerOn += MonitorPowerOn; iPowerSettingNotifier.OnMonitorPowerOff += MonitorPowerOff; }