-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cs
More file actions
255 lines (232 loc) · 9.38 KB
/
Copy pathCommands.cs
File metadata and controls
255 lines (232 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//-------------------------------------------------------------------
// Copyright © 2012 Kindel Systems, LLC
// http://www.kindel.com
// charlie@kindel.com
//
// Published under the MIT License.
// Source control on SourceForge
// http://sourceforge.net/projects/mcecontroller/
//-------------------------------------------------------------------
//#define SERIALIZE
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using WindowsInput;
using WindowsInput.Native;
namespace MCEControl
{
// Base class for all Command types
public abstract class Command
{
[XmlAttribute("Cmd")]
public string Key;
public abstract void Execute(Reply reply);
public virtual IEnumerable<Command> AutoCommands() { yield break; }
}
// Note, do not change the namespace or your will break existing installations
[XmlType(Namespace = "http://www.kindel.com/products/mcecontroller", TypeName = "MCEController")]
public class CommandTable
{
[XmlIgnore]
private readonly Dictionary<string, Command> _hashTable = new Dictionary<string, Command>();
[XmlArray("Commands")]
[XmlArrayItem("StartProcess", typeof(StartProcessCommand))]
[XmlArrayItem("SendInput", typeof(SendInputCommand))]
[XmlArrayItem("SendMessage", typeof(SendMessageCommand))]
[XmlArrayItem("SetForegroundWindow", typeof(SetForegroundWindowCommand))]
[XmlArrayItem("Shutdown", typeof(ShutdownCommand))]
[XmlArrayItem(typeof(Command))]
public Command[] List;
public int CommandCount
{
get { return _hashTable.Count; }
}
public void Execute(Reply reply, string cmd)
{
if (!MainWindow.MainWnd.Settings.DisableInternalCommands)
{
if (cmd.StartsWith(McecCommand.CmdPrefix))
{
var command = new McecCommand(cmd);
command.Execute(reply);
return;
}
if (cmd.StartsWith("chars:"))
{
// "chars:<chars>
string chars = Regex.Unescape(cmd.Substring(6, cmd.Length - 6));
MainWindow.AddLogEntry(string.Format("Cmd: Sending {0} chars: {1}", chars.Length, chars));
var sim = new InputSimulator();
sim.Keyboard.TextEntry(chars);
return;
}
if (cmd.StartsWith("api:"))
{
// "api:API(params)
// TODO: Implement API stuff
return;
}
if (cmd.StartsWith("shiftdown:"))
{
// Modifyer key down
SendInputCommand.ShiftKey(cmd.Substring(10, cmd.Length - 10), true);
return;
}
if (cmd.StartsWith("shiftup:"))
{
// Modifyer key up
SendInputCommand.ShiftKey(cmd.Substring(8, cmd.Length - 8), false);
return;
}
if (cmd.StartsWith(MouseCommand.CmdPrefix))
{
// mouse:<action>[,<parameter>,<parameter>]
var mouseCmd = new MouseCommand(cmd);
mouseCmd.Execute(reply);
return;
}
if (cmd.StartsWith("stopall:"))
{
StopAll(reply);
return;
}
if (cmd.Length == 1)
{
// It's a single character, just send it
// must be upper case (VirtualKeyCode codes are for upper case)
cmd = cmd.ToUpper();
char c = cmd.ToCharArray()[0];
var sim = new InputSimulator();
MainWindow.AddLogEntry("Cmd: Sending keydown for: " + cmd);
sim.Keyboard.KeyPress((VirtualKeyCode)c);
return;
}
}
// Command is in MCEControl.commands
{
Command command;
if (_hashTable.TryGetValue(cmd.ToUpper(), out command))
{
command.Execute(reply);
}
else
{
MainWindow.AddLogEntry($"Unknown command: \"{cmd}\"");
}
}
}
private void StopAll(Reply reply)
{
foreach (var stopCommand in _hashTable.Values.OfType<StopProcessCommand>())
{
stopCommand.Execute(reply);
}
}
private void AddCommand(Command command)
{
var key = command.Key.ToUpper();
//MainWindow.AddLogEntry($"Adding command \"{key}\": {command.GetType().Name}");
_hashTable[key] = command;
foreach (var autoCommand in command.AutoCommands())
{
AddCommand(autoCommand);
}
}
public static CommandTable Deserialize(bool DisableInternalCommands)
{
CommandTable cmds = null;
CommandTable userCmds = null;
if (!DisableInternalCommands)
{
// Load the built-in commands from an assembly resource
try
{
var serializer = new XmlSerializer(typeof(CommandTable));
XmlReader reader =
new XmlTextReader(
Assembly.GetExecutingAssembly()
.GetManifestResourceStream("MCEControl.Resources.MCEControl.commands"));
cmds = (CommandTable)serializer.Deserialize(reader);
foreach (var cmd in cmds.List)
{
cmds.AddCommand(cmd);
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format("No commands loaded. Error parsing built-in commands. {0}",
ex.Message));
MainWindow.AddLogEntry(
string.Format("MCEC: No commands loaded. Error parsing built-in commands. {0}",
ex.Message));
Util.DumpException(ex);
return null;
}
// Populate default VK_ codes
foreach (VirtualKeyCode vk in Enum.GetValues(typeof(VirtualKeyCode)))
{
string s;
if (vk > VirtualKeyCode.HELP && vk < VirtualKeyCode.LWIN)
s = vk.ToString(); // already have VK_
else
s = "VK_" + vk.ToString();
var cmd = new SendInputCommand(s, false, false, false, false);
if (!cmds._hashTable.ContainsKey(s))
cmds._hashTable.Add(s, cmd);
}
}
else
{
cmds = new CommandTable();
}
// Load any over-rides from a text file
FileStream fs = null;
try
{
var serializer = new XmlSerializer(typeof(CommandTable));
// A FileStream is needed to read the XML document.
fs = new FileStream("MCEControl.commands", FileMode.Open, FileAccess.Read);
XmlReader reader = new XmlTextReader(fs);
userCmds = (CommandTable)serializer.Deserialize(reader);
foreach (var cmd in userCmds.List)
{
cmds.AddCommand(cmd);
}
MainWindow.AddLogEntry(string.Format("MCEC: User defined commands loaded."));
}
catch (FileNotFoundException ex)
{
MainWindow.AddLogEntry("MCEC: No user defined commands loaded; MCEControl.commands was not found.");
Util.DumpException(ex);
}
catch (InvalidOperationException ex)
{
MainWindow.AddLogEntry(
string.Format("MCEC: No commands loaded. Error parsing MCEControl.commands file. {0} {1}", ex.Message,
ex.InnerException.Message));
Util.DumpException(ex);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("No commands loaded. Error parsing MCEControl.commands file. {0}",
ex.Message));
MainWindow.AddLogEntry(string.Format("MCEC: No commands loaded. Error parsing MCEControl.commands file. {0}",
ex.Message));
Util.DumpException(ex);
}
finally
{
if (fs != null)
fs.Close();
}
return cmds;
}
}
}