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/
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))
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 _activeProcess = null;
134 /// Updates the contents of a device information window
136 /// <param name="panel">The panel to update</param>
137 public void UpdateInfoPanel(DeviceInformation panel)
139 if (SuppressUpdates || _activeProcess != null) return;
141 _controller.SetControlsEnabled(false);
142 _updatingInfoPanel = panel;
143 panel.SetControlEnabled(panel.bUpdate, false);
144 _activeProcess = new UpdateDeviceInfo(_controller, _controller.Lib, panel);
145 _activeProcess.EventHandler += ProcessEventHandler;
146 (new Thread(_activeProcess.Run)).Start();
150 /// Change the device to which the adapter is connected and/or the HDMI port number
152 /// <param name="address">The new device to which the adapter is connected</param>
153 /// <param name="portnumber">The new HDMI port number</param>
154 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
156 if (SuppressUpdates || _activeProcess != null) return;
158 _controller.SetControlsEnabled(false);
159 _activeProcess = new UpdateConnectedDevice(_controller.Lib, address, portnumber);
160 _activeProcess.EventHandler += ProcessEventHandler;
161 (new Thread(_activeProcess.Run)).Start();
165 /// Changes the physical address setting of libCEC
167 /// <param name="physicalAddress">The new physical address</param>
168 public void SetPhysicalAddress(ushort physicalAddress)
170 if (SuppressUpdates || _activeProcess != null || !_controller.Settings.OverridePhysicalAddress.Value) return;
172 _controller.SetControlsEnabled(false);
173 _activeProcess = new UpdatePhysicalAddress(_controller.Lib, physicalAddress);
174 _activeProcess.EventHandler += ProcessEventHandler;
175 (new Thread(_activeProcess.Run)).Start();
179 /// Send an updated configuration to libCEC
181 /// <param name="config">The new configuration</param>
182 public void UpdateConfigurationAsync(LibCECConfiguration config)
184 if (SuppressUpdates || _activeProcess != null) return;
186 _controller.SetControlsEnabled(false);
187 _activeProcess = new UpdateConfiguration(_controller.Lib, config);
188 _activeProcess.EventHandler += ProcessEventHandler;
189 (new Thread(_activeProcess.Run)).Start();
193 /// Send an image view on command to the device at the given logical address
195 /// <param name="address">The address to send the image view on command to</param>
196 public void SendImageViewOn(CecLogicalAddress address)
198 if (SuppressUpdates || _activeProcess != null) return;
200 _controller.SetControlsEnabled(false);
201 _activeProcess = new SendImageViewOn(_controller.Lib, address);
202 _activeProcess.EventHandler += ProcessEventHandler;
203 (new Thread(_activeProcess.Run)).Start();
207 /// Activate the source at the given logical address.
209 /// <param name="address">The logical address of the device to activate</param>
210 public void ActivateSource(CecLogicalAddress address)
212 if (SuppressUpdates || _activeProcess != null) return;
214 _controller.SetControlsEnabled(false);
215 _activeProcess = new SendActivateSource(_controller.Lib, address);
216 _activeProcess.EventHandler += ProcessEventHandler;
217 (new Thread(_activeProcess.Run)).Start();
221 /// Send a standby command to the device at the given logical address
223 /// <param name="address">The logical address of the device to send to standby</param>
224 public void SendStandby(CecLogicalAddress address)
226 if (SuppressUpdates || _activeProcess != null) return;
228 _controller.SetControlsEnabled(false);
229 _activeProcess = new SendStandby(_controller.Lib, address);
230 _activeProcess.EventHandler += ProcessEventHandler;
231 (new Thread(_activeProcess.Run)).Start();
235 /// Fetch device information and show an information dialog for the device at the given logical address
237 /// <param name="address">The logical address of the device to get the info for</param>
238 public void ShowDeviceInfo(CecLogicalAddress address)
240 if (SuppressUpdates || _activeProcess != null) return;
242 _controller.SetControlsEnabled(false);
243 _activeProcess = new ShowDeviceInfo(_controller, _controller.Lib, address);
244 _activeProcess.EventHandler += ProcessEventHandler;
245 (new Thread(_activeProcess.Run)).Start();
249 /// Poll devices to check which ones are active
251 public void RescanDevices()
253 if (SuppressUpdates || _activeProcess != null) return;
255 _controller.SetControlsEnabled(false);
256 _activeProcess = new RescanDevices(_controller.Lib);
257 _activeProcess.EventHandler += ProcessEventHandler;
258 (new Thread(_activeProcess.Run)).Start();
262 /// Update the physical address of libCEC that is displayed in the UI
264 public void UpdatePhysicalAddress()
266 if (SuppressUpdates || _activeProcess != null) return;
268 _controller.SetControlsEnabled(false);
269 _activeProcess = new GetCurrentPhysicalAddress(_controller.Lib);
270 _activeProcess.EventHandler += ProcessEventHandler;
271 (new Thread(_activeProcess.Run)).Start();
275 /// Connect to the adapter with the given configuration
277 /// <param name="config">The client configuration</param>
278 public void ConnectToDevice(LibCECConfiguration config)
280 if (_activeProcess != null) return;
282 _activeProcess = new ConnectToDevice(_controller.Lib, config);
283 _activeProcess.EventHandler += ProcessEventHandler;
284 (new Thread(_activeProcess.Run)).Start();
289 private readonly CECController _controller;
290 private DeviceInformation _updatingInfoPanel;
291 public bool SuppressUpdates = true;
292 private UpdateProcess _activeProcess;