Refactor logging and start adding rule matching code.

This commit is contained in:
Anna Rose Wiggins 2025-06-27 22:35:17 -04:00
parent 8549f36c8f
commit 33837895d9
4 changed files with 92 additions and 22 deletions

27
internal/logger/logger.go Normal file
View file

@ -0,0 +1,27 @@
package logger
import (
"fmt"
"os"
)
func Log(msg string) {
fmt.Println(msg)
}
func LogIfError(err error, msg string) {
if err == nil {
return
}
fmt.Printf("%s: %s\n", msg, err.Error())
}
func FatalIfError(err error, msg string) {
if err == nil {
return
}
LogIfError(err, msg)
os.Exit(1)
}

View file

@ -0,0 +1,50 @@
package rules
import (
"git.annabunches.net/annabunches/joyful/internal/logger"
"github.com/holoplot/go-evdev"
)
type KeyMappingRule interface {
MatchEvent(*evdev.InputDevice, *evdev.InputEvent)
}
type SimpleMappingRule struct {
Input RuleTarget
Output RuleTarget
}
type ComboMappingRule struct {
Input []RuleTarget
Output RuleTarget
}
func (rule *SimpleMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent) *evdev.InputEvent {
if event.Type != evdev.EV_KEY ||
device != rule.Input.Device ||
event.Code != rule.Input.Code {
return nil
}
// how we process inverted rules depends on the event type
value := event.Value
if rule.Input.Inverted {
switch rule.Input.Type {
case evdev.EV_KEY:
if value == 0 {
value = 1
} else {
value = 0
}
default:
logger.Log("Tried to handle inverted rule for unknown event type. Skipping rule.")
return nil
}
}
return &evdev.InputEvent{
Type: rule.Output.Type,
Code: rule.Output.Code,
Value: value,
}
}

10
internal/rules/types.go Normal file
View file

@ -0,0 +1,10 @@
package rules
import "github.com/holoplot/go-evdev"
type RuleTarget struct {
Device *evdev.InputDevice
Type evdev.EvType
Code evdev.EvCode
Inverted bool
}