52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
|
package xinput_devnodes
|
||
|
|
||
|
// #cgo CFLAGS: -g --std=gnu99 -pedantic -Wall
|
||
|
// #cgo LDFLAGS: -lX11 -lXi
|
||
|
// #include "devnodes.h"
|
||
|
// #include <stdlib.h>
|
||
|
// #include <string.h>
|
||
|
import "C"
|
||
|
import (
|
||
|
"fmt"
|
||
|
"unsafe"
|
||
|
"regexp"
|
||
|
)
|
||
|
|
||
|
|
||
|
func FindKeyboardDevNode() string {
|
||
|
xslaveinfos, freeMethod := GetXSlaveInfo()
|
||
|
defer freeMethod()
|
||
|
|
||
|
re := regexp.MustCompile(`(?i).*keyboard.*`)
|
||
|
for _, xslaveinfo := range xslaveinfos {
|
||
|
name, devNode := C.GoString(xslaveinfo.name), C.GoString(xslaveinfo.dev_node)
|
||
|
if re.MatchString(name) {
|
||
|
return devNode
|
||
|
}
|
||
|
}
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
|
||
|
func Print() {
|
||
|
xslaveinfos, freeMethod := GetXSlaveInfo()
|
||
|
defer freeMethod()
|
||
|
|
||
|
for _, xslaveinfo := range xslaveinfos {
|
||
|
fmt.Printf("%s = %s\n", C.GoString(xslaveinfo.name), C.GoString(xslaveinfo.dev_node))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
func GetXSlaveInfo() ([]C.XSlaveInfo, func()) {
|
||
|
xslaves := C.get_slave_info()
|
||
|
arr := (*[1 << 30]C.XSlaveInfo)(unsafe.Pointer(xslaves.slaves))
|
||
|
slice := arr[:xslaves.length:xslaves.length]
|
||
|
freeMethod := func() {
|
||
|
C.free_xslaves(xslaves)
|
||
|
}
|
||
|
|
||
|
return slice, freeMethod
|
||
|
}
|
||
|
|