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