Inertia – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.ComponentModel; |
||
2 | office | 3 | using System.Configuration; |
1 | office | 4 | using System.Diagnostics; |
5 | using System.Threading; |
||
6 | using System.Threading.Tasks; |
||
7 | using System.Windows.Forms; |
||
8 | using WindowsInput; |
||
9 | using AutoUpdaterDotNET; |
||
10 | using Gma.System.MouseKeyHook; |
||
2 | office | 11 | using Inertia.Properties; |
12 | using Inertia.Utilities; |
||
1 | office | 13 | |
14 | namespace Inertia |
||
15 | { |
||
16 | public partial class Form1 : Form |
||
17 | { |
||
18 | #region Static Fields and Constants |
||
19 | |||
20 | private static IKeyboardMouseEvents _globalMouseKeyHook; |
||
21 | |||
22 | private static InputSimulator _inputSimulator; |
||
23 | |||
24 | private static ScheduledContinuation _scrollUpContinuation; |
||
25 | |||
26 | private static long _upDelta; |
||
27 | |||
28 | private static long _downDelta; |
||
29 | |||
30 | private static ScheduledContinuation _scrollDownContinuation; |
||
31 | |||
32 | private static CancellationTokenSource _scrollCancellationTokenSource; |
||
33 | |||
34 | #endregion |
||
35 | |||
36 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
37 | |||
38 | private AboutForm _aboutForm; |
||
39 | |||
40 | #endregion |
||
41 | |||
42 | #region Constructors, Destructors and Finalizers |
||
43 | |||
44 | public Form1() |
||
45 | { |
||
46 | InitializeComponent(); |
||
47 | AutoUpdater.Start("http://inertia.grimore.org/update/update.xml"); |
||
48 | |||
2 | office | 49 | // Upgrade settings if required. |
50 | if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile) |
||
51 | { |
||
52 | Settings.Default.Upgrade(); |
||
53 | } |
||
54 | |||
55 | // Bind to settings changed event. |
||
56 | Settings.Default.SettingsLoaded += DefaultOnSettingsLoaded; |
||
57 | Settings.Default.SettingsSaving += DefaultOnSettingsSaving; |
||
58 | Settings.Default.PropertyChanged += DefaultOnPropertyChanged; |
||
59 | |||
1 | office | 60 | _inputSimulator = new InputSimulator(); |
61 | |||
62 | _globalMouseKeyHook = Hook.GlobalEvents(); |
||
63 | _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel; |
||
64 | |||
65 | _scrollUpContinuation = new ScheduledContinuation(); |
||
66 | _scrollDownContinuation = new ScheduledContinuation(); |
||
67 | } |
||
68 | |||
69 | /// <summary> |
||
70 | /// Clean up any resources being used. |
||
71 | /// </summary> |
||
72 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
73 | protected override void Dispose(bool disposing) |
||
74 | { |
||
75 | if (disposing && components != null) |
||
76 | { |
||
77 | _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel; |
||
78 | _globalMouseKeyHook.Dispose(); |
||
79 | |||
80 | components.Dispose(); |
||
81 | } |
||
82 | |||
83 | base.Dispose(disposing); |
||
84 | } |
||
85 | |||
86 | #endregion |
||
87 | |||
88 | #region Event Handlers |
||
89 | |||
2 | office | 90 | private void DefaultOnSettingsSaving(object sender, CancelEventArgs e) |
91 | { |
||
92 | } |
||
93 | |||
94 | private void DefaultOnSettingsLoaded(object sender, SettingsLoadedEventArgs e) |
||
95 | { |
||
96 | } |
||
97 | |||
98 | private void DefaultOnPropertyChanged(object sender, PropertyChangedEventArgs e) |
||
99 | { |
||
100 | Settings.Default.Save(); |
||
101 | } |
||
102 | |||
1 | office | 103 | private static void GlobalMouseKeyHookMouseWheel(object sender, MouseEventArgs e) |
104 | { |
||
105 | var delta = Math.Abs(e.Delta); |
||
106 | |||
107 | switch (Math.Sign(e.Delta)) |
||
108 | { |
||
109 | case 0: |
||
110 | break; |
||
111 | case 1: // up |
||
112 | Interlocked.Add(ref _upDelta, delta); |
||
113 | _scrollUpContinuation.Schedule(TimeSpan.FromMilliseconds(50), ScrollUp); |
||
114 | Debug.WriteLine($"Mouse wheel moved up by {e.Delta}."); |
||
115 | break; |
||
116 | case -1: // down |
||
117 | Interlocked.Add(ref _downDelta, delta); |
||
118 | _scrollDownContinuation.Schedule(TimeSpan.FromMilliseconds(50), ScrollDown); |
||
119 | Debug.WriteLine($"Mouse wheel moved down by {e.Delta}."); |
||
120 | break; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
||
125 | { |
||
126 | if (_aboutForm != null) |
||
127 | { |
||
128 | return; |
||
129 | } |
||
130 | |||
131 | _aboutForm = new AboutForm(); |
||
132 | _aboutForm.Closing += AboutForm_Closing; |
||
133 | _aboutForm.Show(); |
||
134 | } |
||
135 | |||
136 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
||
137 | { |
||
138 | if (_aboutForm == null) |
||
139 | { |
||
140 | return; |
||
141 | } |
||
142 | |||
143 | _aboutForm.Closing -= AboutForm_Closing; |
||
144 | _aboutForm.Dispose(); |
||
145 | _aboutForm = null; |
||
146 | } |
||
147 | |||
148 | private void QuitToolStripMenuItem_Click(object sender, EventArgs e) |
||
149 | { |
||
150 | Application.Exit(); |
||
151 | } |
||
152 | |||
2 | office | 153 | private void LaunchOnBootToolStripMenuItem_Click(object sender, EventArgs e) |
154 | { |
||
155 | Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; |
||
156 | |||
157 | LaunchOnBoot.Set(Settings.Default.LaunchOnBoot); |
||
158 | } |
||
159 | |||
1 | office | 160 | #endregion |
161 | |||
162 | #region Private Methods |
||
163 | |||
164 | private static async void ScrollDown() |
||
165 | { |
||
166 | var downDelta = Interlocked.Read(ref _downDelta); |
||
167 | var delta = Math.Abs(downDelta / SystemInformation.MouseWheelScrollDelta); |
||
168 | Interlocked.Exchange(ref _downDelta, 0); |
||
169 | |||
170 | Debug.WriteLine($"Down event expired with delta {delta}"); |
||
171 | |||
172 | if (_scrollCancellationTokenSource != null) |
||
173 | { |
||
174 | _scrollCancellationTokenSource.Cancel(); |
||
175 | } |
||
176 | |||
177 | _scrollCancellationTokenSource = new CancellationTokenSource(); |
||
178 | var cancellationToken = _scrollCancellationTokenSource.Token; |
||
179 | |||
180 | _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel; |
||
181 | |||
182 | try |
||
183 | { |
||
184 | await ScrollDown((int) delta, cancellationToken); |
||
185 | } |
||
186 | finally |
||
187 | { |
||
188 | _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | private static async void ScrollUp() |
||
193 | { |
||
194 | var upDelta = Interlocked.Read(ref _upDelta); |
||
195 | var delta = Math.Abs(upDelta / SystemInformation.MouseWheelScrollDelta); |
||
196 | Interlocked.Exchange(ref _upDelta, 0); |
||
197 | |||
198 | Debug.WriteLine($"Up event expired with delta {delta}"); |
||
199 | |||
200 | if (_scrollCancellationTokenSource != null) |
||
201 | { |
||
202 | _scrollCancellationTokenSource.Cancel(); |
||
203 | } |
||
204 | |||
205 | _scrollCancellationTokenSource = new CancellationTokenSource(); |
||
206 | var cancellationToken = _scrollCancellationTokenSource.Token; |
||
207 | |||
208 | _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel; |
||
209 | |||
210 | try |
||
211 | { |
||
212 | await ScrollUp((int) delta, cancellationToken); |
||
213 | } |
||
214 | finally |
||
215 | { |
||
216 | _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | private static async Task ScrollUp(int delta, CancellationToken cancellationToken) |
||
221 | { |
||
222 | while (!cancellationToken.IsCancellationRequested && delta > 0) |
||
223 | { |
||
224 | _inputSimulator.Mouse.VerticalScroll(delta); |
||
225 | await Task.Delay(25 * delta, cancellationToken); |
||
226 | Debug.WriteLine($"Moving: {delta}"); |
||
227 | delta = (int) Math.Log(delta, 2); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | private static async Task ScrollDown(int delta, CancellationToken cancellationToken) |
||
232 | { |
||
233 | while (!cancellationToken.IsCancellationRequested && delta > 0) |
||
234 | { |
||
235 | _inputSimulator.Mouse.VerticalScroll(-delta); |
||
236 | await Task.Delay(25 * delta, cancellationToken); |
||
237 | Debug.WriteLine($"Moving: {delta}"); |
||
238 | delta = (int) Math.Log(delta, 2); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | #endregion |
||
243 | } |
||
244 | } |