Server/ConsumerElectronicControl.cs
author Stephane Lenclud
Sun, 07 Feb 2016 22:37:31 +0100
changeset 197 c66ec88ed19d
parent 187 549e7d887271
child 202 8784c59c784e
permissions -rw-r--r--
Published v0.8.8.0.
Idle client now monitoring display power status so as to avoid going power
saving when the monitor is still on.
     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         ///This flag will only work properly if both on and off events are monitored.
    19         ///TODO: have a more solid implementation
    20         public bool MonitorPowerOn;
    21 
    22         /// <summary>
    23         /// 
    24         /// </summary>
    25         /// <param name="aWndHandle"></param>
    26         /// <param name="aDeviceName"></param>
    27         /// <param name="aHdmiPort"></param>
    28         public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort, bool aMonitorOn, bool aMonitorOff)
    29         {
    30             //Assuming monitor is on when we start up
    31             MonitorPowerOn = true;
    32 
    33             //Create our power setting notifier and register the event we are interested in
    34             iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
    35 
    36             //
    37             if (aMonitorOn)
    38             {
    39                 iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
    40             }
    41 
    42             //
    43             if (aMonitorOff)
    44             {
    45                 iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
    46             }
    47 
    48             //CEC
    49             iCecClient = new Cec.Client(aDeviceName,aHdmiPort);
    50             if (!iCecClient.Connect(1000))
    51             {
    52                 Debug.WriteLine("WARNING: No CEC connection!");
    53             }
    54         }
    55 
    56         //
    57         public void Stop()
    58         {
    59             //
    60             if (iPowerSettingNotifier != null)
    61             {
    62                 iPowerSettingNotifier.OnMonitorPowerOn -= OnMonitorPowerOn;
    63                 iPowerSettingNotifier.OnMonitorPowerOff -= OnMonitorPowerOff;
    64                 iPowerSettingNotifier = null;
    65             }
    66             //
    67             if (iCecClient != null)
    68             {
    69                 iCecClient.Close();
    70                 iCecClient.Dispose();
    71                 iCecClient = null;
    72             }
    73         }
    74 
    75 
    76         private void OnMonitorPowerOn()
    77         {
    78             Debug.WriteLine("ON");
    79             iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv);
    80             iCecClient.Lib.SetActiveSource(CecDeviceType.Tv);
    81             MonitorPowerOn = true;
    82         }
    83 
    84         private void OnMonitorPowerOff()
    85         {
    86             Debug.WriteLine("OFF");
    87             iCecClient.Lib.StandbyDevices(CecLogicalAddress.Tv);
    88             MonitorPowerOn = false;
    89         }
    90 
    91         /// <summary>
    92         /// We need to handle WM_POWERBROADCAST.
    93         /// </summary>
    94         /// <param name="message"></param>
    95         public void OnWndProc(ref Message message)
    96         {
    97             //Hook in our power manager
    98             if (iPowerSettingNotifier != null)
    99             {
   100                 iPowerSettingNotifier.WndProc(ref message);
   101             }
   102         }
   103 
   104     }
   105 }