1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/PowerManager/PowerManager.cpp Thu Sep 24 21:39:05 2015 +0200
1.3 @@ -0,0 +1,142 @@
1.4 +// This is the main DLL file.
1.5 +
1.6 +#include "stdafx.h"
1.7 +
1.8 +#include "PowerManager.h"
1.9 +
1.10 +using namespace System::Diagnostics;
1.11 +
1.12 +namespace PowerManager
1.13 +{
1.14 + ///
1.15 + PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle, Boolean aService)
1.16 + {
1.17 + Construct(aHandle, aService);
1.18 + }
1.19 +
1.20 + ///
1.21 + PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle)
1.22 + {
1.23 + //By default we assume we run as a Window
1.24 + Construct(aHandle, false);
1.25 + }
1.26 +
1.27 + ///
1.28 + void PowerSettingNotifier::Construct(IntPtr aHandle, Boolean aService)
1.29 + {
1.30 + iHandle = aHandle;
1.31 + iIsService = aService;
1.32 + iMonitorPowerOnDelegate = nullptr;
1.33 + iMonitorPowerOffDelegate = nullptr;
1.34 + iMonitorPowerObserverCount = 0;
1.35 + }
1.36 +
1.37 + ///
1.38 + Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle, Boolean aService)
1.39 + {
1.40 + HANDLE handle = aHandle.ToPointer();
1.41 + HPOWERNOTIFY res=::RegisterPowerSettingNotification(handle, &GUID_MONITOR_POWER_ON, (aService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE));
1.42 + return (res != NULL);
1.43 + };
1.44 +
1.45 + ///
1.46 + Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle)
1.47 + {
1.48 + return RegisterPowerSettingNotification(aHandle,false);
1.49 + };
1.50 +
1.51 + ///
1.52 + void PowerSettingNotifier::WndProc(Message% aMessage)
1.53 + {
1.54 + POWERBROADCAST_SETTING* setting;
1.55 +
1.56 + if (aMessage.Msg == WM_POWERBROADCAST && aMessage.WParam.ToInt32() == PBT_POWERSETTINGCHANGE)
1.57 + {
1.58 + setting=(POWERBROADCAST_SETTING*)aMessage.LParam.ToPointer();
1.59 + if (setting->PowerSetting == GUID_MONITOR_POWER_ON)
1.60 + {
1.61 + if (setting->Data[0] == 0x0)
1.62 + {
1.63 + Debug::WriteLine(L"POWERBROADCAST: Monitor Power Off");
1.64 + OnMonitorPowerOff();
1.65 + }
1.66 + else if (setting->Data[0] == 0x1)
1.67 + {
1.68 + Debug::WriteLine(L"POWERBROADCAST: Monitor Power On");
1.69 + OnMonitorPowerOn();
1.70 + }
1.71 + }
1.72 + }
1.73 +
1.74 +
1.75 + }
1.76 +
1.77 + ///
1.78 + void PowerSettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
1.79 + {
1.80 + iMonitorPowerOnDelegate += d;
1.81 + iMonitorPowerObserverCount++;
1.82 + //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0)
1.83 + if (iMonitorPowerObserverCount == 1)
1.84 + {
1.85 + //TODO: register
1.86 + RegisterPowerSettingNotification(iHandle,iIsService);
1.87 + }
1.88 +
1.89 + }
1.90 +
1.91 + ///
1.92 + void PowerSettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
1.93 + {
1.94 + iMonitorPowerOnDelegate -= d;
1.95 + iMonitorPowerObserverCount--;
1.96 + if (iMonitorPowerObserverCount==0)
1.97 + {
1.98 + //TODO: unregister
1.99 + }
1.100 + }
1.101 +
1.102 + //
1.103 + void PowerSettingNotifier::OnMonitorPowerOn::raise()
1.104 + {
1.105 + if (iMonitorPowerOnDelegate != nullptr)
1.106 + {
1.107 + iMonitorPowerOnDelegate->Invoke();
1.108 + }
1.109 + }
1.110 +
1.111 + ///
1.112 + void PowerSettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
1.113 + {
1.114 + iMonitorPowerOffDelegate += d;
1.115 + iMonitorPowerObserverCount++;
1.116 + if (iMonitorPowerObserverCount == 1)
1.117 + {
1.118 + //TODO: register
1.119 + RegisterPowerSettingNotification(iHandle, iIsService);
1.120 + }
1.121 + }
1.122 +
1.123 + ///
1.124 + void PowerSettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
1.125 + {
1.126 + iMonitorPowerOffDelegate -= d;
1.127 + iMonitorPowerObserverCount--;
1.128 + if (iMonitorPowerObserverCount == 0)
1.129 + {
1.130 + //TODO: unregister
1.131 + }
1.132 + }
1.133 +
1.134 + //
1.135 + void PowerSettingNotifier::OnMonitorPowerOff::raise()
1.136 + {
1.137 + if (iMonitorPowerOffDelegate != nullptr)
1.138 + {
1.139 + iMonitorPowerOffDelegate->Invoke();
1.140 + }
1.141 + }
1.142 +
1.143 +
1.144 +
1.145 +}
1.146 \ No newline at end of file