CEC TV power on and standby working.
1 // This is the main DLL file.
5 #include "PowerManager.h"
7 using namespace System::Diagnostics;
12 SettingNotifier::SettingNotifier(IntPtr aHandle, Boolean aService)
14 Construct(aHandle, aService);
18 SettingNotifier::SettingNotifier(IntPtr aHandle)
20 //By default we assume we run as a Window
21 Construct(aHandle, false);
25 void SettingNotifier::Construct(IntPtr aHandle, Boolean aService)
28 iIsService = aService;
29 iMonitorPowerOnDelegate = nullptr;
30 iMonitorPowerOffDelegate = nullptr;
31 iMonitorPowerObserverCount = 0;
32 iMonitorPowerHandle = NULL;
35 /// TODO: Make this generic by passing the HPOWERNOTIFY by reference and GUID as parameter too
36 HPOWERNOTIFY SettingNotifier::RegisterPowerSettingNotification(LPCGUID aGuid)
38 HANDLE handle = iHandle.ToPointer();
39 return ::RegisterPowerSettingNotification(handle, aGuid, (iIsService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE));
43 void SettingNotifier::WndProc(Message% aMessage)
45 POWERBROADCAST_SETTING* setting;
47 if (aMessage.Msg == WM_POWERBROADCAST && aMessage.WParam.ToInt32() == PBT_POWERSETTINGCHANGE)
49 setting=(POWERBROADCAST_SETTING*)aMessage.LParam.ToPointer();
50 if (setting->PowerSetting == GUID_MONITOR_POWER_ON)
52 if (setting->Data[0] == 0x0)
54 Debug::WriteLine(L"POWERBROADCAST: Monitor Power Off");
57 else if (setting->Data[0] == 0x1)
59 Debug::WriteLine(L"POWERBROADCAST: Monitor Power On");
69 void SettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
71 iMonitorPowerOnDelegate += d;
72 iMonitorPowerObserverCount++;
73 //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0)
74 if (iMonitorPowerObserverCount == 1)
76 //Register for monitor power notifications
77 iMonitorPowerHandle=RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
83 void SettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
85 iMonitorPowerOnDelegate -= d;
86 iMonitorPowerObserverCount--;
87 if (iMonitorPowerObserverCount==0)
89 //Unregister from corresponding power setting notification
90 UnregisterPowerSettingNotification(iMonitorPowerHandle);
95 void SettingNotifier::OnMonitorPowerOn::raise()
97 if (iMonitorPowerOnDelegate != nullptr)
99 iMonitorPowerOnDelegate->Invoke();
104 void SettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
106 iMonitorPowerOffDelegate += d;
107 iMonitorPowerObserverCount++;
108 if (iMonitorPowerObserverCount == 1)
110 //Register for monitor power notifications
111 iMonitorPowerHandle = RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
116 void SettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
118 iMonitorPowerOffDelegate -= d;
119 iMonitorPowerObserverCount--;
120 if (iMonitorPowerObserverCount == 0)
122 //Unregister from corresponding power setting notification
123 UnregisterPowerSettingNotification(iMonitorPowerHandle);
128 void SettingNotifier::OnMonitorPowerOff::raise()
130 if (iMonitorPowerOffDelegate != nullptr)
132 iMonitorPowerOffDelegate->Invoke();