cec-config-gui: display the detected menu language
[deb_libcec.git] / src / cec-config-gui / actions / ConnectToDevice.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using CecSharp;
5 using System.Windows.Forms;
6 using System.IO;
7 using System.Xml;
8 using System.Globalization;
9
10 namespace CecConfigGui.actions
11 {
12 class ConnectToDevice : UpdateProcess
13 {
14 public ConnectToDevice(ref LibCecSharp lib)
15 {
16 Lib = lib;
17 }
18
19 public override void Process()
20 {
21 SendEvent(UpdateEventType.StatusText, "Connecting to the CEC adapter...");
22 SendEvent(UpdateEventType.ProgressBar, 0);
23
24 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
25 if (adapters.Length == 0 || !Lib.Open(adapters[0].ComPort, 10000))
26 {
27 MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
28 Application.Exit();
29 }
30
31 SendEvent(UpdateEventType.StatusText, "Detecting TV vendor...");
32 SendEvent(UpdateEventType.ProgressBar, 25);
33 SendEvent(UpdateEventType.TVVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.Tv));
34
35 SendEvent(UpdateEventType.StatusText, "Detecting menu language...");
36 SendEvent(UpdateEventType.ProgressBar, 40);
37 SendEvent(UpdateEventType.MenuLanguage, Lib.GetDeviceMenuLanguage(CecLogicalAddress.Tv));
38
39 SendEvent(UpdateEventType.ProgressBar, 50);
40 SendEvent(UpdateEventType.StatusText, "Detecting AVR devices...");
41
42 bool hasAVRDevice = Lib.IsActiveDevice(CecLogicalAddress.AudioSystem);
43 SendEvent(UpdateEventType.HasAVRDevice, hasAVRDevice);
44
45 if (hasAVRDevice)
46 {
47 SendEvent(UpdateEventType.ProgressBar, 75);
48 SendEvent(UpdateEventType.StatusText, "Detecting AVR vendor...");
49 SendEvent(UpdateEventType.AVRVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.AudioSystem));
50 }
51
52 if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On))
53 {
54 SendEvent(UpdateEventType.ProgressBar, 80);
55 SendEvent(UpdateEventType.StatusText, "Sending power on command...");
56 Lib.PowerOnDevices(CecLogicalAddress.Tv);
57 }
58
59 SendEvent(UpdateEventType.ProgressBar, 90);
60 SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
61
62 LibCECConfiguration config = new LibCECConfiguration();
63
64 if (!Lib.CanPersistConfiguration())
65 {
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";
72
73 if (File.Exists(file))
74 {
75 XmlTextReader reader = new XmlTextReader(file);
76 while (reader.Read())
77 {
78 gotConfig = true;
79 switch (reader.NodeType)
80 {
81 case XmlNodeType.Element:
82 if (reader.Name.ToLower() == "setting")
83 {
84 string name = string.Empty;
85 string value = string.Empty;
86
87 while (reader.MoveToNextAttribute())
88 {
89 if (reader.Name.ToLower().Equals("id"))
90 name = reader.Value.ToLower();
91 if (reader.Name.ToLower().Equals("value"))
92 value = reader.Value;
93 }
94
95 switch (name)
96 {
97 case "cec_hdmi_port":
98 {
99 byte iPort;
100 if (byte.TryParse(value, out iPort))
101 config.HDMIPort = iPort;
102 }
103 break;
104 case "connected_device":
105 {
106 ushort iDevice;
107 if (ushort.TryParse(value, out iDevice))
108 config.BaseDevice = (CecLogicalAddress)iDevice;
109 }
110 break;
111 case "physical_address":
112 {
113 ushort physicalAddress = 0;
114 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
115 config.PhysicalAddress = physicalAddress;
116 }
117 break;
118 case "device_type":
119 {
120 ushort iType;
121 if (ushort.TryParse(value, out iType))
122 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
123 }
124 break;
125 case "cec_power_on_startup":
126 config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
127 break;
128 case "cec_power_off_shutdown":
129 config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
130 break;
131 case "cec_standby_screensaver":
132 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
133 break;
134 case "standby_pc_on_tv_standby":
135 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
136 break;
137 case "use_tv_menu_language":
138 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
139 break;
140 case "enabled":
141 break;
142 case "port":
143 break;
144 default:
145 break;
146 }
147 }
148 break;
149 default:
150 break;
151 }
152 }
153 }
154
155 if (!gotConfig)
156 Lib.GetCurrentConfiguration(config);
157 }
158 else
159 {
160 Lib.GetCurrentConfiguration(config);
161 }
162 SendEvent(config);
163
164 SendEvent(UpdateEventType.ProgressBar, 100);
165 SendEvent(UpdateEventType.StatusText, "Ready.");
166 }
167
168 private LibCecSharp Lib;
169 }
170 }