From 4555ed72f3a69812ad5481eabc94d54644ca549a Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Tue, 14 Feb 2012 14:42:24 +0100 Subject: [PATCH] cec-config-gui: read the configuration before initialising libCEC --- src/cec-config-gui/CecConfigGUI.cs | 109 ++++++++++++++- src/cec-config-gui/actions/ConnectToDevice.cs | 129 ++---------------- 2 files changed, 114 insertions(+), 124 deletions(-) diff --git a/src/cec-config-gui/CecConfigGUI.cs b/src/cec-config-gui/CecConfigGUI.cs index 10a4844..031c7e8 100644 --- a/src/cec-config-gui/CecConfigGUI.cs +++ b/src/cec-config-gui/CecConfigGUI.cs @@ -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(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 diff --git a/src/cec-config-gui/actions/ConnectToDevice.cs b/src/cec-config-gui/actions/ConnectToDevice.cs index 9250ad0..cb84a00 100644 --- a/src/cec-config-gui/actions/ConnectToDevice.cs +++ b/src/cec-config-gui/actions/ConnectToDevice.cs @@ -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; } } -- 2.34.1