X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=src%2Fcec-config-gui%2FCecConfigGUI.cs;h=b1731cfa2c8dfdcae7bb92ece32b03fab0c9a482;hb=75af24f133bfd316bc69a9b63e190047186f169a;hp=e41c292be80901d16658a6f00ad4189a54cb2bc3;hpb=b7907707d9d2d9e6ae198ebecf6864cb30484327;p=deb_libcec.git diff --git a/src/cec-config-gui/CecConfigGUI.cs b/src/cec-config-gui/CecConfigGUI.cs index e41c292..b1731cf 100644 --- a/src/cec-config-gui/CecConfigGUI.cs +++ b/src/cec-config-gui/CecConfigGUI.cs @@ -14,7 +14,7 @@ using System.Xml; namespace CecConfigGui { - public partial class CecConfigGUI : Form + public partial class CecConfigGUI : AsyncForm { public CecConfigGUI() { @@ -91,6 +91,27 @@ namespace CecConfigGui config.BaseDevice = (CecLogicalAddress)iDevice; } break; + case "cec_power_on_startup": + if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes")) + { + config.ActivateSource = true; + config.WakeDevices.Set(CecLogicalAddress.Tv); + } + break; + case "cec_power_off_shutdown": + if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes")) + config.PowerOffDevices.Set(CecLogicalAddress.Broadcast); + break; + case "cec_standby_screensaver": + config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); + break; + case "standby_pc_on_tv_standby": + config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); + break; + case "use_tv_menu_language": + config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); + break; + // 1.5.0+ settings case "physical_address": { ushort physicalAddress = 0; @@ -105,20 +126,36 @@ namespace CecConfigGui config.DeviceTypes.Types[0] = (CecDeviceType)iType; } break; - case "cec_power_on_startup": - config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); - break; - case "cec_power_off_shutdown": - config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); - break; - case "cec_standby_screensaver": - config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); + case "tv_vendor": + { + UInt64 iVendor; + if (UInt64.TryParse(value, out iVendor)) + config.TvVendor = (CecVendorId)iVendor; + } break; - case "standby_pc_on_tv_standby": - config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); + case "wake_devices": + { + config.WakeDevices.Clear(); + string[] split = value.Split(new char[] { ' ' }); + foreach (string dev in split) + { + byte iLogicalAddress; + if (byte.TryParse(dev, out iLogicalAddress)) + config.WakeDevices.Set((CecLogicalAddress)iLogicalAddress); + } + } break; - case "use_tv_menu_language": - config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); + case "standby_devices": + { + config.PowerOffDevices.Clear(); + string[] split = value.Split(new char[] { ' ' }); + foreach (string dev in split) + { + byte iLogicalAddress; + if (byte.TryParse(dev, out iLogicalAddress)) + config.PowerOffDevices.Set((CecLogicalAddress)iLogicalAddress); + } + } break; case "enabled": break; @@ -222,130 +259,113 @@ namespace CecConfigGui this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty)); } - public int ConfigurationChanged(LibCECConfiguration config) - { - SetControlText(this.tbPhysicalAddress, String.Format("{0,4:X}", config.PhysicalAddress)); - return 1; - } - - public int ReceiveCommand(CecCommand command) - { - return 1; - } - - public int ReceiveKeypress(CecKeypress key) - { - SelectKeypressRow(key); - return 1; - } - - delegate void SelectKeypressRowCallback(CecKeypress key); - private void SelectKeypressRow(CecKeypress key) + private void ProcessEventHandler(object src, UpdateEvent updateEvent) { - if (dgButtons.InvokeRequired) - { - SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow); - try - { - this.Invoke(d, new object[] { key }); - } - catch (Exception) { } - } - else + switch (updateEvent.Type) { - int rowIndex = -1; - foreach (DataGridViewRow row in dgButtons.Rows) - { - CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem; - if (item != null && item.Key.Keycode == key.Keycode) - { - rowIndex = row.Index; - row.Selected = true; - item.Enabled = true; - } - else + case UpdateEventType.StatusText: + SetControlText(lStatus, updateEvent.StringValue); + break; + case UpdateEventType.PhysicalAddress: + Config.PhysicalAddress = (ushort)updateEvent.IntValue; + SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue)); + break; + case UpdateEventType.ProgressBar: + SetProgressValue(pProgress, updateEvent.IntValue); + break; + case UpdateEventType.TVVendorId: + TVVendor = (CecVendorId)updateEvent.IntValue; + UpdateSelectedDevice(); + break; + case UpdateEventType.BaseDevicePhysicalAddress: + SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue)); + break; + case UpdateEventType.BaseDevice: + Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue; + break; + case UpdateEventType.HDMIPort: + Config.HDMIPort = (byte)updateEvent.IntValue; + break; + case UpdateEventType.MenuLanguage: + SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : "")); + break; + case UpdateEventType.HasAVRDevice: + if (HasAVRDevice != updateEvent.BoolValue) { - row.Selected = false; + HasAVRDevice = updateEvent.BoolValue; + UpdateSelectedDevice(); } - } - if (rowIndex > -1) - dgButtons.FirstDisplayedScrollingRowIndex = rowIndex; + break; + case UpdateEventType.AVRVendorId: + AVRVendor = (CecVendorId)updateEvent.IntValue; + UpdateSelectedDevice(); + break; + case UpdateEventType.Configuration: + SuppressUpdates = true; + ConfigurationChanged(updateEvent.ConfigValue); + SuppressUpdates = false; + break; + case UpdateEventType.ProcessCompleted: + ActiveProcess = null; + SetControlsEnabled(true); + break; } } - delegate void AddLogMessageCallback(CecLogMessage message); - private void AddLogMessage(CecLogMessage message) + public void SetPhysicalAddress(ushort physicalAddress) { - if (tbLog.InvokeRequired) + if (!SuppressUpdates && ActiveProcess == null) { - AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage); - try - { - this.Invoke(d, new object[] { message }); - } - catch (Exception) { } + SetControlsEnabled(false); + SetControlText(cbPortNumber, string.Empty); + SetControlText(cbConnectedDevice, string.Empty); + ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress); + ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); + (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); } - else - { - string strLevel = ""; - bool display = false; - switch (message.Level) - { - case CecLogLevel.Error: - strLevel = "ERROR: "; - display = cbLogError.Checked; - break; - case CecLogLevel.Warning: - strLevel = "WARNING: "; - display = cbLogWarning.Checked; - break; - case CecLogLevel.Notice: - strLevel = "NOTICE: "; - display = cbLogNotice.Checked; - break; - case CecLogLevel.Traffic: - strLevel = "TRAFFIC: "; - display = cbLogTraffic.Checked; - break; - case CecLogLevel.Debug: - strLevel = "DEBUG: "; - display = cbLogDebug.Checked; - break; - default: - break; - } + } - if (display) - { - string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine; - tbLog.Text += strLog; - tbLog.Select(tbLog.Text.Length, 0); - tbLog.ScrollToCaret(); - } + public void SendImageViewOn(CecLogicalAddress address) + { + if (!SuppressUpdates && ActiveProcess == null) + { + SetControlsEnabled(false); + ActiveProcess = new SendImageViewOn(ref Lib, address); + ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); + (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); } } - public int ReceiveLogMessage(CecLogMessage message) + public void ActivateSource(CecLogicalAddress address) { - AddLogMessage(message); - return 1; + if (!SuppressUpdates && ActiveProcess == null) + { + SetControlsEnabled(false); + ActiveProcess = new SendActivateSource(ref Lib, address); + ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); + (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); + } } - delegate void SetControlEnabledCallback(Control control, bool val); - private void SetControlEnabled(Control control, bool val) + public void SendStandby(CecLogicalAddress address) { - if (control.InvokeRequired) + if (!SuppressUpdates && ActiveProcess == null) { - SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled); - try - { - this.Invoke(d, new object[] { control, val }); - } - catch (Exception) { } + SetControlsEnabled(false); + ActiveProcess = new SendStandby(ref Lib, address); + ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); + (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); } - else + } + + public void ShowDeviceInfo(CecLogicalAddress address) + { + if (!SuppressUpdates && ActiveProcess == null) { - control.Enabled = val; + SetControlsEnabled(false); + ActiveProcess = new ShowDeviceInfo(this, ref Lib, address); + ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); + (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); } } @@ -356,11 +376,13 @@ namespace CecConfigGui SetControlEnabled(tbPhysicalAddress, val); SetControlEnabled(cbDeviceType, false); // TODO not implemented yet SetControlEnabled(cbUseTVMenuLanguage, val); - SetControlEnabled(cbPowerOnStartup, val); - SetControlEnabled(cbPowerOffShutdown, val); + SetControlEnabled(cbActivateSource, val); SetControlEnabled(cbPowerOffScreensaver, val); SetControlEnabled(cbPowerOffOnStandby, val); SetControlEnabled(cbWakeDevices, false); // TODO not implemented yet + SetControlEnabled(cbPowerOffDevices, false); // TODO not implemented yet + SetControlEnabled(cbVendorOverride, false); // TODO not implemented yet + SetControlEnabled(cbVendorId, false); // TODO not implemented yet SetControlEnabled(bClose, val); SetControlEnabled(bSave, val); @@ -375,196 +397,40 @@ namespace CecConfigGui SetControlEnabled(bMute, enableVolumeButtons); } - delegate void SetControlTextCallback(Control control, string val); - private void SetControlText(Control control, string val) + #region Configuration tab + private void tbPhysicalAddress_TextChanged(object sender, EventArgs e) { - if (control.InvokeRequired) - { - SetControlTextCallback d = new SetControlTextCallback(SetControlText); - try - { - this.Invoke(d, new object[] { control, val }); - } - catch (Exception) { } - } - else - { - control.Text = val; - } + if (tbPhysicalAddress.Text.Length != 4) + return; + ushort physicalAddress = 0; + if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress)) + return; + + SetPhysicalAddress(physicalAddress); } - delegate void SetCheckboxCheckedCallback(CheckBox control, bool val); - private void SetCheckboxChecked(CheckBox control, bool val) + private void UpdateSelectedDevice() { - if (control.InvokeRequired) - { - SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked); - try - { - this.Invoke(d, new object[] { control, val }); - } - catch (Exception) { } - } + if (HasAVRDevice) + SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString }); else - { - control.Checked = val; - } + SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString }); } - delegate void SetProgressValueCallback(ProgressBar control, int val); - private void SetProgressValue(ProgressBar control, int val) + public void SetConnectedDevice(CecLogicalAddress address, int portnumber) { - if (control.InvokeRequired) - { - SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue); - try - { - this.Invoke(d, new object[] { control, val }); - } - catch (Exception) { } - } - else + if (!SuppressUpdates && ActiveProcess == null) { - control.Value = val; + SetControlsEnabled(false); + ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber); + ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); + (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); } } - delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val); - private void SetComboBoxItems(ComboBox control, string selectedText, object[] val) + private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e) { - if (control.InvokeRequired) - { - SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems); - try - { - this.Invoke(d, new object[] { control, selectedText, val }); - } - catch (Exception) { } - } - else - { - control.Items.Clear(); - control.Items.AddRange(val); - control.Text = selectedText; - } - } - - private void ProcessEventHandler(object src, UpdateEvent updateEvent) - { - switch (updateEvent.Type) - { - case UpdateEventType.StatusText: - SetControlText(lStatus, updateEvent.StringValue); - break; - case UpdateEventType.PhysicalAddress: - Config.PhysicalAddress = (ushort)updateEvent.IntValue; - SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue)); - break; - case UpdateEventType.ProgressBar: - SetProgressValue(pProgress, updateEvent.IntValue); - break; - case UpdateEventType.TVVendorId: - TVVendor = (CecVendorId)updateEvent.IntValue; - UpdateSelectedDevice(); - break; - case UpdateEventType.BaseDevicePhysicalAddress: - SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue)); - break; - case UpdateEventType.BaseDevice: - Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue; - break; - case UpdateEventType.HDMIPort: - Config.HDMIPort = (byte)updateEvent.IntValue; - break; - case UpdateEventType.MenuLanguage: - SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : "")); - break; - case UpdateEventType.HasAVRDevice: - if (HasAVRDevice != updateEvent.BoolValue) - { - HasAVRDevice = updateEvent.BoolValue; - UpdateSelectedDevice(); - } - break; - case UpdateEventType.AVRVendorId: - AVRVendor = (CecVendorId)updateEvent.IntValue; - UpdateSelectedDevice(); - break; - case UpdateEventType.Configuration: - Config = updateEvent.ConfigValue; - SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress)); - SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString); - SetControlText(cbPortNumber, Config.HDMIPort.ToString()); - SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage); - SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup); - SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown); - SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver); - SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby); - UpdateSelectedDevice(); - break; - case UpdateEventType.ProcessCompleted: - ActiveProcess = null; - SetControlsEnabled(true); - break; - } - } - - private void UpdateSelectedDevice() - { - if (HasAVRDevice) - SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString }); - else - SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString }); - } - - public string TVVendorString - { - get - { - return TVVendor != CecVendorId.Unknown ? - "Television (" + Lib.ToString(TVVendor) + ")" : - "Television"; - } - } - - public string AVRVendorString - { - get - { - return AVRVendor != CecVendorId.Unknown ? - "AVR (" + Lib.ToString(AVRVendor) + ")" : - "AVR"; - } - } - - protected bool HasAVRDevice; - protected CecVendorId TVVendor = CecVendorId.Unknown; - protected CecVendorId AVRVendor = CecVendorId.Unknown; - protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown; - - protected LibCECConfiguration Config; - protected LibCecSharp Lib; - private CecCallbackWrapper Callbacks; - private UpdateProcess ActiveProcess = null; - - public void SetConnectedDevice(CecLogicalAddress address, int portnumber) - { - if (ActiveProcess == null) - { - SetControlsEnabled(false); - ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber); - ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); - (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); - } - } - - private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e) - { - SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv; - int iPortNumber = 0; - if (!int.TryParse(cbPortNumber.Text, out iPortNumber)) - iPortNumber = 1; - SetConnectedDevice(SelectedConnectedDevice, iPortNumber); + SetConnectedDevice(SelectedConnectedDevice, SelectedPortNumber); } private void bCancel_Click(object sender, EventArgs e) @@ -577,8 +443,7 @@ namespace CecConfigGui SetControlsEnabled(false); Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked; - Config.PowerOnStartup = cbPowerOnStartup.Checked; - Config.PowerOffShutdown = cbPowerOffShutdown.Checked; + Config.ActivateSource = cbActivateSource.Checked; Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked; Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked; @@ -613,15 +478,31 @@ namespace CecConfigGui output.AppendLine(""); output.AppendLine(""); output.AppendLine(""); - output.AppendLine(""); - output.AppendLine(""); - output.AppendLine(""); - output.AppendLine(""); + output.AppendLine(""); + output.AppendLine(""); output.AppendLine(""); output.AppendLine(""); output.AppendLine(""); output.AppendLine(""); output.AppendLine(""); + + // only supported by 1.5.0+ clients + output.AppendLine(""); + output.AppendLine(""); + output.AppendLine(""); + + output.Append(""); + + output.Append(""); + output.AppendLine(""); writer.Write(output.ToString()); writer.Close(); @@ -642,63 +523,40 @@ namespace CecConfigGui } SetControlsEnabled(true); } + #endregion - public void SetPhysicalAddress(ushort physicalAddress) - { - if (ActiveProcess == null) - { - SetControlsEnabled(false); - SetControlText(cbPortNumber, string.Empty); - SetControlText(cbConnectedDevice, string.Empty); - ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress); - ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); - (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); - } - } - - private void tbPhysicalAddress_TextChanged(object sender, EventArgs e) - { - if (tbPhysicalAddress.Text.Length != 4) - return; - ushort physicalAddress = 0; - if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress)) - return; - - SetPhysicalAddress(physicalAddress); - } - - private void bClearLog_Click(object sender, EventArgs e) - { - tbLog.Text = string.Empty; - } - - private void bSaveLog_Click(object sender, EventArgs e) + #region Key configuration tab + delegate void SelectKeypressRowCallback(CecKeypress key); + private void SelectKeypressRow(CecKeypress key) { - SaveFileDialog dialog = new SaveFileDialog() - { - Title = "Where do you want to store the log file?", - InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), - FileName = "cec-log.txt", - Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*", - FilterIndex = 1 - }; - - if (dialog.ShowDialog() == DialogResult.OK) + if (dgButtons.InvokeRequired) { - FileStream fs = (FileStream)dialog.OpenFile(); - if (fs == null) + SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow); + try { - MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); + this.Invoke(d, new object[] { key }); } - else + catch (Exception) { } + } + else + { + int rowIndex = -1; + foreach (DataGridViewRow row in dgButtons.Rows) { - StreamWriter writer = new StreamWriter(fs); - writer.Write(tbLog.Text); - writer.Close(); - fs.Close(); - fs.Dispose(); - MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); + CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem; + if (item != null && item.Key.Keycode == key.Keycode) + { + rowIndex = row.Index; + row.Selected = true; + item.Enabled = true; + } + else + { + row.Selected = false; + } } + if (rowIndex > -1) + dgButtons.FirstDisplayedScrollingRowIndex = rowIndex; } } @@ -709,7 +567,9 @@ namespace CecConfigGui if (data == null || !data.Enabled) e.CellStyle.ForeColor = Color.Gray; } + #endregion + #region CEC Tester tab delegate CecLogicalAddress GetTargetDeviceCallback(); private CecLogicalAddress GetTargetDevice() { @@ -764,65 +624,21 @@ namespace CecConfigGui } } - public void SendImageViewOn(CecLogicalAddress address) - { - if (ActiveProcess == null) - { - SetControlsEnabled(false); - ActiveProcess = new SendImageViewOn(ref Lib, address); - ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); - (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); - } - } - private void bSendImageViewOn_Click(object sender, EventArgs e) { SendImageViewOn(GetTargetDevice()); } - public void SendStandby(CecLogicalAddress address) - { - if (ActiveProcess == null) - { - SetControlsEnabled(false); - ActiveProcess = new SendStandby(ref Lib, address); - ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); - (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); - } - } - private void bStandby_Click(object sender, EventArgs e) { SendStandby(GetTargetDevice()); } - public void ShowDeviceInfo(CecLogicalAddress address) - { - if (ActiveProcess == null) - { - SetControlsEnabled(false); - ActiveProcess = new ShowDeviceInfo(this, ref Lib, address); - ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); - (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); - } - } - private void bScan_Click(object sender, EventArgs e) { ShowDeviceInfo(GetTargetDevice()); } - public void ActivateSource(CecLogicalAddress address) - { - if (ActiveProcess == null) - { - SetControlsEnabled(false); - ActiveProcess = new SendActivateSource(ref Lib, address); - ActiveProcess.EventHandler += new EventHandler(ProcessEventHandler); - (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); - } - } - private void bActivateSource_Click(object sender, EventArgs e) { ActivateSource(GetTargetDevice()); @@ -856,8 +672,194 @@ namespace CecConfigGui Lib.MuteAudio(true); SetControlsEnabled(true); } + #endregion + + #region Log tab + delegate void AddLogMessageCallback(CecLogMessage message); + private void AddLogMessage(CecLogMessage message) + { + if (tbLog.InvokeRequired) + { + AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage); + try + { + this.Invoke(d, new object[] { message }); + } + catch (Exception) { } + } + else + { + string strLevel = ""; + bool display = false; + switch (message.Level) + { + case CecLogLevel.Error: + strLevel = "ERROR: "; + display = cbLogError.Checked; + break; + case CecLogLevel.Warning: + strLevel = "WARNING: "; + display = cbLogWarning.Checked; + break; + case CecLogLevel.Notice: + strLevel = "NOTICE: "; + display = cbLogNotice.Checked; + break; + case CecLogLevel.Traffic: + strLevel = "TRAFFIC: "; + display = cbLogTraffic.Checked; + break; + case CecLogLevel.Debug: + strLevel = "DEBUG: "; + display = cbLogDebug.Checked; + break; + default: + break; + } + + if (display) + { + string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine; + tbLog.Text += strLog; + tbLog.Select(tbLog.Text.Length, 0); + tbLog.ScrollToCaret(); + } + } + } + + private void bClearLog_Click(object sender, EventArgs e) + { + tbLog.Text = string.Empty; + } + + private void bSaveLog_Click(object sender, EventArgs e) + { + SaveFileDialog dialog = new SaveFileDialog() + { + Title = "Where do you want to store the log file?", + InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), + FileName = "cec-log.txt", + Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*", + FilterIndex = 1 + }; + + if (dialog.ShowDialog() == DialogResult.OK) + { + FileStream fs = (FileStream)dialog.OpenFile(); + if (fs == null) + { + MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + else + { + StreamWriter writer = new StreamWriter(fs); + writer.Write(tbLog.Text); + writer.Close(); + fs.Close(); + fs.Dispose(); + MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + } + #endregion + + #region LibCecSharp callbacks + public int ConfigurationChanged(LibCECConfiguration config) + { + Config = config; + SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress)); + SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString); + SetControlText(cbPortNumber, Config.HDMIPort.ToString()); + SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage); + SetCheckboxChecked(cbActivateSource, Config.ActivateSource); + SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver); + SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby); + UpdateSelectedDevice(); + return 1; + } + + public int ReceiveCommand(CecCommand command) + { + return 1; + } + + public int ReceiveKeypress(CecKeypress key) + { + SelectKeypressRow(key); + return 1; + } + + public int ReceiveLogMessage(CecLogMessage message) + { + AddLogMessage(message); + return 1; + } + #endregion + + #region Class members + public bool HasAVRDevice { get; private set; } + #region TV Vendor + private CecVendorId _tvVendor = CecVendorId.Unknown; + public CecVendorId TVVendor + { + get { return _tvVendor;} + private set { _tvVendor = value; } + } + public string TVVendorString + { + get + { + return TVVendor != CecVendorId.Unknown ? + "Television (" + Lib.ToString(TVVendor) + ")" : + "Television"; + } + } + #endregion + #region AVR Vendor + private CecVendorId _avrVendor = CecVendorId.Unknown; + public CecVendorId AVRVendor + { + get { return _avrVendor; } + private set { _avrVendor = value; } + } + public string AVRVendorString + { + get + { + return AVRVendor != CecVendorId.Unknown ? + "AVR (" + Lib.ToString(AVRVendor) + ")" : + "AVR"; + } + } + #endregion + public CecLogicalAddress SelectedConnectedDevice + { + get + { + return (cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv; + } + } + public int SelectedPortNumber + { + get + { + int iPortNumber = 0; + if (!int.TryParse(cbPortNumber.Text, out iPortNumber)) + iPortNumber = 1; + return iPortNumber; + } + } + protected LibCECConfiguration Config; + protected LibCecSharp Lib; + private CecCallbackWrapper Callbacks; + private UpdateProcess ActiveProcess = null; + private bool SuppressUpdates = false; + #endregion } + /// + /// A little wrapper that is needed because we already inherit form + /// internal class CecCallbackWrapper : CecCallbackMethods { public CecCallbackWrapper(CecConfigGUI gui)