2 using System.Collections.Generic;
5 using System.Windows.Forms;
6 using System.Threading;
8 using CecConfigGui.actions;
9 using System.Globalization;
13 namespace CecConfigGui
15 internal enum ConfigTab
23 public partial class CecConfigGUI : AsyncForm
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);
34 LoadXMLConfiguration(ref Config);
35 Lib = new LibCecSharp(Config);
37 InitializeComponent();
38 LoadButtonConfiguration();
40 ActiveProcess = new ConnectToDevice(ref Lib, Config);
41 ActiveProcess.EventHandler += ProcessEventHandler;
42 (new Thread(ActiveProcess.Run)).Start();
45 private bool LoadXMLConfiguration(ref LibCECConfiguration config)
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";
54 if (File.Exists(file))
56 XmlTextReader reader = new XmlTextReader(file);
60 switch (reader.NodeType)
62 case XmlNodeType.Element:
63 if (reader.Name.ToLower() == "setting")
65 string name = string.Empty;
66 string value = string.Empty;
68 while (reader.MoveToNextAttribute())
70 if (reader.Name.ToLower().Equals("id"))
71 name = reader.Value.ToLower();
72 if (reader.Name.ToLower().Equals("value"))
81 if (byte.TryParse(value, out iPort))
82 config.HDMIPort = iPort;
85 case "connected_device":
88 if (ushort.TryParse(value, out iDevice))
89 config.BaseDevice = (CecLogicalAddress)iDevice;
92 case "cec_power_on_startup":
93 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
95 config.ActivateSource = true;
96 config.WakeDevices.Set(CecLogicalAddress.Tv);
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);
103 case "cec_standby_screensaver":
104 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
106 case "standby_pc_on_tv_standby":
107 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
109 case "use_tv_menu_language":
110 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
113 case "physical_address":
115 ushort physicalAddress = 0;
116 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
117 config.PhysicalAddress = physicalAddress;
123 if (ushort.TryParse(value, out iType))
124 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
130 if (UInt64.TryParse(value, out iVendor))
131 config.TvVendor = (CecVendorId)iVendor;
136 config.WakeDevices.Clear();
137 string[] split = value.Split(new[] { ' ' });
138 foreach (string dev in split)
140 byte iLogicalAddress;
141 if (byte.TryParse(dev, out iLogicalAddress))
142 config.WakeDevices.Set((CecLogicalAddress)iLogicalAddress);
146 case "standby_devices":
148 config.PowerOffDevices.Clear();
149 string[] split = value.Split(new char[] { ' ' });
150 foreach (string dev in split)
152 byte iLogicalAddress;
153 if (byte.TryParse(dev, out iLogicalAddress))
154 config.PowerOffDevices.Set((CecLogicalAddress)iLogicalAddress);
176 private void LoadButtonConfiguration()
178 //TODO load the real configuration
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));
260 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
262 switch (updateEvent.Type)
264 case UpdateEventType.StatusText:
265 SetControlText(lStatus, updateEvent.StringValue);
267 case UpdateEventType.PhysicalAddress:
268 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
269 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
271 case UpdateEventType.ProgressBar:
272 SetControlVisible(pProgress, true);
273 SetProgressValue(pProgress, updateEvent.IntValue);
275 case UpdateEventType.TVVendorId:
276 TVVendor = (CecVendorId)updateEvent.IntValue;
277 UpdateSelectedDevice();
279 case UpdateEventType.BaseDevicePhysicalAddress:
280 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
282 case UpdateEventType.BaseDevice:
283 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
285 case UpdateEventType.HDMIPort:
286 Config.HDMIPort = (byte)updateEvent.IntValue;
288 case UpdateEventType.MenuLanguage:
289 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
291 case UpdateEventType.HasAVRDevice:
292 if (HasAVRDevice != updateEvent.BoolValue)
294 HasAVRDevice = updateEvent.BoolValue;
295 UpdateSelectedDevice();
298 case UpdateEventType.AVRVendorId:
299 AVRVendor = (CecVendorId)updateEvent.IntValue;
300 UpdateSelectedDevice();
302 case UpdateEventType.Configuration:
303 SuppressUpdates = true;
304 ConfigurationChanged(updateEvent.ConfigValue);
305 SuppressUpdates = false;
307 case UpdateEventType.PollDevices:
308 CheckActiveDevices();
310 case UpdateEventType.ProcessCompleted:
311 ActiveProcess = null;
312 SetControlsEnabled(true);
313 if (UpdatingInfoPanel != null)
315 UpdatingInfoPanel.SetControlEnabled(UpdatingInfoPanel.bUpdate, true);
316 UpdatingInfoPanel = null;
318 SetControlVisible(pProgress, false);
323 private void SetControlsEnabled(bool val)
325 SetControlEnabled(cbPortNumber, val && !cbOverrideAddress.Checked);
326 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 && !cbOverrideAddress.Checked && val);
327 SetControlEnabled(cbOverrideAddress, val);
328 SetControlEnabled(tbPhysicalAddress, val && !Config.AutodetectAddress && cbOverrideAddress.Checked);
329 SetControlEnabled(cbDeviceType, val);
330 SetControlEnabled(cbUseTVMenuLanguage, val);
331 SetControlEnabled(cbActivateSource, val);
332 SetControlEnabled(cbPowerOffScreensaver, val);
333 SetControlEnabled(cbPowerOffOnStandby, val);
334 SetControlEnabled(cbWakeDevices, val);
335 SetControlEnabled(cbPowerOffDevices, val);
336 SetControlEnabled(cbVendorOverride, val);
337 SetControlEnabled(cbVendorId, val && cbVendorOverride.Checked);
338 SetControlEnabled(bClose, val);
339 SetControlEnabled(bSaveConfig, val);
340 SetControlEnabled(bReloadConfig, val);
341 SetControlEnabled(bRescanDevices, val);
343 SetControlEnabled(bSendImageViewOn, val);
344 SetControlEnabled(bStandby, val);
345 SetControlEnabled(bActivateSource, val);
346 SetControlEnabled(bScan, val);
348 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
349 SetControlEnabled(bVolUp, enableVolumeButtons);
350 SetControlEnabled(bVolDown, enableVolumeButtons);
351 SetControlEnabled(bMute, enableVolumeButtons);
354 private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
356 switch (tabControl1.SelectedIndex)
359 SelectedTab = ConfigTab.Configuration;
362 SelectedTab = ConfigTab.KeyConfiguration;
365 SelectedTab = ConfigTab.Tester;
368 SelectedTab = ConfigTab.Log;
372 SelectedTab = ConfigTab.Configuration;
377 protected override void Dispose(bool disposing)
381 Lib.DisableCallbacks();
382 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
385 if (disposing && (components != null))
387 components.Dispose();
389 base.Dispose(disposing);
393 public void ReloadXmlConfiguration()
395 LoadXMLConfiguration(ref Config);
396 Lib.SetConfiguration(Config);
397 ConfigurationChanged(Config);
400 public void UpdateInfoPanel(DeviceInformation panel)
402 if (!SuppressUpdates && ActiveProcess == null)
404 SetControlsEnabled(false);
405 UpdatingInfoPanel = panel;
406 panel.SetControlEnabled(panel.bUpdate, false);
407 ActiveProcess = new UpdateDeviceInfo(this, ref Lib, panel);
408 ActiveProcess.EventHandler += ProcessEventHandler;
409 (new Thread(ActiveProcess.Run)).Start();
413 public void SetPhysicalAddress(ushort physicalAddress)
415 if (!SuppressUpdates && ActiveProcess == null && cbOverrideAddress.Checked)
417 SetControlsEnabled(false);
418 SetControlText(cbPortNumber, string.Empty);
419 SetControlText(cbConnectedDevice, string.Empty);
420 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
421 ActiveProcess.EventHandler += ProcessEventHandler;
422 (new Thread(ActiveProcess.Run)).Start();
426 public void UpdateConfigurationAsync()
428 if (!SuppressUpdates && ActiveProcess == null)
430 SetControlsEnabled(false);
431 ActiveProcess = new UpdateConfiguration(ref Lib, Config);
432 ActiveProcess.EventHandler += ProcessEventHandler;
433 (new Thread(ActiveProcess.Run)).Start();
437 public void SendImageViewOn(CecLogicalAddress address)
439 if (!SuppressUpdates && ActiveProcess == null)
441 SetControlsEnabled(false);
442 ActiveProcess = new SendImageViewOn(ref Lib, address);
443 ActiveProcess.EventHandler += ProcessEventHandler;
444 (new Thread(ActiveProcess.Run)).Start();
448 public void ActivateSource(CecLogicalAddress address)
450 if (!SuppressUpdates && ActiveProcess == null)
452 SetControlsEnabled(false);
453 ActiveProcess = new SendActivateSource(ref Lib, address);
454 ActiveProcess.EventHandler += ProcessEventHandler;
455 (new Thread(ActiveProcess.Run)).Start();
459 public void SendStandby(CecLogicalAddress address)
461 if (!SuppressUpdates && ActiveProcess == null)
463 SetControlsEnabled(false);
464 ActiveProcess = new SendStandby(ref Lib, address);
465 ActiveProcess.EventHandler += ProcessEventHandler;
466 (new Thread(ActiveProcess.Run)).Start();
470 public void ShowDeviceInfo(CecLogicalAddress address)
472 if (!SuppressUpdates && ActiveProcess == null)
474 SetControlsEnabled(false);
475 ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
476 ActiveProcess.EventHandler += ProcessEventHandler;
477 (new Thread(ActiveProcess.Run)).Start();
482 #region Configuration tab
483 private void cbOverrideAddress_CheckedChanged(object sender, EventArgs e)
485 SetControlEnabled(tbPhysicalAddress, ((CheckBox)sender).Checked);
486 SetControlEnabled(cbPortNumber, !((CheckBox)sender).Checked);
487 SetControlEnabled(cbConnectedDevice, !((CheckBox)sender).Checked && cbConnectedDevice.Items.Count > 1);
490 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
492 if (tbPhysicalAddress.Text.Length != 4 ||
493 cbOverrideAddress.Checked)
495 ushort physicalAddress = 0;
496 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
499 SetPhysicalAddress(physicalAddress);
502 private void UpdateSelectedDevice()
505 SetComboBoxItems(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
507 SetComboBoxItems(cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
510 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
512 if (!SuppressUpdates && ActiveProcess == null)
514 SetControlsEnabled(false);
515 ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
516 ActiveProcess.EventHandler += ProcessEventHandler;
517 (new Thread(ActiveProcess.Run)).Start();
521 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
523 SetConnectedDevice(SelectedConnectedDevice, SelectedPortNumber);
526 private void bCancel_Click(object sender, EventArgs e)
531 private void bSave_Click(object sender, EventArgs e)
533 SetControlsEnabled(false);
535 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
536 Config.ActivateSource = cbActivateSource.Checked;
537 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
538 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
539 Config.WakeDevices = WakeDevices;
540 Config.PowerOffDevices = PowerOffDevices;
542 if (!Lib.CanPersistConfiguration())
544 if (ActiveProcess == null)
546 SetControlsEnabled(false);
547 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
548 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
550 SaveFileDialog dialog = new SaveFileDialog()
552 Title = "Where do you want to store the settings?",
553 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
554 FileName = "usb_2548_1001.xml",
555 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
559 if (dialog.ShowDialog() == DialogResult.OK)
561 FileStream fs = null;
562 string error = string.Empty;
565 fs = (FileStream)dialog.OpenFile();
573 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing" + (error.Length > 0 ? ": " + error : string.Empty ), "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
577 StreamWriter writer = new StreamWriter(fs);
578 StringBuilder output = new StringBuilder();
579 output.AppendLine("<settings>");
580 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
581 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 0) + "\" />");
582 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />");
583 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffDevices.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />");
584 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
585 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
586 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
587 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
588 output.AppendLine("<setting id=\"port\" value=\"\" />");
590 // only supported by 1.5.0+ clients
591 output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->");
592 output.AppendLine("<setting id=\"activate_source\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />");
593 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", cbOverrideAddress.Checked ? Config.PhysicalAddress : 0).Trim() + "\" />");
594 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
595 output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", (int)Config.TvVendor).Trim() + "\" />");
597 output.Append("<setting id=\"wake_devices\" value=\"");
598 StringBuilder strWakeDevices = new StringBuilder();
599 foreach (CecLogicalAddress addr in Config.WakeDevices.Addresses)
600 if (addr != CecLogicalAddress.Unknown)
601 strWakeDevices.Append(" " + (int)addr);
602 output.Append(strWakeDevices.ToString().Trim());
603 output.AppendLine("\" />");
605 output.Append("<setting id=\"standby_devices\" value=\"");
606 StringBuilder strSleepDevices = new StringBuilder();
607 foreach (CecLogicalAddress addr in Config.PowerOffDevices.Addresses)
608 if (addr != CecLogicalAddress.Unknown)
609 strSleepDevices.Append(" " + (int)addr);
610 output.Append(strSleepDevices.ToString().Trim());
611 output.AppendLine("\" />");
613 output.AppendLine("</settings>");
614 writer.Write(output.ToString());
618 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
621 SetControlsEnabled(true);
626 if (!Lib.PersistConfiguration(Config))
627 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
629 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
631 SetControlsEnabled(true);
634 private void bReloadConfig_Click(object sender, EventArgs e)
636 if (Lib.CanPersistConfiguration())
638 Lib.GetCurrentConfiguration(Config);
639 ConfigurationChanged(Config);
643 ReloadXmlConfiguration();
647 private void cbVendorOverride_CheckedChanged(object sender, EventArgs e)
649 if (cbVendorOverride.Checked)
651 cbVendorId.Enabled = true;
652 switch (cbVendorId.Text)
655 Config.TvVendor = CecVendorId.LG;
658 Config.TvVendor = CecVendorId.Onkyo;
661 Config.TvVendor = CecVendorId.Panasonic;
664 Config.TvVendor = CecVendorId.Philips;
667 Config.TvVendor = CecVendorId.Pioneer;
670 Config.TvVendor = CecVendorId.Samsung;
673 Config.TvVendor = CecVendorId.Sony;
676 Config.TvVendor = CecVendorId.Yamaha;
679 Config.TvVendor = CecVendorId.Unknown;
685 cbVendorId.Enabled = false;
686 Config.TvVendor = CecVendorId.Unknown;
690 private void cbDeviceType_SelectedIndexChanged(object sender, EventArgs e)
692 CecDeviceType type = SelectedDeviceType;
693 if (type != Config.DeviceTypes.Types[0])
695 Config.DeviceTypes.Types[0] = type;
696 if (!DeviceChangeWarningDisplayed)
698 DeviceChangeWarningDisplayed = true;
699 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);
705 #region Key configuration tab
706 delegate void SelectKeypressRowCallback(CecKeypress key);
707 private void SelectKeypressRow(CecKeypress key)
709 if (dgButtons.InvokeRequired)
711 SelectKeypressRowCallback d = SelectKeypressRow;
714 Invoke(d, new object[] { key });
716 catch (Exception) { }
721 foreach (DataGridViewRow row in dgButtons.Rows)
723 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
724 if (item != null && item.Key.Keycode == key.Keycode)
726 rowIndex = row.Index;
732 row.Selected = false;
736 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
740 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
742 DataGridView grid = sender as DataGridView;
743 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
744 if (data == null || !data.Enabled)
745 e.CellStyle.ForeColor = Color.Gray;
749 #region CEC Tester tab
750 public void CheckActiveDevices()
752 CecLogicalAddresses activeDevices = Lib.GetActiveDevices();
753 List<string> deviceList = new List<string>();
754 foreach (CecLogicalAddress activeDevice in activeDevices.Addresses)
756 if (activeDevice != CecLogicalAddress.Unknown)
757 deviceList.Add(string.Format("{0,1:X} : {1}", (int)activeDevice, Lib.ToString(activeDevice)));
759 deviceList.Add(string.Format("{0,1:X} : {1}", (int)CecLogicalAddress.Broadcast, Lib.ToString(CecLogicalAddress.Broadcast)));
761 SetActiveDevices(deviceList.ToArray());
764 delegate void SetActiveDevicesCallback(string[] activeDevices);
765 private void SetActiveDevices(string[] activeDevices)
767 if (cbCommandDestination.InvokeRequired)
769 SetActiveDevicesCallback d = SetActiveDevices;
772 Invoke(d, new object[] { activeDevices });
774 catch (Exception) { }
778 cbCommandDestination.Items.Clear();
779 foreach (string item in activeDevices)
780 cbCommandDestination.Items.Add(item);
784 delegate CecLogicalAddress GetTargetDeviceCallback();
785 private CecLogicalAddress GetTargetDevice()
787 if (cbCommandDestination.InvokeRequired)
789 GetTargetDeviceCallback d = GetTargetDevice;
790 CecLogicalAddress retval = CecLogicalAddress.Unknown;
793 retval = (CecLogicalAddress)Invoke(d, new object[] { });
795 catch (Exception) { }
799 return GetLogicalAddressFromString(cbCommandDestination.Text);
802 private CecLogicalAddress GetLogicalAddressFromString(string name)
804 switch (name.Substring(0, 1).ToLower())
807 return CecLogicalAddress.Tv;
809 return CecLogicalAddress.RecordingDevice1;
811 return CecLogicalAddress.RecordingDevice2;
813 return CecLogicalAddress.Tuner1;
815 return CecLogicalAddress.PlaybackDevice1;
817 return CecLogicalAddress.AudioSystem;
819 return CecLogicalAddress.Tuner2;
821 return CecLogicalAddress.Tuner3;
823 return CecLogicalAddress.PlaybackDevice2;
825 return CecLogicalAddress.RecordingDevice3;
827 return CecLogicalAddress.Tuner4;
829 return CecLogicalAddress.PlaybackDevice3;
831 return CecLogicalAddress.Reserved1;
833 return CecLogicalAddress.Reserved2;
835 return CecLogicalAddress.FreeUse;
837 return CecLogicalAddress.Broadcast;
839 return CecLogicalAddress.Unknown;
843 private void bSendImageViewOn_Click(object sender, EventArgs e)
845 SendImageViewOn(GetTargetDevice());
848 private void bStandby_Click(object sender, EventArgs e)
850 SendStandby(GetTargetDevice());
853 private void bScan_Click(object sender, EventArgs e)
855 ShowDeviceInfo(GetTargetDevice());
858 private void bActivateSource_Click(object sender, EventArgs e)
860 ActivateSource(GetTargetDevice());
863 private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
865 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
866 bVolUp.Enabled = enableVolumeButtons;
867 bVolDown.Enabled = enableVolumeButtons;
868 bMute.Enabled = enableVolumeButtons;
869 bActivateSource.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
870 bScan.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
873 private void bVolUp_Click(object sender, EventArgs e)
875 SetControlsEnabled(false);
877 SetControlsEnabled(true);
880 private void bVolDown_Click(object sender, EventArgs e)
882 SetControlsEnabled(false);
883 Lib.VolumeDown(true);
884 SetControlsEnabled(true);
887 private void bMute_Click(object sender, EventArgs e)
889 SetControlsEnabled(false);
891 SetControlsEnabled(true);
894 private void bRescanDevices_Click(object sender, EventArgs e)
896 if (!SuppressUpdates && ActiveProcess == null)
898 SetControlsEnabled(false);
899 ActiveProcess = new RescanDevices(ref Lib);
900 ActiveProcess.EventHandler += ProcessEventHandler;
901 (new Thread(ActiveProcess.Run)).Start();
907 delegate void UpdateLogCallback();
908 private void UpdateLog()
910 if (tbLog.InvokeRequired)
912 UpdateLogCallback d = UpdateLog;
915 Invoke(d, new object[] { });
917 catch (Exception) { }
922 tbLog.Select(tbLog.Text.Length, 0);
923 tbLog.ScrollToCaret();
927 private void AddLogMessage(CecLogMessage message)
929 string strLevel = "";
930 bool display = false;
931 switch (message.Level)
933 case CecLogLevel.Error:
934 strLevel = "ERROR: ";
935 display = cbLogError.Checked;
937 case CecLogLevel.Warning:
938 strLevel = "WARNING: ";
939 display = cbLogWarning.Checked;
941 case CecLogLevel.Notice:
942 strLevel = "NOTICE: ";
943 display = cbLogNotice.Checked;
945 case CecLogLevel.Traffic:
946 strLevel = "TRAFFIC: ";
947 display = cbLogTraffic.Checked;
949 case CecLogLevel.Debug:
950 strLevel = "DEBUG: ";
951 display = cbLogDebug.Checked;
959 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
963 if (SelectedTab == ConfigTab.Log)
967 private void bClearLog_Click(object sender, EventArgs e)
973 private void bSaveLog_Click(object sender, EventArgs e)
975 SaveFileDialog dialog = new SaveFileDialog()
977 Title = "Where do you want to store the log file?",
978 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
979 FileName = "cec-log.txt",
980 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
984 if (dialog.ShowDialog() == DialogResult.OK)
986 FileStream fs = (FileStream)dialog.OpenFile();
989 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
993 StreamWriter writer = new StreamWriter(fs);
998 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
1004 #region LibCecSharp callbacks
1005 public int ConfigurationChanged(LibCECConfiguration config)
1008 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
1009 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
1010 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
1011 switch (config.DeviceTypes.Types[0])
1013 case CecDeviceType.RecordingDevice:
1014 SetControlText(cbDeviceType, "Recorder");
1016 case CecDeviceType.PlaybackDevice:
1017 SetControlText(cbDeviceType, "Player");
1019 case CecDeviceType.Tuner:
1020 SetControlText(cbDeviceType, "Tuner");
1023 SetControlText(cbDeviceType, "Recorder");
1026 if (config.TvVendor != CecVendorId.Unknown)
1028 SetCheckboxChecked(cbVendorOverride, true);
1029 SetControlText(cbVendorId, Lib.ToString(config.TvVendor));
1033 SetCheckboxChecked(cbVendorOverride, false);
1034 SetControlText(cbVendorId, Lib.ToString(TVVendor));
1037 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
1038 SetCheckboxChecked(cbActivateSource, Config.ActivateSource);
1039 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
1040 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
1041 UpdateSelectedDevice();
1043 for (int iPtr = 0; iPtr < 15; iPtr++)
1044 SetCheckboxItemChecked(cbWakeDevices, iPtr, Config.WakeDevices.IsSet((CecLogicalAddress)iPtr));
1045 for (int iPtr = 0; iPtr < 15; iPtr++)
1046 SetCheckboxItemChecked(cbPowerOffDevices, iPtr, Config.PowerOffDevices.IsSet((CecLogicalAddress)iPtr));
1048 SetControlText(this, "Pulse-Eight USB-CEC Adapter - libCEC " + Lib.ToString(Config.ServerVersion));
1052 public int ReceiveCommand(CecCommand command)
1057 public int ReceiveKeypress(CecKeypress key)
1059 SelectKeypressRow(key);
1063 public int ReceiveLogMessage(CecLogMessage message)
1067 AddLogMessage(message);
1069 catch (Exception) { }
1074 #region Class members
1075 public bool HasAVRDevice { get; private set; }
1077 private CecVendorId _tvVendor = CecVendorId.Unknown;
1078 public CecVendorId TVVendor
1080 get { return _tvVendor;}
1081 private set { _tvVendor = value; }
1083 public string TVVendorString
1087 return TVVendor != CecVendorId.Unknown ?
1088 "Television (" + Lib.ToString(TVVendor) + ")" :
1094 private CecVendorId _avrVendor = CecVendorId.Unknown;
1095 public CecVendorId AVRVendor
1097 get { return _avrVendor; }
1098 private set { _avrVendor = value; }
1100 public string AVRVendorString
1104 return AVRVendor != CecVendorId.Unknown ?
1105 "AVR (" + Lib.ToString(AVRVendor) + ")" :
1110 public CecLogicalAddress SelectedConnectedDevice
1114 return (cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
1117 public CecDeviceType SelectedDeviceType
1121 switch (cbDeviceType.Text.ToLower())
1124 return CecDeviceType.PlaybackDevice;
1126 return CecDeviceType.Tuner;
1128 return CecDeviceType.RecordingDevice;
1132 public int SelectedPortNumber
1136 int iPortNumber = 0;
1137 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
1142 protected LibCECConfiguration Config;
1143 protected LibCecSharp Lib;
1144 private CecCallbackWrapper Callbacks;
1145 private UpdateProcess ActiveProcess = null;
1146 private bool SuppressUpdates = true;
1147 private ConfigTab SelectedTab = ConfigTab.Configuration;
1148 private string Log = string.Empty;
1149 private DeviceInformation UpdatingInfoPanel = null;
1150 private bool DeviceChangeWarningDisplayed = false;
1151 public CecLogicalAddresses WakeDevices
1155 CecLogicalAddresses addr = new CecLogicalAddresses();
1156 foreach (object item in cbWakeDevices.CheckedItems)
1158 string c = item as string;
1159 addr.Set(GetLogicalAddressFromString(c));
1164 public CecLogicalAddresses PowerOffDevices
1168 CecLogicalAddresses addr = new CecLogicalAddresses();
1169 foreach (object item in cbPowerOffDevices.CheckedItems)
1171 string c = item as string;
1172 addr.Set(GetLogicalAddressFromString(c));
1181 /// A little wrapper that is needed because we already inherit form
1183 internal class CecCallbackWrapper : CecCallbackMethods
1185 public CecCallbackWrapper(CecConfigGUI gui)
1190 public override int ReceiveCommand(CecCommand command)
1192 return Gui.ReceiveCommand(command);
1195 public override int ReceiveKeypress(CecKeypress key)
1197 return Gui.ReceiveKeypress(key);
1200 public override int ReceiveLogMessage(CecLogMessage message)
1202 return Gui.ReceiveLogMessage(message);
1205 public override int ConfigurationChanged(LibCECConfiguration config)
1207 return Gui.ConfigurationChanged(config);
1210 private CecConfigGUI Gui;