cec-config-gui: added some tooltips
[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.ProgressBar, 50);
36 SendEvent(UpdateEventType.StatusText, "Detecting AVR devices...");
37
38 bool hasAVRDevice = Lib.IsActiveDevice(CecLogicalAddress.AudioSystem);
39 SendEvent(UpdateEventType.HasAVRDevice, hasAVRDevice);
40
41 if (hasAVRDevice)
42 {
43 SendEvent(UpdateEventType.ProgressBar, 75);
44 SendEvent(UpdateEventType.StatusText, "Detecting AVR vendor...");
45 SendEvent(UpdateEventType.AVRVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.AudioSystem));
46 }
47
48 if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On))
49 {
50 SendEvent(UpdateEventType.ProgressBar, 80);
51 SendEvent(UpdateEventType.StatusText, "Sending power on command...");
52 Lib.PowerOnDevices(CecLogicalAddress.Tv);
53 }
54
55 SendEvent(UpdateEventType.ProgressBar, 90);
56 SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
57
58 LibCECConfiguration config = new LibCECConfiguration();
59
60 if (!Lib.CanPersistConfiguration())
61 {
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";
68
69 if (File.Exists(file))
70 {
71 XmlTextReader reader = new XmlTextReader(file);
72 while (reader.Read())
73 {
74 gotConfig = true;
75 switch (reader.NodeType)
76 {
77 case XmlNodeType.Element:
78 if (reader.Name.ToLower() == "setting")
79 {
80 string name = string.Empty;
81 string value = string.Empty;
82
83 while (reader.MoveToNextAttribute())
84 {
85 if (reader.Name.ToLower().Equals("id"))
86 name = reader.Value.ToLower();
87 if (reader.Name.ToLower().Equals("value"))
88 value = reader.Value;
89 }
90
91 switch (name)
92 {
93 case "cec_hdmi_port":
94 {
95 byte iPort;
96 if (byte.TryParse(value, out iPort))
97 config.HDMIPort = iPort;
98 }
99 break;
100 case "connected_device":
101 {
102 ushort iDevice;
103 if (ushort.TryParse(value, out iDevice))
104 config.BaseDevice = (CecLogicalAddress)iDevice;
105 }
106 break;
107 case "physical_address":
108 {
109 ushort physicalAddress = 0;
110 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
111 config.PhysicalAddress = physicalAddress;
112 }
113 break;
114 case "device_type":
115 {
116 ushort iType;
117 if (ushort.TryParse(value, out iType))
118 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
119 }
120 break;
121 case "cec_power_on_startup":
122 config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
123 break;
124 case "cec_power_off_shutdown":
125 config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
126 break;
127 case "cec_standby_screensaver":
128 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
129 break;
130 case "standby_pc_on_tv_standby":
131 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
132 break;
133 case "use_tv_menu_language":
134 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
135 break;
136 case "enabled":
137 break;
138 case "port":
139 break;
140 default:
141 break;
142 }
143 }
144 break;
145 default:
146 break;
147 }
148 }
149 }
150
151 if (!gotConfig)
152 Lib.GetCurrentConfiguration(config);
153 }
154 else
155 {
156 Lib.GetCurrentConfiguration(config);
157 }
158 SendEvent(config);
159
160 SendEvent(UpdateEventType.ProgressBar, 100);
161 SendEvent(UpdateEventType.StatusText, "Ready.");
162 }
163
164 private LibCecSharp Lib;
165 }
166 }