summaryrefslogtreecommitdiffstats
path: root/src/config/keymap.rs
blob: 2f834d1e3bc7f00574a20daac80a256c49dd156e (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
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
use serde_derive::Deserialize;
use std::collections::{hash_map::Entry, HashMap};
use std::process::exit;

use super::{parse_config_file, parse_to_config_file, ConfigStructure, Flattenable};
use crate::commands::{self, CommandKeybind, JoshutoCommand};
use crate::KEYMAP_FILE;

pub const BACKSPACE: i32 = 0x7F;
pub const TAB: i32 = 0x9;
pub const ENTER: i32 = 0xA;
pub const ESCAPE: i32 = 0x1B;

/* #define KEY_ALT(x) KEY_F(60) + (x - 'A') */

const fn default_up() -> i32 {
    ncurses::KEY_UP
}

const fn default_down() -> i32 {
    ncurses::KEY_DOWN
}

const fn default_left() -> i32 {
    ncurses::KEY_LEFT
}

const fn default_right() -> i32 {
    ncurses::KEY_RIGHT
}

const fn default_home() -> i32 {
    ncurses::KEY_HOME
}

const fn default_end() -> i32 {
    ncurses::KEY_END
}

const fn default_backspace() -> i32 {
    BACKSPACE
}

const fn default_delete() -> i32 {
    ncurses::KEY_DC
}

const fn default_enter() -> i32 {
    ENTER
}

const fn default_escape() -> i32 {
    ESCAPE
}

const fn default_tab() -> i32 {
    TAB
}

#[derive(Debug, Deserialize)]
struct JoshutoRawKeymapping {
    #[serde(default)]
    keymaps: JoshutoKeyMapping,
    #[serde(skip)]
    mapcommand: Vec<JoshutoMapCommand>,
}

#[derive(Debug, Deserialize)]
pub struct JoshutoKeyMapping {
    #[serde(default = "default_up")]
    pub up: i32,
    #[serde(default = "default_down")]
    pub down: i32,
    #[serde(default = "default_left")]
    pub left: i32,
    #[serde(default = "default_right")]
    pub right: i32,
    #[serde(default = "default_home")]
    pub home: i32,
    #[serde(default = "default_end")]
    pub end: i32,
    /*
        #[serde(default = "default_up")]
        pub page_up: i32,
        #[serde(default = "default_up")]
        pub page_down: i32,
    */
    #[serde(default = "default_backspace")]
    pub backspace: i32,
    #[serde(default = "default_delete")]
    pub delete: i32,
    #[serde(default = "default_enter")]
    pub enter: i32,
    #[serde(default = "default_escape")]
    pub escape: i32,
    #[serde(default = "default_tab")]
    pub tab: i32,
}

impl std::default::Default for JoshutoKeyMapping {
    fn default() -> Self {
        JoshutoKeyMapping {
            up: default_up(),
            down: default_down(),
            left: default_left(),
            right: default_right(),
            home: default_home(),
            end: default_end(),
            backspace: default_backspace(),
            delete: default_delete(),
            enter: default_enter(),
            escape: default_escape(),
            tab: default_tab(),
        }
    }
}

impl Flattenable<JoshutoKeyMapping> for JoshutoRawKeymapping {
    fn flatten(self) -> JoshutoKeyMapping {
        self.keymaps
    }
}

impl ConfigStructure for JoshutoKeyMapping {
    fn get_config() -> Self {
        parse_to_config_file::<JoshutoRawKeymapping, JoshutoKeyMapping>(KEYMAP_FILE)
            .unwrap_or_else(JoshutoKeyMapping::default)
    }
}

#[derive(Debug, Deserialize)]
struct JoshutoMapCommand {
    pub keys: Vec<i32>,
    pub command: String,
    #[serde(default)]
    pub args: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct JoshutoRawCommandMapping {
    #[serde(skip)]
    keymaps: JoshutoKeyMapping,
    #[serde(default)]
    mapcommand: Vec<JoshutoMapCommand>,
}

impl Flattenable<JoshutoCommandMapping> for JoshutoRawCommandMapping {
    fn flatten(self) -> JoshutoCommandMapping {
        let mut keymaps = JoshutoCommandMapping::new();
        self.mapcommand.iter().for_each(|m| {
            let args: Vec<&str> = m.args.iter().map(|s| s.as_str()).collect();
            match commands::from_args(m.command.as_str(), &args) {
                Ok(command) => insert_keycommand(&mut keymaps, command, &m.keys[..]),
                Err(e) => eprintln!("{}", e),
            }
        });
        keymaps
    }
}

pub type JoshutoCommandMapping = HashMap<i32, CommandKeybind>;

impl ConfigStructure for JoshutoCommandMapping {
    fn get_config() -> Self {
        parse_to_config_file::<JoshutoRawCommandMapping, JoshutoCommandMapping>(KEYMAP_FILE)
            .unwrap_or_else(JoshutoCommandMapping::default)
    }
}

fn insert_keycommand(
    map: &mut JoshutoCommandMapping,
    keycommand: Box<JoshutoCommand>,
    keys: &[i32],
) {
    match keys.len() {
        0 => {}
        1 => match map.entry(keys[0]) {
            Entry::Occupied(_) => {
                eprintln!("Error: Keybindings ambiguous");
                exit(1);
            }
            Entry::Vacant(entry) => {
                entry.insert(CommandKeybind::SimpleKeybind(keycommand));
            }
        },
        _ => match map.entry(keys[0]) {
            Entry::Occupied(mut entry) => match entry.get_mut() {
                CommandKeybind::CompositeKeybind(ref mut m) => {
                    insert_keycommand(m, keycommand, &keys[1..])
                }
                _ => {
                    eprintln!("Error: Keybindings ambiguous");
                    exit(1);
                }
            },
            Entry::Vacant(entry) => {
                let mut new_map = JoshutoCommandMapping::new();
                insert_keycommand(&mut new_map, keycommand, &keys[1..]);
                let composite_command = CommandKeybind::CompositeKeybind(new_map);
                entry.insert(composite_command);
            }
        },
    }
}