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