PowerManager/PowerManager.cpp
author Stephane Lenclud
Thu, 24 Sep 2015 22:45:32 +0200
changeset 160 de942d321cfb
parent 159 e7c8c2b500bd
child 162 15aa52fe5795
permissions -rw-r--r--
Power Setting Notifier can now unregister from Monitor Power events.
     1 // This is the main DLL file.
     2 
     3 #include "stdafx.h"
     4 
     5 #include "PowerManager.h"
     6 
     7 using namespace System::Diagnostics;
     8 
     9 namespace PowerManager
    10 {
    11     ///
    12     SettingNotifier::SettingNotifier(IntPtr aHandle, Boolean aService)
    13     {
    14         Construct(aHandle, aService);
    15     }
    16 
    17     ///
    18     SettingNotifier::SettingNotifier(IntPtr aHandle)
    19     {
    20         //By default we assume we run as a Window
    21         Construct(aHandle, false);
    22     }
    23 
    24     ///
    25     void SettingNotifier::Construct(IntPtr aHandle, Boolean aService)
    26     {
    27         iHandle = aHandle;
    28         iIsService = aService;
    29         iMonitorPowerOnDelegate = nullptr;
    30         iMonitorPowerOffDelegate = nullptr;
    31         iMonitorPowerObserverCount = 0;
    32         iMonitorPowerHandle = NULL;
    33     }
    34 
    35     /// TODO: Make this generic by passing the HPOWERNOTIFY by reference and GUID as parameter too
    36     HPOWERNOTIFY SettingNotifier::RegisterPowerSettingNotification(LPCGUID aGuid)
    37 	{
    38         HANDLE handle = iHandle.ToPointer();
    39         return ::RegisterPowerSettingNotification(handle, aGuid, (iIsService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE));
    40 	}
    41 
    42     ///
    43     void SettingNotifier::WndProc(Message% aMessage)
    44     {
    45         POWERBROADCAST_SETTING* setting;
    46 
    47         if (aMessage.Msg == WM_POWERBROADCAST && aMessage.WParam.ToInt32() == PBT_POWERSETTINGCHANGE)
    48         {
    49             setting=(POWERBROADCAST_SETTING*)aMessage.LParam.ToPointer();
    50             if (setting->PowerSetting == GUID_MONITOR_POWER_ON)
    51             {
    52                 if (setting->Data[0] == 0x0)
    53                 {
    54                     Debug::WriteLine(L"POWERBROADCAST: Monitor Power Off");
    55                     OnMonitorPowerOff();
    56                 }
    57                 else if (setting->Data[0] == 0x1)
    58                 {
    59                     Debug::WriteLine(L"POWERBROADCAST: Monitor Power On");
    60                     OnMonitorPowerOn();
    61                 }
    62             }
    63         }
    64 
    65 
    66     }
    67 
    68     ///
    69     void SettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
    70     {
    71         iMonitorPowerOnDelegate += d;
    72         iMonitorPowerObserverCount++;
    73         //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0)
    74         if (iMonitorPowerObserverCount == 1)
    75         {
    76             //Register for monitor power notifications
    77             iMonitorPowerHandle=RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
    78         }
    79 
    80     }
    81 
    82     ///
    83     void SettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
    84     {
    85         iMonitorPowerOnDelegate -= d;
    86         iMonitorPowerObserverCount--;
    87         if (iMonitorPowerObserverCount==0)
    88         {
    89             //Unregister from corresponding power setting notification
    90             UnregisterPowerSettingNotification(iMonitorPowerHandle);
    91         }
    92     }
    93 
    94     //
    95     void SettingNotifier::OnMonitorPowerOn::raise()
    96     {        
    97         if (iMonitorPowerOnDelegate != nullptr)
    98         {
    99             iMonitorPowerOnDelegate->Invoke();
   100         }
   101     }
   102 
   103     ///
   104     void SettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
   105     {
   106         iMonitorPowerOffDelegate += d;
   107         iMonitorPowerObserverCount++;
   108         if (iMonitorPowerObserverCount == 1)
   109         {
   110             //Register for monitor power notifications
   111             iMonitorPowerHandle = RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
   112         }
   113     }
   114 
   115     ///
   116     void SettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
   117     {
   118         iMonitorPowerOffDelegate -= d;
   119         iMonitorPowerObserverCount--;
   120         if (iMonitorPowerObserverCount == 0)
   121         {
   122             //Unregister from corresponding power setting notification
   123             UnregisterPowerSettingNotification(iMonitorPowerHandle);
   124         }
   125     }
   126 
   127     //
   128     void SettingNotifier::OnMonitorPowerOff::raise()
   129     {
   130         if (iMonitorPowerOffDelegate != nullptr)
   131         {
   132             iMonitorPowerOffDelegate->Invoke();
   133         }        
   134     }
   135 
   136 
   137 
   138 }