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: /// StephaneLenclud@159: PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle, Boolean aService) StephaneLenclud@159: { StephaneLenclud@159: Construct(aHandle, aService); StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// StephaneLenclud@159: PowerSettingNotifier::PowerSettingNotifier(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: /// StephaneLenclud@159: void PowerSettingNotifier::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; StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// StephaneLenclud@159: Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle, Boolean aService) StephaneLenclud@159: { StephaneLenclud@159: HANDLE handle = aHandle.ToPointer(); StephaneLenclud@159: HPOWERNOTIFY res=::RegisterPowerSettingNotification(handle, &GUID_MONITOR_POWER_ON, (aService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE)); StephaneLenclud@159: return (res != NULL); StephaneLenclud@159: }; StephaneLenclud@159: StephaneLenclud@159: /// StephaneLenclud@159: Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle) StephaneLenclud@159: { StephaneLenclud@159: return RegisterPowerSettingNotification(aHandle,false); StephaneLenclud@159: }; StephaneLenclud@159: StephaneLenclud@159: /// StephaneLenclud@159: void PowerSettingNotifier::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: /// StephaneLenclud@159: void PowerSettingNotifier::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: { StephaneLenclud@159: //TODO: register StephaneLenclud@159: RegisterPowerSettingNotification(iHandle,iIsService); StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// StephaneLenclud@159: void PowerSettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOnDelegate -= d; StephaneLenclud@159: iMonitorPowerObserverCount--; StephaneLenclud@159: if (iMonitorPowerObserverCount==0) StephaneLenclud@159: { StephaneLenclud@159: //TODO: unregister StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: // StephaneLenclud@159: void PowerSettingNotifier::OnMonitorPowerOn::raise() StephaneLenclud@159: { StephaneLenclud@159: if (iMonitorPowerOnDelegate != nullptr) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOnDelegate->Invoke(); StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// StephaneLenclud@159: void PowerSettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOffDelegate += d; StephaneLenclud@159: iMonitorPowerObserverCount++; StephaneLenclud@159: if (iMonitorPowerObserverCount == 1) StephaneLenclud@159: { StephaneLenclud@159: //TODO: register StephaneLenclud@159: RegisterPowerSettingNotification(iHandle, iIsService); StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: /// StephaneLenclud@159: void PowerSettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d) StephaneLenclud@159: { StephaneLenclud@159: iMonitorPowerOffDelegate -= d; StephaneLenclud@159: iMonitorPowerObserverCount--; StephaneLenclud@159: if (iMonitorPowerObserverCount == 0) StephaneLenclud@159: { StephaneLenclud@159: //TODO: unregister StephaneLenclud@159: } StephaneLenclud@159: } StephaneLenclud@159: StephaneLenclud@159: // StephaneLenclud@159: void PowerSettingNotifier::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: }