forked from jbensmann/mouseless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
407 lines (370 loc) · 10.3 KB
/
config.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package main
import (
"fmt"
"os"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Action string
const (
ActionTapHold Action = "tap-hold"
ActionTapHoldNext Action = "tap-hold-next"
ActionTapHoldNextRelease Action = "tap-hold-next-release"
ActionMulti Action = "multi"
ActionLayer Action = "layer"
ActionToggleLayer Action = "toggle-layer"
ActionReloadConfig Action = "reload-config"
ActionMove Action = "move"
ActionScroll Action = "scroll"
ActionSpeed Action = "speed"
ActionButton Action = "button"
ActionExec Action = "exec"
)
// RawConfig defines the structure of the config file.
type RawConfig struct {
Devices []string `yaml:"devices"`
StartCommand string `yaml:"startCommand"`
BaseMouseSpeed float64 `yaml:"baseMouseSpeed"`
StartMouseSpeed float64 `yaml:"startMouseSpeed"`
MouseAccelerationCurve float64 `yaml:"mouseAccelerationCurve"`
MouseAccelerationTime float64 `yaml:"mouseAccelerationTime"`
MouseDecelerationCurve float64 `yaml:"mouseDecelerationCurve"`
MouseDecelerationTime float64 `yaml:"mouseDecelerationTime"`
BaseScrollSpeed float64 `yaml:"baseScrollSpeed"`
Layers []RawLayer `yaml:"layers"`
}
type RawLayer struct {
Name string `yaml:"name"`
PassThrough *bool `yaml:"passThrough"`
Bindings map[string]string `yaml:"bindings"`
}
// Config is the parsed form of RawConfig.
type Config struct {
Devices []string
StartCommand string
BaseMouseSpeed float64
MouseAccelerationCurve float64
MouseAccelerationTime float64
MouseDecelerationCurve float64
MouseDecelerationTime float64
StartMouseSpeed float64
BaseScrollSpeed float64
Layers []*Layer
}
type Layer struct {
Name string
PassThrough bool // default true
Bindings map[uint16]Binding
WildcardBinding Binding
}
type Binding interface {
binding()
}
type BaseBinding struct {
}
func (b BaseBinding) binding() {}
type MultiBinding struct {
BaseBinding
Bindings []Binding
}
type TapHoldBinding struct {
BaseBinding
TapBinding Binding
HoldBinding Binding
TimeoutMs int64
TapOnNext bool
TapOnNextRelease bool
}
type LayerBinding struct {
BaseBinding
Layer string
}
type ToggleLayerBinding struct {
BaseBinding
Layer string
}
type ReloadConfigBinding struct {
BaseBinding
}
type KeyBinding struct {
BaseBinding
KeyCombo []uint16
}
type MoveBinding struct {
BaseBinding
X, Y float64
}
type ScrollBinding struct {
BaseBinding
X, Y float64
}
type SpeedBinding struct {
BaseBinding
Speed float64
}
type ButtonBinding struct {
BaseBinding
Button MouseButton
}
type ExecBinding struct {
BaseBinding
Command string
}
// readConfig reads and parses the configuration from the given file.
func readConfig(fileName string) (*Config, error) {
rawConfig, err := readRawConfig(fileName)
if err != nil {
return nil, err
}
config := Config{
MouseAccelerationCurve: 1.0,
MouseDecelerationCurve: 1.0,
}
config.Devices = rawConfig.Devices
config.StartCommand = rawConfig.StartCommand
config.BaseMouseSpeed = rawConfig.BaseMouseSpeed
if rawConfig.MouseAccelerationCurve > 0 {
config.MouseAccelerationCurve = rawConfig.MouseAccelerationCurve
}
config.MouseAccelerationTime = rawConfig.MouseAccelerationTime
if rawConfig.MouseDecelerationCurve > 0 {
config.MouseDecelerationCurve = rawConfig.MouseDecelerationCurve
}
config.MouseDecelerationTime = rawConfig.MouseDecelerationTime
config.StartMouseSpeed = rawConfig.StartMouseSpeed
config.BaseScrollSpeed = rawConfig.BaseScrollSpeed
for i, l := range rawConfig.Layers {
layer, err := parseLayer(l)
if err != nil {
return nil, fmt.Errorf("failed to parse layer %v : %v", i, err)
}
config.Layers = append(config.Layers, layer)
}
log.Debugf("config: %+v", config)
return &config, nil
}
// readRawConfig reads the configuration from the given file.
func readRawConfig(fileName string) (*RawConfig, error) {
var rawConfig RawConfig
file, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(file, &rawConfig)
if err != nil {
return nil, err
}
return &rawConfig, nil
}
// parseLayer parses a single RawLayer to Layer.
func parseLayer(rawLayer RawLayer) (*Layer, error) {
var layer Layer
if rawLayer.Name == "" {
return nil, fmt.Errorf("no name given")
}
layer.Name = rawLayer.Name
layer.Bindings = make(map[uint16]Binding)
if rawLayer.PassThrough == nil {
layer.PassThrough = true
} else {
layer.PassThrough = *rawLayer.PassThrough
}
if rawLayer.Bindings == nil {
rawLayer.Bindings = make(map[string]string)
}
for key, bind := range rawLayer.Bindings {
code, err := parseKey(key)
if err != nil {
return nil, fmt.Errorf("failed to parse the key '%v': %v", key, err)
}
binding, err := parseBinding(bind)
if err != nil {
return nil, fmt.Errorf("failed to parse the binding '%v': %v", bind, err)
}
if code == WildcardKey {
layer.WildcardBinding = binding
} else {
layer.Bindings[code] = binding
}
}
return &layer, nil
}
// parseBinding parses a single binding of a layer.
func parseBinding(rawBinding string) (binding Binding, err error) {
if len(rawBinding) == 0 {
return nil, fmt.Errorf("binding is empty")
}
spaceSplit := strings.Fields(rawBinding)
action := strings.TrimSpace(spaceSplit[0])
argString := strings.TrimSpace(strings.Replace(rawBinding, action, "", 1))
var args []string
if len(spaceSplit) > 0 {
for _, s := range spaceSplit[1:] {
s = strings.TrimSpace(s)
if s == "" {
continue
}
args = append(args, s)
}
}
switch action {
case string(ActionMulti):
metaArgs := strings.Split(argString, ";")
if len(metaArgs) < 2 {
return nil, fmt.Errorf("action requires at least two meta arguments (separated by ;)")
}
multiBinding := MultiBinding{}
for _, arg := range metaArgs {
b, err := parseBinding(arg)
if err != nil {
return nil, err
}
multiBinding.Bindings = append(multiBinding.Bindings, b)
}
binding = multiBinding
case string(ActionTapHold):
tapHoldBinding, err := parseTapHoldBinding(argString)
if err != nil {
return nil, err
}
tapHoldBinding.TapOnNext = false
binding = tapHoldBinding
case string(ActionTapHoldNext):
tapHoldBinding, err := parseTapHoldBinding(argString)
if err != nil {
return nil, err
}
tapHoldBinding.TapOnNext = true
binding = tapHoldBinding
case string(ActionTapHoldNextRelease):
tapHoldBinding, err := parseTapHoldBinding(argString)
if err != nil {
return nil, err
}
tapHoldBinding.TapOnNextRelease = true
binding = tapHoldBinding
case string(ActionLayer):
if len(args) != 1 {
return nil, fmt.Errorf("action requires exactly one argument")
}
binding = LayerBinding{Layer: args[0]}
case string(ActionToggleLayer):
if len(args) != 1 {
return nil, fmt.Errorf("action requires exactly one argument")
}
binding = ToggleLayerBinding{Layer: args[0]}
case string(ActionReloadConfig):
if len(args) != 0 {
return nil, fmt.Errorf("action requires zero arguments")
}
binding = ReloadConfigBinding{}
case string(ActionMove):
if len(args) != 2 {
return nil, fmt.Errorf("action requires exactly two arguments")
}
x, y := 0.0, 0.0
if x, err = strconv.ParseFloat(args[0], 64); err != nil {
return nil, fmt.Errorf("first argument must be a number")
}
if y, err = strconv.ParseFloat(args[1], 64); err != nil {
return nil, fmt.Errorf("second argument must be a number")
}
binding = MoveBinding{X: x, Y: y}
case string(ActionScroll):
if len(args) != 1 {
return nil, fmt.Errorf("action requires exactly one argument")
}
x, y := 0.0, 0.0
switch args[0] {
case "up":
y = -1
case "down":
y = +1
case "left":
x = -1
case "right":
x = +1
default:
return nil, fmt.Errorf("first argument must one of up, down, left or right")
}
binding = ScrollBinding{X: x, Y: y}
case string(ActionSpeed):
if len(args) != 1 {
return nil, fmt.Errorf("action requires exactly one argument")
}
speed := 0.0
if speed, err = strconv.ParseFloat(args[0], 64); err != nil {
return nil, fmt.Errorf("first argument must be a number")
}
binding = SpeedBinding{Speed: speed}
case string(ActionButton):
if len(args) != 1 {
return nil, fmt.Errorf("action requires exactly one argument")
}
button := MouseButton(strings.ToLower(args[0]))
if button != ButtonLeft && button != ButtonMiddle && button != ButtonRight {
return nil, fmt.Errorf("unknown button '%v'", args[0])
}
binding = ButtonBinding{Button: button}
case string(ActionExec):
if len(args) == 0 {
return nil, fmt.Errorf("action requires at least one argument")
}
binding = ExecBinding{Command: argString}
default:
combo, err := parseKeyCombo(rawBinding)
if err != nil {
return nil, fmt.Errorf("neither a valid action nor a valid key sequence")
}
binding = KeyBinding{KeyCombo: combo}
}
return binding, nil
}
func parseTapHoldBinding(argString string) (TapHoldBinding, error) {
b := TapHoldBinding{}
metaArgs := strings.Split(argString, ";")
if len(metaArgs) != 3 {
return b, fmt.Errorf("action requires exactly 3 meta arguments (separated by ;)")
}
b1, err := parseBinding(metaArgs[0])
if err != nil {
return b, err
}
b.TapBinding = b1
b2, err := parseBinding(metaArgs[1])
if err != nil {
return b, err
}
b.HoldBinding = b2
var timeout int64
timeoutStr := strings.TrimSpace(metaArgs[2])
if timeout, err = strconv.ParseInt(timeoutStr, 10, 64); err != nil {
return b, fmt.Errorf("third argument must be a number: %s", timeoutStr)
}
b.TimeoutMs = timeout
return b, nil
}
// parseKeyCombo parses a key combination of the form key1+key2+...
func parseKeyCombo(rawCombo string) (combo []uint16, err error) {
for _, key := range strings.Split(rawCombo, "+") {
code, err := parseKey(key)
if err != nil {
return combo, err
}
combo = append(combo, code)
}
return combo, nil
}
// parseKey parses a single key, which can be either the code itself or an alias.
func parseKey(key string) (code uint16, err error) {
key = strings.TrimSpace(key)
if code, ok := keyAliases[key]; ok {
return code, nil
}
if code, err := strconv.Atoi(key); err == nil {
return uint16(code), nil
}
return 0, fmt.Errorf("neither an integer nor a key alias")
}