aboutsummaryrefslogtreecommitdiffstats
path: root/src/map.rs
blob: 750a728a1b7181de4adc0da50fff79599c82b17f (plain)
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
use std::env;
use std::ffi::OsString;
use std::io::BufReader;
use std::process::Command;

use rodio::{self, Source};

use {Action, HeadphoneButton, MapAction, MapKind};
use ffi::State;
use sounds;

pub fn run_key_action_for_mode<'a>(
    state: &mut State,
    trigger: &'a [HeadphoneButton],
) {
    match state.map_group {
        Some(ref map_group) => {
            let map = map_group.maps.get(trigger);
            let mode = map_group.modes.get(trigger);

            if let Some(in_mode) = state.in_mode.clone() {
                if let Some(mode) = map_group.modes.get(&in_mode) {
                    // Deactivate mode by pressing current mode trigger
                    if &in_mode[..] == trigger {
                        state.in_mode = None;

                        return;
                    }

                    if let Some(map) = mode.get(trigger) {
                        run_action(&map);
                    }
                }
            }

            // TODO: make sure this doesn't run when in_mode
            if state.in_mode.is_none() {
                if let Some(map) = map {
                    run_action(&map);
                }
            }

            if mode.is_some() {
                state.in_mode = Some(trigger.to_vec());
            }
        },
        None => (),
    }
}

fn run_action(map_action: &MapAction) {
    match map_action.kind {
        MapKind::Map => {
            if let Action::Map(action) = &map_action.action {
                for key in action {
                    key.tap()
                }
            }
        },
        MapKind::Command => {
            if let Action::String(action) = &map_action.action {
                let shell = match env::var_os("SHELL") {
                    Some(s) => s,
                    None => OsString::from("/bin/sh"),
                };

                match Command::new(shell)
                    .arg("-c")
                    .arg(action)
                    .spawn() {
                    Ok(_) => (),
                    Err(e) => error!(
                        "Command failed to start: `{}'",
                        e
                    ),
                }
            }
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn play_audio() {
        let mode_activated = include_bytes!("../sounds/activ.wav");
        // let mut sound = sounds::MODE_ACTIVATED;
        // let mut reader = BufReader::new(sounds::MODE_ACTIVATED);
        let file = ::std::fs::File::open("sounds/activ.wav").unwrap();
        // let reader = BufReader::new(&mode_activated[..]);
        // let reader = BufReader::new(mode_activated);
        // let reader = BufReader::new(file);
        let reader = ::std::io::Cursor::new(sounds::MODE_ACTIVATED);
let device = rodio::default_output_device().unwrap();
// let source = rodio::Decoder::new(reader).unwrap();
// rodio::play_raw(&device, source.convert_samples());
// ::std::thread::sleep_ms(2000);
let sink = rodio::play_once(&device, reader).unwrap();
sink.sleep_until_end();
sink.play();
    }
}