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