forked from chbmuc/cec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.go
74 lines (65 loc) · 1.16 KB
/
callbacks.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
package cec
// #include <libcec/cecc.h>
import "C"
import (
"log"
"regexp"
"unsafe"
)
type EventType int
const (
Unknown = EventType(iota)
Pause
Play
Stop
FastForward
Rewind
)
type regexEvent struct {
re *regexp.Regexp
event EventType
}
var events = []*regexEvent{
{
regexp.MustCompile(`key pressed: pause \([0-9]+, [0-9]+\)`),
Pause,
},
{
regexp.MustCompile(`key pressed: play \([0-9]+, [0-9]+\)`),
Play,
},
{
regexp.MustCompile(`key pressed: stop \([0-9]+\) current\(ff\) duration\(0\)`),
Stop,
},
{
regexp.MustCompile(`key pressed: Fast forward \([0-9]+, [0-9]+\)`),
FastForward,
},
{
regexp.MustCompile(`key pressed: rewind \([0-9]+, [0-9]+\)`),
Rewind,
},
}
//export logMessageCallback
func logMessageCallback(c unsafe.Pointer, msg *C.cec_log_message) {
s := C.GoString(msg.message)
for _, conn := range connections {
conn.recvMsg(s)
}
log.Println(s)
}
func (c *Connection) On(e EventType, callback func()) {
c.events[e] = callback
}
func (c *Connection) recvMsg(msg string) {
e := Unknown
for _, evt := range events {
if evt.re.MatchString(msg) {
e = evt.event
}
}
if cb := c.events[e]; cb != nil {
cb()
}
}