2 using System.Collections.Generic;
5 using System.Windows.Forms;
8 using System.Globalization;
10 namespace CecConfigGui.actions
12 class ConnectToDevice : UpdateProcess
14 public ConnectToDevice(ref LibCecSharp lib)
19 public override void Process()
21 SendEvent(UpdateEventType.StatusText, "Connecting to the CEC adapter...");
22 SendEvent(UpdateEventType.ProgressBar, 0);
24 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
25 if (adapters.Length == 0 || !Lib.Open(adapters[0].ComPort, 10000))
27 MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
31 SendEvent(UpdateEventType.StatusText, "Detecting TV vendor...");
32 SendEvent(UpdateEventType.ProgressBar, 25);
33 SendEvent(UpdateEventType.TVVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.Tv));
35 SendEvent(UpdateEventType.ProgressBar, 50);
36 SendEvent(UpdateEventType.StatusText, "Detecting AVR devices...");
38 bool hasAVRDevice = Lib.IsActiveDevice(CecLogicalAddress.AudioSystem);
39 SendEvent(UpdateEventType.HasAVRDevice, hasAVRDevice);
43 SendEvent(UpdateEventType.ProgressBar, 75);
44 SendEvent(UpdateEventType.StatusText, "Detecting AVR vendor...");
45 SendEvent(UpdateEventType.AVRVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.AudioSystem));
48 if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On))
50 SendEvent(UpdateEventType.ProgressBar, 80);
51 SendEvent(UpdateEventType.StatusText, "Sending power on command...");
52 Lib.PowerOnDevices(CecLogicalAddress.Tv);
55 SendEvent(UpdateEventType.ProgressBar, 90);
56 SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
58 LibCECConfiguration config = new LibCECConfiguration();
60 if (!Lib.CanPersistConfiguration())
62 bool gotConfig = false;
63 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
64 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
65 string file = defaultDir + @"\usb_2548_1001.xml";
66 if (File.Exists(xbmcDir + @"\usb_2548_1001.xml"))
67 file = xbmcDir + @"\usb_2548_1001.xml";
69 if (File.Exists(file))
71 XmlTextReader reader = new XmlTextReader(file);
75 switch (reader.NodeType)
77 case XmlNodeType.Element:
78 if (reader.Name.ToLower() == "setting")
80 string name = string.Empty;
81 string value = string.Empty;
83 while (reader.MoveToNextAttribute())
85 if (reader.Name.ToLower().Equals("id"))
86 name = reader.Value.ToLower();
87 if (reader.Name.ToLower().Equals("value"))
96 if (byte.TryParse(value, out iPort))
97 config.HDMIPort = iPort;
100 case "connected_device":
103 if (ushort.TryParse(value, out iDevice))
104 config.BaseDevice = (CecLogicalAddress)iDevice;
107 case "physical_address":
109 ushort physicalAddress = 0;
110 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
111 config.PhysicalAddress = physicalAddress;
117 if (ushort.TryParse(value, out iType))
118 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
121 case "cec_power_on_startup":
122 config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
124 case "cec_power_off_shutdown":
125 config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
127 case "cec_standby_screensaver":
128 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
130 case "standby_pc_on_tv_standby":
131 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
133 case "use_tv_menu_language":
134 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
152 Lib.GetCurrentConfiguration(config);
156 Lib.GetCurrentConfiguration(config);
160 SendEvent(UpdateEventType.ProgressBar, 100);
161 SendEvent(UpdateEventType.StatusText, "Ready.");
164 private LibCecSharp Lib;