Server/ConsumerElectronicControl.cs
author StephaneLenclud
Fri, 22 Jul 2016 18:19:49 +0200
changeset 209 fef4ca058087
parent 204 6345461600d7
child 210 83dd86e73448
permissions -rw-r--r--
Prototype for event and action framework.
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@206
    17
        public Cec.Client Client;
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@204
    22
        private bool iReconnectToPowerTv = false;
StephaneLenclud@204
    23
Stephane@202
    24
        public void TestSendKeys()
Stephane@202
    25
        {
StephaneLenclud@206
    26
            Client.TestSendKey();
Stephane@202
    27
        }
Stephane@202
    28
StephaneLenclud@167
    29
        /// <summary>
StephaneLenclud@167
    30
        /// 
StephaneLenclud@167
    31
        /// </summary>
StephaneLenclud@167
    32
        /// <param name="aWndHandle"></param>
StephaneLenclud@167
    33
        /// <param name="aDeviceName"></param>
StephaneLenclud@167
    34
        /// <param name="aHdmiPort"></param>
StephaneLenclud@204
    35
        public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort, bool aMonitorOn, bool aMonitorOff, bool aReconnectToPowerTv)
StephaneLenclud@167
    36
        {
Stephane@197
    37
            //Assuming monitor is on when we start up
Stephane@197
    38
            MonitorPowerOn = true;
Stephane@197
    39
StephaneLenclud@204
    40
            iReconnectToPowerTv = aReconnectToPowerTv;
StephaneLenclud@204
    41
StephaneLenclud@187
    42
            //Create our power setting notifier and register the event we are interested in
StephaneLenclud@167
    43
            iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
StephaneLenclud@168
    44
StephaneLenclud@168
    45
            //
StephaneLenclud@168
    46
            if (aMonitorOn)
StephaneLenclud@168
    47
            {
StephaneLenclud@168
    48
                iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
StephaneLenclud@168
    49
            }
StephaneLenclud@168
    50
StephaneLenclud@168
    51
            //
StephaneLenclud@168
    52
            if (aMonitorOff)
StephaneLenclud@168
    53
            {
StephaneLenclud@168
    54
                iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
StephaneLenclud@168
    55
            }
StephaneLenclud@167
    56
StephaneLenclud@167
    57
            //CEC
StephaneLenclud@206
    58
            Client = new Cec.Client(aDeviceName,aHdmiPort, CecDeviceType.PlaybackDevice);
StephaneLenclud@204
    59
            ConnectCecClient();
StephaneLenclud@167
    60
        }
StephaneLenclud@167
    61
StephaneLenclud@167
    62
        //
StephaneLenclud@167
    63
        public void Stop()
StephaneLenclud@167
    64
        {
StephaneLenclud@167
    65
            //
StephaneLenclud@168
    66
            if (iPowerSettingNotifier != null)
StephaneLenclud@168
    67
            {
StephaneLenclud@168
    68
                iPowerSettingNotifier.OnMonitorPowerOn -= OnMonitorPowerOn;
StephaneLenclud@168
    69
                iPowerSettingNotifier.OnMonitorPowerOff -= OnMonitorPowerOff;
StephaneLenclud@168
    70
                iPowerSettingNotifier = null;
StephaneLenclud@168
    71
            }
StephaneLenclud@167
    72
            //
StephaneLenclud@206
    73
            if (Client != null)
StephaneLenclud@168
    74
            {
StephaneLenclud@206
    75
                Client.Close();
StephaneLenclud@206
    76
                Client.Dispose();
StephaneLenclud@206
    77
                Client = null;
StephaneLenclud@168
    78
            }
StephaneLenclud@167
    79
        }
StephaneLenclud@167
    80
StephaneLenclud@204
    81
        /// <summary>
StephaneLenclud@204
    82
        /// 
StephaneLenclud@204
    83
        /// </summary>
StephaneLenclud@204
    84
        private void ConnectCecClient()
StephaneLenclud@204
    85
        {
StephaneLenclud@204
    86
            //Our client takes care of closing before trying to connect
StephaneLenclud@206
    87
            if (!Client.Connect(1000))
StephaneLenclud@204
    88
            {
StephaneLenclud@204
    89
                Debug.WriteLine("WARNING: No CEC connection!");
StephaneLenclud@204
    90
            }
StephaneLenclud@204
    91
        }
StephaneLenclud@167
    92
StephaneLenclud@167
    93
        private void OnMonitorPowerOn()
StephaneLenclud@167
    94
        {
StephaneLenclud@206
    95
            Console.WriteLine("OnMonitorPowerOn");
StephaneLenclud@204
    96
StephaneLenclud@204
    97
            if (iReconnectToPowerTv)
StephaneLenclud@204
    98
            {
StephaneLenclud@204
    99
                ConnectCecClient();
StephaneLenclud@204
   100
            }
StephaneLenclud@204
   101
Stephane@203
   102
            //Turn on the TV
Stephane@203
   103
            //iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv);
Stephane@203
   104
            //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv,CecUserControlCode.PowerOnFunction,true);
Stephane@203
   105
            //Set ourselves as the active source
StephaneLenclud@206
   106
            Client.Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
Stephane@197
   107
            MonitorPowerOn = true;
StephaneLenclud@167
   108
        }
StephaneLenclud@167
   109
StephaneLenclud@167
   110
        private void OnMonitorPowerOff()
StephaneLenclud@167
   111
        {
StephaneLenclud@206
   112
            Console.WriteLine("OnMonitorPowerOff");
StephaneLenclud@204
   113
StephaneLenclud@204
   114
            if (iReconnectToPowerTv)
StephaneLenclud@204
   115
            {
StephaneLenclud@204
   116
                ConnectCecClient();
StephaneLenclud@204
   117
            }
StephaneLenclud@204
   118
Stephane@203
   119
            //Try turning off the TV
StephaneLenclud@206
   120
            Client.Lib.StandbyDevices(CecLogicalAddress.Tv);
Stephane@203
   121
            //iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.PowerOffFunction, true);
Stephane@203
   122
            //Tell everyone that we are no longer active
Stephane@203
   123
            //iCecClient.Lib.SetInactiveView();
Stephane@203
   124
Stephane@197
   125
            MonitorPowerOn = false;
StephaneLenclud@167
   126
        }
StephaneLenclud@167
   127
StephaneLenclud@167
   128
        /// <summary>
Stephane@197
   129
        /// We need to handle WM_POWERBROADCAST.
StephaneLenclud@167
   130
        /// </summary>
StephaneLenclud@167
   131
        /// <param name="message"></param>
StephaneLenclud@167
   132
        public void OnWndProc(ref Message message)
StephaneLenclud@167
   133
        {
StephaneLenclud@167
   134
            //Hook in our power manager
StephaneLenclud@167
   135
            if (iPowerSettingNotifier != null)
StephaneLenclud@167
   136
            {
StephaneLenclud@167
   137
                iPowerSettingNotifier.WndProc(ref message);
StephaneLenclud@167
   138
            }
StephaneLenclud@167
   139
        }
StephaneLenclud@167
   140
StephaneLenclud@167
   141
    }
StephaneLenclud@167
   142
}