Server/ConsumerElectronicControl.cs
author StephaneLenclud
Wed, 17 Aug 2016 16:39:36 +0200
changeset 237 1a1c2ae3a29c
parent 234 0c75dec19d39
child 260 d44943088c67
permissions -rw-r--r--
Adding HID consumer control event.
     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 using SharpLib.Ear;
    10 
    11 namespace SharpDisplayManager
    12 {
    13     class ConsumerElectronicControl
    14     {
    15         ///
    16         private PowerManager.SettingNotifier iPowerSettingNotifier;
    17         ///
    18         public Cec.Client Client;
    19         ///This flag will only work properly if both on and off events are monitored.
    20         ///TODO: have a more solid implementation
    21         public bool MonitorPowerOn;
    22 
    23 
    24         public void TestSendKeys()
    25         {
    26             Client.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)
    36         {
    37             //Assuming monitor is on when we start up
    38             MonitorPowerOn = true;
    39 
    40             //Create our power setting notifier and register the event we are interested in
    41             iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
    42             iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
    43             iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
    44             
    45             //CEC
    46             Client = new Cec.Client(aDeviceName,aHdmiPort, CecDeviceType.PlaybackDevice);
    47             ConnectCecClient();
    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 (Client != null)
    62             {
    63                 Client.Close();
    64                 Client.Dispose();
    65                 Client = null;
    66             }
    67         }
    68 
    69         /// <summary>
    70         /// 
    71         /// </summary>
    72         private void ConnectCecClient()
    73         {
    74             //Our client takes care of closing before trying to connect
    75             if (!Client.Open(1000))
    76             {
    77                 Debug.WriteLine("WARNING: No CEC connection!");
    78             }
    79         }
    80 
    81         private void OnMonitorPowerOn()
    82         {
    83             MonitorPowerOn = true;
    84             //Trigger corresponding event thus executing associated actions
    85             Properties.Settings.Default.EarManager.TriggerEvent<EventMonitorPowerOn>();            
    86         }
    87 
    88         private void OnMonitorPowerOff()
    89         {
    90             MonitorPowerOn = false;
    91             //Trigger corresponding event thus executing associated actions
    92             Properties.Settings.Default.EarManager.TriggerEvent<EventMonitorPowerOff>();
    93         }
    94 
    95         /// <summary>
    96         /// We need to handle WM_POWERBROADCAST.
    97         /// </summary>
    98         /// <param name="message"></param>
    99         public void OnWndProc(ref Message message)
   100         {
   101             //Hook in our power manager
   102             if (iPowerSettingNotifier != null)
   103             {
   104                 iPowerSettingNotifier.WndProc(ref message);
   105             }
   106         }
   107 
   108     }
   109 }