Server/ConsumerElectronicControl.cs
author StephaneLenclud
Thu, 21 Jan 2016 20:32:51 +0100
changeset 181 503b711d5e8c
parent 168 2568261f74fb
child 187 549e7d887271
permissions -rw-r--r--
Published v0.8.0.0.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Diagnostics;
     7 using System.Windows.Forms;
     8 using CecSharp;
     9 
    10 namespace SharpDisplayManager
    11 {
    12     class ConsumerElectronicControl
    13     {
    14         ///
    15         private PowerManager.SettingNotifier iPowerSettingNotifier;
    16         ///
    17         private Cec.Client iCecClient;
    18 
    19         /// <summary>
    20         /// 
    21         /// </summary>
    22         /// <param name="aWndHandle"></param>
    23         /// <param name="aDeviceName"></param>
    24         /// <param name="aHdmiPort"></param>
    25         public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort, bool aMonitorOn, bool aMonitorOff)
    26         {
    27             //Create our power setting notifier and register the event we are insterrested in
    28             iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
    29 
    30             //
    31             if (aMonitorOn)
    32             {
    33                 iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
    34             }
    35 
    36             //
    37             if (aMonitorOff)
    38             {
    39                 iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
    40             }
    41 
    42             //CEC
    43             iCecClient = new Cec.Client(aDeviceName,aHdmiPort);
    44             if (!iCecClient.Connect(1000))
    45             {
    46                 Debug.WriteLine("WARNING: No CEC connection!");
    47             }
    48         }
    49 
    50         //
    51         public void Stop()
    52         {
    53             //
    54             if (iPowerSettingNotifier != null)
    55             {
    56                 iPowerSettingNotifier.OnMonitorPowerOn -= OnMonitorPowerOn;
    57                 iPowerSettingNotifier.OnMonitorPowerOff -= OnMonitorPowerOff;
    58                 iPowerSettingNotifier = null;
    59             }
    60             //
    61             if (iCecClient != null)
    62             {
    63                 iCecClient.Close();
    64                 iCecClient.Dispose();
    65                 iCecClient = null;
    66             }
    67         }
    68 
    69 
    70         private void OnMonitorPowerOn()
    71         {
    72             Debug.WriteLine("ON");
    73             iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv);
    74             iCecClient.Lib.SetActiveSource(CecDeviceType.Tv);
    75         }
    76 
    77         private void OnMonitorPowerOff()
    78         {
    79             Debug.WriteLine("OFF");
    80             iCecClient.Lib.StandbyDevices(CecLogicalAddress.Tv);
    81         }
    82 
    83         /// <summary>
    84         /// We need to handle WM_INPUT.
    85         /// </summary>
    86         /// <param name="message"></param>
    87         public void OnWndProc(ref Message message)
    88         {
    89             //Hook in our power manager
    90             if (iPowerSettingNotifier != null)
    91             {
    92                 iPowerSettingNotifier.WndProc(ref message);
    93             }
    94         }
    95 
    96     }
    97 }