cec-config-gui: read the configuration before initialising libCEC
authorLars Op den Kamp <lars@opdenkamp.eu>
Tue, 14 Feb 2012 13:42:24 +0000 (14:42 +0100)
committerLars Op den Kamp <lars@opdenkamp.eu>
Tue, 14 Feb 2012 13:42:24 +0000 (14:42 +0100)
src/cec-config-gui/CecConfigGUI.cs
src/cec-config-gui/actions/ConnectToDevice.cs

index 10a484411cea0d143b390980cdc22081c81d08be..031c7e8fe4cb32f6506ae1a19b1329a1cd620719 100644 (file)
@@ -10,6 +10,7 @@ using CecSharp;
 using CecConfigGui.actions;
 using System.Globalization;
 using System.IO;
+using System.Xml;
 
 namespace CecConfigGui
 {
@@ -24,17 +25,119 @@ namespace CecConfigGui
       Config.ClientVersion = CecClientVersion.Version1_5_0;
       Callbacks = new CecCallbackWrapper(this);
       Config.SetCallbacks(Callbacks);
-
-      InitializeComponent();
+      LoadXMLConfiguration(ref Config);
       Lib = new LibCecSharp(Config);
 
+      InitializeComponent();
       LoadButtonConfiguration();
 
-      ActiveProcess = new ConnectToDevice(ref Lib);
+      //TODO read the com port setting from the configuration
+      CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
+      if (adapters.Length == 0 || !Lib.Open(adapters[0].ComPort, 10000))
+      {
+        MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
+        Application.Exit();
+      }
+
+      ActiveProcess = new ConnectToDevice(ref Lib, Config);
       ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
       (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
     }
 
+    private bool LoadXMLConfiguration(ref LibCECConfiguration config)
+    {
+      bool gotConfig = false;
+      string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
+      string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
+      string file = defaultDir + @"\usb_2548_1001.xml";
+      if (File.Exists(xbmcDir + @"\usb_2548_1001.xml"))
+        file = xbmcDir + @"\usb_2548_1001.xml";
+
+      if (File.Exists(file))
+      {
+        XmlTextReader reader = new XmlTextReader(file);
+        while (reader.Read())
+        {
+          gotConfig = true;
+          switch (reader.NodeType)
+          {
+            case XmlNodeType.Element:
+              if (reader.Name.ToLower() == "setting")
+              {
+                string name = string.Empty;
+                string value = string.Empty;
+
+                while (reader.MoveToNextAttribute())
+                {
+                  if (reader.Name.ToLower().Equals("id"))
+                    name = reader.Value.ToLower();
+                  if (reader.Name.ToLower().Equals("value"))
+                    value = reader.Value;
+                }
+
+                switch (name)
+                {
+                  case "cec_hdmi_port":
+                    {
+                      byte iPort;
+                      if (byte.TryParse(value, out iPort))
+                        config.HDMIPort = iPort;
+                    }
+                    break;
+                  case "connected_device":
+                    {
+                      ushort iDevice;
+                      if (ushort.TryParse(value, out iDevice))
+                        config.BaseDevice = (CecLogicalAddress)iDevice;
+                    }
+                    break;
+                  case "physical_address":
+                    {
+                      ushort physicalAddress = 0;
+                      if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
+                        config.PhysicalAddress = physicalAddress;
+                    }
+                    break;
+                  case "device_type":
+                    {
+                      ushort iType;
+                      if (ushort.TryParse(value, out iType))
+                        config.DeviceTypes.Types[0] = (CecDeviceType)iType;
+                    }
+                    break;
+                  case "cec_power_on_startup":
+                    config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "cec_power_off_shutdown":
+                    config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "cec_standby_screensaver":
+                    config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "standby_pc_on_tv_standby":
+                    config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "use_tv_menu_language":
+                    config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "enabled":
+                    break;
+                  case "port":
+                    //TODO
+                    break;
+                  default:
+                    break;
+                }
+              }
+              break;
+            default:
+              break;
+          }
+        }
+      }
+      return gotConfig;
+    }
+
     private void LoadButtonConfiguration()
     {
       //TODO load the real configuration
index 9250ad09b73a7fc9c447abee32325f8e79dc4a58..cb84a00b403d6b75b22087f984d26f47fd2f2bde 100644 (file)
@@ -1,33 +1,18 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-using CecSharp;
+using CecSharp;
 using System.Windows.Forms;
-using System.IO;
-using System.Xml;
-using System.Globalization;
 
 namespace CecConfigGui.actions
 {
   class ConnectToDevice : UpdateProcess
   {
-    public ConnectToDevice(ref LibCecSharp lib)
+    public ConnectToDevice(ref LibCecSharp lib, LibCECConfiguration config)
     {
       Lib = lib;
+      Config = config;
     }
 
     public override void Process()
     {
-      SendEvent(UpdateEventType.StatusText, "Connecting to the CEC adapter...");
-      SendEvent(UpdateEventType.ProgressBar, 0);
-
-      CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
-      if (adapters.Length == 0 || !Lib.Open(adapters[0].ComPort, 10000))
-      {
-        MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
-        Application.Exit();
-      }
-
       SendEvent(UpdateEventType.StatusText, "Detecting TV vendor...");
       SendEvent(UpdateEventType.ProgressBar, 25);
       SendEvent(UpdateEventType.TVVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.Tv));
@@ -52,119 +37,21 @@ namespace CecConfigGui.actions
       if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On))
       {
         SendEvent(UpdateEventType.ProgressBar, 80);
-        SendEvent(UpdateEventType.StatusText, "Sending power on command...");
-        Lib.PowerOnDevices(CecLogicalAddress.Tv);
+        SendEvent(UpdateEventType.StatusText, "Activating the source...");
+        Lib.SetActiveSource(CecDeviceType.Reserved);
       }
 
       SendEvent(UpdateEventType.ProgressBar, 90);
       SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
 
-      LibCECConfiguration config = new LibCECConfiguration();
-
-      if (!Lib.CanPersistConfiguration())
-      {
-        bool gotConfig = false;
-        string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
-        string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
-        string file = defaultDir + @"\usb_2548_1001.xml";
-        if (File.Exists(xbmcDir + @"\usb_2548_1001.xml"))
-          file = xbmcDir + @"\usb_2548_1001.xml";
-
-        if (File.Exists(file))
-        {
-          XmlTextReader reader = new XmlTextReader(file);
-          while (reader.Read())
-          {
-            gotConfig = true;
-            switch (reader.NodeType)
-            {
-              case XmlNodeType.Element:
-                if (reader.Name.ToLower() == "setting")
-                {
-                  string name = string.Empty;
-                  string value = string.Empty;
-
-                  while (reader.MoveToNextAttribute())
-                  {
-                    if (reader.Name.ToLower().Equals("id"))
-                      name = reader.Value.ToLower();
-                    if (reader.Name.ToLower().Equals("value"))
-                      value = reader.Value;
-                  }
-
-                  switch (name)
-                  {
-                    case "cec_hdmi_port":
-                      {
-                        byte iPort;
-                        if (byte.TryParse(value, out iPort))
-                          config.HDMIPort = iPort;
-                      }
-                      break;
-                    case "connected_device":
-                      {
-                        ushort iDevice;
-                        if (ushort.TryParse(value, out iDevice))
-                          config.BaseDevice = (CecLogicalAddress)iDevice;
-                      }
-                      break;
-                    case "physical_address":
-                      {
-                        ushort physicalAddress = 0;
-                        if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
-                          config.PhysicalAddress = physicalAddress;
-                      }
-                      break;
-                    case "device_type":
-                      {
-                        ushort iType;
-                        if (ushort.TryParse(value, out iType))
-                          config.DeviceTypes.Types[0] = (CecDeviceType)iType;
-                      }
-                      break;
-                    case "cec_power_on_startup":
-                      config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "cec_power_off_shutdown":
-                      config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "cec_standby_screensaver":
-                      config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "standby_pc_on_tv_standby":
-                      config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "use_tv_menu_language":
-                      config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
-                      break;
-                    case "enabled":
-                      break;
-                    case "port":
-                      break;
-                    default:
-                      break;
-                  }
-                }
-                break;
-              default:
-                break;
-            }
-          }
-        }
-
-        if (!gotConfig)
-          Lib.GetCurrentConfiguration(config);
-      }
-      else
-      {
-        Lib.GetCurrentConfiguration(config);
-      }
-      SendEvent(config);
+      Lib.GetCurrentConfiguration(Config);
+      SendEvent(Config);
 
       SendEvent(UpdateEventType.ProgressBar, 100);
       SendEvent(UpdateEventType.StatusText, "Ready.");
     }
 
     private LibCecSharp Lib;
+    private LibCECConfiguration Config;
   }
 }