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/
33 using System.Threading;
35 using LibCECTray.controller.actions;
37 using System.Windows.Forms;
39 namespace LibCECTray.controller
42 /// Actions that can be executed by a background thread
44 internal class Actions
46 public Actions(CECController controller)
48 _controller = controller;
52 /// Event handler for processing updates for a background thread
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)
58 switch (updateEvent.Type)
60 case UpdateEventType.StatusText:
61 _controller.SetStatusText(updateEvent.StringValue);
63 case UpdateEventType.ProgressBar:
64 _controller.SetProgressBar(updateEvent.IntValue, true);
66 case UpdateEventType.PhysicalAddress:
67 _controller.Settings.PhysicalAddress.Value = (ushort)updateEvent.IntValue;
69 case UpdateEventType.TVVendorId:
70 _controller.Settings.SetVendorName(CecLogicalAddress.Tv, (CecVendorId)updateEvent.IntValue, _controller.Lib.ToString((CecVendorId)updateEvent.IntValue));
72 case UpdateEventType.BaseDevice:
73 _controller.Settings.ConnectedDevice.Value = (CecLogicalAddress)updateEvent.IntValue;
75 case UpdateEventType.HDMIPort:
76 _controller.Settings.HDMIPort.Value = (byte)updateEvent.IntValue;
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;
85 case UpdateEventType.AVRVendorId:
86 _controller.Settings.SetVendorName(CecLogicalAddress.AudioSystem, (CecVendorId)updateEvent.IntValue, _controller.Lib.ToString((CecVendorId)updateEvent.IntValue));
88 case UpdateEventType.Configuration:
89 SuppressUpdates = true;
90 _controller.ConfigurationChanged(updateEvent.ConfigValue);
91 SuppressUpdates = false;
93 case UpdateEventType.PollDevices:
94 _controller.CheckActiveDevices();
96 case UpdateEventType.ProcessCompleted:
97 if (!(_activeProcess is GetCurrentPhysicalAddress) && !SuppressUpdates)
99 _activeProcess = new GetCurrentPhysicalAddress(_controller.Lib);
100 _activeProcess.EventHandler += ProcessEventHandler;
101 (new Thread(_activeProcess.Run)).Start();
105 _activeProcess = null;
108 if (_updatingInfoPanel != null)
110 _updatingInfoPanel.SetControlEnabled(_updatingInfoPanel.bUpdate, true);
111 _updatingInfoPanel = null;
114 _controller.SetControlsEnabled(true);
115 _controller.SetProgressBar(100, false);
117 if (_controller.Settings.StartHidden.Value)
119 _controller.SetShowInTaskbar(false);
120 //SetToolStripMenuText(tsMenuShowHide, Resources.show);
121 _controller.Hide(true);
125 case UpdateEventType.ExitApplication:
126 SuppressUpdates = true;
127 _activeProcess = null;
135 /// Updates the contents of a device information window
137 /// <param name="panel">The panel to update</param>
138 public void UpdateInfoPanel(DeviceInformation panel)
140 if (SuppressUpdates || _activeProcess != null) return;
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();
151 /// Change the device to which the adapter is connected and/or the HDMI port number
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)
157 if (SuppressUpdates || _activeProcess != null) return;
159 _controller.SetControlsEnabled(false);
160 _activeProcess = new UpdateConnectedDevice(_controller.Lib, address, portnumber);
161 _activeProcess.EventHandler += ProcessEventHandler;
162 (new Thread(_activeProcess.Run)).Start();
166 /// Changes the physical address setting of libCEC
168 /// <param name="physicalAddress">The new physical address</param>
169 public void SetPhysicalAddress(ushort physicalAddress)
171 if (SuppressUpdates || _activeProcess != null || !_controller.Settings.OverridePhysicalAddress.Value) return;
173 _controller.SetControlsEnabled(false);
174 _activeProcess = new UpdatePhysicalAddress(_controller.Lib, physicalAddress);
175 _activeProcess.EventHandler += ProcessEventHandler;
176 (new Thread(_activeProcess.Run)).Start();
180 /// Send an updated configuration to libCEC
182 /// <param name="config">The new configuration</param>
183 public void UpdateConfigurationAsync(LibCECConfiguration config)
185 if (SuppressUpdates || _activeProcess != null) return;
187 _controller.SetControlsEnabled(false);
188 _activeProcess = new UpdateConfiguration(_controller.Lib, config);
189 _activeProcess.EventHandler += ProcessEventHandler;
190 (new Thread(_activeProcess.Run)).Start();
194 /// Send an image view on command to the device at the given logical address
196 /// <param name="address">The address to send the image view on command to</param>
197 public void SendImageViewOn(CecLogicalAddress address)
199 if (SuppressUpdates || _activeProcess != null) return;
201 _controller.SetControlsEnabled(false);
202 _activeProcess = new SendImageViewOn(_controller.Lib, address);
203 _activeProcess.EventHandler += ProcessEventHandler;
204 (new Thread(_activeProcess.Run)).Start();
208 /// Activate the source at the given logical address.
210 /// <param name="address">The logical address of the device to activate</param>
211 public void ActivateSource(CecLogicalAddress address)
213 if (SuppressUpdates || _activeProcess != null) return;
215 _controller.SetControlsEnabled(false);
216 _activeProcess = new SendActivateSource(_controller.Lib, address);
217 _activeProcess.EventHandler += ProcessEventHandler;
218 (new Thread(_activeProcess.Run)).Start();
222 /// Send a standby command to the device at the given logical address
224 /// <param name="address">The logical address of the device to send to standby</param>
225 public void SendStandby(CecLogicalAddress address)
227 if (SuppressUpdates || _activeProcess != null) return;
229 _controller.SetControlsEnabled(false);
230 _activeProcess = new SendStandby(_controller.Lib, address);
231 _activeProcess.EventHandler += ProcessEventHandler;
232 (new Thread(_activeProcess.Run)).Start();
236 /// Fetch device information and show an information dialog for the device at the given logical address
238 /// <param name="address">The logical address of the device to get the info for</param>
239 public void ShowDeviceInfo(CecLogicalAddress address)
241 if (SuppressUpdates || _activeProcess != null) return;
243 _controller.SetControlsEnabled(false);
244 _activeProcess = new ShowDeviceInfo(_controller, _controller.Lib, address);
245 _activeProcess.EventHandler += ProcessEventHandler;
246 (new Thread(_activeProcess.Run)).Start();
250 /// Poll devices to check which ones are active
252 public void RescanDevices()
254 if (SuppressUpdates || _activeProcess != null) return;
256 _controller.SetControlsEnabled(false);
257 _activeProcess = new RescanDevices(_controller.Lib);
258 _activeProcess.EventHandler += ProcessEventHandler;
259 (new Thread(_activeProcess.Run)).Start();
263 /// Update the physical address of libCEC that is displayed in the UI
265 public void UpdatePhysicalAddress()
267 if (SuppressUpdates || _activeProcess != null) return;
269 _controller.SetControlsEnabled(false);
270 _activeProcess = new GetCurrentPhysicalAddress(_controller.Lib);
271 _activeProcess.EventHandler += ProcessEventHandler;
272 (new Thread(_activeProcess.Run)).Start();
276 /// Connect to the adapter with the given configuration
278 /// <param name="config">The client configuration</param>
279 public void ConnectToDevice(LibCECConfiguration config)
281 if (_activeProcess != null) return;
283 _activeProcess = new ConnectToDevice(_controller.Lib, config);
284 _activeProcess.EventHandler += ProcessEventHandler;
285 (new Thread(_activeProcess.Run)).Start();
290 private readonly CECController _controller;
291 private DeviceInformation _updatingInfoPanel;
292 public bool SuppressUpdates = true;
293 private UpdateProcess _activeProcess;