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 ActiveProcess = new ConnectToDevice(ref Lib);
32 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
33 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
36 public int ReceiveCommand(CecCommand command)
41 public int ReceiveKeypress(CecKeypress key)
46 delegate void AddLogMessageCallback(CecLogMessage message);
47 private void AddLogMessage(CecLogMessage message)
49 if (tbLog.InvokeRequired)
51 AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage);
54 this.Invoke(d, new object[] { message });
62 switch (message.Level)
64 case CecLogLevel.Error:
66 display = cbLogError.Checked;
68 case CecLogLevel.Warning:
69 strLevel = "WARNING: ";
70 display = cbLogWarning.Checked;
72 case CecLogLevel.Notice:
73 strLevel = "NOTICE: ";
74 display = cbLogNotice.Checked;
76 case CecLogLevel.Traffic:
77 strLevel = "TRAFFIC: ";
78 display = cbLogTraffic.Checked;
80 case CecLogLevel.Debug:
82 display = cbLogDebug.Checked;
90 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
92 tbLog.Select(tbLog.Text.Length, 0);
93 tbLog.ScrollToCaret();
98 public int ReceiveLogMessage(CecLogMessage message)
100 AddLogMessage(message);
104 delegate void SetControlEnabledCallback(Control control, bool val);
105 private void SetControlEnabled(Control control, bool val)
107 if (control.InvokeRequired)
109 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
112 this.Invoke(d, new object[] { control, val });
114 catch (Exception) { }
118 control.Enabled = val;
122 private void SetControlsEnabled(bool val)
124 SetControlEnabled(cbPortNumber, val);
125 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
126 SetControlEnabled(tbPhysicalAddress, val);
127 SetControlEnabled(cbDeviceType, val);
128 SetControlEnabled(cbUseTVMenuLanguage, val);
129 SetControlEnabled(cbPowerOnStartup, val);
130 SetControlEnabled(cbPowerOffShutdown, val);
131 SetControlEnabled(cbPowerOffScreensaver, val);
132 SetControlEnabled(cbPowerOffOnStandby, val);
133 SetControlEnabled(bClose, val);
134 SetControlEnabled(bSave, val);
137 delegate void SetControlTextCallback(Control control, string val);
138 private void SetControlText(Control control, string val)
140 if (control.InvokeRequired)
142 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
145 this.Invoke(d, new object[] { control, val });
147 catch (Exception) { }
155 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
156 private void SetCheckboxChecked(CheckBox control, bool val)
158 if (control.InvokeRequired)
160 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
163 this.Invoke(d, new object[] { control, val });
165 catch (Exception) { }
169 control.Checked = val;
173 delegate void SetProgressValueCallback(ProgressBar control, int val);
174 private void SetProgressValue(ProgressBar control, int val)
176 if (control.InvokeRequired)
178 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
181 this.Invoke(d, new object[] { control, val });
183 catch (Exception) { }
191 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
192 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
194 if (control.InvokeRequired)
196 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
199 this.Invoke(d, new object[] { control, selectedText, val });
201 catch (Exception) { }
205 control.Items.Clear();
206 control.Items.AddRange(val);
207 control.Text = selectedText;
211 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
213 switch (updateEvent.Type)
215 case UpdateEventType.StatusText:
216 SetControlText(lStatus, updateEvent.StringValue);
218 case UpdateEventType.PhysicalAddress:
219 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
220 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
222 case UpdateEventType.ProgressBar:
223 SetProgressValue(pProgress, updateEvent.IntValue);
225 case UpdateEventType.TVVendorId:
226 TVVendor = (CecVendorId)updateEvent.IntValue;
227 UpdateSelectedDevice();
229 case UpdateEventType.BaseDevicePhysicalAddress:
230 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
232 case UpdateEventType.BaseDevice:
233 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
235 case UpdateEventType.HDMIPort:
236 Config.HDMIPort = (byte)updateEvent.IntValue;
238 case UpdateEventType.MenuLanguage:
239 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
241 case UpdateEventType.HasAVRDevice:
242 if (HasAVRDevice != updateEvent.BoolValue)
244 HasAVRDevice = updateEvent.BoolValue;
245 UpdateSelectedDevice();
248 case UpdateEventType.AVRVendorId:
249 AVRVendor = (CecVendorId)updateEvent.IntValue;
250 UpdateSelectedDevice();
252 case UpdateEventType.Configuration:
253 Config = updateEvent.ConfigValue;
254 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
255 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
256 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
257 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
258 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
259 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
260 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
261 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
262 UpdateSelectedDevice();
264 case UpdateEventType.ProcessCompleted:
265 ActiveProcess = null;
266 SetControlsEnabled(true);
271 private void UpdateSelectedDevice()
274 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
276 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
279 public string TVVendorString
283 return TVVendor != CecVendorId.Unknown ?
284 "Television (" + Lib.ToString(TVVendor) + ")" :
289 public string AVRVendorString
293 return AVRVendor != CecVendorId.Unknown ?
294 "AVR (" + Lib.ToString(AVRVendor) + ")" :
299 protected bool HasAVRDevice;
300 protected CecVendorId TVVendor = CecVendorId.Unknown;
301 protected CecVendorId AVRVendor = CecVendorId.Unknown;
302 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
304 protected LibCECConfiguration Config;
305 protected LibCecSharp Lib;
306 private CecCallbackWrapper Callbacks;
307 private UpdateProcess ActiveProcess = null;
309 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
311 if (ActiveProcess == null)
313 SetControlsEnabled(false);
314 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
316 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
318 ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber);
319 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
320 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
324 private void bCancel_Click(object sender, EventArgs e)
329 private void bSave_Click(object sender, EventArgs e)
331 SetControlsEnabled(false);
333 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
334 Config.PowerOnStartup = cbPowerOnStartup.Checked;
335 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
336 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
337 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
339 if (!Lib.CanPersistConfiguration())
341 if (ActiveProcess == null)
343 SetControlsEnabled(false);
344 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
345 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
347 SaveFileDialog dialog = new SaveFileDialog()
349 Title = "Where do you want to store the settings?",
350 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
351 FileName = "usb_2548_1001.xml",
352 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
356 if (dialog.ShowDialog() == DialogResult.OK)
358 FileStream fs = (FileStream)dialog.OpenFile();
361 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
365 StreamWriter writer = new StreamWriter(fs);
366 StringBuilder output = new StringBuilder();
367 output.AppendLine("<settings>");
368 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
369 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
370 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
371 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
372 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
373 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
374 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
375 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
376 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
377 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
378 output.AppendLine("<setting id=\"port\" value=\"\" />");
379 output.AppendLine("</settings>");
380 writer.Write(output.ToString());
384 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
387 SetControlsEnabled(true);
392 if (!Lib.PersistConfiguration(Config))
393 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
395 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
397 SetControlsEnabled(true);
400 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
402 if (ActiveProcess == null)
404 if (tbPhysicalAddress.Text.Length != 4)
406 ushort physicalAddress = 0;
407 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
409 SetControlsEnabled(false);
410 SetControlText(cbPortNumber, string.Empty);
411 SetControlText(cbConnectedDevice, string.Empty);
412 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
413 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
414 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
418 private void bClearLog_Click(object sender, EventArgs e)
420 tbLog.Text = string.Empty;
423 private void bSaveLog_Click(object sender, EventArgs e)
425 SaveFileDialog dialog = new SaveFileDialog()
427 Title = "Where do you want to store the log file?",
428 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
429 FileName = "cec-log.txt",
430 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
434 if (dialog.ShowDialog() == DialogResult.OK)
436 FileStream fs = (FileStream)dialog.OpenFile();
439 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
443 StreamWriter writer = new StreamWriter(fs);
444 writer.Write(tbLog.Text);
448 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
454 internal class CecCallbackWrapper : CecCallbackMethods
456 public CecCallbackWrapper(CecConfigGUI gui)
461 public override int ReceiveCommand(CecCommand command)
463 return Gui.ReceiveCommand(command);
466 public override int ReceiveKeypress(CecKeypress key)
468 return Gui.ReceiveKeypress(key);
471 public override int ReceiveLogMessage(CecLogMessage message)
473 return Gui.ReceiveLogMessage(message);
476 private CecConfigGUI Gui;