summaryrefslogtreecommitdiffstats
path: root/src/config/keymap.rs
blob: 4b44b2d3e11736c18e35d1864999e6ab8c00d060 (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
use serde_derive::Deserialize;
use std::collections::hash_map;
use std::collections::HashMap;
use std::process::exit;

use crate::commands;
use crate::config::{parse_config_file, Flattenable};
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') */

#[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 Flattenable<JoshutoKeymap> for JoshutoRawKeymap {
    fn flatten(self) -> JoshutoKeymap {
        let mut keymaps: HashMap<i32, commands::CommandKeybind> = HashMap::new();
        if let Some(maps) = self.mapcommand {
            for mapcommand in maps {
                match commands::from_args(mapcommand.command.as_str(), mapcommand.args.as_ref()) {
                    Some(command) => {
                        insert_keycommand(&mut keymaps, command, &mapcommand.keys[..]);
                    }
                    None => {
                        eprintln!("Unknown command: {}", mapcommand.command);
                    }
                }
            }
        }
        JoshutoKeymap { keymaps }
    }
}

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

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

    pub fn get_config() -> JoshutoKeymap {
        parse_config_file::<JoshutoRawKeymap, JoshutoKeymap>(KEYMAP_FILE)
            .unwrap_or_else(JoshutoKeymap::new)
    }
}

fn insert_keycommand(
    map: &mut HashMap<i32, commands::CommandKeybind>,
    keycommand: Box<commands::JoshutoCommand>,
    keys: &[String],
) {
    if keys.len() == 1 {
        if let Some(s) = key_to_i32(&keys[0]) {
            match map.entry(s) {
                hash_map::Entry::Occupied(_) => {
                    eprintln!("Error: Keybindings ambiguous");
                    exit(1);
                }
                hash_map::Entry::Vacant(entry) => {
                    entry.insert(commands::CommandKeybind::SimpleKeybind(keycommand));
                }
            }
        } else {
            eprintln!("Error: Failed to parse keycode: {}", keys[0]);
        }
    } else if let Some(s) = key_to_i32(&keys[0]) {
        let mut new_map: HashMap<i32, commands::CommandKeybind>;
        match map.remove(&s) {
            Some(commands::CommandKeybind::CompositeKeybind(m)) => {
                new_map = m;
            }
            Some(_) => {
                eprintln!("Error: Keybindings ambiguous");
                exit(1);
            }
            None => {
                new_map = HashMap::new();
            }
        }
        insert_keycommand(&mut new_map, keycommand, &keys[1..]);
        let composite_command = commands::CommandKeybind::CompositeKeybind(new_map);
        map.insert(s, composite_command);
    }
}

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);
            }
        }
        None
    } else {
        match keycode {
            "Tab" => Some(TAB),
            "ShiftTab" => Some(ncurses::KEY_BTAB),
            "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,
        }
    }
}