diff --git a/internal/config/devices.go b/internal/config/devices.go index 2fb0e50..38d74d8 100644 --- a/internal/config/devices.go +++ b/internal/config/devices.go @@ -33,9 +33,9 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice Version: 1, }, map[evdev.EvType][]evdev.EvCode{ - evdev.EV_KEY: makeButtons(int(deviceConfig.Buttons)), - evdev.EV_ABS: makeAxes(int(deviceConfig.Axes)), - evdev.EV_REL: makeRelativeAxes(deviceConfig.RelativeAxes), + evdev.EV_KEY: makeButtons(deviceConfig.NumButtons, deviceConfig.Buttons), + evdev.EV_ABS: makeAxes(deviceConfig.NumAxes, deviceConfig.Axes), + evdev.EV_REL: makeRelativeAxes(deviceConfig.NumRelativeAxes, deviceConfig.RelativeAxes), }, ) @@ -81,12 +81,29 @@ func (parser *ConfigParser) ConnectPhysicalDevices() map[string]*evdev.InputDevi return deviceMap } -func makeButtons(numButtons int) []evdev.EvCode { +func makeButtons(numButtons int, buttonList []string) []evdev.EvCode { + if numButtons > 0 && len(buttonList) > 0 { + logger.Log("'num_buttons' and 'buttons' both specified, ignoring 'num_buttons'") + } + if numButtons > VirtualDeviceMaxButtons { numButtons = VirtualDeviceMaxButtons logger.Logf("Limiting virtual device buttons to %d", VirtualDeviceMaxButtons) } + if len(buttonList) > 0 { + buttons := make([]evdev.EvCode, len(buttonList)) + for _, codeStr := range buttonList { + code, err := parseCode(codeStr, "BTN") + if err != nil { + logger.LogError(err, "Failed to create button, skipping") + continue + } + buttons = append(buttons, code) + } + return buttons + } + buttons := make([]evdev.EvCode, numButtons) startCode := 0x120 @@ -104,7 +121,24 @@ func makeButtons(numButtons int) []evdev.EvCode { return buttons } -func makeAxes(numAxes int) []evdev.EvCode { +func makeAxes(numAxes int, axisList []string) []evdev.EvCode { + if numAxes > 0 && len(axisList) > 0 { + logger.Log("'num_axes' and 'axes' both specified, ignoring 'num_axes'") + } + + if len(axisList) > 0 { + axes := make([]evdev.EvCode, len(axisList)) + for _, codeStr := range axisList { + code, err := parseCode(codeStr, "ABS") + if err != nil { + logger.LogError(err, "Failed to create axis, skipping") + continue + } + axes = append(axes, code) + } + return axes + } + if numAxes > 8 { numAxes = 8 logger.Log("Limiting virtual device axes to 8") @@ -118,19 +152,33 @@ func makeAxes(numAxes int) []evdev.EvCode { return axes } -func makeRelativeAxes(axes []string) []evdev.EvCode { - codes := make([]evdev.EvCode, 0) - - for _, axis := range axes { - code, ok := evdev.RELFromString[axis] - - if !ok { - logger.Logf("Relative axis '%s' invalid. Skipping.", axis) - continue - } - - codes = append(codes, code) +func makeRelativeAxes(numAxes int, axisList []string) []evdev.EvCode { + if numAxes > 0 && len(axisList) > 0 { + logger.Log("'num_rel_axes' and 'rel_axes' both specified, ignoring 'num_rel_axes'") } - return codes + if len(axisList) > 0 { + axes := make([]evdev.EvCode, len(axisList)) + for _, codeStr := range axisList { + code, err := parseCode(codeStr, "REL") + if err != nil { + logger.LogError(err, "Failed to create axis, skipping") + continue + } + axes = append(axes, code) + } + return axes + } + + if numAxes > 8 { + numAxes = 8 + logger.Log("Limiting virtual device relative axes to 8") + } + + axes := make([]evdev.EvCode, numAxes) + for i := 0; i < numAxes; i++ { + axes[i] = evdev.EvCode(i) + } + + return axes } diff --git a/internal/config/schema.go b/internal/config/schema.go index d8edaf1..c869804 100644 --- a/internal/config/schema.go +++ b/internal/config/schema.go @@ -16,13 +16,16 @@ type Config struct { } type DeviceConfig struct { - Name string `yaml:"name"` - Type string `yaml:"type"` - DeviceName string `yaml:"device_name,omitempty"` - Uuid string `yaml:"uuid,omitempty"` - Buttons int `yaml:"buttons,omitempty"` - Axes int `yaml:"axes,omitempty"` - RelativeAxes []string `yaml:"rel_axes,omitempty"` + Name string `yaml:"name"` + Type string `yaml:"type"` + DeviceName string `yaml:"device_name,omitempty"` + Uuid string `yaml:"uuid,omitempty"` + NumButtons int `yaml:"num_buttons,omitempty"` + NumAxes int `yaml:"num_axes,omitempty"` + NumRelativeAxes int `yaml:"num_rel_axes"` + Buttons []string `yaml:"buttons,omitempty"` + Axes []string `yaml:"axes,omitempty"` + RelativeAxes []string `yaml:"rel_axes,omitempty"` } type RuleConfig struct {