PowerManager/PowerManager.cpp
author StephaneLenclud
Sun, 08 Jan 2017 12:19:35 +0100
changeset 278 2481c46d1f93
parent 160 de942d321cfb
permissions -rw-r--r--
Reading audio output is stopped when audio visualizers are not in used.
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
StephaneLenclud@162
    35
    ///
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
    ///
Stephane@160
    67
    void SettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
StephaneLenclud@159
    68
    {
StephaneLenclud@159
    69
        iMonitorPowerOnDelegate += d;
StephaneLenclud@159
    70
        iMonitorPowerObserverCount++;
StephaneLenclud@159
    71
        //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0)
StephaneLenclud@159
    72
        if (iMonitorPowerObserverCount == 1)
StephaneLenclud@159
    73
        {
Stephane@160
    74
            //Register for monitor power notifications
Stephane@160
    75
            iMonitorPowerHandle=RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
StephaneLenclud@159
    76
        }
StephaneLenclud@159
    77
StephaneLenclud@159
    78
    }
StephaneLenclud@159
    79
StephaneLenclud@159
    80
    ///
Stephane@160
    81
    void SettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
StephaneLenclud@159
    82
    {
StephaneLenclud@159
    83
        iMonitorPowerOnDelegate -= d;
StephaneLenclud@159
    84
        iMonitorPowerObserverCount--;
StephaneLenclud@159
    85
        if (iMonitorPowerObserverCount==0)
StephaneLenclud@159
    86
        {
Stephane@160
    87
            //Unregister from corresponding power setting notification
Stephane@160
    88
            UnregisterPowerSettingNotification(iMonitorPowerHandle);
StephaneLenclud@159
    89
        }
StephaneLenclud@159
    90
    }
StephaneLenclud@159
    91
StephaneLenclud@159
    92
    //
Stephane@160
    93
    void SettingNotifier::OnMonitorPowerOn::raise()
StephaneLenclud@159
    94
    {        
StephaneLenclud@159
    95
        if (iMonitorPowerOnDelegate != nullptr)
StephaneLenclud@159
    96
        {
StephaneLenclud@159
    97
            iMonitorPowerOnDelegate->Invoke();
StephaneLenclud@159
    98
        }
StephaneLenclud@159
    99
    }
StephaneLenclud@159
   100
StephaneLenclud@159
   101
    ///
Stephane@160
   102
    void SettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
StephaneLenclud@159
   103
    {
StephaneLenclud@159
   104
        iMonitorPowerOffDelegate += d;
StephaneLenclud@159
   105
        iMonitorPowerObserverCount++;
StephaneLenclud@159
   106
        if (iMonitorPowerObserverCount == 1)
StephaneLenclud@159
   107
        {
Stephane@160
   108
            //Register for monitor power notifications
Stephane@160
   109
            iMonitorPowerHandle = RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
StephaneLenclud@159
   110
        }
StephaneLenclud@159
   111
    }
StephaneLenclud@159
   112
StephaneLenclud@159
   113
    ///
Stephane@160
   114
    void SettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
StephaneLenclud@159
   115
    {
StephaneLenclud@159
   116
        iMonitorPowerOffDelegate -= d;
StephaneLenclud@159
   117
        iMonitorPowerObserverCount--;
StephaneLenclud@159
   118
        if (iMonitorPowerObserverCount == 0)
StephaneLenclud@159
   119
        {
Stephane@160
   120
            //Unregister from corresponding power setting notification
Stephane@160
   121
            UnregisterPowerSettingNotification(iMonitorPowerHandle);
StephaneLenclud@159
   122
        }
StephaneLenclud@159
   123
    }
StephaneLenclud@159
   124
StephaneLenclud@159
   125
    //
Stephane@160
   126
    void SettingNotifier::OnMonitorPowerOff::raise()
StephaneLenclud@159
   127
    {
StephaneLenclud@159
   128
        if (iMonitorPowerOffDelegate != nullptr)
StephaneLenclud@159
   129
        {
StephaneLenclud@159
   130
            iMonitorPowerOffDelegate->Invoke();
StephaneLenclud@159
   131
        }        
StephaneLenclud@159
   132
    }
StephaneLenclud@159
   133
StephaneLenclud@159
   134
StephaneLenclud@159
   135
StephaneLenclud@159
   136
}