cir 0.1.4

Linux Infrared Tooling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//! Parse linux rc keymaps

use super::{Keymap, Raw};
use irp::{Message, Pronto};
use std::{collections::HashMap, ffi::OsStr, fmt::Write, fs::File, io::Read, path::Path};
use toml::{Table, Value};

peg::parser! {
    grammar text_keymap() for str {
        pub rule keymap() -> Vec<Keymap>
        = (_ newline())* first:first_line() lines:lines() _
        {
            let mut scancodes = HashMap::new();

            for (code, name) in lines.into_iter().flatten() {
                let code = string_to_scancode(code).unwrap();
                scancodes.insert(code, name.to_owned());
            }

            let mut protocol = vec![Keymap {
                    name: first.0.to_owned(),
                    protocol: first.1[0].to_owned(),
                    scancodes,
                    ..Default::default()
            }];

            for other in &first.1[1..] {
                protocol.push(Keymap { protocol: other.to_string(), ..Default::default() });
            }

            protocol
        }

        rule first_line() -> (&'input str, Vec<&'input str>)
        = _ "#" _ "table" (":" / "=")? _ name:identifier()  _ "," _ "type" (":" / "=")? _ protocols:protocols() _ newline()
        { (name, protocols) }

        rule identifier() -> &'input str
        = quiet!{$([ 'a'..='z' | 'A'..='Z']['a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-' ]*)}
        / expected!("identifier")

        rule protocols() -> Vec<&'input str>
        = protocols:(identifier() ++ ("," _)) { protocols }

        rule lines() -> Vec<Option<(&'input str, &'input str)>>
        = codes:((scancode() / comment()) ** newline()) { codes }

        rule newline()
        = "\r\n" / "\n"

        rule comment() -> Option<(&'input str, &'input str)>
        = _ "#" [^'\n']* { None }
        / _ { None }

        rule scancode() -> Option<(&'input str, &'input str)>
        = _ hex:hex() _ id:identifier() _ { Some((hex, id)) }

        rule hex() -> &'input str
        = hex:$("0x" ['0'..='9' | 'a'..='f' | 'A'..='F']+) _ { hex }

        rule _ = quiet!{[' ' | '\t']*}
    }
}

fn string_to_scancode(s: &str) -> Result<u64, std::num::ParseIntError> {
    if let Some(hex) = s.strip_prefix("0x") {
        u64::from_str_radix(hex, 16)
    } else {
        str::parse(s)
    }
}
impl Keymap {
    /// Load a file and parse it as a Keymap
    pub fn parse_file(path: &Path) -> Result<Vec<Keymap>, String> {
        let mut f = File::open(path).map_err(|e| format!("{}: {e}", path.display()))?;

        let mut contents = String::new();

        f.read_to_string(&mut contents)
            .map_err(|e| format!("{}: {e}", path.display()))?;

        Keymap::parse_str(&contents, path)
    }

    /// Parse a rc keymap file, either toml or old text format. No validation is done of key codes or protocol names
    pub fn parse_str(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}", filename.display()))
        }
    }
}

fn parse_toml(contents: &str, filename: &Path) -> Result<Vec<Keymap>, String> {
    let top = contents
        .parse::<Table>()
        .map_err(|e| format!("{}: {e}", filename.display()))?;

    let Some(Value::Array(protocols)) = top.get("protocols") else {
        return Err(format!(
            "{}: missing top level protocols array",
            filename.display()
        ));
    };

    let mut res = Vec::new();

    for entry in protocols {
        let Some(Value::String(name)) = entry.get("name") else {
            return Err(format!("{}: missing name", filename.display()));
        };

        let Some(Value::String(protocol)) = entry.get("protocol") else {
            return Err(format!("{}: missing protocol", filename.display()));
        };

        let mut variant = None;
        if let Some(Value::String(entry)) = entry.get("variant") {
            variant = Some(entry.to_owned());
        }

        let mut rc_protocol = None;
        if let Some(Value::Integer(n)) = entry.get("rc_protocol") {
            if let Ok(n) = (*n).try_into() {
                rc_protocol = Some(n);
            } else {
                return Err(format!(
                    "{}: rc_protocol {n} must be 16 bit value",
                    filename.display()
                ));
            }
        }

        let mut irp = None;
        let mut raw_entries = Vec::new();
        let mut scancodes = HashMap::new();

        if protocol == "raw" {
            // find raw entries
            let Some(Value::Array(e)) = entry.get("raw") else {
                return Err(format!(
                    "{}: raw protocol is misssing raw entries",
                    filename.display()
                ));
            };

            for e in e {
                let Some(Value::String(keycode)) = e.get("keycode") else {
                    return Err(format!("{}: missing keycode", filename.display()));
                };

                let raw = if let Some(Value::String(raw)) = e.get("raw") {
                    let mut raw =
                        Message::parse(raw).map_err(|e| format!("{}: {e}", filename.display()))?;

                    if !raw.has_trailing_gap() {
                        raw.raw.push(20000);
                    }
                    Some(raw)
                } else {
                    None
                };

                let repeat = if let Some(Value::String(repeat)) = e.get("repeat") {
                    let mut repeat = Message::parse(repeat)
                        .map_err(|e| format!("{}: {e}", filename.display()))?;
                    if !repeat.has_trailing_gap() {
                        repeat.raw.push(20000);
                    }
                    Some(repeat)
                } else {
                    None
                };

                let pronto = if let Some(Value::String(pronto)) = e.get("pronto") {
                    let pronto = Pronto::parse(pronto)
                        .map_err(|e| format!("{}: {e}", filename.display()))?;
                    Some(pronto)
                } else {
                    None
                };

                if pronto.is_some() {
                    if raw.is_some() {
                        return Err(format!(
                            "{}: raw entry has both pronto hex code and raw",
                            filename.display()
                        ));
                    }
                    if repeat.is_some() {
                        return Err(format!(
                            "{}: raw entry has both pronto hex code and repeat",
                            filename.display()
                        ));
                    }
                } else if raw.is_none() {
                    return Err(format!(
                        "{}: raw entry has neither pronto hex code nor raw",
                        filename.display()
                    ));
                }

                raw_entries.push(Raw {
                    keycode: keycode.to_owned(),
                    raw,
                    repeat,
                    pronto,
                });
            }
        } else {
            if entry.get("raw").is_some() {
                return Err(format!(
                    "{}: raw entries for non-raw protocol",
                    filename.display()
                ));
            }

            if protocol == "irp" {
                if let Some(Value::String(entry)) = entry.get("irp") {
                    irp = Some(entry.to_owned());
                }
            } else if entry.get("irp").is_some() {
                return Err(format!(
                    "{}: set the protocol to irp when using irp",
                    filename.display()
                ));
            } else {
                irp = bpf_protocol_irp(protocol, entry.as_table().unwrap());
            }

            if let Some(Value::Table(codes)) = entry.get("scancodes") {
                for (scancode, keycode) in codes {
                    let scancode = string_to_scancode(scancode).map_err(|_| {
                        format!("{}: {scancode} is a not valid scancode", filename.display())
                    })?;
                    let Value::String(keycode) = keycode else {
                        return Err(format!("{}: keycode should be string", filename.display()));
                    };

                    scancodes.insert(scancode, keycode.to_owned());
                }
            }
        }

        res.push(Keymap {
            name: name.to_owned(),
            protocol: protocol.to_owned(),
            variant,
            raw: raw_entries,
            rc_protocol,
            scancodes,
            irp,
        });
    }

    Ok(res)
}

fn bpf_protocol_irp(protocol: &str, entry: &Table) -> Option<String> {
    let param = |name: &str, default: i64| -> i64 {
        if let Some(Value::Integer(n)) = entry.get(name) {
            *n
        } else {
            default
        }
    };

    match protocol {
        "pulse_distance" => {
            let mut irp = "{".to_owned();
            let bits = param("bits", 4);

            if param("reverse", 0) == 0 {
                irp.push_str("msb,");
            }

            if entry.contains_key("carrier") {
                write!(irp, "{}Hz,", param("carrier", 0)).unwrap();
            }

            if irp.ends_with(',') {
                irp.pop();
            }

            write!(
                irp,
                "}}<{},-{}|{},-{}>({},-{},CODE:{},{},-40m",
                param("bit_pulse", 625),
                param("bit_0_space", 375),
                param("bit_pulse", 625),
                param("bit_1_space", 1625),
                param("header_pulse", 2125),
                param("header_space", 1875),
                bits,
                param("trailer_pulse", 625),
            )
            .unwrap();

            let header_optional = param("header_optional", 0);

            if header_optional > 0 {
                write!(
                    irp,
                    ",(CODE:{},{},-40m)*",
                    bits,
                    param("trailer_pulse", 625),
                )
                .unwrap();
            } else {
                let repeat_pulse = param("repeat_pulse", 0);
                if repeat_pulse > 0 {
                    write!(
                        irp,
                        ",({},-{},{},-40)*",
                        repeat_pulse,
                        param("repeat_space", 0),
                        param("trailer_pulse", 625)
                    )
                    .unwrap();
                }
            }

            write!(irp, ") [CODE:0..{}]", gen_mask(bits)).unwrap();

            Some(irp)
        }
        "pulse_length" => {
            let mut irp = "{".to_owned();
            let bits = param("bits", 4);

            if param("reverse", 0) == 0 {
                irp.push_str("msb,");
            }

            if entry.contains_key("carrier") {
                write!(irp, "{}Hz,", param("carrier", 0)).unwrap();
            }

            if irp.ends_with(',') {
                irp.pop();
            }

            write!(
                irp,
                "}}<{},-{}|{},-{}>({},-{},CODE:{},-40m",
                param("bit_0_pulse", 375),
                param("bit_space", 625),
                param("bit_1_pulse", 1625),
                param("bit_space", 625),
                param("header_pulse", 2125),
                param("header_space", 1875),
                bits,
            )
            .unwrap();

            let header_optional = param("header_optional", 0);

            if header_optional > 0 {
                write!(irp, ",(CODE:{},-40m)*", bits).unwrap();
            } else {
                let repeat_pulse = param("repeat_pulse", 0);
                if repeat_pulse > 0 {
                    write!(
                        irp,
                        ",({},-{},{},-40)*",
                        repeat_pulse,
                        param("repeat_space", 0),
                        param("trailer_pulse", 625)
                    )
                    .unwrap();
                }
            }

            write!(irp, ") [CODE:0..{}]", gen_mask(bits)).unwrap();

            Some(irp)
        }
        "manchester" => {
            let mut irp = "{msb,".to_owned();
            let bits = param("bits", 14);
            let toggle_bit = param("toggle_bit", 100);

            if entry.contains_key("carrier") {
                write!(irp, "{}Hz,", param("carrier", 0)).unwrap();
            }

            if irp.ends_with(',') {
                irp.pop();
            }

            write!(
                irp,
                "}}<-{},{}|{},-{}>(",
                param("zero_space", 888),
                param("zero_pulse", 888),
                param("one_pulse", 888),
                param("one_space", 888),
            )
            .unwrap();

            let header_pulse = param("header_pulse", 0);
            let header_space = param("header_space", 0);

            if header_pulse > 0 && header_space > 0 {
                write!(irp, "{},-{},", header_pulse, header_space).unwrap();
            }

            if toggle_bit >= bits {
                write!(irp, "CODE:{},-40m", bits,).unwrap();
            } else {
                let leading = bits - toggle_bit;
                if leading > 1 {
                    write!(irp, "CODE:{}:{},", leading - 1, toggle_bit + 1).unwrap();
                }
                write!(irp, "T:1,").unwrap();
                if toggle_bit > 0 {
                    write!(irp, "CODE:{},", toggle_bit).unwrap();
                }
                irp.pop();
            }

            write!(irp, ",-40m) [CODE:0..{}]", gen_mask(bits)).unwrap();

            Some(irp)
        }
        _ => None,
    }
}

fn gen_mask(v: i64) -> u64 {
    if v < 64 {
        (1u64 << v) - 1
    } else {
        u64::MAX
    }
}

#[test]
fn parse_toml_test() {
    let s = r#"
    [[protocols]]
    name = "hauppauge"
    protocol = "rc5"
    variant = "rc5"
    [protocols.scancodes]
    0x1e3b = "KEY_SELECT"
    0x1e3d = "KEY_POWER2"
    0x1e1c = "KEY_TV"
    "#;

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

    assert_eq!(k[0].name, "hauppauge");
    assert_eq!(k[0].protocol, "rc5");
    assert_eq!(k[0].variant, Some(String::from("rc5")));
    for s in &k[0].scancodes {
        match (s.0, s.1.as_str()) {
            (0x1e3b, "KEY_SELECT") | (0x1e3d, "KEY_POWER2") | (0x1e1c, "KEY_TV") => {}
            _ => panic!("{s:?} not expected"),
        }
    }

    let s = r#"
    [[protocols]]
    name = "hauppauge"
    protocol = "raw"
    [protocols.scancodes]
    0x1e3b = "KEY_SELECT"
    0x1e3d = "KEY_POWER2"
    0x1e1c = "KEY_TV"
    "#;

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

    let s = r#"
    [[protocols]]
    name = "hauppauge"
    protocol = "raw"
    [[protocols.raw]]
    keycode = 'FOO'
    "#;

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

    let s = r#"
    [[protocols]]
    name = "hauppauge"
    protocol = "raw"
    [[protocols.raw]]
    keycode = 'FOO'
    repeat = '+100'
    "#;

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

#[test]
fn parse_text_test() {
    let s = r#"
    # table hauppauge, type: RC5
    0x1e3b KEY_SELECT
    0x1e3d KEY_POWER2
    0x1e1c KEY_TV
    "#;

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

    assert_eq!(k[0].name, "hauppauge");
    assert_eq!(k[0].protocol, "RC5");
    assert_eq!(k[0].variant, None);
    for s in &k[0].scancodes {
        match (s.0, s.1.as_str()) {
            (0x1e3b, "KEY_SELECT") | (0x1e3d, "KEY_POWER2") | (0x1e1c, "KEY_TV") => {}
            _ => panic!("{s:?} not expected"),
        }
    }

    let s = r#"
    # table: rc6_mce, type: RC6, foo
    0x800f0400 KEY_NUMERIC_0
    0x800f0401 KEY_NUMERIC_1
    # foobar
    0x800f0402 KEY_NUMERIC_2

    0x800f0403 KEY_NUMERIC_3
    "#;

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

    assert_eq!(k[0].name, "rc6_mce");
    assert_eq!(k[0].protocol, "RC6");
    assert_eq!(k[1].protocol, "foo");
    assert_eq!(k[0].variant, None);
    for s in &k[0].scancodes {
        match (s.0, s.1.as_str()) {
            (0x800f0400, "KEY_NUMERIC_0")
            | (0x800f0401, "KEY_NUMERIC_1")
            | (0x800f0402, "KEY_NUMERIC_2")
            | (0x800f0403, "KEY_NUMERIC_3") => {}
            _ => panic!("{s:?} not expected"),
        }
    }

    let s = r#"
    # table streamzap, type: RC-5-SZ
    0x28c0 KEY_NUMERIC_0
    0x28c1 KEY_NUMERIC_1
    0x28c2 KEY_NUMERIC_2
    "#;

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

    assert_eq!(k[0].name, "streamzap");
    assert_eq!(k[0].protocol, "RC-5-SZ");
    assert_eq!(k[0].variant, None);
    for s in &k[0].scancodes {
        match (s.0, s.1.as_str()) {
            (0x28c0, "KEY_NUMERIC_0") | (0x28c1, "KEY_NUMERIC_1") | (0x28c2, "KEY_NUMERIC_2") => {}
            _ => panic!("{s:?} not expected"),
        }
    }
}

#[test]
fn parse_bpf_toml_test() {
    let s = r#"
    [[protocols]]
    name = "meh"
    protocol = "manchester"
    toggle_bit = 12
    [protocols.scancodes]
    0x1e3b = "KEY_SELECT"
    0x1e3d = "KEY_POWER2"
    0x1e1c = "KEY_TV"
    "#;

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

    assert_eq!(k[0].name, "meh");
    assert_eq!(k[0].protocol, "manchester");
    assert_eq!(
        k[0].irp,
        Some("{msb}<-888,888|888,-888>(CODE:1:13,T:1,CODE:12,-40m) [CODE:0..16383]".into())
    );

    let s = r#"
    [[protocols]]
    name = "meh"
    protocol = "manchester"
    toggle_bit = 1
    carrier = 38000
    header_pulse = 300
    header_space = 350
    [protocols.scancodes]
    0x1e3b = "KEY_SELECT"
    0x1e3d = "KEY_POWER2"
    0x1e1c = "KEY_TV"
    "#;

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

    assert_eq!(k[0].name, "meh");
    assert_eq!(k[0].protocol, "manchester");
    assert_eq!(
        k[0].irp,
        Some(
            "{msb,38000Hz}<-888,888|888,-888>(300,-350,CODE:12:2,T:1,CODE:1,-40m) [CODE:0..16383]"
                .into()
        )
    );
}