-
Notifications
You must be signed in to change notification settings - Fork 21
/
libcec.go
301 lines (248 loc) · 8.01 KB
/
libcec.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package cec
/*
#cgo pkg-config: libcec
//#cgo CFLAGS: -Iinclude
//#cgo LDFLAGS: -lcec
#include <stdio.h>
#include <libcec/cecc.h>
#include <stdlib.h>
ICECCallbacks g_callbacks;
// callbacks.go exports
void logMessageCallback(void *, const cec_log_message *);
void setupCallbacks(libcec_configuration *conf)
{
g_callbacks.logMessage = &logMessageCallback;
g_callbacks.keyPress = NULL;
g_callbacks.commandReceived = NULL;
g_callbacks.configurationChanged = NULL;
g_callbacks.alert = NULL;
g_callbacks.menuStateChanged = NULL;
g_callbacks.sourceActivated = NULL;
(*conf).callbacks = &g_callbacks;
}
void setName(libcec_configuration *conf, char *name)
{
snprintf((*conf).strDeviceName, 13, "%s", name);
}
*/
import "C"
import (
"encoding/hex"
"errors"
"fmt"
"log"
"strings"
"unsafe"
)
// Connection class
type Connection struct {
connection C.libcec_connection_t
}
type cecAdapter struct {
Path string
Comm string
}
func cecInit(deviceName string, printLogs bool) (C.libcec_connection_t, error) {
var connection C.libcec_connection_t
var conf C.libcec_configuration
conf.clientVersion = C.uint32_t(C.LIBCEC_VERSION_CURRENT)
conf.deviceTypes.types[0] = C.CEC_DEVICE_TYPE_RECORDING_DEVICE
C.setName(&conf, C.CString(deviceName))
if printLogs {
C.setupCallbacks(&conf)
}
connection = C.libcec_initialise(&conf)
if connection == C.libcec_connection_t(nil) {
return connection, errors.New("Failed to init CEC")
}
return connection, nil
}
func getAdapter(connection C.libcec_connection_t, name string) (cecAdapter, error) {
var adapter cecAdapter
var deviceList [10]C.cec_adapter
devicesFound := int(C.libcec_find_adapters(connection, &deviceList[0], 10, nil))
for i := 0; i < devicesFound; i++ {
device := deviceList[i]
adapter.Path = C.GoStringN(&device.path[0], 1024)
adapter.Comm = C.GoStringN(&device.comm[0], 1024)
if strings.Contains(adapter.Path, name) || strings.Contains(adapter.Comm, name) {
return adapter, nil
}
}
return adapter, errors.New("No Device Found")
}
func openAdapter(connection C.libcec_connection_t, adapter cecAdapter) error {
C.libcec_init_video_standalone(connection)
result := C.libcec_open(connection, C.CString(adapter.Comm), C.CEC_DEFAULT_CONNECT_TIMEOUT)
if result < 1 {
return errors.New("Failed to open adapter")
}
return nil
}
func (c *Connection) GetLibInfo() string {
return C.GoString(C.libcec_get_lib_info(c.connection))
}
// Transmit CEC command - command is encoded as a hex string with
// colons (e.g. "40:04")
func (c *Connection) Transmit(command string) {
var cecCommand C.cec_command
cmd, err := hex.DecodeString(removeSeparators(command))
if err != nil {
log.Fatal(err)
}
cmdLen := len(cmd)
if cmdLen > 0 {
cecCommand.initiator = C.cec_logical_address((cmd[0] >> 4) & 0xF)
cecCommand.destination = C.cec_logical_address(cmd[0] & 0xF)
if cmdLen > 1 {
cecCommand.opcode_set = 1
cecCommand.opcode = C.cec_opcode(cmd[1])
} else {
cecCommand.opcode_set = 0
}
if cmdLen > 2 {
cecCommand.parameters.size = C.uint8_t(cmdLen - 2)
for i := 0; i < cmdLen-2; i++ {
cecCommand.parameters.data[i] = C.uint8_t(cmd[i+2])
}
} else {
cecCommand.parameters.size = 0
}
}
C.libcec_transmit(c.connection, (*C.cec_command)(&cecCommand))
}
// Destroy - destroy the cec connection
func (c *Connection) Destroy() {
C.libcec_destroy(c.connection)
}
// PowerOn - power on the device with the given logical address
func (c *Connection) PowerOn(address int) error {
if C.libcec_power_on_devices(c.connection, C.cec_logical_address(address)) != 0 {
return errors.New("Error in cec_power_on_devices")
}
return nil
}
// Standby - put the device with the given address in standby mode
func (c *Connection) Standby(address int) error {
if C.libcec_standby_devices(c.connection, C.cec_logical_address(address)) != 0 {
return errors.New("Error in cec_standby_devices")
}
return nil
}
// VolumeUp - send a volume up command to the amp if present
func (c *Connection) VolumeUp() error {
if C.libcec_volume_up(c.connection, 1) != 0 {
return errors.New("Error in cec_volume_up")
}
return nil
}
// VolumeDown - send a volume down command to the amp if present
func (c *Connection) VolumeDown() error {
if C.libcec_volume_down(c.connection, 1) != 0 {
return errors.New("Error in cec_volume_down")
}
return nil
}
// Mute - send a mute/unmute command to the amp if present
func (c *Connection) Mute() error {
if C.libcec_mute_audio(c.connection, 1) != 0 {
return errors.New("Error in cec_mute_audio")
}
return nil
}
// KeyPress - send a key press (down) command code to the given address
func (c *Connection) KeyPress(address int, key int) error {
if C.libcec_send_keypress(c.connection, C.cec_logical_address(address), C.cec_user_control_code(key), 1) != 1 {
return errors.New("Error in cec_send_keypress")
}
return nil
}
// KeyRelease - send a key releas command to the given address
func (c *Connection) KeyRelease(address int) error {
if C.libcec_send_key_release(c.connection, C.cec_logical_address(address), 1) != 1 {
return errors.New("Error in cec_send_key_release")
}
return nil
}
// GetActiveDevices - returns an array of active devices
func (c *Connection) GetActiveDevices() [16]bool {
var devices [16]bool
result := C.libcec_get_active_devices(c.connection)
for i := 0; i < 16; i++ {
if int(result.addresses[i]) > 0 {
devices[i] = true
}
}
return devices
}
// GetDeviceOSDName - get the OSD name of the specified device
func (c *Connection) GetDeviceOSDName(address int) string {
name := make([]byte, 14)
C.libcec_get_device_osd_name(c.connection, C.cec_logical_address(address), (*C.char)(unsafe.Pointer(&name[0])))
return string(name)
}
// IsActiveSource - check if the device at the given address is the active source
func (c *Connection) IsActiveSource(address int) bool {
result := C.libcec_is_active_source(c.connection, C.cec_logical_address(address))
if int(result) != 0 {
return true
}
return false
}
// GetDeviceVendorID - Get the Vendor-ID of the device at the given address
func (c *Connection) GetDeviceVendorID(address int) uint64 {
result := C.libcec_get_device_vendor_id(c.connection, C.cec_logical_address(address))
return uint64(result)
}
// GetDevicePhysicalAddress - Get the physical address of the device at
// the given logical address
func (c *Connection) GetDevicePhysicalAddress(address int) string {
result := C.libcec_get_device_physical_address(c.connection, C.cec_logical_address(address))
return fmt.Sprintf("%x.%x.%x.%x", (uint(result)>>12)&0xf, (uint(result)>>8)&0xf, (uint(result)>>4)&0xf, uint(result)&0xf)
}
// GetDevicePowerStatus - Get the power status of the device at the
// given address
func (c *Connection) GetDevicePowerStatus(address int) string {
result := C.libcec_get_device_power_status(c.connection, C.cec_logical_address(address))
// C.CEC_POWER_STATUS_UNKNOWN == error
if int(result) == C.CEC_POWER_STATUS_ON {
return "on"
} else if int(result) == C.CEC_POWER_STATUS_STANDBY {
return "standby"
} else if int(result) == C.CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON {
return "starting"
} else if int(result) == C.CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY {
return "shutting down"
} else {
return ""
}
}
func (c *Connection) GetDeviceCecVersion(address int) string {
result := int(C.libcec_get_device_cec_version(c.connection, C.cec_logical_address(address)))
if result == C.CEC_VERSION_1_2 {
return "1.2"
} else if result == C.CEC_VERSION_1_2A {
return "1.2a"
} else if result == C.CEC_VERSION_1_3 {
return "1.3"
} else if result == C.CEC_VERSION_1_3A {
return "1.3a"
} else if result == C.CEC_VERSION_1_4 {
return "1.4"
} else if result == C.CEC_VERSION_2_0 {
return "2.0"
} else if result == C.CEC_VERSION_UNKNOWN {
return "unknown"
}
return ""
}
func (c *Connection) GetDeviceMenuLanguage(address int) string {
lang := make([]byte, 4)
result := (*C.char)(unsafe.Pointer(&lang))
C.libcec_get_device_menu_language(c.connection, C.cec_logical_address(address), result)
resultString := C.GoString(result)
if resultString == "" {
return "unknown"
}
return resultString
}