-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
215 lines (173 loc) · 4 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"math"
"os"
"time"
"github.com/go-vgo/robotgo"
)
var (
pause = time.Millisecond * 100
debug = false
zero = false
left = false
right = false
mid = false
scrolled = false
keys = [4]*bool{&zero, &left, &right, &mid}
keyPressed = uint8(0)
)
func main() {
flag.BoolVar(&debug, "d", false, "print debug")
path := flag.String("p", "/dev/hidraw0", "path to device file")
flag.DurationVar(&pause, "t", time.Millisecond*100, "Milliseconds to wait for middle button emulation."+
" If two buttons pressed simultaneously before timeout, middle button will be pressed."+
" If one button is pressed more than timeout, usual button will be pressed")
flag.Parse()
deb(`"BC" - before click processing; "AC" - after click processing; "MM" - mouse movement;
1 is left button; 2 is right button; 3 is middle button.
raw: {h='horizont rel' v='vertical rel' k='key_pressed'};
prog: {pk='previous key pressed; kp='key programm is holding down' s='was scrolling'};
pressed: { 1_button 2_button 3_button }
`)
f, err := os.Open(*path)
if err != nil {
log.Fatalln("Error opening f!!!", err)
return
}
defer f.Close()
// declare chunk size
const maxSz = 4
// create buffer
b := make([]byte, maxSz)
k, pk := uint8(0), uint8(0) // k='key pressed', pk='previous key pressed'
for {
// read content to buffer
readTotal, err := f.Read(b)
if err != nil {
log.Println("Reading error", err)
return
}
if readTotal != 4 || b[0] != 24 {
continue
}
k = b[1]
if k > 9 {
continue
}
h, v := int(int8(b[2])), int(int8(b[3]))
move(h, v)
deb("MM raw: {h=%d v=%d k=%d}; prog: {pk=%d; kp=%d; scr: %t;}; pressed: { %t %t %t }", h, v, k, pk, keyPressed, scrolled, *keys[1], *keys[2], *keys[3])
// if mouse click do not change
if k != pk {
deb("BC raw: {h=%d v=%d k=%d}; prog: {pk=%d; kp=%d; scr: %t;}; pressed: { %t %t %t }", h, v, k, pk, keyPressed, scrolled, *keys[1], *keys[2], *keys[3])
switch k {
case 0:
up()
case 1, 2:
if keyPressed == 0 {
keyPressed = k
go func() {
time.Sleep(pause)
if keyPressed == k {
down(k)
}
}()
}
case 3:
keyPressed = k
down(k)
}
deb("AC raw: {h=%d v=%d k=%d}; prog: {pk=%d; kp=%d; scr: %t;}; pressed: { %t %t %t }", h, v, k, pk, keyPressed, scrolled, *keys[1], *keys[2], *keys[3])
}
pk = k
}
}
func up() {
b := keyPressed
defer func() {
*keys[b] = false
keyPressed = 0
}()
if b == 0 {
return
} else if b == 3 && scrolled { // if we was scrolling, we don't press middle button
scrolled = false
return
}
if *keys[b] == true && b != 3 { // if it's middle button we press it only on "up", so it's always is in up state here
deb("up %d", b)
robotgo.MouseToggle("up", bt(b))
} else {
deb("down %d", b)
robotgo.MouseToggle("down", bt(b))
deb("up %d", b)
robotgo.MouseToggle("up", bt(b))
}
*keys[b] = false
keyPressed = 0
}
func down(b uint8) {
if *keys[b] == true || keyPressed == 0 {
return
}
if b != 3 { // if it's middle button we press it on "up" action
deb("down %d", b)
robotgo.MouseToggle("down", bt(b))
*keys[b] = true
keyPressed = b
}
}
func move(h, v int) {
if keyPressed == 3 {
h, v = scr(h, &H), scr(v, &V)
if h == 0 && v == 0 {
return
}
robotgo.Scroll(h, v, 5)
scrolled = true
return
}
h, v = e(h), e(v)
if h == 0 && v == 0 {
return
}
robotgo.MoveRelative(h, v)
}
var H, V float64
func scr(x int, base *float64) int {
if x == 0 {
return 0
}
f := float64(x)
sign := int(-f/math.Abs(f)) * int(math.Sqrt(math.Abs(f)))
*base -= math.Abs(f) * math.Abs(f)
if *base < 0 {
*base = 77
return sign
}
return 0
}
func e(x int) int {
f := float64(x)
return int(f * math.Log(math.Abs(f)*math.Exp(math.Abs(f))))
}
func deb(format string, a ...interface{}) {
if !debug {
return
}
fmt.Printf(format+"\n", a...)
}
func bt(x uint8) string {
switch x {
case 1:
return "left"
case 2:
return "right"
case 3:
return "center"
}
return "left"
}