Skip to content

Commit

Permalink
Put all keymap related files into cir::keymap module
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Young <sean@mess.org>
  • Loading branch information
seanyoung committed Apr 27, 2024
1 parent 0050e6c commit 7666778
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 46 deletions.
5 changes: 3 additions & 2 deletions src/bin/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use aya::programs::{Link, LircMode2};
use cir::{
keymap, lirc, lircd_conf,
keymap::Keymap,
lirc, lircd_conf,
rc_maps::parse_rc_maps_file,
rcdev,
rcdev::{enumerate_rc_dev, Rcdev},
Expand Down Expand Up @@ -163,7 +164,7 @@ fn clear_scancodes(inputdev: &Device) {
fn load_keymap(inputdev: &Device, keymap_filename: &Path) {
let keymap_contents = fs::read_to_string(keymap_filename).unwrap();

let map = match keymap::parse(&keymap_contents, keymap_filename) {
let map = match Keymap::parse(&keymap_contents, keymap_filename) {
Ok(map) => map,
Err(e) => {
eprintln!("error: {}: {e}", keymap_filename.display());
Expand Down
2 changes: 1 addition & 1 deletion src/bin/commands/transmit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(target_os = "linux")]
use super::config::{open_lirc, Purpose};
use cir::{linux_protocol::LinuxProtocol, lircd_conf};
use cir::{keymap::LinuxProtocol, lircd_conf};
use irp::{Irp, Message, Pronto, Vartable};
use log::{error, info, warn};
use std::{ffi::OsStr, fs, path::Path, str::FromStr};
Expand Down
32 changes: 32 additions & 0 deletions src/keymap/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::collections::HashMap;

mod parse;
mod protocol;

/// A Linux keymap, either toml or text format used by ir-keytable
#[derive(PartialEq, Eq, Debug, Default)]
pub struct Keymap {
pub name: String,
pub protocol: String,
pub variant: Option<String>,
pub irp: Option<String>,
pub rc_protocol: Option<u16>,
pub raw: Option<Vec<Raw>>,
pub scancodes: Option<HashMap<String, String>>,
}

#[derive(PartialEq, Eq, Debug)]
pub struct Raw {
pub keycode: String,
pub raw: Option<String>,
pub repeat: Option<String>,
pub pronto: Option<String>,
}

pub struct LinuxProtocol {
pub name: &'static str,
pub decoder: &'static str,
pub irp: Option<&'static str>,
pub scancode_mask: u32,
pub protocol_no: u32,
}
52 changes: 18 additions & 34 deletions src/keymap.rs → src/keymap/parse.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
//! Parse linux rc keymaps
use super::{Keymap, Raw};
use std::{collections::HashMap, ffi::OsStr, fmt::Write, path::Path};
use toml::{Table, Value};

#[derive(PartialEq, Eq, Debug, Default)]
pub struct Keymap {
pub name: String,
pub protocol: String,
pub variant: Option<String>,
pub irp: Option<String>,
pub rc_protocol: Option<u16>,
pub raw: Option<Vec<Raw>>,
pub scancodes: Option<HashMap<String, String>>,
}

#[derive(PartialEq, Eq, Debug)]
pub struct Raw {
pub keycode: String,
pub raw: Option<String>,
pub repeat: Option<String>,
pub pronto: Option<String>,
}

peg::parser! {
grammar text_keymap() for str {
pub rule keymap() -> Vec<Keymap>
Expand Down Expand Up @@ -78,12 +60,14 @@ peg::parser! {
}
}

/// Parse a rc keymap file, either toml or old text format. No validation is done of key codes or protocol names
pub fn parse(contents: &str, filename: &Path) -> Result<Vec<Keymap>, String> {
if filename.extension() == Some(OsStr::new("toml")) {
parse_toml(contents, filename)
} else {
text_keymap::keymap(contents).map_err(|pos| format!("parse error at {pos}"))
impl Keymap {
/// Parse a rc keymap file, either toml or old text format. No validation is done of key codes or protocol names
pub fn parse(contents: &str, filename: &Path) -> Result<Vec<Keymap>, String> {
if filename.extension() == Some(OsStr::new("toml")) {
parse_toml(contents, filename)
} else {
text_keymap::keymap(contents).map_err(|pos| format!("parse error at {pos}"))
}
}
}

Expand Down Expand Up @@ -416,7 +400,7 @@ fn parse_toml_test() {
0x1e1c = "KEY_TV"
"#;

let k = parse(s, Path::new("x.toml")).unwrap();
let k = Keymap::parse(s, Path::new("x.toml")).unwrap();

assert_eq!(k[0].name, "hauppauge");
assert_eq!(k[0].protocol, "rc5");
Expand All @@ -441,7 +425,7 @@ fn parse_toml_test() {
"#;

assert_eq!(
parse(s, Path::new("x.toml")),
Keymap::parse(s, Path::new("x.toml")),
Err("raw protocol is misssing raw entries".to_string())
);

Expand All @@ -454,7 +438,7 @@ fn parse_toml_test() {
"#;

assert_eq!(
parse(s, Path::new("x.toml")),
Keymap::parse(s, Path::new("x.toml")),
Err("raw entry has neither pronto hex code nor raw".to_string())
);

Expand All @@ -468,7 +452,7 @@ fn parse_toml_test() {
"#;

assert_eq!(
parse(s, Path::new("x.toml")),
Keymap::parse(s, Path::new("x.toml")),
Err("raw entry has neither pronto hex code nor raw".to_string())
);
}
Expand All @@ -482,7 +466,7 @@ fn parse_text_test() {
0x1e1c KEY_TV
"#;

let k = parse(s, Path::new("hauppauge")).unwrap();
let k = Keymap::parse(s, Path::new("hauppauge")).unwrap();

assert_eq!(k[0].name, "hauppauge");
assert_eq!(k[0].protocol, "RC5");
Expand All @@ -506,7 +490,7 @@ fn parse_text_test() {
0x800f0403 KEY_NUMERIC_3
"#;

let k = parse(s, Path::new("hauppauge")).unwrap();
let k = Keymap::parse(s, Path::new("hauppauge")).unwrap();

assert_eq!(k[0].name, "rc6_mce");
assert_eq!(k[0].protocol, "RC6");
Expand All @@ -531,7 +515,7 @@ fn parse_text_test() {
0x28c2 KEY_NUMERIC_2
"#;

let k = parse(s, Path::new("hauppauge")).unwrap();
let k = Keymap::parse(s, Path::new("hauppauge")).unwrap();

assert_eq!(k[0].name, "streamzap");
assert_eq!(k[0].protocol, "RC-5-SZ");
Expand Down Expand Up @@ -561,7 +545,7 @@ fn parse_bpf_toml_test() {
0x1e1c = "KEY_TV"
"#;

let k = parse(s, Path::new("x.toml")).unwrap();
let k = Keymap::parse(s, Path::new("x.toml")).unwrap();

assert_eq!(k[0].name, "meh");
assert_eq!(k[0].protocol, "manchester");
Expand All @@ -584,7 +568,7 @@ fn parse_bpf_toml_test() {
0x1e1c = "KEY_TV"
"#;

let k = parse(s, Path::new("x.toml")).unwrap();
let k = Keymap::parse(s, Path::new("x.toml")).unwrap();

assert_eq!(k[0].name, "meh");
assert_eq!(k[0].protocol, "manchester");
Expand Down
8 changes: 1 addition & 7 deletions src/linux_protocol.rs → src/keymap/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
pub struct LinuxProtocol {
pub name: &'static str,
pub decoder: &'static str,
pub irp: Option<&'static str>,
pub scancode_mask: u32,
pub protocol_no: u32,
}
use super::LinuxProtocol;

impl LinuxProtocol {
pub fn find(name: &str) -> Option<&'static LinuxProtocol> {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod keymap;
pub mod linux_protocol;
#[cfg(target_os = "linux")]
pub mod lirc;
pub mod lircd_conf;
Expand Down
3 changes: 2 additions & 1 deletion tests/bpf_encode_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cir::keymap::Keymap;
use irp::{Irp, Vartable};
use libirctl::{encode_bpf_protocol, free_keymap, keymap, parse_keymap};
use std::{ffi::CString, fs::File, io::Read, path::PathBuf};
Expand Down Expand Up @@ -29,7 +30,7 @@ fn irctl_compare_encode(path: &str, scancode: u32) {

f.read_to_string(&mut contents).unwrap();

let keymap = cir::keymap::parse(&contents, &path).unwrap();
let keymap = Keymap::parse(&contents, &path).unwrap();

let irp = keymap[0].irp.as_ref().unwrap();
let irp = Irp::parse(irp).unwrap();
Expand Down

0 comments on commit 7666778

Please sign in to comment.