-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
240 lines (205 loc) · 6 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
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
package main
import (
"fmt"
"github.com/jbensmann/mouseless/actions"
"github.com/jbensmann/mouseless/config"
"github.com/jbensmann/mouseless/handlers"
"github.com/jbensmann/mouseless/keyboard"
"github.com/jbensmann/mouseless/virtual"
"os"
"os/exec"
"os/user"
"path/filepath"
"time"
evdev "github.com/gvalkov/golang-evdev"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
)
const (
defaultConfigFile = ".config/mouseless/config.yaml"
)
var (
version string
configFile string
keyboardDevices []*keyboard.Device
virtualMouse *virtual.Mouse
virtualKeyboard *virtual.VirtualKeyboard
eventInChannel chan keyboard.Event
tapHoldHandler *handlers.TapHoldHandler
comboHandler *handlers.ComboHandler
reloadConfigChannel chan struct{}
)
var opts struct {
Version bool `short:"v" long:"version" description:"Show the version"`
Debug bool `short:"d" long:"debug" description:"Show verbose debug information"`
ConfigFile string `short:"c" long:"config" description:"The config file"`
}
func main() {
var err error
_, err = flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
if opts.Version {
fmt.Println(version)
os.Exit(0)
}
// init logging
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, TimestampFormat: "15:04:05.000"})
if opts.Debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
// if no config file is given, use the default one
configFile = opts.ConfigFile
if configFile == "" {
u, err := user.Current()
if err != nil {
exitError(err, "Failed to get the current user")
}
configFile = filepath.Join(u.HomeDir, defaultConfigFile)
}
log.Debugf("Using config file: %s", configFile)
conf, err := config.ReadConfig(configFile)
if err != nil {
exitError(err, "Failed to read the config file")
}
run(conf)
}
func run(conf *config.Config) {
eventInChannel = make(chan keyboard.Event, 1000)
reloadConfigChannel = make(chan struct{}, 1)
detectedKeyboardDevices := findKeyboardDevices()
// check if another instance of mouse is already running
for _, device := range detectedKeyboardDevices {
if device.Name == "mouseless" {
exitError(nil, "Found a keyboard device with name mouseless, "+
"which probably means that another instance of mouseless is already running")
}
}
// if no devices are specified, use the detected ones
if len(conf.Devices) == 0 {
for _, device := range detectedKeyboardDevices {
conf.Devices = append(conf.Devices, device.Fn)
}
if len(conf.Devices) == 0 {
exitError(nil, "No keyboard devices found")
}
}
// init virtual mouse and keyboard
var err error
virtualMouse, err = virtual.NewMouse(conf)
if err != nil {
exitError(err, "Failed to init the virtual mouse")
}
defer virtualMouse.Close()
virtualKeyboard, err = virtual.NewVirtualKeyboard()
if err != nil {
exitError(err, "Failed to init the virtual keyboard")
}
defer virtualKeyboard.Close()
// init keyboard devices
for _, dev := range conf.Devices {
kd := keyboard.NewKeyboardDevice(dev, eventInChannel)
keyboardDevices = append(keyboardDevices, kd)
go kd.ReadLoop()
}
initHandlers(conf)
if conf.StartCommand != "" {
log.Debugf("Executing start command: %s", conf.StartCommand)
cmd := exec.Command("sh", "-c", conf.StartCommand)
err := cmd.Run()
if err != nil {
exitError(err, "Execution of start command failed")
}
}
virtualMouse.StartLoop()
mainLoop()
}
func initHandlers(conf *config.Config) {
executor := actions.NewBindingExecutor(conf, virtualKeyboard, virtualMouse, reloadConfigChannel)
defaultHandler := handlers.NewDefaultHandler()
defaultHandler.SetLayerManager(executor)
defaultHandler.SetNextHandler(executor)
tapHoldHandler = handlers.NewTapHoldHandler(int64(conf.QuickTapTime))
tapHoldHandler.SetLayerManager(executor)
tapHoldHandler.SetNextHandler(defaultHandler)
comboHandler = handlers.NewComboHandler(int64(conf.ComboTime))
comboHandler.SetLayerManager(executor)
comboHandler.SetNextHandler(tapHoldHandler)
}
func mainLoop() {
checkTimer := time.NewTimer(5 * time.Second)
// listen for incoming keyboard events
for {
select {
case <-reloadConfigChannel:
reloadConfig()
case e := <-eventInChannel:
comboHandler.HandleEvent(handlers.EventBinding{Event: e})
case <-checkTimer.C:
}
// check if at least one device is opened
oneDeviceOpen := false
for _, device := range keyboardDevices {
if device.IsOpen() {
oneDeviceOpen = true
}
}
if !oneDeviceOpen {
log.Warnf("No keyboard device could be opened:")
for i, device := range keyboardDevices {
log.Warnf("Device %d: %s: %s", i+1, device.DeviceName(), device.LastOpenError())
}
time.Sleep(10 * time.Second)
}
}
}
// findKeyboardDevices finds all available keyboard input devices.
func findKeyboardDevices() []*evdev.InputDevice {
var devices []*evdev.InputDevice
devices, _ = evdev.ListInputDevices("/dev/input/event*")
// filter out the keyboard devices that have at least an A key or a 1 key
var keyboardDevices []*evdev.InputDevice
for _, dev := range devices {
for capType, codes := range dev.Capabilities {
if capType.Type == evdev.EV_KEY {
for _, code := range codes {
if code.Code == evdev.KEY_A || code.Code == evdev.KEY_KP1 {
keyboardDevices = append(keyboardDevices, dev)
break
}
}
}
}
}
// print the keyboard devices
log.Debugf("Auto detected keyboard devices:")
for _, dev := range keyboardDevices {
log.Debugf("- %s: %s\n", dev.Fn, dev.Name)
}
return keyboardDevices
}
// reloadConfig reloads the config file and updates the handlers.
// But it does not reload the keyboard devices to read from.
func reloadConfig() {
log.Infof("Reloading the config file: %s", configFile)
conf, err := config.ReadConfig(configFile)
if err != nil {
log.Warnf("Failed to read the config file: %v", err)
return
}
initHandlers(conf)
virtualMouse.SetConfig(conf)
}
func exitError(err error, msg string) {
if err != nil {
log.Errorf(msg+": %v", err)
} else {
log.Error(msg)
}
log.Error("Exiting")
os.Exit(1)
}