| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved. |
| 5 | * libCEC(R) is an original work, containing original code. |
| 6 | * |
| 7 | * libCEC(R) is a trademark of Pulse-Eight Limited. |
| 8 | * |
| 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. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 22 | * |
| 23 | * |
| 24 | * Alternatively, you can license this library under a commercial license, |
| 25 | * please contact Pulse-Eight Licensing for more information. |
| 26 | * |
| 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/ |
| 31 | */ |
| 32 | |
| 33 | using System.Threading; |
| 34 | using CecSharp; |
| 35 | using LibCECTray.controller.actions; |
| 36 | using LibCECTray.ui; |
| 37 | using System.Windows.Forms; |
| 38 | |
| 39 | namespace LibCECTray.controller |
| 40 | { |
| 41 | /// <summary> |
| 42 | /// Actions that can be executed by a background thread |
| 43 | /// </summary> |
| 44 | internal class Actions |
| 45 | { |
| 46 | public Actions(CECController controller) |
| 47 | { |
| 48 | _controller = controller; |
| 49 | } |
| 50 | |
| 51 | /// <summary> |
| 52 | /// Event handler for processing updates for a background thread |
| 53 | /// </summary> |
| 54 | /// <param name="src">The source that sent the event</param> |
| 55 | /// <param name="updateEvent">The type of event</param> |
| 56 | private void ProcessEventHandler(object src, UpdateEvent updateEvent) |
| 57 | { |
| 58 | switch (updateEvent.Type) |
| 59 | { |
| 60 | case UpdateEventType.StatusText: |
| 61 | _controller.SetStatusText(updateEvent.StringValue); |
| 62 | break; |
| 63 | case UpdateEventType.ProgressBar: |
| 64 | _controller.SetProgressBar(updateEvent.IntValue, true); |
| 65 | break; |
| 66 | case UpdateEventType.PhysicalAddress: |
| 67 | _controller.Settings.PhysicalAddress.Value = (ushort)updateEvent.IntValue; |
| 68 | break; |
| 69 | case UpdateEventType.TVVendorId: |
| 70 | _controller.Settings.SetVendorName(CecLogicalAddress.Tv, (CecVendorId)updateEvent.IntValue, _controller.Lib.ToString((CecVendorId)updateEvent.IntValue)); |
| 71 | break; |
| 72 | case UpdateEventType.BaseDevice: |
| 73 | _controller.Settings.ConnectedDevice.Value = (CecLogicalAddress)updateEvent.IntValue; |
| 74 | break; |
| 75 | case UpdateEventType.HDMIPort: |
| 76 | _controller.Settings.HDMIPort.Value = (byte)updateEvent.IntValue; |
| 77 | break; |
| 78 | case UpdateEventType.HasAVRDevice: |
| 79 | CecLogicalAddresses allowedMask = new CecLogicalAddresses(); |
| 80 | allowedMask.Set(CecLogicalAddress.Tv); |
| 81 | if (updateEvent.BoolValue) |
| 82 | allowedMask.Set(CecLogicalAddress.AudioSystem); |
| 83 | _controller.Settings.ConnectedDevice.AllowedAddressMask = allowedMask; |
| 84 | break; |
| 85 | case UpdateEventType.AVRVendorId: |
| 86 | _controller.Settings.SetVendorName(CecLogicalAddress.AudioSystem, (CecVendorId)updateEvent.IntValue, _controller.Lib.ToString((CecVendorId)updateEvent.IntValue)); |
| 87 | break; |
| 88 | case UpdateEventType.Configuration: |
| 89 | SuppressUpdates = true; |
| 90 | _controller.ConfigurationChanged(updateEvent.ConfigValue); |
| 91 | SuppressUpdates = false; |
| 92 | break; |
| 93 | case UpdateEventType.PollDevices: |
| 94 | _controller.CheckActiveDevices(); |
| 95 | break; |
| 96 | case UpdateEventType.ProcessCompleted: |
| 97 | if (!(_activeProcess is GetCurrentPhysicalAddress) && !SuppressUpdates) |
| 98 | { |
| 99 | _activeProcess = new GetCurrentPhysicalAddress(_controller.Lib); |
| 100 | _activeProcess.EventHandler += ProcessEventHandler; |
| 101 | (new Thread(_activeProcess.Run)).Start(); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | _activeProcess = null; |
| 106 | } |
| 107 | |
| 108 | if (_updatingInfoPanel != null) |
| 109 | { |
| 110 | _updatingInfoPanel.SetControlEnabled(_updatingInfoPanel.bUpdate, true); |
| 111 | _updatingInfoPanel = null; |
| 112 | } |
| 113 | |
| 114 | _controller.SetControlsEnabled(true); |
| 115 | _controller.SetProgressBar(100, false); |
| 116 | |
| 117 | if (_controller.Settings.StartHidden.Value) |
| 118 | { |
| 119 | _controller.SetShowInTaskbar(false); |
| 120 | //SetToolStripMenuText(tsMenuShowHide, Resources.show); |
| 121 | _controller.Hide(true); |
| 122 | } |
| 123 | |
| 124 | break; |
| 125 | case UpdateEventType.ExitApplication: |
| 126 | SuppressUpdates = true; |
| 127 | _activeProcess = null; |
| 128 | Application.Exit(); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #region Actions |
| 134 | /// <summary> |
| 135 | /// Updates the contents of a device information window |
| 136 | /// </summary> |
| 137 | /// <param name="panel">The panel to update</param> |
| 138 | public void UpdateInfoPanel(DeviceInformation panel) |
| 139 | { |
| 140 | if (SuppressUpdates || _activeProcess != null) return; |
| 141 | |
| 142 | _controller.SetControlsEnabled(false); |
| 143 | _updatingInfoPanel = panel; |
| 144 | panel.SetControlEnabled(panel.bUpdate, false); |
| 145 | _activeProcess = new UpdateDeviceInfo(_controller, _controller.Lib, panel); |
| 146 | _activeProcess.EventHandler += ProcessEventHandler; |
| 147 | (new Thread(_activeProcess.Run)).Start(); |
| 148 | } |
| 149 | |
| 150 | /// <summary> |
| 151 | /// Change the device to which the adapter is connected and/or the HDMI port number |
| 152 | /// </summary> |
| 153 | /// <param name="address">The new device to which the adapter is connected</param> |
| 154 | /// <param name="portnumber">The new HDMI port number</param> |
| 155 | public void SetConnectedDevice(CecLogicalAddress address, int portnumber) |
| 156 | { |
| 157 | if (SuppressUpdates || _activeProcess != null) return; |
| 158 | |
| 159 | _controller.SetControlsEnabled(false); |
| 160 | _activeProcess = new UpdateConnectedDevice(_controller.Lib, address, portnumber); |
| 161 | _activeProcess.EventHandler += ProcessEventHandler; |
| 162 | (new Thread(_activeProcess.Run)).Start(); |
| 163 | } |
| 164 | |
| 165 | /// <summary> |
| 166 | /// Changes the physical address setting of libCEC |
| 167 | /// </summary> |
| 168 | /// <param name="physicalAddress">The new physical address</param> |
| 169 | public void SetPhysicalAddress(ushort physicalAddress) |
| 170 | { |
| 171 | if (SuppressUpdates || _activeProcess != null || !_controller.Settings.OverridePhysicalAddress.Value) return; |
| 172 | |
| 173 | _controller.SetControlsEnabled(false); |
| 174 | _activeProcess = new UpdatePhysicalAddress(_controller.Lib, physicalAddress); |
| 175 | _activeProcess.EventHandler += ProcessEventHandler; |
| 176 | (new Thread(_activeProcess.Run)).Start(); |
| 177 | } |
| 178 | |
| 179 | /// <summary> |
| 180 | /// Send an updated configuration to libCEC |
| 181 | /// </summary> |
| 182 | /// <param name="config">The new configuration</param> |
| 183 | public void UpdateConfigurationAsync(LibCECConfiguration config) |
| 184 | { |
| 185 | if (SuppressUpdates || _activeProcess != null) return; |
| 186 | |
| 187 | _controller.SetControlsEnabled(false); |
| 188 | _activeProcess = new UpdateConfiguration(_controller.Lib, config); |
| 189 | _activeProcess.EventHandler += ProcessEventHandler; |
| 190 | (new Thread(_activeProcess.Run)).Start(); |
| 191 | } |
| 192 | |
| 193 | /// <summary> |
| 194 | /// Send an image view on command to the device at the given logical address |
| 195 | /// </summary> |
| 196 | /// <param name="address">The address to send the image view on command to</param> |
| 197 | public void SendImageViewOn(CecLogicalAddress address) |
| 198 | { |
| 199 | if (SuppressUpdates || _activeProcess != null) return; |
| 200 | |
| 201 | _controller.SetControlsEnabled(false); |
| 202 | _activeProcess = new SendImageViewOn(_controller.Lib, address); |
| 203 | _activeProcess.EventHandler += ProcessEventHandler; |
| 204 | (new Thread(_activeProcess.Run)).Start(); |
| 205 | } |
| 206 | |
| 207 | /// <summary> |
| 208 | /// Activate the source at the given logical address. |
| 209 | /// </summary> |
| 210 | /// <param name="address">The logical address of the device to activate</param> |
| 211 | public void SetStreamPath(CecLogicalAddress address) |
| 212 | { |
| 213 | if (SuppressUpdates || _activeProcess != null) return; |
| 214 | |
| 215 | _controller.SetControlsEnabled(false); |
| 216 | _activeProcess = new SendActivateSource(_controller.Lib, address); |
| 217 | _activeProcess.EventHandler += ProcessEventHandler; |
| 218 | (new Thread(_activeProcess.Run)).Start(); |
| 219 | } |
| 220 | |
| 221 | public void ActivateSource() |
| 222 | { |
| 223 | if (SuppressUpdates || _activeProcess != null) return; |
| 224 | |
| 225 | _controller.SetControlsEnabled(false); |
| 226 | _activeProcess = new ActivateSource(_controller.Lib); |
| 227 | _activeProcess.EventHandler += ProcessEventHandler; |
| 228 | (new Thread(_activeProcess.Run)).Start(); |
| 229 | } |
| 230 | |
| 231 | /// <summary> |
| 232 | /// Send a standby command to the device at the given logical address |
| 233 | /// </summary> |
| 234 | /// <param name="address">The logical address of the device to send to standby</param> |
| 235 | public void SendStandby(CecLogicalAddress address) |
| 236 | { |
| 237 | if (SuppressUpdates || _activeProcess != null) return; |
| 238 | |
| 239 | _controller.SetControlsEnabled(false); |
| 240 | _activeProcess = new SendStandby(_controller.Lib, address); |
| 241 | _activeProcess.EventHandler += ProcessEventHandler; |
| 242 | (new Thread(_activeProcess.Run)).Start(); |
| 243 | } |
| 244 | |
| 245 | /// <summary> |
| 246 | /// Fetch device information and show an information dialog for the device at the given logical address |
| 247 | /// </summary> |
| 248 | /// <param name="address">The logical address of the device to get the info for</param> |
| 249 | public void ShowDeviceInfo(CecLogicalAddress address) |
| 250 | { |
| 251 | if (SuppressUpdates || _activeProcess != null) return; |
| 252 | |
| 253 | _controller.SetControlsEnabled(false); |
| 254 | _activeProcess = new ShowDeviceInfo(_controller, _controller.Lib, address); |
| 255 | _activeProcess.EventHandler += ProcessEventHandler; |
| 256 | (new Thread(_activeProcess.Run)).Start(); |
| 257 | } |
| 258 | |
| 259 | /// <summary> |
| 260 | /// Poll devices to check which ones are active |
| 261 | /// </summary> |
| 262 | public void RescanDevices() |
| 263 | { |
| 264 | if (SuppressUpdates || _activeProcess != null) return; |
| 265 | |
| 266 | _controller.SetControlsEnabled(false); |
| 267 | _activeProcess = new RescanDevices(_controller.Lib); |
| 268 | _activeProcess.EventHandler += ProcessEventHandler; |
| 269 | (new Thread(_activeProcess.Run)).Start(); |
| 270 | } |
| 271 | |
| 272 | /// <summary> |
| 273 | /// Update the physical address of libCEC that is displayed in the UI |
| 274 | /// </summary> |
| 275 | public void UpdatePhysicalAddress() |
| 276 | { |
| 277 | if (SuppressUpdates || _activeProcess != null) return; |
| 278 | |
| 279 | _controller.SetControlsEnabled(false); |
| 280 | _activeProcess = new GetCurrentPhysicalAddress(_controller.Lib); |
| 281 | _activeProcess.EventHandler += ProcessEventHandler; |
| 282 | (new Thread(_activeProcess.Run)).Start(); |
| 283 | } |
| 284 | |
| 285 | /// <summary> |
| 286 | /// Connect to the adapter with the given configuration |
| 287 | /// </summary> |
| 288 | /// <param name="config">The client configuration</param> |
| 289 | public void ConnectToDevice(LibCECConfiguration config) |
| 290 | { |
| 291 | if (_activeProcess != null) return; |
| 292 | |
| 293 | _activeProcess = new ConnectToDevice(_controller.Lib, config); |
| 294 | _activeProcess.EventHandler += ProcessEventHandler; |
| 295 | (new Thread(_activeProcess.Run)).Start(); |
| 296 | } |
| 297 | #endregion |
| 298 | |
| 299 | #region Members |
| 300 | private readonly CECController _controller; |
| 301 | private DeviceInformation _updatingInfoPanel; |
| 302 | public bool SuppressUpdates = true; |
| 303 | private UpdateProcess _activeProcess; |
| 304 | #endregion |
| 305 | } |
| 306 | } |