Server/ConsumerElectronicControl.cs
author StephaneLenclud
Mon, 18 Jul 2016 15:11:33 +0200
changeset 204 6345461600d7
parent 203 ce63ebb51da4
child 206 33be8cb90c57
permissions -rw-r--r--
Adding option to reconnect CEC before TV power action.
     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         private bool iReconnectToPowerTv = false;
    23 
    24         public void TestSendKeys()
    25         {
    26             iCecClient.TestSendKey();
    27         }
    28 
    29         /// <summary>
    30         /// 
    31         /// </summary>
    32         /// <param name="aWndHandle"></param>
    33         /// <param name="aDeviceName"></param>
    34         /// <param name="aHdmiPort"></param>
    35         public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort, bool aMonitorOn, bool aMonitorOff, bool aReconnectToPowerTv)
    36         {
    37             //Assuming monitor is on when we start up
    38             MonitorPowerOn = true;
    39 
    40             iReconnectToPowerTv = aReconnectToPowerTv;
    41 
    42             //Create our power setting notifier and register the event we are interested in
    43             iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
    44 
    45             //
    46             if (aMonitorOn)
    47             {
    48                 iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
    49             }
    50 
    51             //
    52             if (aMonitorOff)
    53             {
    54                 iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
    55             }
    56 
    57             //CEC
    58             iCecClient = new Cec.Client(aDeviceName,aHdmiPort, CecDeviceType.PlaybackDevice, CecLogLevel.All&~CecLogLevel.Traffic&~CecLogLevel.Debug);
    59             ConnectCecClient();
    60         }
    61 
    62         //
    63         public void Stop()
    64         {
    65             //
    66             if (iPowerSettingNotifier != null)
    67             {
    68                 iPowerSettingNotifier.OnMonitorPowerOn -= OnMonitorPowerOn;
    69                 iPowerSettingNotifier.OnMonitorPowerOff -= OnMonitorPowerOff;
    70                 iPowerSettingNotifier = null;
    71             }
    72             //
    73             if (iCecClient != null)
    74             {
    75                 iCecClient.Close();
    76                 iCecClient.Dispose();
    77                 iCecClient = null;
    78             }
    79         }
    80 
    81         /// <summary>
    82         /// 
    83         /// </summary>
    84         private void ConnectCecClient()
    85         {
    86             //Our client takes care of closing before trying to connect
    87             if (!iCecClient.Connect(1000))
    88             {
    89                 Debug.WriteLine("WARNING: No CEC connection!");
    90             }
    91         }
    92 
    93         private void OnMonitorPowerOn()
    94         {
    95             Console.WriteLine("ON");
    96 
    97             if (iReconnectToPowerTv)
    98             {
    99                 ConnectCecClient();
   100             }
   101 
   102             //Turn on the TV
   103             //iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv);
   104             //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv,CecUserControlCode.PowerOnFunction,true);
   105             //Set ourselves as the active source
   106             iCecClient.Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
   107             MonitorPowerOn = true;
   108         }
   109 
   110         private void OnMonitorPowerOff()
   111         {
   112             Console.WriteLine("OFF");
   113 
   114             if (iReconnectToPowerTv)
   115             {
   116                 ConnectCecClient();
   117             }
   118 
   119             //Try turning off the TV
   120             iCecClient.Lib.StandbyDevices(CecLogicalAddress.Tv);
   121             //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.PowerOffFunction, true);
   122             //Tell everyone that we are no longer active
   123             //iCecClient.Lib.SetInactiveView();
   124 
   125             MonitorPowerOn = false;
   126         }
   127 
   128         /// <summary>
   129         /// We need to handle WM_POWERBROADCAST.
   130         /// </summary>
   131         /// <param name="message"></param>
   132         public void OnWndProc(ref Message message)
   133         {
   134             //Hook in our power manager
   135             if (iPowerSettingNotifier != null)
   136             {
   137                 iPowerSettingNotifier.WndProc(ref message);
   138             }
   139         }
   140 
   141     }
   142 }