2 using System.Collections.Generic;
5 using System.Threading.Tasks;
6 using System.Diagnostics;
7 using System.Windows.Forms;
10 namespace SharpDisplayManager
12 class ConsumerElectronicControl
15 private PowerManager.SettingNotifier iPowerSettingNotifier;
17 private Cec.Client iCecClient;
18 ///This flag will only work properly if both on and off events are monitored.
19 ///TODO: have a more solid implementation
20 public bool MonitorPowerOn;
22 private bool iReconnectToPowerTv = false;
24 public void TestSendKeys()
26 iCecClient.TestSendKey();
32 /// <param name="aWndHandle"></param>
33 /// <param name="aDeviceName"></param>
34 /// <param name="aHdmiPort"></param>
35 public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort, bool aMonitorOn, bool aMonitorOff, bool aReconnectToPowerTv)
37 //Assuming monitor is on when we start up
38 MonitorPowerOn = true;
40 iReconnectToPowerTv = aReconnectToPowerTv;
42 //Create our power setting notifier and register the event we are interested in
43 iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
48 iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
54 iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
58 iCecClient = new Cec.Client(aDeviceName,aHdmiPort, CecDeviceType.PlaybackDevice, CecLogLevel.All&~CecLogLevel.Traffic&~CecLogLevel.Debug);
66 if (iPowerSettingNotifier != null)
68 iPowerSettingNotifier.OnMonitorPowerOn -= OnMonitorPowerOn;
69 iPowerSettingNotifier.OnMonitorPowerOff -= OnMonitorPowerOff;
70 iPowerSettingNotifier = null;
73 if (iCecClient != null)
84 private void ConnectCecClient()
86 //Our client takes care of closing before trying to connect
87 if (!iCecClient.Connect(1000))
89 Debug.WriteLine("WARNING: No CEC connection!");
93 private void OnMonitorPowerOn()
95 Console.WriteLine("ON");
97 if (iReconnectToPowerTv)
103 //iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv);
104 //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv,CecUserControlCode.PowerOnFunction,true);
105 //Set ourselves as the active source
106 iCecClient.Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
107 MonitorPowerOn = true;
110 private void OnMonitorPowerOff()
112 Console.WriteLine("OFF");
114 if (iReconnectToPowerTv)
119 //Try turning off the TV
120 iCecClient.Lib.StandbyDevices(CecLogicalAddress.Tv);
121 //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.PowerOffFunction, true);
122 //Tell everyone that we are no longer active
123 //iCecClient.Lib.SetInactiveView();
125 MonitorPowerOn = false;
129 /// We need to handle WM_POWERBROADCAST.
131 /// <param name="message"></param>
132 public void OnWndProc(ref Message message)
134 //Hook in our power manager
135 if (iPowerSettingNotifier != null)
137 iPowerSettingNotifier.WndProc(ref message);