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@167: /// StephaneLenclud@167: /// StephaneLenclud@167: /// StephaneLenclud@167: /// StephaneLenclud@167: /// StephaneLenclud@167: /// StephaneLenclud@168: public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort, bool aMonitorOn, bool aMonitorOff) 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@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@167: iCecClient = new Cec.Client(aDeviceName,aHdmiPort); StephaneLenclud@167: if (!iCecClient.Connect(1000)) StephaneLenclud@167: { StephaneLenclud@167: Debug.WriteLine("WARNING: No CEC connection!"); StephaneLenclud@167: } 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@167: StephaneLenclud@167: private void OnMonitorPowerOn() StephaneLenclud@167: { StephaneLenclud@167: Debug.WriteLine("ON"); StephaneLenclud@169: iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv); StephaneLenclud@167: iCecClient.Lib.SetActiveSource(CecDeviceType.Tv); Stephane@197: MonitorPowerOn = true; StephaneLenclud@167: } StephaneLenclud@167: StephaneLenclud@167: private void OnMonitorPowerOff() StephaneLenclud@167: { StephaneLenclud@167: Debug.WriteLine("OFF"); StephaneLenclud@167: iCecClient.Lib.StandbyDevices(CecLogicalAddress.Tv); 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: }