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