summaryrefslogtreecommitdiffstats
path: root/src/joshuto/keymap.rs
blob: dec5ab5d11cfc179c8ce3f2bd06070a578c51957 (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
extern crate fs_extra;
extern crate toml;
extern crate xdg;

use std::collections::HashMap;
use std::fs;
use std::io;
use std::io::BufRead;
use std::process;

use joshuto::command;
use joshuto::input;

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') */

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


#[derive(Debug, Deserialize)]
struct JoshutoRawKeymap {
    mapcommand: Option<Vec<JoshutoMapCommand>>,
}

impl JoshutoRawKeymap {

    pub fn flatten(self) -> JoshutoKeymap
    {
        let mut keymaps: HashMap<i32, command::CommandKeybind> = HashMap::new();
        if let Some(maps) = self.mapcommand {
            for mapcommand in maps {
                match command::from_args(mapcommand.command.as_str(), mapcommand.args.as_ref()) {
                    Some(command) => {
                        insert_keycommand(&mut keymaps, command, &mapcommand.keys[..]);
                    },
                    None => {
                        println!("Unknown command: {}", mapcommand.command);
                    }
                }
            }
        }
        JoshutoKeymap {
            keymaps,
        }
    }
}

#[derive(Debug)]
pub struct JoshutoKeymap {
    pub keymaps: HashMap<i32, command::CommandKeybind>,
}

impl JoshutoKeymap {
    pub fn new() -> Self
    {
        let keymaps = input::initialize_default_keymap();
        JoshutoKeymap {
            keymaps,
        }
    }

    fn read_config() -> Option<JoshutoRawKeymap>
    {
        match xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "") {
            Ok(dirs) => {
                let config_path = dirs.find_config_file(::KEYMAP_FILE)?;
                match fs::read_to_string(&config_path) {
                    Ok(config_contents) => {
                        match toml::from_str(&config_contents) {
                            Ok(config) => {
                                Some(config)
                            },
                            Err(e) => {
                                eprintln!("{}", e);
                                process::exit(1);
                            },
                        }
                    },
                    Err(e) => {
                        eprintln!("{}", e);
                        process::exit(1);
                    },
                }
            },
            Err(e) => {
                eprintln!("{}", e);
                None
            },
        }
    }

    pub fn get_config() -> JoshutoKeymap
    {
        match JoshutoKeymap::read_config() {
            Some(config) => {
                config.flatten()
            }
            None => {
                JoshutoKeymap::new()
            }
        }
    }
}


fn insert_keycommand(map: &mut HashMap<i32, command::CommandKeybind>,
        keycommand: Box<dyn command::JoshutoCommand>, keys: &[String])
{
    if keys.len() == 1 {
        match key_to_i32(&keys[0]) {
            Some(s) => {
                map.insert(s, command::CommandKeybind::SimpleKeybind(keycommand));
            },
            None => {}
        }
    } else {
        match key_to_i32(&keys[0]) {
            Some(s) => {
                let mut new_map: HashMap<i32, command::CommandKeybind>;
                match map.remove(&s) {
                    Some(command::CommandKeybind::CompositeKeybind(mut m)) => {
                        new_map = m;
                    },
                    Some(_) => {
                        eprintln!("Error: Keybindings ambiguous");
                        process::exit(1);
                    },
                    None => {
                        new_map = HashMap::new();
                    }
                }
                insert_keycommand(&mut new_map, keycommand, &keys[1..]);
                let composite_command = command::CommandKeybind::CompositeKeybind(new_map);
                map.insert(s as i32, composite_command);
            },
            None => {}
        }
    }
}


pub fn key_to_i32(keycode: &str) -> Option<i32>
{
    if keycode.len() == 1 {
        for ch in keycode.chars() {
            if ch.is_ascii() {
                return Some(ch as i32);
            }
        }
        return None;
    } else {
        match keycode {
            "Comma" => Some(',' as i32),
            "Tab" => Some(TAB),
            "Space" => Some(' ' as i32),
            "Backspace" => Some(BACKSPACE),
            "Delete" => Some(ncurses::KEY_DC),
            "Enter" => Some(ENTER),
            "Escape" => Some(ESCAPE),


            "F0" => Some(ncurses::KEY_F0),
            "F1" => Some(ncurses::KEY_F1),
            "F2" => Some(ncurses::KEY_F2),
            "F3" => Some(ncurses::KEY_F3),
            "F4" => Some(ncurses::KEY_F4),
            "F5" => Some(ncurses::KEY_F5),
            "F6" => Some(ncurses::KEY_F6),
            "F7" => Some(ncurses::KEY_F7),
            "F8" => Some(ncurses::KEY_F8),
            "F9" => Some(ncurses::KEY_F9),
            "F10" => Some(ncurses::KEY_F10),
            "F11" => Some(ncurses::KEY_F11),
            "F12" => Some(ncurses::KEY_F12),
            "F13" => Some(ncurses::KEY_F13),
            "F14" => Some(ncurses::KEY_F14),
            "F15" => Some(ncurses::KEY_F15),

            "Insert" => Some(ncurses::KEY_IC),           /* insert-character key */
            "PageUp" => Some(ncurses::KEY_PPAGE),        /* next-page key */
            "PageDown" => Some(ncurses::KEY_NPAGE),      /* previous-page key */
            "PrintScreen" => Some(ncurses::KEY_PRINT),    /* print key */

            "Up" => Some(ncurses::KEY_UP),
            "Down" => Some(ncurses::KEY_DOWN),
            "Left" => Some(ncurses::KEY_LEFT),
            "Right" => Some(ncurses::KEY_RIGHT),
            "Home" => Some(ncurses::KEY_HOME),
            "End" => Some(ncurses::KEY_END),
            _ => None,
        }
    }
}