Inertia – Blame information for rev 5
?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 | |||
5 | office | 62 | _scrollCancellationTokenSource = new CancellationTokenSource(); |
63 | |||
1 | office | 64 | _globalMouseKeyHook = Hook.GlobalEvents(); |
65 | _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel; |
||
66 | |||
67 | _scrollUpContinuation = new ScheduledContinuation(); |
||
68 | _scrollDownContinuation = new ScheduledContinuation(); |
||
69 | } |
||
70 | |||
71 | /// <summary> |
||
72 | /// Clean up any resources being used. |
||
73 | /// </summary> |
||
74 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
75 | protected override void Dispose(bool disposing) |
||
76 | { |
||
77 | if (disposing && components != null) |
||
78 | { |
||
79 | _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel; |
||
80 | _globalMouseKeyHook.Dispose(); |
||
81 | |||
82 | components.Dispose(); |
||
83 | } |
||
84 | |||
85 | base.Dispose(disposing); |
||
86 | } |
||
87 | |||
88 | #endregion |
||
89 | |||
90 | #region Event Handlers |
||
91 | |||
2 | office | 92 | private void DefaultOnSettingsSaving(object sender, CancelEventArgs e) |
93 | { |
||
94 | } |
||
95 | |||
96 | private void DefaultOnSettingsLoaded(object sender, SettingsLoadedEventArgs e) |
||
97 | { |
||
98 | } |
||
99 | |||
100 | private void DefaultOnPropertyChanged(object sender, PropertyChangedEventArgs e) |
||
101 | { |
||
102 | Settings.Default.Save(); |
||
103 | } |
||
104 | |||
1 | office | 105 | private static void GlobalMouseKeyHookMouseWheel(object sender, MouseEventArgs e) |
106 | { |
||
107 | var delta = Math.Abs(e.Delta); |
||
108 | |||
109 | switch (Math.Sign(e.Delta)) |
||
110 | { |
||
111 | case 0: |
||
112 | break; |
||
113 | case 1: // up |
||
114 | Interlocked.Add(ref _upDelta, delta); |
||
115 | _scrollUpContinuation.Schedule(TimeSpan.FromMilliseconds(50), ScrollUp); |
||
116 | Debug.WriteLine($"Mouse wheel moved up by {e.Delta}."); |
||
117 | break; |
||
118 | case -1: // down |
||
119 | Interlocked.Add(ref _downDelta, delta); |
||
120 | _scrollDownContinuation.Schedule(TimeSpan.FromMilliseconds(50), ScrollDown); |
||
121 | Debug.WriteLine($"Mouse wheel moved down by {e.Delta}."); |
||
122 | break; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
||
127 | { |
||
128 | if (_aboutForm != null) |
||
129 | { |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | _aboutForm = new AboutForm(); |
||
134 | _aboutForm.Closing += AboutForm_Closing; |
||
135 | _aboutForm.Show(); |
||
136 | } |
||
137 | |||
138 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
||
139 | { |
||
140 | if (_aboutForm == null) |
||
141 | { |
||
142 | return; |
||
143 | } |
||
144 | |||
145 | _aboutForm.Closing -= AboutForm_Closing; |
||
146 | _aboutForm.Dispose(); |
||
147 | _aboutForm = null; |
||
148 | } |
||
149 | |||
150 | private void QuitToolStripMenuItem_Click(object sender, EventArgs e) |
||
151 | { |
||
152 | Application.Exit(); |
||
153 | } |
||
154 | |||
2 | office | 155 | private void LaunchOnBootToolStripMenuItem_Click(object sender, EventArgs e) |
156 | { |
||
157 | Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; |
||
158 | |||
159 | LaunchOnBoot.Set(Settings.Default.LaunchOnBoot); |
||
160 | } |
||
161 | |||
1 | office | 162 | #endregion |
163 | |||
164 | #region Private Methods |
||
165 | |||
166 | private static async void ScrollDown() |
||
167 | { |
||
5 | office | 168 | _scrollCancellationTokenSource.Cancel(); |
169 | _scrollCancellationTokenSource = new CancellationTokenSource(); |
||
170 | var cancellationToken = _scrollCancellationTokenSource.Token; |
||
171 | |||
1 | office | 172 | var downDelta = Interlocked.Read(ref _downDelta); |
173 | var delta = Math.Abs(downDelta / SystemInformation.MouseWheelScrollDelta); |
||
174 | Interlocked.Exchange(ref _downDelta, 0); |
||
175 | |||
176 | Debug.WriteLine($"Down event expired with delta {delta}"); |
||
177 | |||
178 | _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel; |
||
179 | |||
180 | try |
||
5 | office | 181 | { |
1 | office | 182 | await ScrollDown((int) delta, cancellationToken); |
183 | } |
||
3 | office | 184 | catch (TaskCanceledException) |
185 | { |
||
186 | // We do not care. |
||
187 | } |
||
1 | office | 188 | finally |
189 | { |
||
190 | _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | private static async void ScrollUp() |
||
195 | { |
||
5 | office | 196 | _scrollCancellationTokenSource.Cancel(); |
197 | _scrollCancellationTokenSource = new CancellationTokenSource(); |
||
198 | var cancellationToken = _scrollCancellationTokenSource.Token; |
||
199 | |||
1 | office | 200 | var upDelta = Interlocked.Read(ref _upDelta); |
201 | var delta = Math.Abs(upDelta / SystemInformation.MouseWheelScrollDelta); |
||
202 | Interlocked.Exchange(ref _upDelta, 0); |
||
203 | |||
204 | Debug.WriteLine($"Up event expired with delta {delta}"); |
||
205 | |||
206 | _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel; |
||
207 | |||
208 | try |
||
209 | { |
||
210 | await ScrollUp((int) delta, cancellationToken); |
||
211 | } |
||
3 | office | 212 | catch (TaskCanceledException) |
213 | { |
||
214 | // We do not care. |
||
215 | } |
||
1 | office | 216 | finally |
217 | { |
||
218 | _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | private static async Task ScrollUp(int delta, CancellationToken cancellationToken) |
||
223 | { |
||
5 | office | 224 | while (!cancellationToken.IsCancellationRequested && delta > 1) |
1 | office | 225 | { |
226 | _inputSimulator.Mouse.VerticalScroll(delta); |
||
227 | await Task.Delay(25 * delta, cancellationToken); |
||
228 | Debug.WriteLine($"Moving: {delta}"); |
||
229 | delta = (int) Math.Log(delta, 2); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | private static async Task ScrollDown(int delta, CancellationToken cancellationToken) |
||
234 | { |
||
5 | office | 235 | while (!cancellationToken.IsCancellationRequested && delta > 1) |
1 | office | 236 | { |
237 | _inputSimulator.Mouse.VerticalScroll(-delta); |
||
238 | await Task.Delay(25 * delta, cancellationToken); |
||
239 | Debug.WriteLine($"Moving: {delta}"); |
||
240 | delta = (int) Math.Log(delta, 2); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | #endregion |
||
245 | } |
||
246 | } |