-
Notifications
You must be signed in to change notification settings - Fork 4
/
LibreScroll.au3
383 lines (365 loc) · 16.5 KB
/
LibreScroll.au3
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#NoTrayIcon
If $CmdLine[0]==7 Then
Global $rect = DllStructCreate('long x1;long y1;long x2;long y2;');
Child($CmdLine)
Else
If IsAdmin() Then Sleep(100)
If _Singleton('LibreScroll',0) Then Parent()
EndIf
Func Parent()
Opt("TrayIconHide", 0)
Opt('TrayAutoPause',0)
Opt('TrayOnEventMode',1)
Opt('TrayMenuMode',1+2)
If Not IsAdmin() Then TrayItemSetOnEvent(TrayCreateItem('Restart as admin'),Elevate)
TrayItemSetOnEvent(TrayCreateItem('About LibreScroll'),Info)
TrayItemSetOnEvent(TrayCreateItem('Options'),Unhide)
TrayItemSetOnEvent(TrayCreateItem('Quit'),Quit)
Global $g_hWnd = GUICreate('LibreScroll options',215,210,Default,Default,0x00C80000)
GUISetIcon('main.cpl',608)
TraySetOnEvent(-7,Unhide)
TraySetOnEvent(-11,UpdateTip)
TraySetClick(8)
TraySetIcon('main.cpl',608)
GUICtrlCreateLabel('Friction' ,5, 5,100,20,0x0201)
GUICtrlCreateLabel('Y-Sensitivity' ,5, 30,100,20,0x0201)
GUICtrlCreateLabel('X-Sensitivity' ,5, 55,100,20,0x0201)
GUICtrlCreateLabel('Minimum Y Step',5, 80,100,20,0x0201)
GUICtrlCreateLabel('Minimum X Step',5,105,100,20,0x0201)
Local $decay = GUICtrlCreateInput(IniRead('options.ini','LibreScroll','decay',3) , 110 , 5 , 100 , 20)
Local $sensY = GUICtrlCreateInput(IniRead('options.ini','LibreScroll','sensY',9) , 110 , 30 , 100 , 20)
Local $sensX = GUICtrlCreateInput(IniRead('options.ini','LibreScroll','sensX',0) , 110 , 55 , 100 , 20)
Local $stepY = GUICtrlCreateInput(IniRead('options.ini','LibreScroll','stepY',1) , 110 , 80 , 100 , 20)
Local $stepX = GUICtrlCreateInput(IniRead('options.ini','LibreScroll','stepX',1) , 110 ,105 , 100 , 20)
Local $flick = GUICtrlCreateCheckbox('Flick mode',110,130)
GUICtrlSetState( $flick , 1=IniRead('options.ini','LibreScroll','flick',0) ? 1 : 4 )
Local $think = GUICtrlCreateCheckbox('ThinkPad mode',110,155)
GUICtrlSetState( $think , 1=IniRead('options.ini','LibreScroll','think',0) ? 1 : 4 )
Local $pause = GUICtrlCreateButton('Pause', 5,180,100)
Local $apply = GUICtrlCreateButton('Apply',110,180,100)
GUICtrlSetTip($decay,'How quickly the momentum decays, by applying a drag force that is proportional to speed.')
GUICtrlSetTip($sensY,'Vertical scrolling sensitivity. Set a negative number for reverse scrolling.')
GUICtrlSetTip($sensX,'Horizontal scrolling sensitivity. Set a negative number for reverse scrolling.')
GUICtrlSetTip($stepY,'How many scroll counts to accumulate before sending.' & @CRLF & 'A regular coarse scroll step is 120.')
GUICtrlSetTip($stepX,'How many scroll counts to accumulate before sending.' & @CRLF & 'A regular coarse scroll step is 120.')
GUICtrlSetTip($flick,'Continue scrolling even after mouse3 is released.' & @CRLF & 'The momentum can be reset by clicking any button or moving the wheel.')
GUICtrlSetTip($think,'Emulate how ThinkPad TrackPoint scrolling works.' & @CRLF & 'Only X or Y scrolling is sent at any given time.')
Local $str = ''
$str &= Number(GUICtrlRead($sensX)) & ' '
$str &= Number(GUICtrlRead($sensY)) & ' '
$str &= Number(GUICtrlRead($decay)) & ' '
$str &= Round(GUICtrlRead($stepX)) & ' '
$str &= Round(GUICtrlRead($stepY)) & ' '
$str &= Number((1=GUICtrlRead($flick)) + (1=GUICtrlRead($think))*2) & ' '
StartChild($str)
While 1
Local $id = GUIGetMsg()
Switch $id
Case -3
GUISetState(@SW_HIDE,$g_hWnd)
Case $decay
Local $value = Number(GUICtrlRead($id))
If $value<1 Then $value = 1
GUICtrlSetData($id,$value)
Case $sensX,$sensY
GUICtrlSetData($id,Number(GUICtrlRead($id)))
Case $stepX,$stepY
Local $value = Number(GUICtrlRead($id))
If $value<1 Then
GUICtrlSetData($id,1)
ElseIf $value>120 Then
GUICtrlSetData($id,120)
Else
GUICtrlSetData($id,Round($value))
EndIf
Case $flick
Case $think
Case $apply,$pause
If $id==$pause And GUICtrlRead($pause)=='Pause' Then
CloseChild()
GUICtrlSetData($pause,'Unpause')
Else
Local $arr = [ _
Number(GUICtrlRead($sensX)) , _
Number(GUICtrlRead($sensY)) , _
Number(GUICtrlRead($decay)) , _
Round(GUICtrlRead($stepX)) , _
Round(GUICtrlRead($stepY)) , _
Number((1=GUICtrlRead($flick)) + (1=GUICtrlRead($think))*2) _
]
Local $str = $arr[0] & ' ' & $arr[1] & ' ' & $arr[2] & ' ' & $arr[3] & ' ' & $arr[4] & ' ' & $arr[5]
RestartChild($str)
GUICtrlSetData($pause,'Pause')
IniWrite('options.ini','LibreScroll','sensX',$arr[0])
IniWrite('options.ini','LibreScroll','sensY',$arr[1])
IniWrite('options.ini','LibreScroll','decay',$arr[2])
IniWrite('options.ini','LibreScroll','stepX',$arr[3])
IniWrite('options.ini','LibreScroll','stepY',$arr[4])
IniWrite('options.ini','LibreScroll','flick',BitAnd(1,$arr[5]) ? 1 : 0)
IniWrite('options.ini','LibreScroll','think',BitAnd(2,$arr[5]) ? 1 : 0)
EndIf
EndSwitch
WEnd
EndFunc
Func Unhide()
GUISetState(@SW_SHOW,$g_hWnd)
GUISetState(@SW_RESTORE,$g_hWnd)
EndFunc
Func UpdateTip()
TraySetToolTip('LibreScroll - ' & ( ProcessExists($g_childPID) ? 'Active' : 'Inactive') )
EndFunc
Func Info()
MsgBox(0,'About LibreScroll v1.2','Visit https://github.com/EsportToys/LibreScroll for more info.')
EndFunc
Func Elevate()
CloseChild()
ShellExecute( @AutoItExe , @Compiled ? '' : @ScriptFullPath , '' , 'runas' )
Exit
EndFunc
Func Quit()
Exit
EndFunc
Func RestartChild($argStr)
CloseChild()
StartChild($argStr)
EndFunc
Func CloseChild()
OnAutoItExitUnRegister ( CloseChild )
If $g_childPID Then ProcessClose($g_childPID)
$g_childPID = Null
EndFunc
Func StartChild($argStr)
Global $g_childPID = Run(@AutoItExe & ' ' & (@Compiled?'':@ScriptFullPath) & ' ' & @AutoItPID & ' ' & $argStr)
If ProcessExists($g_childPID) Then
OnAutoItExitRegister ( CloseChild )
Else
Exit
EndIf
EndFunc
Func Child($args)
If Not ProcessExists($args[1]) Then Return
Global $g_sensitivity_x = Number($args[2])
Global $g_sensitivity_y = Number($args[3])
Global $g_damping = $args[4]>0 ? $args[4] : 3
Global $g_threshold_x = $args[5]>1 ? $args[5] : 1
Global $g_threshold_y = $args[6]>1 ? $args[6] : 1
Global $g_flickMode = BitAnd(1,Number($args[7])) ? True : False
Global $g_thinkMode = BitAnd(2,Number($args[7])) ? True : False
Global Const $user32dll = DllOpen('user32.dll')
Global Const $kernel32dll = DllOpen('kernel32.dll')
Global $g_scrollVel = [0,0]
Global $g_scrollAccu = [0,0]
Global $g_trigger_isDown = False
Global $g_cancel_pending = False
Local $_ = DllStructCreate('ushort page;ushort usage;dword flags;hwnd target')
Local $size = DllStructGetSize($_)
$_.page = 1
$_.usage = 2
$_.flags = 256
$_.target = GUICreate('')
DllCall('user32.dll','bool','SetProcessDPIAware')
DllCall('user32.dll','bool','RegisterRawInputDevices','struct*',$_,'uint',1,'uint',$size)
GUIRegisterMsg(0xFF,WM_INPUT)
Local $time = Now()
While Sleep(10)
Local $now = Now()
Tick($now-$time)
$time = $now
WEnd
EndFunc
Func Now()
Return DllCall($kernel32dll,'bool','QueryPerformanceCounter','uint64*',Null)[1]
EndFunc
Func WM_INPUT($h,$m,$w,$l)
Local Static $HEAD = 'struct;dword Type;dword Size;handle hDevice;wparam wParam;endstruct;'
Local Static $MOU = $HEAD & 'ushort Flags;ushort Alignment;ushort ButtonFlags;short ButtonData;ulong RawButtons;long LastX;long LastY;ulong ExtraInformation;'
Local Static $raw = DllStructCreate($MOU)
Local Static $size = DllStructGetSize($raw)
Local Static $headsize = DllStructGetSize(DllStructCreate($HEAD))
DllCall ( $user32dll,'uint','GetRawInputData','handle',$l,'uint',0x10000003,'struct*',$raw,'uint*',$size,'uint',$headsize)
If $raw.hDevice Then
If $raw.ButtonFlags Then
If BitAnd(32,$raw.ButtonFlags) Then
$g_trigger_isDown = False
$g_cancel_pending = False
If Not $g_flickMode Then
$g_scrollVel[0] = 0
$g_scrollVel[1] = 0
EndIf
DllCall($user32dll,'bool','ClipCursor','struct*',Null)
ElseIf BitAnd(16,$raw.ButtonFlags) Then
$g_scrollAccu[0] = 0
$g_scrollAccu[1] = 0
$g_scrollVel[0] = 0
$g_scrollVel[1] = 0
$g_trigger_isDown = True
$g_cancel_pending = True
DllCall($user32dll,'bool','GetCursorPos','struct*',$rect)
$rect.x2 = $rect.x1 + 1
$rect.y2 = $rect.y1 + 1
DllCall($user32dll,'bool','ClipCursor','struct*',$rect)
ElseIf $g_flickMode And Not $g_trigger_isDown Then
$g_scrollVel[1] = 0
$g_scrollVel[0] = 0
EndIf
Else
$g_scrollAccu[0] += $raw.LastX
$g_scrollAccu[1] += $raw.LastY
EndIf
EndIf
Return 0
EndFunc
Func SendCancel()
Local Static $SIZE = DllStructGetSize(DllStructCreate('dword;struct;long;long;dword;dword;dword;ulong_ptr;endstruct;'))
Local Static $arr = CancelStruct()
DllCall( $user32dll, 'uint', 'SendInput', 'uint', 2, 'struct*', $arr, 'int', $SIZE )
EndFunc
Func CancelStruct()
Local $SIZE = DllStructGetSize(DllStructCreate('dword;struct;long;long;dword;dword;dword;ulong_ptr;endstruct;'))
Local $arr = DllStructCreate('byte[' & $SIZE*2 & ']')
Local $ptr = DllStructGetPtr($arr)
Local $struct = DllStructCreate('dword;struct;word;word;dword;dword;ulong_ptr;endstruct;byte[8]',$ptr)
DllStructSetData($struct,1,1)
DllStructSetData($struct,2,0)
DllStructSetData($struct,3,0)
DllStructSetData($struct,4,0)
Local $struct = DllStructCreate('dword;struct;word;word;dword;dword;ulong_ptr;endstruct;byte[8]',$ptr+$SIZE)
DllStructSetData($struct,1,1)
DllStructSetData($struct,2,0)
DllStructSetData($struct,3,0)
DllStructSetData($struct,4,2)
Return $arr
EndFunc
Func Tick($ticks)
Local Static $qpf = DllCall($kernel32dll,'bool','QueryPerformanceFrequency','int64*',Null)[1]
Local Static $current_rect = DllStructCreate('long x1;long y1;long x2;long y2;');
If $g_trigger_isDown Then
DllCall($user32dll,'bool','GetClipCursor','struct*',$current_rect)
If $current_rect.x1 <> $rect.x1 or $current_rect.x2 <> $rect.x2 or $current_rect.y1 <> $rect.y1 or $current_rect.y2 <> $rect.y2 Then
DllCall($user32dll,'bool','ClipCursor','struct*',$rect)
EndIf
Local $deltaX = $g_scrollAccu[0]
Local $deltaY = $g_scrollAccu[1]
$g_scrollVel[0] += $deltaX*$g_sensitivity_x
$g_scrollVel[1] += $deltaY*$g_sensitivity_y
$g_scrollAccu[0] -= $deltaX
$g_scrollAccu[1] -= $deltaY
Else
If $g_flickMode Then
$g_scrollAccu[0]=0
$g_scrollAccu[1]=0
Else
Return
EndIf
EndIf
Local $dt = $ticks/$qpf
Local $mu = $g_damping
Local $f0 = Exp(-$dt * $mu)
Local $f1 = 0=$mu ? $dt : (1-$f0) / $mu
SendScroll($f1*$g_scrollVel[0],-$f1*$g_scrollVel[1])
$g_scrollVel[0] *= $f0
$g_scrollVel[1] *= $f0
If $g_scrollVel[0]*$g_scrollVel[0]+$g_scrollVel[1]*$g_scrollVel[1] < 0.25 Then
$g_scrollVel[0] = 0
$g_scrollVel[1] = 0
EndIf
EndFunc
Func SendScroll($deltaX,$deltaY)
Local Static $residue = [0,0]
$residue[0] += $deltaX
$residue[1] += $deltaY
Local $multipleX = Int($residue[0]/$g_threshold_x) , $multipleY = Int($residue[1]/$g_threshold_y)
Local $sendX = 0 , $sendY = 0
If $multipleX<>0 Then
$sendX = $multipleX*$g_threshold_x
$residue[0] -= $sendX
EndIf
If $multipleY<>0 Then
$sendY = $multipleY*$g_threshold_y
$residue[1] -= $sendY
EndIf
If $g_thinkMode Then
If Abs($sendX) > Abs($sendY) Then
$sendY = 0
$residue[1] = 0
Else
$sendX = 0
$residue[0] = 0
EndIf
EndIf
If $sendX<>0 or $sendY<>0 Then
If $g_cancel_pending Then
$g_cancel_pending = False
SendCancel()
EndIf
ScrollMouseXY($sendX,$sendY)
EndIf
EndFunc
Func ScrollMouseXY($dx,$dy)
Local Static $SIZE = DllStructGetSize(DllStructCreate('dword;struct;long;long;dword;dword;dword;ulong_ptr;endstruct;'))
Local Static $arr = DllStructCreate('byte[' & $SIZE*2 & ']')
Local Static $ptr = DllStructGetPtr($arr)
Local Static $yData = DllStructCreate('dword type;struct;long;long;dword data;dword flag;dword;ulong_ptr;endstruct;', $ptr)
Local Static $xData = DllStructCreate('dword type;struct;long;long;dword data;dword flag;dword;ulong_ptr;endstruct;', $ptr+$SIZE)
Local Static $_ = SetupScrollData($xData,$yData)
Local $count = ($dx?1:0)+($dy?1:0)
Local $addr = $dy ? $ptr : $ptr+$SIZE
DllStructSetData($yData,'data',$dy)
DllStructSetData($xData,'data',$dx)
DllCall( $user32dll, 'uint', 'SendInput', 'uint', $count, 'struct*', $addr, 'int', $SIZE )
EndFunc
Func SetupScrollData($xData,$yData)
DllStructSetData($xData,'type',0)
DllStructSetData($xData,'flag',0x1000)
DllStructSetData($yData,'type',0)
DllStructSetData($yData,'flag',0x0800)
EndFunc
; #FUNCTION# ====================================================================================================================
; Author ........: Valik
; Modified.......:
; ===============================================================================================================================
Global Const $tagSECURITY_ATTRIBUTES = "dword Length;ptr SecurityDescriptor;bool InheritHandle"
Func _Singleton($sOccurrenceName, $iFlag = 0)
Local Const $ERROR_ALREADY_EXISTS = 183
Local Const $SECURITY_DESCRIPTOR_REVISION = 1
Local $tSecurityAttributes = 0
If BitAND($iFlag, 2) Then
; The size of SECURITY_DESCRIPTOR is 20 bytes. We just
; need a block of memory the right size, we aren't going to
; access any members directly so it's not important what
; the members are, just that the total size is correct.
Local $tSecurityDescriptor = DllStructCreate("byte;byte;word;ptr[4]")
; Initialize the security descriptor.
Local $aCall = DllCall("advapi32.dll", "bool", "InitializeSecurityDescriptor", _
"struct*", $tSecurityDescriptor, "dword", $SECURITY_DESCRIPTOR_REVISION)
If @error Then Return SetError(@error, @extended, 0)
If $aCall[0] Then
; Add the NULL DACL specifying access to everybody.
$aCall = DllCall("advapi32.dll", "bool", "SetSecurityDescriptorDacl", _
"struct*", $tSecurityDescriptor, "bool", 1, "ptr", 0, "bool", 0)
If @error Then Return SetError(@error, @extended, 0)
If $aCall[0] Then
; Create a SECURITY_ATTRIBUTES structure.
$tSecurityAttributes = DllStructCreate($tagSECURITY_ATTRIBUTES)
; Assign the members.
DllStructSetData($tSecurityAttributes, 1, DllStructGetSize($tSecurityAttributes))
DllStructSetData($tSecurityAttributes, 2, DllStructGetPtr($tSecurityDescriptor))
DllStructSetData($tSecurityAttributes, 3, 0)
EndIf
EndIf
EndIf
Local $aHandle = DllCall("kernel32.dll", "handle", "CreateMutexW", "struct*", $tSecurityAttributes, "bool", 1, "wstr", $sOccurrenceName)
If @error Then Return SetError(@error, @extended, 0)
Local $aLastError = DllCall("kernel32.dll", "dword", "GetLastError")
If @error Then Return SetError(@error, @extended, 0)
If $aLastError[0] = $ERROR_ALREADY_EXISTS Then
If BitAND($iFlag, 1) Then
DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $aHandle[0])
If @error Then Return SetError(@error, @extended, 0)
Return SetError($aLastError[0], $aLastError[0], 0)
Else
Exit -1
EndIf
EndIf
Return $aHandle[0]
EndFunc ;==>_Singleton