/
pimouse.go
161 lines (146 loc) · 4.22 KB
/
pimouse.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
package main
import (
"fmt"
"os"
"github.com/gvalkov/golang-evdev"
"strings"
"time"
"log"
"path/filepath"
"errors"
"gadget"
"config"
)
func main() {
// initialize usb gadget
gadget.Initialize()
// open usb gadget device for write only
gadgetDevice, err := os.OpenFile(
"/dev/hidg0",
os.O_WRONLY,
0000,
)
if err != nil {
log.Fatal(err)
}
defer gadgetDevice.Close()
var miceHooked []*evdev.InputDevice
for {
micePaths, err := getMicePaths("/dev/input/by-id/")
if err != nil {
// errors when no usb devices plugged in
fmt.Printf("\rWaiting no devices found...")
} else {
next:
for _, mousePath := range micePaths {
// don't hook mice that are already hooked
for _, mouseHooked := range miceHooked {
if mousePath == mouseHooked.Fn {
continue next
}
}
mouseDevice, err := evdev.Open(mousePath)
if err != nil {
fmt.Println(err)
} else {
// load default.conf config if mouseDevice specific one does not exist
var configPath string
configDir := "/etc/pimouse"
configPathDefault := filepath.Join(configDir, "default.yaml")
configPathCustom := filepath.Join(configDir, filepath.Base(mousePath) + ".yaml")
_, err = os.Stat(configPathCustom)
if errors.Is(err, os.ErrNotExist) {
configPath = configPathDefault
} else {
configPath = configPathCustom
}
config, _ := config.Parse(configPath)
// track mice that are currently connected and hooked in miceHooked slice
miceHooked = append(miceHooked, mouseDevice)
fmt.Printf("\nConnected \"%s\"\n", mouseDevice.Name)
fmt.Printf(" Device %s\n", mouseDevice.Fn)
fmt.Printf(" Config %s\n", configPath)
// hook the mouse
go hookMouse(mouseDevice, config, gadgetDevice, &miceHooked)
}
}
}
time.Sleep(1 * time.Second)
}
}
func hookMouse(mouseDevice *evdev.InputDevice, config config.MouseConfig, gadgetDevice *os.File, miceHooked *[]*evdev.InputDevice) {
// main mouseDevice event loop
mouseDevice.Grab()
gadgetBytes := make([]byte, 4)
prev_time := time.Now()
for {
//start := time.Now()
// read events from mouseDevice
mouseEvent, err := mouseDevice.ReadOne()
//elapsed := time.Since(start)
// if events cannot be read from mouseDevice remove mouse from miceHooked and exit goroutine
if err != nil {
for i, mouseHooked := range *miceHooked {
if mouseDevice == mouseHooked {
(*miceHooked)[i] = (*miceHooked)[len(*miceHooked)-1]
*miceHooked = (*miceHooked)[:len(*miceHooked)-1]
}
}
fmt.Printf("\nDisconnected \"%s\"\n", mouseDevice.Name)
return
}
// button event
if mouseEvent.Type == 1 {
if button_byte, ok := config.ButtonMap[mouseEvent.Code]; ok {
if mouseEvent.Value == 1 {
gadgetBytes[0] = button_byte
} else {
gadgetBytes[0] = byte(0)
}
}
// movement event
} else if mouseEvent.Type == 2 {
// x axis
if mouseEvent.Code == 0 {
gadgetBytes[1] = byte(mouseEvent.Value * int32(config.CursorSpeed))
gadgetBytes[2] = byte(0)
// y axis
} else if mouseEvent.Code == 1 {
gadgetBytes[2] = byte(mouseEvent.Value * int32(config.CursorSpeed))
gadgetBytes[1] = byte(0)
// scroll event
} else if mouseEvent.Code == 8 {
gadgetBytes[3] = byte(mouseEvent.Value * int32(config.ScrollSpeed))
}
elapsed := time.Since(prev_time)
period := elapsed.Seconds()
hertz := 1 / period
fmt.Printf("polling rate: %0.2fhz elapsed: %s\n", hertz, elapsed)
prev_time = time.Now()
} else if mouseEvent.Type == 0 {
gadgetBytes[1] = byte(0)
gadgetBytes[2] = byte(0)
gadgetBytes[3] = byte(0)
}
//fmt.Println("event:", mouseEvent.Type, mouseEvent.Code, mouseEvent.Value, "gadget:", gadgetBytes)
// gadgetDevice.Write(gadgetBytes)
//fmt.Println("event:", mouseEvent.Type, mouseEvent.Code, mouseEvent.Value, "gadget:", gadgetBytes, "time:", elapsed)
}
}
func getMicePaths(basePath string) ([]string, error) {
dir, err := os.Open(basePath)
if err != nil {
return nil, err
}
devices, err := dir.Readdir(0)
if err != nil {
return nil, err
}
var micePaths []string
for _, device := range devices {
if strings.Contains(device.Name(), "event-mouse") {
micePaths = append(micePaths, basePath + device.Name())
}
}
return micePaths, nil
}