-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMain.cs
228 lines (187 loc) · 7.66 KB
/
FormMain.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
namespace Capybara
{
public partial class FormMain : Form
{
//TODO:
//- Recording interpolation between non-zero flag events
//- Option to ignore movement-only events, and reset mouse position after event
//- Potentially allow direct editing of a record
/// <summary>
/// Single thread used to replay a recording
/// </summary>
protected Thread _worker;
/// <summary>
/// Global hotkey ID
/// </summary>
protected int _id;
/// <summary>
/// List of recorded events
/// </summary>
protected volatile LinkedList<EventInformation> _record;
/// <summary>
/// Notifies the worker thread if it should stop, instead of using Thread.Abort
/// </summary>
protected volatile bool _running;
/// <summary>
/// The number of mouse events to record per second
/// </summary>
public const int EventsPerSecond = 60;
public FormMain()
{
InitializeComponent();
//For some reason VS complains about this being present in the Designer.cs
this.trackBarReplaySpeed.Scroll += (o, e) =>
{
toolTipReplaySpeed.SetToolTip(this.trackBarReplaySpeed, System.String.Format("{0:0.0000}x", System.Math.Pow(2, trackBarReplaySpeed.Value / 10f)));
};
//Keep this form on the top
this.TopMost = true;
//Attempt to register Pause as a hotkey for our application
//Fails if another process has registered Pause as a hotkey
if ((_id = Enumerable.Range(1, 100).FirstOrDefault(v => Windows.RegisterHotKey(this.Handle, v, 0, (int)Keys.Pause))) == 0)
{
MessageBox.Show("Failed to register Pause as the interrupt hotkey", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//TODO: Add hotkeys for replaying and recording
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Unregister the presumably set hotkey
Windows.UnregisterHotKey(this.Handle, _id);
StopWorkerThread();
}
private void buttonRecord_Click(object sender, EventArgs e)
{
this.Text = "Capybara - Recording";
StopWorkerThread();
//Clear out the record list
if (_record == null)
_record = new LinkedList<EventInformation>();
else
_record.Clear();
//Recreate the worker thread
_worker = new Thread(() =>
{
MouseButtons oldState = default(MouseButtons), newState = default(MouseButtons);
while(_running)
{
newState = Control.MouseButtons;
_record.AddLast(new EventInformation(Cursor.Position,
(newState.HasFlag(MouseButtons.Left) && !oldState.HasFlag(MouseButtons.Left)? Windows.LEFT_DOWN : 0) |
(!newState.HasFlag(MouseButtons.Left) && oldState.HasFlag(MouseButtons.Left) ? Windows.LEFT_UP : 0) |
(newState.HasFlag(MouseButtons.Right) && !oldState.HasFlag(MouseButtons.Right) ? Windows.RIGHT_DOWN : 0) |
(!newState.HasFlag(MouseButtons.Right) && oldState.HasFlag(MouseButtons.Right) ? Windows.RIGHT_UP : 0)
));
oldState = newState;
Thread.Sleep(1000 / EventsPerSecond);
}
//TODO: Change this so we only add a 0x4 if there's an unterminated 0x2,
// and same for 0x10 and 0x8
_record.AddLast(new EventInformation(Cursor.Position, 0x6 | 0x18));
});
_running = true;
//Disable everything except buttonStop
foreach(Control c in this.Controls)
if(c != buttonStop)
c.Enabled = false;
_worker.Start();
}
private void buttonStop_Click(object sender, EventArgs e)
{
this.Text = "Capybara";
StopWorkerThread();
//Re-enable all disabled controls
foreach (Button b in this.Controls.OfType<Button>())
b.Enabled = true;
}
private void buttonPlay_Click(object sender, EventArgs e)
{
StopWorkerThread();
//Stop if there's nothing to play
if (_record == null || _record.Count == 0)
return;
this.Text = "Capybara - Playing";
bool _repeat = checkBoxRepeat.Checked;
//We expect trackBarReplaySpeed to be a LOGARITHMIC scale, so we need to transform the value
double _replayRate = Math.Pow(2, trackBarReplaySpeed.Value / 10f);
//Recreate the worker thread
_worker = new Thread(() =>
{
do
{
foreach (var entry in _record)
{
//Early termination
if (!_running)
break;
Cursor.Position = entry.Position;
Windows.SendClick(entry.Flag);
Thread.Sleep((int)(1000 / (EventsPerSecond * _replayRate)));
}
} while (_repeat);
});
_running = true;
//Disable everything except buttonStop
foreach (Control c in this.Controls)
if (c != buttonStop)
c.Enabled = false;
_worker.Start();
}
protected override void WndProc(ref Message m)
{
//Bailout
//WM_HOTKEY = 0x0312
if (m.Msg == 0x0312 && m.WParam.ToInt32() == _id)
{
//Should act the same as pressing the Stop button if something is running
if (IsRunning())
buttonStop.PerformClick();
//Otherwise, begin running
else
buttonPlay.PerformClick();
}
base.WndProc(ref m);
}
protected void StopWorkerThread()
{
//Notify the worker thread to stop running
if (IsRunning())
{
//Signal the thread to stop
_running = false;
//Give the worker thread 50ms to finish
if (!_worker.Join(50))
{
//Otherwise, forcibly abort it
_worker.Abort();
}
}
}
protected bool IsRunning()
{
return _worker != null && _worker.IsAlive;
}
}
public sealed class EventInformation
{
/// <summary>
/// Position at which the event was recorded
/// </summary>
public readonly Point Position;
/// <summary>
/// The dwFlags value passed to SendInput
/// </summary>
public readonly uint Flag;
public EventInformation(Point pos, uint flag)
{
Position = pos;
Flag = flag;
}
}
}