From af21756cefa7e670ec31c2089e1fcfdea0ab5522 Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Tue, 5 Aug 2025 14:45:29 -0400 Subject: [PATCH 1/6] First sketch of a Rust re-implementation. --- .gitignore | 6 +- Cargo.lock | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 ++ src/main.rs | 230 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 486 insertions(+), 1 deletion(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index d163863..57d0e8c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -build/ \ No newline at end of file +build/ + +# Added by cargo + +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..76da654 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,244 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anstream" +version = "0.6.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.5.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed87a9d530bb41a67537289bafcac159cb3ee28460e0a4571123d2a778a6a882" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64f4f3f3c77c94aff3c7e9aac9a2ca1974a5adf392a8bb751e827d6d127ab966" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef4f52386a59ca4c860f7393bcf8abd8dfd91ecccc0f774635ff68e92eeef491" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "joyful" +version = "0.1.0" +dependencies = [ + "clap", +] + +[[package]] +name = "once_cell_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5535819 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "joyful" +version = "0.1.0" +edition = "2024" + +[dependencies] +clap = { version = "4.5.42", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9373da7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,230 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + #[arg(short, long, default_value = "~/.config/joyful/")] + config: String, + + #[arg(short, long)] + debug: bool, + + #[arg(long, default_value_t = 100)] + tts_volume: u8, + + #[arg(long, default_value_t = 50)] + tts_pitch: u8, + + #[arg(long, default_value_t = 50)] + tts_range: u8, + + #[arg(long, default_value_t = 175)] + tts_speed: u8, + + #[arg(long, default_value = "en")] + tts_voice: String, +} + +fn main() { + // Parse Command-line + let args = Args::parse(); + + // Parse configs + + // Initialize TTS + + // Create Virtual Devices + + // Create Physical Devices + + // Create Rules + + // Create listening threads? + + // Loop: Parse Input +} + +// package main + +// import ( +// "context" +// "fmt" +// "os" +// "strings" +// "sync" + +// "github.com/holoplot/go-evdev" +// flag "github.com/spf13/pflag" + +// "git.annabunches.net/annabunches/joyful/internal/config" +// "git.annabunches.net/annabunches/joyful/internal/logger" +// "git.annabunches.net/annabunches/joyful/internal/mappingrules" +// "git.annabunches.net/annabunches/joyful/internal/virtualdevice" +// ) + +// func getConfigDir(dir string) string { +// configDir := strings.ReplaceAll(dir, "~", "${HOME}") +// return os.ExpandEnv(configDir) +// } + +// func readConfig(configDir string) *config.ConfigParser { +// parser := &config.ConfigParser{} +// err := parser.Parse(configDir) +// logger.FatalIfError(err, "Failed to parse config") +// return parser +// } + +// func initVirtualBuffers(config *config.ConfigParser) (map[string]*virtualdevice.EventBuffer, map[*evdev.InputDevice]*virtualdevice.EventBuffer) { +// vDevices := config.CreateVirtualDevices() +// if len(vDevices) == 0 { +// logger.Log("Warning: no virtual devices found in configuration. No rules will work.") +// } + +// vBuffersByName := make(map[string]*virtualdevice.EventBuffer) +// vBuffersByDevice := make(map[*evdev.InputDevice]*virtualdevice.EventBuffer) +// for name, device := range vDevices { +// vBuffersByName[name] = virtualdevice.NewEventBuffer(device) +// vBuffersByDevice[device] = vBuffersByName[name] +// } +// return vBuffersByName, vBuffersByDevice +// } + +// // Extracts the evdev devices from a list of virtual buffers and returns them. +// func getVirtualDevices(buffers map[string]*virtualdevice.EventBuffer) map[string]*evdev.InputDevice { +// devices := make(map[string]*evdev.InputDevice) +// for name, buffer := range buffers { +// devices[name] = buffer.Device.(*evdev.InputDevice) +// } +// return devices +// } + +// func initPhysicalDevices(config *config.ConfigParser) map[string]*evdev.InputDevice { +// pDeviceMap := config.ConnectPhysicalDevices() +// if len(pDeviceMap) == 0 { +// logger.Log("Warning: no physical devices found in configuration. No rules will work.") +// } +// return pDeviceMap +// } + +// func main() { +// // parse command-line +// var configFlag string +// flag.BoolVarP(&logger.IsDebugMode, "debug", "d", false, "Output very verbose debug messages.") +// flag.StringVarP(&configFlag, "config", "c", "~/.config/joyful", "Directory to read configuration from.") +// ttsOps := addTTSFlags() +// flag.Parse() + +// // parse configs +// configDir := getConfigDir(configFlag) +// config := readConfig(configDir) + +// // initialize TTS +// tts, err := newTTS(ttsOps) +// logger.LogIfError(err, "Failed to initialize TTS") + +// // Initialize virtual devices with event buffers +// vBuffersByName, vBuffersByDevice := initVirtualBuffers(config) + +// // Initialize physical devices +// pDevices := initPhysicalDevices(config) + +// // Load the rules +// rules, eventChannel, cancel, wg := loadRules(config, pDevices, getVirtualDevices(vBuffersByName)) + +// // initialize the mode variable +// mode := config.GetModes()[0] + +// // initialize TTS phrases for modes +// for _, m := range config.GetModes() { +// tts.AddMessage(m) +// logger.LogDebugf("Added TTS message '%s'", m) +// } + +// fmt.Println("Joyful Running! Press Ctrl+C to quit. Press Enter to reload rules.") +// if len(config.GetModes()) > 1 { +// logger.Logf("Initial mode set to '%s'", mode) +// } + +// for { +// lastMode := mode +// // Get an event (blocks if necessary) +// channelEvent := <-eventChannel + +// switch channelEvent.Type { +// case ChannelEventInput: +// switch channelEvent.Event.Type { +// case evdev.EV_SYN: +// // We've received a SYN_REPORT, so now we send all pending events; since SYN_REPORTs +// // might come from multiple input devices, we'll always flush, just to be sure. +// for _, buffer := range vBuffersByName { +// buffer.SendEvents() +// } + +// case evdev.EV_KEY, evdev.EV_ABS: +// // We have a matchable event type. Check all the events +// for _, rule := range rules { +// device, outputEvent := rule.MatchEvent(channelEvent.Device, channelEvent.Event, &mode) +// if device == nil || outputEvent == nil { +// continue +// } +// vBuffersByDevice[device].AddEvent(outputEvent) +// } +// } + +// case ChannelEventTimer: +// // Timer events give us the device and event to use directly +// vBuffersByDevice[channelEvent.Device].AddEvent(channelEvent.Event) +// // If we get a timer event, flush the output device buffer immediately +// vBuffersByDevice[channelEvent.Device].SendEvents() + +// case ChannelEventReload: +// // stop existing channels +// fmt.Println("Reloading rules.") +// cancel() +// fmt.Println("Waiting for existing listeners to exit. Provide input from each of your devices.") +// wg.Wait() +// fmt.Println("Listeners exited. Parsing config.") +// config := readConfig(configDir) // reload the config +// rules, eventChannel, cancel, wg = loadRules(config, pDevices, getVirtualDevices(vBuffersByName)) +// fmt.Println("Config re-loaded. Only rule changes applied. Device and Mode changes require restart.") +// } + +// if lastMode != mode && tts != nil { +// tts.Say(mode) +// } +// } +// } + +// func loadRules( +// config *config.ConfigParser, +// pDevices map[string]*evdev.InputDevice, +// vDevices map[string]*evdev.InputDevice) ([]mappingrules.MappingRule, <-chan ChannelEvent, func(), *sync.WaitGroup) { + +// var wg sync.WaitGroup +// eventChannel := make(chan ChannelEvent, 1000) +// ctx, cancel := context.WithCancel(context.Background()) + +// // Initialize rules +// rules := config.BuildRules(pDevices, vDevices) +// logger.Logf("Created %d mapping rules.", len(rules)) + +// // start listening for events on devices and timers +// for _, device := range pDevices { +// wg.Add(1) +// go eventWatcher(device, eventChannel, ctx, &wg) +// } + +// timerCount := 0 +// for _, rule := range rules { +// if timedRule, ok := rule.(mappingrules.TimedEventEmitter); ok { +// wg.Add(1) +// go timerWatcher(timedRule, eventChannel, ctx, &wg) +// timerCount++ +// } +// } +// logger.Logf("Registered %d timers.", timerCount) + +// go consoleWatcher(eventChannel) + +// return rules, eventChannel, cancel, &wg +// } From 08aac599a60ecdab62ff27e86acda25d01be34bb Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Wed, 6 Aug 2025 15:00:50 -0400 Subject: [PATCH 2/6] More rust. I have no idea what I'm doing and I am beginning to think golang is better. --- Cargo.lock | 237 +++++++++++++++++++++++++++++++++ Cargo.toml | 3 + src/bin/evinfo.rs | 29 ++++ src/{main.rs => bin/joyful.rs} | 50 ++++++- 4 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 src/bin/evinfo.rs rename src/{main.rs => bin/joyful.rs} (83%) diff --git a/Cargo.lock b/Cargo.lock index 76da654..bbb9cf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,6 +52,47 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "bstr" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "234113d19d0d7d613b40e86fb654acf958910802bcceab913a4f9e7cda03b1a4" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + +[[package]] +name = "cfg-if" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "clap" version = "4.5.42" @@ -98,6 +139,56 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +[[package]] +name = "dirs" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys", +] + +[[package]] +name = "evdev" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3c10865aeab1a7399b3c2d6046e8dcc7f5227b656f235ed63ef5ee45a47b8f8" +dependencies = [ + "bitvec", + "cfg-if", + "libc", + "nix", +] + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "heck" version = "0.5.0" @@ -115,6 +206,42 @@ name = "joyful" version = "0.1.0" dependencies = [ "clap", + "evdev", + "shellexpand", +] + +[[package]] +name = "libc" +version = "0.2.174" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" + +[[package]] +name = "libredox" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "391290121bad3d37fbddad76d8f5d1c1c314cfc646d143d7e07a3086ddff0ce3" +dependencies = [ + "bitflags", + "libc", +] + +[[package]] +name = "memchr" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" + +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", ] [[package]] @@ -123,6 +250,21 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "os_str_bytes" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" +dependencies = [ + "memchr", +] + [[package]] name = "proc-macro2" version = "1.0.95" @@ -141,6 +283,60 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "redox_users" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4e608c6638b9c18977b00b475ac1f28d14e84b27d8d42f70e0bf1e3dec127ac" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "shellexpand" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b1fdf65dd6331831494dd616b30351c38e96e45921a27745cf98490458b90bb" +dependencies = [ + "bstr", + "dirs", + "os_str_bytes", +] + [[package]] name = "strsim" version = "0.11.1" @@ -158,6 +354,32 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-ident" version = "1.0.18" @@ -170,6 +392,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + [[package]] name = "windows-sys" version = "0.59.0" @@ -242,3 +470,12 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] diff --git a/Cargo.toml b/Cargo.toml index 5535819..a380d4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "joyful" version = "0.1.0" +description = "Joystick remapper" edition = "2024" [dependencies] clap = { version = "4.5.42", features = ["derive"] } +evdev = { version = "0.13.1" } +shellexpand = { version = "3.1.1", features = ["full"] } diff --git a/src/bin/evinfo.rs b/src/bin/evinfo.rs new file mode 100644 index 0000000..c392f44 --- /dev/null +++ b/src/bin/evinfo.rs @@ -0,0 +1,29 @@ +use std::io; + +use clap::Parser; +use evdev::Device; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + #[arg(short, long)] + verbose: bool, +} + +fn main() { + let args = Args::parse(); + let mut _verbosity = 0; + if args.verbose { + _verbosity += 1; + } + + let devices = get_devices(); + + for _device in devices.iter() { + // print_device(device); + } +} + +fn get_devices() -> Vec { + return Vec::new(); +} diff --git a/src/main.rs b/src/bin/joyful.rs similarity index 83% rename from src/main.rs rename to src/bin/joyful.rs index 9373da7..5e26148 100644 --- a/src/main.rs +++ b/src/bin/joyful.rs @@ -1,26 +1,40 @@ +use std::env; +use std::error; +use std::fs; + use clap::Parser; +use shellexpand; + +type Result = std::result::Result>; #[derive(Parser, Debug)] -#[command(version, about, long_about = None)] +#[command(version, about, long_about)] struct Args { + /// The directory that contains your YAML configuration. #[arg(short, long, default_value = "~/.config/joyful/")] config: String, + /// Print extra information to the console. #[arg(short, long)] debug: bool, + /// Volume for text-to-speech. (0-200) #[arg(long, default_value_t = 100)] tts_volume: u8, + /// Median pitch for text-to-speech. (0-100) #[arg(long, default_value_t = 50)] tts_pitch: u8, + /// Pitch range for text-to-speech. (0-100) #[arg(long, default_value_t = 50)] tts_range: u8, + /// Speaking speed for text-to-speech in words per minute. #[arg(long, default_value_t = 175)] tts_speed: u8, + /// The espeak-ng voice to use for text-to-speech. #[arg(long, default_value = "en")] tts_voice: String, } @@ -30,6 +44,7 @@ fn main() { let args = Args::parse(); // Parse configs + let config_files = get_config_files(args.config); // Initialize TTS @@ -44,6 +59,39 @@ fn main() { // Loop: Parse Input } +fn get_config_files(config_dir: String) -> Result> { + let config_dir = shellexpand::full(&config_dir)?; + let paths = fs::read_dir(config_dir)?; + + let mut files: Vec = Vec::new(); + + for path in paths { + let path = match path { + Ok(path) => { + // DEBUG + println!("{:?}", path.path()); + path.path() + } + Err(err) => { + println!("{err}"); + continue; + } + }; + + if path.ends_with("yml") || path.ends_with("yaml") { + let path = match path.to_str() { + Some(path) => path, + None => { + continue; + } + }; + files.push(path.to_string()); + } + } + + return files; +} + // package main // import ( From 3f3382ffa7360fcfae8d80b514784f2f8a6b7c09 Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Thu, 7 Aug 2025 19:02:55 -0400 Subject: [PATCH 3/6] Make evinfo build. --- src/bin/evinfo.rs | 50 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/src/bin/evinfo.rs b/src/bin/evinfo.rs index c392f44..82665f1 100644 --- a/src/bin/evinfo.rs +++ b/src/bin/evinfo.rs @@ -1,29 +1,49 @@ -use std::io; +use std::path::PathBuf; use clap::Parser; -use evdev::Device; +use evdev::raw_stream::RawDevice; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { - #[arg(short, long)] - verbose: bool, + /// Print additional information + #[arg(short, long, action = clap::ArgAction::Count)] + verbose: u8, } fn main() { let args = Args::parse(); - let mut _verbosity = 0; - if args.verbose { - _verbosity += 1; - } + let devices = evdev::raw_stream::enumerate(); + devices.for_each(|(path, dev)| match print_device(path, dev, args.verbose) { + Ok(_) => return, + Err(err) => println!("Failed to print device info: {err}"), + }); - let devices = get_devices(); - - for _device in devices.iter() { - // print_device(device); - } + println!("{}", args.verbose); } -fn get_devices() -> Vec { - return Vec::new(); +fn print_device(path: PathBuf, device: RawDevice, verbose: u8) -> Result<(), std::io::Error> { + println!( + "{}: \"{}\"", + path.to_str().unwrap_or_default(), + device.name().unwrap_or_default() + ); + + if verbose > 0 { + let input_id = device.input_id(); + println!("\tUUID:\t{}", device.unique_name().unwrap_or_default()); + println!("\tVendor:\t{}", input_id.vendor()); + println!("\tProduct:\t{}", input_id.product()); + println!("\tVersion:\t{}", input_id.version()); + } + + // if verbose > 1 { + // let absInfo = device.get_absinfo()?; + // if absInfo.count() > 0 { + // println!("\tAxis Data:"); + // absInfo.for_each(|info| println!("\t\t{} {}")); + // } + // } + + Ok(()) } From 63824510a5133dc15ed8c3cd45321c47c98e8da6 Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Fri, 8 Aug 2025 10:21:48 -0400 Subject: [PATCH 4/6] Begin implementing is_joystick_like --- src/bin/evinfo.rs | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/bin/evinfo.rs b/src/bin/evinfo.rs index 82665f1..da52482 100644 --- a/src/bin/evinfo.rs +++ b/src/bin/evinfo.rs @@ -6,7 +6,7 @@ use evdev::raw_stream::RawDevice; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { - /// Print additional information + /// Print additional information about each device. (-vv for even more verbosity) #[arg(short, long, action = clap::ArgAction::Count)] verbose: u8, } @@ -14,15 +14,35 @@ struct Args { fn main() { let args = Args::parse(); let devices = evdev::raw_stream::enumerate(); - devices.for_each(|(path, dev)| match print_device(path, dev, args.verbose) { - Ok(_) => return, - Err(err) => println!("Failed to print device info: {err}"), + devices.for_each(|(path, dev)| { + if is_joystick_like(&dev) { + print_device(path, dev, args.verbose) + } }); - - println!("{}", args.verbose); } -fn print_device(path: PathBuf, device: RawDevice, verbose: u8) -> Result<(), std::io::Error> { +// Todo: can use a macro here... +const JOYSTICK_BUTTONS: Vec { + evdev::KeyCode::BTN_TRIGGER_HAPPY1, + evdev::KeyCode::BTN_TRIGGER_HAPPY2, + evdev::KeyCode::BTN_TRIGGER_HAPPY3, + evdev::KeyCode::BTN_TRIGGER_HAPPY4, + evdev::KeyCode::BTN_TRIGGER_HAPPY5, + evdev::KeyCode::BTN_TRIGGER_HAPPY6, + evdev::KeyCode::BTN_TRIGGER_HAPPY7, + evdev::KeyCode::BTN_TRIGGER_HAPPY8, + evdev::KeyCode::BTN_TRIGGER_HAPPY9, + evdev::KeyCode::BTN_TRIGGER_HAPPY10, +} + +fn is_joystick_like(device: &RawDevice) -> bool { + + if device.supported_absolute_axes().map_or(false, |axes| axes.contains()); + + return false; +} + +fn print_device(path: PathBuf, device: RawDevice, verbose: u8) { println!( "{}: \"{}\"", path.to_str().unwrap_or_default(), @@ -31,10 +51,10 @@ fn print_device(path: PathBuf, device: RawDevice, verbose: u8) -> Result<(), std if verbose > 0 { let input_id = device.input_id(); - println!("\tUUID:\t{}", device.unique_name().unwrap_or_default()); - println!("\tVendor:\t{}", input_id.vendor()); - println!("\tProduct:\t{}", input_id.product()); - println!("\tVersion:\t{}", input_id.version()); + println!("\tUUID:\t\t'{}'", device.unique_name().unwrap_or_default()); + println!("\tVendor:\t\t'{:x}'", input_id.vendor()); + println!("\tProduct:\t'{:x}'", input_id.product()); + println!("\tVersion:\t'{}'", input_id.version()); } // if verbose > 1 { @@ -44,6 +64,4 @@ fn print_device(path: PathBuf, device: RawDevice, verbose: u8) -> Result<(), std // absInfo.for_each(|info| println!("\t\t{} {}")); // } // } - - Ok(()) } From 43bdc008a1c8e850332a83232a947c632656b2d5 Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Fri, 8 Aug 2025 11:57:03 -0400 Subject: [PATCH 5/6] Eh. --- src/bin/evinfo.rs | 51 ++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/src/bin/evinfo.rs b/src/bin/evinfo.rs index da52482..4acd9ab 100644 --- a/src/bin/evinfo.rs +++ b/src/bin/evinfo.rs @@ -14,15 +14,15 @@ struct Args { fn main() { let args = Args::parse(); let devices = evdev::raw_stream::enumerate(); - devices.for_each(|(path, dev)| { + + for (path, dev) in devices { if is_joystick_like(&dev) { print_device(path, dev, args.verbose) } - }); + } } -// Todo: can use a macro here... -const JOYSTICK_BUTTONS: Vec { +const JOYSTICK_BUTTONS: &[evdev::KeyCode] = &[ evdev::KeyCode::BTN_TRIGGER_HAPPY1, evdev::KeyCode::BTN_TRIGGER_HAPPY2, evdev::KeyCode::BTN_TRIGGER_HAPPY3, @@ -33,11 +33,21 @@ const JOYSTICK_BUTTONS: Vec { evdev::KeyCode::BTN_TRIGGER_HAPPY8, evdev::KeyCode::BTN_TRIGGER_HAPPY9, evdev::KeyCode::BTN_TRIGGER_HAPPY10, -} + evdev::KeyCode::BTN_TRIGGER_HAPPY11, +]; fn is_joystick_like(device: &RawDevice) -> bool { + if let Some(_) = device.supported_absolute_axes() { + return true; + } - if device.supported_absolute_axes().map_or(false, |axes| axes.contains()); + if let Some(keys) = device.supported_keys() { + for key in keys.iter() { + if JOYSTICK_BUTTONS.contains(&key) { + return true; + } + } + } return false; } @@ -45,23 +55,28 @@ fn is_joystick_like(device: &RawDevice) -> bool { fn print_device(path: PathBuf, device: RawDevice, verbose: u8) { println!( "{}: \"{}\"", - path.to_str().unwrap_or_default(), - device.name().unwrap_or_default() + path.to_str().unwrap_or("unknown_device_path"), + device.name().unwrap_or("unknown_device_name") ); if verbose > 0 { let input_id = device.input_id(); - println!("\tUUID:\t\t'{}'", device.unique_name().unwrap_or_default()); - println!("\tVendor:\t\t'{:x}'", input_id.vendor()); - println!("\tProduct:\t'{:x}'", input_id.product()); + println!("\tUUID:\t\t'{}'", device.unique_name().unwrap_or("n/a")); + println!("\tVendor:\t\t'0x{:x}'", input_id.vendor()); + println!("\tProduct:\t'0x{:x}'", input_id.product()); println!("\tVersion:\t'{}'", input_id.version()); } - // if verbose > 1 { - // let absInfo = device.get_absinfo()?; - // if absInfo.count() > 0 { - // println!("\tAxis Data:"); - // absInfo.for_each(|info| println!("\t\t{} {}")); - // } - // } + if verbose > 1 { + if let Ok(abs_info) = device.get_absinfo() { + if abs_info.count() > 0 { + println!("\tAxis Data:"); + abs_info.for_each(|info| println!("\t\t{} {}")); + } + } + } + + if verbose > 0 { + println!(); + } } From dde97be4a01be41cfe89f6ce64608481df375f78 Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Mon, 11 Aug 2025 11:55:16 -0400 Subject: [PATCH 6/6] Fix static list (still only partially filled out) --- src/bin/evinfo.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bin/evinfo.rs b/src/bin/evinfo.rs index 4acd9ab..a9d7648 100644 --- a/src/bin/evinfo.rs +++ b/src/bin/evinfo.rs @@ -22,7 +22,7 @@ fn main() { } } -const JOYSTICK_BUTTONS: &[evdev::KeyCode] = &[ +static JOYSTICK_BUTTONS: [evdev::KeyCode; 11] = [ evdev::KeyCode::BTN_TRIGGER_HAPPY1, evdev::KeyCode::BTN_TRIGGER_HAPPY2, evdev::KeyCode::BTN_TRIGGER_HAPPY3, @@ -71,7 +71,9 @@ fn print_device(path: PathBuf, device: RawDevice, verbose: u8) { if let Ok(abs_info) = device.get_absinfo() { if abs_info.count() > 0 { println!("\tAxis Data:"); - abs_info.for_each(|info| println!("\t\t{} {}")); + for (axis, info) in abs_info { + println!("\t\t{} {}-{}", axis, info.minimum(), info.maximum()); + } } } }