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