Skip to content

Commit

Permalink
Add back the gamecontroller mappings database.
Browse files Browse the repository at this point in the history
This works around Ebiten's lagging behind (currently by two months -
first missing commit is from Oct 4, 2021). We want as complete mappings as
possible because there will be no input customize dialog.
  • Loading branch information
divVerent committed Dec 13, 2021
1 parent 25cdd51 commit 68720ed
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "third_party/SDL_GameControllerDB/assets/input"]
path = third_party/SDL_GameControllerDB/assets/input
url = https://github.com/gabomdq/SDL_GameControllerDB
branch = master
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ To build the game for yourself, install `git`, `golang`, `graphviz`,
``` sh
git clone https://github.com/divVerent/aaaaxy
cd aaaaxy
git submodule update --init --remote
make
```

Expand All @@ -61,6 +62,7 @@ To update and rebuild, run:
``` sh
cd aaaaxy
git pull
git submodule update --remote
make
```

Expand Down
70 changes: 50 additions & 20 deletions internal/input/gamepad.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
package input

import (
"fmt"
"io/ioutil"
"os"
"regexp"

"github.com/hajimehoshi/ebiten/v2"

"github.com/divVerent/aaaaxy/internal/flag"
"github.com/divVerent/aaaaxy/internal/log"
"github.com/divVerent/aaaaxy/internal/vfs"
)

var (
Expand Down Expand Up @@ -172,37 +175,64 @@ func gamepadScan() {
}
}

func applyAndLogGameControllerDb(config string, err error, name string) {
if os.IsNotExist(err) {
log.Infof("%v not provided - OK", name)
return
}
if err != nil {
log.Warningf("could not load %v: %v", name, err)
return
}
if config == "" {
log.Infof("%v not provided - OK", name)
return
}
applied, err := ebiten.UpdateStandardGamepadLayoutMappings(config)
if err != nil {
log.Errorf("could not add %v: %v", name, err)
} else if applied {
log.Infof("%v applied", name)
} else {
log.Infof("%v exist but are not used on this platform", name)
}
}

func readBuiltinGamepadMappings() (string, error) {
configHandle, err := vfs.Load("input", "gamecontrollerdb.txt")
if err != nil {
return "", fmt.Errorf("open: %v", err)
}
defer configHandle.Close()
configBytes, err := ioutil.ReadAll(configHandle)
if err != nil {
return "", fmt.Errorf("read: %v", err)
}
return string(configBytes), nil
}

func gamepadInit() {
// Note: we're also stripping spaces before/after a semicolon
// as a user might be putting some given they're usual in English,
// yet they're technically invalid in SDL_GameControllerDB format.
semiRE := regexp.MustCompile(`\s*;\s*`)

// Support an included gamecontrollerdb.txt override.
// Doing this because Ebiten's lags behind.
mappings, err := readBuiltinGamepadMappings()
applyAndLogGameControllerDb(mappings, err, "included gamepad mappings")

// Support ~/.config/AAAAXY/gamecontrollerdb.txt.
configBytes, err := vfs.ReadState(vfs.Config, "gamecontrollerdb.txt")
applyAndLogGameControllerDb(string(configBytes), err, "gamepad mappings from gamecontrollerdb.txt")

// Support the environment variable.
config := semiRE.ReplaceAllString(os.Getenv("SDL_GAMECONTROLLERCONFIG"), "\n")
if config != "" {
applied, err := ebiten.UpdateStandardGamepadLayoutMappings(config)
if err != nil {
log.Errorf("could not add SDL_GAMECONTROLLERCONFIG mappings: %v", err)
} else if applied {
log.Infof("SDL_GAMECONTROLLERCONFIG applied.")
} else {
log.Warningf("SDL_GAMECONTROLLERCONFIG set but not used on this platform.")
}
}
applyAndLogGameControllerDb(config, nil, "gamepad mappings from $SDL_GAMECONTROLLERCONFIG")

// Also support the flag. Note that the flag value is saved.
config = semiRE.ReplaceAllString(*gamepadOverride, "\n")
if config != "" {
applied, err := ebiten.UpdateStandardGamepadLayoutMappings(config)
if err != nil {
log.Errorf("could not add --gamepad_override mappings: %v", err)
} else if applied {
log.Infof("--gamepad_override applied.")
} else {
log.Warningf("--gamepad_override set but not used on this platform.")
}
}
applyAndLogGameControllerDb(config, nil, "gamepad mappings from --gamepad_override")
}

func gamepadEasterEggKeyState() easterEggKeyState {
Expand Down
3 changes: 3 additions & 0 deletions scripts/binary-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ $(git log --format='%w(72,2,4)- %s' "$prev"..)
EOF
vi .commitmsg

# Update gamecontroller mappings.
git submodule update --remote

# Update metainfo with current date and version already, and replace the text by a placeholder.
VERSION=$new DATE=$(date +%Y-%m-%d) MSG=$(cat .commitmsg) perl -0777 -pi -e '
use strict;
Expand Down
1 change: 1 addition & 0 deletions third_party/SDL_GameControllerDB/assets/input
Submodule input added at c4d3b5

0 comments on commit 68720ed

Please sign in to comment.