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@203
|
54 |
iCecClient = new Cec.Client(aDeviceName,aHdmiPort, CecDeviceType.PlaybackDevice, CecLogLevel.All&~CecLogLevel.Traffic&~CecLogLevel.Debug);
|
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 |
{
|
Stephane@203
|
83 |
Console.WriteLine("ON");
|
Stephane@203
|
84 |
//Turn on the TV
|
Stephane@203
|
85 |
//iCecClient.Lib.PowerOnDevices(CecLogicalAddress.Tv);
|
Stephane@203
|
86 |
//iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv,CecUserControlCode.PowerOnFunction,true);
|
Stephane@203
|
87 |
//Set ourselves as the active source
|
Stephane@203
|
88 |
iCecClient.Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
|
Stephane@197
|
89 |
MonitorPowerOn = true;
|
StephaneLenclud@167
|
90 |
}
|
StephaneLenclud@167
|
91 |
|
StephaneLenclud@167
|
92 |
private void OnMonitorPowerOff()
|
StephaneLenclud@167
|
93 |
{
|
Stephane@203
|
94 |
Console.WriteLine("OFF");
|
Stephane@203
|
95 |
//Try turning off the TV
|
StephaneLenclud@167
|
96 |
iCecClient.Lib.StandbyDevices(CecLogicalAddress.Tv);
|
Stephane@203
|
97 |
//iCecClient.Lib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.PowerOffFunction, true);
|
Stephane@203
|
98 |
//Tell everyone that we are no longer active
|
Stephane@203
|
99 |
//iCecClient.Lib.SetInactiveView();
|
Stephane@203
|
100 |
|
Stephane@197
|
101 |
MonitorPowerOn = false;
|
StephaneLenclud@167
|
102 |
}
|
StephaneLenclud@167
|
103 |
|
StephaneLenclud@167
|
104 |
/// <summary>
|
Stephane@197
|
105 |
/// We need to handle WM_POWERBROADCAST.
|
StephaneLenclud@167
|
106 |
/// </summary>
|
StephaneLenclud@167
|
107 |
/// <param name="message"></param>
|
StephaneLenclud@167
|
108 |
public void OnWndProc(ref Message message)
|
StephaneLenclud@167
|
109 |
{
|
StephaneLenclud@167
|
110 |
//Hook in our power manager
|
StephaneLenclud@167
|
111 |
if (iPowerSettingNotifier != null)
|
StephaneLenclud@167
|
112 |
{
|
StephaneLenclud@167
|
113 |
iPowerSettingNotifier.WndProc(ref message);
|
StephaneLenclud@167
|
114 |
}
|
StephaneLenclud@167
|
115 |
}
|
StephaneLenclud@167
|
116 |
|
StephaneLenclud@167
|
117 |
}
|
StephaneLenclud@167
|
118 |
} |