StephaneLenclud@159: // This is the main DLL file. StephaneLenclud@159: StephaneLenclud@159: #include "stdafx.h" StephaneLenclud@159: StephaneLenclud@159: #include "PowerManager.h" StephaneLenclud@159: StephaneLenclud@159: using namespace System::Diagnostics; StephaneLenclud@159: StephaneLenclud@159: namespace PowerManager StephaneLenclud@159: { StephaneLenclud@159: /// Stephane@160: SettingNotifier::SettingNotifier(IntPtr aHandle, Boolean aService) StephaneLenclud@159: { StephaneLenclud@159: Construct(aHandle, aService); StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// Stephane@160: SettingNotifier::SettingNotifier(IntPtr aHandle) StephaneLenclud@159: { StephaneLenclud@159: //By default we assume we run as a Window StephaneLenclud@159: Construct(aHandle, false); StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// Stephane@160: void SettingNotifier::Construct(IntPtr aHandle, Boolean aService) StephaneLenclud@159: { StephaneLenclud@159: iHandle = aHandle; StephaneLenclud@159: iIsService = aService; StephaneLenclud@159: iMonitorPowerOnDelegate = nullptr; StephaneLenclud@159: iMonitorPowerOffDelegate = nullptr; StephaneLenclud@159: iMonitorPowerObserverCount = 0; Stephane@160: iMonitorPowerHandle = NULL; StephaneLenclud@159: } StephaneLenclud@159: Stephane@160: /// TODO: Make this generic by passing the HPOWERNOTIFY by reference and GUID as parameter too Stephane@160: HPOWERNOTIFY SettingNotifier::RegisterPowerSettingNotification(LPCGUID aGuid) StephaneLenclud@159: { Stephane@160: HANDLE handle = iHandle.ToPointer(); Stephane@160: return ::RegisterPowerSettingNotification(handle, aGuid, (iIsService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE)); Stephane@160: } StephaneLenclud@159: StephaneLenclud@159: /// Stephane@160: void SettingNotifier::WndProc(Message% aMessage) StephaneLenclud@159: { StephaneLenclud@159: POWERBROADCAST_SETTING* setting; StephaneLenclud@159: StephaneLenclud@159: if (aMessage.Msg == WM_POWERBROADCAST && aMessage.WParam.ToInt32() == PBT_POWERSETTINGCHANGE) StephaneLenclud@159: { StephaneLenclud@159: setting=(POWERBROADCAST_SETTING*)aMessage.LParam.ToPointer(); StephaneLenclud@159: if (setting->PowerSetting == GUID_MONITOR_POWER_ON) StephaneLenclud@159: { StephaneLenclud@159: if (setting->Data[0] == 0x0) StephaneLenclud@159: { StephaneLenclud@159: Debug::WriteLine(L"POWERBROADCAST: Monitor Power Off"); StephaneLenclud@159: OnMonitorPowerOff(); StephaneLenclud@159: } StephaneLenclud@159: else if (setting->Data[0] == 0x1) StephaneLenclud@159: { StephaneLenclud@159: Debug::WriteLine(L"POWERBROADCAST: Monitor Power On"); StephaneLenclud@159: OnMonitorPowerOn(); StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// Stephane@160: void SettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOnDelegate += d; StephaneLenclud@159: iMonitorPowerObserverCount++; StephaneLenclud@159: //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0) StephaneLenclud@159: if (iMonitorPowerObserverCount == 1) StephaneLenclud@159: { Stephane@160: //Register for monitor power notifications Stephane@160: iMonitorPowerHandle=RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON); StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// Stephane@160: void SettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOnDelegate -= d; StephaneLenclud@159: iMonitorPowerObserverCount--; StephaneLenclud@159: if (iMonitorPowerObserverCount==0) StephaneLenclud@159: { Stephane@160: //Unregister from corresponding power setting notification Stephane@160: UnregisterPowerSettingNotification(iMonitorPowerHandle); StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: // Stephane@160: void SettingNotifier::OnMonitorPowerOn::raise() StephaneLenclud@159: { StephaneLenclud@159: if (iMonitorPowerOnDelegate != nullptr) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOnDelegate->Invoke(); StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// Stephane@160: void SettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOffDelegate += d; StephaneLenclud@159: iMonitorPowerObserverCount++; StephaneLenclud@159: if (iMonitorPowerObserverCount == 1) StephaneLenclud@159: { Stephane@160: //Register for monitor power notifications Stephane@160: iMonitorPowerHandle = RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON); StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// Stephane@160: void SettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOffDelegate -= d; StephaneLenclud@159: iMonitorPowerObserverCount--; StephaneLenclud@159: if (iMonitorPowerObserverCount == 0) StephaneLenclud@159: { Stephane@160: //Unregister from corresponding power setting notification Stephane@160: UnregisterPowerSettingNotification(iMonitorPowerHandle); StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: // Stephane@160: void SettingNotifier::OnMonitorPowerOff::raise() StephaneLenclud@159: { StephaneLenclud@159: if (iMonitorPowerOffDelegate != nullptr) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOffDelegate->Invoke(); StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: StephaneLenclud@159: StephaneLenclud@159: }