2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
34 using System.Windows.Forms;
37 using LibCECTray.Properties;
38 using LibCECTray.controller;
39 using LibCECTray.controller.applications;
40 using LibCECTray.settings;
42 namespace LibCECTray.ui
45 /// The tab pages in this application
47 internal enum ConfigTab
58 /// Main LibCecTray GUI
60 partial class CECTray : AsyncForm
64 Text = Resources.app_name;
65 InitializeComponent();
66 _controller = new CECController(this);
67 VisibleChanged += delegate
76 public override sealed string Text
78 get { return base.Text; }
79 set { base.Text = value; }
82 private void CECTrayLoad(object sender, EventArgs e)
84 _controller.Initialise();
87 protected override void Dispose(bool disposing)
94 if (disposing && (components != null))
98 base.Dispose(disposing);
101 #region Configuration tab
103 /// Replaces the gui controls by the ones that are bound to the settings.
104 /// this is a fugly way to do it, but the gui designer doesn't allow us to ref CECSettings, since it uses symbols from LibCecSharp
106 public void InitialiseSettingsComponent(CECSettings settings)
108 settings.WakeDevices.ReplaceControls(this, Configuration.Controls, lWakeDevices, cbWakeDevices);
109 settings.PowerOffDevices.ReplaceControls(this, Configuration.Controls, lPowerOff, cbPowerOffDevices);
110 settings.OverridePhysicalAddress.ReplaceControls(this, Configuration.Controls, cbOverrideAddress);
111 settings.OverrideTVVendor.ReplaceControls(this, Configuration.Controls, cbVendorOverride);
112 settings.PhysicalAddress.ReplaceControls(this, Configuration.Controls, tbPhysicalAddress);
113 settings.HDMIPort.ReplaceControls(this, Configuration.Controls, lPortNumber, cbPortNumber);
114 settings.ConnectedDevice.ReplaceControls(this, Configuration.Controls, lConnectedDevice, cbConnectedDevice);
115 settings.ActivateSource.ReplaceControls(this, Configuration.Controls, cbActivateSource);
116 settings.DeviceType.ReplaceControls(this, Configuration.Controls, lDeviceType, cbDeviceType);
117 settings.TVVendor.ReplaceControls(this, Configuration.Controls, cbVendorId);
118 settings.StartHidden.ReplaceControls(this, Configuration.Controls, cbStartMinimised);
121 private void BSaveClick(object sender, EventArgs e)
123 _controller.PersistSettings();
126 private void BReloadConfigClick(object sender, EventArgs e)
128 _controller.ResetDefaultSettings();
132 #region CEC Tester tab
133 delegate void SetActiveDevicesCallback(string[] activeDevices);
134 public void SetActiveDevices(string[] activeDevices)
136 if (cbCommandDestination.InvokeRequired)
138 SetActiveDevicesCallback d = SetActiveDevices;
141 Invoke(d, new object[] { activeDevices });
143 catch (Exception) { }
147 cbCommandDestination.Items.Clear();
148 foreach (string item in activeDevices)
149 cbCommandDestination.Items.Add(item);
153 delegate CecLogicalAddress GetTargetDeviceCallback();
154 private CecLogicalAddress GetTargetDevice()
156 if (cbCommandDestination.InvokeRequired)
158 GetTargetDeviceCallback d = GetTargetDevice;
159 CecLogicalAddress retval = CecLogicalAddress.Unknown;
162 retval = (CecLogicalAddress)Invoke(d, new object[] { });
164 catch (Exception) { }
168 return CECSettingLogicalAddresses.GetLogicalAddressFromString(cbCommandDestination.Text);
171 private void BSendImageViewOnClick(object sender, EventArgs e)
173 _controller.CECActions.SendImageViewOn(GetTargetDevice());
176 private void BStandbyClick(object sender, EventArgs e)
178 _controller.CECActions.SendStandby(GetTargetDevice());
181 private void BScanClick(object sender, EventArgs e)
183 _controller.CECActions.ShowDeviceInfo(GetTargetDevice());
186 private void BActivateSourceClick(object sender, EventArgs e)
188 _controller.CECActions.ActivateSource(GetTargetDevice());
191 private void CbCommandDestinationSelectedIndexChanged(object sender, EventArgs e)
193 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
194 bVolUp.Enabled = enableVolumeButtons;
195 bVolDown.Enabled = enableVolumeButtons;
196 bMute.Enabled = enableVolumeButtons;
197 bActivateSource.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
198 bScan.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
201 private void BVolUpClick(object sender, EventArgs e)
203 _controller.Lib.VolumeUp(true);
206 private void BVolDownClick(object sender, EventArgs e)
208 _controller.Lib.VolumeDown(true);
211 private void BMuteClick(object sender, EventArgs e)
213 _controller.Lib.MuteAudio(true);
216 private void BRescanDevicesClick(object sender, EventArgs e)
218 _controller.CECActions.RescanDevices();
223 delegate void UpdateLogCallback();
224 private void UpdateLog()
226 if (tbLog.InvokeRequired)
228 UpdateLogCallback d = UpdateLog;
231 Invoke(d, new object[] { });
233 catch (Exception) { }
238 tbLog.Select(tbLog.Text.Length, 0);
239 tbLog.ScrollToCaret();
243 public void AddLogMessage(CecLogMessage message)
245 string strLevel = "";
246 bool display = false;
247 switch (message.Level)
249 case CecLogLevel.Error:
250 strLevel = "ERROR: ";
251 display = cbLogError.Checked;
253 case CecLogLevel.Warning:
254 strLevel = "WARNING: ";
255 display = cbLogWarning.Checked;
257 case CecLogLevel.Notice:
258 strLevel = "NOTICE: ";
259 display = cbLogNotice.Checked;
261 case CecLogLevel.Traffic:
262 strLevel = "TRAFFIC: ";
263 display = cbLogTraffic.Checked;
265 case CecLogLevel.Debug:
266 strLevel = "DEBUG: ";
267 display = cbLogDebug.Checked;
273 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + Environment.NewLine;
274 AddLogMessage(strLog);
278 public void AddLogMessage(string message)
282 if (_selectedTab == ConfigTab.Log)
286 private void BClearLogClick(object sender, EventArgs e)
292 private void BSaveLogClick(object sender, EventArgs e)
294 SaveFileDialog dialog = new SaveFileDialog
296 Title = Resources.where_do_you_want_to_store_the_log,
297 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
298 FileName = Resources.cec_log_filename,
299 Filter = Resources.cec_log_filter,
303 if (dialog.ShowDialog() == DialogResult.OK)
305 FileStream fs = (FileStream)dialog.OpenFile();
308 MessageBox.Show(string.Format(Resources.cannot_open_for_writing, dialog.FileName), Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Error);
312 StreamWriter writer = new StreamWriter(fs);
317 MessageBox.Show(string.Format(Resources.log_stored_as, dialog.FileName), Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Information);
323 #region Tray icon and window controls
324 private void HideToolStripMenuItemClick(object sender, EventArgs e)
329 private void CloseToolStripMenuItemClick(object sender, EventArgs e)
334 private void AboutToolStripMenuItemClick(object sender, EventArgs e)
336 (new About(_controller.LibServerVersion, _controller.LibClientVersion, _controller.LibInfo)).ShowDialog();
339 private void AdvancedModeToolStripMenuItemClick(object sender, EventArgs e)
341 _controller.Settings.AdvancedMode.Value = !advancedModeToolStripMenuItem.Checked;
342 ShowHideAdvanced(!advancedModeToolStripMenuItem.Checked);
345 private void BCancelClick(object sender, EventArgs e)
350 private void TrayIconClick(object sender, EventArgs e)
352 if (e is MouseEventArgs && (e as MouseEventArgs).Button == MouseButtons.Left)
358 ShowInTaskbar = false;
360 tsMenuShowHide.Text = Resources.show;
365 ShowInTaskbar = true;
366 WindowState = FormWindowState.Normal;
368 tsMenuShowHide.Text = Resources.hide;
371 private void ShowHideToggle()
373 if (Visible && WindowState != FormWindowState.Minimized)
375 _controller.Settings.StartHidden.Value = true;
380 _controller.Settings.StartHidden.Value = false;
385 private void TsMenuCloseClick(object sender, EventArgs e)
390 private void CECTrayResize(object sender, EventArgs e)
392 if (WindowState == FormWindowState.Minimized)
398 private void TsMenuShowHideClick(object sender, EventArgs e)
403 public void ShowHideAdvanced(bool setTo)
407 tsAdvanced.Checked = true;
408 advancedModeToolStripMenuItem.Checked = true;
410 if (!tabPanel.Controls.Contains(tbTestCommands))
411 TabControls.Add(tbTestCommands);
412 if (!tabPanel.Controls.Contains(LogOutput))
413 TabControls.Add(LogOutput);
418 tsAdvanced.Checked = false;
419 advancedModeToolStripMenuItem.Checked = false;
421 tabPanel.Controls.Remove(tbTestCommands);
422 tabPanel.Controls.Remove(LogOutput);
427 private void TsAdvancedClick(object sender, EventArgs e)
429 _controller.Settings.AdvancedMode.Value = !tsAdvanced.Checked;
430 ShowHideAdvanced(!tsAdvanced.Checked);
433 public void SetStatusText(string status)
435 SetControlText(lStatus, status);
438 public void SetProgressBar(int progress, bool visible)
440 SetControlVisible(pProgress, visible);
441 SetProgressValue(pProgress, progress);
444 public void SetControlsEnabled(bool val)
447 SetControlEnabled(bClose, val);
448 SetControlEnabled(bSaveConfig, val);
449 SetControlEnabled(bReloadConfig, val);
452 SetControlEnabled(bRescanDevices, val);
453 SetControlEnabled(bSendImageViewOn, val);
454 SetControlEnabled(bStandby, val);
455 SetControlEnabled(bActivateSource, val);
456 SetControlEnabled(bScan, val);
458 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
459 SetControlEnabled(bVolUp, enableVolumeButtons);
460 SetControlEnabled(bVolDown, enableVolumeButtons);
461 SetControlEnabled(bMute, enableVolumeButtons);
464 private void TabControl1SelectedIndexChanged(object sender, EventArgs e)
466 switch (tabPanel.TabPages[tabPanel.SelectedIndex].Name)
468 case "tbTestCommands":
469 _selectedTab = ConfigTab.Tester;
472 _selectedTab = ConfigTab.Log;
476 _selectedTab = ConfigTab.Configuration;
482 #region Class members
483 private ConfigTab _selectedTab = ConfigTab.Configuration;
484 private string _log = string.Empty;
485 private readonly CECController _controller;
486 public CECController Controller
488 get { return _controller; }
490 public Control.ControlCollection TabControls
492 get { return tabPanel.Controls; }
494 public string SelectedTabName
496 get { return GetSelectedTabName(tabPanel, tabPanel.TabPages); }
500 private void AddNewApplicationToolStripMenuItemClick(object sender, EventArgs e)
502 ConfigureApplication appConfig = new ConfigureApplication(_controller.Settings, _controller);
503 _controller.DisplayDialog(appConfig, false);