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.Diagnostics;
37 using System.Windows.Forms;
39 using LibCECTray.Properties;
40 using LibCECTray.settings;
41 using Timer = System.Timers.Timer;
43 namespace LibCECTray.controller.applications
45 public delegate void OnApplicationRunningChanged(bool running);
48 /// Controls an application on the PC: send key presses, open the application, close it, etc.
50 class ApplicationController
52 public ApplicationController(CECSettings settings, string uiName, string processName, string filename, string workingDirectory)
56 ProcessName = processName;
57 ApplicationFilename = filename;
58 ApplicationWorkingDirectory = workingDirectory;
59 SuppressApplicationStart = false;
63 public static ApplicationController FromString(CECSettings settings, string serialisedConfig)
65 var splitString = serialisedConfig.Split(';');
66 if (splitString.Length != 4)
67 throw new InvalidDataException("incorrect number of parameters");
69 return new ApplicationController(settings, splitString[0], splitString[1], splitString[2], splitString[3]);
72 public string AsString()
74 return string.Format("{0};{1};{2};{3}", UiName, ProcessName, ApplicationFilename, ApplicationWorkingDirectory);
77 public void BindButtonConfiguration(DataGridView gridView, BindingSource bindingSource)
79 CecButtonGridView = gridView;
81 DataGridViewCell buttonCellTemplate = new DataGridViewTextBoxCell();
82 CecButtonGridView.Columns.Add(new DataGridViewColumn(buttonCellTemplate)
84 DataPropertyName = "CecButtonName",
85 Name = Resources.config_cec_button,
90 DataGridViewButtonCell mappedToCellTemplate = new DataGridViewButtonCell();
91 CecButtonGridView.Columns.Add(new DataGridViewColumn(mappedToCellTemplate)
93 DataPropertyName = "MappedButtonName",
94 Name = Resources.config_button_mapped_to,
99 bindingSource.DataSource = ButtonConfig;
100 CecButtonGridView.DataSource = bindingSource;
102 gridView.CellFormatting += delegate(object sender, DataGridViewCellFormattingEventArgs args)
104 DataGridView grid = sender as DataGridView;
105 var data = grid != null ? grid.Rows[args.RowIndex].DataBoundItem as CecButtonConfigItem : null;
106 if (data == null || !data.Enabled)
108 args.CellStyle.ForeColor = Color.Gray;
112 gridView.CellClick += delegate(object sender, DataGridViewCellEventArgs args)
114 var item = args.RowIndex < ButtonConfig.Count ? ButtonConfig[args.RowIndex] : null;
117 if (args.ColumnIndex >= 0)
119 (new CecButtonConfigUI(item)).ShowDialog();
123 var mappedButton = ButtonConfig[item.Key];
124 if (mappedButton == null || mappedButton.Value.Empty())
127 var controlWindow = FindInstance();
128 if (controlWindow != IntPtr.Zero && item.Key.Duration == 0)
129 mappedButton.Value.Transmit(controlWindow);
133 foreach (var item in _buttonConfig)
135 item.SettingChanged += delegate
142 #region Start and stop the application
144 /// Check if the application is running
146 /// <returns>True when running, false otherwise</returns>
147 public virtual bool IsRunning()
149 return FindInstance() != IntPtr.Zero;
153 /// Start the application if it's not running already, and suppress further starts for 5 seconds
155 /// <returns>True when started or suppressed, false otherwise</returns>
156 public virtual bool Start(bool bExitAfterStarting)
164 if (SuppressApplicationStart)
167 SuppressApplicationStart = true;
168 Timer timer = new Timer {Interval = 5000, AutoReset = false};
169 timer.Elapsed += delegate { SuppressApplicationStart = false; };
175 Process runningProcess = new Process
179 WorkingDirectory = ApplicationWorkingDirectory,
180 FileName = ApplicationFilename
184 // start maximised if the option is enabled
185 if (StartFullScreen.Value)
186 runningProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
188 runningProcess.Start();
196 if (bExitAfterStarting)
203 /// Initialise the controller and autostart the application
205 public virtual void Initialise()
207 Timer timer = new Timer { Interval = 1000, AutoReset = true };
208 timer.Elapsed += delegate { CheckApplicationEnabled(); };
211 if (AutoStartApplication.Value)
215 public event OnApplicationRunningChanged ApplicationRunningChanged;
217 private void CheckApplicationEnabled()
219 var isRunning = IsRunning();
220 if (isRunning != _applicationRunning && ApplicationRunningChanged != null)
221 ApplicationRunningChanged(isRunning);
223 _applicationRunning = isRunning;
224 UiControl.SetStartButtonEnabled(!isRunning && !SuppressApplicationStart);
228 #region Send input to the application
230 /// Send a keypress to the application if it's running
232 /// <param name="key">The keypress to send</param>
233 /// <param name="isSelectedTab">True when this tab is currently selected in the UI</param>
234 /// <returns>True when sent, false otherwise</returns>
235 public virtual bool SendKey(CecKeypress key, bool isSelectedTab)
238 UiControl.SelectKeypressRow(UiControl, CecButtonGridView, key);
240 if (isSelectedTab && SuppressKeypressWhenSelected.Value)
243 if (!ControlApplication.Value)
246 var mappedButton = ButtonConfig[key];
247 if (mappedButton == null || mappedButton.Value.Empty())
250 var controlWindow = FindInstance();
251 if (controlWindow != IntPtr.Zero && (key.Duration == 0 || key.Duration > 500))
252 return mappedButton.Value.Transmit(controlWindow);
258 #region Process control
260 /// Make this application the foreground application if it's running
262 public virtual void SetForeground()
264 var wmcInstance = FindInstance();
265 if (wmcInstance != IntPtr.Zero)
266 WindowsAPI.SetForegroundWindow(wmcInstance);
270 /// The main window handle of the application if it's running.
272 /// <returns>The main window handle, or IntPtr.Zero if it's not found</returns>
273 protected virtual IntPtr FindInstance()
275 var processes = Process.GetProcessesByName(ProcessName);
276 return processes.Length > 0 ? processes[0].MainWindowHandle : IntPtr.Zero;
282 /// The name of the process in the process manager
284 public string ProcessName { set; get; }
287 /// The filename of the application
289 public string ApplicationFilename { set; get; }
292 /// The working directory of the application
294 public string ApplicationWorkingDirectory { set; get; }
297 /// Don't start the application while true
299 public bool SuppressApplicationStart { get; private set; }
302 /// The name of the application how it shows up in this application
304 public string UiName { set; get; }
307 /// True when this application should be autostarted when this application is activated, or made the active source
309 public CECSettingBool AutoStartApplication
313 if (!Settings.ContainsKey(ProcessName + "_autostart"))
315 CECSettingBool setting = new CECSettingBool(ProcessName + "_autostart", "Autostart application", false, null);
316 Settings.Load(setting);
317 Settings[ProcessName + "_autostart"] = setting;
319 return Settings[ProcessName + "_autostart"].AsSettingBool;
324 /// True when keypresses should be routed to this application
326 public CECSettingBool ControlApplication
330 if (!Settings.ContainsKey(ProcessName + "_control"))
332 CECSettingBool setting = new CECSettingBool(ProcessName + "_control", "Control application", true, null);
333 Settings.Load(setting);
334 Settings[ProcessName + "_control"] = setting;
336 return Settings[ProcessName + "_control"].AsSettingBool;
341 /// True when this application should be autostarted when this application is activated, or made the active source
343 public CECSettingBool SuppressKeypressWhenSelected
347 if (!Settings.ContainsKey(ProcessName + "_suppress_when_selected"))
349 CECSettingBool setting = new CECSettingBool(ProcessName + "_suppress_when_selected", "Suppress keypress when this tab is selected", true, null);
350 Settings.Load(setting);
351 Settings[ProcessName + "_suppress_when_selected"] = setting;
353 return Settings[ProcessName + "_suppress_when_selected"].AsSettingBool;
358 /// True when the application should be started in full screen mode
360 public CECSettingBool StartFullScreen
364 if (!Settings.ContainsKey(ProcessName + "_start_fullscreen"))
366 CECSettingBool setting = new CECSettingBool(ProcessName + "_start_fullscreen", "Start in full screen mode", true, null);
367 Settings.Load(setting);
368 Settings[ProcessName + "_start_fullscreen"] = setting;
370 return Settings[ProcessName + "_start_fullscreen"].AsSettingBool;
374 protected ControllerTabPage UIControlInternal;
375 public virtual ControllerTabPage UiControl
377 get { return UIControlInternal ?? (UIControlInternal = new ApplicationControllerUI(this)); }
380 private CecButtonConfig _buttonConfig;
381 public CecButtonConfig ButtonConfig
383 get { return _buttonConfig ?? (_buttonConfig = new CecButtonConfig(this)); }
386 public CECSettings Settings;
387 protected DataGridView CecButtonGridView;
389 public virtual ApplicationAction DefaultValue(CecKeypress key)
394 public virtual bool HasDefaultValue(CecKeypress key)
396 return DefaultValue(key) != null;
399 public bool IsInternal { protected set; get; }
400 public bool CanConfigureProcess
408 private bool _applicationRunning;