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.", "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 ReceiveCommand(CecCommand command)
227 bool bGetNewPhysicalAddress = false;
228 if (command.Opcode == CecOpcode.ReportPhysicalAddress)
229 bGetNewPhysicalAddress = true;
231 if (bGetNewPhysicalAddress)
233 LibCECConfiguration config = new LibCECConfiguration();
234 Lib.GetCurrentConfiguration(config);
235 SetControlText(this.tbPhysicalAddress, String.Format("{0,4:X}", config.PhysicalAddress));
240 public int ReceiveKeypress(CecKeypress key)
242 SelectKeypressRow(key);
246 delegate void SelectKeypressRowCallback(CecKeypress key);
247 private void SelectKeypressRow(CecKeypress key)
249 if (dgButtons.InvokeRequired)
251 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
254 this.Invoke(d, new object[] { key });
256 catch (Exception) { }
261 foreach (DataGridViewRow row in dgButtons.Rows)
263 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
264 if (item != null && item.Key.Keycode == key.Keycode)
266 rowIndex = row.Index;
272 row.Selected = false;
276 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
280 delegate void AddLogMessageCallback(CecLogMessage message);
281 private void AddLogMessage(CecLogMessage message)
283 if (tbLog.InvokeRequired)
285 AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage);
288 this.Invoke(d, new object[] { message });
290 catch (Exception) { }
294 string strLevel = "";
295 bool display = false;
296 switch (message.Level)
298 case CecLogLevel.Error:
299 strLevel = "ERROR: ";
300 display = cbLogError.Checked;
302 case CecLogLevel.Warning:
303 strLevel = "WARNING: ";
304 display = cbLogWarning.Checked;
306 case CecLogLevel.Notice:
307 strLevel = "NOTICE: ";
308 display = cbLogNotice.Checked;
310 case CecLogLevel.Traffic:
311 strLevel = "TRAFFIC: ";
312 display = cbLogTraffic.Checked;
314 case CecLogLevel.Debug:
315 strLevel = "DEBUG: ";
316 display = cbLogDebug.Checked;
324 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
325 tbLog.Text += strLog;
326 tbLog.Select(tbLog.Text.Length, 0);
327 tbLog.ScrollToCaret();
332 public int ReceiveLogMessage(CecLogMessage message)
334 AddLogMessage(message);
338 delegate void SetControlEnabledCallback(Control control, bool val);
339 private void SetControlEnabled(Control control, bool val)
341 if (control.InvokeRequired)
343 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
346 this.Invoke(d, new object[] { control, val });
348 catch (Exception) { }
352 control.Enabled = val;
356 private void SetControlsEnabled(bool val)
358 SetControlEnabled(cbPortNumber, val);
359 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
360 SetControlEnabled(tbPhysicalAddress, val);
361 SetControlEnabled(cbDeviceType, false); // TODO not implemented yet
362 SetControlEnabled(cbUseTVMenuLanguage, val);
363 SetControlEnabled(cbPowerOnStartup, val);
364 SetControlEnabled(cbPowerOffShutdown, val);
365 SetControlEnabled(cbPowerOffScreensaver, val);
366 SetControlEnabled(cbPowerOffOnStandby, val);
367 SetControlEnabled(cbWakeDevices, false); // TODO not implemented yet
368 SetControlEnabled(bClose, val);
369 SetControlEnabled(bSave, val);
371 SetControlEnabled(bSendImageViewOn, val);
372 SetControlEnabled(bStandby, val);
373 SetControlEnabled(bActivateSource, val);
374 SetControlEnabled(bScan, val);
376 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
377 SetControlEnabled(bVolUp, enableVolumeButtons);
378 SetControlEnabled(bVolDown, enableVolumeButtons);
379 SetControlEnabled(bMute, enableVolumeButtons);
382 delegate void SetControlTextCallback(Control control, string val);
383 private void SetControlText(Control control, string val)
385 if (control.InvokeRequired)
387 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
390 this.Invoke(d, new object[] { control, val });
392 catch (Exception) { }
400 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
401 private void SetCheckboxChecked(CheckBox control, bool val)
403 if (control.InvokeRequired)
405 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
408 this.Invoke(d, new object[] { control, val });
410 catch (Exception) { }
414 control.Checked = val;
418 delegate void SetProgressValueCallback(ProgressBar control, int val);
419 private void SetProgressValue(ProgressBar control, int val)
421 if (control.InvokeRequired)
423 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
426 this.Invoke(d, new object[] { control, val });
428 catch (Exception) { }
436 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
437 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
439 if (control.InvokeRequired)
441 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
444 this.Invoke(d, new object[] { control, selectedText, val });
446 catch (Exception) { }
450 control.Items.Clear();
451 control.Items.AddRange(val);
452 control.Text = selectedText;
456 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
458 switch (updateEvent.Type)
460 case UpdateEventType.StatusText:
461 SetControlText(lStatus, updateEvent.StringValue);
463 case UpdateEventType.PhysicalAddress:
464 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
465 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
467 case UpdateEventType.ProgressBar:
468 SetProgressValue(pProgress, updateEvent.IntValue);
470 case UpdateEventType.TVVendorId:
471 TVVendor = (CecVendorId)updateEvent.IntValue;
472 UpdateSelectedDevice();
474 case UpdateEventType.BaseDevicePhysicalAddress:
475 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
477 case UpdateEventType.BaseDevice:
478 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
480 case UpdateEventType.HDMIPort:
481 Config.HDMIPort = (byte)updateEvent.IntValue;
483 case UpdateEventType.MenuLanguage:
484 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
486 case UpdateEventType.HasAVRDevice:
487 if (HasAVRDevice != updateEvent.BoolValue)
489 HasAVRDevice = updateEvent.BoolValue;
490 UpdateSelectedDevice();
493 case UpdateEventType.AVRVendorId:
494 AVRVendor = (CecVendorId)updateEvent.IntValue;
495 UpdateSelectedDevice();
497 case UpdateEventType.Configuration:
498 Config = updateEvent.ConfigValue;
499 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
500 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
501 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
502 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
503 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
504 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
505 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
506 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
507 UpdateSelectedDevice();
509 case UpdateEventType.ProcessCompleted:
510 ActiveProcess = null;
511 SetControlsEnabled(true);
516 private void UpdateSelectedDevice()
519 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
521 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
524 public string TVVendorString
528 return TVVendor != CecVendorId.Unknown ?
529 "Television (" + Lib.ToString(TVVendor) + ")" :
534 public string AVRVendorString
538 return AVRVendor != CecVendorId.Unknown ?
539 "AVR (" + Lib.ToString(AVRVendor) + ")" :
544 protected bool HasAVRDevice;
545 protected CecVendorId TVVendor = CecVendorId.Unknown;
546 protected CecVendorId AVRVendor = CecVendorId.Unknown;
547 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
549 protected LibCECConfiguration Config;
550 protected LibCecSharp Lib;
551 private CecCallbackWrapper Callbacks;
552 private UpdateProcess ActiveProcess = null;
554 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
556 if (ActiveProcess == null)
558 SetControlsEnabled(false);
559 ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
560 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
561 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
565 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
567 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
569 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
571 SetConnectedDevice(SelectedConnectedDevice, iPortNumber);
574 private void bCancel_Click(object sender, EventArgs e)
579 private void bSave_Click(object sender, EventArgs e)
581 SetControlsEnabled(false);
583 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
584 Config.PowerOnStartup = cbPowerOnStartup.Checked;
585 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
586 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
587 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
589 if (!Lib.CanPersistConfiguration())
591 if (ActiveProcess == null)
593 SetControlsEnabled(false);
594 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
595 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
597 SaveFileDialog dialog = new SaveFileDialog()
599 Title = "Where do you want to store the settings?",
600 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
601 FileName = "usb_2548_1001.xml",
602 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
606 if (dialog.ShowDialog() == DialogResult.OK)
608 FileStream fs = (FileStream)dialog.OpenFile();
611 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
615 StreamWriter writer = new StreamWriter(fs);
616 StringBuilder output = new StringBuilder();
617 output.AppendLine("<settings>");
618 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
619 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
620 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
621 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
622 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
623 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
624 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
625 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
626 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
627 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
628 output.AppendLine("<setting id=\"port\" value=\"\" />");
629 output.AppendLine("</settings>");
630 writer.Write(output.ToString());
634 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
637 SetControlsEnabled(true);
642 if (!Lib.PersistConfiguration(Config))
643 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
645 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
647 SetControlsEnabled(true);
650 public void SetPhysicalAddress(ushort physicalAddress)
652 if (ActiveProcess == null)
654 SetControlsEnabled(false);
655 SetControlText(cbPortNumber, string.Empty);
656 SetControlText(cbConnectedDevice, string.Empty);
657 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
658 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
659 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
663 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
665 if (tbPhysicalAddress.Text.Length != 4)
667 ushort physicalAddress = 0;
668 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
671 SetPhysicalAddress(physicalAddress);
674 private void bClearLog_Click(object sender, EventArgs e)
676 tbLog.Text = string.Empty;
679 private void bSaveLog_Click(object sender, EventArgs e)
681 SaveFileDialog dialog = new SaveFileDialog()
683 Title = "Where do you want to store the log file?",
684 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
685 FileName = "cec-log.txt",
686 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
690 if (dialog.ShowDialog() == DialogResult.OK)
692 FileStream fs = (FileStream)dialog.OpenFile();
695 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
699 StreamWriter writer = new StreamWriter(fs);
700 writer.Write(tbLog.Text);
704 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
709 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
711 DataGridView grid = sender as DataGridView;
712 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
713 if (data == null || !data.Enabled)
714 e.CellStyle.ForeColor = Color.Gray;
717 delegate CecLogicalAddress GetTargetDeviceCallback();
718 private CecLogicalAddress GetTargetDevice()
720 if (this.cbCommandDestination.InvokeRequired)
722 GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice);
723 CecLogicalAddress retval = CecLogicalAddress.Unknown;
726 retval = (CecLogicalAddress)this.Invoke(d, new object[] { });
728 catch (Exception) { }
732 switch (this.cbCommandDestination.Text.Substring(0, 1).ToLower())
735 return CecLogicalAddress.Tv;
737 return CecLogicalAddress.RecordingDevice1;
739 return CecLogicalAddress.RecordingDevice2;
741 return CecLogicalAddress.Tuner1;
743 return CecLogicalAddress.PlaybackDevice1;
745 return CecLogicalAddress.AudioSystem;
747 return CecLogicalAddress.Tuner2;
749 return CecLogicalAddress.Tuner3;
751 return CecLogicalAddress.PlaybackDevice2;
753 return CecLogicalAddress.RecordingDevice3;
755 return CecLogicalAddress.Tuner4;
757 return CecLogicalAddress.PlaybackDevice3;
759 return CecLogicalAddress.Reserved1;
761 return CecLogicalAddress.Reserved2;
763 return CecLogicalAddress.FreeUse;
765 return CecLogicalAddress.Broadcast;
767 return CecLogicalAddress.Unknown;
771 public void SendImageViewOn(CecLogicalAddress address)
773 if (ActiveProcess == null)
775 SetControlsEnabled(false);
776 ActiveProcess = new SendImageViewOn(ref Lib, address);
777 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
778 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
782 private void bSendImageViewOn_Click(object sender, EventArgs e)
784 SendImageViewOn(GetTargetDevice());
787 public void SendStandby(CecLogicalAddress address)
789 if (ActiveProcess == null)
791 SetControlsEnabled(false);
792 ActiveProcess = new SendStandby(ref Lib, address);
793 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
794 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
798 private void bStandby_Click(object sender, EventArgs e)
800 SendStandby(GetTargetDevice());
803 public void ShowDeviceInfo(CecLogicalAddress address)
805 if (ActiveProcess == null)
807 SetControlsEnabled(false);
808 ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
809 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
810 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
814 private void bScan_Click(object sender, EventArgs e)
816 ShowDeviceInfo(GetTargetDevice());
819 public void ActivateSource(CecLogicalAddress address)
821 if (ActiveProcess == null)
823 SetControlsEnabled(false);
824 ActiveProcess = new SendActivateSource(ref Lib, address);
825 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
826 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
830 private void bActivateSource_Click(object sender, EventArgs e)
832 ActivateSource(GetTargetDevice());
835 private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
837 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
838 this.bVolUp.Enabled = enableVolumeButtons;
839 this.bVolDown.Enabled = enableVolumeButtons;
840 this.bMute.Enabled = enableVolumeButtons;
843 private void bVolUp_Click(object sender, EventArgs e)
845 SetControlsEnabled(false);
847 SetControlsEnabled(true);
850 private void bVolDown_Click(object sender, EventArgs e)
852 SetControlsEnabled(false);
853 Lib.VolumeDown(true);
854 SetControlsEnabled(true);
857 private void bMute_Click(object sender, EventArgs e)
859 SetControlsEnabled(false);
861 SetControlsEnabled(true);
865 internal class CecCallbackWrapper : CecCallbackMethods
867 public CecCallbackWrapper(CecConfigGUI gui)
872 public override int ReceiveCommand(CecCommand command)
874 return Gui.ReceiveCommand(command);
877 public override int ReceiveKeypress(CecKeypress key)
879 return Gui.ReceiveKeypress(key);
882 public override int ReceiveLogMessage(CecLogMessage message)
884 return Gui.ReceiveLogMessage(message);
887 private CecConfigGUI Gui;