feat(steamcfg): typed config model with layer and mode-shift resolution

Action sets/layers ordered by preset id (binding targets are id+1),
group_source_bindings, activators, radial menu items. Layer groups fall
through per input unless they change the source's mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013UzjWSBjtVZ2zi8fJxS2hH
This commit is contained in:
ziadarafatandClaude Opus 5.5 committed 2026-10-30 15:59:39 -06:00
1 parent 3d2789e6a6
commit e012bbcf16
3 files changed
+680

No files matched your search

+274
View File
@@ -0,0 +1,274 @@
// Package steamcfg models a Steam Input controller configuration
// ("controller_mappings" VDF) and answers "what does this control do right
// now?" for a given action set, layer stack and mode-shift state.
package steamcfg
import (
"fmt"
"io"
"slices"
"strconv"
"strings"
"github.com/RustyRaptor/deckhud/internal/binding"
"github.com/RustyRaptor/deckhud/internal/vdf"
)
// Config is one parsed controller configuration.
type Config struct {
Title string
Sets []*Set // action sets and layers, ordered by preset id
Groups map[int]*Group
}
// Set is an action set or, when Layer is true, an action layer on top of Parent.
type Set struct {
ID int // preset id; binding targets are ID+1
Key string
Title string
Layer bool
Parent string
Sources []SourceBinding
}
// SourceBinding attaches a group to a physical source within a set.
type SourceBinding struct {
GroupID int
Source string // "switch", "button_diamond", "right_joystick", ...
Active bool
ModeShift bool
}
// Group is a mode (dpad, radial_menu, switches, ...) with its inputs.
type Group struct {
ID int
Mode string
Name string
Layer bool
Inputs map[string][]Activator
Settings map[string]string
}
// Activator is one way of triggering an input (Full_Press, Long_Press, ...).
type Activator struct {
Kind string
Bindings []binding.Binding
Settings map[string]string
}
// Load parses a controller_mappings document.
func Load(r io.Reader) (*Config, error) {
root, err := vdf.Parse(r)
if err != nil {
return nil, err
}
cm := root.Get("controller_mappings")
if cm == nil {
return nil, fmt.Errorf("steamcfg: no controller_mappings block")
}
c := &Config{Title: cm.Str("title"), Groups: map[int]*Group{}}
for _, gn := range cm.All("group") {
g := parseGroup(gn)
c.Groups[g.ID] = g
}
byKey := map[string]*Set{}
for _, a := range cm.Get("actions").Children {
s := &Set{ID: -1, Key: a.Key, Title: a.Str("title")}
byKey[strings.ToLower(a.Key)] = s
c.Sets = append(c.Sets, s)
}
for _, a := range cm.Get("action_layers").Children {
s := &Set{ID: -1, Key: a.Key, Title: a.Str("title"), Layer: true, Parent: a.Str("parent_set_name")}
byKey[strings.ToLower(a.Key)] = s
c.Sets = append(c.Sets, s)
}
for _, p := range cm.All("preset") {
s := byKey[strings.ToLower(p.Str("name"))]
if s == nil {
continue
}
s.ID, _ = strconv.Atoi(p.Str("id"))
for _, sb := range p.Get("group_source_bindings").Children {
id, err := strconv.Atoi(sb.Key)
if err != nil {
continue
}
f := strings.Fields(sb.Value)
if len(f) == 0 {
continue
}
s.Sources = append(s.Sources, SourceBinding{
GroupID: id,
Source: f[0],
Active: slices.Contains(f[1:], "active"),
ModeShift: slices.Contains(f[1:], "modeshift"),
})
}
}
slices.SortStableFunc(c.Sets, func(a, b *Set) int { return a.ID - b.ID })
return c, nil
}
func parseGroup(n *vdf.Node) *Group {
id, _ := strconv.Atoi(n.Str("id"))
g := &Group{
ID: id,
Mode: n.Str("mode"),
Name: n.Str("name"),
Inputs: map[string][]Activator{},
Settings: leaves(n.Get("settings")),
}
g.Layer = g.Settings["layer"] == "1"
for _, in := range n.Get("inputs").Children {
var acts []Activator
for _, a := range in.Get("activators").Children {
act := Activator{Kind: a.Key, Settings: leaves(a.Get("settings"))}
for _, b := range a.Get("bindings").All("binding") {
act.Bindings = append(act.Bindings, binding.Parse(b.Value))
}
acts = append(acts, act)
}
if len(acts) > 0 {
g.Inputs[in.Key] = acts
}
}
return g
}
func leaves(n *vdf.Node) map[string]string {
m := map[string]string{}
if n == nil {
return m
}
for _, c := range n.Children {
if !c.IsBlock() {
m[c.Key] = c.Value
}
}
return m
}
// Base returns the first action set (preset id 0 in practice).
func (c *Config) Base() *Set {
for _, s := range c.Sets {
if !s.Layer {
return s
}
}
return nil
}
// SetByTarget resolves the 1-based index used by CHANGE_PRESET / *_layer bindings.
func (c *Config) SetByTarget(n int) *Set {
for _, s := range c.Sets {
if s.ID == n-1 {
return s
}
}
return nil
}
// groupFor returns the active group bound to source in set s.
func (c *Config) groupFor(s *Set, modeShift bool, source string) *Group {
var normal, shifted *Group
for _, sb := range s.Sources {
if !sb.Active || sb.Source != source {
continue
}
if sb.ModeShift {
shifted = c.Groups[sb.GroupID]
} else {
normal = c.Groups[sb.GroupID]
}
}
if modeShift && shifted != nil {
return shifted
}
return normal
}
// chain lists the groups that apply to source, topmost layer first, ending at
// the group that defines the source's mode. A layer group with no inputs and
// the same mode as the group beneath it is transparent.
func (c *Config) chain(base *Set, layers []*Set, modeShift bool, source string) []*Group {
var stack []*Group
if g := c.groupFor(base, modeShift, source); g != nil {
stack = append(stack, g)
}
for _, l := range layers {
g := c.groupFor(l, modeShift, source)
if g == nil {
continue
}
if len(stack) > 0 && g.Mode != stack[len(stack)-1].Mode {
stack = stack[:0] // a different mode replaces the source outright
}
stack = append(stack, g)
}
slices.Reverse(stack)
return stack
}
// ActiveGroup returns the group that currently defines source's mode.
func (c *Config) ActiveGroup(base *Set, layers []*Set, modeShift bool, source string) *Group {
ch := c.chain(base, layers, modeShift, source)
for _, g := range ch {
if len(g.Inputs) > 0 {
return g
}
}
if len(ch) > 0 {
return ch[0]
}
return nil
}
// Activators returns what input on source does, letting unset inputs in
// layer groups fall through to the groups below.
func (c *Config) Activators(base *Set, layers []*Set, modeShift bool, source, input string) []Activator {
for _, g := range c.chain(base, layers, modeShift, source) {
if acts, ok := g.Inputs[input]; ok {
return acts
}
}
return nil
}
// MenuItem is one entry of a radial or touch menu.
type MenuItem struct {
Index int // N from touch_menu_button_N
Input string
Label string
Icon string
Bindings []binding.Binding // bindings of the first activator
Activators []Activator
}
// MenuItems returns a menu group's items ordered by index.
func (g *Group) MenuItems() []MenuItem {
var items []MenuItem
for name, acts := range g.Inputs {
n, ok := strings.CutPrefix(name, "touch_menu_button_")
if !ok {
continue
}
idx, err := strconv.Atoi(n)
if err != nil {
continue
}
it := MenuItem{Index: idx, Input: name, Activators: acts, Bindings: acts[0].Bindings}
for _, b := range it.Bindings {
if it.Label == "" {
it.Label = b.Label
}
if it.Icon == "" {
it.Icon = b.Icon
}
}
items = append(items, it)
}
slices.SortFunc(items, func(a, b MenuItem) int { return a.Index - b.Index })
return items
}
+181
View File
@@ -0,0 +1,181 @@
package steamcfg
import (
"os"
"testing"
"github.com/RustyRaptor/deckhud/internal/binding"
)
func load(t *testing.T, name string) *Config {
t.Helper()
f, err := os.Open("../../testdata/vdf/" + name)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c, err := Load(f)
if err != nil {
t.Fatalf("Load(%s): %v", name, err)
}
return c
}
func TestLoadSetsAndLayers(t *testing.T) {
c := load(t, "layers.vdf")
if c.Title != "layers fixture" {
t.Errorf("Title = %q", c.Title)
}
if len(c.Sets) != 3 {
t.Fatalf("got %d sets, want 3", len(c.Sets))
}
want := []Set{
{ID: 0, Key: "Default", Title: "Desktop"},
{ID: 1, Key: "Preset_1000001", Title: "Gamepad"},
{ID: 2, Key: "Preset_1000002", Title: "Editing", Layer: true, Parent: "Default"},
}
for i, w := range want {
g := c.Sets[i]
if g.ID != w.ID || g.Key != w.Key || g.Title != w.Title || g.Layer != w.Layer || g.Parent != w.Parent {
t.Errorf("set %d = %+v, want %+v", i, *g, w)
}
}
if c.Base().Key != "Default" {
t.Errorf("Base = %q", c.Base().Key)
}
// Binding targets are 1-based preset ids: "hold_layer 3" means preset id 2.
if s := c.SetByTarget(3); s == nil || s.Key != "Preset_1000002" {
t.Errorf("SetByTarget(3) = %+v", s)
}
if c.SetByTarget(9) != nil || c.SetByTarget(0) != nil {
t.Error("out-of-range targets should be nil")
}
}
func TestSourceBindings(t *testing.T) {
c := load(t, "layers.vdf")
got := c.SetByTarget(1).Sources
want := []SourceBinding{
{GroupID: 0, Source: "switch", Active: true},
{GroupID: 1, Source: "right_joystick", Active: true},
{GroupID: 3, Source: "right_joystick", Active: true, ModeShift: true},
{GroupID: 5, Source: "button_diamond", Active: false},
}
if len(got) != len(want) {
t.Fatalf("got %d sources, want %d: %+v", len(got), len(want), got)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("source %d = %+v, want %+v", i, got[i], want[i])
}
}
}
func TestGroupsAndActivators(t *testing.T) {
c := load(t, "layers.vdf")
g := c.Groups[0]
if g == nil || g.Mode != "switches" || g.Layer {
t.Fatalf("group 0 = %+v", g)
}
acts := g.Inputs["button_back_right"]
if len(acts) != 2 {
t.Fatalf("button_back_right has %d activators, want 2", len(acts))
}
if acts[0].Kind != "Full_Press" || len(acts[0].Bindings) != 2 || acts[0].Bindings[1].Args[0] != "V" {
t.Errorf("Full_Press = %+v", acts[0])
}
if acts[1].Kind != "Long_Press" || acts[1].Settings["long_press_time"] != "400" {
t.Errorf("Long_Press = %+v", acts[1])
}
if sc, ok := acts[1].Bindings[0].StateChange(); !ok || sc.Op != binding.HoldLayer || sc.Target != 3 {
t.Errorf("Long_Press state change = %+v %v", sc, ok)
}
if !c.Groups[2].Layer {
t.Error("group 2 should be a layer group")
}
}
func TestMenuItemsOrderedByIndex(t *testing.T) {
c := load(t, "layers.vdf")
items := c.Groups[3].MenuItems()
if len(items) != 2 {
t.Fatalf("got %d items", len(items))
}
if items[0].Index != 1 || items[0].Label != "Copy" || items[1].Index != 2 || items[1].Label != "Paste" {
t.Errorf("items = %+v", items)
}
if items[1].Icon != "ghost_045_move_0010.png" {
t.Errorf("icon = %q", items[1].Icon)
}
if got := binding.Chord(items[0].Bindings); len(got) != 2 || got[0] != "Ctrl" {
t.Errorf("chord = %v", got)
}
}
func TestResolveWithLayersAndModeShift(t *testing.T) {
c := load(t, "layers.vdf")
base := c.Base()
layer := c.SetByTarget(3)
// Base set: R4 is Ctrl+V; L5 is Space.
if acts := c.Activators(base, nil, false, "switch", "button_back_right"); len(acts) != 2 {
t.Errorf("base R4 activators = %+v", acts)
}
if acts := c.Activators(base, nil, false, "switch", "button_back_left"); acts[0].Bindings[0].Args[0] != "SPACE" {
t.Errorf("base L5 = %+v", acts)
}
// With the layer: L5 overridden, R4 falls through to the base group.
stack := []*Set{layer}
if acts := c.Activators(base, stack, false, "switch", "button_back_left"); acts[0].Bindings[1].Args[0] != "Z" {
t.Errorf("layer L5 = %+v", acts)
}
if acts := c.Activators(base, stack, false, "switch", "button_back_right"); len(acts) != 2 {
t.Errorf("layer R4 should fall through, got %+v", acts)
}
// Inactive groups are ignored; unknown inputs are nil.
if acts := c.Activators(base, nil, false, "button_diamond", "button_a"); acts != nil {
t.Errorf("inactive group used: %+v", acts)
}
if acts := c.Activators(base, nil, false, "switch", "nope"); acts != nil {
t.Errorf("unknown input: %+v", acts)
}
// Group selection: mode shift swaps right_joystick to the radial menu.
if g := c.ActiveGroup(base, nil, false, "right_joystick"); g == nil || g.ID != 1 {
t.Errorf("right stick group = %+v", g)
}
if g := c.ActiveGroup(base, nil, true, "right_joystick"); g == nil || g.ID != 3 {
t.Errorf("mode-shifted right stick group = %+v", g)
}
// An empty layer group of a *different* mode replaces the source outright.
if g := c.ActiveGroup(base, stack, false, "right_joystick"); g == nil || g.ID != 4 {
t.Errorf("layered right stick group = %+v", g)
}
}
func TestRealDesktopConfig(t *testing.T) {
c := load(t, "desktop_neptune.vdf")
if c.Title != "d4sktop dogeeeee" || c.Base().Title != "Desktop" {
t.Fatalf("title %q base %q", c.Title, c.Base().Title)
}
g := c.ActiveGroup(c.Base(), nil, false, "right_joystick")
if g == nil || g.Mode != "radial_menu" {
t.Fatalf("right stick should be a radial menu, got %+v", g)
}
if len(g.MenuItems()) == 0 {
t.Error("radial menu has no items")
}
acts := c.Activators(c.Base(), nil, false, "switch", "button_back_right_upper")
if len(acts) == 0 {
t.Fatal("R4 has no activators")
}
if got := binding.Chord(acts[0].Bindings); len(got) != 3 || got[0] != "Meta" || got[1] != "Shift" || got[2] != "." {
t.Errorf("R4 chord = %v, want Meta Shift .", got)
}
// "CHANGE_PRESET 2" switches to the Gamepad set.
if s := c.SetByTarget(2); s == nil || s.Title != "Gamepad" {
t.Errorf("SetByTarget(2) = %+v", s)
}
}
+225
View File
@@ -0,0 +1,225 @@
// Hand-written fixture: two action sets, one layer, a mode shift and a radial menu.
"controller_mappings"
{
"version" "3"
"title" "layers fixture"
"controller_type" "controller_neptune"
"actions"
{
"Default"
{
"title" "Desktop"
}
"Preset_1000001"
{
"title" "Gamepad"
}
}
"action_layers"
{
"Preset_1000002"
{
"title" "Editing"
"set_layer" "1"
"parent_set_name" "Default"
}
}
"group"
{
"id" "0"
"mode" "switches"
"inputs"
{
"button_back_right"
{
"activators"
{
"Full_Press"
{
"bindings"
{
"binding" "key_press LEFT_CONTROL, , "
"binding" "key_press V, , "
}
}
"Long_Press"
{
"bindings"
{
"binding" "controller_action hold_layer 3 0 0, , "
}
"settings"
{
"long_press_time" "400"
}
}
}
}
"button_back_left"
{
"activators"
{
"Full_Press"
{
"bindings"
{
"binding" "key_press SPACE, , "
}
}
}
}
"left_bumper"
{
"activators"
{
"Full_Press"
{
"bindings"
{
"binding" "mode_shift right_joystick 3"
}
}
}
}
}
}
"group"
{
"id" "1"
"mode" "joystick_mouse"
"inputs"
{
}
}
"group"
{
"id" "2"
"mode" "switches"
"inputs"
{
"button_back_left"
{
"activators"
{
"Full_Press"
{
"bindings"
{
"binding" "key_press LEFT_CONTROL, , "
"binding" "key_press Z, Undo, "
}
}
}
}
}
"settings"
{
"layer" "1"
}
}
"group"
{
"id" "3"
"mode" "radial_menu"
"name" "text ops"
"inputs"
{
"touch_menu_button_2"
{
"activators"
{
"Full_Press"
{
"bindings"
{
"binding" "key_press LEFT_CONTROL, Paste, ghost_045_move_0010.png, "
"binding" "key_press V, , "
}
}
}
}
"touch_menu_button_1"
{
"activators"
{
"Full_Press"
{
"bindings"
{
"binding" "key_press LEFT_CONTROL, Copy, "
"binding" "key_press C, , "
}
}
}
}
}
"settings"
{
"touchmenu_button_fire_type" "0"
}
}
"group"
{
"id" "4"
"mode" "radial_menu"
"inputs"
{
}
"settings"
{
"layer" "1"
}
}
"group"
{
"id" "5"
"mode" "four_buttons"
"inputs"
{
"button_a"
{
"activators"
{
"Full_Press"
{
"bindings"
{
"binding" "xinput_button A, , "
}
}
}
}
}
}
"preset"
{
"id" "0"
"name" "Default"
"group_source_bindings"
{
"0" "switch active"
"1" "right_joystick active"
"3" "right_joystick active modeshift"
"5" "button_diamond inactive"
}
}
"preset"
{
"id" "1"
"name" "Preset_1000001"
"group_source_bindings"
{
"5" "button_diamond active"
}
}
"preset"
{
"id" "2"
"name" "Preset_1000002"
"group_source_bindings"
{
"2" "switch active"
"4" "right_joystick active"
}
}
}