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 and try again.", "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 ConfigurationChanged(LibCECConfiguration config)
227 SetControlText(this.tbPhysicalAddress, String.Format("{0,4:X}", config.PhysicalAddress));
231 public int ReceiveCommand(CecCommand command)
236 public int ReceiveKeypress(CecKeypress key)
238 SelectKeypressRow(key);
242 delegate void SelectKeypressRowCallback(CecKeypress key);
243 private void SelectKeypressRow(CecKeypress key)
245 if (dgButtons.InvokeRequired)
247 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
250 this.Invoke(d, new object[] { key });
252 catch (Exception) { }
257 foreach (DataGridViewRow row in dgButtons.Rows)
259 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
260 if (item != null && item.Key.Keycode == key.Keycode)
262 rowIndex = row.Index;
268 row.Selected = false;
272 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
276 delegate void AddLogMessageCallback(CecLogMessage message);
277 private void AddLogMessage(CecLogMessage message)
279 if (tbLog.InvokeRequired)
281 AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage);
284 this.Invoke(d, new object[] { message });
286 catch (Exception) { }
290 string strLevel = "";
291 bool display = false;
292 switch (message.Level)
294 case CecLogLevel.Error:
295 strLevel = "ERROR: ";
296 display = cbLogError.Checked;
298 case CecLogLevel.Warning:
299 strLevel = "WARNING: ";
300 display = cbLogWarning.Checked;
302 case CecLogLevel.Notice:
303 strLevel = "NOTICE: ";
304 display = cbLogNotice.Checked;
306 case CecLogLevel.Traffic:
307 strLevel = "TRAFFIC: ";
308 display = cbLogTraffic.Checked;
310 case CecLogLevel.Debug:
311 strLevel = "DEBUG: ";
312 display = cbLogDebug.Checked;
320 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
321 tbLog.Text += strLog;
322 tbLog.Select(tbLog.Text.Length, 0);
323 tbLog.ScrollToCaret();
328 public int ReceiveLogMessage(CecLogMessage message)
330 AddLogMessage(message);
334 delegate void SetControlEnabledCallback(Control control, bool val);
335 private void SetControlEnabled(Control control, bool val)
337 if (control.InvokeRequired)
339 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
342 this.Invoke(d, new object[] { control, val });
344 catch (Exception) { }
348 control.Enabled = val;
352 private void SetControlsEnabled(bool val)
354 SetControlEnabled(cbPortNumber, val);
355 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
356 SetControlEnabled(tbPhysicalAddress, val);
357 SetControlEnabled(cbDeviceType, false); // TODO not implemented yet
358 SetControlEnabled(cbUseTVMenuLanguage, val);
359 SetControlEnabled(cbPowerOnStartup, val);
360 SetControlEnabled(cbPowerOffShutdown, val);
361 SetControlEnabled(cbPowerOffScreensaver, val);
362 SetControlEnabled(cbPowerOffOnStandby, val);
363 SetControlEnabled(cbWakeDevices, false); // TODO not implemented yet
364 SetControlEnabled(bClose, val);
365 SetControlEnabled(bSave, val);
367 SetControlEnabled(bSendImageViewOn, val);
368 SetControlEnabled(bStandby, val);
369 SetControlEnabled(bActivateSource, val);
370 SetControlEnabled(bScan, val);
372 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
373 SetControlEnabled(bVolUp, enableVolumeButtons);
374 SetControlEnabled(bVolDown, enableVolumeButtons);
375 SetControlEnabled(bMute, enableVolumeButtons);
378 delegate void SetControlTextCallback(Control control, string val);
379 private void SetControlText(Control control, string val)
381 if (control.InvokeRequired)
383 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
386 this.Invoke(d, new object[] { control, val });
388 catch (Exception) { }
396 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
397 private void SetCheckboxChecked(CheckBox control, bool val)
399 if (control.InvokeRequired)
401 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
404 this.Invoke(d, new object[] { control, val });
406 catch (Exception) { }
410 control.Checked = val;
414 delegate void SetProgressValueCallback(ProgressBar control, int val);
415 private void SetProgressValue(ProgressBar control, int val)
417 if (control.InvokeRequired)
419 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
422 this.Invoke(d, new object[] { control, val });
424 catch (Exception) { }
432 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
433 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
435 if (control.InvokeRequired)
437 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
440 this.Invoke(d, new object[] { control, selectedText, val });
442 catch (Exception) { }
446 control.Items.Clear();
447 control.Items.AddRange(val);
448 control.Text = selectedText;
452 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
454 switch (updateEvent.Type)
456 case UpdateEventType.StatusText:
457 SetControlText(lStatus, updateEvent.StringValue);
459 case UpdateEventType.PhysicalAddress:
460 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
461 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
463 case UpdateEventType.ProgressBar:
464 SetProgressValue(pProgress, updateEvent.IntValue);
466 case UpdateEventType.TVVendorId:
467 TVVendor = (CecVendorId)updateEvent.IntValue;
468 UpdateSelectedDevice();
470 case UpdateEventType.BaseDevicePhysicalAddress:
471 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
473 case UpdateEventType.BaseDevice:
474 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
476 case UpdateEventType.HDMIPort:
477 Config.HDMIPort = (byte)updateEvent.IntValue;
479 case UpdateEventType.MenuLanguage:
480 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
482 case UpdateEventType.HasAVRDevice:
483 if (HasAVRDevice != updateEvent.BoolValue)
485 HasAVRDevice = updateEvent.BoolValue;
486 UpdateSelectedDevice();
489 case UpdateEventType.AVRVendorId:
490 AVRVendor = (CecVendorId)updateEvent.IntValue;
491 UpdateSelectedDevice();
493 case UpdateEventType.Configuration:
494 Config = updateEvent.ConfigValue;
495 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
496 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
497 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
498 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
499 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
500 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
501 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
502 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
503 UpdateSelectedDevice();
505 case UpdateEventType.ProcessCompleted:
506 ActiveProcess = null;
507 SetControlsEnabled(true);
512 private void UpdateSelectedDevice()
515 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
517 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
520 public string TVVendorString
524 return TVVendor != CecVendorId.Unknown ?
525 "Television (" + Lib.ToString(TVVendor) + ")" :
530 public string AVRVendorString
534 return AVRVendor != CecVendorId.Unknown ?
535 "AVR (" + Lib.ToString(AVRVendor) + ")" :
540 protected bool HasAVRDevice;
541 protected CecVendorId TVVendor = CecVendorId.Unknown;
542 protected CecVendorId AVRVendor = CecVendorId.Unknown;
543 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
545 protected LibCECConfiguration Config;
546 protected LibCecSharp Lib;
547 private CecCallbackWrapper Callbacks;
548 private UpdateProcess ActiveProcess = null;
550 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
552 if (ActiveProcess == null)
554 SetControlsEnabled(false);
555 ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
556 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
557 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
561 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
563 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
565 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
567 SetConnectedDevice(SelectedConnectedDevice, iPortNumber);
570 private void bCancel_Click(object sender, EventArgs e)
575 private void bSave_Click(object sender, EventArgs e)
577 SetControlsEnabled(false);
579 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
580 Config.PowerOnStartup = cbPowerOnStartup.Checked;
581 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
582 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
583 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
585 if (!Lib.CanPersistConfiguration())
587 if (ActiveProcess == null)
589 SetControlsEnabled(false);
590 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
591 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
593 SaveFileDialog dialog = new SaveFileDialog()
595 Title = "Where do you want to store the settings?",
596 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
597 FileName = "usb_2548_1001.xml",
598 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
602 if (dialog.ShowDialog() == DialogResult.OK)
604 FileStream fs = (FileStream)dialog.OpenFile();
607 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
611 StreamWriter writer = new StreamWriter(fs);
612 StringBuilder output = new StringBuilder();
613 output.AppendLine("<settings>");
614 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
615 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
616 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
617 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
618 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
619 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
620 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
621 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
622 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
623 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
624 output.AppendLine("<setting id=\"port\" value=\"\" />");
625 output.AppendLine("</settings>");
626 writer.Write(output.ToString());
630 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
633 SetControlsEnabled(true);
638 if (!Lib.PersistConfiguration(Config))
639 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
641 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
643 SetControlsEnabled(true);
646 public void SetPhysicalAddress(ushort physicalAddress)
648 if (ActiveProcess == null)
650 SetControlsEnabled(false);
651 SetControlText(cbPortNumber, string.Empty);
652 SetControlText(cbConnectedDevice, string.Empty);
653 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
654 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
655 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
659 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
661 if (tbPhysicalAddress.Text.Length != 4)
663 ushort physicalAddress = 0;
664 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
667 SetPhysicalAddress(physicalAddress);
670 private void bClearLog_Click(object sender, EventArgs e)
672 tbLog.Text = string.Empty;
675 private void bSaveLog_Click(object sender, EventArgs e)
677 SaveFileDialog dialog = new SaveFileDialog()
679 Title = "Where do you want to store the log file?",
680 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
681 FileName = "cec-log.txt",
682 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
686 if (dialog.ShowDialog() == DialogResult.OK)
688 FileStream fs = (FileStream)dialog.OpenFile();
691 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
695 StreamWriter writer = new StreamWriter(fs);
696 writer.Write(tbLog.Text);
700 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
705 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
707 DataGridView grid = sender as DataGridView;
708 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
709 if (data == null || !data.Enabled)
710 e.CellStyle.ForeColor = Color.Gray;
713 delegate CecLogicalAddress GetTargetDeviceCallback();
714 private CecLogicalAddress GetTargetDevice()
716 if (this.cbCommandDestination.InvokeRequired)
718 GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice);
719 CecLogicalAddress retval = CecLogicalAddress.Unknown;
722 retval = (CecLogicalAddress)this.Invoke(d, new object[] { });
724 catch (Exception) { }
728 switch (this.cbCommandDestination.Text.Substring(0, 1).ToLower())
731 return CecLogicalAddress.Tv;
733 return CecLogicalAddress.RecordingDevice1;
735 return CecLogicalAddress.RecordingDevice2;
737 return CecLogicalAddress.Tuner1;
739 return CecLogicalAddress.PlaybackDevice1;
741 return CecLogicalAddress.AudioSystem;
743 return CecLogicalAddress.Tuner2;
745 return CecLogicalAddress.Tuner3;
747 return CecLogicalAddress.PlaybackDevice2;
749 return CecLogicalAddress.RecordingDevice3;
751 return CecLogicalAddress.Tuner4;
753 return CecLogicalAddress.PlaybackDevice3;
755 return CecLogicalAddress.Reserved1;
757 return CecLogicalAddress.Reserved2;
759 return CecLogicalAddress.FreeUse;
761 return CecLogicalAddress.Broadcast;
763 return CecLogicalAddress.Unknown;
767 public void SendImageViewOn(CecLogicalAddress address)
769 if (ActiveProcess == null)
771 SetControlsEnabled(false);
772 ActiveProcess = new SendImageViewOn(ref Lib, address);
773 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
774 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
778 private void bSendImageViewOn_Click(object sender, EventArgs e)
780 SendImageViewOn(GetTargetDevice());
783 public void SendStandby(CecLogicalAddress address)
785 if (ActiveProcess == null)
787 SetControlsEnabled(false);
788 ActiveProcess = new SendStandby(ref Lib, address);
789 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
790 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
794 private void bStandby_Click(object sender, EventArgs e)
796 SendStandby(GetTargetDevice());
799 public void ShowDeviceInfo(CecLogicalAddress address)
801 if (ActiveProcess == null)
803 SetControlsEnabled(false);
804 ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
805 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
806 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
810 private void bScan_Click(object sender, EventArgs e)
812 ShowDeviceInfo(GetTargetDevice());
815 public void ActivateSource(CecLogicalAddress address)
817 if (ActiveProcess == null)
819 SetControlsEnabled(false);
820 ActiveProcess = new SendActivateSource(ref Lib, address);
821 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
822 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
826 private void bActivateSource_Click(object sender, EventArgs e)
828 ActivateSource(GetTargetDevice());
831 private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
833 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
834 this.bVolUp.Enabled = enableVolumeButtons;
835 this.bVolDown.Enabled = enableVolumeButtons;
836 this.bMute.Enabled = enableVolumeButtons;
839 private void bVolUp_Click(object sender, EventArgs e)
841 SetControlsEnabled(false);
843 SetControlsEnabled(true);
846 private void bVolDown_Click(object sender, EventArgs e)
848 SetControlsEnabled(false);
849 Lib.VolumeDown(true);
850 SetControlsEnabled(true);
853 private void bMute_Click(object sender, EventArgs e)
855 SetControlsEnabled(false);
857 SetControlsEnabled(true);
861 internal class CecCallbackWrapper : CecCallbackMethods
863 public CecCallbackWrapper(CecConfigGUI gui)
868 public override int ReceiveCommand(CecCommand command)
870 return Gui.ReceiveCommand(command);
873 public override int ReceiveKeypress(CecKeypress key)
875 return Gui.ReceiveKeypress(key);
878 public override int ReceiveLogMessage(CecLogMessage message)
880 return Gui.ReceiveLogMessage(message);
883 public override int ConfigurationChanged(LibCECConfiguration config)
885 return Gui.ConfigurationChanged(config);
888 private CecConfigGUI Gui;