2022-01-10 14:10:29 +01:00
|
|
|
// 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"
|
2022-01-28 17:28:53 +01:00
|
|
|
"time"
|
2022-01-10 14:10:29 +01:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/gamepaddb"
|
|
|
|
)
|
|
|
|
|
2022-02-05 14:42:14 +01:00
|
|
|
type ID int
|
|
|
|
|
2022-01-10 14:10:29 +01:00
|
|
|
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 {
|
2022-01-28 19:06:56 +01:00
|
|
|
inited bool
|
2022-01-10 14:10:29 +01:00
|
|
|
gamepads []*Gamepad
|
2022-01-28 19:41:47 +01:00
|
|
|
m sync.Mutex
|
2022-01-10 14:10:29 +01:00
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
native nativeGamepads
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
type nativeGamepads interface {
|
|
|
|
init(gamepads *gamepads) error
|
|
|
|
update(gamepads *gamepads) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var theGamepads = gamepads{
|
|
|
|
native: newNativeGamepadsImpl(),
|
|
|
|
}
|
2022-01-10 14:10:29 +01:00
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// AppendGamepadIDs is concurrent-safe.
|
2022-02-05 14:42:14 +01:00
|
|
|
func AppendGamepadIDs(ids []ID) []ID {
|
2022-01-10 14:10:29 +01:00
|
|
|
return theGamepads.appendGamepadIDs(ids)
|
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// Update is concurrent-safe.
|
2022-01-22 20:20:54 +01:00
|
|
|
func Update() error {
|
|
|
|
return theGamepads.update()
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// Get is concurrent-safe.
|
2022-02-05 14:42:14 +01:00
|
|
|
func Get(id ID) *Gamepad {
|
2022-01-10 14:10:29 +01:00
|
|
|
return theGamepads.get(id)
|
|
|
|
}
|
|
|
|
|
2022-06-03 11:42:35 +02:00
|
|
|
func SetNativeWindow(nativeWindow uintptr) {
|
|
|
|
theGamepads.setNativeWindow(nativeWindow)
|
|
|
|
}
|
|
|
|
|
2022-02-05 14:42:14 +01:00
|
|
|
func (g *gamepads) appendGamepadIDs(ids []ID) []ID {
|
2022-01-28 19:41:47 +01:00
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-01-10 14:10:29 +01:00
|
|
|
for i, gp := range g.gamepads {
|
2022-01-30 10:59:25 +01:00
|
|
|
if gp != nil {
|
2022-02-05 14:42:14 +01:00
|
|
|
ids = append(ids, ID(i))
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2022-01-22 20:20:54 +01:00
|
|
|
func (g *gamepads) update() error {
|
2022-01-28 19:41:47 +01:00
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-01-28 19:06:56 +01:00
|
|
|
if !g.inited {
|
2022-06-24 11:28:02 +02:00
|
|
|
if err := g.native.init(g); err != nil {
|
2022-01-22 20:20:54 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-01-28 19:06:56 +01:00
|
|
|
g.inited = true
|
|
|
|
}
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
if err := g.native.update(g); err != nil {
|
2022-01-22 20:20:54 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:22:25 +02:00
|
|
|
// A gamepad can be detected even though there are not. Apparently, some special devices are
|
2023-01-28 11:06:38 +01:00
|
|
|
// recognized as gamepads by OSes. In this case, the number of the 'buttons' can exceed the
|
2022-03-31 19:22:25 +02:00
|
|
|
// maximum. Skip such devices as a tentative solution (#1173, #2039).
|
|
|
|
g.remove(func(gamepad *Gamepad) bool {
|
2022-09-07 08:40:52 +02:00
|
|
|
return gamepad.ButtonCount() > ButtonCount
|
2022-03-31 19:22:25 +02:00
|
|
|
})
|
|
|
|
|
2022-01-10 14:10:29 +01:00
|
|
|
for _, gp := range g.gamepads {
|
2022-01-22 20:20:54 +01:00
|
|
|
if gp == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := gp.update(g); err != nil {
|
|
|
|
return err
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 20:20:54 +01:00
|
|
|
return nil
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-02-05 14:42:14 +01:00
|
|
|
func (g *gamepads) get(id ID) *Gamepad {
|
2022-01-28 19:41:47 +01:00
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-01-10 14:10:29 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 11:42:35 +02:00
|
|
|
func (g *gamepads) setNativeWindow(nativeWindow uintptr) {
|
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-11-03 07:33:09 +01:00
|
|
|
var n any = g.native
|
2022-06-03 11:42:35 +02:00
|
|
|
if n, ok := n.(interface{ setNativeWindow(uintptr) }); ok {
|
|
|
|
n.setNativeWindow(nativeWindow)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 14:10:29 +01:00
|
|
|
type Gamepad struct {
|
|
|
|
name string
|
|
|
|
sdlID string
|
|
|
|
m sync.Mutex
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
native nativeGamepad
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 15:29:04 +01:00
|
|
|
type mappingInput interface {
|
|
|
|
Pressed() bool
|
|
|
|
Value() float64 // Normalized to range: 0..1.
|
|
|
|
}
|
|
|
|
|
|
|
|
type axisMappingInput struct {
|
|
|
|
g nativeGamepad
|
|
|
|
axis int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a axisMappingInput) Pressed() bool {
|
|
|
|
return a.g.axisValue(a.axis) > gamepaddb.ButtonPressedThreshold
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a axisMappingInput) Value() float64 {
|
|
|
|
return a.g.axisValue(a.axis)*0.5 + 0.5
|
|
|
|
}
|
|
|
|
|
|
|
|
type buttonMappingInput struct {
|
|
|
|
g nativeGamepad
|
|
|
|
button int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b buttonMappingInput) Pressed() bool {
|
|
|
|
return b.g.isButtonPressed(b.button)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b buttonMappingInput) Value() float64 {
|
|
|
|
return b.g.buttonValue(b.button)
|
|
|
|
}
|
|
|
|
|
|
|
|
type hatMappingInput struct {
|
|
|
|
g nativeGamepad
|
|
|
|
hat int
|
|
|
|
direction int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h hatMappingInput) Pressed() bool {
|
|
|
|
return h.g.hatState(h.hat)&h.direction != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h hatMappingInput) Value() float64 {
|
|
|
|
if h.Pressed() {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:03:56 +02:00
|
|
|
type nativeGamepad interface {
|
|
|
|
update(gamepads *gamepads) error
|
|
|
|
hasOwnStandardLayoutMapping() bool
|
2023-03-03 15:29:04 +01:00
|
|
|
standardAxisInOwnMapping(axis gamepaddb.StandardAxis) mappingInput
|
|
|
|
standardButtonInOwnMapping(button gamepaddb.StandardButton) mappingInput
|
2022-06-24 13:03:56 +02:00
|
|
|
axisCount() int
|
|
|
|
buttonCount() int
|
|
|
|
hatCount() int
|
|
|
|
axisValue(axis int) float64
|
|
|
|
buttonValue(button int) float64
|
|
|
|
isButtonPressed(button int) bool
|
|
|
|
hatState(hat int) int
|
|
|
|
vibrate(duration time.Duration, strongMagnitude float64, weakMagnitude float64)
|
|
|
|
}
|
|
|
|
|
2022-01-22 20:20:54 +01:00
|
|
|
func (g *Gamepad) update(gamepads *gamepads) error {
|
2022-01-10 14:10:29 +01:00
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
return g.native.update(gamepads)
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// Name is concurrent-safe.
|
2022-01-10 14:10:29 +01:00
|
|
|
func (g *Gamepad) Name() string {
|
|
|
|
// This is immutable and doesn't have to be protected by a mutex.
|
2022-01-25 13:13:59 +01:00
|
|
|
if name := gamepaddb.Name(g.sdlID); name != "" {
|
|
|
|
return name
|
|
|
|
}
|
2022-01-10 14:10:29 +01:00
|
|
|
return g.name
|
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// SDLID is concurrent-safe.
|
2022-01-10 14:10:29 +01:00
|
|
|
func (g *Gamepad) SDLID() string {
|
|
|
|
// This is immutable and doesn't have to be protected by a mutex.
|
|
|
|
return g.sdlID
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:01:46 +01:00
|
|
|
// AxisCount is concurrent-safe.
|
|
|
|
func (g *Gamepad) AxisCount() int {
|
2022-01-10 14:10:29 +01:00
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
return g.native.axisCount()
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 18:01:46 +01:00
|
|
|
// ButtonCount is concurrent-safe.
|
|
|
|
func (g *Gamepad) ButtonCount() int {
|
2022-01-10 14:10:29 +01:00
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
return g.native.buttonCount()
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 18:01:46 +01:00
|
|
|
// HatCount is concurrent-safe.
|
|
|
|
func (g *Gamepad) HatCount() int {
|
2022-01-10 14:10:29 +01:00
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
return g.native.hatCount()
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// Axis is concurrent-safe.
|
2022-01-10 14:10:29 +01:00
|
|
|
func (g *Gamepad) Axis(axis int) float64 {
|
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
return g.native.axisValue(axis)
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// Button is concurrent-safe.
|
2022-01-10 14:10:29 +01:00
|
|
|
func (g *Gamepad) Button(button int) bool {
|
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
return g.native.isButtonPressed(button)
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// Hat is concurrent-safe.
|
2022-01-10 14:10:29 +01:00
|
|
|
func (g *Gamepad) Hat(hat int) int {
|
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
return g.native.hatState(hat)
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// IsStandardLayoutAvailable is concurrent-safe.
|
2022-01-10 14:10:29 +01:00
|
|
|
func (g *Gamepad) IsStandardLayoutAvailable() bool {
|
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-01-28 17:28:53 +01:00
|
|
|
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
|
|
|
|
return true
|
|
|
|
}
|
2022-06-24 11:28:02 +02:00
|
|
|
return g.native.hasOwnStandardLayoutMapping()
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-08-11 05:35:20 +02:00
|
|
|
// IsStandardAxisAvailable is concurrent safe.
|
2023-03-03 13:17:47 +01:00
|
|
|
func (g *Gamepad) IsStandardAxisAvailable(axis gamepaddb.StandardAxis) bool {
|
2022-08-11 05:35:20 +02:00
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
|
|
|
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
|
2023-03-03 13:17:47 +01:00
|
|
|
return gamepaddb.HasStandardAxis(g.sdlID, axis)
|
2022-08-11 05:35:20 +02:00
|
|
|
}
|
2023-03-03 15:29:04 +01:00
|
|
|
return g.native.standardAxisInOwnMapping(axis) != nil
|
2022-08-11 05:35:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsStandardButtonAvailable is concurrent safe.
|
|
|
|
func (g *Gamepad) IsStandardButtonAvailable(button gamepaddb.StandardButton) bool {
|
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
|
|
|
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
|
|
|
|
return gamepaddb.HasStandardButton(g.sdlID, button)
|
|
|
|
}
|
2023-03-03 15:29:04 +01:00
|
|
|
return g.native.standardButtonInOwnMapping(button) != nil
|
2022-08-11 05:35:20 +02:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// StandardAxisValue is concurrent-safe.
|
2022-02-05 15:11:09 +01:00
|
|
|
func (g *Gamepad) StandardAxisValue(axis gamepaddb.StandardAxis) float64 {
|
2022-01-28 17:28:53 +01:00
|
|
|
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
|
|
|
|
return gamepaddb.AxisValue(g.sdlID, axis, g)
|
|
|
|
}
|
2023-03-03 15:29:04 +01:00
|
|
|
if m := g.native.standardAxisInOwnMapping(axis); m != nil {
|
|
|
|
return m.Value()*2 - 1
|
2022-01-28 17:28:53 +01:00
|
|
|
}
|
|
|
|
return 0
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// StandardButtonValue is concurrent-safe.
|
2022-02-05 15:11:09 +01:00
|
|
|
func (g *Gamepad) StandardButtonValue(button gamepaddb.StandardButton) float64 {
|
2022-01-28 17:28:53 +01:00
|
|
|
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
|
|
|
|
return gamepaddb.ButtonValue(g.sdlID, button, g)
|
|
|
|
}
|
2023-03-03 15:29:04 +01:00
|
|
|
if m := g.native.standardButtonInOwnMapping(button); m != nil {
|
|
|
|
return m.Value()
|
2022-01-28 17:28:53 +01:00
|
|
|
}
|
|
|
|
return 0
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// IsStandardButtonPressed is concurrent-safe.
|
2022-02-05 15:11:09 +01:00
|
|
|
func (g *Gamepad) IsStandardButtonPressed(button gamepaddb.StandardButton) bool {
|
2022-01-28 17:28:53 +01:00
|
|
|
if gamepaddb.HasStandardLayoutMapping(g.sdlID) {
|
|
|
|
return gamepaddb.IsButtonPressed(g.sdlID, button, g)
|
|
|
|
}
|
2023-03-03 15:29:04 +01:00
|
|
|
if m := g.native.standardButtonInOwnMapping(button); m != nil {
|
|
|
|
return m.Pressed()
|
2022-01-28 17:28:53 +01:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-28 19:48:36 +01:00
|
|
|
// Vibrate is concurrent-safe.
|
2022-01-28 17:28:53 +01:00
|
|
|
func (g *Gamepad) Vibrate(duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
|
2022-01-28 19:48:36 +01:00
|
|
|
g.m.Lock()
|
|
|
|
defer g.m.Unlock()
|
|
|
|
|
2022-06-24 11:28:02 +02:00
|
|
|
g.native.vibrate(duration, strongMagnitude, weakMagnitude)
|
2022-01-10 14:10:29 +01:00
|
|
|
}
|