/
flirc.go
56 lines (51 loc) · 1012 Bytes
/
flirc.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
package main
import (
"fmt"
"github.com/jteeuwen/evdev"
log "github.com/sirupsen/logrus"
"os"
"strings"
)
const idVendorFLIRC uint16 = 0x20a0
const idProductFLIRC uint16 = 0x0001
const FLIRCName = "flirc.tv flirc"
func OpenFLIRC(grab bool) {
i := 0
var dev *evdev.Device
var err error
for {
node := fmt.Sprintf("/dev/input/event%d", i)
dev, err = evdev.Open(node)
i++
if err != nil {
if os.IsNotExist(err) {
log.Fatalln("could not find FLIRC device")
}
log.Fatalln(err)
}
if strings.TrimRight(dev.Name(), "\x00") != FLIRCName {
dev.Close()
continue
}
break
}
if grab && !dev.Grab() {
log.Warnln("failed to grab device")
}
log.Infoln("using device:", strings.TrimRight(dev.Path(), "\x00"))
for event := range dev.Inbox {
if event.Type != evdev.EvKeys {
continue
}
if event.Value != 0 {
continue
}
log.Infoln("FLIRC event", event.Code)
for i, code := range flircMap {
if int(event.Code) == code {
setState(i)
break
}
}
}
}