-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (141 loc) · 3.61 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
package main
import (
"fmt"
"os"
"strings"
"time"
"log/slog"
"github.com/nvlled/carrot"
"github.com/nvlled/dumbwheel/mouse"
"github.com/nvlled/dumbwheel/xdo"
)
func findMouseEventDevice() string {
dir := "/dev/input/by-id"
entries, err := os.ReadDir(dir)
ruhOh(err)
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), "-event-mouse") {
return dir + "/" + entry.Name()
}
}
return ""
}
func usage() {
program := os.Args[0]
fmt.Printf("usage: %v [input event filename]\n", program)
fmt.Printf("if no filename is given, it will automatically look for one.\n")
fmt.Printf("\n")
fmt.Printf("Examples: %v /dev/input/10\n", program)
fmt.Printf(" %v /dev/input/by-id/usb-MOSART_Semi._2.4G_INPUT_DEVICE-if01-event-mouse\n", program)
fmt.Printf(" %v /dev/input/by-path/pci-0000:00:14.0-usb-0:2:1.1-event-mouse\n", program)
fmt.Printf("\n")
fmt.Printf("Note: when reading from /dev/input/by-id or /dev/input/by-path, look for filenames\n")
fmt.Printf(" that ends with '-event-mouse'\n")
os.Exit(1)
}
func main() {
deviceName := ""
if len(os.Args) < 2 {
deviceName = findMouseEventDevice()
} else {
deviceName = os.Args[1]
}
if deviceName == "" {
usage()
}
slog.Info("using input event device")
slog.Info(deviceName)
var programLevel = new(slog.LevelVar)
h := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: programLevel})
slog.SetDefault(slog.New(h))
if os.Getenv("DEBUG") != "" {
programLevel.Set(slog.LevelDebug)
}
var prevEvent *mouse.Event = nil
var scrollScript *carrot.Script
var loop *Interval
move := 0
maxMove := 1000
xd := xdo.New()
scrollScript = carrot.Start(func(ctrl *carrot.Control) {
loopStart := time.Now().Add(-1 * time.Second)
accelerated := false
for {
for prevEvent == nil {
ctrl.Yield()
}
accelerated = time.Since(loopStart) < 300*time.Millisecond
loopStart = time.Now()
current := *prevEvent
var mouseWheel xdo.MouseButton
if current.Button == mouse.ButtonThumbUp {
slog.Debug("start scroll up", "accelerated", accelerated)
mouseWheel = xdo.MbWheelUp
} else {
slog.Debug("start scroll down", "accelerated", accelerated)
mouseWheel = xdo.MbWheelDown
}
slog.Debug("scroll")
xd.MouseClick(mouseWheel)
sub := ctrl.StartAsync(func(ctrl *carrot.Control) {
ctrl.Sleep(256 * time.Millisecond)
for {
if accelerated {
xd.MouseClick(mouseWheel)
xd.MouseClick(mouseWheel)
if move > 200 {
xd.MouseClick(mouseWheel)
}
if move > 500 {
xd.MouseClick(mouseWheel)
xd.MouseClick(mouseWheel)
}
if move > 1000 {
xd.MouseClick(mouseWheel)
xd.MouseClick(mouseWheel)
xd.MouseClick(mouseWheel)
}
slog.Debug("scroll", "acceleration", move)
} else {
slog.Debug("scroll")
xd.MouseClick(mouseWheel)
}
ctrl.Yield()
}
})
loop.Start()
for {
if prevEvent.Type == mouse.EventOnUp && prevEvent.Button == current.Button {
break
}
ctrl.Yield()
}
sub.Cancel()
loop.Stop()
prevEvent = nil
slog.Debug("end scroll")
}
})
loop = NewInterval(scrollScript.Update, 100*time.Millisecond)
for event := range mouse.ReadEvents(deviceName) {
if event.Type != mouse.EventOnMove || move == 0 {
slog.Debug("read mouse event", "data", event)
}
if event.Type == mouse.EventOnMove {
if move < maxMove {
move++
}
continue
}
if event.Button == mouse.ButtonThumbUp || event.Button == mouse.ButtonThumbDown {
prevEvent = &event
move = 0
scrollScript.Update()
}
}
}
func ruhOh(err error) {
if err != nil {
panic(err)
}
}