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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#[macro_use]
extern crate lazy_static;
extern crate petgraph;

use petgraph::graphmap::DiGraphMap;
use petgraph::dot::{Dot, Config};

#[derive(Hash, Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub struct Key {
    pub value: char,
    pub shifted: char,
}


pub trait KeySearch {
    fn find_key(&self, v: char) -> Option<Key>;
}


impl KeySearch for DiGraphMap<Key, Edge> {
    fn find_key(&self, v: char) -> Option<Key> {
        self.nodes().filter(|x| x.value == v || x.shifted == v).nth(0)
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Direction {
    Previous = -1,
    Next = 1,
    Same = 0,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Edge {
    pub horizontal: Direction,
    pub vertical: Direction,
}

#[derive(PartialEq)]
enum KeyboardStyle {
    Slanted,
    Aligned,
}

fn get_slanted_positions() -> Vec<Edge> {
    use Direction::{Previous, Next, Same};
    vec![ 
        Edge{ horizontal: Previous, vertical: Same },
        Edge{ horizontal: Same, vertical: Previous },
        Edge{ horizontal: Next, vertical: Previous },
        Edge{ horizontal: Next, vertical: Same },
        Edge{ horizontal: Same, vertical: Next },
        Edge{ horizontal: Previous, vertical: Next },
    ]
}

fn get_aligned_positions() -> Vec<Edge> {
    use Direction::{Previous, Next, Same};
    vec![
        Edge{ horizontal: Previous, vertical: Same },
        Edge{ horizontal: Previous, vertical: Previous },
        Edge{ horizontal: Same, vertical: Previous },
        Edge{ horizontal: Next, vertical: Previous },
        Edge{ horizontal: Next, vertical: Same },
        Edge{ horizontal: Next, vertical: Next },
        Edge{ horizontal: Same, vertical: Next },
        Edge{ horizontal: Previous, vertical: Next },
    ]
}


lazy_static! {
    pub static ref QWERTY_US: DiGraphMap<Key, Edge> = generate_qwerty_us();
    pub static ref DVORAK: DiGraphMap<Key, Edge> = generate_dvorak(); 
    pub static ref STANDARD_NUMPAD: DiGraphMap<Key, Edge> = generate_standard_numpad();
    pub static ref MAC_NUMPAD: DiGraphMap<Key, Edge> = generate_mac_numpad();
}

#[test]
fn test_qwerty_us() {
    // Printing out graphviz for debugging
    println!("{:?}",
             Dot::with_config(&*QWERTY_US, &[Config::EdgeNoLabel]));
}

static ALPHABET: &'static str = "abcdefghijklmnopqrstuvwxyz";
static NUMBERS: &'static str = "0123456789";


/// Function to add all alphabet characters to keyboard. (a-z & A-Z).
/// With qwerty and dvorak unshifted is lowercase and shifted is uppercase so
/// these keys are common.
fn add_alphabetics(graph: &mut DiGraphMap<Key, Edge>) {
    for c in ALPHABET.chars() {
        graph.add_node(Key {
            value: c,
            shifted: c.to_uppercase().nth(0).unwrap(),
        });
    }
}


/// Numpads typically have no shift modifiers so use this function to populate
/// the numeric keys.
fn add_unshifted_number_keys(graph: &mut DiGraphMap<Key, Edge>) {

    for c in NUMBERS.chars() {
        graph.add_node(Key {
            value: c,
            shifted: '\0',
        });
    }
}


fn connect_keyboard_nodes(keyboard: &str,
                          graph: &mut DiGraphMap<Key, Edge>,
                          style: KeyboardStyle,
                          add_missing_keys: bool) {

    let relative_positions = if style == KeyboardStyle::Slanted {
        get_slanted_positions()
    } else {
        get_aligned_positions()
    };
    let rows = keyboard.lines()
                       .map(|x| x.chars().filter(|y| y != &' ').collect::<Vec<char>>())
                       .collect::<Vec<Vec<char>>>();

    let rowcount = rows.iter().count() as i32;
    for (i, row) in rows.iter().enumerate() {
        for (j, key) in row.iter().enumerate() {
            // Get the adjacent keys now
            let k = graph.find_key(*key);
            if k.is_none() && !add_missing_keys {
                continue;
            }
            let k = if k.is_some() {
                k.unwrap()
            } else {
                Key {
                    value: *key,
                    shifted: '\0',
                }
            };

            for dir in relative_positions.iter() {
                let y: i32 = i as i32 + dir.vertical as i32;
                let x: i32 = j as i32 + dir.horizontal as i32;
                if y > -1 && y < rowcount && x > -1 {
                    let temp_row = if dir.vertical == Direction::Same {
                        row
                    } else {
                        rows.get(y as usize).unwrap()
                    };

                    if let Some(temp_char) = temp_row.get(x as usize) {

                        let n = graph.find_key(*temp_char);

                        if n.is_none() && !add_missing_keys {
                            continue;
                        }

                        let n = if n.is_some() {
                            n.unwrap()
                        } else {
                            Key {
                                value: *temp_char,
                                shifted: '\0',
                            }
                        };

                        graph.add_edge(k, n, *dir);
                    }
                }
            }
        }
    }
}


fn add_remaining_keys(keys: Vec<Key>, graph: &mut DiGraphMap<Key, Edge>) {

    for k in keys.iter() {
        graph.add_node(k.clone());
    }
}


fn generate_qwerty_us() -> DiGraphMap<Key, Edge> {
    let mut result = DiGraphMap::<Key, Edge>::new();
    // This is a bit nasty but I don't see how to do it nicer..
    // Trailing space after \n represents keyboard offset.
    let qwerty_us = "` 1 2 3 4 5 6 7 8 9 0 - =\n\0 q w e r t y u i o p [ ] \\\n\0 a s d f g h j k \
                     l ; '\n\0 z x c v b n m , . /";

    add_alphabetics(&mut result);

    let remaining_keys = vec![ 
        Key{ value: '`', shifted: '~'},
        Key{ value: '1', shifted: '!'},
        Key{ value: '2', shifted: '@'},
        Key{ value: '3', shifted: '#'},
        Key{ value: '4', shifted: '$'},
        Key{ value: '5', shifted: '%'},
        Key{ value: '6', shifted: '^'},
        Key{ value: '7', shifted: '&'},
        Key{ value: '8', shifted: '*'},
        Key{ value: '9', shifted: '('},
        Key{ value: '0', shifted: ')'},
        Key{ value: '-', shifted: '_'},
        Key{ value: '=', shifted: '+'},
        Key{ value: '[', shifted: '{'},
        Key{ value: ']', shifted: '}'},
        Key{ value: '\\', shifted: '|'},
        Key{ value: ';', shifted: ':'},
        Key{ value: '\'', shifted: '\"'},
        Key{ value: ',', shifted: '<'},
        Key{ value: '.', shifted: '>'},
        Key{ value: '/', shifted: '?'}
    ];
    add_remaining_keys(remaining_keys, &mut result);

    connect_keyboard_nodes(qwerty_us, &mut result, KeyboardStyle::Slanted, false);

    result
}


fn generate_dvorak() -> DiGraphMap<Key, Edge> {
    let mut result = DiGraphMap::<Key, Edge>::new();
    // This is a bit nasty but I don't see how to do it nicer..
    // Trailing space after \n represents keyboard offset.
    let qwerty_us = "` 1 2 3 4 5 6 7 8 9 0 [ ]\n\0 ' , . p y f g c r l / = \\\n\0 a o e u i d h t \
                     n s -\n\0 ; q j k x b m w v z";

    add_alphabetics(&mut result);

    let remaining_keys = vec![ 
        Key{ value: '`', shifted: '~'},
        Key{ value: '1', shifted: '!'},
        Key{ value: '2', shifted: '@'},
        Key{ value: '3', shifted: '#'},
        Key{ value: '4', shifted: '$'},
        Key{ value: '5', shifted: '%'},
        Key{ value: '6', shifted: '^'},
        Key{ value: '7', shifted: '&'},
        Key{ value: '8', shifted: '*'},
        Key{ value: '9', shifted: '('},
        Key{ value: '0', shifted: ')'},
        Key{ value: '-', shifted: '_'},
        Key{ value: '=', shifted: '+'},
        Key{ value: '[', shifted: '{'},
        Key{ value: ']', shifted: '}'},
        Key{ value: '\\', shifted: '|'},
        Key{ value: ';', shifted: ':'},
        Key{ value: '\'', shifted: '\"'},
        Key{ value: ',', shifted: '<'},
        Key{ value: '.', shifted: '>'},
        Key{ value: '/', shifted: '?'}
    ];
    add_remaining_keys(remaining_keys, &mut result);

    connect_keyboard_nodes(qwerty_us, &mut result, KeyboardStyle::Slanted, false);

    result
}

fn generate_standard_numpad() -> DiGraphMap<Key, Edge> {
    let mut result = DiGraphMap::<Key, Edge>::new();
    let numpad = "\0 / * -\n7 8 9 +\n4 5 6\n1 2 3\n\0 0 .";

    add_unshifted_number_keys(&mut result);

    connect_keyboard_nodes(numpad, &mut result, KeyboardStyle::Aligned, true);
    result
}


#[test]
fn test_standard_numpad() {
    let g = generate_standard_numpad();
    println!("{:?}",
             Dot::with_config(&g.into_graph::<u32>(), &[Config::EdgeNoLabel]));

}

fn generate_mac_numpad() -> DiGraphMap<Key, Edge> {
    let mut result = DiGraphMap::<Key, Edge>::new();
    let numpad = "\0 = / *\n7 8 9 -\n4 5 6 +\n1 2 3\n\0 0 .";

    connect_keyboard_nodes(numpad, &mut result, KeyboardStyle::Aligned, true);
    result
}