2 using System.Collections.Generic;
3 using System.ComponentModel;
7 using System.Windows.Forms;
8 using System.Threading;
10 using CecConfigGui.actions;
11 using System.Globalization;
15 namespace CecConfigGui
17 internal enum ConfigTab
25 public partial class CecConfigGUI : AsyncForm
29 Config = new LibCECConfiguration();
30 Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
31 Config.DeviceName = "CEC Config";
32 Config.GetSettingsFromROM = true;
33 Config.ClientVersion = CecClientVersion.Version1_5_0;
34 Callbacks = new CecCallbackWrapper(this);
35 Config.SetCallbacks(Callbacks);
36 LoadXMLConfiguration(ref Config);
37 Lib = new LibCecSharp(Config);
39 InitializeComponent();
40 LoadButtonConfiguration();
42 ActiveProcess = new ConnectToDevice(ref Lib, Config);
43 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
44 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
47 private bool LoadXMLConfiguration(ref LibCECConfiguration config)
49 bool gotConfig = false;
50 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
51 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
52 string file = defaultDir + @"\usb_2548_1001.xml";
53 if (File.Exists(xbmcDir + @"\usb_2548_1001.xml"))
54 file = xbmcDir + @"\usb_2548_1001.xml";
56 if (File.Exists(file))
58 XmlTextReader reader = new XmlTextReader(file);
62 switch (reader.NodeType)
64 case XmlNodeType.Element:
65 if (reader.Name.ToLower() == "setting")
67 string name = string.Empty;
68 string value = string.Empty;
70 while (reader.MoveToNextAttribute())
72 if (reader.Name.ToLower().Equals("id"))
73 name = reader.Value.ToLower();
74 if (reader.Name.ToLower().Equals("value"))
83 if (byte.TryParse(value, out iPort))
84 config.HDMIPort = iPort;
87 case "connected_device":
90 if (ushort.TryParse(value, out iDevice))
91 config.BaseDevice = (CecLogicalAddress)iDevice;
94 case "cec_power_on_startup":
95 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
97 config.ActivateSource = true;
98 config.WakeDevices.Set(CecLogicalAddress.Tv);
101 case "cec_power_off_shutdown":
102 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
103 config.PowerOffDevices.Set(CecLogicalAddress.Broadcast);
105 case "cec_standby_screensaver":
106 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
108 case "standby_pc_on_tv_standby":
109 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
111 case "use_tv_menu_language":
112 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
115 case "physical_address":
117 ushort physicalAddress = 0;
118 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
119 config.PhysicalAddress = physicalAddress;
125 if (ushort.TryParse(value, out iType))
126 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
132 if (UInt64.TryParse(value, out iVendor))
133 config.TvVendor = (CecVendorId)iVendor;
138 config.WakeDevices.Clear();
139 string[] split = value.Split(new char[] { ' ' });
140 foreach (string dev in split)
142 byte iLogicalAddress;
143 if (byte.TryParse(dev, out iLogicalAddress))
144 config.WakeDevices.Set((CecLogicalAddress)iLogicalAddress);
148 case "standby_devices":
150 config.PowerOffDevices.Clear();
151 string[] split = value.Split(new char[] { ' ' });
152 foreach (string dev in split)
154 byte iLogicalAddress;
155 if (byte.TryParse(dev, out iLogicalAddress))
156 config.PowerOffDevices.Set((CecLogicalAddress)iLogicalAddress);
178 private void LoadButtonConfiguration()
180 //TODO load the real configuration
181 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecSharp.CecKeypress() { Keycode = 0x00 }), string.Empty));
182 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecSharp.CecKeypress() { Keycode = 0x01 }), string.Empty));
183 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecSharp.CecKeypress() { Keycode = 0x02 }), string.Empty));
184 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecSharp.CecKeypress() { Keycode = 0x03 }), string.Empty));
185 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecSharp.CecKeypress() { Keycode = 0x04 }), string.Empty));
186 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecSharp.CecKeypress() { Keycode = 0x05 }), string.Empty));
187 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecSharp.CecKeypress() { Keycode = 0x06 }), string.Empty));
188 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecSharp.CecKeypress() { Keycode = 0x07 }), string.Empty));
189 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecSharp.CecKeypress() { Keycode = 0x08 }), string.Empty));
190 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecSharp.CecKeypress() { Keycode = 0x09 }), string.Empty));
191 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecSharp.CecKeypress() { Keycode = 0x0A }), string.Empty));
192 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecSharp.CecKeypress() { Keycode = 0x0B }), string.Empty));
193 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecSharp.CecKeypress() { Keycode = 0x0C }), string.Empty));
194 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecSharp.CecKeypress() { Keycode = 0x0D }), string.Empty));
195 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecSharp.CecKeypress() { Keycode = 0x20 }), string.Empty));
196 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecSharp.CecKeypress() { Keycode = 0x21 }), string.Empty));
197 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecSharp.CecKeypress() { Keycode = 0x22 }), string.Empty));
198 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecSharp.CecKeypress() { Keycode = 0x23 }), string.Empty));
199 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecSharp.CecKeypress() { Keycode = 0x24 }), string.Empty));
200 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecSharp.CecKeypress() { Keycode = 0x25 }), string.Empty));
201 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecSharp.CecKeypress() { Keycode = 0x26 }), string.Empty));
202 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecSharp.CecKeypress() { Keycode = 0x27 }), string.Empty));
203 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecSharp.CecKeypress() { Keycode = 0x28 }), string.Empty));
204 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecSharp.CecKeypress() { Keycode = 0x29 }), string.Empty));
205 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecSharp.CecKeypress() { Keycode = 0x2A }), string.Empty));
206 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecSharp.CecKeypress() { Keycode = 0x2B }), string.Empty));
207 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecSharp.CecKeypress() { Keycode = 0x2C }), string.Empty));
208 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecSharp.CecKeypress() { Keycode = 0x2F }), string.Empty));
209 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecSharp.CecKeypress() { Keycode = 0x30 }), string.Empty));
210 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecSharp.CecKeypress() { Keycode = 0x31 }), string.Empty));
211 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecSharp.CecKeypress() { Keycode = 0x32 }), string.Empty));
212 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecSharp.CecKeypress() { Keycode = 0x33 }), string.Empty));
213 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecSharp.CecKeypress() { Keycode = 0x34 }), string.Empty));
214 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecSharp.CecKeypress() { Keycode = 0x35 }), string.Empty));
215 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecSharp.CecKeypress() { Keycode = 0x36 }), string.Empty));
216 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecSharp.CecKeypress() { Keycode = 0x37 }), string.Empty));
217 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecSharp.CecKeypress() { Keycode = 0x38 }), string.Empty));
218 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecSharp.CecKeypress() { Keycode = 0x40 }), string.Empty));
219 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecSharp.CecKeypress() { Keycode = 0x41 }), string.Empty));
220 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecSharp.CecKeypress() { Keycode = 0x42 }), string.Empty));
221 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecSharp.CecKeypress() { Keycode = 0x43 }), string.Empty));
222 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecSharp.CecKeypress() { Keycode = 0x44 }), string.Empty));
223 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecSharp.CecKeypress() { Keycode = 0x45 }), string.Empty));
224 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecSharp.CecKeypress() { Keycode = 0x46 }), string.Empty));
225 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecSharp.CecKeypress() { Keycode = 0x47 }), string.Empty));
226 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecSharp.CecKeypress() { Keycode = 0x48 }), string.Empty));
227 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecSharp.CecKeypress() { Keycode = 0x49 }), string.Empty));
228 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecSharp.CecKeypress() { Keycode = 0x4A }), string.Empty));
229 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecSharp.CecKeypress() { Keycode = 0x4B }), string.Empty));
230 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecSharp.CecKeypress() { Keycode = 0x4C }), string.Empty));
231 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecSharp.CecKeypress() { Keycode = 0x4D }), string.Empty));
232 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecSharp.CecKeypress() { Keycode = 0x4E }), string.Empty));
233 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecSharp.CecKeypress() { Keycode = 0x50 }), string.Empty));
234 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecSharp.CecKeypress() { Keycode = 0x51 }), string.Empty));
235 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecSharp.CecKeypress() { Keycode = 0x52 }), string.Empty));
236 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecSharp.CecKeypress() { Keycode = 0x53 }), string.Empty));
237 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecSharp.CecKeypress() { Keycode = 0x54 }), string.Empty));
238 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecSharp.CecKeypress() { Keycode = 0x55 }), string.Empty));
239 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecSharp.CecKeypress() { Keycode = 0x60 }), string.Empty));
240 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecSharp.CecKeypress() { Keycode = 0x61 }), string.Empty));
241 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecSharp.CecKeypress() { Keycode = 0x62 }), string.Empty));
242 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecSharp.CecKeypress() { Keycode = 0x63 }), string.Empty));
243 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecSharp.CecKeypress() { Keycode = 0x64 }), string.Empty));
244 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecSharp.CecKeypress() { Keycode = 0x65 }), string.Empty));
245 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecSharp.CecKeypress() { Keycode = 0x66 }), string.Empty));
246 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecSharp.CecKeypress() { Keycode = 0x67 }), string.Empty));
247 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecSharp.CecKeypress() { Keycode = 0x68 }), string.Empty));
248 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecSharp.CecKeypress() { Keycode = 0x69 }), string.Empty));
249 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecSharp.CecKeypress() { Keycode = 0x6A }), string.Empty));
250 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecSharp.CecKeypress() { Keycode = 0x6B }), string.Empty));
251 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecSharp.CecKeypress() { Keycode = 0x6C }), string.Empty));
252 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecSharp.CecKeypress() { Keycode = 0x6D }), string.Empty));
253 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecSharp.CecKeypress() { Keycode = 0x71 }), string.Empty));
254 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecSharp.CecKeypress() { Keycode = 0x72 }), string.Empty));
255 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecSharp.CecKeypress() { Keycode = 0x73 }), string.Empty));
256 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecSharp.CecKeypress() { Keycode = 0x74 }), string.Empty));
257 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecSharp.CecKeypress() { Keycode = 0x75 }), string.Empty));
258 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecSharp.CecKeypress() { Keycode = 0x76 }), string.Empty));
259 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty));
262 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
264 switch (updateEvent.Type)
266 case UpdateEventType.StatusText:
267 SetControlText(lStatus, updateEvent.StringValue);
269 case UpdateEventType.PhysicalAddress:
270 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
271 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
273 case UpdateEventType.ProgressBar:
274 SetProgressValue(pProgress, updateEvent.IntValue);
276 case UpdateEventType.TVVendorId:
277 TVVendor = (CecVendorId)updateEvent.IntValue;
278 UpdateSelectedDevice();
280 case UpdateEventType.BaseDevicePhysicalAddress:
281 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
283 case UpdateEventType.BaseDevice:
284 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
286 case UpdateEventType.HDMIPort:
287 Config.HDMIPort = (byte)updateEvent.IntValue;
289 case UpdateEventType.MenuLanguage:
290 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
292 case UpdateEventType.HasAVRDevice:
293 if (HasAVRDevice != updateEvent.BoolValue)
295 HasAVRDevice = updateEvent.BoolValue;
296 UpdateSelectedDevice();
299 case UpdateEventType.AVRVendorId:
300 AVRVendor = (CecVendorId)updateEvent.IntValue;
301 UpdateSelectedDevice();
303 case UpdateEventType.Configuration:
304 SuppressUpdates = true;
305 ConfigurationChanged(updateEvent.ConfigValue);
306 SuppressUpdates = false;
308 case UpdateEventType.PollDevices:
309 CheckActiveDevices();
311 case UpdateEventType.ProcessCompleted:
312 ActiveProcess = null;
313 SetControlsEnabled(true);
314 if (UpdatingInfoPanel != null)
316 UpdatingInfoPanel.SetControlEnabled(UpdatingInfoPanel.bUpdate, true);
317 UpdatingInfoPanel = null;
323 private void SetControlsEnabled(bool val)
325 SetControlEnabled(cbPortNumber, val);
326 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
327 SetControlEnabled(tbPhysicalAddress, val);
328 SetControlEnabled(cbDeviceType, val);
329 SetControlEnabled(cbUseTVMenuLanguage, val);
330 SetControlEnabled(cbActivateSource, val);
331 SetControlEnabled(cbPowerOffScreensaver, val);
332 SetControlEnabled(cbPowerOffOnStandby, val);
333 SetControlEnabled(cbWakeDevices, false); // TODO not implemented yet
334 SetControlEnabled(cbPowerOffDevices, false); // TODO not implemented yet
335 SetControlEnabled(cbVendorOverride, val);
336 SetControlEnabled(cbVendorId, val && cbVendorOverride.Checked);
337 SetControlEnabled(bClose, val);
338 SetControlEnabled(bSaveConfig, val);
339 SetControlEnabled(bReloadConfig, val);
340 SetControlEnabled(bRescanDevices, val);
342 SetControlEnabled(bSendImageViewOn, val);
343 SetControlEnabled(bStandby, val);
344 SetControlEnabled(bActivateSource, val);
345 SetControlEnabled(bScan, val);
347 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
348 SetControlEnabled(bVolUp, enableVolumeButtons);
349 SetControlEnabled(bVolDown, enableVolumeButtons);
350 SetControlEnabled(bMute, enableVolumeButtons);
353 private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
355 switch (tabControl1.SelectedIndex)
358 SelectedTab = ConfigTab.Configuration;
361 SelectedTab = ConfigTab.KeyConfiguration;
364 SelectedTab = ConfigTab.Tester;
367 SelectedTab = ConfigTab.Log;
371 SelectedTab = ConfigTab.Configuration;
377 public void ReloadXMLConfiguration()
379 LoadXMLConfiguration(ref Config);
380 Lib.SetConfiguration(Config);
381 ConfigurationChanged(Config);
384 public void UpdateInfoPanel(DeviceInformation panel)
386 if (!SuppressUpdates && ActiveProcess == null)
388 SetControlsEnabled(false);
389 UpdatingInfoPanel = panel;
390 panel.SetControlEnabled(panel.bUpdate, false);
391 ActiveProcess = new UpdateDeviceInfo(this, ref Lib, panel);
392 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
393 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
397 public void SetPhysicalAddress(ushort physicalAddress)
399 if (!SuppressUpdates && ActiveProcess == null)
401 SetControlsEnabled(false);
402 SetControlText(cbPortNumber, string.Empty);
403 SetControlText(cbConnectedDevice, string.Empty);
404 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
405 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
406 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
410 public void UpdateConfigurationAsync()
412 if (!SuppressUpdates && ActiveProcess == null)
414 SetControlsEnabled(false);
415 ActiveProcess = new UpdateConfiguration(ref Lib, Config);
416 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
417 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
421 public void SendImageViewOn(CecLogicalAddress address)
423 if (!SuppressUpdates && ActiveProcess == null)
425 SetControlsEnabled(false);
426 ActiveProcess = new SendImageViewOn(ref Lib, address);
427 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
428 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
432 public void ActivateSource(CecLogicalAddress address)
434 if (!SuppressUpdates && ActiveProcess == null)
436 SetControlsEnabled(false);
437 ActiveProcess = new SendActivateSource(ref Lib, address);
438 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
439 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
443 public void SendStandby(CecLogicalAddress address)
445 if (!SuppressUpdates && ActiveProcess == null)
447 SetControlsEnabled(false);
448 ActiveProcess = new SendStandby(ref Lib, address);
449 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
450 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
454 public void ShowDeviceInfo(CecLogicalAddress address)
456 if (!SuppressUpdates && ActiveProcess == null)
458 SetControlsEnabled(false);
459 ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
460 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
461 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
466 #region Configuration tab
467 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
469 if (tbPhysicalAddress.Text.Length != 4)
471 ushort physicalAddress = 0;
472 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
475 SetPhysicalAddress(physicalAddress);
478 private void UpdateSelectedDevice()
481 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
483 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
486 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
488 if (!SuppressUpdates && ActiveProcess == null)
490 SetControlsEnabled(false);
491 ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
492 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
493 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
497 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
499 SetConnectedDevice(SelectedConnectedDevice, SelectedPortNumber);
502 private void bCancel_Click(object sender, EventArgs e)
507 private void bSave_Click(object sender, EventArgs e)
509 SetControlsEnabled(false);
511 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
512 Config.ActivateSource = cbActivateSource.Checked;
513 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
514 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
516 if (!Lib.CanPersistConfiguration())
518 if (ActiveProcess == null)
520 SetControlsEnabled(false);
521 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
522 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
524 SaveFileDialog dialog = new SaveFileDialog()
526 Title = "Where do you want to store the settings?",
527 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
528 FileName = "usb_2548_1001.xml",
529 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
533 if (dialog.ShowDialog() == DialogResult.OK)
535 FileStream fs = null;
536 string error = string.Empty;
539 fs = (FileStream)dialog.OpenFile();
547 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing" + (error.Length > 0 ? ": " + error : string.Empty ), "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
551 StreamWriter writer = new StreamWriter(fs);
552 StringBuilder output = new StringBuilder();
553 output.AppendLine("<settings>");
554 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
555 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
556 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />");
557 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffDevices.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />");
558 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
559 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
560 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
561 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
562 output.AppendLine("<setting id=\"port\" value=\"\" />");
564 // only supported by 1.5.0+ clients
565 output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->");
566 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
567 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
568 output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", (int)Config.TvVendor) + "\" />");
570 output.Append("<setting id=\"wake_devices\" value=\"");
571 foreach (CecLogicalAddress addr in Config.WakeDevices.Addresses)
572 if (addr != CecLogicalAddress.Unregistered)
573 output.Append(" " + addr);
574 output.AppendLine("\" />");
576 output.Append("<setting id=\"standby_devices\" value=\"");
577 foreach (CecLogicalAddress addr in Config.PowerOffDevices.Addresses)
578 if (addr != CecLogicalAddress.Unregistered)
579 output.Append(" " + addr);
580 output.AppendLine("\" />");
582 output.AppendLine("</settings>");
583 writer.Write(output.ToString());
587 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
590 SetControlsEnabled(true);
595 if (!Lib.PersistConfiguration(Config))
596 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
598 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
600 SetControlsEnabled(true);
603 private void bReloadConfig_Click(object sender, EventArgs e)
605 if (Lib.CanPersistConfiguration())
607 Lib.GetCurrentConfiguration(Config);
608 ConfigurationChanged(Config);
612 ReloadXMLConfiguration();
616 private void cbVendorOverride_CheckedChanged(object sender, EventArgs e)
618 if (cbVendorOverride.Checked)
620 cbVendorId.Enabled = true;
621 switch (cbVendorId.Text)
624 Config.TvVendor = CecVendorId.LG;
627 Config.TvVendor = CecVendorId.Onkyo;
630 Config.TvVendor = CecVendorId.Panasonic;
633 Config.TvVendor = CecVendorId.Philips;
636 Config.TvVendor = CecVendorId.Pioneer;
639 Config.TvVendor = CecVendorId.Samsung;
642 Config.TvVendor = CecVendorId.Sony;
645 Config.TvVendor = CecVendorId.Yamaha;
648 Config.TvVendor = CecVendorId.Unknown;
654 cbVendorId.Enabled = false;
655 Config.TvVendor = CecVendorId.Unknown;
659 private void cbDeviceType_SelectedIndexChanged(object sender, EventArgs e)
661 CecDeviceType type = SelectedDeviceType;
662 if (type != Config.DeviceTypes.Types[0])
664 Config.DeviceTypes.Types[0] = type;
665 if (!DeviceChangeWarningDisplayed)
667 DeviceChangeWarningDisplayed = true;
668 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);
674 #region Key configuration tab
675 delegate void SelectKeypressRowCallback(CecKeypress key);
676 private void SelectKeypressRow(CecKeypress key)
678 if (dgButtons.InvokeRequired)
680 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
683 this.Invoke(d, new object[] { key });
685 catch (Exception) { }
690 foreach (DataGridViewRow row in dgButtons.Rows)
692 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
693 if (item != null && item.Key.Keycode == key.Keycode)
695 rowIndex = row.Index;
701 row.Selected = false;
705 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
709 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
711 DataGridView grid = sender as DataGridView;
712 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
713 if (data == null || !data.Enabled)
714 e.CellStyle.ForeColor = Color.Gray;
718 #region CEC Tester tab
719 public void CheckActiveDevices()
721 CecLogicalAddresses activeDevices = Lib.GetActiveDevices();
722 List<string> deviceList = new List<string>();
723 foreach (CecLogicalAddress activeDevice in activeDevices.Addresses)
725 if (activeDevice != CecLogicalAddress.Unregistered)
726 deviceList.Add(string.Format("{0,1:X} : {1}", (int)activeDevice, Lib.ToString(activeDevice)));
728 deviceList.Add(string.Format("{0,1:X} : {1}", (int)CecLogicalAddress.Broadcast, Lib.ToString(CecLogicalAddress.Broadcast)));
730 SetActiveDevices(deviceList.ToArray());
733 delegate void SetActiveDevicesCallback(string[] activeDevices);
734 private void SetActiveDevices(string[] activeDevices)
736 if (this.cbCommandDestination.InvokeRequired)
738 SetActiveDevicesCallback d = new SetActiveDevicesCallback(SetActiveDevices);
741 this.Invoke(d, new object[] { activeDevices });
743 catch (Exception) { }
747 this.cbCommandDestination.Items.Clear();
748 foreach (string item in activeDevices)
749 this.cbCommandDestination.Items.Add(item);
753 delegate CecLogicalAddress GetTargetDeviceCallback();
754 private CecLogicalAddress GetTargetDevice()
756 if (this.cbCommandDestination.InvokeRequired)
758 GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice);
759 CecLogicalAddress retval = CecLogicalAddress.Unknown;
762 retval = (CecLogicalAddress)this.Invoke(d, new object[] { });
764 catch (Exception) { }
768 switch (this.cbCommandDestination.Text.Substring(0, 1).ToLower())
771 return CecLogicalAddress.Tv;
773 return CecLogicalAddress.RecordingDevice1;
775 return CecLogicalAddress.RecordingDevice2;
777 return CecLogicalAddress.Tuner1;
779 return CecLogicalAddress.PlaybackDevice1;
781 return CecLogicalAddress.AudioSystem;
783 return CecLogicalAddress.Tuner2;
785 return CecLogicalAddress.Tuner3;
787 return CecLogicalAddress.PlaybackDevice2;
789 return CecLogicalAddress.RecordingDevice3;
791 return CecLogicalAddress.Tuner4;
793 return CecLogicalAddress.PlaybackDevice3;
795 return CecLogicalAddress.Reserved1;
797 return CecLogicalAddress.Reserved2;
799 return CecLogicalAddress.FreeUse;
801 return CecLogicalAddress.Broadcast;
803 return CecLogicalAddress.Unknown;
807 private void bSendImageViewOn_Click(object sender, EventArgs e)
809 SendImageViewOn(GetTargetDevice());
812 private void bStandby_Click(object sender, EventArgs e)
814 SendStandby(GetTargetDevice());
817 private void bScan_Click(object sender, EventArgs e)
819 ShowDeviceInfo(GetTargetDevice());
822 private void bActivateSource_Click(object sender, EventArgs e)
824 ActivateSource(GetTargetDevice());
827 private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
829 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
830 this.bVolUp.Enabled = enableVolumeButtons;
831 this.bVolDown.Enabled = enableVolumeButtons;
832 this.bMute.Enabled = enableVolumeButtons;
833 this.bActivateSource.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
834 this.bScan.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
837 private void bVolUp_Click(object sender, EventArgs e)
839 SetControlsEnabled(false);
841 SetControlsEnabled(true);
844 private void bVolDown_Click(object sender, EventArgs e)
846 SetControlsEnabled(false);
847 Lib.VolumeDown(true);
848 SetControlsEnabled(true);
851 private void bMute_Click(object sender, EventArgs e)
853 SetControlsEnabled(false);
855 SetControlsEnabled(true);
858 private void bRescanDevices_Click(object sender, EventArgs e)
860 if (!SuppressUpdates && ActiveProcess == null)
862 SetControlsEnabled(false);
863 ActiveProcess = new RescanDevices();
864 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
865 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
871 delegate void UpdateLogCallback();
872 private void UpdateLog()
874 if (tbLog.InvokeRequired)
876 UpdateLogCallback d = new UpdateLogCallback(UpdateLog);
879 this.Invoke(d, new object[] { });
881 catch (Exception) { }
886 tbLog.Select(tbLog.Text.Length, 0);
887 tbLog.ScrollToCaret();
891 private void AddLogMessage(CecLogMessage message)
893 string strLevel = "";
894 bool display = false;
895 switch (message.Level)
897 case CecLogLevel.Error:
898 strLevel = "ERROR: ";
899 display = cbLogError.Checked;
901 case CecLogLevel.Warning:
902 strLevel = "WARNING: ";
903 display = cbLogWarning.Checked;
905 case CecLogLevel.Notice:
906 strLevel = "NOTICE: ";
907 display = cbLogNotice.Checked;
909 case CecLogLevel.Traffic:
910 strLevel = "TRAFFIC: ";
911 display = cbLogTraffic.Checked;
913 case CecLogLevel.Debug:
914 strLevel = "DEBUG: ";
915 display = cbLogDebug.Checked;
923 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
927 if (SelectedTab == ConfigTab.Log)
931 private void bClearLog_Click(object sender, EventArgs e)
937 private void bSaveLog_Click(object sender, EventArgs e)
939 SaveFileDialog dialog = new SaveFileDialog()
941 Title = "Where do you want to store the log file?",
942 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
943 FileName = "cec-log.txt",
944 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
948 if (dialog.ShowDialog() == DialogResult.OK)
950 FileStream fs = (FileStream)dialog.OpenFile();
953 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
957 StreamWriter writer = new StreamWriter(fs);
962 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
968 #region LibCecSharp callbacks
969 public int ConfigurationChanged(LibCECConfiguration config)
972 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
973 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
974 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
975 switch (config.DeviceTypes.Types[0])
977 case CecDeviceType.RecordingDevice:
978 SetControlText(cbDeviceType, "Recorder");
980 case CecDeviceType.PlaybackDevice:
981 SetControlText(cbDeviceType, "Player");
983 case CecDeviceType.Tuner:
984 SetControlText(cbDeviceType, "Tuner");
987 SetControlText(cbDeviceType, "Recorder");
990 if (config.TvVendor != CecVendorId.Unknown)
992 SetCheckboxChecked(cbVendorOverride, true);
993 SetControlText(cbVendorId, Lib.ToString(config.TvVendor));
997 SetCheckboxChecked(cbVendorOverride, false);
998 SetControlText(cbVendorId, Lib.ToString(TVVendor));
1001 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
1002 SetCheckboxChecked(cbActivateSource, Config.ActivateSource);
1003 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
1004 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
1005 UpdateSelectedDevice();
1009 public int ReceiveCommand(CecCommand command)
1014 public int ReceiveKeypress(CecKeypress key)
1016 SelectKeypressRow(key);
1020 public int ReceiveLogMessage(CecLogMessage message)
1022 AddLogMessage(message);
1027 #region Class members
1028 public bool HasAVRDevice { get; private set; }
1030 private CecVendorId _tvVendor = CecVendorId.Unknown;
1031 public CecVendorId TVVendor
1033 get { return _tvVendor;}
1034 private set { _tvVendor = value; }
1036 public string TVVendorString
1040 return TVVendor != CecVendorId.Unknown ?
1041 "Television (" + Lib.ToString(TVVendor) + ")" :
1047 private CecVendorId _avrVendor = CecVendorId.Unknown;
1048 public CecVendorId AVRVendor
1050 get { return _avrVendor; }
1051 private set { _avrVendor = value; }
1053 public string AVRVendorString
1057 return AVRVendor != CecVendorId.Unknown ?
1058 "AVR (" + Lib.ToString(AVRVendor) + ")" :
1063 public CecLogicalAddress SelectedConnectedDevice
1067 return (cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
1070 public CecDeviceType SelectedDeviceType
1074 switch (cbDeviceType.Text.ToLower())
1077 return CecDeviceType.PlaybackDevice;
1079 return CecDeviceType.Tuner;
1081 return CecDeviceType.RecordingDevice;
1085 public int SelectedPortNumber
1089 int iPortNumber = 0;
1090 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
1095 protected LibCECConfiguration Config;
1096 protected LibCecSharp Lib;
1097 private CecCallbackWrapper Callbacks;
1098 private UpdateProcess ActiveProcess = null;
1099 private bool SuppressUpdates = true;
1100 private ConfigTab SelectedTab = ConfigTab.Configuration;
1101 private string Log = string.Empty;
1102 private DeviceInformation UpdatingInfoPanel = null;
1103 private bool DeviceChangeWarningDisplayed = false;
1108 /// A little wrapper that is needed because we already inherit form
1110 internal class CecCallbackWrapper : CecCallbackMethods
1112 public CecCallbackWrapper(CecConfigGUI gui)
1117 public override int ReceiveCommand(CecCommand command)
1119 return Gui.ReceiveCommand(command);
1122 public override int ReceiveKeypress(CecKeypress key)
1124 return Gui.ReceiveKeypress(key);
1127 public override int ReceiveLogMessage(CecLogMessage message)
1129 return Gui.ReceiveLogMessage(message);
1132 public override int ConfigurationChanged(LibCECConfiguration config)
1134 return Gui.ConfigurationChanged(config);
1137 private CecConfigGUI Gui;