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;
14 namespace CecConfigGui
16 public partial class CecConfigGUI : Form
20 Config = new LibCECConfiguration();
21 Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
22 Config.DeviceName = "CEC Config";
23 Config.GetSettingsFromROM = true;
24 Config.ClientVersion = CecClientVersion.Version1_5_0;
25 Callbacks = new CecCallbackWrapper(this);
26 Config.SetCallbacks(Callbacks);
28 InitializeComponent();
29 Lib = new LibCecSharp(Config);
31 LoadButtonConfiguration();
33 ActiveProcess = new ConnectToDevice(ref Lib);
34 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
35 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
38 private void LoadButtonConfiguration()
40 //TODO load the real configuration
41 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecSharp.CecKeypress() { Keycode = 0x00 }), string.Empty));
42 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecSharp.CecKeypress() { Keycode = 0x01 }), string.Empty));
43 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecSharp.CecKeypress() { Keycode = 0x02 }), string.Empty));
44 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecSharp.CecKeypress() { Keycode = 0x03 }), string.Empty));
45 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecSharp.CecKeypress() { Keycode = 0x04 }), string.Empty));
46 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecSharp.CecKeypress() { Keycode = 0x05 }), string.Empty));
47 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecSharp.CecKeypress() { Keycode = 0x06 }), string.Empty));
48 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecSharp.CecKeypress() { Keycode = 0x07 }), string.Empty));
49 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecSharp.CecKeypress() { Keycode = 0x08 }), string.Empty));
50 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecSharp.CecKeypress() { Keycode = 0x09 }), string.Empty));
51 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecSharp.CecKeypress() { Keycode = 0x0A }), string.Empty));
52 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecSharp.CecKeypress() { Keycode = 0x0B }), string.Empty));
53 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecSharp.CecKeypress() { Keycode = 0x0C }), string.Empty));
54 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecSharp.CecKeypress() { Keycode = 0x0D }), string.Empty));
55 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecSharp.CecKeypress() { Keycode = 0x20 }), string.Empty));
56 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecSharp.CecKeypress() { Keycode = 0x21 }), string.Empty));
57 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecSharp.CecKeypress() { Keycode = 0x22 }), string.Empty));
58 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecSharp.CecKeypress() { Keycode = 0x23 }), string.Empty));
59 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecSharp.CecKeypress() { Keycode = 0x24 }), string.Empty));
60 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecSharp.CecKeypress() { Keycode = 0x25 }), string.Empty));
61 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecSharp.CecKeypress() { Keycode = 0x26 }), string.Empty));
62 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecSharp.CecKeypress() { Keycode = 0x27 }), string.Empty));
63 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecSharp.CecKeypress() { Keycode = 0x28 }), string.Empty));
64 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecSharp.CecKeypress() { Keycode = 0x29 }), string.Empty));
65 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecSharp.CecKeypress() { Keycode = 0x2A }), string.Empty));
66 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecSharp.CecKeypress() { Keycode = 0x2B }), string.Empty));
67 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecSharp.CecKeypress() { Keycode = 0x2C }), string.Empty));
68 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecSharp.CecKeypress() { Keycode = 0x2F }), string.Empty));
69 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecSharp.CecKeypress() { Keycode = 0x30 }), string.Empty));
70 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecSharp.CecKeypress() { Keycode = 0x31 }), string.Empty));
71 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecSharp.CecKeypress() { Keycode = 0x32 }), string.Empty));
72 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecSharp.CecKeypress() { Keycode = 0x33 }), string.Empty));
73 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecSharp.CecKeypress() { Keycode = 0x34 }), string.Empty));
74 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecSharp.CecKeypress() { Keycode = 0x35 }), string.Empty));
75 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecSharp.CecKeypress() { Keycode = 0x36 }), string.Empty));
76 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecSharp.CecKeypress() { Keycode = 0x37 }), string.Empty));
77 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecSharp.CecKeypress() { Keycode = 0x38 }), string.Empty));
78 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecSharp.CecKeypress() { Keycode = 0x40 }), string.Empty));
79 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecSharp.CecKeypress() { Keycode = 0x41 }), string.Empty));
80 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecSharp.CecKeypress() { Keycode = 0x42 }), string.Empty));
81 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecSharp.CecKeypress() { Keycode = 0x43 }), string.Empty));
82 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecSharp.CecKeypress() { Keycode = 0x44 }), string.Empty));
83 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecSharp.CecKeypress() { Keycode = 0x45 }), string.Empty));
84 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecSharp.CecKeypress() { Keycode = 0x46 }), string.Empty));
85 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecSharp.CecKeypress() { Keycode = 0x47 }), string.Empty));
86 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecSharp.CecKeypress() { Keycode = 0x48 }), string.Empty));
87 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecSharp.CecKeypress() { Keycode = 0x49 }), string.Empty));
88 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecSharp.CecKeypress() { Keycode = 0x4A }), string.Empty));
89 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecSharp.CecKeypress() { Keycode = 0x4B }), string.Empty));
90 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecSharp.CecKeypress() { Keycode = 0x4C }), string.Empty));
91 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecSharp.CecKeypress() { Keycode = 0x4D }), string.Empty));
92 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecSharp.CecKeypress() { Keycode = 0x4E }), string.Empty));
93 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecSharp.CecKeypress() { Keycode = 0x50 }), string.Empty));
94 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecSharp.CecKeypress() { Keycode = 0x51 }), string.Empty));
95 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecSharp.CecKeypress() { Keycode = 0x52 }), string.Empty));
96 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecSharp.CecKeypress() { Keycode = 0x53 }), string.Empty));
97 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecSharp.CecKeypress() { Keycode = 0x54 }), string.Empty));
98 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecSharp.CecKeypress() { Keycode = 0x55 }), string.Empty));
99 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecSharp.CecKeypress() { Keycode = 0x60 }), string.Empty));
100 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecSharp.CecKeypress() { Keycode = 0x61 }), string.Empty));
101 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecSharp.CecKeypress() { Keycode = 0x62 }), string.Empty));
102 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecSharp.CecKeypress() { Keycode = 0x63 }), string.Empty));
103 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecSharp.CecKeypress() { Keycode = 0x64 }), string.Empty));
104 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecSharp.CecKeypress() { Keycode = 0x65 }), string.Empty));
105 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecSharp.CecKeypress() { Keycode = 0x66 }), string.Empty));
106 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecSharp.CecKeypress() { Keycode = 0x67 }), string.Empty));
107 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecSharp.CecKeypress() { Keycode = 0x68 }), string.Empty));
108 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecSharp.CecKeypress() { Keycode = 0x69 }), string.Empty));
109 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecSharp.CecKeypress() { Keycode = 0x6A }), string.Empty));
110 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecSharp.CecKeypress() { Keycode = 0x6B }), string.Empty));
111 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecSharp.CecKeypress() { Keycode = 0x6C }), string.Empty));
112 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecSharp.CecKeypress() { Keycode = 0x6D }), string.Empty));
113 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecSharp.CecKeypress() { Keycode = 0x71 }), string.Empty));
114 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecSharp.CecKeypress() { Keycode = 0x72 }), string.Empty));
115 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecSharp.CecKeypress() { Keycode = 0x73 }), string.Empty));
116 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecSharp.CecKeypress() { Keycode = 0x74 }), string.Empty));
117 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecSharp.CecKeypress() { Keycode = 0x75 }), string.Empty));
118 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecSharp.CecKeypress() { Keycode = 0x76 }), string.Empty));
119 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty));
122 public int ReceiveCommand(CecCommand command)
127 public int ReceiveKeypress(CecKeypress key)
129 SelectKeypressRow(key);
133 delegate void SelectKeypressRowCallback(CecKeypress key);
134 private void SelectKeypressRow(CecKeypress key)
136 if (dgButtons.InvokeRequired)
138 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
141 this.Invoke(d, new object[] { key });
143 catch (Exception) { }
148 foreach (DataGridViewRow row in dgButtons.Rows)
150 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
151 if (item != null && item.Key.Keycode == key.Keycode)
153 rowIndex = row.Index;
159 row.Selected = false;
163 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
167 delegate void AddLogMessageCallback(CecLogMessage message);
168 private void AddLogMessage(CecLogMessage message)
170 if (tbLog.InvokeRequired)
172 AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage);
175 this.Invoke(d, new object[] { message });
177 catch (Exception) { }
181 string strLevel = "";
182 bool display = false;
183 switch (message.Level)
185 case CecLogLevel.Error:
186 strLevel = "ERROR: ";
187 display = cbLogError.Checked;
189 case CecLogLevel.Warning:
190 strLevel = "WARNING: ";
191 display = cbLogWarning.Checked;
193 case CecLogLevel.Notice:
194 strLevel = "NOTICE: ";
195 display = cbLogNotice.Checked;
197 case CecLogLevel.Traffic:
198 strLevel = "TRAFFIC: ";
199 display = cbLogTraffic.Checked;
201 case CecLogLevel.Debug:
202 strLevel = "DEBUG: ";
203 display = cbLogDebug.Checked;
211 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
212 tbLog.Text += strLog;
213 tbLog.Select(tbLog.Text.Length, 0);
214 tbLog.ScrollToCaret();
219 public int ReceiveLogMessage(CecLogMessage message)
221 AddLogMessage(message);
225 delegate void SetControlEnabledCallback(Control control, bool val);
226 private void SetControlEnabled(Control control, bool val)
228 if (control.InvokeRequired)
230 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
233 this.Invoke(d, new object[] { control, val });
235 catch (Exception) { }
239 control.Enabled = val;
243 private void SetControlsEnabled(bool val)
245 SetControlEnabled(cbPortNumber, val);
246 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
247 SetControlEnabled(tbPhysicalAddress, val);
248 SetControlEnabled(cbDeviceType, val);
249 SetControlEnabled(cbUseTVMenuLanguage, val);
250 SetControlEnabled(cbPowerOnStartup, val);
251 SetControlEnabled(cbPowerOffShutdown, val);
252 SetControlEnabled(cbPowerOffScreensaver, val);
253 SetControlEnabled(cbPowerOffOnStandby, val);
254 SetControlEnabled(bClose, val);
255 SetControlEnabled(bSave, val);
258 delegate void SetControlTextCallback(Control control, string val);
259 private void SetControlText(Control control, string val)
261 if (control.InvokeRequired)
263 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
266 this.Invoke(d, new object[] { control, val });
268 catch (Exception) { }
276 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
277 private void SetCheckboxChecked(CheckBox control, bool val)
279 if (control.InvokeRequired)
281 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
284 this.Invoke(d, new object[] { control, val });
286 catch (Exception) { }
290 control.Checked = val;
294 delegate void SetProgressValueCallback(ProgressBar control, int val);
295 private void SetProgressValue(ProgressBar control, int val)
297 if (control.InvokeRequired)
299 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
302 this.Invoke(d, new object[] { control, val });
304 catch (Exception) { }
312 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
313 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
315 if (control.InvokeRequired)
317 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
320 this.Invoke(d, new object[] { control, selectedText, val });
322 catch (Exception) { }
326 control.Items.Clear();
327 control.Items.AddRange(val);
328 control.Text = selectedText;
332 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
334 switch (updateEvent.Type)
336 case UpdateEventType.StatusText:
337 SetControlText(lStatus, updateEvent.StringValue);
339 case UpdateEventType.PhysicalAddress:
340 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
341 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
343 case UpdateEventType.ProgressBar:
344 SetProgressValue(pProgress, updateEvent.IntValue);
346 case UpdateEventType.TVVendorId:
347 TVVendor = (CecVendorId)updateEvent.IntValue;
348 UpdateSelectedDevice();
350 case UpdateEventType.BaseDevicePhysicalAddress:
351 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
353 case UpdateEventType.BaseDevice:
354 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
356 case UpdateEventType.HDMIPort:
357 Config.HDMIPort = (byte)updateEvent.IntValue;
359 case UpdateEventType.MenuLanguage:
360 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
362 case UpdateEventType.HasAVRDevice:
363 if (HasAVRDevice != updateEvent.BoolValue)
365 HasAVRDevice = updateEvent.BoolValue;
366 UpdateSelectedDevice();
369 case UpdateEventType.AVRVendorId:
370 AVRVendor = (CecVendorId)updateEvent.IntValue;
371 UpdateSelectedDevice();
373 case UpdateEventType.Configuration:
374 Config = updateEvent.ConfigValue;
375 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
376 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
377 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
378 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
379 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
380 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
381 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
382 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
383 UpdateSelectedDevice();
385 case UpdateEventType.ProcessCompleted:
386 ActiveProcess = null;
387 SetControlsEnabled(true);
392 private void UpdateSelectedDevice()
395 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
397 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
400 public string TVVendorString
404 return TVVendor != CecVendorId.Unknown ?
405 "Television (" + Lib.ToString(TVVendor) + ")" :
410 public string AVRVendorString
414 return AVRVendor != CecVendorId.Unknown ?
415 "AVR (" + Lib.ToString(AVRVendor) + ")" :
420 protected bool HasAVRDevice;
421 protected CecVendorId TVVendor = CecVendorId.Unknown;
422 protected CecVendorId AVRVendor = CecVendorId.Unknown;
423 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
425 protected LibCECConfiguration Config;
426 protected LibCecSharp Lib;
427 private CecCallbackWrapper Callbacks;
428 private UpdateProcess ActiveProcess = null;
430 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
432 if (ActiveProcess == null)
434 SetControlsEnabled(false);
435 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
437 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
439 ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber);
440 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
441 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
445 private void bCancel_Click(object sender, EventArgs e)
450 private void bSave_Click(object sender, EventArgs e)
452 SetControlsEnabled(false);
454 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
455 Config.PowerOnStartup = cbPowerOnStartup.Checked;
456 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
457 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
458 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
460 if (!Lib.CanPersistConfiguration())
462 if (ActiveProcess == null)
464 SetControlsEnabled(false);
465 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
466 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
468 SaveFileDialog dialog = new SaveFileDialog()
470 Title = "Where do you want to store the settings?",
471 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
472 FileName = "usb_2548_1001.xml",
473 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
477 if (dialog.ShowDialog() == DialogResult.OK)
479 FileStream fs = (FileStream)dialog.OpenFile();
482 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
486 StreamWriter writer = new StreamWriter(fs);
487 StringBuilder output = new StringBuilder();
488 output.AppendLine("<settings>");
489 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
490 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
491 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
492 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
493 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
494 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
495 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
496 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
497 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
498 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
499 output.AppendLine("<setting id=\"port\" value=\"\" />");
500 output.AppendLine("</settings>");
501 writer.Write(output.ToString());
505 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
508 SetControlsEnabled(true);
513 if (!Lib.PersistConfiguration(Config))
514 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
516 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
518 SetControlsEnabled(true);
521 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
523 if (ActiveProcess == null)
525 if (tbPhysicalAddress.Text.Length != 4)
527 ushort physicalAddress = 0;
528 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
530 SetControlsEnabled(false);
531 SetControlText(cbPortNumber, string.Empty);
532 SetControlText(cbConnectedDevice, string.Empty);
533 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
534 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
535 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
539 private void bClearLog_Click(object sender, EventArgs e)
541 tbLog.Text = string.Empty;
544 private void bSaveLog_Click(object sender, EventArgs e)
546 SaveFileDialog dialog = new SaveFileDialog()
548 Title = "Where do you want to store the log file?",
549 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
550 FileName = "cec-log.txt",
551 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
555 if (dialog.ShowDialog() == DialogResult.OK)
557 FileStream fs = (FileStream)dialog.OpenFile();
560 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
564 StreamWriter writer = new StreamWriter(fs);
565 writer.Write(tbLog.Text);
569 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
574 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
576 DataGridView grid = sender as DataGridView;
577 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
578 if (data == null || !data.Enabled)
579 e.CellStyle.ForeColor = Color.Gray;
583 internal class CecCallbackWrapper : CecCallbackMethods
585 public CecCallbackWrapper(CecConfigGUI gui)
590 public override int ReceiveCommand(CecCommand command)
592 return Gui.ReceiveCommand(command);
595 public override int ReceiveKeypress(CecKeypress key)
597 return Gui.ReceiveKeypress(key);
600 public override int ReceiveLogMessage(CecLogMessage message)
602 return Gui.ReceiveLogMessage(message);
605 private CecConfigGUI Gui;