2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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.Collections.Generic;
35 using System.Windows.Forms;
37 using LibCECTray.Properties;
38 using LibCECTray.controller.applications;
39 using LibCECTray.settings;
42 namespace LibCECTray.controller
44 internal class CECController : CecCallbackMethods
46 public CECController(CECTray gui)
49 CECActions = new Actions(this);
53 Settings.SettingChanged += OnSettingChanged;
58 /// Called when a setting changed
60 /// <param name="setting">The setting that changed</param>
61 /// <param name="oldValue">The old value</param>
62 /// <param name="newValue">The new value</param>
63 private void OnSettingChanged(CECSetting setting, object oldValue, object newValue)
65 if (setting.KeyName == CECSettings.KeyHDMIPort)
67 CECSettingByte byteSetting = setting as CECSettingByte;
68 if (byteSetting != null)
70 if (!Settings.OverridePhysicalAddress.Value)
71 Config.HDMIPort = byteSetting.Value;
72 CECActions.SetConnectedDevice(Settings.ConnectedDevice.Value, byteSetting.Value);
75 else if (setting.KeyName == CECSettings.KeyConnectedToHDMIDevice)
77 CECSettingLogicalAddress laSetting = setting as CECSettingLogicalAddress;
78 if (laSetting != null)
80 if (!Settings.OverridePhysicalAddress.Value)
81 Config.BaseDevice = laSetting.Value;
82 CECActions.SetConnectedDevice(laSetting.Value, Settings.HDMIPort.Value);
85 else if (setting.KeyName == CECSettings.KeyPhysicalAddress)
87 CECSettingUShort ushortSetting = setting as CECSettingUShort;
88 if (ushortSetting != null && Settings.OverridePhysicalAddress.Value && Config.PhysicalAddress != ushortSetting.Value)
90 Config.PhysicalAddress = ushortSetting.Value;
91 CECActions.SetPhysicalAddress(ushortSetting.Value);
94 else if (setting.KeyName == CECSettings.KeyOverridePhysicalAddress)
96 CECSettingBool boolSetting = setting as CECSettingBool;
97 if (boolSetting != null)
99 Settings.PhysicalAddress.Enabled = boolSetting.Value;
100 Settings.HDMIPort.Enabled = !boolSetting.Value;
101 Settings.ConnectedDevice.Enabled = !boolSetting.Value;
102 if (boolSetting.Value)
104 Config.BaseDevice = Settings.ConnectedDevice.Value;
106 Config.PhysicalAddress = Settings.PhysicalAddress.Value;
110 Config.BaseDevice = Settings.ConnectedDevice.Value;
111 Config.HDMIPort = Settings.HDMIPort.Value;
112 Config.PhysicalAddress = 0xFFFF;
116 else if (setting.KeyName == CECSettings.KeyDeviceType)
118 CECSettingDeviceType dtSetting = setting as CECSettingDeviceType;
119 if (dtSetting != null)
121 if (dtSetting.Value != Config.DeviceTypes.Types[0] && Settings.OverrideTVVendor.Value)
123 Config.DeviceTypes.Types[0] = dtSetting.Value;
124 if (!_deviceChangeWarningDisplayed)
126 _deviceChangeWarningDisplayed = true;
127 MessageBox.Show(Resources.device_type_changed, Resources.app_name, MessageBoxButtons.OK,
128 MessageBoxIcon.Warning);
133 else if (setting.KeyName == CECSettings.KeyOverrideTVVendor)
135 CECSettingBool boolSetting = setting as CECSettingBool;
136 if (boolSetting != null)
138 Settings.TVVendor.Enabled = boolSetting.Value;
139 Settings.TVVendor.Value = boolSetting.Value ? Lib.GetDeviceVendorId(CecLogicalAddress.Tv) : CecVendorId.Unknown;
140 Config.TvVendor = boolSetting.Value ? Settings.TVVendor.Value : CecVendorId.Unknown;
143 else if (setting.KeyName == CECSettings.KeyActivateSource)
145 CECSettingBool boolSetting = setting as CECSettingBool;
146 if (boolSetting != null)
147 Config.ActivateSource = boolSetting.Value;
149 else if (setting.KeyName == CECSettings.KeyTVVendor)
151 CECSettingVendorId vendorSetting = setting as CECSettingVendorId;
152 if (vendorSetting != null && Settings.OverrideTVVendor.Value)
153 Config.TvVendor = vendorSetting.Value;
155 else if (setting.KeyName == CECSettings.KeyWakeDevices)
157 CECSettingLogicalAddresses laSetting = setting as CECSettingLogicalAddresses;
158 if (laSetting != null)
159 Config.WakeDevices = laSetting.Value;
161 else if (setting.KeyName == CECSettings.KeyPowerOffDevices)
163 CECSettingLogicalAddresses laSetting = setting as CECSettingLogicalAddresses;
164 if (laSetting != null)
165 Config.PowerOffDevices = laSetting.Value;
170 /// Persist all known settings in the registry
172 public void PersistSettings()
174 /* save settings in the eeprom */
175 Lib.SetConfiguration(Config);
176 Lib.PersistConfiguration(Config);
178 /* and in the registry */
183 /// Reset all settings to their default values
185 public void ResetDefaultSettings()
187 SetControlsEnabled(false);
188 _gui.ShowHideAdvanced(false);
190 CECActions.SuppressUpdates = true;
191 Settings.SetDefaultValues();
193 Lib.SetConfiguration(Config);
194 CECActions.SuppressUpdates = false;
196 _gui.ShowHideAdvanced(Settings.AdvancedMode.Value);
197 SetControlsEnabled(true);
202 /// Opens a connection to libCEC and register known applications
204 public void Initialise()
206 CECActions.ConnectToDevice(Config);
207 Applications.Initialise(this);
211 /// Closes the connection to libCEC
215 Lib.DisableCallbacks();
216 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
221 /// Register a new application controller, which will add a new tab for the application
223 /// <param name="app">The new application to register</param>
224 /// <returns>True when registered, false otherwise</returns>
225 public bool RegisterApplication(ApplicationController app)
227 if (_applications.Contains(app)) return false;
228 _applications.Add(app);
230 _gui.SuspendLayout();
231 _gui.TabControls.Add(app.UiControl);
241 /// Initialises the UI
243 private void InitialiseGui()
245 _gui.SuspendLayout();
246 _gui.InitialiseSettingsComponent(Settings);
248 // add the controller tabs to the gui
249 foreach (var ui in ApplicationUIs)
250 _gui.TabControls.Add(ui);
252 // enable/disable advanced mode
253 _gui.ShowHideAdvanced(Settings.AdvancedMode.Value);
261 /// <param name="control">The dialog to display</param>
262 /// <param name="modal">True when modal</param>
263 public void DisplayDialog(Form control, bool modal)
265 _gui.DisplayDialog(control, modal);
269 /// Changes the status text of this window
271 /// <param name="status">The new status text</param>
272 public void SetStatusText(string status)
274 _gui.SetStatusText(status);
278 /// Changes the progress bar value
280 /// <param name="progress">The new progress percentage</param>
281 /// <param name="visible">True to make the bar visible, false otherwise</param>
282 public void SetProgressBar(int progress, bool visible)
284 _gui.SetProgressBar(progress, visible);
288 /// Enable/disable all controls
290 /// <param name="val">True to enable, false to disable</param>
291 public void SetControlsEnabled(bool val)
293 Settings.Enabled = val;
294 foreach (var app in _applications)
295 app.UiControl.SetEnabled(val);
296 _gui.SetControlsEnabled(val);
300 /// (re)check the logical addresses of the active devices on the bus
302 public void CheckActiveDevices()
304 var activeDevices = Lib.GetActiveDevices();
305 List<string> deviceList = new List<string>();
306 foreach (var activeDevice in activeDevices.Addresses)
308 if (activeDevice != CecLogicalAddress.Unknown)
309 deviceList.Add(string.Format("{0,1:X} : {1}", (int)activeDevice, Lib.ToString(activeDevice)));
311 deviceList.Add(string.Format("{0,1:X} : {1}", (int)CecLogicalAddress.Broadcast, Lib.ToString(CecLogicalAddress.Broadcast)));
313 _gui.SetActiveDevices(deviceList.ToArray());
317 /// Show/hide the taskbar entry
319 /// <param name="val">True to show, false to hide</param>
320 public void SetShowInTaskbar(bool val)
322 _gui.SetShowInTaskbar(val);
326 /// Show or hide this window
328 /// <param name="val">True to show, false to hide</param>
329 public void Hide(bool val)
335 #region Callbacks called by libCEC
336 public override int ReceiveCommand(CecCommand command)
341 public override int ReceiveKeypress(CecKeypress key)
343 foreach (var app in _applications)
345 bool keySent = app.SendKey(key, app.UiName == _gui.SelectedTabName);
349 string strLog = string.Format("sent key '{0}' to '{1}'", (int) key.Keycode, app.UiName) + Environment.NewLine;
350 _gui.AddLogMessage(strLog);
358 public override int ReceiveLogMessage(CecLogMessage message)
360 _gui.AddLogMessage(message);
364 public override int ConfigurationChanged(LibCECConfiguration config)
366 Settings.PhysicalAddress.Value = Config.PhysicalAddress;
367 Settings.ConnectedDevice.Value = Config.BaseDevice == CecLogicalAddress.AudioSystem ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
368 Settings.HDMIPort.Value = Config.HDMIPort;
369 Settings.WakeDevices.Value = Config.WakeDevices;
370 Settings.PowerOffDevices.Value = Config.PowerOffDevices;
371 Settings.ActivateSource.Value = Config.ActivateSource;
372 Settings.DeviceType.Value = config.DeviceTypes.Types[0];
374 if (config.TvVendor != CecVendorId.Unknown)
376 Settings.OverrideTVVendor.Value = true;
377 Settings.TVVendor.Value = config.TvVendor;
381 Settings.OverrideTVVendor.Value = false;
384 _gui.SetControlText(_gui, Resources.app_name + " - libCEC " + Lib.ToString(Config.ServerVersion));
386 CECActions.UpdatePhysicalAddress();
390 public override void SourceActivated(CecLogicalAddress logicalAddress, bool activated)
395 foreach (var app in _applications)
397 if (app.AutoStartApplication.Value)
405 /// List of tab pages for each application that the UI supports
407 public List<ControllerTabPage> ApplicationUIs
411 List<ControllerTabPage> retVal = new List<ControllerTabPage>();
412 foreach (var app in _applications)
413 retVal.Add(app.UiControl);
419 /// List of application controllers that the UI supports
421 private readonly List<ApplicationController> _applications = new List<ApplicationController>();
424 /// Settings that are persisted in the registry (when not using the default value)
426 public CECSettings Settings
428 get { return _settings ?? (_settings = new CECSettings(OnSettingChanged)); }
430 private CECSettings _settings;
433 /// libCEC configuration for the application
435 private LibCECConfiguration Config
441 _config = new LibCECConfiguration { DeviceName = "CEC Tray", ClientVersion = CecClientVersion.Version2_1_0 };
442 _config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
443 _config.SetCallbacks(this);
445 if (Settings.OverridePhysicalAddress.Value)
447 _config.PhysicalAddress = Settings.PhysicalAddress.Value;
448 _config.HDMIPort = 0;
449 _config.BaseDevice = CecLogicalAddress.Unknown;
453 _config.PhysicalAddress = 0;
454 _config.HDMIPort = Settings.HDMIPort.Value;
455 _config.BaseDevice = Settings.ConnectedDevice.Value;
457 _config.ActivateSource = Settings.ActivateSource.Value;
458 _config.DeviceTypes.Types[0] = Settings.DeviceType.Value;
459 if (Settings.OverrideTVVendor.Value)
460 _config.TvVendor = Settings.TVVendor.Value;
461 _config.WakeDevices = Settings.WakeDevices.Value;
462 _config.PowerOffDevices = Settings.PowerOffDevices.Value;
467 private LibCECConfiguration _config;
470 /// Get build info from libCEC
472 public string LibInfo
474 get { return Lib.GetLibInfo(); }
478 /// libCEC API version
480 public string LibServerVersion
482 get { return Lib.ToString(Config.ServerVersion); }
486 /// libCEC client API version
488 public string LibClientVersion
490 get { return Lib.ToString(Config.ClientVersion); }
494 /// Get the usb vendor id
496 public ushort AdapterVendorId
498 get { return Lib.GetAdapterVendorId(); }
502 /// Get the usb product id
504 public ushort AdapterProductId
506 get { return Lib.GetAdapterProductId(); }
513 public LibCecSharp Lib
515 get { return _lib ?? (_lib = new LibCecSharp(Config)); }
517 private LibCecSharp _lib;
519 private readonly CECTray _gui;
520 public Actions CECActions;
521 private bool _deviceChangeWarningDisplayed;