PowerManager/PowerManager.cpp
author StephaneLenclud
Sat, 26 Sep 2015 00:20:31 +0200
changeset 163 0c5930689ea1
parent 160 de942d321cfb
permissions -rw-r--r--
Fixing binaries output paths.
     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     ///
    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     void SettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
    68     {
    69         iMonitorPowerOnDelegate += d;
    70         iMonitorPowerObserverCount++;
    71         //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0)
    72         if (iMonitorPowerObserverCount == 1)
    73         {
    74             //Register for monitor power notifications
    75             iMonitorPowerHandle=RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
    76         }
    77 
    78     }
    79 
    80     ///
    81     void SettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
    82     {
    83         iMonitorPowerOnDelegate -= d;
    84         iMonitorPowerObserverCount--;
    85         if (iMonitorPowerObserverCount==0)
    86         {
    87             //Unregister from corresponding power setting notification
    88             UnregisterPowerSettingNotification(iMonitorPowerHandle);
    89         }
    90     }
    91 
    92     //
    93     void SettingNotifier::OnMonitorPowerOn::raise()
    94     {        
    95         if (iMonitorPowerOnDelegate != nullptr)
    96         {
    97             iMonitorPowerOnDelegate->Invoke();
    98         }
    99     }
   100 
   101     ///
   102     void SettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
   103     {
   104         iMonitorPowerOffDelegate += d;
   105         iMonitorPowerObserverCount++;
   106         if (iMonitorPowerObserverCount == 1)
   107         {
   108             //Register for monitor power notifications
   109             iMonitorPowerHandle = RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
   110         }
   111     }
   112 
   113     ///
   114     void SettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
   115     {
   116         iMonitorPowerOffDelegate -= d;
   117         iMonitorPowerObserverCount--;
   118         if (iMonitorPowerObserverCount == 0)
   119         {
   120             //Unregister from corresponding power setting notification
   121             UnregisterPowerSettingNotification(iMonitorPowerHandle);
   122         }
   123     }
   124 
   125     //
   126     void SettingNotifier::OnMonitorPowerOff::raise()
   127     {
   128         if (iMonitorPowerOffDelegate != nullptr)
   129         {
   130             iMonitorPowerOffDelegate->Invoke();
   131         }        
   132     }
   133 
   134 
   135 
   136 }