kanata 1.11.0

Multi-layer keyboard customization
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Output that just prints text to stdout instead of actually doing anything OS-related.
//! See <../../docs/simulated_output/sim_out.txt> for an example output.
use indoc::formatdoc;
use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};

pub fn concat_os_str2(a: &OsStr, b: &OsStr) -> OsString {
    let mut ret = OsString::with_capacity(a.len() + b.len()); // allocate once
    ret.push(a);
    ret.push(b); // doesn't allocate
    ret
}
fn append_file_name(path: impl AsRef<Path>, appendix: impl AsRef<OsStr>) -> PathBuf {
    let path = path.as_ref();
    let mut result = path.to_owned();
    let stem_in = path.file_stem().unwrap_or(OsStr::new(""));
    let stem_out = concat_os_str2(stem_in, OsStr::new(&appendix));
    result.set_file_name(stem_out);
    if let Some(ext) = path.extension() {
        result.set_extension(ext);
    }
    result
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum LogFmtT {
    InKeyUp,
    InKeyDown,
    InKeyRep,
    InTick,
    KeyUp,
    KeyDown,
    Tick,
    MouseUp,
    MouseDown,
    MouseMove,
    Unicode,
    Code,
    RawUp,
    RawDown,
}

pub struct LogFmt {
    ticks: u64,
    //In       	//
    in_time: String,
    in_key_up: String,
    in_key_down: String,
    in_key_rep: String,
    in_combo: String,
    //Out      	//
    time: String,
    key_up: String,
    key_down: String,
    raw_up: String,
    raw_down: String,
    combo: String,
    mouse_up: String,
    mouse_down: String,
    mouse_move: String,
    unicode: String,
    code: String,
}
impl Default for LogFmt {
    fn default() -> Self {
        Self::new()
    }
}
impl LogFmt {
    pub fn new() -> Self {
        Self {
            ticks: 0,
            //In       	//
            in_time: String::new(),
            in_key_up: String::new(),
            in_key_down: String::new(),
            in_key_rep: String::new(),
            in_combo: String::new(),
            //Out      	//
            time: String::new(),
            key_up: String::new(),
            key_down: String::new(),
            raw_up: String::new(),
            raw_down: String::new(),
            mouse_up: String::new(),
            mouse_down: String::new(),
            mouse_move: String::new(),
            unicode: String::new(),
            code: String::new(),
            combo: String::new(),
        }
    }
    pub fn fmt(&mut self, key: LogFmtT, value: String) {
        let mut pad = value.len();
        let mut time = "".to_string();
        if self.ticks > 0 {
            pad = std::cmp::max(value.len(), self.ticks.to_string().len()); // add extra padding if
            // event tick is wider
            time = format!("  {: <pad$}", self.ticks);
            self.ticks = 0;
        }
        let blank = format!("  {: <pad$}", ""); //+␠ to allow combo log indicator
        let val = format!("  {value: <pad$}");
        self.in_time += if key == LogFmtT::InTick {
            self.combo += &blank;
            self.in_combo += &format!(" 🕐{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.in_key_up += if key == LogFmtT::InKeyUp {
            self.combo += &blank;
            self.in_combo += &format!(" ↑{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.in_key_down += if key == LogFmtT::InKeyDown {
            self.combo += &blank;
            self.in_combo += &format!(" ↓{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.in_key_rep += if key == LogFmtT::InKeyRep {
            self.combo += &blank;
            self.in_combo += &format!(" ⟳{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.time += if !time.is_empty() { &time } else { &blank };
        self.key_up += if key == LogFmtT::KeyUp {
            self.in_combo += &blank;
            self.combo += &format!(" ↑{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.key_down += if key == LogFmtT::KeyDown {
            self.in_combo += &blank;
            self.combo += &format!(" ↓{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.mouse_up += if key == LogFmtT::MouseUp {
            self.in_combo += &blank;
            self.combo += &format!(" ↑{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.mouse_down += if key == LogFmtT::MouseDown {
            self.in_combo += &blank;
            self.combo += &format!(" ↓{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.mouse_move += if key == LogFmtT::MouseMove {
            self.in_combo += &blank;
            self.combo += &val;
            &val
        } else {
            &blank
        };
        self.raw_up += if key == LogFmtT::RawUp {
            self.in_combo += &blank;
            self.combo += &format!(" ↑{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.raw_down += if key == LogFmtT::RawDown {
            self.in_combo += &blank;
            self.combo += &format!(" ↓{value: <pad$}");
            &val
        } else {
            &blank
        };
        self.unicode += if key == LogFmtT::Unicode {
            self.in_combo += &blank;
            self.combo += &val;
            &val
        } else {
            &blank
        };
        self.code += if key == LogFmtT::Code {
            self.in_combo += &blank;
            self.combo += &val;
            &val
        } else {
            &blank
        };
    }

    #[cfg(any(target_os = "linux", target_os = "android"))]
    pub fn write_raw(&mut self, event: InputEvent) {
        let key_name = KeyCode::from(OsCode::from(event.code));
        if event.up {
            self.fmt(LogFmtT::RawUp, key_name.to_string())
        } else {
            self.fmt(LogFmtT::RawDown, key_name.to_string())
        }
    }
    pub fn in_tick(&mut self, t: u128) {
        self.fmt(LogFmtT::InTick, t.to_string())
    }
    pub fn in_press_key(&mut self, key: OsCode) {
        self.fmt(LogFmtT::InKeyDown, KeyCode::from(key).to_string())
    }
    pub fn in_release_key(&mut self, key: OsCode) {
        self.fmt(LogFmtT::InKeyUp, KeyCode::from(key).to_string())
    }
    pub fn in_repeat_key(&mut self, key: OsCode) {
        self.fmt(LogFmtT::InKeyRep, KeyCode::from(key).to_string())
    }
    pub fn press_key(&mut self, key: OsCode) {
        self.fmt(LogFmtT::KeyDown, KeyCode::from(key).to_string())
    }
    pub fn release_key(&mut self, key: OsCode) {
        self.fmt(LogFmtT::KeyUp, KeyCode::from(key).to_string())
    }
    pub fn send_unicode(&mut self, c: char) {
        self.fmt(LogFmtT::Unicode, c.to_string())
    }
    pub fn click_btn(&mut self, btn: Btn) {
        self.fmt(LogFmtT::MouseDown, btn.to_string())
    }
    pub fn release_btn(&mut self, btn: Btn) {
        self.fmt(LogFmtT::MouseUp, btn.to_string())
    }
    pub fn set_mouse(&mut self, x: u16, y: u16) {
        self.fmt(LogFmtT::MouseMove, format!("@{x},{y}"))
    }
    pub fn scroll(&mut self, dir: MWheelDirection, dist: u16) {
        self.fmt(LogFmtT::MouseMove, format!("{dir}{dist}"))
    }
    pub fn move_mouse(&mut self, dir: MoveDirection, dist: u16) {
        self.fmt(LogFmtT::MouseMove, format!("{dir}{dist}"))
    }
    pub fn write_code(&mut self, code: u32, value: KeyValue) {
        self.fmt(LogFmtT::Code, format!("{code};{value:?}"))
    }

    pub fn end(&self, in_path: &PathBuf, appendix: Option<String>) {
        let pad = self.combo.len().saturating_sub(3);
        let table_out = formatdoc!(
            "🕐Δms│{}
          In───┼{:─<pad$}
           k↑  │{}
           k↓  │{}
           k⟳  │{}
          Σin  │{}
          Out──┼{:─<pad$}
           k↑  │{}
           k↓  │{}
           🖰↑  │{}
           🖰↓  │{}
           🖰   │{}
           🔣  │{}
           code│{}
           raw↑│{}
           raw↓│{}
          Σout │{}
          ",
            self.time,
            "",
            self.in_key_up,
            self.in_key_down,
            self.in_key_rep,
            self.in_combo,
            "",
            self.key_up,
            self.key_down,
            self.mouse_up,
            self.mouse_down,
            self.mouse_move,
            self.unicode,
            self.code,
            self.raw_up,
            self.raw_down,
            self.combo
        );
        eprintln!("{table_out}");
        if let Some(appendix_s) = appendix {
            let out_path = append_file_name(in_path, appendix_s);
            let out_path_s = out_path.display();
            let mut out_file = match File::create(&out_path) {
                Err(e) => panic!("✗ Couldn't create {out_path_s}: {e}"),
                Ok(out_file) => out_file,
            };
            match out_file.write_all(table_out.as_bytes()) {
                Err(e) => panic!("✗ Couldn't write to {out_path_s}: {e}"),
                Ok(_) => eprintln!("Saved output → {out_path_s}"),
            }
        }
    }
}

use super::*;

use crate::kanata::CalculatedMouseMove;
use kanata_parser::custom_action::*;

use std::io;

use kanata_keyberon::key_code::KeyCode;
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
use std::fmt;

pub struct Outputs {
    pub events: Vec<String>,
    ticks: u64,
}

impl Outputs {
    fn new() -> Self {
        Self {
            events: vec![],
            ticks: 0,
        }
    }

    fn push(&mut self, event: impl AsRef<str>) {
        if self.ticks > 0 {
            self.events.push(format!("t:{}ms", self.ticks));
        }
        self.events.push(event.as_ref().to_string());
        self.ticks = 0;
    }
}

/// Handle for writing keys to the OS.
pub struct KbdOut {
    pub log: LogFmt,
    pub outputs: Outputs,
}

impl KbdOut {
    fn new_actual() -> Result<Self, io::Error> {
        Ok(Self {
            log: LogFmt::new(),
            outputs: Outputs::new(),
        })
    }

    #[cfg(not(any(target_os = "linux", target_os = "android")))]
    pub fn new() -> Result<Self, io::Error> {
        Self::new_actual()
    }
    #[cfg(any(target_os = "linux", target_os = "android"))]
    pub fn new(
        _s: &Option<String>,
        _tp: bool,
        _name: &str,
        _bustype: evdev::BusType,
    ) -> Result<Self, io::Error> {
        Self::new_actual()
    }
    #[cfg(any(target_os = "linux", target_os = "android"))]
    pub fn write_raw(&mut self, event: InputEvent) -> Result<(), io::Error> {
        self.log.write_raw(event);
        self.outputs.push(format!("out-raw:{event:?}"));
        Ok(())
    }
    pub fn write(&mut self, event: InputEvent) -> Result<(), io::Error> {
        self.outputs.push(format!("out:{event}"));
        Ok(())
    }
    pub fn write_key(&mut self, key: OsCode, value: KeyValue) -> Result<(), io::Error> {
        let key_ev = KeyEvent::new(key, value);
        let event = {
            #[cfg(target_os = "macos")]
            {
                key_ev.try_into().unwrap()
            }
            #[cfg(not(target_os = "macos"))]
            {
                key_ev.into()
            }
        };
        self.write(event)
    }
    pub fn write_code(&mut self, code: u32, value: KeyValue) -> Result<(), io::Error> {
        self.log.write_code(code, value);
        self.outputs.push(format!("out-code:{code};{value:?}"));
        Ok(())
    }
    pub fn press_key(&mut self, key: OsCode) -> Result<(), io::Error> {
        self.log.press_key(key);
        self.write_key(key, KeyValue::Press)
    }
    pub fn release_key(&mut self, key: OsCode) -> Result<(), io::Error> {
        self.log.release_key(key);
        self.write_key(key, KeyValue::Release)
    }
    pub fn send_unicode(&mut self, c: char) -> Result<(), io::Error> {
        self.log.send_unicode(c);
        self.outputs.push(format!("outU:{c}"));
        Ok(())
    }
    pub fn click_btn(&mut self, btn: Btn) -> Result<(), io::Error> {
        self.log.click_btn(btn);
        self.outputs.push(format!("out🖰:↓{btn:?}"));
        Ok(())
    }
    pub fn release_btn(&mut self, btn: Btn) -> Result<(), io::Error> {
        self.log.release_btn(btn);
        self.outputs.push(format!("out🖰:↑{btn:?}"));
        Ok(())
    }
    pub fn scroll(&mut self, direction: MWheelDirection, distance: u16) -> Result<(), io::Error> {
        self.log.scroll(direction, distance);
        self.outputs
            .push(format!("scroll:{direction:?},{distance:?}"));
        Ok(())
    }
    pub fn move_mouse(&mut self, mv: CalculatedMouseMove) -> Result<(), io::Error> {
        let (direction, distance) = (mv.direction, mv.distance);
        self.log.move_mouse(direction, distance);
        self.outputs
            .push(format!("out🖰:move {direction:?},{distance:?}"));
        Ok(())
    }
    pub fn move_mouse_many(&mut self, moves: &[CalculatedMouseMove]) -> Result<(), io::Error> {
        for mv in moves {
            let (direction, distance) = (&mv.direction, &mv.distance);
            self.log.move_mouse(*direction, *distance);
            self.outputs
                .push(format!("out🖰:move {direction:?},{distance:?}"));
        }
        Ok(())
    }
    pub fn set_mouse(&mut self, x: u16, y: u16) -> Result<(), io::Error> {
        self.log.set_mouse(x, y);
        log::info!("out🖰:@{x},{y}");
        Ok(())
    }
    pub fn tick(&mut self) {
        self.outputs.ticks += 1;
        self.log.ticks += 1;
    }
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
#[derive(Debug, Clone, Copy)]
pub struct InputEvent {
    pub code: u32,
    /// Key was released
    pub up: bool,
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
impl fmt::Display for InputEvent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let direction = if self.up { "" } else { "" };
        let key_name = KeyCode::from(OsCode::from(self.code));
        write!(f, "{direction}{key_name:?}")
    }
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
impl InputEvent {
    pub fn from_oscode(code: OsCode, val: KeyValue) -> Self {
        Self {
            code: code.into(),
            up: val.into(),
        }
    }
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
impl TryFrom<InputEvent> for KeyEvent {
    type Error = ();
    fn try_from(item: InputEvent) -> Result<Self, Self::Error> {
        Ok(Self {
            code: OsCode::from_u16(item.code as u16).ok_or(())?,
            value: match item.up {
                true => KeyValue::Release,
                false => KeyValue::Press,
            },
        })
    }
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
impl From<KeyEvent> for InputEvent {
    fn from(item: KeyEvent) -> Self {
        Self {
            code: item.code.into(),
            up: item.value.into(),
        }
    }
}

#[cfg(all(target_os = "windows", feature = "interception_driver"))]
impl From<KeyEvent> for InputEvent {
    fn from(_item: KeyEvent) -> Self {
        unimplemented!()
    }
}