Power Setting Notifier can now unregister from Monitor Power events.
authorStephane Lenclud
Thu, 24 Sep 2015 22:45:32 +0200
changeset 160de942d321cfb
parent 159 e7c8c2b500bd
child 161 7b19bea5d73a
Power Setting Notifier can now unregister from Monitor Power events.
PowerManager/PowerManager.cpp
PowerManager/PowerManager.h
Server/MainForm.Hid.cs
     1.1 --- a/PowerManager/PowerManager.cpp	Thu Sep 24 21:39:05 2015 +0200
     1.2 +++ b/PowerManager/PowerManager.cpp	Thu Sep 24 22:45:32 2015 +0200
     1.3 @@ -9,44 +9,38 @@
     1.4  namespace PowerManager
     1.5  {
     1.6      ///
     1.7 -    PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle, Boolean aService)
     1.8 +    SettingNotifier::SettingNotifier(IntPtr aHandle, Boolean aService)
     1.9      {
    1.10          Construct(aHandle, aService);
    1.11      }
    1.12  
    1.13      ///
    1.14 -    PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle)
    1.15 +    SettingNotifier::SettingNotifier(IntPtr aHandle)
    1.16      {
    1.17          //By default we assume we run as a Window
    1.18          Construct(aHandle, false);
    1.19      }
    1.20  
    1.21      ///
    1.22 -    void PowerSettingNotifier::Construct(IntPtr aHandle, Boolean aService)
    1.23 +    void SettingNotifier::Construct(IntPtr aHandle, Boolean aService)
    1.24      {
    1.25          iHandle = aHandle;
    1.26          iIsService = aService;
    1.27          iMonitorPowerOnDelegate = nullptr;
    1.28          iMonitorPowerOffDelegate = nullptr;
    1.29          iMonitorPowerObserverCount = 0;
    1.30 +        iMonitorPowerHandle = NULL;
    1.31      }
    1.32  
    1.33 -    ///
    1.34 -    Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle, Boolean aService)
    1.35 +    /// TODO: Make this generic by passing the HPOWERNOTIFY by reference and GUID as parameter too
    1.36 +    HPOWERNOTIFY SettingNotifier::RegisterPowerSettingNotification(LPCGUID aGuid)
    1.37  	{
    1.38 -        HANDLE handle = aHandle.ToPointer();        
    1.39 -        HPOWERNOTIFY res=::RegisterPowerSettingNotification(handle, &GUID_MONITOR_POWER_ON, (aService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE));
    1.40 -        return (res != NULL);
    1.41 -	};
    1.42 -
    1.43 -    /// 
    1.44 -    Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle)
    1.45 -    {
    1.46 -        return RegisterPowerSettingNotification(aHandle,false);
    1.47 -    };
    1.48 +        HANDLE handle = iHandle.ToPointer();
    1.49 +        return ::RegisterPowerSettingNotification(handle, aGuid, (iIsService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE));
    1.50 +	}
    1.51  
    1.52      ///
    1.53 -    void PowerSettingNotifier::WndProc(Message% aMessage)
    1.54 +    void SettingNotifier::WndProc(Message% aMessage)
    1.55      {
    1.56          POWERBROADCAST_SETTING* setting;
    1.57  
    1.58 @@ -72,32 +66,33 @@
    1.59      }
    1.60  
    1.61      ///
    1.62 -    void PowerSettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
    1.63 +    void SettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
    1.64      {
    1.65          iMonitorPowerOnDelegate += d;
    1.66          iMonitorPowerObserverCount++;
    1.67          //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0)
    1.68          if (iMonitorPowerObserverCount == 1)
    1.69          {
    1.70 -            //TODO: register
    1.71 -            RegisterPowerSettingNotification(iHandle,iIsService);
    1.72 +            //Register for monitor power notifications
    1.73 +            iMonitorPowerHandle=RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
    1.74          }
    1.75  
    1.76      }
    1.77  
    1.78      ///
    1.79 -    void PowerSettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
    1.80 +    void SettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
    1.81      {
    1.82          iMonitorPowerOnDelegate -= d;
    1.83          iMonitorPowerObserverCount--;
    1.84          if (iMonitorPowerObserverCount==0)
    1.85          {
    1.86 -            //TODO: unregister
    1.87 +            //Unregister from corresponding power setting notification
    1.88 +            UnregisterPowerSettingNotification(iMonitorPowerHandle);
    1.89          }
    1.90      }
    1.91  
    1.92      //
    1.93 -    void PowerSettingNotifier::OnMonitorPowerOn::raise()
    1.94 +    void SettingNotifier::OnMonitorPowerOn::raise()
    1.95      {        
    1.96          if (iMonitorPowerOnDelegate != nullptr)
    1.97          {
    1.98 @@ -106,30 +101,31 @@
    1.99      }
   1.100  
   1.101      ///
   1.102 -    void PowerSettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
   1.103 +    void SettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
   1.104      {
   1.105          iMonitorPowerOffDelegate += d;
   1.106          iMonitorPowerObserverCount++;
   1.107          if (iMonitorPowerObserverCount == 1)
   1.108          {
   1.109 -            //TODO: register
   1.110 -            RegisterPowerSettingNotification(iHandle, iIsService);
   1.111 +            //Register for monitor power notifications
   1.112 +            iMonitorPowerHandle = RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
   1.113          }
   1.114      }
   1.115  
   1.116      ///
   1.117 -    void PowerSettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
   1.118 +    void SettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
   1.119      {
   1.120          iMonitorPowerOffDelegate -= d;
   1.121          iMonitorPowerObserverCount--;
   1.122          if (iMonitorPowerObserverCount == 0)
   1.123          {
   1.124 -            //TODO: unregister
   1.125 +            //Unregister from corresponding power setting notification
   1.126 +            UnregisterPowerSettingNotification(iMonitorPowerHandle);
   1.127          }
   1.128      }
   1.129  
   1.130      //
   1.131 -    void PowerSettingNotifier::OnMonitorPowerOff::raise()
   1.132 +    void SettingNotifier::OnMonitorPowerOff::raise()
   1.133      {
   1.134          if (iMonitorPowerOffDelegate != nullptr)
   1.135          {
     2.1 --- a/PowerManager/PowerManager.h	Thu Sep 24 21:39:05 2015 +0200
     2.2 +++ b/PowerManager/PowerManager.h	Thu Sep 24 22:45:32 2015 +0200
     2.3 @@ -9,11 +9,12 @@
     2.4  {
     2.5      public delegate void PowerManagerDelegate();
     2.6  
     2.7 -	public ref class PowerSettingNotifier
     2.8 +	public ref class SettingNotifier
     2.9  	{
    2.10      public:
    2.11 -        PowerSettingNotifier(IntPtr aHandle, Boolean aService);
    2.12 -        PowerSettingNotifier(IntPtr aHandle);
    2.13 +        //Constructors
    2.14 +        SettingNotifier(IntPtr aHandle, Boolean aService);
    2.15 +        SettingNotifier(IntPtr aHandle);
    2.16          //
    2.17          void WndProc(Message% aMessage);
    2.18  
    2.19 @@ -36,8 +37,7 @@
    2.20      private:
    2.21          void Construct(IntPtr aHandle, Boolean aService);
    2.22          //
    2.23 -        Boolean RegisterPowerSettingNotification(IntPtr aHandle, Boolean aService);
    2.24 -        Boolean RegisterPowerSettingNotification(IntPtr aHandle);
    2.25 +        HPOWERNOTIFY RegisterPowerSettingNotification(LPCGUID aGuid);
    2.26  
    2.27      private:
    2.28          PowerManagerDelegate^ iMonitorPowerOnDelegate;
    2.29 @@ -51,5 +51,6 @@
    2.30          Boolean iIsService;
    2.31          ///
    2.32          int iMonitorPowerObserverCount;
    2.33 +        HPOWERNOTIFY iMonitorPowerHandle;
    2.34  	};
    2.35  }
     3.1 --- a/Server/MainForm.Hid.cs	Thu Sep 24 21:39:05 2015 +0200
     3.2 +++ b/Server/MainForm.Hid.cs	Thu Sep 24 22:45:32 2015 +0200
     3.3 @@ -32,7 +32,7 @@
     3.4          private Hid.Handler iHidHandler;
     3.5  
     3.6          ///
     3.7 -        private PowerManager.PowerSettingNotifier iPowerSettingNotifier;
     3.8 +        private PowerManager.SettingNotifier iPowerSettingNotifier;
     3.9  
    3.10          /// <summary>
    3.11          /// Register HID devices so that we receive corresponding WM_INPUT messages.
    3.12 @@ -96,7 +96,7 @@
    3.13              iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
    3.14  
    3.15              //TODO: Move this some place else
    3.16 -            iPowerSettingNotifier = new PowerManager.PowerSettingNotifier(Handle);
    3.17 +            iPowerSettingNotifier = new PowerManager.SettingNotifier(Handle);
    3.18              iPowerSettingNotifier.OnMonitorPowerOn += MonitorPowerOn;
    3.19              iPowerSettingNotifier.OnMonitorPowerOff += MonitorPowerOff;
    3.20          }