Commit | Line | Data |
---|---|---|
006b76b9 LOK |
1 | using System; |
2 | using System.Collections.Generic; | |
006b76b9 LOK |
3 | using System.Drawing; |
4 | using System.Text; | |
5 | using System.Windows.Forms; | |
6 | using System.Threading; | |
7 | using CecSharp; | |
8 | using CecConfigGui.actions; | |
9 | using System.Globalization; | |
10 | using System.IO; | |
4555ed72 | 11 | using System.Xml; |
006b76b9 LOK |
12 | |
13 | namespace CecConfigGui | |
14 | { | |
5de1dde4 LOK |
15 | internal enum ConfigTab |
16 | { | |
17 | Configuration, | |
18 | KeyConfiguration, | |
19 | Tester, | |
20 | Log | |
21 | } | |
22 | ||
75af24f1 | 23 | public partial class CecConfigGUI : AsyncForm |
006b76b9 LOK |
24 | { |
25 | public CecConfigGUI() | |
26 | { | |
27 | Config = new LibCECConfiguration(); | |
28 | Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice; | |
29 | Config.DeviceName = "CEC Config"; | |
30 | Config.GetSettingsFromROM = true; | |
31 | Config.ClientVersion = CecClientVersion.Version1_5_0; | |
32 | Callbacks = new CecCallbackWrapper(this); | |
33 | Config.SetCallbacks(Callbacks); | |
4555ed72 | 34 | LoadXMLConfiguration(ref Config); |
006b76b9 LOK |
35 | Lib = new LibCecSharp(Config); |
36 | ||
4555ed72 | 37 | InitializeComponent(); |
8674df6a LOK |
38 | LoadButtonConfiguration(); |
39 | ||
4555ed72 | 40 | ActiveProcess = new ConnectToDevice(ref Lib, Config); |
5cf99fae LOK |
41 | ActiveProcess.EventHandler += ProcessEventHandler; |
42 | (new Thread(ActiveProcess.Run)).Start(); | |
006b76b9 LOK |
43 | } |
44 | ||
4555ed72 LOK |
45 | private bool LoadXMLConfiguration(ref LibCECConfiguration config) |
46 | { | |
47 | bool gotConfig = false; | |
48 | string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data"; | |
49 | string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); | |
50 | string file = defaultDir + @"\usb_2548_1001.xml"; | |
51 | if (File.Exists(xbmcDir + @"\usb_2548_1001.xml")) | |
52 | file = xbmcDir + @"\usb_2548_1001.xml"; | |
53 | ||
54 | if (File.Exists(file)) | |
55 | { | |
56 | XmlTextReader reader = new XmlTextReader(file); | |
57 | while (reader.Read()) | |
58 | { | |
59 | gotConfig = true; | |
60 | switch (reader.NodeType) | |
61 | { | |
62 | case XmlNodeType.Element: | |
63 | if (reader.Name.ToLower() == "setting") | |
64 | { | |
65 | string name = string.Empty; | |
66 | string value = string.Empty; | |
67 | ||
68 | while (reader.MoveToNextAttribute()) | |
69 | { | |
70 | if (reader.Name.ToLower().Equals("id")) | |
71 | name = reader.Value.ToLower(); | |
72 | if (reader.Name.ToLower().Equals("value")) | |
73 | value = reader.Value; | |
74 | } | |
75 | ||
76 | switch (name) | |
77 | { | |
78 | case "cec_hdmi_port": | |
79 | { | |
80 | byte iPort; | |
81 | if (byte.TryParse(value, out iPort)) | |
82 | config.HDMIPort = iPort; | |
83 | } | |
84 | break; | |
85 | case "connected_device": | |
86 | { | |
87 | ushort iDevice; | |
88 | if (ushort.TryParse(value, out iDevice)) | |
89 | config.BaseDevice = (CecLogicalAddress)iDevice; | |
90 | } | |
91 | break; | |
75af24f1 LOK |
92 | case "cec_power_on_startup": |
93 | if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes")) | |
94 | { | |
95 | config.ActivateSource = true; | |
96 | config.WakeDevices.Set(CecLogicalAddress.Tv); | |
97 | } | |
98 | break; | |
99 | case "cec_power_off_shutdown": | |
100 | if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes")) | |
101 | config.PowerOffDevices.Set(CecLogicalAddress.Broadcast); | |
102 | break; | |
103 | case "cec_standby_screensaver": | |
104 | config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); | |
105 | break; | |
106 | case "standby_pc_on_tv_standby": | |
107 | config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); | |
108 | break; | |
109 | case "use_tv_menu_language": | |
110 | config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); | |
111 | break; | |
112 | // 1.5.0+ settings | |
4555ed72 LOK |
113 | case "physical_address": |
114 | { | |
115 | ushort physicalAddress = 0; | |
116 | if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress)) | |
117 | config.PhysicalAddress = physicalAddress; | |
118 | } | |
119 | break; | |
120 | case "device_type": | |
121 | { | |
122 | ushort iType; | |
123 | if (ushort.TryParse(value, out iType)) | |
124 | config.DeviceTypes.Types[0] = (CecDeviceType)iType; | |
125 | } | |
126 | break; | |
75af24f1 LOK |
127 | case "tv_vendor": |
128 | { | |
129 | UInt64 iVendor; | |
130 | if (UInt64.TryParse(value, out iVendor)) | |
131 | config.TvVendor = (CecVendorId)iVendor; | |
132 | } | |
4555ed72 | 133 | break; |
75af24f1 LOK |
134 | case "wake_devices": |
135 | { | |
136 | config.WakeDevices.Clear(); | |
5cf99fae | 137 | string[] split = value.Split(new[] { ' ' }); |
75af24f1 LOK |
138 | foreach (string dev in split) |
139 | { | |
140 | byte iLogicalAddress; | |
141 | if (byte.TryParse(dev, out iLogicalAddress)) | |
142 | config.WakeDevices.Set((CecLogicalAddress)iLogicalAddress); | |
143 | } | |
144 | } | |
4555ed72 | 145 | break; |
75af24f1 LOK |
146 | case "standby_devices": |
147 | { | |
148 | config.PowerOffDevices.Clear(); | |
149 | string[] split = value.Split(new char[] { ' ' }); | |
150 | foreach (string dev in split) | |
151 | { | |
152 | byte iLogicalAddress; | |
153 | if (byte.TryParse(dev, out iLogicalAddress)) | |
154 | config.PowerOffDevices.Set((CecLogicalAddress)iLogicalAddress); | |
155 | } | |
156 | } | |
4555ed72 LOK |
157 | break; |
158 | case "enabled": | |
159 | break; | |
160 | case "port": | |
161 | //TODO | |
162 | break; | |
163 | default: | |
164 | break; | |
165 | } | |
166 | } | |
167 | break; | |
168 | default: | |
169 | break; | |
170 | } | |
171 | } | |
172 | } | |
173 | return gotConfig; | |
174 | } | |
175 | ||
8674df6a LOK |
176 | private void LoadButtonConfiguration() |
177 | { | |
178 | //TODO load the real configuration | |
5cf99fae LOK |
179 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecKeypress { Keycode = 0x00 }), string.Empty)); |
180 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecKeypress { Keycode = 0x01 }), string.Empty)); | |
181 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecKeypress { Keycode = 0x02 }), string.Empty)); | |
182 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecKeypress { Keycode = 0x03 }), string.Empty)); | |
183 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecKeypress { Keycode = 0x04 }), string.Empty)); | |
184 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecKeypress { Keycode = 0x05 }), string.Empty)); | |
185 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecKeypress { Keycode = 0x06 }), string.Empty)); | |
186 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecKeypress { Keycode = 0x07 }), string.Empty)); | |
187 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecKeypress { Keycode = 0x08 }), string.Empty)); | |
188 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecKeypress { Keycode = 0x09 }), string.Empty)); | |
189 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecKeypress { Keycode = 0x0A }), string.Empty)); | |
190 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecKeypress { Keycode = 0x0B }), string.Empty)); | |
191 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecKeypress { Keycode = 0x0C }), string.Empty)); | |
192 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecKeypress { Keycode = 0x0D }), string.Empty)); | |
193 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecKeypress { Keycode = 0x20 }), string.Empty)); | |
194 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecKeypress { Keycode = 0x21 }), string.Empty)); | |
195 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecKeypress { Keycode = 0x22 }), string.Empty)); | |
196 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecKeypress { Keycode = 0x23 }), string.Empty)); | |
197 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecKeypress { Keycode = 0x24 }), string.Empty)); | |
198 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecKeypress { Keycode = 0x25 }), string.Empty)); | |
199 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecKeypress { Keycode = 0x26 }), string.Empty)); | |
200 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecKeypress { Keycode = 0x27 }), string.Empty)); | |
201 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecKeypress { Keycode = 0x28 }), string.Empty)); | |
202 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecKeypress { Keycode = 0x29 }), string.Empty)); | |
203 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecKeypress { Keycode = 0x2A }), string.Empty)); | |
204 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecKeypress { Keycode = 0x2B }), string.Empty)); | |
205 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecKeypress { Keycode = 0x2C }), string.Empty)); | |
206 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecKeypress { Keycode = 0x2F }), string.Empty)); | |
207 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecKeypress { Keycode = 0x30 }), string.Empty)); | |
208 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecKeypress { Keycode = 0x31 }), string.Empty)); | |
209 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecKeypress { Keycode = 0x32 }), string.Empty)); | |
210 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecKeypress { Keycode = 0x33 }), string.Empty)); | |
211 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecKeypress { Keycode = 0x34 }), string.Empty)); | |
212 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecKeypress { Keycode = 0x35 }), string.Empty)); | |
213 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecKeypress { Keycode = 0x36 }), string.Empty)); | |
214 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecKeypress { Keycode = 0x37 }), string.Empty)); | |
215 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecKeypress { Keycode = 0x38 }), string.Empty)); | |
216 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecKeypress { Keycode = 0x40 }), string.Empty)); | |
217 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecKeypress { Keycode = 0x41 }), string.Empty)); | |
218 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecKeypress { Keycode = 0x42 }), string.Empty)); | |
219 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecKeypress { Keycode = 0x43 }), string.Empty)); | |
220 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecKeypress { Keycode = 0x44 }), string.Empty)); | |
221 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecKeypress { Keycode = 0x45 }), string.Empty)); | |
222 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecKeypress { Keycode = 0x46 }), string.Empty)); | |
223 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecKeypress { Keycode = 0x47 }), string.Empty)); | |
224 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecKeypress { Keycode = 0x48 }), string.Empty)); | |
225 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecKeypress { Keycode = 0x49 }), string.Empty)); | |
226 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecKeypress { Keycode = 0x4A }), string.Empty)); | |
227 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecKeypress { Keycode = 0x4B }), string.Empty)); | |
228 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecKeypress { Keycode = 0x4C }), string.Empty)); | |
229 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecKeypress { Keycode = 0x4D }), string.Empty)); | |
230 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecKeypress { Keycode = 0x4E }), string.Empty)); | |
231 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecKeypress { Keycode = 0x50 }), string.Empty)); | |
232 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecKeypress { Keycode = 0x51 }), string.Empty)); | |
233 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecKeypress { Keycode = 0x52 }), string.Empty)); | |
234 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecKeypress { Keycode = 0x53 }), string.Empty)); | |
235 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecKeypress { Keycode = 0x54 }), string.Empty)); | |
236 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecKeypress { Keycode = 0x55 }), string.Empty)); | |
237 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecKeypress { Keycode = 0x60 }), string.Empty)); | |
238 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecKeypress { Keycode = 0x61 }), string.Empty)); | |
239 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecKeypress { Keycode = 0x62 }), string.Empty)); | |
240 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecKeypress { Keycode = 0x63 }), string.Empty)); | |
241 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecKeypress { Keycode = 0x64 }), string.Empty)); | |
242 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecKeypress { Keycode = 0x65 }), string.Empty)); | |
243 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecKeypress { Keycode = 0x66 }), string.Empty)); | |
244 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecKeypress { Keycode = 0x67 }), string.Empty)); | |
245 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecKeypress { Keycode = 0x68 }), string.Empty)); | |
246 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecKeypress { Keycode = 0x69 }), string.Empty)); | |
247 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecKeypress { Keycode = 0x6A }), string.Empty)); | |
248 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecKeypress { Keycode = 0x6B }), string.Empty)); | |
249 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecKeypress { Keycode = 0x6C }), string.Empty)); | |
250 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecKeypress { Keycode = 0x6D }), string.Empty)); | |
251 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecKeypress { Keycode = 0x71 }), string.Empty)); | |
252 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecKeypress { Keycode = 0x72 }), string.Empty)); | |
253 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecKeypress { Keycode = 0x73 }), string.Empty)); | |
254 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecKeypress { Keycode = 0x74 }), string.Empty)); | |
255 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecKeypress { Keycode = 0x75 }), string.Empty)); | |
256 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecKeypress { Keycode = 0x76 }), string.Empty)); | |
257 | cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecKeypress { Keycode = 0x91 }), string.Empty)); | |
8674df6a LOK |
258 | } |
259 | ||
75af24f1 | 260 | private void ProcessEventHandler(object src, UpdateEvent updateEvent) |
8674df6a | 261 | { |
75af24f1 | 262 | switch (updateEvent.Type) |
8674df6a | 263 | { |
75af24f1 LOK |
264 | case UpdateEventType.StatusText: |
265 | SetControlText(lStatus, updateEvent.StringValue); | |
266 | break; | |
267 | case UpdateEventType.PhysicalAddress: | |
268 | Config.PhysicalAddress = (ushort)updateEvent.IntValue; | |
269 | SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue)); | |
270 | break; | |
271 | case UpdateEventType.ProgressBar: | |
68b94c34 | 272 | SetControlVisible(pProgress, true); |
75af24f1 LOK |
273 | SetProgressValue(pProgress, updateEvent.IntValue); |
274 | break; | |
275 | case UpdateEventType.TVVendorId: | |
276 | TVVendor = (CecVendorId)updateEvent.IntValue; | |
277 | UpdateSelectedDevice(); | |
278 | break; | |
279 | case UpdateEventType.BaseDevicePhysicalAddress: | |
280 | SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue)); | |
281 | break; | |
282 | case UpdateEventType.BaseDevice: | |
283 | Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue; | |
284 | break; | |
285 | case UpdateEventType.HDMIPort: | |
286 | Config.HDMIPort = (byte)updateEvent.IntValue; | |
287 | break; | |
288 | case UpdateEventType.MenuLanguage: | |
289 | SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : "")); | |
290 | break; | |
291 | case UpdateEventType.HasAVRDevice: | |
292 | if (HasAVRDevice != updateEvent.BoolValue) | |
8674df6a | 293 | { |
75af24f1 LOK |
294 | HasAVRDevice = updateEvent.BoolValue; |
295 | UpdateSelectedDevice(); | |
8674df6a | 296 | } |
75af24f1 LOK |
297 | break; |
298 | case UpdateEventType.AVRVendorId: | |
299 | AVRVendor = (CecVendorId)updateEvent.IntValue; | |
300 | UpdateSelectedDevice(); | |
301 | break; | |
302 | case UpdateEventType.Configuration: | |
303 | SuppressUpdates = true; | |
304 | ConfigurationChanged(updateEvent.ConfigValue); | |
305 | SuppressUpdates = false; | |
306 | break; | |
6d866874 LOK |
307 | case UpdateEventType.PollDevices: |
308 | CheckActiveDevices(); | |
309 | break; | |
75af24f1 LOK |
310 | case UpdateEventType.ProcessCompleted: |
311 | ActiveProcess = null; | |
312 | SetControlsEnabled(true); | |
6d866874 LOK |
313 | if (UpdatingInfoPanel != null) |
314 | { | |
315 | UpdatingInfoPanel.SetControlEnabled(UpdatingInfoPanel.bUpdate, true); | |
316 | UpdatingInfoPanel = null; | |
317 | } | |
68b94c34 | 318 | SetControlVisible(pProgress, false); |
75af24f1 | 319 | break; |
8674df6a LOK |
320 | } |
321 | } | |
322 | ||
5de1dde4 LOK |
323 | private void SetControlsEnabled(bool val) |
324 | { | |
9f8f2139 | 325 | SetControlEnabled(cbPortNumber, val && !Config.AutodetectAddress && Config.PhysicalAddress != 0); |
5cf99fae | 326 | SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 && !Config.AutodetectAddress && val); |
66609663 LOK |
327 | SetControlEnabled(cbOverrideAddress, val); |
328 | SetControlEnabled(tbPhysicalAddress, val && !Config.AutodetectAddress && cbOverrideAddress.Checked); | |
6d866874 | 329 | SetControlEnabled(cbDeviceType, val); |
5de1dde4 LOK |
330 | SetControlEnabled(cbUseTVMenuLanguage, val); |
331 | SetControlEnabled(cbActivateSource, val); | |
332 | SetControlEnabled(cbPowerOffScreensaver, val); | |
333 | SetControlEnabled(cbPowerOffOnStandby, val); | |
2b3c67ec LOK |
334 | SetControlEnabled(cbWakeDevices, val); |
335 | SetControlEnabled(cbPowerOffDevices, val); | |
5de1dde4 LOK |
336 | SetControlEnabled(cbVendorOverride, val); |
337 | SetControlEnabled(cbVendorId, val && cbVendorOverride.Checked); | |
338 | SetControlEnabled(bClose, val); | |
339 | SetControlEnabled(bSaveConfig, val); | |
340 | SetControlEnabled(bReloadConfig, val); | |
6d866874 | 341 | SetControlEnabled(bRescanDevices, val); |
5de1dde4 LOK |
342 | |
343 | SetControlEnabled(bSendImageViewOn, val); | |
344 | SetControlEnabled(bStandby, val); | |
345 | SetControlEnabled(bActivateSource, val); | |
346 | SetControlEnabled(bScan, val); | |
347 | ||
348 | bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val; | |
349 | SetControlEnabled(bVolUp, enableVolumeButtons); | |
350 | SetControlEnabled(bVolDown, enableVolumeButtons); | |
351 | SetControlEnabled(bMute, enableVolumeButtons); | |
352 | } | |
353 | ||
354 | private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) | |
355 | { | |
356 | switch (tabControl1.SelectedIndex) | |
357 | { | |
358 | case 0: | |
359 | SelectedTab = ConfigTab.Configuration; | |
360 | break; | |
361 | case 1: | |
362 | SelectedTab = ConfigTab.KeyConfiguration; | |
363 | break; | |
364 | case 2: | |
365 | SelectedTab = ConfigTab.Tester; | |
366 | break; | |
367 | case 3: | |
368 | SelectedTab = ConfigTab.Log; | |
369 | UpdateLog(); | |
370 | break; | |
371 | default: | |
372 | SelectedTab = ConfigTab.Configuration; | |
373 | break; | |
374 | } | |
375 | } | |
376 | ||
68b94c34 LOK |
377 | protected override void Dispose(bool disposing) |
378 | { | |
379 | if (disposing) | |
380 | { | |
381 | Lib.DisableCallbacks(); | |
382 | Lib.StandbyDevices(CecLogicalAddress.Broadcast); | |
383 | Lib.Close(); | |
384 | } | |
385 | if (disposing && (components != null)) | |
386 | { | |
387 | components.Dispose(); | |
388 | } | |
389 | base.Dispose(disposing); | |
390 | } | |
391 | ||
5de1dde4 | 392 | #region Actions |
5cf99fae | 393 | public void ReloadXmlConfiguration() |
5de1dde4 LOK |
394 | { |
395 | LoadXMLConfiguration(ref Config); | |
396 | Lib.SetConfiguration(Config); | |
397 | ConfigurationChanged(Config); | |
398 | } | |
399 | ||
6d866874 LOK |
400 | public void UpdateInfoPanel(DeviceInformation panel) |
401 | { | |
402 | if (!SuppressUpdates && ActiveProcess == null) | |
403 | { | |
404 | SetControlsEnabled(false); | |
405 | UpdatingInfoPanel = panel; | |
406 | panel.SetControlEnabled(panel.bUpdate, false); | |
407 | ActiveProcess = new UpdateDeviceInfo(this, ref Lib, panel); | |
5cf99fae LOK |
408 | ActiveProcess.EventHandler += ProcessEventHandler; |
409 | (new Thread(ActiveProcess.Run)).Start(); | |
6d866874 LOK |
410 | } |
411 | } | |
412 | ||
75af24f1 | 413 | public void SetPhysicalAddress(ushort physicalAddress) |
006b76b9 | 414 | { |
66609663 | 415 | if (!SuppressUpdates && ActiveProcess == null && cbOverrideAddress.Checked) |
006b76b9 | 416 | { |
75af24f1 LOK |
417 | SetControlsEnabled(false); |
418 | SetControlText(cbPortNumber, string.Empty); | |
419 | SetControlText(cbConnectedDevice, string.Empty); | |
420 | ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress); | |
5cf99fae LOK |
421 | ActiveProcess.EventHandler += ProcessEventHandler; |
422 | (new Thread(ActiveProcess.Run)).Start(); | |
006b76b9 | 423 | } |
75af24f1 | 424 | } |
6b92c1c4 | 425 | |
6d866874 LOK |
426 | public void UpdateConfigurationAsync() |
427 | { | |
428 | if (!SuppressUpdates && ActiveProcess == null) | |
429 | { | |
430 | SetControlsEnabled(false); | |
431 | ActiveProcess = new UpdateConfiguration(ref Lib, Config); | |
5cf99fae LOK |
432 | ActiveProcess.EventHandler += ProcessEventHandler; |
433 | (new Thread(ActiveProcess.Run)).Start(); | |
6d866874 LOK |
434 | } |
435 | } | |
436 | ||
75af24f1 LOK |
437 | public void SendImageViewOn(CecLogicalAddress address) |
438 | { | |
439 | if (!SuppressUpdates && ActiveProcess == null) | |
440 | { | |
441 | SetControlsEnabled(false); | |
442 | ActiveProcess = new SendImageViewOn(ref Lib, address); | |
5cf99fae LOK |
443 | ActiveProcess.EventHandler += ProcessEventHandler; |
444 | (new Thread(ActiveProcess.Run)).Start(); | |
006b76b9 LOK |
445 | } |
446 | } | |
447 | ||
75af24f1 | 448 | public void ActivateSource(CecLogicalAddress address) |
006b76b9 | 449 | { |
75af24f1 LOK |
450 | if (!SuppressUpdates && ActiveProcess == null) |
451 | { | |
452 | SetControlsEnabled(false); | |
453 | ActiveProcess = new SendActivateSource(ref Lib, address); | |
5cf99fae LOK |
454 | ActiveProcess.EventHandler += ProcessEventHandler; |
455 | (new Thread(ActiveProcess.Run)).Start(); | |
75af24f1 | 456 | } |
006b76b9 LOK |
457 | } |
458 | ||
75af24f1 | 459 | public void SendStandby(CecLogicalAddress address) |
006b76b9 | 460 | { |
75af24f1 | 461 | if (!SuppressUpdates && ActiveProcess == null) |
006b76b9 | 462 | { |
75af24f1 LOK |
463 | SetControlsEnabled(false); |
464 | ActiveProcess = new SendStandby(ref Lib, address); | |
5cf99fae LOK |
465 | ActiveProcess.EventHandler += ProcessEventHandler; |
466 | (new Thread(ActiveProcess.Run)).Start(); | |
006b76b9 | 467 | } |
75af24f1 LOK |
468 | } |
469 | ||
470 | public void ShowDeviceInfo(CecLogicalAddress address) | |
471 | { | |
472 | if (!SuppressUpdates && ActiveProcess == null) | |
006b76b9 | 473 | { |
75af24f1 LOK |
474 | SetControlsEnabled(false); |
475 | ActiveProcess = new ShowDeviceInfo(this, ref Lib, address); | |
5cf99fae LOK |
476 | ActiveProcess.EventHandler += ProcessEventHandler; |
477 | (new Thread(ActiveProcess.Run)).Start(); | |
006b76b9 LOK |
478 | } |
479 | } | |
5de1dde4 | 480 | #endregion |
006b76b9 | 481 | |
75af24f1 | 482 | #region Configuration tab |
66609663 LOK |
483 | private void cbOverrideAddress_CheckedChanged(object sender, EventArgs e) |
484 | { | |
485 | SetControlEnabled(tbPhysicalAddress, ((CheckBox)sender).Checked); | |
486 | } | |
487 | ||
75af24f1 | 488 | private void tbPhysicalAddress_TextChanged(object sender, EventArgs e) |
006b76b9 | 489 | { |
66609663 LOK |
490 | if (tbPhysicalAddress.Text.Length != 4 || |
491 | cbOverrideAddress.Checked) | |
75af24f1 LOK |
492 | return; |
493 | ushort physicalAddress = 0; | |
494 | if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress)) | |
495 | return; | |
496 | ||
497 | SetPhysicalAddress(physicalAddress); | |
006b76b9 LOK |
498 | } |
499 | ||
75af24f1 | 500 | private void UpdateSelectedDevice() |
006b76b9 | 501 | { |
75af24f1 | 502 | if (HasAVRDevice) |
5cf99fae | 503 | SetComboBoxItems(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString }); |
006b76b9 | 504 | else |
5cf99fae | 505 | SetComboBoxItems(cbConnectedDevice, TVVendorString, new object[] { TVVendorString }); |
006b76b9 LOK |
506 | } |
507 | ||
75af24f1 | 508 | public void SetConnectedDevice(CecLogicalAddress address, int portnumber) |
006b76b9 | 509 | { |
75af24f1 | 510 | if (!SuppressUpdates && ActiveProcess == null) |
006b76b9 | 511 | { |
75af24f1 LOK |
512 | SetControlsEnabled(false); |
513 | ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber); | |
5cf99fae LOK |
514 | ActiveProcess.EventHandler += ProcessEventHandler; |
515 | (new Thread(ActiveProcess.Run)).Start(); | |
006b76b9 LOK |
516 | } |
517 | } | |
518 | ||
75af24f1 | 519 | private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e) |
006b76b9 | 520 | { |
75af24f1 | 521 | SetConnectedDevice(SelectedConnectedDevice, SelectedPortNumber); |
ece1582e LOK |
522 | } |
523 | ||
006b76b9 LOK |
524 | private void bCancel_Click(object sender, EventArgs e) |
525 | { | |
5cf99fae | 526 | Dispose(); |
006b76b9 LOK |
527 | } |
528 | ||
529 | private void bSave_Click(object sender, EventArgs e) | |
530 | { | |
531 | SetControlsEnabled(false); | |
532 | ||
533 | Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked; | |
75af24f1 | 534 | Config.ActivateSource = cbActivateSource.Checked; |
006b76b9 LOK |
535 | Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked; |
536 | Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked; | |
2b3c67ec LOK |
537 | Config.WakeDevices = WakeDevices; |
538 | Config.PowerOffDevices = PowerOffDevices; | |
006b76b9 LOK |
539 | |
540 | if (!Lib.CanPersistConfiguration()) | |
541 | { | |
542 | if (ActiveProcess == null) | |
543 | { | |
544 | SetControlsEnabled(false); | |
545 | string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data"; | |
546 | string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); | |
547 | ||
548 | SaveFileDialog dialog = new SaveFileDialog() | |
549 | { | |
550 | Title = "Where do you want to store the settings?", | |
551 | InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir, | |
552 | FileName = "usb_2548_1001.xml", | |
553 | Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*", | |
554 | FilterIndex = 1 | |
555 | }; | |
556 | ||
557 | if (dialog.ShowDialog() == DialogResult.OK) | |
558 | { | |
de90f347 LOK |
559 | FileStream fs = null; |
560 | string error = string.Empty; | |
561 | try | |
562 | { | |
563 | fs = (FileStream)dialog.OpenFile(); | |
564 | } | |
565 | catch (Exception ex) | |
566 | { | |
567 | error = ex.Message; | |
568 | } | |
006b76b9 LOK |
569 | if (fs == null) |
570 | { | |
de90f347 | 571 | MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing" + (error.Length > 0 ? ": " + error : string.Empty ), "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
006b76b9 LOK |
572 | } |
573 | else | |
574 | { | |
575 | StreamWriter writer = new StreamWriter(fs); | |
576 | StringBuilder output = new StringBuilder(); | |
577 | output.AppendLine("<settings>"); | |
578 | output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />"); | |
959712c6 | 579 | output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 0) + "\" />"); |
75af24f1 LOK |
580 | output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />"); |
581 | output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffDevices.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />"); | |
006b76b9 LOK |
582 | output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />"); |
583 | output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />"); | |
584 | output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />"); | |
585 | output.AppendLine("<setting id=\"enabled\" value=\"1\" />"); | |
586 | output.AppendLine("<setting id=\"port\" value=\"\" />"); | |
75af24f1 LOK |
587 | |
588 | // only supported by 1.5.0+ clients | |
6d866874 | 589 | output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->"); |
c9549d35 | 590 | output.AppendLine("<setting id=\"activate_source\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />"); |
66609663 | 591 | output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", cbOverrideAddress.Checked ? Config.PhysicalAddress : 0).Trim() + "\" />"); |
75af24f1 | 592 | output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />"); |
2b3c67ec | 593 | output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", (int)Config.TvVendor).Trim() + "\" />"); |
75af24f1 LOK |
594 | |
595 | output.Append("<setting id=\"wake_devices\" value=\""); | |
66609663 | 596 | StringBuilder strWakeDevices = new StringBuilder(); |
75af24f1 | 597 | foreach (CecLogicalAddress addr in Config.WakeDevices.Addresses) |
2b3c67ec | 598 | if (addr != CecLogicalAddress.Unknown) |
66609663 LOK |
599 | strWakeDevices.Append(" " + (int)addr); |
600 | output.Append(strWakeDevices.ToString().Trim()); | |
75af24f1 LOK |
601 | output.AppendLine("\" />"); |
602 | ||
603 | output.Append("<setting id=\"standby_devices\" value=\""); | |
66609663 | 604 | StringBuilder strSleepDevices = new StringBuilder(); |
75af24f1 | 605 | foreach (CecLogicalAddress addr in Config.PowerOffDevices.Addresses) |
2b3c67ec | 606 | if (addr != CecLogicalAddress.Unknown) |
66609663 LOK |
607 | strSleepDevices.Append(" " + (int)addr); |
608 | output.Append(strSleepDevices.ToString().Trim()); | |
75af24f1 LOK |
609 | output.AppendLine("\" />"); |
610 | ||
006b76b9 LOK |
611 | output.AppendLine("</settings>"); |
612 | writer.Write(output.ToString()); | |
613 | writer.Close(); | |
614 | fs.Close(); | |
615 | fs.Dispose(); | |
616 | MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); | |
617 | } | |
618 | } | |
619 | SetControlsEnabled(true); | |
620 | } | |
621 | } | |
622 | else | |
623 | { | |
624 | if (!Lib.PersistConfiguration(Config)) | |
625 | MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); | |
626 | else | |
627 | MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); | |
628 | } | |
629 | SetControlsEnabled(true); | |
630 | } | |
de90f347 | 631 | |
5de1dde4 LOK |
632 | private void bReloadConfig_Click(object sender, EventArgs e) |
633 | { | |
634 | if (Lib.CanPersistConfiguration()) | |
635 | { | |
636 | Lib.GetCurrentConfiguration(Config); | |
637 | ConfigurationChanged(Config); | |
638 | } | |
639 | else | |
640 | { | |
5cf99fae | 641 | ReloadXmlConfiguration(); |
5de1dde4 LOK |
642 | } |
643 | } | |
644 | ||
de90f347 LOK |
645 | private void cbVendorOverride_CheckedChanged(object sender, EventArgs e) |
646 | { | |
647 | if (cbVendorOverride.Checked) | |
648 | { | |
649 | cbVendorId.Enabled = true; | |
650 | switch (cbVendorId.Text) | |
651 | { | |
652 | case "LG": | |
653 | Config.TvVendor = CecVendorId.LG; | |
654 | break; | |
655 | case "Onkyo": | |
656 | Config.TvVendor = CecVendorId.Onkyo; | |
657 | break; | |
658 | case "Panasonic": | |
659 | Config.TvVendor = CecVendorId.Panasonic; | |
660 | break; | |
661 | case "Philips": | |
662 | Config.TvVendor = CecVendorId.Philips; | |
663 | break; | |
664 | case "Pioneer": | |
665 | Config.TvVendor = CecVendorId.Pioneer; | |
666 | break; | |
667 | case "Samsung": | |
668 | Config.TvVendor = CecVendorId.Samsung; | |
669 | break; | |
670 | case "Sony": | |
671 | Config.TvVendor = CecVendorId.Sony; | |
672 | break; | |
673 | case "Yamaha": | |
674 | Config.TvVendor = CecVendorId.Yamaha; | |
675 | break; | |
676 | default: | |
677 | Config.TvVendor = CecVendorId.Unknown; | |
678 | break; | |
679 | } | |
680 | } | |
681 | else | |
682 | { | |
683 | cbVendorId.Enabled = false; | |
684 | Config.TvVendor = CecVendorId.Unknown; | |
685 | } | |
686 | } | |
6d866874 LOK |
687 | |
688 | private void cbDeviceType_SelectedIndexChanged(object sender, EventArgs e) | |
689 | { | |
690 | CecDeviceType type = SelectedDeviceType; | |
691 | if (type != Config.DeviceTypes.Types[0]) | |
692 | { | |
693 | Config.DeviceTypes.Types[0] = type; | |
694 | if (!DeviceChangeWarningDisplayed) | |
695 | { | |
696 | DeviceChangeWarningDisplayed = true; | |
697 | MessageBox.Show("You have changed the device type. Save the configuration, and restart the application to use the new setting.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Warning); | |
698 | } | |
699 | } | |
700 | } | |
75af24f1 | 701 | #endregion |
006b76b9 | 702 | |
75af24f1 LOK |
703 | #region Key configuration tab |
704 | delegate void SelectKeypressRowCallback(CecKeypress key); | |
705 | private void SelectKeypressRow(CecKeypress key) | |
6b92c1c4 | 706 | { |
75af24f1 | 707 | if (dgButtons.InvokeRequired) |
6b92c1c4 | 708 | { |
5cf99fae | 709 | SelectKeypressRowCallback d = SelectKeypressRow; |
75af24f1 | 710 | try |
6b92c1c4 | 711 | { |
5cf99fae | 712 | Invoke(d, new object[] { key }); |
6b92c1c4 | 713 | } |
75af24f1 LOK |
714 | catch (Exception) { } |
715 | } | |
716 | else | |
717 | { | |
718 | int rowIndex = -1; | |
719 | foreach (DataGridViewRow row in dgButtons.Rows) | |
6b92c1c4 | 720 | { |
75af24f1 LOK |
721 | CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem; |
722 | if (item != null && item.Key.Keycode == key.Keycode) | |
723 | { | |
724 | rowIndex = row.Index; | |
725 | row.Selected = true; | |
726 | item.Enabled = true; | |
727 | } | |
728 | else | |
729 | { | |
730 | row.Selected = false; | |
731 | } | |
6b92c1c4 | 732 | } |
75af24f1 LOK |
733 | if (rowIndex > -1) |
734 | dgButtons.FirstDisplayedScrollingRowIndex = rowIndex; | |
6b92c1c4 LOK |
735 | } |
736 | } | |
8674df6a LOK |
737 | |
738 | private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) | |
739 | { | |
740 | DataGridView grid = sender as DataGridView; | |
741 | CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem; | |
742 | if (data == null || !data.Enabled) | |
743 | e.CellStyle.ForeColor = Color.Gray; | |
744 | } | |
75af24f1 | 745 | #endregion |
96fa7764 | 746 | |
75af24f1 | 747 | #region CEC Tester tab |
6d866874 LOK |
748 | public void CheckActiveDevices() |
749 | { | |
750 | CecLogicalAddresses activeDevices = Lib.GetActiveDevices(); | |
751 | List<string> deviceList = new List<string>(); | |
752 | foreach (CecLogicalAddress activeDevice in activeDevices.Addresses) | |
753 | { | |
2b3c67ec | 754 | if (activeDevice != CecLogicalAddress.Unknown) |
6d866874 LOK |
755 | deviceList.Add(string.Format("{0,1:X} : {1}", (int)activeDevice, Lib.ToString(activeDevice))); |
756 | } | |
757 | deviceList.Add(string.Format("{0,1:X} : {1}", (int)CecLogicalAddress.Broadcast, Lib.ToString(CecLogicalAddress.Broadcast))); | |
758 | ||
759 | SetActiveDevices(deviceList.ToArray()); | |
760 | } | |
761 | ||
762 | delegate void SetActiveDevicesCallback(string[] activeDevices); | |
763 | private void SetActiveDevices(string[] activeDevices) | |
764 | { | |
5cf99fae | 765 | if (cbCommandDestination.InvokeRequired) |
6d866874 | 766 | { |
5cf99fae | 767 | SetActiveDevicesCallback d = SetActiveDevices; |
6d866874 LOK |
768 | try |
769 | { | |
5cf99fae | 770 | Invoke(d, new object[] { activeDevices }); |
6d866874 LOK |
771 | } |
772 | catch (Exception) { } | |
773 | } | |
774 | else | |
775 | { | |
5cf99fae | 776 | cbCommandDestination.Items.Clear(); |
6d866874 | 777 | foreach (string item in activeDevices) |
5cf99fae | 778 | cbCommandDestination.Items.Add(item); |
6d866874 LOK |
779 | } |
780 | } | |
781 | ||
ece1582e | 782 | delegate CecLogicalAddress GetTargetDeviceCallback(); |
96fa7764 LOK |
783 | private CecLogicalAddress GetTargetDevice() |
784 | { | |
5cf99fae | 785 | if (cbCommandDestination.InvokeRequired) |
ece1582e | 786 | { |
5cf99fae | 787 | GetTargetDeviceCallback d = GetTargetDevice; |
ece1582e LOK |
788 | CecLogicalAddress retval = CecLogicalAddress.Unknown; |
789 | try | |
790 | { | |
5cf99fae | 791 | retval = (CecLogicalAddress)Invoke(d, new object[] { }); |
ece1582e LOK |
792 | } |
793 | catch (Exception) { } | |
794 | return retval; | |
795 | } | |
796 | ||
5cf99fae | 797 | return GetLogicalAddressFromString(cbCommandDestination.Text); |
2b3c67ec LOK |
798 | } |
799 | ||
800 | private CecLogicalAddress GetLogicalAddressFromString(string name) | |
801 | { | |
802 | switch (name.Substring(0, 1).ToLower()) | |
96fa7764 LOK |
803 | { |
804 | case "0": | |
805 | return CecLogicalAddress.Tv; | |
806 | case "1": | |
807 | return CecLogicalAddress.RecordingDevice1; | |
808 | case "2": | |
809 | return CecLogicalAddress.RecordingDevice2; | |
810 | case "3": | |
811 | return CecLogicalAddress.Tuner1; | |
812 | case "4": | |
813 | return CecLogicalAddress.PlaybackDevice1; | |
814 | case "5": | |
815 | return CecLogicalAddress.AudioSystem; | |
816 | case "6": | |
817 | return CecLogicalAddress.Tuner2; | |
818 | case "7": | |
819 | return CecLogicalAddress.Tuner3; | |
820 | case "8": | |
821 | return CecLogicalAddress.PlaybackDevice2; | |
822 | case "9": | |
823 | return CecLogicalAddress.RecordingDevice3; | |
824 | case "a": | |
825 | return CecLogicalAddress.Tuner4; | |
826 | case "b": | |
827 | return CecLogicalAddress.PlaybackDevice3; | |
828 | case "c": | |
829 | return CecLogicalAddress.Reserved1; | |
830 | case "d": | |
831 | return CecLogicalAddress.Reserved2; | |
832 | case "e": | |
833 | return CecLogicalAddress.FreeUse; | |
834 | case "f": | |
835 | return CecLogicalAddress.Broadcast; | |
836 | default: | |
837 | return CecLogicalAddress.Unknown; | |
838 | } | |
839 | } | |
840 | ||
ece1582e LOK |
841 | private void bSendImageViewOn_Click(object sender, EventArgs e) |
842 | { | |
843 | SendImageViewOn(GetTargetDevice()); | |
844 | } | |
845 | ||
ece1582e LOK |
846 | private void bStandby_Click(object sender, EventArgs e) |
847 | { | |
848 | SendStandby(GetTargetDevice()); | |
849 | } | |
850 | ||
ece1582e LOK |
851 | private void bScan_Click(object sender, EventArgs e) |
852 | { | |
853 | ShowDeviceInfo(GetTargetDevice()); | |
854 | } | |
855 | ||
ece1582e LOK |
856 | private void bActivateSource_Click(object sender, EventArgs e) |
857 | { | |
858 | ActivateSource(GetTargetDevice()); | |
859 | } | |
860 | ||
861 | private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e) | |
862 | { | |
863 | bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem); | |
5cf99fae LOK |
864 | bVolUp.Enabled = enableVolumeButtons; |
865 | bVolDown.Enabled = enableVolumeButtons; | |
866 | bMute.Enabled = enableVolumeButtons; | |
867 | bActivateSource.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast); | |
868 | bScan.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast); | |
ece1582e LOK |
869 | } |
870 | ||
871 | private void bVolUp_Click(object sender, EventArgs e) | |
872 | { | |
873 | SetControlsEnabled(false); | |
874 | Lib.VolumeUp(true); | |
875 | SetControlsEnabled(true); | |
876 | } | |
877 | ||
878 | private void bVolDown_Click(object sender, EventArgs e) | |
879 | { | |
880 | SetControlsEnabled(false); | |
881 | Lib.VolumeDown(true); | |
882 | SetControlsEnabled(true); | |
883 | } | |
884 | ||
885 | private void bMute_Click(object sender, EventArgs e) | |
886 | { | |
887 | SetControlsEnabled(false); | |
888 | Lib.MuteAudio(true); | |
889 | SetControlsEnabled(true); | |
890 | } | |
6d866874 LOK |
891 | |
892 | private void bRescanDevices_Click(object sender, EventArgs e) | |
893 | { | |
894 | if (!SuppressUpdates && ActiveProcess == null) | |
895 | { | |
896 | SetControlsEnabled(false); | |
3efda01a | 897 | ActiveProcess = new RescanDevices(ref Lib); |
5cf99fae LOK |
898 | ActiveProcess.EventHandler += ProcessEventHandler; |
899 | (new Thread(ActiveProcess.Run)).Start(); | |
6d866874 LOK |
900 | } |
901 | } | |
75af24f1 LOK |
902 | #endregion |
903 | ||
904 | #region Log tab | |
5de1dde4 LOK |
905 | delegate void UpdateLogCallback(); |
906 | private void UpdateLog() | |
75af24f1 LOK |
907 | { |
908 | if (tbLog.InvokeRequired) | |
909 | { | |
5cf99fae | 910 | UpdateLogCallback d = UpdateLog; |
75af24f1 LOK |
911 | try |
912 | { | |
5cf99fae | 913 | Invoke(d, new object[] { }); |
75af24f1 LOK |
914 | } |
915 | catch (Exception) { } | |
916 | } | |
917 | else | |
918 | { | |
5de1dde4 LOK |
919 | tbLog.Text = Log; |
920 | tbLog.Select(tbLog.Text.Length, 0); | |
921 | tbLog.ScrollToCaret(); | |
922 | } | |
923 | } | |
75af24f1 | 924 | |
5de1dde4 LOK |
925 | private void AddLogMessage(CecLogMessage message) |
926 | { | |
927 | string strLevel = ""; | |
928 | bool display = false; | |
929 | switch (message.Level) | |
930 | { | |
931 | case CecLogLevel.Error: | |
932 | strLevel = "ERROR: "; | |
933 | display = cbLogError.Checked; | |
934 | break; | |
935 | case CecLogLevel.Warning: | |
936 | strLevel = "WARNING: "; | |
937 | display = cbLogWarning.Checked; | |
938 | break; | |
939 | case CecLogLevel.Notice: | |
940 | strLevel = "NOTICE: "; | |
941 | display = cbLogNotice.Checked; | |
942 | break; | |
943 | case CecLogLevel.Traffic: | |
944 | strLevel = "TRAFFIC: "; | |
945 | display = cbLogTraffic.Checked; | |
946 | break; | |
947 | case CecLogLevel.Debug: | |
948 | strLevel = "DEBUG: "; | |
949 | display = cbLogDebug.Checked; | |
950 | break; | |
951 | default: | |
952 | break; | |
953 | } | |
954 | ||
955 | if (display) | |
956 | { | |
957 | string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine; | |
958 | Log += strLog; | |
75af24f1 | 959 | } |
5de1dde4 LOK |
960 | |
961 | if (SelectedTab == ConfigTab.Log) | |
962 | UpdateLog(); | |
75af24f1 LOK |
963 | } |
964 | ||
965 | private void bClearLog_Click(object sender, EventArgs e) | |
966 | { | |
6d866874 LOK |
967 | Log = string.Empty; |
968 | UpdateLog(); | |
75af24f1 LOK |
969 | } |
970 | ||
971 | private void bSaveLog_Click(object sender, EventArgs e) | |
972 | { | |
973 | SaveFileDialog dialog = new SaveFileDialog() | |
974 | { | |
975 | Title = "Where do you want to store the log file?", | |
976 | InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), | |
977 | FileName = "cec-log.txt", | |
978 | Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*", | |
979 | FilterIndex = 1 | |
980 | }; | |
981 | ||
982 | if (dialog.ShowDialog() == DialogResult.OK) | |
983 | { | |
984 | FileStream fs = (FileStream)dialog.OpenFile(); | |
985 | if (fs == null) | |
986 | { | |
987 | MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); | |
988 | } | |
989 | else | |
990 | { | |
991 | StreamWriter writer = new StreamWriter(fs); | |
6d866874 | 992 | writer.Write(Log); |
75af24f1 LOK |
993 | writer.Close(); |
994 | fs.Close(); | |
995 | fs.Dispose(); | |
996 | MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); | |
997 | } | |
998 | } | |
999 | } | |
1000 | #endregion | |
1001 | ||
1002 | #region LibCecSharp callbacks | |
1003 | public int ConfigurationChanged(LibCECConfiguration config) | |
1004 | { | |
1005 | Config = config; | |
1006 | SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress)); | |
1007 | SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString); | |
1008 | SetControlText(cbPortNumber, Config.HDMIPort.ToString()); | |
de90f347 LOK |
1009 | switch (config.DeviceTypes.Types[0]) |
1010 | { | |
1011 | case CecDeviceType.RecordingDevice: | |
1012 | SetControlText(cbDeviceType, "Recorder"); | |
1013 | break; | |
1014 | case CecDeviceType.PlaybackDevice: | |
1015 | SetControlText(cbDeviceType, "Player"); | |
1016 | break; | |
1017 | case CecDeviceType.Tuner: | |
1018 | SetControlText(cbDeviceType, "Tuner"); | |
1019 | break; | |
1020 | default: | |
1021 | SetControlText(cbDeviceType, "Recorder"); | |
1022 | break; | |
1023 | } | |
1024 | if (config.TvVendor != CecVendorId.Unknown) | |
1025 | { | |
1026 | SetCheckboxChecked(cbVendorOverride, true); | |
1027 | SetControlText(cbVendorId, Lib.ToString(config.TvVendor)); | |
1028 | } | |
1029 | else | |
1030 | { | |
1031 | SetCheckboxChecked(cbVendorOverride, false); | |
1032 | SetControlText(cbVendorId, Lib.ToString(TVVendor)); | |
1033 | } | |
1034 | ||
75af24f1 LOK |
1035 | SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage); |
1036 | SetCheckboxChecked(cbActivateSource, Config.ActivateSource); | |
1037 | SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver); | |
1038 | SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby); | |
1039 | UpdateSelectedDevice(); | |
2b3c67ec LOK |
1040 | |
1041 | for (int iPtr = 0; iPtr < 15; iPtr++) | |
1042 | SetCheckboxItemChecked(cbWakeDevices, iPtr, Config.WakeDevices.IsSet((CecLogicalAddress)iPtr)); | |
1043 | for (int iPtr = 0; iPtr < 15; iPtr++) | |
1044 | SetCheckboxItemChecked(cbPowerOffDevices, iPtr, Config.PowerOffDevices.IsSet((CecLogicalAddress)iPtr)); | |
3efda01a LOK |
1045 | |
1046 | SetControlText(this, "Pulse-Eight USB-CEC Adapter - libCEC " + Lib.ToString(Config.ServerVersion)); | |
75af24f1 LOK |
1047 | return 1; |
1048 | } | |
1049 | ||
1050 | public int ReceiveCommand(CecCommand command) | |
1051 | { | |
1052 | return 1; | |
1053 | } | |
1054 | ||
1055 | public int ReceiveKeypress(CecKeypress key) | |
1056 | { | |
1057 | SelectKeypressRow(key); | |
1058 | return 1; | |
1059 | } | |
1060 | ||
1061 | public int ReceiveLogMessage(CecLogMessage message) | |
1062 | { | |
2b3c67ec LOK |
1063 | try |
1064 | { | |
1065 | AddLogMessage(message); | |
1066 | } | |
1067 | catch (Exception) { } | |
75af24f1 LOK |
1068 | return 1; |
1069 | } | |
1070 | #endregion | |
1071 | ||
1072 | #region Class members | |
1073 | public bool HasAVRDevice { get; private set; } | |
1074 | #region TV Vendor | |
1075 | private CecVendorId _tvVendor = CecVendorId.Unknown; | |
1076 | public CecVendorId TVVendor | |
1077 | { | |
1078 | get { return _tvVendor;} | |
1079 | private set { _tvVendor = value; } | |
1080 | } | |
1081 | public string TVVendorString | |
1082 | { | |
1083 | get | |
1084 | { | |
1085 | return TVVendor != CecVendorId.Unknown ? | |
1086 | "Television (" + Lib.ToString(TVVendor) + ")" : | |
1087 | "Television"; | |
1088 | } | |
1089 | } | |
1090 | #endregion | |
1091 | #region AVR Vendor | |
1092 | private CecVendorId _avrVendor = CecVendorId.Unknown; | |
1093 | public CecVendorId AVRVendor | |
1094 | { | |
1095 | get { return _avrVendor; } | |
1096 | private set { _avrVendor = value; } | |
1097 | } | |
1098 | public string AVRVendorString | |
1099 | { | |
1100 | get | |
1101 | { | |
1102 | return AVRVendor != CecVendorId.Unknown ? | |
1103 | "AVR (" + Lib.ToString(AVRVendor) + ")" : | |
1104 | "AVR"; | |
1105 | } | |
1106 | } | |
1107 | #endregion | |
1108 | public CecLogicalAddress SelectedConnectedDevice | |
1109 | { | |
1110 | get | |
1111 | { | |
1112 | return (cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv; | |
1113 | } | |
1114 | } | |
6d866874 LOK |
1115 | public CecDeviceType SelectedDeviceType |
1116 | { | |
1117 | get | |
1118 | { | |
1119 | switch (cbDeviceType.Text.ToLower()) | |
1120 | { | |
1121 | case "player": | |
1122 | return CecDeviceType.PlaybackDevice; | |
1123 | case "tuner": | |
1124 | return CecDeviceType.Tuner; | |
1125 | default: | |
1126 | return CecDeviceType.RecordingDevice; | |
1127 | } | |
1128 | } | |
1129 | } | |
75af24f1 LOK |
1130 | public int SelectedPortNumber |
1131 | { | |
1132 | get | |
1133 | { | |
1134 | int iPortNumber = 0; | |
1135 | if (!int.TryParse(cbPortNumber.Text, out iPortNumber)) | |
1136 | iPortNumber = 1; | |
1137 | return iPortNumber; | |
1138 | } | |
1139 | } | |
1140 | protected LibCECConfiguration Config; | |
1141 | protected LibCecSharp Lib; | |
1142 | private CecCallbackWrapper Callbacks; | |
1143 | private UpdateProcess ActiveProcess = null; | |
5b8c2761 | 1144 | private bool SuppressUpdates = true; |
5de1dde4 LOK |
1145 | private ConfigTab SelectedTab = ConfigTab.Configuration; |
1146 | private string Log = string.Empty; | |
6d866874 LOK |
1147 | private DeviceInformation UpdatingInfoPanel = null; |
1148 | private bool DeviceChangeWarningDisplayed = false; | |
2b3c67ec LOK |
1149 | public CecLogicalAddresses WakeDevices |
1150 | { | |
1151 | get | |
1152 | { | |
1153 | CecLogicalAddresses addr = new CecLogicalAddresses(); | |
5cf99fae | 1154 | foreach (object item in cbWakeDevices.CheckedItems) |
2b3c67ec LOK |
1155 | { |
1156 | string c = item as string; | |
1157 | addr.Set(GetLogicalAddressFromString(c)); | |
1158 | } | |
1159 | return addr; | |
1160 | } | |
1161 | } | |
1162 | public CecLogicalAddresses PowerOffDevices | |
1163 | { | |
1164 | get | |
1165 | { | |
1166 | CecLogicalAddresses addr = new CecLogicalAddresses(); | |
5cf99fae | 1167 | foreach (object item in cbPowerOffDevices.CheckedItems) |
2b3c67ec LOK |
1168 | { |
1169 | string c = item as string; | |
1170 | addr.Set(GetLogicalAddressFromString(c)); | |
1171 | } | |
1172 | return addr; | |
1173 | } | |
1174 | } | |
75af24f1 | 1175 | #endregion |
006b76b9 LOK |
1176 | } |
1177 | ||
75af24f1 LOK |
1178 | /// <summary> |
1179 | /// A little wrapper that is needed because we already inherit form | |
1180 | /// </summary> | |
006b76b9 LOK |
1181 | internal class CecCallbackWrapper : CecCallbackMethods |
1182 | { | |
1183 | public CecCallbackWrapper(CecConfigGUI gui) | |
1184 | { | |
1185 | Gui = gui; | |
1186 | } | |
1187 | ||
1188 | public override int ReceiveCommand(CecCommand command) | |
1189 | { | |
1190 | return Gui.ReceiveCommand(command); | |
1191 | } | |
1192 | ||
1193 | public override int ReceiveKeypress(CecKeypress key) | |
1194 | { | |
1195 | return Gui.ReceiveKeypress(key); | |
1196 | } | |
1197 | ||
1198 | public override int ReceiveLogMessage(CecLogMessage message) | |
1199 | { | |
1200 | return Gui.ReceiveLogMessage(message); | |
1201 | } | |
1202 | ||
32403cc3 LOK |
1203 | public override int ConfigurationChanged(LibCECConfiguration config) |
1204 | { | |
1205 | return Gui.ConfigurationChanged(config); | |
1206 | } | |
1207 | ||
006b76b9 LOK |
1208 | private CecConfigGUI Gui; |
1209 | } | |
1210 | } |