-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeycalibrator.go
196 lines (184 loc) · 5.1 KB
/
keycalibrator.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/JoshuaDoes/json"
)
type MenuKeycodeBinding struct {
Keycode uint16 `json:"keycode"`
Action string `json:"action"`
OnRelease bool `json:"onRelease"`
}
func bindKeys() {
for keyboard, bindings := range keyCalibration {
kl, err := NewKeycodeListener(keyboard)
if err != nil {
panic(fmt.Sprintf("error listening to keyboard %s: %v", keyboard, err))
}
for _, binding := range bindings {
var action func()
switch binding.Action {
case "prevItem":
action = menuEngine.PrevItem
case "nextItem":
action = menuEngine.NextItem
case "selectItem":
action = menuEngine.Action
default:
panic("unknown action: " + binding.Action)
}
kl.Bind(binding.Keycode, binding.OnRelease, action)
}
go kl.Run()
}
}
type KeyCalibration struct {
Ready bool
Action string
KLs []*KeycodeListener
}
func (kc *KeyCalibration) Input(keyboard string, keycode uint16, onRelease bool) {
if !kc.Ready {
os.Exit(0)
}
if kc.Action == "" || kc.Action == "cancel" {
kc.Action = ""
return
}
if onRelease {
return
}
if keyCalibration[keyboard] == nil {
keyCalibration[keyboard] = make([]*MenuKeycodeBinding, 0)
}
keyCalibration[keyboard] = append(keyCalibration[keyboard], &MenuKeycodeBinding{
Keycode: keycode,
Action: kc.Action,
OnRelease: true,
})
kc.Action = ""
}
func calibrate() {
//Generate a key calibration file if one doesn't exist yet
calibrator := &KeyCalibration{KLs: make([]*KeycodeListener, 0)}
//Get a list of keyboards
keyboards := make([]string, 0)
err := filepath.Walk("/dev/input", func(path string, info os.FileInfo, err error) error {
if len(path) < 16 || string(path[:16]) != "/dev/input/event" {
return nil
}
keyboards = append(keyboards, path)
return nil
})
if err != nil {
panic(fmt.Sprintf("error walking inputs: %v", err))
}
//Bind all keyboards to calibrator input
for _, keyboard := range keyboards {
kl, err := NewKeycodeListener(keyboard)
if err != nil {
panic(fmt.Sprintf("error listening to walked keyboard %s: %v", keyboard, err))
}
kl.RootBind = calibrator.Input
calibrator.KLs = append(calibrator.KLs, kl)
go kl.Run()
}
//Start calibrating!
stages := 6
for stage := 0; stage < stages; stage++ {
switch stage {
case 0:
keyCalibrationJSON, err := ioutil.ReadFile(keyCalibrationFile)
if err == nil {
keyCalibration = make(map[string][]*MenuKeycodeBinding)
err = json.Unmarshal(keyCalibrationJSON, &keyCalibration)
if err != nil {
stage = 1
continue
}
clear(3)
fmt.Println("\t\tYou've calibrated before!\n")
time.Sleep(time.Second * 2)
fmt.Println("\t\tPress any key within\n\t\t5 seconds to recalibrate.\n")
calibrator.Ready = true
calibrator.Action = "cancel"
timeout := time.Now()
for calibrator.Action != "" {
if time.Now().Sub(timeout).Seconds() > 5 {
break
}
time.Sleep(time.Millisecond * 100)
}
if time.Now().Sub(timeout).Seconds() < 5 {
calibrator.Action = ""
fmt.Println("\t\tRecalibration time!")
time.Sleep(time.Second * 2)
continue
}
stage = stages-1 //Skip to the end of the stages
}
case 1:
calibrator.Ready = false
keyCalibration = make(map[string][]*MenuKeycodeBinding)
clear(5)
fmt.Println("\t\tWelcome to the calibrator!\n")
fmt.Println("\t\tPress any key to cancel.\n")
time.Sleep(time.Second * 2)
fmt.Println("\t\tControllers and remotes\n\t\tare also supported.\n")
time.Sleep(time.Second * 2)
fmt.Println("\t\tThis is a guided process.\n")
time.Sleep(time.Second * 2)
fmt.Println("\t\tGet ready!\n")
time.Sleep(time.Second * 3)
case 2:
clear(1)
calibrator.Ready = true
calibrator.Action = "nextItem"
fmt.Printf("\n")
fmt.Println("\t\tPress any key to use to\n\t\tnavigate down in a menu.\n")
fmt.Println("\t\t\tRecommended: volume down")
for calibrator.Action != "" {
}
case 3:
calibrator.Action = "prevItem"
fmt.Printf("\n")
fmt.Println("\t\tPress any key to use to\n\t\tnavigate up in a menu.\n")
fmt.Println("\t\t\tRecommended: volume up")
for calibrator.Action != "" {
}
case 4:
calibrator.Action = "selectItem"
fmt.Printf("\n")
fmt.Println("\t\tPress any key to use to\n\t\tselect a menu item.\n")
fmt.Println("\t\t\tRecommended: touch screen")
for calibrator.Action != "" {
}
case 5:
clear(5)
fmt.Println("\t\tSaving results...\n")
keyboards, err := json.Marshal(keyCalibration, true)
if err != nil {
panic(fmt.Sprintf("error encoding calibration results: %v", err))
}
keyboardsFile, err := os.Create(keyCalibrationFile)
if err != nil {
panic(fmt.Sprintf("error creating calibration file: %v", err))
}
defer keyboardsFile.Close()
_, err = keyboardsFile.Write(keyboards)
if err != nil {
panic(fmt.Sprintf("error writing calibration file: %v", err))
}
//fmt.Println(string(keyboards))
fmt.Println("\t\tCalibration complete!")
time.Sleep(time.Second * 2)
//calibrator.Ready = false
}
}
for i := 0; i < len(calibrator.KLs); i++ {
calibrator.KLs[i].Close()
}
}