about summary refs log tree commit diff stats
path: root/pkgs/by-name/ri/river-mk-keymap/src/key_map/commands.rs
blob: a4ac0ebd1b3a990d7c6fc98636f4824b777c43c9 (plain) (blame)
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
use std::process::Command;

use keymaps::key_repr::{KeyValue, MediaKeyCode, MouseKeyValue};

use super::{KeyMap, MapMode};

impl KeyMap {
    #[must_use]
    pub fn to_commands(self) -> Vec<Command> {
        self.0
            .iter()
            .flat_map(|(key, value)| {
                let key = key.last().expect("Will exist");
                let mods = {
                    let modifiers = key.modifiers();
                    let mut output = vec![];

                    if modifiers.alt() {
                        output.push("Alt");
                    }
                    if modifiers.ctrl() {
                        output.push("Control");
                    }
                    if modifiers.meta() {
                        output.push("Super");
                    }
                    if modifiers.shift() {
                        output.push("Shift");
                    }
                    if output.is_empty() {
                        "None".to_owned()
                    } else {
                        output.join("+")
                    }
                };
                let key_value = match key.value() {
                    KeyValue::Backspace => "BackSpace".to_owned(),
                    KeyValue::Enter => "Enter".to_owned(),
                    KeyValue::Left => "Left".to_owned(),
                    KeyValue::Right => "Right".to_owned(),
                    KeyValue::Up => "Up".to_owned(),
                    KeyValue::Down => "Down".to_owned(),
                    KeyValue::Home => "Home".to_owned(),
                    KeyValue::End => "End".to_owned(),
                    KeyValue::PageUp => "Page_Up".to_owned(),
                    KeyValue::PageDown => "Page_Down".to_owned(),
                    KeyValue::Tab => "Tab".to_owned(),
                    KeyValue::BackTab => "BackTab".to_owned(),
                    KeyValue::Delete => "Delete".to_owned(),
                    KeyValue::Insert => "Insert".to_owned(),
                    KeyValue::F(num) => format!("F{num}"),
                    KeyValue::Char(a) => a.to_string(),
                    KeyValue::Null => "Null".to_owned(),
                    KeyValue::Esc => "Esc".to_owned(),
                    KeyValue::CapsLock => "CapsLock".to_owned(),
                    KeyValue::ScrollLock => "ScrollLock".to_owned(),
                    KeyValue::NumLock => "NumLock".to_owned(),
                    KeyValue::PrintScreen => "Print".to_owned(),
                    KeyValue::Pause => "Pause".to_owned(),
                    KeyValue::Menu => "Menu".to_owned(),
                    KeyValue::KeypadBegin => "KeypadBegin".to_owned(),
                    KeyValue::Media(media_key_code) => match media_key_code {
                        MediaKeyCode::Play => "XF86AudioPlay".to_owned(),
                        MediaKeyCode::Pause => "XF86AudioPause".to_owned(),
                        MediaKeyCode::PlayPause => "XF86AudioPlayPause".to_owned(),
                        MediaKeyCode::Reverse => "XF86AudioReverse".to_owned(),
                        MediaKeyCode::Stop => "XF86AudioStop".to_owned(),
                        MediaKeyCode::FastForward => "XF86AudioFastForward".to_owned(),
                        MediaKeyCode::Rewind => "XF86AudioRewind".to_owned(),
                        MediaKeyCode::TrackNext => "XF86AudioTrackNext".to_owned(),
                        MediaKeyCode::TrackPrevious => "XF86AudioTrackPrevious".to_owned(),
                        MediaKeyCode::Record => "XF86AudioRecord".to_owned(),
                        MediaKeyCode::LowerVolume => "XF86AudioLowerVolume".to_owned(),
                        MediaKeyCode::RaiseVolume => "XF86AudioRaiseVolume".to_owned(),
                        MediaKeyCode::MuteVolume => "XF86AudioMuteVolume".to_owned(),
                    },
                    KeyValue::MouseKey(mouse_key_value) => match mouse_key_value {
                        MouseKeyValue::Left => "BTN_LEFT".to_owned(),
                        MouseKeyValue::Right => "BTN_RIGHT".to_owned(),
                        MouseKeyValue::Middle => "BTN_MIDDLE".to_owned(),
                    },
                    _ => todo!(),
                };

                value
                    .modes
                    .iter()
                    .map(|mode| {
                        let mut riverctl = Command::new("riverctl");
                        riverctl.args([value.map_mode.as_command(), mode, &mods, &key_value]);

                        riverctl.args(value.command.iter().map(String::as_str));
                        riverctl
                    })
                    .collect::<Vec<_>>()
            })
            .collect()
    }
}

impl MapMode {
    pub(crate) fn as_command(self) -> &'static str {
        match self {
            MapMode::Map => "map",
            MapMode::MapMouse => "map-pointer",
            MapMode::Unmap => "unmap",
        }
    }
}