Merge branch 'master' into release
[deb_libcec.git] / src / LibCecTray / controller / applications / KeyInput.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.Collections.Generic;
34 using System;
35 using System.Globalization;
36 using System.Text;
37
38 namespace LibCECTray.controller.applications
39 {
40 class KeyInput : ApplicationAction
41 {
42 public KeyInput(ApplicationController controller) :
43 base(controller, ActionType.SendKey)
44 {
45 }
46
47 public KeyInput(ApplicationController controller, WindowsAPI.VirtualKeyCode keyCode) :
48 base(controller, ActionType.SendKey)
49 {
50 AddKey(keyCode);
51 }
52
53 public static ApplicationAction FromString(ApplicationController controller, string value)
54 {
55 KeyInput retVal = new KeyInput(controller);
56 var split = value.Trim().Split(' ');
57 foreach (var item in split)
58 {
59 var param = retVal.GetParameterFromString(item);
60 ushort iValue;
61 if (ushort.TryParse(param, NumberStyles.AllowHexSpecifier, null, out iValue))
62 retVal.AddKey((WindowsAPI.VirtualKeyCode)iValue);
63 }
64 return retVal;
65 }
66
67 public int KeyCount
68 {
69 get
70 {
71 int count = 0;
72 foreach (var input in _input)
73 if (input.Data.Keyboard.Flags == 0)
74 count++;
75 return count;
76 }
77 }
78
79 public override string AsString()
80 {
81 StringBuilder sb = new StringBuilder();
82 foreach (var input in _input)
83 {
84 if (input.Data.Keyboard.Flags == 0)
85 sb.AppendFormat("{0}({1:X}) ", TypePrefix, input.Data.Keyboard.KeyCode);
86 }
87 return sb.ToString().Trim();
88 }
89
90 public override string AsFriendlyString()
91 {
92 StringBuilder sb = new StringBuilder();
93 bool bMultipleKeys = KeyCount > 1;
94 foreach (var input in _input)
95 {
96 if (input.Data.Keyboard.Flags == 0)
97 {
98 sb.AppendFormat(bMultipleKeys ? "[{0}] " : "{0} ",
99 WindowsAPI.GetVirtualKeyName((WindowsAPI.VirtualKeyCode) input.Data.Keyboard.KeyCode));
100 }
101 }
102 return sb.ToString().Trim();
103 }
104
105 public int Size()
106 {
107 return _input.Count;
108 }
109
110 public override bool Empty()
111 {
112 return _input.Count == 0;
113 }
114
115 public override bool CanAppend(ApplicationAction value)
116 {
117 return (value.ActionType == ActionType.SendKey);
118 }
119
120 public override ApplicationAction Append(ApplicationAction value)
121 {
122 if (value.ActionType == ActionType.SendKey)
123 AddKey(value as KeyInput);
124
125 return this;
126 }
127
128 public WindowsAPI.Input[] ToArray()
129 {
130 return _input.ToArray();
131 }
132
133 public void AddKey(WindowsAPI.VirtualKeyCode keyCode)
134 {
135 AddKeyDown(keyCode);
136 AddKeyUp(keyCode);
137 }
138
139 private void AddKeyDown(WindowsAPI.VirtualKeyCode keyCode)
140 {
141 var key = new WindowsAPI.Input {
142 Type = WindowsAPI.InputType.Keyboard,
143 Data = {
144 Keyboard = new WindowsAPI.KeyboardInput {
145 KeyCode = (UInt16) keyCode,
146 Scan = 0,
147 Flags = 0,
148 Time = 0,
149 ExtraInfo = IntPtr.Zero
150 }
151 }
152 };
153
154 _input.Add(key);
155 }
156
157 private void AddKeyUp(WindowsAPI.VirtualKeyCode keyCode)
158 {
159 var key = new WindowsAPI.Input {
160 Type = WindowsAPI.InputType.Keyboard,
161 Data = {
162 Keyboard = new WindowsAPI.KeyboardInput {
163 KeyCode = (UInt16) keyCode,
164 Scan = 0,
165 Flags = (uint)WindowsAPI.KeyEvent.KeyUp,
166 Time = 0,
167 ExtraInfo = IntPtr.Zero
168 }
169 }
170 };
171
172 _input.Add(key);
173 }
174
175 public KeyInput AddKey(KeyInput input)
176 {
177 if (input != null)
178 {
179 bool foundModifier = false;
180 if (KeyCount > 0)
181 {
182 for (int i = _input.Count - 1; i >= 0; i--)
183 {
184
185 if (_input[i].Data.Keyboard.Flags == 0 && (
186 (ushort)WindowsAPI.VirtualKeyCode.VK_SHIFT == _input[i].Data.Keyboard.KeyCode ||
187 (ushort)WindowsAPI.VirtualKeyCode.VK_LSHIFT == _input[i].Data.Keyboard.KeyCode ||
188 (ushort)WindowsAPI.VirtualKeyCode.VK_RSHIFT == _input[i].Data.Keyboard.KeyCode ||
189
190 (ushort)WindowsAPI.VirtualKeyCode.VK_CONTROL == _input[i].Data.Keyboard.KeyCode ||
191 (ushort)WindowsAPI.VirtualKeyCode.VK_LCONTROL == _input[i].Data.Keyboard.KeyCode ||
192 (ushort)WindowsAPI.VirtualKeyCode.VK_RCONTROL == _input[i].Data.Keyboard.KeyCode ||
193
194 (ushort)WindowsAPI.VirtualKeyCode.VK_MENU == _input[i].Data.Keyboard.KeyCode ||
195 (ushort)WindowsAPI.VirtualKeyCode.VK_LMENU == _input[i].Data.Keyboard.KeyCode ||
196 (ushort)WindowsAPI.VirtualKeyCode.VK_RMENU == _input[i].Data.Keyboard.KeyCode
197 ))
198 {
199 foundModifier = true;
200 _input.Insert(i+1, input._input[0]);
201 _input.Insert(i+2, input._input[1]);
202 break;
203 }
204 }
205 }
206
207 if (!foundModifier)
208 {
209 _input.Add(input._input[0]);
210 _input.Add(input._input[1]);
211 }
212 }
213 return this;
214 }
215
216 public override bool Transmit(IntPtr windowHandle)
217 {
218 var inputAr = ToArray();
219 if (inputAr.Length == 0)
220 return false;
221
222 try
223 {
224 return WindowsAPI.SendInputTo(windowHandle, (uint) inputAr.Length, inputAr,
225 System.Runtime.InteropServices.Marshal.SizeOf(typeof (WindowsAPI.Input)));
226 }
227 catch (Exception e)
228 {
229 Console.Error.WriteLine(e.Message);
230 return false;
231 }
232 }
233
234 public ApplicationAction RemoveItem(int index)
235 {
236 var current = 0;
237 var downKeyIndex = 0;
238 var upKeyIndex = 0;
239 bool downFound = false;
240
241 for (int i = 0; i < _input.Count; i++)
242 {
243 if (_input[i].Data.Keyboard.Flags == 0 && !downFound)
244 {
245 if (index == current)
246 {
247 downKeyIndex = i;
248 downFound = true;
249 }
250 current++;
251 }
252 else if(_input[i].Data.Keyboard.Flags != 0 && downFound)
253 {
254 if (_input[i].Data.Keyboard.KeyCode == _input[downKeyIndex].Data.Keyboard.KeyCode)
255 {
256 upKeyIndex = i;
257 break;
258 }
259 }
260 }
261
262 WindowsAPI.Input downKey = _input[downKeyIndex];
263 WindowsAPI.Input upKey = _input[upKeyIndex];
264
265 _input.Remove(downKey);
266 _input.Remove(upKey);
267
268 return this;
269 }
270
271 public override ApplicationAction RemoveKey(int index)
272 {
273 var current = 0;
274 var item = 0;
275 foreach (var input in _input)
276 {
277 if (input.Data.Keyboard.Flags == 0)
278 {
279 var tmp = string.Format(KeyCount > 1 ? "[{0}] " : "{0} ",
280 WindowsAPI.GetVirtualKeyName((WindowsAPI.VirtualKeyCode) input.Data.Keyboard.KeyCode));
281 current += tmp.Length;
282 if (index <= current)
283 return RemoveItem(item);
284 item++;
285 }
286 }
287
288 return this;
289 }
290
291 private readonly List<WindowsAPI.Input> _input = new List<WindowsAPI.Input>();
292 }
293 }