Server/ConsumerElectronicControl.cs
author StephaneLenclud
Thu, 18 Aug 2016 17:13:21 +0200
changeset 239 dd7770b97916
parent 234 0c75dec19d39
child 260 d44943088c67
permissions -rw-r--r--
Improved object editor dialog validation.
Added EAR support for object validation.
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
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@215
    35
        public void Start(IntPtr aWndHandle, string aDeviceName, byte aHdmiPort)
StephaneLenclud@167
    36
        {
Stephane@197
    37
            //Assuming monitor is on when we start up
Stephane@197
    38
            MonitorPowerOn = true;
Stephane@197
    39
StephaneLenclud@187
    40
            //Create our power setting notifier and register the event we are interested in
StephaneLenclud@167
    41
            iPowerSettingNotifier = new PowerManager.SettingNotifier(aWndHandle);
StephaneLenclud@215
    42
            iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
StephaneLenclud@215
    43
            iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
StephaneLenclud@215
    44
            
StephaneLenclud@167
    45
            //CEC
StephaneLenclud@206
    46
            Client = new Cec.Client(aDeviceName,aHdmiPort, CecDeviceType.PlaybackDevice);
StephaneLenclud@204
    47
            ConnectCecClient();
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@206
    61
            if (Client != null)
StephaneLenclud@168
    62
            {
StephaneLenclud@206
    63
                Client.Close();
StephaneLenclud@206
    64
                Client.Dispose();
StephaneLenclud@206
    65
                Client = null;
StephaneLenclud@168
    66
            }
StephaneLenclud@167
    67
        }
StephaneLenclud@167
    68
StephaneLenclud@204
    69
        /// <summary>
StephaneLenclud@204
    70
        /// 
StephaneLenclud@204
    71
        /// </summary>
StephaneLenclud@204
    72
        private void ConnectCecClient()
StephaneLenclud@204
    73
        {
StephaneLenclud@204
    74
            //Our client takes care of closing before trying to connect
StephaneLenclud@214
    75
            if (!Client.Open(1000))
StephaneLenclud@204
    76
            {
StephaneLenclud@204
    77
                Debug.WriteLine("WARNING: No CEC connection!");
StephaneLenclud@204
    78
            }
StephaneLenclud@204
    79
        }
StephaneLenclud@167
    80
StephaneLenclud@167
    81
        private void OnMonitorPowerOn()
StephaneLenclud@167
    82
        {
Stephane@197
    83
            MonitorPowerOn = true;
StephaneLenclud@215
    84
            //Trigger corresponding event thus executing associated actions
StephaneLenclud@235
    85
            Properties.Settings.Default.EarManager.TriggerEvent<EventMonitorPowerOn>();            
StephaneLenclud@167
    86
        }
StephaneLenclud@167
    87
StephaneLenclud@167
    88
        private void OnMonitorPowerOff()
StephaneLenclud@167
    89
        {
StephaneLenclud@215
    90
            MonitorPowerOn = false;
StephaneLenclud@215
    91
            //Trigger corresponding event thus executing associated actions
StephaneLenclud@235
    92
            Properties.Settings.Default.EarManager.TriggerEvent<EventMonitorPowerOff>();
StephaneLenclud@167
    93
        }
StephaneLenclud@167
    94
StephaneLenclud@167
    95
        /// <summary>
Stephane@197
    96
        /// We need to handle WM_POWERBROADCAST.
StephaneLenclud@167
    97
        /// </summary>
StephaneLenclud@167
    98
        /// <param name="message"></param>
StephaneLenclud@167
    99
        public void OnWndProc(ref Message message)
StephaneLenclud@167
   100
        {
StephaneLenclud@167
   101
            //Hook in our power manager
StephaneLenclud@167
   102
            if (iPowerSettingNotifier != null)
StephaneLenclud@167
   103
            {
StephaneLenclud@167
   104
                iPowerSettingNotifier.WndProc(ref message);
StephaneLenclud@167
   105
            }
StephaneLenclud@167
   106
        }
StephaneLenclud@167
   107
StephaneLenclud@167
   108
    }
StephaneLenclud@167
   109
}