-
Notifications
You must be signed in to change notification settings - Fork 5
/
uinput_utils.go
101 lines (77 loc) · 2.06 KB
/
uinput_utils.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
package uinput
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"syscall"
)
func ioctl(df *os.File, op, arg uintptr) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, df.Fd(), op, arg)
if err != 0 {
return syscall.Errno(err)
}
return nil
}
func emitEvent(devFile *os.File, typ uint16, code uint16, value int32) error {
var ie inputEvent
ie.Type = typ
ie.Code = code
ie.Value = value
buf, err := inputEventToBuffer(ie)
if err != nil {
return fmt.Errorf("Could not write inputEvent to buffer: %v", err)
}
_, err = devFile.Write(buf)
if err != nil {
return fmt.Errorf("Could not write inputEvent to device: %v", err)
}
return nil
}
func emitAbsEvent(devFile *os.File, xPos int32, yPos int32) error {
var ie [2]inputEvent
ie[0].Type = EvAbs
ie[0].Code = AbsX
ie[0].Value = xPos
ie[1].Type = EvAbs
ie[1].Code = AbsY
ie[1].Value = yPos
buf, err := inputEventToBuffer(ie[0])
if err != nil {
return fmt.Errorf("Could not write inputEvent[0] to buffer: %v", err)
}
_, err = devFile.Write(buf)
if err != nil {
return fmt.Errorf("Could write inputEvent[0] to device: %v", err)
}
buf, err = inputEventToBuffer(ie[1])
if err != nil {
return fmt.Errorf("Could not write inputEvent[1] to buffer: %v", err)
}
_, err = devFile.Write(buf)
if err != nil {
return fmt.Errorf("Could write inputEvent[1] to device: %v", err)
}
return nil
}
func uinputSetupNameToBytes(name []byte) (uinputName [uinputMaxNameSize]byte) {
var bytesName [uinputMaxNameSize]byte
copy(bytesName[:], name)
return bytesName
}
func inputEventToBuffer(iev inputEvent) (buffer []byte, err error) {
buf := new(bytes.Buffer)
err = binary.Write(buf, binary.LittleEndian, iev)
if err != nil {
return nil, fmt.Errorf("Could not write inputEvent to buffer: %v", err)
}
return buf.Bytes(), nil
}
func uinputUserDevToBuffer(uud uinputUserDev) (buffer []byte, err error) {
buf := new(bytes.Buffer)
err = binary.Write(buf, binary.LittleEndian, uud)
if err != nil {
return nil, fmt.Errorf("Could not write uinputUserDev to buffer: %v", err)
}
return buf.Bytes(), nil
}