kll-compiler 0.1.2

KLL (Keyboard Layout Language) Compiler - Rust Edition
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#![feature(if_let_guard)]
#![feature(map_try_insert)]
#![allow(incomplete_features)]

pub mod emitters;
pub mod parser;
mod test;
pub mod types;

#[macro_use]
extern crate derive_object_merge;

use object_merge::Merge;
pub use parser::parse_int;
use parser::PestError;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::ops::Range;
use std::path::{Path, PathBuf};
pub use types::{
    Action, Animation, AnimationResult, Capability, Key, KllFile, Mapping, PixelDef, Position,
    ResultList, ResultType, Statement, Trigger, TriggerList, TriggerMode, TriggerType,
};

pub use layouts_rs::Layouts;

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum Value<'a> {
    List(Vec<&'a str>),
    Single(&'a str),
}

#[derive(Debug, Default, Clone, Merge)]
pub struct KllState<'a> {
    #[combine]
    pub defines: HashMap<&'a str, &'a str>,
    #[combine]
    pub variables: HashMap<&'a str, Value<'a>>,
    #[combine]
    pub capabilities: HashMap<&'a str, Capability<'a>>,
    #[combine]
    pub keymap: Vec<Mapping<'a>>,
    #[combine]
    pub positions: HashMap<usize, Position>,
    #[combine]
    pub pixelmap: HashMap<usize, PixelDef>,
    #[combine]
    pub animations: HashMap<&'a str, Animation<'a>>,
}

impl<'a> KllFile<'a> {
    pub fn into_struct(self) -> KllState<'a> {
        let mut kll = KllState::default();
        for statement in self.statements {
            match statement {
                Statement::Define((name, val)) => {
                    kll.defines.insert(name, val);
                }
                Statement::Variable((name, index, val)) => {
                    let entry = kll.variables.entry(name).or_insert_with(|| match index {
                        Some(_) => Value::List(vec![]),
                        None => Value::Single(val),
                    });
                    match entry {
                        Value::List(vec) => {
                            let index = index.unwrap(); // Should be set because this is an array
                            if index >= vec.len() {
                                vec.resize(index + 1, "");
                            }
                            vec[index] = val;
                        }
                        Value::Single(s) => {
                            *s = val;
                        }
                    };
                }
                Statement::Capability((name, cap)) => {
                    kll.capabilities.insert(name, cap);
                }
                Statement::Keymap(mapping) => {
                    kll.keymap.push(mapping);
                }
                Statement::Position((indices, pos)) => {
                    for range in indices {
                        for index in range {
                            kll.positions.insert(index, pos.clone());
                        }
                    }
                }
                Statement::Pixelmap((indices, map)) => {
                    for range in indices {
                        for index in range {
                            kll.pixelmap.insert(index, map.clone());
                        }
                    }
                }
                Statement::Animation((name, anim)) => {
                    kll.animations.insert(name, anim);
                }
                Statement::Frame((name, indices, frame)) => {
                    let animation = kll.animations.entry(name).or_default();
                    let frames = &mut animation.frames;
                    for range in indices {
                        for index in range {
                            if frames.len() <= index {
                                frames.resize(index + 1, vec![]);
                            }
                            frames[index] = frame.clone();
                        }
                    }
                }
                Statement::NOP => {}
            };
        }

        kll
    }
}

impl<'a> KllState<'a> {
    pub fn triggers(&self) -> impl Iterator<Item = &Trigger> + '_ {
        let groups = self
            .keymap
            .iter()
            .map(|Mapping(trigger_groups, _, _)| trigger_groups);
        let combos = groups.into_iter().map(|tl| tl.iter());
        combos.into_iter().flatten()
    }

    pub fn trigger_lists(&self) -> impl Iterator<Item = &TriggerList> + '_ {
        self.keymap
            .iter()
            .map(|Mapping(trigger_groups, _, _)| trigger_groups)
    }

    pub fn actions(&self) -> impl Iterator<Item = &Action> + '_ {
        let groups = self
            .keymap
            .iter()
            .map(|Mapping(_, _, result_groups)| result_groups);
        let combos = groups.into_iter().map(|rl| rl.iter());
        combos.into_iter().flatten()
    }

    pub fn result_lists(&self) -> impl Iterator<Item = &ResultList> + '_ {
        self.keymap
            .iter()
            .map(|Mapping(_, _, result_groups)| result_groups)
    }

    pub fn trigger_result_lists(&self) -> impl Iterator<Item = (&TriggerList, &ResultList)> + '_ {
        self.keymap
            .iter()
            .map(|Mapping(trigger_groups, _, result_groups)| (trigger_groups, result_groups))
    }

    pub fn scancode_map(&self) -> HashMap<&str, usize> {
        self.keymap
            .iter()
            .filter_map(|Mapping(trigger_groups, _, result_groups)| match 1 {
                _ if trigger_groups.iter().count() == 1 && result_groups.iter().count() == 1 => match 1 {
                    _ if let (TriggerType::Key(Key::Scancode(s)), ResultType::Output(Key::Usb(u))) = (&trigger_groups.iter().next().unwrap().trigger, &result_groups.iter().next().unwrap().result)  => Some((*u, *s)),
                    _ => None,
                },
                _ => None
            })
        .collect::<HashMap<&str, usize>>()
    }

    pub fn scancodes(&self) -> Vec<usize> {
        self.triggers()
            .filter_map(|t| match &t.trigger {
                TriggerType::Key(key) => Some(key),
                _ => None,
            })
            .filter_map(|key| match key {
                Key::Scancode(s) => Some(*s),
                _ => None,
            })
            .collect()
    }

    pub fn animations(&self) -> impl Iterator<Item = &AnimationResult> + '_ {
        self.actions().filter_map(|action| match &action.result {
            ResultType::Animation(anim) => Some(anim),
            _ => None,
        })
    }

    pub fn unicode_strings(&self) -> HashSet<String> {
        self.actions()
            .filter_map(|action| match &action.result {
                ResultType::UnicodeText(text) => Some(text.to_string()),
                _ => None,
            })
            .collect()
    }

    pub fn reduce(&self, base: KllState<'a>) -> Vec<Mapping<'a>> {
        let scancode_map = base.scancode_map();
        let mut new_keymap: Vec<Mapping> = self
            .keymap
            .iter()
            .map(|Mapping(trigger_groups, mode, result_groups)| {
                let new_triggers = TriggerList(match mode {
                    TriggerMode::SoftReplace => trigger_groups.0.clone(),
                    _ => trigger_groups
                        .0
                        .iter()
                        .map(|combo| {
                            combo
                                .iter()
                                .map(|t| match &t.trigger {
                                    TriggerType::Key(Key::Usb(u)) => {
                                        let s = scancode_map.get(u).unwrap();
                                        Trigger {
                                            trigger: TriggerType::Key(Key::Scancode(*s)),
                                            state: t.state.clone(),
                                        }
                                    }
                                    _ => t.clone(),
                                })
                                .collect::<Vec<_>>()
                        })
                        .collect::<Vec<_>>(),
                });
                let new_results = ResultList(match mode {
                    TriggerMode::SoftReplace => result_groups.0.clone(),
                    _ => result_groups
                        .0
                        .iter()
                        .map(|combo| {
                            combo
                                .iter()
                                .map(|r| match &r.result {
                                    ResultType::Output(Key::Usb(u)) => Action {
                                        result: ResultType::Capability((
                                            Capability::new("usbKeyOut", vec![u]),
                                            None,
                                        )),
                                        state: r.state.clone(),
                                    },
                                    _ => r.clone(),
                                })
                                .collect::<Vec<_>>()
                        })
                        .collect::<Vec<_>>(),
                });

                Mapping(new_triggers, mode.clone(), new_results)
            })
            .collect::<Vec<_>>();

        new_keymap.sort_by(|a, b| {
            let a = format!("{}", a);
            let b = format!("{}", b);
            alphanumeric_sort::compare_path(a, b)
        });

        new_keymap
    }

    /// Replaces any implied (KLL) state with explicit state
    pub fn generate_state_scheduling(&mut self) {
        // Generate explicit state for each Mapping
        // Determine if we should use Result/Option for implied_state (probably option?)
        // Build new keymap Vector with new keymapping
        let mut new_keymap = Vec::new();
        for mapping in &self.keymap {
            if let Some(mut mapping_vec) = mapping.implied_state() {
                new_keymap.append(&mut mapping_vec);
            } else {
                new_keymap.push(mapping.clone());
            }
        }

        self.keymap = new_keymap;
    }
}

#[derive(Debug, Default, Clone)]
pub struct KllDatastore<'a> {
    pub scancode_range: Range<usize>,
    pub unicode_strings: HashSet<String>,
    pub unique_triggers: HashSet<Trigger<'a>>,
    pub unique_results: HashSet<Action<'a>>,
    pub unique_animations: HashSet<AnimationResult<'a>>,
}

impl<'a> KllDatastore<'a> {
    pub fn get_scancode_range(state: &KllState) -> Range<usize> {
        let mut range = Range {
            start: 0xFFFF,
            end: 0,
        };

        for scancode in state.scancodes() {
            if scancode < range.start {
                range.start = scancode;
            }
            if scancode > range.end {
                range.end = scancode;
            }
        }

        if range.start == 0xFFFF {
            range.start = 0; // No keys found
        }

        assert!(range.start <= range.end);
        range
    }

    pub fn new(state: &'a KllState<'a>) -> KllDatastore<'a> {
        KllDatastore {
            unicode_strings: state.unicode_strings(),
            scancode_range: KllDatastore::get_scancode_range(state),
            unique_triggers: state.triggers().cloned().collect(),
            unique_results: state.actions().cloned().collect(),
            unique_animations: state.animations().cloned().collect(),
        }
    }
}

#[allow(clippy::result_large_err)]
pub fn parse(text: &str) -> Result<KllFile, PestError> {
    KllFile::from_str(text)
}

// Holds owned version of all files
// All other data structures are borrowed from this
pub struct Filestore {
    files: HashMap<PathBuf, String>,
}

impl Filestore {
    pub fn new() -> Self {
        Filestore {
            files: HashMap::new(),
        }
    }
    pub fn load_file(&mut self, path: &Path) {
        //dbg!(&path);
        let raw_text = fs::read_to_string(path).expect("cannot read file");
        self.files.insert(path.to_path_buf(), raw_text);
    }

    pub fn get_file<'a>(&'a self, path: &Path) -> KllState<'a> {
        let raw_text = self.files.get(path).unwrap();
        parse(raw_text).unwrap().into_struct()
    }
}

impl Default for Filestore {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug)]
pub struct KllGroups<'a> {
    config: Vec<KllState<'a>>,
    base: Vec<KllState<'a>>,
    default: Vec<KllState<'a>>,
    partials: Vec<KllState<'a>>,
}

impl<'a> KllGroups<'a> {
    pub fn new(
        filestore: &'a Filestore,
        config: &[PathBuf],
        base: &[PathBuf],
        default: &[PathBuf],
        partials: &[PathBuf],
    ) -> Self {
        KllGroups {
            config: config.iter().map(|p| filestore.get_file(p)).collect(),
            base: base.iter().map(|p| filestore.get_file(p)).collect(),
            default: default.iter().map(|p| filestore.get_file(p)).collect(),
            partials: partials.iter().map(|p| filestore.get_file(p)).collect(),
        }
    }

    pub fn config(&self) -> KllState<'a> {
        let mut configs = self.config.iter();
        let mut config = configs.next().unwrap().clone();
        for c in configs {
            config.merge(c);
        }
        config
    }

    pub fn basemap(&self) -> KllState<'a> {
        let mut layouts = self.base.iter();
        let mut layout = layouts.next().unwrap().clone();
        for base in layouts {
            layout.merge(base);
        }
        layout
    }
    pub fn defaultmap(&self) -> KllState<'a> {
        let mut layout = self.basemap();
        for default in &self.default {
            layout.merge(default);
        }
        layout
    }
    pub fn partialmaps(&self) -> Vec<KllState<'a>> {
        let mut partials: Vec<KllState> = vec![];
        for partial in &self.partials {
            let mut layout = self.basemap();
            layout.merge(partial);
            partials.push(layout);
        }

        partials
    }
    /// Combines default and partialmaps as a list of layers (defaultmap being 0)
    pub fn layers(&self) -> Vec<KllState<'a>> {
        let mut layers: Vec<KllState> = vec![self.defaultmap()];
        for layer in self.partialmaps() {
            layers.push(layer);
        }

        layers
    }
}