Server/ConsumerElectronicControl.cs
author StephaneLenclud
Tue, 26 Jan 2016 18:51:36 +0100
changeset 187 549e7d887271
parent 169 4d78ad195827
child 197 c66ec88ed19d
permissions -rw-r--r--
Timer know disabled when no display connection.
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@168
    25
        public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort, bool aMonitorOn, bool aMonitorOff)
StephaneLenclud@167
    26
        {
StephaneLenclud@187
    27
            //Create our power setting notifier and register the event we are interested in
StephaneLenclud@167
    28
            iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
StephaneLenclud@168
    29
StephaneLenclud@168
    30
            //
StephaneLenclud@168
    31
            if (aMonitorOn)
StephaneLenclud@168
    32
            {
StephaneLenclud@168
    33
                iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
StephaneLenclud@168
    34
            }
StephaneLenclud@168
    35
StephaneLenclud@168
    36
            //
StephaneLenclud@168
    37
            if (aMonitorOff)
StephaneLenclud@168
    38
            {
StephaneLenclud@168
    39
                iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
StephaneLenclud@168
    40
            }
StephaneLenclud@167
    41
StephaneLenclud@167
    42
            //CEC
StephaneLenclud@167
    43
            iCecClient = new Cec.Client(aDeviceName,aHdmiPort);
StephaneLenclud@167
    44
            if (!iCecClient.Connect(1000))
StephaneLenclud@167
    45
            {
StephaneLenclud@167
    46
                Debug.WriteLine("WARNING: No CEC connection!");
StephaneLenclud@167
    47
            }
StephaneLenclud@167
    48
        }
StephaneLenclud@167
    49
StephaneLenclud@167
    50
        //
StephaneLenclud@167
    51
        public void Stop()
StephaneLenclud@167
    52
        {
StephaneLenclud@167
    53
            //
StephaneLenclud@168
    54
            if (iPowerSettingNotifier != null)
StephaneLenclud@168
    55
            {
StephaneLenclud@168
    56
                iPowerSettingNotifier.OnMonitorPowerOn -= OnMonitorPowerOn;
StephaneLenclud@168
    57
                iPowerSettingNotifier.OnMonitorPowerOff -= OnMonitorPowerOff;
StephaneLenclud@168
    58
                iPowerSettingNotifier = null;
StephaneLenclud@168
    59
            }
StephaneLenclud@167
    60
            //
StephaneLenclud@168
    61
            if (iCecClient != null)
StephaneLenclud@168
    62
            {
StephaneLenclud@168
    63
                iCecClient.Close();
StephaneLenclud@168
    64
                iCecClient.Dispose();
StephaneLenclud@168
    65
                iCecClient = null;
StephaneLenclud@168
    66
            }
StephaneLenclud@167
    67
        }
StephaneLenclud@167
    68
StephaneLenclud@167
    69
StephaneLenclud@167
    70
        private void OnMonitorPowerOn()
StephaneLenclud@167
    71
        {
StephaneLenclud@167
    72
            Debug.WriteLine("ON");
StephaneLenclud@169
    73
            iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv);
StephaneLenclud@167
    74
            iCecClient.Lib.SetActiveSource(CecDeviceType.Tv);
StephaneLenclud@167
    75
        }
StephaneLenclud@167
    76
StephaneLenclud@167
    77
        private void OnMonitorPowerOff()
StephaneLenclud@167
    78
        {
StephaneLenclud@167
    79
            Debug.WriteLine("OFF");
StephaneLenclud@167
    80
            iCecClient.Lib.StandbyDevices(CecLogicalAddress.Tv);
StephaneLenclud@167
    81
        }
StephaneLenclud@167
    82
StephaneLenclud@167
    83
        /// <summary>
StephaneLenclud@167
    84
        /// We need to handle WM_INPUT.
StephaneLenclud@167
    85
        /// </summary>
StephaneLenclud@167
    86
        /// <param name="message"></param>
StephaneLenclud@167
    87
        public void OnWndProc(ref Message message)
StephaneLenclud@167
    88
        {
StephaneLenclud@167
    89
            //Hook in our power manager
StephaneLenclud@167
    90
            if (iPowerSettingNotifier != null)
StephaneLenclud@167
    91
            {
StephaneLenclud@167
    92
                iPowerSettingNotifier.WndProc(ref message);
StephaneLenclud@167
    93
            }
StephaneLenclud@167
    94
        }
StephaneLenclud@167
    95
StephaneLenclud@167
    96
    }
StephaneLenclud@167
    97
}