-
Notifications
You must be signed in to change notification settings - Fork 4
/
macro.go
127 lines (105 loc) · 2.28 KB
/
macro.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
package main
import (
"fmt"
"github.com/hagen1778/macros/keyboard"
"log"
"strings"
"time"
)
type Macro struct {
cfg *config
kb *keyboard.InputDevice
mList []*macros
}
func (m *Macro) checkEvent(e *keyboard.InputEvent) macrosFunc {
if e.Type == keyboard.EV_KEY && e.Value == 1 {
for _, macros := range m.mList {
if macros.isModifiersPressed(m.kb) && macros.key == e.String() {
return macros.Run
}
}
}
return nil
}
func (m *Macro) listen() {
kb_events, err := m.kb.Listen()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Listen for %s events!\n", m.kb.Name)
for e := range kb_events {
if f := m.checkEvent(&e); f != nil {
f(m.kb, m.cfg.defaultDelay)
}
}
}
func (m *Macro) applyRules() error {
for _, rule := range m.cfg.Rules {
macros := macros{
name: rule.Name,
}
for _, v := range strings.Fields(rule.Activate) {
if keyboard.IsModifier(v) {
macros.modifiers = append(macros.modifiers, v)
continue
}
macros.key = v
}
for _, rule := range rule.Scenario {
action, err := rule.convertToAction()
if err != nil {
return fmt.Errorf("invalid rule %#v", rule)
}
macros.actions = append(macros.actions, action)
}
m.mList = append(m.mList, ¯os)
log.Printf("Macro %q registred", macros)
}
return nil
}
func Init(confPath string) {
cfg, err := loadFile(confPath)
if err != nil {
log.Fatalf("Error while loading config >> %s", err)
}
macro := Macro{
cfg: cfg,
}
macro.applyRules()
kb, err := keyboard.Init()
if err != nil {
log.Fatalf("Error while initing keyboard: %s", err)
}
macro.kb = kb
macro.listen()
}
type macrosFunc func(d *keyboard.InputDevice, dur time.Duration)
type macros struct {
name string
key string
modifiers []string
actions []runner
}
func (m *macros) Run(d *keyboard.InputDevice, dur time.Duration) {
for _, a := range m.actions {
a.Run(d)
time.Sleep(dur)
}
}
func (m *macros) isModifiersPressed(kb *keyboard.InputDevice) bool {
result := true
for _, mod := range m.modifiers {
if pressed, _ := kb.Modifiers[mod]; !pressed {
result = false
}
}
return result
}
func (m macros) String() string {
modifiers := ""
for _, mod := range m.modifiers {
modifiers += mod + " + "
}
return fmt.Sprintf("%s: %s%s", m.name, modifiers, m.key)
}