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