cec: added RescanDevices()/cec_rescan_devices() to the interface, to let libCEC force...
[deb_libcec.git] / src / CecSharpTester / CecSharpClient.cs
index 3d8ec32c4588ae593750f7a463f21944a072f67e..006c83ae24e15a72e0063aff54690dfd4867ede2 100644 (file)
 
 using System;
 using System.Collections.Generic;
-using System.Linq;
 using System.Text;
+using CecSharp;
 
 namespace CecSharpClient
 {
-  class CecSharpClient
+  class CecSharpClient : CecCallbackMethods
   {
     public CecSharpClient()
     {
-      CecDeviceTypeList types = new CecDeviceTypeList();
-      types.Types[0] = CecDeviceType.RecordingDevice;
+      Config = new LibCECConfiguration();
+      Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
+      Config.DeviceName = "CEC Tester";
+      Config.ClientVersion = CecClientVersion.Version1_5_0;
+      Config.SetCallbacks(this);
+      LogLevel = (int)CecLogLevel.All;
 
-      Lib = new LibCecSharp("CEC Tester", types);
-      LogLevel = (int) CecLogLevel.All;
+      Lib = new LibCecSharp(Config);
 
-      Console.WriteLine("CEC Parser created - libcec version " + Lib.GetLibVersionMajor() + "." + Lib.GetLibVersionMinor());
+      Console.WriteLine("CEC Parser created - libcec version " + Lib.ToString(Config.ServerVersion));
+    }
+
+    public override int ReceiveCommand(CecCommand command)
+    {
+      return 1;
+    }
+
+    public override int ReceiveKeypress(CecKeypress key)
+    {
+      return 1;
+    }
+
+    public override int ReceiveLogMessage(CecLogMessage message)
+    {
+      if (((int)message.Level & LogLevel) == (int)message.Level)
+      {
+        string strLevel = "";
+        switch (message.Level)
+        {
+          case CecLogLevel.Error:
+            strLevel = "ERROR:   ";
+            break;
+          case CecLogLevel.Warning:
+            strLevel = "WARNING: ";
+            break;
+          case CecLogLevel.Notice:
+            strLevel = "NOTICE:  ";
+            break;
+          case CecLogLevel.Traffic:
+            strLevel = "TRAFFIC: ";
+            break;
+          case CecLogLevel.Debug:
+            strLevel = "DEBUG:   ";
+            break;
+          default:
+            break;
+        }
+        string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
+        Console.WriteLine(strLog);
+      }
+      return 1;
     }
 
     void FlushLog()
@@ -154,17 +198,10 @@ namespace CecSharpClient
 
     public void MainLoop()
     {
-      Lib.PowerOnDevices(CecLogicalAddress.Tv);
-      FlushLog();
-
-      Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
-      FlushLog();
-
       bool bContinue = true;
       string command;
       while (bContinue)
       {
-        FlushLog();
         Console.WriteLine("waiting for input");
 
         command = Console.ReadLine();
@@ -218,7 +255,7 @@ namespace CecSharpClient
         else if (splitCommand[0] == "pa")
         {
           if (splitCommand.Length > 1)
-            Lib.SetPhysicalAddress(short.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
+            Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
         }
         else if (splitCommand[0] == "osd")
         {
@@ -283,11 +320,9 @@ namespace CecSharpClient
         {
           Console.WriteLine("closing the connection");
           Lib.Close();
-          FlushLog();
 
           Console.WriteLine("opening a new connection");
           Connect(10000);
-          FlushLog();
 
           Console.WriteLine("setting active source");
           Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
@@ -297,7 +332,7 @@ namespace CecSharpClient
           Console.WriteLine("CEC bus information");
           Console.WriteLine("===================");
           CecLogicalAddresses addresses = Lib.GetActiveDevices();
-          for (int iPtr = 0; iPtr < addresses.Addresses.Count(); iPtr++)
+          for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
           {
             CecLogicalAddress address = (CecLogicalAddress)iPtr;
             if (!addresses.IsSet(address))
@@ -346,10 +381,10 @@ namespace CecSharpClient
       {
         Console.WriteLine("Could not open a connection to the CEC adapter");
       }
-      p.FlushLog();
     }
 
-    private int         LogLevel;
-    private LibCecSharp Lib;
+    private int                 LogLevel;
+    private LibCecSharp         Lib;
+    private LibCECConfiguration Config;
   }
 }