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: 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@215: public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort) StephaneLenclud@167: { Stephane@197: //Assuming monitor is on when we start up Stephane@197: MonitorPowerOn = true; Stephane@197: StephaneLenclud@187: //Create our power setting notifier and register the event we are interested in StephaneLenclud@167: iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle); StephaneLenclud@215: iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn; StephaneLenclud@215: iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff; StephaneLenclud@215: 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@197: MonitorPowerOn = true; StephaneLenclud@215: //Trigger corresponding event thus executing associated actions StephaneLenclud@215: ManagerEventAction.Current.GetEvent().Trigger(); StephaneLenclud@167: } StephaneLenclud@167: StephaneLenclud@167: private void OnMonitorPowerOff() StephaneLenclud@167: { StephaneLenclud@215: MonitorPowerOn = false; StephaneLenclud@215: //Trigger corresponding event thus executing associated actions Stephane@212: ManagerEventAction.Current.GetEvent().Trigger(); 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: }