-
-
Notifications
You must be signed in to change notification settings - Fork 623
/
gamepad.go
289 lines (240 loc) · 5.88 KB
/
gamepad.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
// Copyright 2022 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gamepad
import (
"sync"
"time"
"github.com/hajimehoshi/ebiten/v2/internal/gamepaddb"
)
type ID int
const (
hatCentered = 0
hatUp = 1
hatRight = 2
hatDown = 4
hatLeft = 8
hatRightUp = hatRight | hatUp
hatRightDown = hatRight | hatDown
hatLeftUp = hatLeft | hatUp
hatLeftDown = hatLeft | hatDown
)
type gamepads struct {
inited bool
gamepads []*Gamepad
m sync.Mutex
nativeGamepads
}
var theGamepads gamepads
// AppendGamepadIDs is concurrent-safe.
func AppendGamepadIDs(ids []ID) []ID {
return theGamepads.appendGamepadIDs(ids)
}
// Update is concurrent-safe.
func Update() error {
return theGamepads.update()
}
// Get is concurrent-safe.
func Get(id ID) *Gamepad {
return theGamepads.get(id)
}
func (g *gamepads) appendGamepadIDs(ids []ID) []ID {
g.m.Lock()
defer g.m.Unlock()
for i, gp := range g.gamepads {
if gp != nil {
ids = append(ids, ID(i))
}
}
return ids
}
func (g *gamepads) update() error {
g.m.Lock()
defer g.m.Unlock()
if !g.inited {
if err := g.nativeGamepads.init(g); err != nil {
return err
}
g.inited = true
}
if err := g.nativeGamepads.update(g); err != nil {
return err
}
// A gamepad can be detected even though there are not. Apparently, some special devices are
// recognized as gamepads by OSes. In this case, the number of the 'buttons' can exceeds the
// maximum. Skip such devices as a tentative solution (#1173, #2039).
g.remove(func(gamepad *Gamepad) bool {
return gamepad.ButtonCount() > ButtonNum
})
for _, gp := range g.gamepads {
if gp == nil {
continue
}
if err := gp.update(g); err != nil {
return err
}
}
return nil
}
func (g *gamepads) get(id ID) *Gamepad {
g.m.Lock()
defer g.m.Unlock()
if id < 0 || int(id) >= len(g.gamepads) {
return nil
}
return g.gamepads[id]
}
func (g *gamepads) find(cond func(*Gamepad) bool) *Gamepad {
for _, gp := range g.gamepads {
if gp == nil {
continue
}
if cond(gp) {
return gp
}
}
return nil
}
func (g *gamepads) add(name, sdlID string) *Gamepad {
for i, gp := range g.gamepads {
if gp == nil {
gp := &Gamepad{
name: name,
sdlID: sdlID,
}
g.gamepads[i] = gp
return gp
}
}
gp := &Gamepad{
name: name,
sdlID: sdlID,
}
g.gamepads = append(g.gamepads, gp)
return gp
}
func (g *gamepads) remove(cond func(*Gamepad) bool) {
for i, gp := range g.gamepads {
if gp == nil {
continue
}
if cond(gp) {
g.gamepads[i] = nil
}
}
}
type Gamepad struct {
name string
sdlID string
m sync.Mutex
nativeGamepad
}
func (g *Gamepad) update(gamepads *gamepads) error {
g.m.Lock()
defer g.m.Unlock()
return g.nativeGamepad.update(gamepads)
}
// Name is concurrent-safe.
func (g *Gamepad) Name() string {
// This is immutable and doesn't have to be protected by a mutex.
if name := gamepaddb.Name(g.sdlID); name != "" {
return name
}
return g.name
}
// SDLID is concurrent-safe.
func (g *Gamepad) SDLID() string {
// This is immutable and doesn't have to be protected by a mutex.
return g.sdlID
}
// AxisCount is concurrent-safe.
func (g *Gamepad) AxisCount() int {
g.m.Lock()
defer g.m.Unlock()
return g.nativeGamepad.axisCount()
}
// ButtonCount is concurrent-safe.
func (g *Gamepad) ButtonCount() int {
g.m.Lock()
defer g.m.Unlock()
return g.nativeGamepad.buttonCount()
}
// HatCount is concurrent-safe.
func (g *Gamepad) HatCount() int {
g.m.Lock()
defer g.m.Unlock()
return g.nativeGamepad.hatCount()
}
// Axis is concurrent-safe.
func (g *Gamepad) Axis(axis int) float64 {
g.m.Lock()
defer g.m.Unlock()
return g.nativeGamepad.axisValue(axis)
}
// Button is concurrent-safe.
func (g *Gamepad) Button(button int) bool {
g.m.Lock()
defer g.m.Unlock()
return g.nativeGamepad.isButtonPressed(button)
}
// Hat is concurrent-safe.
func (g *Gamepad) Hat(hat int) int {
g.m.Lock()
defer g.m.Unlock()
return g.nativeGamepad.hatState(hat)
}
// IsStandardLayoutAvailable is concurrent-safe.
func (g *Gamepad) IsStandardLayoutAvailable() bool {
g.m.Lock()
defer g.m.Unlock()
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
return true
}
return g.hasOwnStandardLayoutMapping()
}
// StandardAxisValue is concurrent-safe.
func (g *Gamepad) StandardAxisValue(axis gamepaddb.StandardAxis) float64 {
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
return gamepaddb.AxisValue(g.sdlID, axis, g)
}
if g.hasOwnStandardLayoutMapping() {
return g.nativeGamepad.axisValue(int(axis))
}
return 0
}
// StandardButtonValue is concurrent-safe.
func (g *Gamepad) StandardButtonValue(button gamepaddb.StandardButton) float64 {
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
return gamepaddb.ButtonValue(g.sdlID, button, g)
}
if g.hasOwnStandardLayoutMapping() {
return g.nativeGamepad.buttonValue(int(button))
}
return 0
}
// IsStandardButtonPressed is concurrent-safe.
func (g *Gamepad) IsStandardButtonPressed(button gamepaddb.StandardButton) bool {
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
return gamepaddb.IsButtonPressed(g.sdlID, button, g)
}
if g.hasOwnStandardLayoutMapping() {
return g.nativeGamepad.isButtonPressed(int(button))
}
return false
}
// Vibrate is concurrent-safe.
func (g *Gamepad) Vibrate(duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
g.m.Lock()
defer g.m.Unlock()
g.nativeGamepad.vibrate(duration, strongMagnitude, weakMagnitude)
}