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 //TODO read the com port setting from the configuration
43 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
44 if (adapters.Length == 0 || !Lib.Open(adapters[0].ComPort, 10000))
46 MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration and try again.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
50 ActiveProcess = new ConnectToDevice(ref Lib, Config);
51 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
52 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
55 private bool LoadXMLConfiguration(ref LibCECConfiguration config)
57 bool gotConfig = false;
58 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
59 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
60 string file = defaultDir + @"\usb_2548_1001.xml";
61 if (File.Exists(xbmcDir + @"\usb_2548_1001.xml"))
62 file = xbmcDir + @"\usb_2548_1001.xml";
64 if (File.Exists(file))
66 XmlTextReader reader = new XmlTextReader(file);
70 switch (reader.NodeType)
72 case XmlNodeType.Element:
73 if (reader.Name.ToLower() == "setting")
75 string name = string.Empty;
76 string value = string.Empty;
78 while (reader.MoveToNextAttribute())
80 if (reader.Name.ToLower().Equals("id"))
81 name = reader.Value.ToLower();
82 if (reader.Name.ToLower().Equals("value"))
91 if (byte.TryParse(value, out iPort))
92 config.HDMIPort = iPort;
95 case "connected_device":
98 if (ushort.TryParse(value, out iDevice))
99 config.BaseDevice = (CecLogicalAddress)iDevice;
102 case "cec_power_on_startup":
103 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
105 config.ActivateSource = true;
106 config.WakeDevices.Set(CecLogicalAddress.Tv);
109 case "cec_power_off_shutdown":
110 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
111 config.PowerOffDevices.Set(CecLogicalAddress.Broadcast);
113 case "cec_standby_screensaver":
114 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
116 case "standby_pc_on_tv_standby":
117 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
119 case "use_tv_menu_language":
120 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
123 case "physical_address":
125 ushort physicalAddress = 0;
126 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
127 config.PhysicalAddress = physicalAddress;
133 if (ushort.TryParse(value, out iType))
134 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
140 if (UInt64.TryParse(value, out iVendor))
141 config.TvVendor = (CecVendorId)iVendor;
146 config.WakeDevices.Clear();
147 string[] split = value.Split(new char[] { ' ' });
148 foreach (string dev in split)
150 byte iLogicalAddress;
151 if (byte.TryParse(dev, out iLogicalAddress))
152 config.WakeDevices.Set((CecLogicalAddress)iLogicalAddress);
156 case "standby_devices":
158 config.PowerOffDevices.Clear();
159 string[] split = value.Split(new char[] { ' ' });
160 foreach (string dev in split)
162 byte iLogicalAddress;
163 if (byte.TryParse(dev, out iLogicalAddress))
164 config.PowerOffDevices.Set((CecLogicalAddress)iLogicalAddress);
186 private void LoadButtonConfiguration()
188 //TODO load the real configuration
189 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecSharp.CecKeypress() { Keycode = 0x00 }), string.Empty));
190 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecSharp.CecKeypress() { Keycode = 0x01 }), string.Empty));
191 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecSharp.CecKeypress() { Keycode = 0x02 }), string.Empty));
192 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecSharp.CecKeypress() { Keycode = 0x03 }), string.Empty));
193 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecSharp.CecKeypress() { Keycode = 0x04 }), string.Empty));
194 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecSharp.CecKeypress() { Keycode = 0x05 }), string.Empty));
195 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecSharp.CecKeypress() { Keycode = 0x06 }), string.Empty));
196 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecSharp.CecKeypress() { Keycode = 0x07 }), string.Empty));
197 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecSharp.CecKeypress() { Keycode = 0x08 }), string.Empty));
198 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecSharp.CecKeypress() { Keycode = 0x09 }), string.Empty));
199 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecSharp.CecKeypress() { Keycode = 0x0A }), string.Empty));
200 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecSharp.CecKeypress() { Keycode = 0x0B }), string.Empty));
201 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecSharp.CecKeypress() { Keycode = 0x0C }), string.Empty));
202 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecSharp.CecKeypress() { Keycode = 0x0D }), string.Empty));
203 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecSharp.CecKeypress() { Keycode = 0x20 }), string.Empty));
204 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecSharp.CecKeypress() { Keycode = 0x21 }), string.Empty));
205 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecSharp.CecKeypress() { Keycode = 0x22 }), string.Empty));
206 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecSharp.CecKeypress() { Keycode = 0x23 }), string.Empty));
207 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecSharp.CecKeypress() { Keycode = 0x24 }), string.Empty));
208 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecSharp.CecKeypress() { Keycode = 0x25 }), string.Empty));
209 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecSharp.CecKeypress() { Keycode = 0x26 }), string.Empty));
210 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecSharp.CecKeypress() { Keycode = 0x27 }), string.Empty));
211 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecSharp.CecKeypress() { Keycode = 0x28 }), string.Empty));
212 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecSharp.CecKeypress() { Keycode = 0x29 }), string.Empty));
213 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecSharp.CecKeypress() { Keycode = 0x2A }), string.Empty));
214 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecSharp.CecKeypress() { Keycode = 0x2B }), string.Empty));
215 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecSharp.CecKeypress() { Keycode = 0x2C }), string.Empty));
216 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecSharp.CecKeypress() { Keycode = 0x2F }), string.Empty));
217 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecSharp.CecKeypress() { Keycode = 0x30 }), string.Empty));
218 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecSharp.CecKeypress() { Keycode = 0x31 }), string.Empty));
219 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecSharp.CecKeypress() { Keycode = 0x32 }), string.Empty));
220 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecSharp.CecKeypress() { Keycode = 0x33 }), string.Empty));
221 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecSharp.CecKeypress() { Keycode = 0x34 }), string.Empty));
222 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecSharp.CecKeypress() { Keycode = 0x35 }), string.Empty));
223 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecSharp.CecKeypress() { Keycode = 0x36 }), string.Empty));
224 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecSharp.CecKeypress() { Keycode = 0x37 }), string.Empty));
225 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecSharp.CecKeypress() { Keycode = 0x38 }), string.Empty));
226 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecSharp.CecKeypress() { Keycode = 0x40 }), string.Empty));
227 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecSharp.CecKeypress() { Keycode = 0x41 }), string.Empty));
228 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecSharp.CecKeypress() { Keycode = 0x42 }), string.Empty));
229 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecSharp.CecKeypress() { Keycode = 0x43 }), string.Empty));
230 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecSharp.CecKeypress() { Keycode = 0x44 }), string.Empty));
231 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecSharp.CecKeypress() { Keycode = 0x45 }), string.Empty));
232 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecSharp.CecKeypress() { Keycode = 0x46 }), string.Empty));
233 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecSharp.CecKeypress() { Keycode = 0x47 }), string.Empty));
234 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecSharp.CecKeypress() { Keycode = 0x48 }), string.Empty));
235 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecSharp.CecKeypress() { Keycode = 0x49 }), string.Empty));
236 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecSharp.CecKeypress() { Keycode = 0x4A }), string.Empty));
237 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecSharp.CecKeypress() { Keycode = 0x4B }), string.Empty));
238 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecSharp.CecKeypress() { Keycode = 0x4C }), string.Empty));
239 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecSharp.CecKeypress() { Keycode = 0x4D }), string.Empty));
240 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecSharp.CecKeypress() { Keycode = 0x4E }), string.Empty));
241 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecSharp.CecKeypress() { Keycode = 0x50 }), string.Empty));
242 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecSharp.CecKeypress() { Keycode = 0x51 }), string.Empty));
243 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecSharp.CecKeypress() { Keycode = 0x52 }), string.Empty));
244 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecSharp.CecKeypress() { Keycode = 0x53 }), string.Empty));
245 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecSharp.CecKeypress() { Keycode = 0x54 }), string.Empty));
246 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecSharp.CecKeypress() { Keycode = 0x55 }), string.Empty));
247 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecSharp.CecKeypress() { Keycode = 0x60 }), string.Empty));
248 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecSharp.CecKeypress() { Keycode = 0x61 }), string.Empty));
249 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecSharp.CecKeypress() { Keycode = 0x62 }), string.Empty));
250 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecSharp.CecKeypress() { Keycode = 0x63 }), string.Empty));
251 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecSharp.CecKeypress() { Keycode = 0x64 }), string.Empty));
252 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecSharp.CecKeypress() { Keycode = 0x65 }), string.Empty));
253 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecSharp.CecKeypress() { Keycode = 0x66 }), string.Empty));
254 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecSharp.CecKeypress() { Keycode = 0x67 }), string.Empty));
255 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecSharp.CecKeypress() { Keycode = 0x68 }), string.Empty));
256 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecSharp.CecKeypress() { Keycode = 0x69 }), string.Empty));
257 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecSharp.CecKeypress() { Keycode = 0x6A }), string.Empty));
258 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecSharp.CecKeypress() { Keycode = 0x6B }), string.Empty));
259 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecSharp.CecKeypress() { Keycode = 0x6C }), string.Empty));
260 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecSharp.CecKeypress() { Keycode = 0x6D }), string.Empty));
261 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecSharp.CecKeypress() { Keycode = 0x71 }), string.Empty));
262 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecSharp.CecKeypress() { Keycode = 0x72 }), string.Empty));
263 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecSharp.CecKeypress() { Keycode = 0x73 }), string.Empty));
264 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecSharp.CecKeypress() { Keycode = 0x74 }), string.Empty));
265 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecSharp.CecKeypress() { Keycode = 0x75 }), string.Empty));
266 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecSharp.CecKeypress() { Keycode = 0x76 }), string.Empty));
267 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty));
270 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
272 switch (updateEvent.Type)
274 case UpdateEventType.StatusText:
275 SetControlText(lStatus, updateEvent.StringValue);
277 case UpdateEventType.PhysicalAddress:
278 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
279 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
281 case UpdateEventType.ProgressBar:
282 SetProgressValue(pProgress, updateEvent.IntValue);
284 case UpdateEventType.TVVendorId:
285 TVVendor = (CecVendorId)updateEvent.IntValue;
286 UpdateSelectedDevice();
288 case UpdateEventType.BaseDevicePhysicalAddress:
289 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
291 case UpdateEventType.BaseDevice:
292 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
294 case UpdateEventType.HDMIPort:
295 Config.HDMIPort = (byte)updateEvent.IntValue;
297 case UpdateEventType.MenuLanguage:
298 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
300 case UpdateEventType.HasAVRDevice:
301 if (HasAVRDevice != updateEvent.BoolValue)
303 HasAVRDevice = updateEvent.BoolValue;
304 UpdateSelectedDevice();
307 case UpdateEventType.AVRVendorId:
308 AVRVendor = (CecVendorId)updateEvent.IntValue;
309 UpdateSelectedDevice();
311 case UpdateEventType.Configuration:
312 SuppressUpdates = true;
313 ConfigurationChanged(updateEvent.ConfigValue);
314 SuppressUpdates = false;
316 case UpdateEventType.ProcessCompleted:
317 ActiveProcess = null;
318 SetControlsEnabled(true);
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, false); // TODO not implemented yet
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);
341 SetControlEnabled(bSendImageViewOn, val);
342 SetControlEnabled(bStandby, val);
343 SetControlEnabled(bActivateSource, val);
344 SetControlEnabled(bScan, val);
346 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
347 SetControlEnabled(bVolUp, enableVolumeButtons);
348 SetControlEnabled(bVolDown, enableVolumeButtons);
349 SetControlEnabled(bMute, enableVolumeButtons);
352 private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
354 switch (tabControl1.SelectedIndex)
357 SelectedTab = ConfigTab.Configuration;
360 SelectedTab = ConfigTab.KeyConfiguration;
363 SelectedTab = ConfigTab.Tester;
366 SelectedTab = ConfigTab.Log;
370 SelectedTab = ConfigTab.Configuration;
376 public void ReloadXMLConfiguration()
378 LoadXMLConfiguration(ref Config);
379 Lib.SetConfiguration(Config);
380 ConfigurationChanged(Config);
383 public void SetPhysicalAddress(ushort physicalAddress)
385 if (!SuppressUpdates && ActiveProcess == null)
387 SetControlsEnabled(false);
388 SetControlText(cbPortNumber, string.Empty);
389 SetControlText(cbConnectedDevice, string.Empty);
390 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
391 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
392 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
396 public void SendImageViewOn(CecLogicalAddress address)
398 if (!SuppressUpdates && ActiveProcess == null)
400 SetControlsEnabled(false);
401 ActiveProcess = new SendImageViewOn(ref Lib, address);
402 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
403 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
407 public void ActivateSource(CecLogicalAddress address)
409 if (!SuppressUpdates && ActiveProcess == null)
411 SetControlsEnabled(false);
412 ActiveProcess = new SendActivateSource(ref Lib, address);
413 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
414 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
418 public void SendStandby(CecLogicalAddress address)
420 if (!SuppressUpdates && ActiveProcess == null)
422 SetControlsEnabled(false);
423 ActiveProcess = new SendStandby(ref Lib, address);
424 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
425 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
429 public void ShowDeviceInfo(CecLogicalAddress address)
431 if (!SuppressUpdates && ActiveProcess == null)
433 SetControlsEnabled(false);
434 ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
435 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
436 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
441 #region Configuration tab
442 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
444 if (tbPhysicalAddress.Text.Length != 4)
446 ushort physicalAddress = 0;
447 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
450 SetPhysicalAddress(physicalAddress);
453 private void UpdateSelectedDevice()
456 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
458 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
461 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
463 if (!SuppressUpdates && ActiveProcess == null)
465 SetControlsEnabled(false);
466 ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
467 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
468 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
472 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
474 SetConnectedDevice(SelectedConnectedDevice, SelectedPortNumber);
477 private void bCancel_Click(object sender, EventArgs e)
482 private void bSave_Click(object sender, EventArgs e)
484 SetControlsEnabled(false);
486 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
487 Config.ActivateSource = cbActivateSource.Checked;
488 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
489 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
491 if (!Lib.CanPersistConfiguration())
493 if (ActiveProcess == null)
495 SetControlsEnabled(false);
496 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
497 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
499 SaveFileDialog dialog = new SaveFileDialog()
501 Title = "Where do you want to store the settings?",
502 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
503 FileName = "usb_2548_1001.xml",
504 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
508 if (dialog.ShowDialog() == DialogResult.OK)
510 FileStream fs = null;
511 string error = string.Empty;
514 fs = (FileStream)dialog.OpenFile();
522 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing" + (error.Length > 0 ? ": " + error : string.Empty ), "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
526 StreamWriter writer = new StreamWriter(fs);
527 StringBuilder output = new StringBuilder();
528 output.AppendLine("<settings>");
529 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
530 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
531 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />");
532 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffDevices.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />");
533 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
534 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
535 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
536 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
537 output.AppendLine("<setting id=\"port\" value=\"\" />");
539 // only supported by 1.5.0+ clients
540 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
541 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
542 output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", (int)Config.TvVendor) + "\" />");
544 output.Append("<setting id=\"wake_devices\" value=\"");
545 foreach (CecLogicalAddress addr in Config.WakeDevices.Addresses)
546 if (addr != CecLogicalAddress.Unregistered)
547 output.Append(" " + addr);
548 output.AppendLine("\" />");
550 output.Append("<setting id=\"standby_devices\" value=\"");
551 foreach (CecLogicalAddress addr in Config.PowerOffDevices.Addresses)
552 if (addr != CecLogicalAddress.Unregistered)
553 output.Append(" " + addr);
554 output.AppendLine("\" />");
556 output.AppendLine("</settings>");
557 writer.Write(output.ToString());
561 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
564 SetControlsEnabled(true);
569 if (!Lib.PersistConfiguration(Config))
570 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
572 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
574 SetControlsEnabled(true);
577 private void bReloadConfig_Click(object sender, EventArgs e)
579 if (Lib.CanPersistConfiguration())
581 Lib.GetCurrentConfiguration(Config);
582 ConfigurationChanged(Config);
586 ReloadXMLConfiguration();
590 private void cbVendorOverride_CheckedChanged(object sender, EventArgs e)
592 if (cbVendorOverride.Checked)
594 cbVendorId.Enabled = true;
595 switch (cbVendorId.Text)
598 Config.TvVendor = CecVendorId.LG;
601 Config.TvVendor = CecVendorId.Onkyo;
604 Config.TvVendor = CecVendorId.Panasonic;
607 Config.TvVendor = CecVendorId.Philips;
610 Config.TvVendor = CecVendorId.Pioneer;
613 Config.TvVendor = CecVendorId.Samsung;
616 Config.TvVendor = CecVendorId.Sony;
619 Config.TvVendor = CecVendorId.Yamaha;
622 Config.TvVendor = CecVendorId.Unknown;
628 cbVendorId.Enabled = false;
629 Config.TvVendor = CecVendorId.Unknown;
634 #region Key configuration tab
635 delegate void SelectKeypressRowCallback(CecKeypress key);
636 private void SelectKeypressRow(CecKeypress key)
638 if (dgButtons.InvokeRequired)
640 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
643 this.Invoke(d, new object[] { key });
645 catch (Exception) { }
650 foreach (DataGridViewRow row in dgButtons.Rows)
652 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
653 if (item != null && item.Key.Keycode == key.Keycode)
655 rowIndex = row.Index;
661 row.Selected = false;
665 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
669 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
671 DataGridView grid = sender as DataGridView;
672 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
673 if (data == null || !data.Enabled)
674 e.CellStyle.ForeColor = Color.Gray;
678 #region CEC Tester tab
679 delegate CecLogicalAddress GetTargetDeviceCallback();
680 private CecLogicalAddress GetTargetDevice()
682 if (this.cbCommandDestination.InvokeRequired)
684 GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice);
685 CecLogicalAddress retval = CecLogicalAddress.Unknown;
688 retval = (CecLogicalAddress)this.Invoke(d, new object[] { });
690 catch (Exception) { }
694 switch (this.cbCommandDestination.Text.Substring(0, 1).ToLower())
697 return CecLogicalAddress.Tv;
699 return CecLogicalAddress.RecordingDevice1;
701 return CecLogicalAddress.RecordingDevice2;
703 return CecLogicalAddress.Tuner1;
705 return CecLogicalAddress.PlaybackDevice1;
707 return CecLogicalAddress.AudioSystem;
709 return CecLogicalAddress.Tuner2;
711 return CecLogicalAddress.Tuner3;
713 return CecLogicalAddress.PlaybackDevice2;
715 return CecLogicalAddress.RecordingDevice3;
717 return CecLogicalAddress.Tuner4;
719 return CecLogicalAddress.PlaybackDevice3;
721 return CecLogicalAddress.Reserved1;
723 return CecLogicalAddress.Reserved2;
725 return CecLogicalAddress.FreeUse;
727 return CecLogicalAddress.Broadcast;
729 return CecLogicalAddress.Unknown;
733 private void bSendImageViewOn_Click(object sender, EventArgs e)
735 SendImageViewOn(GetTargetDevice());
738 private void bStandby_Click(object sender, EventArgs e)
740 SendStandby(GetTargetDevice());
743 private void bScan_Click(object sender, EventArgs e)
745 ShowDeviceInfo(GetTargetDevice());
748 private void bActivateSource_Click(object sender, EventArgs e)
750 ActivateSource(GetTargetDevice());
753 private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
755 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
756 this.bVolUp.Enabled = enableVolumeButtons;
757 this.bVolDown.Enabled = enableVolumeButtons;
758 this.bMute.Enabled = enableVolumeButtons;
761 private void bVolUp_Click(object sender, EventArgs e)
763 SetControlsEnabled(false);
765 SetControlsEnabled(true);
768 private void bVolDown_Click(object sender, EventArgs e)
770 SetControlsEnabled(false);
771 Lib.VolumeDown(true);
772 SetControlsEnabled(true);
775 private void bMute_Click(object sender, EventArgs e)
777 SetControlsEnabled(false);
779 SetControlsEnabled(true);
784 delegate void UpdateLogCallback();
785 private void UpdateLog()
787 if (tbLog.InvokeRequired)
789 UpdateLogCallback d = new UpdateLogCallback(UpdateLog);
792 this.Invoke(d, new object[] { });
794 catch (Exception) { }
799 tbLog.Select(tbLog.Text.Length, 0);
800 tbLog.ScrollToCaret();
804 private void AddLogMessage(CecLogMessage message)
806 string strLevel = "";
807 bool display = false;
808 switch (message.Level)
810 case CecLogLevel.Error:
811 strLevel = "ERROR: ";
812 display = cbLogError.Checked;
814 case CecLogLevel.Warning:
815 strLevel = "WARNING: ";
816 display = cbLogWarning.Checked;
818 case CecLogLevel.Notice:
819 strLevel = "NOTICE: ";
820 display = cbLogNotice.Checked;
822 case CecLogLevel.Traffic:
823 strLevel = "TRAFFIC: ";
824 display = cbLogTraffic.Checked;
826 case CecLogLevel.Debug:
827 strLevel = "DEBUG: ";
828 display = cbLogDebug.Checked;
836 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
840 if (SelectedTab == ConfigTab.Log)
844 private void bClearLog_Click(object sender, EventArgs e)
846 tbLog.Text = string.Empty;
849 private void bSaveLog_Click(object sender, EventArgs e)
851 SaveFileDialog dialog = new SaveFileDialog()
853 Title = "Where do you want to store the log file?",
854 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
855 FileName = "cec-log.txt",
856 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
860 if (dialog.ShowDialog() == DialogResult.OK)
862 FileStream fs = (FileStream)dialog.OpenFile();
865 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
869 StreamWriter writer = new StreamWriter(fs);
870 writer.Write(tbLog.Text);
874 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
880 #region LibCecSharp callbacks
881 public int ConfigurationChanged(LibCECConfiguration config)
884 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
885 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
886 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
887 switch (config.DeviceTypes.Types[0])
889 case CecDeviceType.RecordingDevice:
890 SetControlText(cbDeviceType, "Recorder");
892 case CecDeviceType.PlaybackDevice:
893 SetControlText(cbDeviceType, "Player");
895 case CecDeviceType.Tuner:
896 SetControlText(cbDeviceType, "Tuner");
899 SetControlText(cbDeviceType, "Recorder");
902 if (config.TvVendor != CecVendorId.Unknown)
904 SetCheckboxChecked(cbVendorOverride, true);
905 SetControlText(cbVendorId, Lib.ToString(config.TvVendor));
909 SetCheckboxChecked(cbVendorOverride, false);
910 SetControlText(cbVendorId, Lib.ToString(TVVendor));
913 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
914 SetCheckboxChecked(cbActivateSource, Config.ActivateSource);
915 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
916 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
917 UpdateSelectedDevice();
921 public int ReceiveCommand(CecCommand command)
926 public int ReceiveKeypress(CecKeypress key)
928 SelectKeypressRow(key);
932 public int ReceiveLogMessage(CecLogMessage message)
934 AddLogMessage(message);
939 #region Class members
940 public bool HasAVRDevice { get; private set; }
942 private CecVendorId _tvVendor = CecVendorId.Unknown;
943 public CecVendorId TVVendor
945 get { return _tvVendor;}
946 private set { _tvVendor = value; }
948 public string TVVendorString
952 return TVVendor != CecVendorId.Unknown ?
953 "Television (" + Lib.ToString(TVVendor) + ")" :
959 private CecVendorId _avrVendor = CecVendorId.Unknown;
960 public CecVendorId AVRVendor
962 get { return _avrVendor; }
963 private set { _avrVendor = value; }
965 public string AVRVendorString
969 return AVRVendor != CecVendorId.Unknown ?
970 "AVR (" + Lib.ToString(AVRVendor) + ")" :
975 public CecLogicalAddress SelectedConnectedDevice
979 return (cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
982 public int SelectedPortNumber
987 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
992 protected LibCECConfiguration Config;
993 protected LibCecSharp Lib;
994 private CecCallbackWrapper Callbacks;
995 private UpdateProcess ActiveProcess = null;
996 private bool SuppressUpdates = false;
997 private ConfigTab SelectedTab = ConfigTab.Configuration;
998 private string Log = string.Empty;
1003 /// A little wrapper that is needed because we already inherit form
1005 internal class CecCallbackWrapper : CecCallbackMethods
1007 public CecCallbackWrapper(CecConfigGUI gui)
1012 public override int ReceiveCommand(CecCommand command)
1014 return Gui.ReceiveCommand(command);
1017 public override int ReceiveKeypress(CecKeypress key)
1019 return Gui.ReceiveKeypress(key);
1022 public override int ReceiveLogMessage(CecLogMessage message)
1024 return Gui.ReceiveLogMessage(message);
1027 public override int ConfigurationChanged(LibCECConfiguration config)
1029 return Gui.ConfigurationChanged(config);
1032 private CecConfigGUI Gui;