Inertia – Diff between revs 3 and 5

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