-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (86 loc) · 1.76 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
package main
import (
_ "embed"
"flag"
"fmt"
"os"
hook "github.com/robotn/gohook"
"github.com/getlantern/systray"
"github.com/tajtiattila/vjoy"
)
//go:embed build/icon-systray.ico
var icon []byte
var joy *vjoy.Device
var joyNum = uint(1)
var pullRange = 1000
func main() {
initializeArgs()
initializeJoy()
systray.Run(onSystrayReady, onSystrayExit)
}
func initializeArgs() {
flag.UintVar(&joyNum, "joyNum", 1, "Joy number")
flag.IntVar(&pullRange, "pullDistance", 1000, "Pull distance")
flag.Parse()
}
func onSystrayReady() {
systray.SetIcon(icon)
systray.SetTitle("Mouse Plunger")
mQuit := systray.AddMenuItem("Quit", "Quit application")
go func() {
<-mQuit.ClickedCh
systray.Quit()
os.Exit(0)
}()
registerHooks()
}
func onSystrayExit() {
cleanup()
}
func cleanup() {
joy.Reset()
joy.Relinquish()
}
func initializeJoy() {
var err error
joy, err = vjoy.Acquire(joyNum)
if err != nil {
panic(fmt.Sprintf("Couldn't find vJoy device %d. Is it configured?", joyNum))
}
setJoyZ(0)
update()
}
func registerHooks() {
var initialY int16
var active = false
hook.Register(hook.MouseHold, []string{}, func(e hook.Event) {
if e.Button == hook.MouseMap["left"] {
active = true
initialY = e.Y
}
})
hook.Register(hook.MouseDrag, []string{}, func(e hook.Event) {
if active {
yDistance := initialY - e.Y
setJoyZ(int(float32(-yDistance)/float32(pullRange) * 0x7fff))
update()
}
})
hook.Register(hook.MouseUp, []string{}, func(e hook.Event) {
if e.Button == hook.MouseMap["left"] {
setJoyZ(0)
active = false
update()
}
})
s := hook.Start()
<-hook.Process(s)
}
func setJoyZ(val int) {
joy.Axis(vjoy.AxisZ).Setc(val)
}
func update() {
if err := joy.Update(); err != nil {
fmt.Printf("Got error: %s\n", err.Error())
}
}