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.MenuLanguage:
230 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
232 case UpdateEventType.HasAVRDevice:
233 if (HasAVRDevice != updateEvent.BoolValue)
235 HasAVRDevice = updateEvent.BoolValue;
236 UpdateSelectedDevice();
239 case UpdateEventType.AVRVendorId:
240 AVRVendor = (CecVendorId)updateEvent.IntValue;
241 UpdateSelectedDevice();
243 case UpdateEventType.Configuration:
244 Config = updateEvent.ConfigValue;
245 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
246 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
247 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
248 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
249 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
250 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
251 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
252 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
253 UpdateSelectedDevice();
255 case UpdateEventType.ProcessCompleted:
256 ActiveProcess = null;
257 SetControlsEnabled(true);
262 private void UpdateSelectedDevice()
265 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
267 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
270 public string TVVendorString
274 return TVVendor != CecVendorId.Unknown ?
275 "Television (" + Lib.ToString(TVVendor) + ")" :
280 public string AVRVendorString
284 return AVRVendor != CecVendorId.Unknown ?
285 "AVR (" + Lib.ToString(AVRVendor) + ")" :
290 protected bool HasAVRDevice;
291 protected CecVendorId TVVendor = CecVendorId.Unknown;
292 protected CecVendorId AVRVendor = CecVendorId.Unknown;
293 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
294 protected int LogLevel = (int)CecLogLevel.All;
296 protected LibCECConfiguration Config;
297 protected LibCecSharp Lib;
298 private CecCallbackWrapper Callbacks;
299 private UpdateProcess ActiveProcess = null;
301 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
303 if (ActiveProcess == null)
305 SetControlsEnabled(false);
306 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
308 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
310 ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber);
311 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
312 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
316 private void bCancel_Click(object sender, EventArgs e)
321 private void bSave_Click(object sender, EventArgs e)
323 SetControlsEnabled(false);
325 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
326 Config.PowerOnStartup = cbPowerOnStartup.Checked;
327 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
328 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
329 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
331 if (!Lib.CanPersistConfiguration())
333 if (ActiveProcess == null)
335 SetControlsEnabled(false);
336 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
337 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
339 SaveFileDialog dialog = new SaveFileDialog()
341 Title = "Where do you want to store the settings?",
342 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
343 FileName = "usb_2548_1001.xml",
344 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
348 if (dialog.ShowDialog() == DialogResult.OK)
350 FileStream fs = (FileStream)dialog.OpenFile();
353 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
357 StreamWriter writer = new StreamWriter(fs);
358 StringBuilder output = new StringBuilder();
359 output.AppendLine("<settings>");
360 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
361 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
362 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
363 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
364 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
365 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
366 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
367 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
368 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
369 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
370 output.AppendLine("<setting id=\"port\" value=\"\" />");
371 output.AppendLine("</settings>");
372 writer.Write(output.ToString());
376 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
379 SetControlsEnabled(true);
384 if (!Lib.PersistConfiguration(Config))
385 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
387 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
389 SetControlsEnabled(true);
392 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
394 if (ActiveProcess == null)
396 if (tbPhysicalAddress.Text.Length != 4)
398 ushort physicalAddress = 0;
399 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
401 SetControlsEnabled(false);
402 SetControlText(cbPortNumber, string.Empty);
403 SetControlText(cbConnectedDevice, string.Empty);
404 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
405 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
406 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
411 internal class CecCallbackWrapper : CecCallbackMethods
413 public CecCallbackWrapper(CecConfigGUI gui)
418 public override int ReceiveCommand(CecCommand command)
420 return Gui.ReceiveCommand(command);
423 public override int ReceiveKeypress(CecKeypress key)
425 return Gui.ReceiveKeypress(key);
428 public override int ReceiveLogMessage(CecLogMessage message)
430 return Gui.ReceiveLogMessage(message);
433 private CecConfigGUI Gui;