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 public partial class CecConfigGUI : Form
21 Config = new LibCECConfiguration();
22 Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
23 Config.DeviceName = "CEC Config";
24 Config.GetSettingsFromROM = true;
25 Config.ClientVersion = CecClientVersion.Version1_5_0;
26 Callbacks = new CecCallbackWrapper(this);
27 Config.SetCallbacks(Callbacks);
28 LoadXMLConfiguration(ref Config);
29 Lib = new LibCecSharp(Config);
31 InitializeComponent();
32 LoadButtonConfiguration();
34 //TODO read the com port setting from the configuration
35 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
36 if (adapters.Length == 0 || !Lib.Open(adapters[0].ComPort, 10000))
38 MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
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 "physical_address":
96 ushort physicalAddress = 0;
97 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
98 config.PhysicalAddress = physicalAddress;
104 if (ushort.TryParse(value, out iType))
105 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
108 case "cec_power_on_startup":
109 config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
111 case "cec_power_off_shutdown":
112 config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
114 case "cec_standby_screensaver":
115 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
117 case "standby_pc_on_tv_standby":
118 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
120 case "use_tv_menu_language":
121 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
141 private void LoadButtonConfiguration()
143 //TODO load the real configuration
144 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecSharp.CecKeypress() { Keycode = 0x00 }), string.Empty));
145 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecSharp.CecKeypress() { Keycode = 0x01 }), string.Empty));
146 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecSharp.CecKeypress() { Keycode = 0x02 }), string.Empty));
147 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecSharp.CecKeypress() { Keycode = 0x03 }), string.Empty));
148 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecSharp.CecKeypress() { Keycode = 0x04 }), string.Empty));
149 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecSharp.CecKeypress() { Keycode = 0x05 }), string.Empty));
150 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecSharp.CecKeypress() { Keycode = 0x06 }), string.Empty));
151 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecSharp.CecKeypress() { Keycode = 0x07 }), string.Empty));
152 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecSharp.CecKeypress() { Keycode = 0x08 }), string.Empty));
153 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecSharp.CecKeypress() { Keycode = 0x09 }), string.Empty));
154 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecSharp.CecKeypress() { Keycode = 0x0A }), string.Empty));
155 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecSharp.CecKeypress() { Keycode = 0x0B }), string.Empty));
156 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecSharp.CecKeypress() { Keycode = 0x0C }), string.Empty));
157 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecSharp.CecKeypress() { Keycode = 0x0D }), string.Empty));
158 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecSharp.CecKeypress() { Keycode = 0x20 }), string.Empty));
159 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecSharp.CecKeypress() { Keycode = 0x21 }), string.Empty));
160 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecSharp.CecKeypress() { Keycode = 0x22 }), string.Empty));
161 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecSharp.CecKeypress() { Keycode = 0x23 }), string.Empty));
162 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecSharp.CecKeypress() { Keycode = 0x24 }), string.Empty));
163 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecSharp.CecKeypress() { Keycode = 0x25 }), string.Empty));
164 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecSharp.CecKeypress() { Keycode = 0x26 }), string.Empty));
165 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecSharp.CecKeypress() { Keycode = 0x27 }), string.Empty));
166 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecSharp.CecKeypress() { Keycode = 0x28 }), string.Empty));
167 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecSharp.CecKeypress() { Keycode = 0x29 }), string.Empty));
168 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecSharp.CecKeypress() { Keycode = 0x2A }), string.Empty));
169 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecSharp.CecKeypress() { Keycode = 0x2B }), string.Empty));
170 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecSharp.CecKeypress() { Keycode = 0x2C }), string.Empty));
171 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecSharp.CecKeypress() { Keycode = 0x2F }), string.Empty));
172 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecSharp.CecKeypress() { Keycode = 0x30 }), string.Empty));
173 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecSharp.CecKeypress() { Keycode = 0x31 }), string.Empty));
174 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecSharp.CecKeypress() { Keycode = 0x32 }), string.Empty));
175 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecSharp.CecKeypress() { Keycode = 0x33 }), string.Empty));
176 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecSharp.CecKeypress() { Keycode = 0x34 }), string.Empty));
177 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecSharp.CecKeypress() { Keycode = 0x35 }), string.Empty));
178 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecSharp.CecKeypress() { Keycode = 0x36 }), string.Empty));
179 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecSharp.CecKeypress() { Keycode = 0x37 }), string.Empty));
180 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecSharp.CecKeypress() { Keycode = 0x38 }), string.Empty));
181 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecSharp.CecKeypress() { Keycode = 0x40 }), string.Empty));
182 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecSharp.CecKeypress() { Keycode = 0x41 }), string.Empty));
183 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecSharp.CecKeypress() { Keycode = 0x42 }), string.Empty));
184 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecSharp.CecKeypress() { Keycode = 0x43 }), string.Empty));
185 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecSharp.CecKeypress() { Keycode = 0x44 }), string.Empty));
186 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecSharp.CecKeypress() { Keycode = 0x45 }), string.Empty));
187 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecSharp.CecKeypress() { Keycode = 0x46 }), string.Empty));
188 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecSharp.CecKeypress() { Keycode = 0x47 }), string.Empty));
189 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecSharp.CecKeypress() { Keycode = 0x48 }), string.Empty));
190 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecSharp.CecKeypress() { Keycode = 0x49 }), string.Empty));
191 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecSharp.CecKeypress() { Keycode = 0x4A }), string.Empty));
192 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecSharp.CecKeypress() { Keycode = 0x4B }), string.Empty));
193 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecSharp.CecKeypress() { Keycode = 0x4C }), string.Empty));
194 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecSharp.CecKeypress() { Keycode = 0x4D }), string.Empty));
195 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecSharp.CecKeypress() { Keycode = 0x4E }), string.Empty));
196 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecSharp.CecKeypress() { Keycode = 0x50 }), string.Empty));
197 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecSharp.CecKeypress() { Keycode = 0x51 }), string.Empty));
198 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecSharp.CecKeypress() { Keycode = 0x52 }), string.Empty));
199 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecSharp.CecKeypress() { Keycode = 0x53 }), string.Empty));
200 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecSharp.CecKeypress() { Keycode = 0x54 }), string.Empty));
201 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecSharp.CecKeypress() { Keycode = 0x55 }), string.Empty));
202 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecSharp.CecKeypress() { Keycode = 0x60 }), string.Empty));
203 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecSharp.CecKeypress() { Keycode = 0x61 }), string.Empty));
204 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecSharp.CecKeypress() { Keycode = 0x62 }), string.Empty));
205 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecSharp.CecKeypress() { Keycode = 0x63 }), string.Empty));
206 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecSharp.CecKeypress() { Keycode = 0x64 }), string.Empty));
207 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecSharp.CecKeypress() { Keycode = 0x65 }), string.Empty));
208 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecSharp.CecKeypress() { Keycode = 0x66 }), string.Empty));
209 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecSharp.CecKeypress() { Keycode = 0x67 }), string.Empty));
210 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecSharp.CecKeypress() { Keycode = 0x68 }), string.Empty));
211 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecSharp.CecKeypress() { Keycode = 0x69 }), string.Empty));
212 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecSharp.CecKeypress() { Keycode = 0x6A }), string.Empty));
213 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecSharp.CecKeypress() { Keycode = 0x6B }), string.Empty));
214 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecSharp.CecKeypress() { Keycode = 0x6C }), string.Empty));
215 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecSharp.CecKeypress() { Keycode = 0x6D }), string.Empty));
216 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecSharp.CecKeypress() { Keycode = 0x71 }), string.Empty));
217 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecSharp.CecKeypress() { Keycode = 0x72 }), string.Empty));
218 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecSharp.CecKeypress() { Keycode = 0x73 }), string.Empty));
219 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecSharp.CecKeypress() { Keycode = 0x74 }), string.Empty));
220 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecSharp.CecKeypress() { Keycode = 0x75 }), string.Empty));
221 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecSharp.CecKeypress() { Keycode = 0x76 }), string.Empty));
222 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty));
225 public int ReceiveCommand(CecCommand command)
230 public int ReceiveKeypress(CecKeypress key)
232 SelectKeypressRow(key);
236 delegate void SelectKeypressRowCallback(CecKeypress key);
237 private void SelectKeypressRow(CecKeypress key)
239 if (dgButtons.InvokeRequired)
241 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
244 this.Invoke(d, new object[] { key });
246 catch (Exception) { }
251 foreach (DataGridViewRow row in dgButtons.Rows)
253 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
254 if (item != null && item.Key.Keycode == key.Keycode)
256 rowIndex = row.Index;
262 row.Selected = false;
266 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
270 delegate void AddLogMessageCallback(CecLogMessage message);
271 private void AddLogMessage(CecLogMessage message)
273 if (tbLog.InvokeRequired)
275 AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage);
278 this.Invoke(d, new object[] { message });
280 catch (Exception) { }
284 string strLevel = "";
285 bool display = false;
286 switch (message.Level)
288 case CecLogLevel.Error:
289 strLevel = "ERROR: ";
290 display = cbLogError.Checked;
292 case CecLogLevel.Warning:
293 strLevel = "WARNING: ";
294 display = cbLogWarning.Checked;
296 case CecLogLevel.Notice:
297 strLevel = "NOTICE: ";
298 display = cbLogNotice.Checked;
300 case CecLogLevel.Traffic:
301 strLevel = "TRAFFIC: ";
302 display = cbLogTraffic.Checked;
304 case CecLogLevel.Debug:
305 strLevel = "DEBUG: ";
306 display = cbLogDebug.Checked;
314 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
315 tbLog.Text += strLog;
316 tbLog.Select(tbLog.Text.Length, 0);
317 tbLog.ScrollToCaret();
322 public int ReceiveLogMessage(CecLogMessage message)
324 AddLogMessage(message);
328 delegate void SetControlEnabledCallback(Control control, bool val);
329 private void SetControlEnabled(Control control, bool val)
331 if (control.InvokeRequired)
333 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
336 this.Invoke(d, new object[] { control, val });
338 catch (Exception) { }
342 control.Enabled = val;
346 private void SetControlsEnabled(bool val)
348 SetControlEnabled(cbPortNumber, val);
349 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
350 SetControlEnabled(tbPhysicalAddress, val);
351 SetControlEnabled(cbDeviceType, val);
352 SetControlEnabled(cbUseTVMenuLanguage, val);
353 SetControlEnabled(cbPowerOnStartup, val);
354 SetControlEnabled(cbPowerOffShutdown, val);
355 SetControlEnabled(cbPowerOffScreensaver, val);
356 SetControlEnabled(cbPowerOffOnStandby, val);
357 SetControlEnabled(bClose, val);
358 SetControlEnabled(bSave, val);
361 delegate void SetControlTextCallback(Control control, string val);
362 private void SetControlText(Control control, string val)
364 if (control.InvokeRequired)
366 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
369 this.Invoke(d, new object[] { control, val });
371 catch (Exception) { }
379 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
380 private void SetCheckboxChecked(CheckBox control, bool val)
382 if (control.InvokeRequired)
384 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
387 this.Invoke(d, new object[] { control, val });
389 catch (Exception) { }
393 control.Checked = val;
397 delegate void SetProgressValueCallback(ProgressBar control, int val);
398 private void SetProgressValue(ProgressBar control, int val)
400 if (control.InvokeRequired)
402 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
405 this.Invoke(d, new object[] { control, val });
407 catch (Exception) { }
415 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
416 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
418 if (control.InvokeRequired)
420 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
423 this.Invoke(d, new object[] { control, selectedText, val });
425 catch (Exception) { }
429 control.Items.Clear();
430 control.Items.AddRange(val);
431 control.Text = selectedText;
435 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
437 switch (updateEvent.Type)
439 case UpdateEventType.StatusText:
440 SetControlText(lStatus, updateEvent.StringValue);
442 case UpdateEventType.PhysicalAddress:
443 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
444 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
446 case UpdateEventType.ProgressBar:
447 SetProgressValue(pProgress, updateEvent.IntValue);
449 case UpdateEventType.TVVendorId:
450 TVVendor = (CecVendorId)updateEvent.IntValue;
451 UpdateSelectedDevice();
453 case UpdateEventType.BaseDevicePhysicalAddress:
454 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
456 case UpdateEventType.BaseDevice:
457 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
459 case UpdateEventType.HDMIPort:
460 Config.HDMIPort = (byte)updateEvent.IntValue;
462 case UpdateEventType.MenuLanguage:
463 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
465 case UpdateEventType.HasAVRDevice:
466 if (HasAVRDevice != updateEvent.BoolValue)
468 HasAVRDevice = updateEvent.BoolValue;
469 UpdateSelectedDevice();
472 case UpdateEventType.AVRVendorId:
473 AVRVendor = (CecVendorId)updateEvent.IntValue;
474 UpdateSelectedDevice();
476 case UpdateEventType.Configuration:
477 Config = updateEvent.ConfigValue;
478 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
479 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
480 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
481 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
482 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
483 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
484 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
485 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
486 UpdateSelectedDevice();
488 case UpdateEventType.ProcessCompleted:
489 ActiveProcess = null;
490 SetControlsEnabled(true);
495 private void UpdateSelectedDevice()
498 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
500 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
503 public string TVVendorString
507 return TVVendor != CecVendorId.Unknown ?
508 "Television (" + Lib.ToString(TVVendor) + ")" :
513 public string AVRVendorString
517 return AVRVendor != CecVendorId.Unknown ?
518 "AVR (" + Lib.ToString(AVRVendor) + ")" :
523 protected bool HasAVRDevice;
524 protected CecVendorId TVVendor = CecVendorId.Unknown;
525 protected CecVendorId AVRVendor = CecVendorId.Unknown;
526 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
528 protected LibCECConfiguration Config;
529 protected LibCecSharp Lib;
530 private CecCallbackWrapper Callbacks;
531 private UpdateProcess ActiveProcess = null;
533 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
535 if (ActiveProcess == null)
537 SetControlsEnabled(false);
538 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
540 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
542 ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber);
543 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
544 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
548 private void bCancel_Click(object sender, EventArgs e)
553 private void bSave_Click(object sender, EventArgs e)
555 SetControlsEnabled(false);
557 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
558 Config.PowerOnStartup = cbPowerOnStartup.Checked;
559 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
560 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
561 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
563 if (!Lib.CanPersistConfiguration())
565 if (ActiveProcess == null)
567 SetControlsEnabled(false);
568 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
569 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
571 SaveFileDialog dialog = new SaveFileDialog()
573 Title = "Where do you want to store the settings?",
574 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
575 FileName = "usb_2548_1001.xml",
576 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
580 if (dialog.ShowDialog() == DialogResult.OK)
582 FileStream fs = (FileStream)dialog.OpenFile();
585 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
589 StreamWriter writer = new StreamWriter(fs);
590 StringBuilder output = new StringBuilder();
591 output.AppendLine("<settings>");
592 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
593 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
594 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
595 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
596 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
597 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
598 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
599 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
600 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
601 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
602 output.AppendLine("<setting id=\"port\" value=\"\" />");
603 output.AppendLine("</settings>");
604 writer.Write(output.ToString());
608 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
611 SetControlsEnabled(true);
616 if (!Lib.PersistConfiguration(Config))
617 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
619 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
621 SetControlsEnabled(true);
624 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
626 if (ActiveProcess == null)
628 if (tbPhysicalAddress.Text.Length != 4)
630 ushort physicalAddress = 0;
631 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
633 SetControlsEnabled(false);
634 SetControlText(cbPortNumber, string.Empty);
635 SetControlText(cbConnectedDevice, string.Empty);
636 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
637 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
638 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
642 private void bClearLog_Click(object sender, EventArgs e)
644 tbLog.Text = string.Empty;
647 private void bSaveLog_Click(object sender, EventArgs e)
649 SaveFileDialog dialog = new SaveFileDialog()
651 Title = "Where do you want to store the log file?",
652 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
653 FileName = "cec-log.txt",
654 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
658 if (dialog.ShowDialog() == DialogResult.OK)
660 FileStream fs = (FileStream)dialog.OpenFile();
663 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
667 StreamWriter writer = new StreamWriter(fs);
668 writer.Write(tbLog.Text);
672 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
677 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
679 DataGridView grid = sender as DataGridView;
680 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
681 if (data == null || !data.Enabled)
682 e.CellStyle.ForeColor = Color.Gray;
685 private CecLogicalAddress GetTargetDevice()
687 switch (this.cbCommandDestination.Text.Substring(0, 1).ToLower())
690 return CecLogicalAddress.Tv;
692 return CecLogicalAddress.RecordingDevice1;
694 return CecLogicalAddress.RecordingDevice2;
696 return CecLogicalAddress.Tuner1;
698 return CecLogicalAddress.PlaybackDevice1;
700 return CecLogicalAddress.AudioSystem;
702 return CecLogicalAddress.Tuner2;
704 return CecLogicalAddress.Tuner3;
706 return CecLogicalAddress.PlaybackDevice2;
708 return CecLogicalAddress.RecordingDevice3;
710 return CecLogicalAddress.Tuner4;
712 return CecLogicalAddress.PlaybackDevice3;
714 return CecLogicalAddress.Reserved1;
716 return CecLogicalAddress.Reserved2;
718 return CecLogicalAddress.FreeUse;
720 return CecLogicalAddress.Broadcast;
722 return CecLogicalAddress.Unknown;
726 private void bSendImageViewOn_Click(object sender, EventArgs e)
728 if (ActiveProcess == null)
730 SetControlsEnabled(false);
731 ActiveProcess = new SendImageViewOn(ref Lib, GetTargetDevice());
732 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
733 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
737 private void bStandby_Click(object sender, EventArgs e)
739 if (ActiveProcess == null)
741 SetControlsEnabled(false);
742 ActiveProcess = new SendStandby(ref Lib, GetTargetDevice());
743 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
744 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
748 private void bScan_Click(object sender, EventArgs e)
750 if (ActiveProcess == null)
752 SetControlsEnabled(false);
753 ActiveProcess = new ShowDeviceInfo(ref Lib, GetTargetDevice());
754 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
755 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
759 private void bActivateSource_Click(object sender, EventArgs e)
761 if (ActiveProcess == null)
763 SetControlsEnabled(false);
764 ActiveProcess = new SendActivateSource(ref Lib, GetTargetDevice());
765 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
766 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
771 internal class CecCallbackWrapper : CecCallbackMethods
773 public CecCallbackWrapper(CecConfigGUI gui)
778 public override int ReceiveCommand(CecCommand command)
780 return Gui.ReceiveCommand(command);
783 public override int ReceiveKeypress(CecKeypress key)
785 return Gui.ReceiveKeypress(key);
788 public override int ReceiveLogMessage(CecLogMessage message)
790 return Gui.ReceiveLogMessage(message);
793 private CecConfigGUI Gui;