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.StatusText, "Detecting menu language...");
36 SendEvent(UpdateEventType.ProgressBar, 40);
37 SendEvent(UpdateEventType.MenuLanguage, Lib.GetDeviceMenuLanguage(CecLogicalAddress.Tv));
39 SendEvent(UpdateEventType.ProgressBar, 50);
40 SendEvent(UpdateEventType.StatusText, "Detecting AVR devices...");
42 bool hasAVRDevice = Lib.IsActiveDevice(CecLogicalAddress.AudioSystem);
43 SendEvent(UpdateEventType.HasAVRDevice, hasAVRDevice);
47 SendEvent(UpdateEventType.ProgressBar, 75);
48 SendEvent(UpdateEventType.StatusText, "Detecting AVR vendor...");
49 SendEvent(UpdateEventType.AVRVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.AudioSystem));
52 if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On))
54 SendEvent(UpdateEventType.ProgressBar, 80);
55 SendEvent(UpdateEventType.StatusText, "Sending power on command...");
56 Lib.PowerOnDevices(CecLogicalAddress.Tv);
59 SendEvent(UpdateEventType.ProgressBar, 90);
60 SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
62 LibCECConfiguration config = new LibCECConfiguration();
64 if (!Lib.CanPersistConfiguration())
66 bool gotConfig = false;
67 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
68 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
69 string file = defaultDir + @"\usb_2548_1001.xml";
70 if (File.Exists(xbmcDir + @"\usb_2548_1001.xml"))
71 file = xbmcDir + @"\usb_2548_1001.xml";
73 if (File.Exists(file))
75 XmlTextReader reader = new XmlTextReader(file);
79 switch (reader.NodeType)
81 case XmlNodeType.Element:
82 if (reader.Name.ToLower() == "setting")
84 string name = string.Empty;
85 string value = string.Empty;
87 while (reader.MoveToNextAttribute())
89 if (reader.Name.ToLower().Equals("id"))
90 name = reader.Value.ToLower();
91 if (reader.Name.ToLower().Equals("value"))
100 if (byte.TryParse(value, out iPort))
101 config.HDMIPort = iPort;
104 case "connected_device":
107 if (ushort.TryParse(value, out iDevice))
108 config.BaseDevice = (CecLogicalAddress)iDevice;
111 case "physical_address":
113 ushort physicalAddress = 0;
114 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
115 config.PhysicalAddress = physicalAddress;
121 if (ushort.TryParse(value, out iType))
122 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
125 case "cec_power_on_startup":
126 config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
128 case "cec_power_off_shutdown":
129 config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
131 case "cec_standby_screensaver":
132 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
134 case "standby_pc_on_tv_standby":
135 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
137 case "use_tv_menu_language":
138 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
156 Lib.GetCurrentConfiguration(config);
160 Lib.GetCurrentConfiguration(config);
164 SendEvent(UpdateEventType.ProgressBar, 100);
165 SendEvent(UpdateEventType.StatusText, "Ready.");
168 private LibCecSharp Lib;