forked from luckfox-eng29/kvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys.go
More file actions
245 lines (220 loc) · 5.24 KB
/
keys.go
File metadata and controls
245 lines (220 loc) · 5.24 KB
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
package kvm
import (
"context"
"encoding/binary"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
"golang.org/x/sys/unix"
)
const (
evKey = 0x01
)
type keyHoldResetDetector struct {
mu sync.Mutex
pressAt map[uint16]time.Time
threshold time.Duration
now func() time.Time
afterFunc func(d time.Duration, f func()) func() bool
stop map[uint16]func() bool
triggered bool
onTrigger func(code uint16, hold time.Duration)
}
func newKeyHoldResetDetector(threshold time.Duration, now func() time.Time, afterFunc func(d time.Duration, f func()) func() bool, onTrigger func(code uint16, hold time.Duration)) *keyHoldResetDetector {
if now == nil {
now = time.Now
}
if afterFunc == nil {
afterFunc = func(d time.Duration, f func()) func() bool {
t := time.AfterFunc(d, f)
return t.Stop
}
}
return &keyHoldResetDetector{
pressAt: map[uint16]time.Time{},
threshold: threshold,
now: now,
afterFunc: afterFunc,
stop: map[uint16]func() bool{},
onTrigger: onTrigger,
}
}
func (d *keyHoldResetDetector) close() {
d.mu.Lock()
defer d.mu.Unlock()
for code, stop := range d.stop {
_ = stop()
delete(d.stop, code)
delete(d.pressAt, code)
}
}
func (d *keyHoldResetDetector) fire(code uint16) {
d.mu.Lock()
if d.triggered {
d.mu.Unlock()
return
}
d.triggered = true
t0, ok := d.pressAt[code]
now := d.now()
d.mu.Unlock()
hold := d.threshold
if ok {
hold = now.Sub(t0)
}
if d.onTrigger != nil {
d.onTrigger(code, hold)
}
}
func (d *keyHoldResetDetector) onEvent(typ uint16, code uint16, val int32) {
if typ != evKey {
return
}
switch val {
case 1, 2:
d.mu.Lock()
if d.triggered {
d.mu.Unlock()
return
}
if _, exists := d.pressAt[code]; exists {
d.mu.Unlock()
return
}
d.pressAt[code] = d.now()
d.stop[code] = d.afterFunc(d.threshold, func() { d.fire(code) })
d.mu.Unlock()
return
case 0:
d.mu.Lock()
if stop, ok := d.stop[code]; ok {
_ = stop()
delete(d.stop, code)
}
delete(d.pressAt, code)
d.mu.Unlock()
return
default:
return
}
}
func defaultInputEventSize() int {
if strconv.IntSize == 64 {
return 24
}
return 16
}
func findInputEventDeviceByName(deviceName string) (string, error) {
entries, err := os.ReadDir("/sys/class/input")
if err != nil {
return "", err
}
for _, e := range entries {
if !strings.HasPrefix(e.Name(), "event") {
continue
}
namePath := filepath.Join("/sys/class/input", e.Name(), "device/name")
b, err := os.ReadFile(namePath)
if err != nil {
continue
}
n := strings.TrimSpace(string(b))
if n == deviceName {
return filepath.Join("/dev/input", e.Name()), nil
}
}
return "", errors.New("input device not found")
}
func watchAdcKeysLongPressReset(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
}
dev, err := findInputEventDeviceByName("adc-keys")
if err != nil {
keysLogger.Warn().Err(err).Msg("adc-keys device not found")
time.Sleep(2 * time.Second)
continue
}
f, err := os.OpenFile(dev, os.O_RDONLY, 0)
if err != nil {
keysLogger.Warn().Err(err).Str("device", dev).Msg("failed to open adc-keys device")
time.Sleep(2 * time.Second)
continue
}
keysLogger.Info().Str("device", dev).Msg("watching adc-keys events")
var resetOnce sync.Once
detector := newKeyHoldResetDetector(
5*time.Second,
nil,
nil,
func(code uint16, hold time.Duration) {
resetOnce.Do(func() {
keysLogger.Warn().Uint16("code", code).Dur("hold", hold).Msg("adc-keys long press detected, resetting config")
resetConfigFileAndReboot()
})
},
)
eventSize := defaultInputEventSize()
buf := make([]byte, eventSize)
for {
select {
case <-ctx.Done():
detector.close()
_ = f.Close()
return
default:
}
_, err := io.ReadFull(f, buf)
if err != nil {
if errors.Is(err, syscall.EINVAL) {
if eventSize == 24 {
eventSize = 16
} else {
eventSize = 24
}
buf = make([]byte, eventSize)
keysLogger.Info().Str("device", dev).Int("event_size", eventSize).Msg("adc-keys switched input_event size")
continue
}
detector.close()
_ = f.Close()
keysLogger.Warn().Err(err).Str("device", dev).Msg("adc-keys read failed, reopening")
time.Sleep(500 * time.Millisecond)
break
}
typeOff, codeOff, valOff := 16, 18, 20
if eventSize == 16 {
typeOff, codeOff, valOff = 8, 10, 12
}
typ := binary.LittleEndian.Uint16(buf[typeOff : typeOff+2])
code := binary.LittleEndian.Uint16(buf[codeOff : codeOff+2])
val := int32(binary.LittleEndian.Uint32(buf[valOff : valOff+4]))
detector.onEvent(typ, code, val)
}
}
}
func resetConfigFileAndReboot() {
resetFirewallForFactory()
if err := os.Remove(configPath); err != nil && !errors.Is(err, os.ErrNotExist) {
keysLogger.Error().Err(err).Str("path", configPath).Msg("failed to delete config file")
} else {
keysLogger.Warn().Str("path", configPath).Msg("config file deleted")
}
unix.Sync()
time.Sleep(200 * time.Millisecond)
if err := unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART); err != nil {
keysLogger.Error().Err(err).Msg("syscall reboot failed, trying /sbin/reboot")
_ = exec.Command("/sbin/reboot", "-f").Run()
_ = exec.Command("reboot", "-f").Run()
}
}