Additional logging, translate other keys

This commit is contained in:
Gabriel Simmer 2023-04-23 17:45:20 +01:00
parent 89e9d13d64
commit 68b8d4db3d
Signed by: arch
GPG key ID: C81B106D46C5B875
3 changed files with 98 additions and 25 deletions

81
Cargo.lock generated
View file

@ -30,12 +30,6 @@ dependencies = [
"iovec", "iovec",
] ]
[[package]]
name = "cc"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "0.1.10" version = "0.1.10"
@ -72,6 +66,12 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.9.3" version = "1.9.3"
@ -84,13 +84,14 @@ dependencies = [
[[package]] [[package]]
name = "input" name = "input"
version = "0.6.0" version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f68c2be575d5695a03d446acd1016b1665b26172fb37a75300459abeffba09d1" checksum = "f95640ef27dac9b23ef1fbd760c67a88ce3cab2143a2c18390e71f39c53b815f"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"input-sys", "input-sys",
"libc", "libc",
"log",
"udev", "udev",
] ]
@ -105,14 +106,15 @@ dependencies = [
[[package]] [[package]]
name = "inputbot" name = "inputbot"
version = "0.5.1" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/obv-mikhail/InputBot?branch=develop#f5e94cad04779f0d43ebcf07ef313ead5c1ef233"
checksum = "8bc90284daff09ee1df087ae5f74ac724749dc26ed6e9fce7fd69e11e2a44284"
dependencies = [ dependencies = [
"input", "input",
"libc", "libc",
"nix 0.22.3", "nix 0.24.3",
"once_cell", "once_cell",
"strum",
"strum_macros",
"uinput", "uinput",
"winapi", "winapi",
"x11", "x11",
@ -149,6 +151,15 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if 1.0.0",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"
@ -186,12 +197,11 @@ dependencies = [
[[package]] [[package]]
name = "nix" name = "nix"
version = "0.22.3" version = "0.24.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4916f159ed8e5de0082076562152a76b7a1f64a01fd9d1e0fea002c37624faf" checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"cc",
"cfg-if 1.0.0", "cfg-if 1.0.0",
"libc", "libc",
"memoffset", "memoffset",
@ -257,6 +267,12 @@ dependencies = [
"nom", "nom",
] ]
[[package]]
name = "rustversion"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f3208ce4d8448b3f3e7d168a73f5e0c43a61e32930de3bceeccedb388b6bf06"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.160" version = "1.0.160"
@ -274,7 +290,7 @@ checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.15",
] ]
[[package]] [[package]]
@ -286,6 +302,39 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "strum"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.24.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn 1.0.109",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.15" version = "2.0.15"

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
inputbot = "0.5.1" inputbot = { git = "https://github.com/obv-mikhail/InputBot", branch = "develop" }
rosc = "0.10.0" rosc = "0.10.0"
serde = { version = "1.0.160", features = ["derive"] } serde = { version = "1.0.160", features = ["derive"] }
toml = "0.7.3" toml = "0.7.3"

View file

@ -1,24 +1,26 @@
extern crate inputbot;
use std::{net::{SocketAddrV4, UdpSocket}, str::FromStr}; use std::{net::{SocketAddrV4, UdpSocket}, str::FromStr};
use rosc::OscPacket; use rosc::OscPacket;
use toml; use toml;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use inputbot::{KeybdKey, KeybdKey::*}; use inputbot::{KeybdKey, KeybdKey::*, get_keybd_key};
// Configuration struct with mapping of OSC addresses to keyboard keys, and a general server configuration. // Configuration struct with mapping of OSC addresses to keyboard keys, and a general server configuration.
#[derive(Deserialize)] #[derive(Deserialize, Serialize)]
struct Config { struct Config {
server: ServerConfig, server: ServerConfig,
// Mappings is called mapping in config // Mappings is called mapping in config
#[serde(rename = "mapping")] #[serde(rename = "mapping")]
mappings: Vec<EventKeyMapping>, mappings: Vec<EventKeyMapping>,
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize)]
struct EventKeyMapping { struct EventKeyMapping {
event: String, event: String,
key: String, key: String,
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize)]
struct ServerConfig { struct ServerConfig {
port: u16, port: u16,
} }
@ -34,12 +36,17 @@ fn main() {
let mut event_key_map = std::collections::HashMap::new(); let mut event_key_map = std::collections::HashMap::new();
for mapping in config.mappings { for mapping in config.mappings {
// Convert the key string to a KeybdKey // Convert the key string to a KeybdKey. First try to convert it to an F key, then to a normal key by matching the char.
let key = match to_fkey(&mapping.key) { let key = match to_fkey(&mapping.key) {
Some(k) => k, Some(k) => k,
None => { None => {
println!("Invalid key: {}", mapping.key); match mapping.key.chars().next() {
continue; Some(c) => get_keybd_key(c).unwrap(),
None => {
println!("Invalid key: {}", mapping.key);
return;
}
}
} }
}; };
event_key_map.insert(mapping.event, key); event_key_map.insert(mapping.event, key);
@ -71,6 +78,18 @@ fn main() {
} }
fn load_config() -> Result<Config, String> { fn load_config() -> Result<Config, String> {
// If the config file isn't found, create one with default values
if !std::path::Path::new("config.toml").exists() {
let default_config = Config {
server: ServerConfig {
port: 9000,
},
mappings: vec![],
};
let toml = toml::to_string_pretty(&default_config).map_err(|e| e.to_string())?;
std::fs::write("config.toml", toml).map_err(|e| e.to_string())?;
println!("Created default config file at config.toml");
}
let config_file = std::fs::read_to_string("config.toml").map_err(|e| e.to_string())?; let config_file = std::fs::read_to_string("config.toml").map_err(|e| e.to_string())?;
let config: Config = toml::from_str(&config_file).map_err(|e| e.to_string())?; let config: Config = toml::from_str(&config_file).map_err(|e| e.to_string())?;
Ok(config) Ok(config)
@ -79,6 +98,10 @@ fn load_config() -> Result<Config, String> {
fn handle_packet(packet: OscPacket, mappings: &std::collections::HashMap<String, KeybdKey>) { fn handle_packet(packet: OscPacket, mappings: &std::collections::HashMap<String, KeybdKey>) {
match packet { match packet {
OscPacket::Message(msg) => { OscPacket::Message(msg) => {
// Print message
println!("OSC Message: {:?}", msg);
// Print address
println!("OSC Address: {}", msg.addr);
// Match event to key // Match event to key
let key = match mappings.get(&msg.addr) { let key = match mappings.get(&msg.addr) {
Some(k) => k, Some(k) => k,
@ -87,6 +110,7 @@ fn handle_packet(packet: OscPacket, mappings: &std::collections::HashMap<String,
} }
}; };
// Press key // Press key
println!("Pressing key: {:?}", key);
key.press(); key.press();
} }
OscPacket::Bundle(bundle) => { OscPacket::Bundle(bundle) => {