-
Notifications
You must be signed in to change notification settings - Fork 7
/
joysticks_linux.go
85 lines (75 loc) · 2.54 KB
/
joysticks_linux.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
// +build linux
package joysticks
import (
"encoding/binary"
"io"
"os"
"strconv"
"time"
)
// see; https://www.kernel.org/doc/Documentation/input/joystick-api.txt
type osEventRecord struct {
Time uint32 // event timestamp, unknown base, in milliseconds 32bit, so about a month
Value int16 // value
Type uint8 // event type
Index uint8 // axis/button
}
const maxValue = 1<<15 - 1
// common path root, so Connect and DeviceExists are not thread safe.
var inputPathSlice = []byte("/dev/input/js ")[0:13]
// see if Device exists.
func DeviceExists(index uint8) bool {
_,err:=os.Stat(string(strconv.AppendUint(inputPathSlice, uint64(index-1), 10)))
return err==nil
}
// Connect sets up a go routine that puts a joysticks events onto registered channels.
// to register channels use the returned HID object's On<xxx>(index) methods.
// Note: only one event, of each type '<xxx>', for each 'index', so re-registering, or deleting, an event stops events going on the old channel.
// It Needs the HID objects ParcelOutEvents() method to be running to perform routing.(so usually in a go routine.)
func Connect(index int) (d *HID) {
r, e := os.OpenFile(string(strconv.AppendUint(inputPathSlice, uint64(index-1), 10)), os.O_RDWR, 0)
if e != nil {
return nil
}
d = &HID{make(chan osEventRecord), make(map[uint8]button), make(map[uint8]hatAxis), make(map[eventSignature]chan Event)}
// start thread to read joystick events to the joystick.state osEvent channel
go eventPipe(r, d.OSEvents)
d.populate()
return d
}
// fill in the joysticks available events from the synthetic events burst produced initially by the driver.
func (d HID) populate() {
for buttonNumber, hatNumber, axisNumber := 1, 1, 1; ; {
evt := <-d.OSEvents
switch evt.Type {
case 0x81:
d.Buttons[evt.Index] = button{uint8(buttonNumber), toDuration(evt.Time), evt.Value != 0}
buttonNumber += 1
case 0x82:
d.HatAxes[evt.Index] = hatAxis{uint8(hatNumber), uint8(axisNumber), false, toDuration(evt.Time), float32(evt.Value) / maxValue}
axisNumber += 1
if axisNumber > 2 {
axisNumber = 1
hatNumber += 1
}
default:
go func() { d.OSEvents <- evt }() // have to consume a real event to know we reached the end of the synthetic burst, so refire it.
return
}
}
return
}
// pipe any readable events onto channel.
func eventPipe(r io.Reader, c chan osEventRecord) {
var evt osEventRecord
for {
if binary.Read(r, binary.LittleEndian, &evt) != nil {
close(c)
return
}
c <- evt
}
}
func toDuration(m uint32) time.Duration {
return time.Duration(m) * 1000000
}