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.Collections.Generic;
36 using System.Windows.Forms;
38 using LibCECTray.Properties;
40 namespace LibCECTray.controller.applications
43 /// The type of the action that is executed
48 CloseControllerApplication = 1,
54 /// Class that contains one or more actions that can be executed or sent to the application
56 abstract class ApplicationAction
61 /// <param name="controller">The controller instance of the application that this action is targeting</param>
62 /// <param name="type">The type of the action that is executed</param>
63 protected ApplicationAction(ApplicationController controller, ActionType type)
65 Controller = controller;
70 /// The type of the action that is executed
72 public ActionType ActionType { private set; get; }
75 /// Execute the action
77 /// <param name="windowHandle">The window of the application that is targeted</param>
78 /// <returns>True when executed, false otherwise</returns>
79 public abstract bool Transmit(IntPtr windowHandle);
82 /// Serialisable string representation of this action
84 /// <returns>The requested string</returns>
85 public abstract string AsString();
88 /// String representation of this action in human readable form
90 /// <returns>The requested string</returns>
91 public abstract string AsFriendlyString();
94 /// Check whether this action is empty (not doing anything)
96 /// <returns>True when empty, false otherwise</returns>
97 public abstract bool Empty();
100 /// Checks whether the given action can be appended to this one.
102 /// <param name="value">The action to check</param>
103 /// <returns>True when it can be appended, false otherwise</returns>
104 public abstract bool CanAppend(ApplicationAction value);
107 /// Append the given action to this action, and return the combined result
109 /// <param name="value">The action to append to this one</param>
110 /// <returns>The combined result</returns>
111 public abstract ApplicationAction Append(ApplicationAction value);
114 /// Remove item at the given index from this action
116 /// <param name="index">The index of the item to remove</param>
117 /// <returns>True when removed, false otherwise</returns>
118 public abstract ApplicationAction RemoveKey(int index);
121 /// The prefix to use for serialisation for this type
123 protected string TypePrefix
125 get { return Enum.GetName(typeof(ActionType), ActionType); }
129 /// Get the parameter value from a string representation of an action of this type
131 /// <param name="value">The value to get the parameter from</param>
132 /// <returns>The parameter</returns>
133 protected string GetParameterFromString(string value)
135 var trimmedItem = value.Trim();
136 if (!trimmedItem.StartsWith(TypePrefix + "(") || !trimmedItem.EndsWith(")"))
139 return trimmedItem.Substring(TypePrefix.Length + 1, trimmedItem.Length - TypePrefix.Length - 2);
142 protected readonly ApplicationController Controller;
146 /// Closes LibCecTray
148 internal class ApplicationActionCloseController : ApplicationAction
150 public ApplicationActionCloseController(ApplicationController controller)
151 : base(controller, ActionType.CloseControllerApplication)
155 public override bool Transmit(IntPtr windowHandle)
161 public override string AsString()
166 public override string AsFriendlyString()
168 return string.Format("[{0}]", Resources.action_type_close_controller_application);
171 public static bool HasDefaultValue(CecKeypress key)
173 return DefaultValue(key) != null;
176 public static ApplicationAction DefaultValue(CecKeypress key)
180 case CecUserControlCode.Power:
181 case CecUserControlCode.PowerOnFunction:
182 case CecUserControlCode.PowerOffFunction:
183 case CecUserControlCode.PowerToggleFunction:
184 return new ApplicationActionCloseController(null);
189 public override bool Empty()
194 public override bool CanAppend(ApplicationAction value)
199 public override ApplicationAction Append(ApplicationAction value)
204 public override ApplicationAction RemoveKey(int index)
209 public static ApplicationAction FromString(ApplicationController controller, string value)
211 ApplicationActionCloseController retVal = new ApplicationActionCloseController(controller);
212 return value.Trim().Equals(retVal.AsString()) ? retVal : null;
217 /// Starts an application
219 internal class ApplicationActionStart : ApplicationAction
221 public ApplicationActionStart(ApplicationController controller) :
222 base(controller, ActionType.StartApplication)
226 public override bool Transmit(IntPtr windowHandle)
228 return Controller.Start(false);
231 public override string AsString()
236 public override string AsFriendlyString()
238 return string.Format("[{0}]", Resources.action_type_start_application);
241 public override bool Empty()
246 public override bool CanAppend(ApplicationAction value)
251 public override ApplicationAction Append(ApplicationAction value)
256 public override ApplicationAction RemoveKey(int index)
261 public static ApplicationAction FromString(ApplicationController controller, string value)
263 ApplicationActionStart retVal = new ApplicationActionStart(controller);
264 return value.Trim().Equals(retVal.AsString()) ? retVal : null;
267 public static bool HasDefaultValue(CecKeypress key)
269 return DefaultValue(key) != null;
272 public static ApplicationAction DefaultValue(CecKeypress key)
279 /// Sends one or more actions to an application
281 internal class ApplicationInput : ApplicationAction
283 public ApplicationInput(ApplicationController controller) :
284 base(controller, ActionType.Generic)
288 public static string FriendlyActionName(ActionType type)
292 case ActionType.Generic:
293 return Resources.action_type_generic;
294 case ActionType.CloseControllerApplication:
295 return Resources.action_type_close_controller_application;
296 case ActionType.StartApplication:
297 return Resources.action_type_start_application;
298 case ActionType.SendKey:
299 return Resources.action_type_sendkey;
301 return type.ToString();
305 public static ApplicationAction DefaultValue(ApplicationController controller, CecKeypress key)
307 return controller.HasDefaultValue(key) ? controller.DefaultValue(key) : new ApplicationInput(null);
310 public override bool Empty()
312 foreach (var item in _input)
318 public override bool CanAppend(ApplicationAction value)
323 public override ApplicationAction Append(ApplicationAction value)
329 if (_input.Count > 0)
331 if (_input[_input.Count - 1].CanAppend(value))
333 _input[_input.Count - 1].Append(value);
343 public override bool Transmit(IntPtr windowHandle)
346 foreach (var input in _input)
348 retval &= input.Transmit(windowHandle);
353 public override string AsString()
355 StringBuilder sb = new StringBuilder();
356 foreach (var input in _input)
358 sb.AppendFormat("{0} ", input.AsString());
360 return sb.ToString().TrimEnd();
363 public override string AsFriendlyString()
365 StringBuilder sb = new StringBuilder();
366 foreach (var input in _input)
367 sb.AppendFormat("{0} ", input.AsFriendlyString());
368 return sb.ToString().Trim();
371 public override ApplicationAction RemoveKey(int index)
374 for (var itemPtr = 0; itemPtr < _input.Count; itemPtr++)
376 var item = _input[itemPtr];
377 var currentPtr = item.AsFriendlyString().Length;
378 if (index <= ptr + currentPtr)
380 var newItem = item.RemoveKey(index - ptr);
381 if (newItem == null || newItem.Empty())
384 _input[itemPtr] = newItem;
392 public ApplicationInput RemoveItem(int index)
394 return RemoveKey(index) as ApplicationInput;
397 public static ApplicationInput FromString(ApplicationController controller, string value)
399 ApplicationInput retVal = new ApplicationInput(controller);
400 var split = value.Trim().Split(' ');
401 foreach (var item in split)
403 var addAction = KeyInput.FromString(controller, item);
405 if (addAction == null || addAction.Empty())
406 addAction = ApplicationActionCloseController.FromString(controller, item);
408 if (addAction == null || addAction.Empty())
409 addAction = ApplicationActionStart.FromString(controller, item);
411 if (addAction != null && !addAction.Empty())
412 retVal.Append(addAction);
417 public ApplicationInput AddKey(WindowsAPI.VirtualKeyCode keyCode)
419 var key = new KeyInput(Controller, keyCode);
425 public ApplicationInput AddAction(ActionType action)
429 case ActionType.CloseControllerApplication:
430 Append(new ApplicationActionCloseController(Controller));
432 case ActionType.StartApplication:
433 Append(new ApplicationActionStart(Controller));
439 private readonly List<ApplicationAction> _input = new List<ApplicationAction>();