StephaneLenclud@167: using System;
StephaneLenclud@167: using System.Collections.Generic;
StephaneLenclud@167: using System.Linq;
StephaneLenclud@167: using System.Text;
StephaneLenclud@167: using System.Threading.Tasks;
StephaneLenclud@167: using System.Diagnostics;
StephaneLenclud@167: using System.Windows.Forms;
StephaneLenclud@167: using CecSharp;
StephaneLenclud@167:
StephaneLenclud@167: namespace SharpDisplayManager
StephaneLenclud@167: {
StephaneLenclud@167: class ConsumerElectronicControl
StephaneLenclud@167: {
StephaneLenclud@167: ///
StephaneLenclud@167: private PowerManager.SettingNotifier iPowerSettingNotifier;
StephaneLenclud@167: ///
StephaneLenclud@167: private Cec.Client iCecClient;
Stephane@197: ///This flag will only work properly if both on and off events are monitored.
Stephane@197: ///TODO: have a more solid implementation
Stephane@197: public bool MonitorPowerOn;
StephaneLenclud@167:
StephaneLenclud@204: private bool iReconnectToPowerTv = false;
StephaneLenclud@204:
Stephane@202: public void TestSendKeys()
Stephane@202: {
Stephane@202: iCecClient.TestSendKey();
Stephane@202: }
Stephane@202:
StephaneLenclud@167: ///
StephaneLenclud@167: ///
StephaneLenclud@167: ///
StephaneLenclud@167: ///
StephaneLenclud@167: ///
StephaneLenclud@167: ///
StephaneLenclud@204: public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort, bool aMonitorOn, bool aMonitorOff, bool aReconnectToPowerTv)
StephaneLenclud@167: {
Stephane@197: //Assuming monitor is on when we start up
Stephane@197: MonitorPowerOn = true;
Stephane@197:
StephaneLenclud@204: iReconnectToPowerTv = aReconnectToPowerTv;
StephaneLenclud@204:
StephaneLenclud@187: //Create our power setting notifier and register the event we are interested in
StephaneLenclud@167: iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
StephaneLenclud@168:
StephaneLenclud@168: //
StephaneLenclud@168: if (aMonitorOn)
StephaneLenclud@168: {
StephaneLenclud@168: iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
StephaneLenclud@168: }
StephaneLenclud@168:
StephaneLenclud@168: //
StephaneLenclud@168: if (aMonitorOff)
StephaneLenclud@168: {
StephaneLenclud@168: iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
StephaneLenclud@168: }
StephaneLenclud@167:
StephaneLenclud@167: //CEC
Stephane@203: iCecClient = new Cec.Client(aDeviceName,aHdmiPort, CecDeviceType.PlaybackDevice, CecLogLevel.All&~CecLogLevel.Traffic&~CecLogLevel.Debug);
StephaneLenclud@204: ConnectCecClient();
StephaneLenclud@167: }
StephaneLenclud@167:
StephaneLenclud@167: //
StephaneLenclud@167: public void Stop()
StephaneLenclud@167: {
StephaneLenclud@167: //
StephaneLenclud@168: if (iPowerSettingNotifier != null)
StephaneLenclud@168: {
StephaneLenclud@168: iPowerSettingNotifier.OnMonitorPowerOn -= OnMonitorPowerOn;
StephaneLenclud@168: iPowerSettingNotifier.OnMonitorPowerOff -= OnMonitorPowerOff;
StephaneLenclud@168: iPowerSettingNotifier = null;
StephaneLenclud@168: }
StephaneLenclud@167: //
StephaneLenclud@168: if (iCecClient != null)
StephaneLenclud@168: {
StephaneLenclud@168: iCecClient.Close();
StephaneLenclud@168: iCecClient.Dispose();
StephaneLenclud@168: iCecClient = null;
StephaneLenclud@168: }
StephaneLenclud@167: }
StephaneLenclud@167:
StephaneLenclud@204: ///
StephaneLenclud@204: ///
StephaneLenclud@204: ///
StephaneLenclud@204: private void ConnectCecClient()
StephaneLenclud@204: {
StephaneLenclud@204: //Our client takes care of closing before trying to connect
StephaneLenclud@204: if (!iCecClient.Connect(1000))
StephaneLenclud@204: {
StephaneLenclud@204: Debug.WriteLine("WARNING: No CEC connection!");
StephaneLenclud@204: }
StephaneLenclud@204: }
StephaneLenclud@167:
StephaneLenclud@167: private void OnMonitorPowerOn()
StephaneLenclud@167: {
Stephane@203: Console.WriteLine("ON");
StephaneLenclud@204:
StephaneLenclud@204: if (iReconnectToPowerTv)
StephaneLenclud@204: {
StephaneLenclud@204: ConnectCecClient();
StephaneLenclud@204: }
StephaneLenclud@204:
Stephane@203: //Turn on the TV
Stephane@203: //iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv);
Stephane@203: //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv,CecUserControlCode.PowerOnFunction,true);
Stephane@203: //Set ourselves as the active source
Stephane@203: iCecClient.Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
Stephane@197: MonitorPowerOn = true;
StephaneLenclud@167: }
StephaneLenclud@167:
StephaneLenclud@167: private void OnMonitorPowerOff()
StephaneLenclud@167: {
Stephane@203: Console.WriteLine("OFF");
StephaneLenclud@204:
StephaneLenclud@204: if (iReconnectToPowerTv)
StephaneLenclud@204: {
StephaneLenclud@204: ConnectCecClient();
StephaneLenclud@204: }
StephaneLenclud@204:
Stephane@203: //Try turning off the TV
StephaneLenclud@167: iCecClient.Lib.StandbyDevices(CecLogicalAddress.Tv);
Stephane@203: //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.PowerOffFunction, true);
Stephane@203: //Tell everyone that we are no longer active
Stephane@203: //iCecClient.Lib.SetInactiveView();
Stephane@203:
Stephane@197: MonitorPowerOn = false;
StephaneLenclud@167: }
StephaneLenclud@167:
StephaneLenclud@167: ///
Stephane@197: /// We need to handle WM_POWERBROADCAST.
StephaneLenclud@167: ///
StephaneLenclud@167: ///
StephaneLenclud@167: public void OnWndProc(ref Message message)
StephaneLenclud@167: {
StephaneLenclud@167: //Hook in our power manager
StephaneLenclud@167: if (iPowerSettingNotifier != null)
StephaneLenclud@167: {
StephaneLenclud@167: iPowerSettingNotifier.WndProc(ref message);
StephaneLenclud@167: }
StephaneLenclud@167: }
StephaneLenclud@167:
StephaneLenclud@167: }
StephaneLenclud@167: }