-
Notifications
You must be signed in to change notification settings - Fork 18
/
joystick_linux.go
180 lines (149 loc) · 3.59 KB
/
joystick_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
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
// +build linux
package joystick
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"strconv"
"sync"
"unsafe"
)
const (
_JS_EVENT_BUTTON uint8 = 0x01 /* button pressed/released */
_JS_EVENT_AXIS uint8 = 0x02 /* joystick moved */
_JS_EVENT_INIT uint8 = 0x80
)
var (
_JSIOCGAXES = _IOR('j', 0x11, 1) /* get number of axes */
_JSIOCGBUTTONS = _IOR('j', 0x12, 1) /* get number of buttons */
_JSIOCGNAME = func(len int) int { /* get identifier string */
return _IOR('j', 0x13, len)
}
)
type joystickImpl struct {
file *os.File
axisCount int
buttonCount int
name string
state State
mutex sync.RWMutex
readerr error
}
// Open opens the Joystick for reading, with the supplied id
//
// Under linux the id is used to construct the joystick device name:
// for example: id 0 will open device: "/dev/input/js0"
// Under Windows the id is the actual numeric id of the joystick
//
// If successful, a Joystick interface is returned which can be used to
// read the state of the joystick, else an error is returned
func Open(id int) (Joystick, error) {
f, err := os.OpenFile(fmt.Sprintf("/dev/input/js%d", id), os.O_RDONLY, 0666)
if err != nil {
return nil, err
}
var axisCount uint8 = 0
var buttCount uint8 = 0
var buffer [256]byte
ioerr := ioctl(f, _JSIOCGAXES, unsafe.Pointer(&axisCount))
if ioerr != 0 {
panic(ioerr)
}
ioerr = ioctl(f, _JSIOCGBUTTONS, unsafe.Pointer(&buttCount))
if ioerr != 0 {
panic(ioerr)
}
ioerr = ioctl(f, _JSIOCGNAME(len(buffer)-1), unsafe.Pointer(&buffer))
if ioerr != 0 {
panic(ioerr)
}
js := &joystickImpl{}
js.axisCount = int(axisCount)
js.buttonCount = int(buttCount)
js.file = f
js.name = string(buffer[:])
js.state.AxisData = make([]int, axisCount, axisCount)
go updateState(js)
return js, nil
}
func updateState(js *joystickImpl) {
var err error
var ev event
for err == nil {
ev, err = js.getEvent()
if ev.Type&_JS_EVENT_BUTTON != 0 {
js.mutex.Lock()
if ev.Value == 0 {
js.state.Buttons &= ^(1 << uint(ev.Number))
} else {
js.state.Buttons |= 1 << ev.Number
}
js.mutex.Unlock()
}
if ev.Type&_JS_EVENT_AXIS != 0 {
js.mutex.Lock()
js.state.AxisData[ev.Number] = int(ev.Value)
js.mutex.Unlock()
}
}
js.mutex.Lock()
js.readerr = err
js.mutex.Unlock()
}
func (js *joystickImpl) AxisCount() int {
return js.axisCount
}
func (js *joystickImpl) ButtonCount() int {
return js.buttonCount
}
func (js *joystickImpl) Name() string {
return js.name
}
func (js *joystickImpl) Read() (State, error) {
js.mutex.RLock()
state, err := js.state, js.readerr
js.mutex.RUnlock()
return state, err
}
func (js *joystickImpl) Close() {
js.file.Close()
}
type event struct {
Time uint32 /* event timestamp in milliseconds */
Value int16 /* value */
Type uint8 /* event type */
Number uint8 /* axis/button number */
}
func (j *event) String() string {
var Type, Number string
if j.Type&_JS_EVENT_INIT > 0 {
Type = "Init "
}
if j.Type&_JS_EVENT_BUTTON > 0 {
Type += "Button"
Number = strconv.FormatUint(uint64(j.Number), 10)
}
if j.Type&_JS_EVENT_AXIS > 0 {
Type = "Axis"
Number = "Axis " + strconv.FormatUint(uint64(j.Number), 10)
}
return fmt.Sprintf("[Time: %v, Type: %v, Number: %v, Value: %v]", j.Time, Type, Number, j.Value)
}
func (j *joystickImpl) getEvent() (event, error) {
var ev event
if j.file == nil {
panic("file is nil")
}
b := make([]byte, 8)
_, err := j.file.Read(b)
if err != nil {
return event{}, err
}
data := bytes.NewReader(b)
err = binary.Read(data, binary.LittleEndian, &ev)
if err != nil {
return event{}, err
}
return ev, nil
}