13dc92035be67a078ffb42660552a6d98c78baf8
[deb_libcec.git] / src / LibCecTray / controller / Actions.cs
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))
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 _activeProcess = null;
127 Application.Exit();
128 break;
129 }
130 }
131
132 #region Actions
133 /// <summary>
134 /// Updates the contents of a device information window
135 /// </summary>
136 /// <param name="panel">The panel to update</param>
137 public void UpdateInfoPanel(DeviceInformation panel)
138 {
139 if (SuppressUpdates || _activeProcess != null) return;
140
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();
147 }
148
149 /// <summary>
150 /// Change the device to which the adapter is connected and/or the HDMI port number
151 /// </summary>
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)
155 {
156 if (SuppressUpdates || _activeProcess != null) return;
157
158 _controller.SetControlsEnabled(false);
159 _activeProcess = new UpdateConnectedDevice(_controller.Lib, address, portnumber);
160 _activeProcess.EventHandler += ProcessEventHandler;
161 (new Thread(_activeProcess.Run)).Start();
162 }
163
164 /// <summary>
165 /// Changes the physical address setting of libCEC
166 /// </summary>
167 /// <param name="physicalAddress">The new physical address</param>
168 public void SetPhysicalAddress(ushort physicalAddress)
169 {
170 if (SuppressUpdates || _activeProcess != null || !_controller.Settings.OverridePhysicalAddress.Value) return;
171
172 _controller.SetControlsEnabled(false);
173 _activeProcess = new UpdatePhysicalAddress(_controller.Lib, physicalAddress);
174 _activeProcess.EventHandler += ProcessEventHandler;
175 (new Thread(_activeProcess.Run)).Start();
176 }
177
178 /// <summary>
179 /// Send an updated configuration to libCEC
180 /// </summary>
181 /// <param name="config">The new configuration</param>
182 public void UpdateConfigurationAsync(LibCECConfiguration config)
183 {
184 if (SuppressUpdates || _activeProcess != null) return;
185
186 _controller.SetControlsEnabled(false);
187 _activeProcess = new UpdateConfiguration(_controller.Lib, config);
188 _activeProcess.EventHandler += ProcessEventHandler;
189 (new Thread(_activeProcess.Run)).Start();
190 }
191
192 /// <summary>
193 /// Send an image view on command to the device at the given logical address
194 /// </summary>
195 /// <param name="address">The address to send the image view on command to</param>
196 public void SendImageViewOn(CecLogicalAddress address)
197 {
198 if (SuppressUpdates || _activeProcess != null) return;
199
200 _controller.SetControlsEnabled(false);
201 _activeProcess = new SendImageViewOn(_controller.Lib, address);
202 _activeProcess.EventHandler += ProcessEventHandler;
203 (new Thread(_activeProcess.Run)).Start();
204 }
205
206 /// <summary>
207 /// Activate the source at the given logical address.
208 /// </summary>
209 /// <param name="address">The logical address of the device to activate</param>
210 public void ActivateSource(CecLogicalAddress address)
211 {
212 if (SuppressUpdates || _activeProcess != null) return;
213
214 _controller.SetControlsEnabled(false);
215 _activeProcess = new SendActivateSource(_controller.Lib, address);
216 _activeProcess.EventHandler += ProcessEventHandler;
217 (new Thread(_activeProcess.Run)).Start();
218 }
219
220 /// <summary>
221 /// Send a standby command to the device at the given logical address
222 /// </summary>
223 /// <param name="address">The logical address of the device to send to standby</param>
224 public void SendStandby(CecLogicalAddress address)
225 {
226 if (SuppressUpdates || _activeProcess != null) return;
227
228 _controller.SetControlsEnabled(false);
229 _activeProcess = new SendStandby(_controller.Lib, address);
230 _activeProcess.EventHandler += ProcessEventHandler;
231 (new Thread(_activeProcess.Run)).Start();
232 }
233
234 /// <summary>
235 /// Fetch device information and show an information dialog for the device at the given logical address
236 /// </summary>
237 /// <param name="address">The logical address of the device to get the info for</param>
238 public void ShowDeviceInfo(CecLogicalAddress address)
239 {
240 if (SuppressUpdates || _activeProcess != null) return;
241
242 _controller.SetControlsEnabled(false);
243 _activeProcess = new ShowDeviceInfo(_controller, _controller.Lib, address);
244 _activeProcess.EventHandler += ProcessEventHandler;
245 (new Thread(_activeProcess.Run)).Start();
246 }
247
248 /// <summary>
249 /// Poll devices to check which ones are active
250 /// </summary>
251 public void RescanDevices()
252 {
253 if (SuppressUpdates || _activeProcess != null) return;
254
255 _controller.SetControlsEnabled(false);
256 _activeProcess = new RescanDevices(_controller.Lib);
257 _activeProcess.EventHandler += ProcessEventHandler;
258 (new Thread(_activeProcess.Run)).Start();
259 }
260
261 /// <summary>
262 /// Update the physical address of libCEC that is displayed in the UI
263 /// </summary>
264 public void UpdatePhysicalAddress()
265 {
266 if (SuppressUpdates || _activeProcess != null) return;
267
268 _controller.SetControlsEnabled(false);
269 _activeProcess = new GetCurrentPhysicalAddress(_controller.Lib);
270 _activeProcess.EventHandler += ProcessEventHandler;
271 (new Thread(_activeProcess.Run)).Start();
272 }
273
274 /// <summary>
275 /// Connect to the adapter with the given configuration
276 /// </summary>
277 /// <param name="config">The client configuration</param>
278 public void ConnectToDevice(LibCECConfiguration config)
279 {
280 if (_activeProcess != null) return;
281
282 _activeProcess = new ConnectToDevice(_controller.Lib, config);
283 _activeProcess.EventHandler += ProcessEventHandler;
284 (new Thread(_activeProcess.Run)).Start();
285 }
286 #endregion
287
288 #region Members
289 private readonly CECController _controller;
290 private DeviceInformation _updatingInfoPanel;
291 public bool SuppressUpdates = true;
292 private UpdateProcess _activeProcess;
293 #endregion
294 }
295 }