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 });
60 if (((int)message.Level & LogLevel) == (int)message.Level)
63 switch (message.Level)
65 case CecLogLevel.Error:
68 case CecLogLevel.Warning:
69 strLevel = "WARNING: ";
71 case CecLogLevel.Notice:
72 strLevel = "NOTICE: ";
74 case CecLogLevel.Traffic:
75 strLevel = "TRAFFIC: ";
77 case CecLogLevel.Debug:
83 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
89 public int ReceiveLogMessage(CecLogMessage message)
91 AddLogMessage(message);
95 delegate void SetControlEnabledCallback(Control control, bool val);
96 private void SetControlEnabled(Control control, bool val)
98 if (control.InvokeRequired)
100 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
103 this.Invoke(d, new object[] { control, val });
105 catch (Exception) { }
109 control.Enabled = val;
113 private void SetControlsEnabled(bool val)
115 SetControlEnabled(cbPortNumber, val);
116 SetControlEnabled(cbConnectedDevice, val);
117 SetControlEnabled(tbPhysicalAddress, val);
118 SetControlEnabled(cbDeviceType, val);
119 SetControlEnabled(cbUseTVMenuLanguage, val);
120 SetControlEnabled(cbPowerOnStartup, val);
121 SetControlEnabled(cbPowerOffShutdown, val);
122 SetControlEnabled(cbPowerOffScreensaver, val);
123 SetControlEnabled(cbPowerOffOnStandby, val);
124 SetControlEnabled(bClose, val);
125 SetControlEnabled(bSave, val);
128 delegate void SetControlTextCallback(Control control, string val);
129 private void SetControlText(Control control, string val)
131 if (control.InvokeRequired)
133 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
136 this.Invoke(d, new object[] { control, val });
138 catch (Exception) { }
146 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
147 private void SetCheckboxChecked(CheckBox control, bool val)
149 if (control.InvokeRequired)
151 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
154 this.Invoke(d, new object[] { control, val });
156 catch (Exception) { }
160 control.Checked = val;
164 delegate void SetProgressValueCallback(ProgressBar control, int val);
165 private void SetProgressValue(ProgressBar control, int val)
167 if (control.InvokeRequired)
169 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
172 this.Invoke(d, new object[] { control, val });
174 catch (Exception) { }
182 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
183 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
185 if (control.InvokeRequired)
187 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
190 this.Invoke(d, new object[] { control, selectedText, val });
192 catch (Exception) { }
196 control.Items.Clear();
197 control.Items.AddRange(val);
198 control.Text = selectedText;
202 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
204 switch (updateEvent.Type)
206 case UpdateEventType.StatusText:
207 SetControlText(lStatus, updateEvent.StringValue);
209 case UpdateEventType.PhysicalAddress:
210 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
211 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
213 case UpdateEventType.ProgressBar:
214 SetProgressValue(pProgress, updateEvent.IntValue);
216 case UpdateEventType.TVVendorId:
217 TVVendor = (CecVendorId)updateEvent.IntValue;
218 UpdateSelectedDevice();
220 case UpdateEventType.BaseDevicePhysicalAddress:
221 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
223 case UpdateEventType.BaseDevice:
224 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
226 case UpdateEventType.HDMIPort:
227 Config.HDMIPort = (byte)updateEvent.IntValue;
229 case UpdateEventType.HasAVRDevice:
230 if (HasAVRDevice != updateEvent.BoolValue)
232 HasAVRDevice = updateEvent.BoolValue;
233 UpdateSelectedDevice();
236 case UpdateEventType.AVRVendorId:
237 AVRVendor = (CecVendorId)updateEvent.IntValue;
238 UpdateSelectedDevice();
240 case UpdateEventType.Configuration:
241 Config = updateEvent.ConfigValue;
242 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
243 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
244 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
245 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
246 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
247 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
248 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
249 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
250 UpdateSelectedDevice();
252 case UpdateEventType.ProcessCompleted:
253 ActiveProcess = null;
254 SetControlsEnabled(true);
259 private void UpdateSelectedDevice()
262 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
264 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
267 public string TVVendorString
271 return TVVendor != CecVendorId.Unknown ?
272 "Television (" + Lib.ToString(TVVendor) + ")" :
277 public string AVRVendorString
281 return AVRVendor != CecVendorId.Unknown ?
282 "AVR (" + Lib.ToString(AVRVendor) + ")" :
287 protected bool HasAVRDevice;
288 protected CecVendorId TVVendor = CecVendorId.Unknown;
289 protected CecVendorId AVRVendor = CecVendorId.Unknown;
290 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
291 protected int LogLevel = (int)CecLogLevel.All;
293 protected LibCECConfiguration Config;
294 protected LibCecSharp Lib;
295 private CecCallbackWrapper Callbacks;
296 private UpdateProcess ActiveProcess = null;
298 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
300 if (ActiveProcess == null)
302 SetControlsEnabled(false);
303 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
305 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
307 ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber);
308 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
309 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
313 private void bCancel_Click(object sender, EventArgs e)
318 private void bSave_Click(object sender, EventArgs e)
320 SetControlsEnabled(false);
322 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
323 Config.PowerOnStartup = cbPowerOnStartup.Checked;
324 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
325 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
326 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
328 if (!Lib.CanPersistConfiguration())
330 if (ActiveProcess == null)
332 SetControlsEnabled(false);
333 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
334 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
336 SaveFileDialog dialog = new SaveFileDialog()
338 Title = "Where do you want to store the settings?",
339 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
340 FileName = "usb_2548_1001.xml",
341 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
345 if (dialog.ShowDialog() == DialogResult.OK)
347 FileStream fs = (FileStream)dialog.OpenFile();
350 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
354 StreamWriter writer = new StreamWriter(fs);
355 StringBuilder output = new StringBuilder();
356 output.AppendLine("<settings>");
357 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
358 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
359 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
360 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
361 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
362 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
363 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
364 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
365 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
366 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
367 output.AppendLine("<setting id=\"port\" value=\"\" />");
368 output.AppendLine("</settings>");
369 writer.Write(output.ToString());
373 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
376 SetControlsEnabled(true);
381 if (!Lib.PersistConfiguration(Config))
382 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
384 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
386 SetControlsEnabled(true);
389 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
391 if (ActiveProcess == null)
393 if (tbPhysicalAddress.Text.Length != 4)
395 ushort physicalAddress = 0;
396 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
398 SetControlsEnabled(false);
399 SetControlText(cbPortNumber, string.Empty);
400 SetControlText(cbConnectedDevice, string.Empty);
401 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
402 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
403 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
408 internal class CecCallbackWrapper : CecCallbackMethods
410 public CecCallbackWrapper(CecConfigGUI gui)
415 public override int ReceiveCommand(CecCommand command)
417 return Gui.ReceiveCommand(command);
420 public override int ReceiveKeypress(CecKeypress key)
422 return Gui.ReceiveKeypress(key);
425 public override int ReceiveLogMessage(CecLogMessage message)
427 return Gui.ReceiveLogMessage(message);
430 private CecConfigGUI Gui;