forked from EarthwormO/ew-ergodex-dx1-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MacroPlayer.cs
321 lines (271 loc) · 9.8 KB
/
MacroPlayer.cs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace DX1Utility
{
public class MacroPlayer
{
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
const uint KEYEVENTF_KEYUP = 0x0002;
const uint KEYEVENTF_UNICODE = 0x0004;
const uint KEYEVENTF_SCANCODE = 0x0008;
const uint XBUTTON1 = 0x0001;
const uint XBUTTON2 = 0x0002;
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
List<Macro> mMacros = new List<Macro>();
public void Add(Macro macro)
{
mMacros.Add(macro);
}
public const UInt64 kForever = 0xffffffffffffffff;
public UInt64 Tick(UInt64 curTime)
{
UInt64 minTime = kForever;
foreach(Macro macro in mMacros)
{
UInt64 next = macro.ProcessMacroToTime(curTime);
if (next < minTime)
minTime = next;
}
return minTime;
}
[Serializable]
public class MacroDefinition
{
public enum MacroType
{
// kMacroTimed =0,
kMacroMultiKey = 0x1,
kMacroUseScanCodes = 0x02
}
static int VERSION = 1;
public String name = "";
public MacroType macroType = 0 | MacroType.kMacroUseScanCodes;
public KeySequenceEntry[] sequence;
public MacroDefinition(String n, KeySequenceEntry[] keys, MacroType type)
{
name = n;
sequence = keys;
macroType = type;
}
public MacroDefinition(KeySequenceEntry[] keys)
{
sequence = keys;
}
public static MacroDefinition Read(System.IO.Stream stream)
{
IFormatter formatter = new BinaryFormatter();
int version = (Int32)formatter.Deserialize(stream);
MacroDefinition macro = (MacroDefinition)formatter.Deserialize(stream);
return macro;
}
public static void Write(System.IO.Stream stream, MacroDefinition macro)
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, VERSION);
formatter.Serialize(stream, macro);
}
private List<INPUT> CreateKeyDownInputList()
{
List<INPUT> keyActions = new List<INPUT>();
for (int i = 0; i < sequence.Length; i++)
{
KeySequenceEntry entry = sequence[i];
INPUT temp = new INPUT();
temp.type = INPUT_KEYBOARD;
if ((macroType & MacroDefinition.MacroType.kMacroUseScanCodes) != 0)
{
temp.ki.wScan = entry.keyCode;
temp.ki.dwFlags = KEYEVENTF_SCANCODE;
}
else
{
temp.ki.wVk = entry.keyCode;
temp.ki.dwFlags = 0;
}
keyActions.Add(temp);
}
return keyActions;
}
// Invert order for Key up
private List<INPUT> CreateKeyUpInputList()
{
List<INPUT> keyActions = new List<INPUT>();
for (int i = sequence.Length-1; i >= 0 ; --i)
{
KeySequenceEntry entry = sequence[i];
INPUT temp = new INPUT();
temp.type = INPUT_KEYBOARD;
if ((macroType & MacroDefinition.MacroType.kMacroUseScanCodes) != 0)
{
// Note scan codes send a different value for key up
temp.ki.wScan = (ushort) entry.keyCode;
temp.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
}
else
{
temp.ki.wVk = entry.keyCode;
temp.ki.dwFlags = KEYEVENTF_KEYUP;
}
keyActions.Add(temp);
}
return keyActions;
}
private void AllMacroKeys(bool KeyUp)
{
INPUT[] keyArray = (KeyUp ? CreateKeyUpInputList() : CreateKeyDownInputList()).ToArray();
if (keyArray.Length > 0)
{
int size = Marshal.SizeOf(keyArray[0]);
SendInput((uint)keyArray.Length, keyArray, size);
}
}
public void AllMacroKeysDown()
{
AllMacroKeys(false);
}
public void AllMacroKeysUp()
{
AllMacroKeys(true);
}
}
[Serializable]
public class KeySequenceEntry
{
public UInt32 time;
public bool keyUp;
public byte keyCode;
public int keyType = 0;
public KeySequenceEntry(UInt32 t, bool up, byte key, int type)
{
time = t;
keyUp = up;
keyCode = key;
keyType = type;
}
}
public class Macro
{
UInt64 baseTime;
MacroDefinition macroKeys;
int IP = 0;
public Macro(MacroDefinition keys)
{
macroKeys = keys;
}
public UInt64 ProcessMacroToTime(UInt64 time)
{
List<INPUT> keyCommands = new List<INPUT>();
for (; IP < macroKeys.sequence.Length; IP++)
{
KeySequenceEntry entry = macroKeys.sequence[IP];
if (baseTime + entry.time > time)
break;
INPUT temp = new INPUT();
if(entry.keyType != 2)
{
//Standard keyboard playback
temp.type = INPUT_KEYBOARD;
if (((macroKeys.macroType & MacroDefinition.MacroType.kMacroUseScanCodes) != 0))
{
temp.ki.wScan = (ushort)entry.keyCode;
temp.ki.dwFlags = KEYEVENTF_SCANCODE | (entry.keyUp ? KEYEVENTF_KEYUP : 0);
}
else
{
temp.ki.wVk = entry.keyCode;
temp.ki.dwFlags = entry.keyUp ? KEYEVENTF_KEYUP : 0;
}
}
else
{
//New Mouse playback
temp.type = INPUT_MOUSE;
temp.mi.dx = 0;
temp.mi.dy = 0;
temp.mi.mouseData = 0;
temp.mi.dwFlags = (uint)entry.keyCode;
}
keyCommands.Add(temp);
}
INPUT[] keyArray = keyCommands.ToArray();
if (keyArray.Length > 0)
{
int size = Marshal.SizeOf(keyArray[0]);
SendInput((uint)keyArray.Length, keyArray, size);
}
return IP < macroKeys.sequence.Length ? macroKeys.sequence[IP].time + baseTime : kForever;
}
// Returns time of next Event or -1 if done
public UInt64 InitMacroAtTime(UInt64 time)
{
if ((macroKeys.macroType & MacroDefinition.MacroType.kMacroMultiKey) == 0)
{
baseTime = time;
return ProcessMacroToTime(time);
}
return kForever;
}
}
[StructLayout(LayoutKind.Sequential)]
private struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
[StructLayout(LayoutKind.Explicit)]
private struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(8)] //*
public MOUSEINPUT mi;
[FieldOffset(8)] //*
public KEYBDINPUT ki;
[FieldOffset(8)] //*
HARDWAREINPUT hi;
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs,
INPUT[] pInputs,
int cbSize);
}
}