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@210: using SharpLib.Ear; 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@206: public Cec.Client Client; 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: { StephaneLenclud@206: Client.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 StephaneLenclud@206: Client = new Cec.Client(aDeviceName,aHdmiPort, CecDeviceType.PlaybackDevice); 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@206: if (Client != null) StephaneLenclud@168: { StephaneLenclud@206: Client.Close(); StephaneLenclud@206: Client.Dispose(); StephaneLenclud@206: Client = 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@214: if (!Client.Open(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@212: ManagerEventAction.Current.GetEvent().Trigger(); StephaneLenclud@210: StephaneLenclud@206: Console.WriteLine("OnMonitorPowerOn"); StephaneLenclud@204: StephaneLenclud@204: if (iReconnectToPowerTv) StephaneLenclud@204: { StephaneLenclud@204: ConnectCecClient(); StephaneLenclud@210: } 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 StephaneLenclud@206: Client.Lib.SetActiveSource(CecDeviceType.PlaybackDevice); Stephane@197: MonitorPowerOn = true; StephaneLenclud@167: } StephaneLenclud@167: StephaneLenclud@167: private void OnMonitorPowerOff() StephaneLenclud@167: { Stephane@212: ManagerEventAction.Current.GetEvent().Trigger(); StephaneLenclud@210: StephaneLenclud@206: Console.WriteLine("OnMonitorPowerOff"); StephaneLenclud@204: StephaneLenclud@204: if (iReconnectToPowerTv) StephaneLenclud@204: { StephaneLenclud@204: ConnectCecClient(); StephaneLenclud@204: } StephaneLenclud@204: Stephane@203: //Try turning off the TV StephaneLenclud@206: Client.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: }