EAR: Actions now support multiple iterations.
2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
36 using System.Threading;
37 using System.Diagnostics;
41 class Client : CecCallbackMethods
44 /// Enable public static access
46 public static Client Static;
49 /// Provide direct access to CEC library
51 public LibCecSharp Lib
62 public int LogLevel = (int)CecLogLevel.Notice;
67 public bool FilterOutPollLogs = true;
72 private LibCecSharp iLib;
76 private LibCECConfiguration Config;
81 private bool iConnected;
86 /// <param name="aDeviceName"></param>
87 /// <param name="aHdmiPort"></param>
88 public Client(string aDeviceName, byte aHdmiPort, CecDeviceType aDeviceType = CecDeviceType.PlaybackDevice)
90 Config = new LibCECConfiguration();
91 Config.DeviceTypes.Types[0] = aDeviceType;
92 Config.DeviceName = aDeviceName;
93 Config.HDMIPort = aHdmiPort;
94 //Config.ClientVersion = LibCECConfiguration.CurrentVersion;
95 Config.SetCallbacks(this);
97 iLib = new LibCecSharp(Config);
98 iLib.InitVideoStandalone();
102 Trace.WriteLine("WARNING: CEC client static already exists");
110 //Trace.WriteLine("CEC Parser created - libCEC version " + Lib.VersionToString(Config.ServerVersion));
111 Trace.WriteLine("INFO: CEC Parser created - libCEC version " + Config.ServerVersion);
118 /// <param name="alert"></param>
119 /// <param name="data"></param>
120 /// <returns></returns>
121 public override int ReceiveAlert(CecAlert alert, CecParameter data)
123 string log = "CEC alert: " + alert.ToString();
124 if (data != null && data.Type == CecParameterType.ParameterTypeString)
126 log += " " + data.Data;
129 Trace.WriteLine(log);
140 /// <param name="newVal"></param>
141 /// <returns></returns>
142 public override int ReceiveMenuStateChange(CecMenuState newVal)
144 Trace.WriteLine("CEC menu state changed to: " + iLib.ToString(newVal));
151 /// <param name="logicalAddress"></param>
152 /// <param name="activated"></param>
153 public override void SourceActivated(CecLogicalAddress logicalAddress, bool activated)
155 Trace.WriteLine("CEC source activated: " + iLib.ToString(logicalAddress) + "/" + activated.ToString() );
159 public override int ReceiveCommand(CecCommand command)
161 Trace.WriteLine(string.Format("CEC command '{5}' from {0} to {1} - Ack: {2} Eom: {3} OpcodeSet: {4} Timeout: {6}",
162 iLib.ToString(command.Initiator),
163 iLib.ToString(command.Destination),
164 command.Ack.ToString(),
165 command.Eom.ToString(),
166 command.OpcodeSet.ToString(),
167 iLib.ToString(command.Opcode),
168 command.TransmitTimeout.ToString()
173 public override int ReceiveKeypress(CecKeypress key)
175 Trace.WriteLine(string.Format("CEC keypress: {0} Duration:{1} Empty: {2}",
176 key.Keycode.ToString(), key.Duration.ToString(), key.Empty.ToString()));
180 public override int ReceiveLogMessage(CecLogMessage message)
182 if (((int)message.Level & LogLevel) == (int)message.Level)
184 string strLevel = "";
185 switch (message.Level)
187 case CecLogLevel.Error:
188 strLevel = "ERROR: ";
190 case CecLogLevel.Warning:
191 strLevel = "WARNING: ";
193 case CecLogLevel.Notice:
194 strLevel = "NOTICE: ";
196 case CecLogLevel.Traffic:
197 strLevel = "TRAFFIC: ";
199 case CecLogLevel.Debug:
200 strLevel = "DEBUG: ";
201 if (message.Message.IndexOf("POLL") != -1 && FilterOutPollLogs)
203 //We have an option to prevent spamming with poll debug logs
210 string strLog = string.Format("{0} {1} {2}", strLevel, message.Time, message.Message);
211 Trace.WriteLine(strLog);
220 /// <param name="timeout"></param>
221 /// <returns></returns>
222 public bool Open(int timeout)
225 CecAdapter[] adapters = iLib.FindAdapters(string.Empty);
226 if (adapters.Length > 0)
228 Open(adapters[0].ComPort, timeout);
232 Trace.WriteLine("CEC did not find any adapters");
238 public bool Open(string port, int timeout)
241 iConnected = iLib.Open(port, timeout);
252 public void TestSendKey()
254 //SetupMenu: opens option menu
255 //RootMenu: opens Android home menu
259 //Philips TopMenu = 16
260 //Philips PopupMenu = 17
262 //bool res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.DisplayInformation, true);
263 //Thread.Sleep(3000); //Wait few seconds for menu to open
264 //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.SetupMenu, true);
266 for (int i = 0; i < 256; i++)
269 //res = iLib.SendKeyRelease(CecLogicalAddress.Tv, true);
271 switch ((CecUserControlCode)i)
273 case CecUserControlCode.Power:
274 case CecUserControlCode.PowerOffFunction:
275 case CecUserControlCode.PowerOnFunction:
276 case CecUserControlCode.PowerToggleFunction:
277 case CecUserControlCode.ElectronicProgramGuide:
278 case CecUserControlCode.InputSelect:
279 case CecUserControlCode.RootMenu:
284 Trace.WriteLine(i.ToString());
286 iLib.SendKeypress(CecLogicalAddress.Tv, (CecUserControlCode)i, true);
296 for (int i=0;i<7;i++)
299 //res = iLib.SendKeyRelease(CecLogicalAddress.Tv, true);
301 //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.Down, true);
305 //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.Select, true);
307 //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.Select, true);
308 //res = iLib.SendKeyRelease(CecLogicalAddress.Tv, true);
317 scanRes += "CEC bus information\n";
318 scanRes += "===================\n";
319 CecLogicalAddresses addresses = Lib.GetActiveDevices();
320 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
322 CecLogicalAddress address = (CecLogicalAddress) iPtr;
323 if (!addresses.IsSet(address))
326 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
327 bool bActive = Lib.IsActiveDevice(address);
328 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
329 string strAddr = Lib.PhysicalAddressToString(iPhysicalAddress);
330 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
331 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
332 string osdName = Lib.GetDeviceOSDName(address);
333 string lang = Lib.GetDeviceMenuLanguage(address);
335 scanRes += "device #" + iPtr + ": " + Lib.ToString(address) + "\n";
336 scanRes += "address: " + strAddr + "\n";
337 scanRes += "active source: " + (bActive ? "yes" : "no") + "\n";
338 scanRes += "vendor: " + Lib.ToString(iVendorId) + "\n";
339 scanRes += "osd string: " + osdName + "\n";
340 scanRes += "CEC version: " + Lib.ToString(iCecVersion) + "\n";
341 scanRes += "power status: " + Lib.ToString(power) + "\n";
342 if (!string.IsNullOrEmpty(lang))
343 scanRes += "language: " + lang + "\n";
344 scanRes += "===================" + "\n";
347 Trace.Write(scanRes);
350 public void ListAdapters()
353 foreach (CecAdapter adapter in iLib.FindAdapters(string.Empty))
355 Trace.WriteLine("Adapter: " + iAdapter++);
356 Trace.WriteLine("Path: " + adapter.Path);
357 Trace.WriteLine("Com port: " + adapter.ComPort);