Skip to content

Commit

Permalink
Merge pull request #606 from chewing/workman
Browse files Browse the repository at this point in the history
feat(editor): Add workman layout support
  • Loading branch information
kanru authored Jul 14, 2024
2 parents a40f7c7 + 2a874ec commit e3caee6
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions capi/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ pub unsafe extern "C" fn chewing_config_set_str(
AnyKeyboardLayout::colemak_dh_orth(),
Box::new(Standard::new()),
),
KB::Workman => (AnyKeyboardLayout::workman(), Box::new(Standard::new())),
};
ctx.keyboard = keyboard;
ctx.editor.set_syllable_editor(syl);
Expand Down Expand Up @@ -614,6 +615,7 @@ pub unsafe extern "C" fn chewing_set_KBType(ctx: *mut ChewingContext, kbtype: c_
AnyKeyboardLayout::colemak_dh_orth(),
Box::new(Standard::new()),
),
KB::Workman => (AnyKeyboardLayout::workman(), Box::new(Standard::new())),
};
ctx.kb_compat = kb_compat;
ctx.keyboard = keyboard;
Expand Down
3 changes: 3 additions & 0 deletions capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ pub mod layout {
/// * KB_THL_PINYIN
/// * KB_MPS2_PINYIN
/// * KB_CARPALX
/// * KB_COLEMAK_DH_ANSI
/// * KB_COLEMAK_DH_ORTH
/// * KB_WORKMAN
///
/// See also [chewing_kbtype_Enumerate] for getting the list of supported
/// layouts programmatically.
Expand Down
1 change: 1 addition & 0 deletions capi/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub enum KB {
Carpalx,
ColemakDhAnsi,
ColemakDhOrth,
Workman,
}

/// Opaque context handle used for chewing APIs.
Expand Down
1 change: 1 addition & 0 deletions include/chewing.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ typedef enum KB {
KB_CARPALX,
KB_COLEMAK_DH_ANSI,
KB_COLEMAK_DH_ORTH,
KB_WORKMAN,
KB_TYPE_NUM,
} KB;

Expand Down
7 changes: 7 additions & 0 deletions src/editor/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod dvorak;
mod dvorak_on_qwerty;
mod qgmlwy;
mod qwerty;
mod workman;

use core::fmt;

Expand All @@ -20,6 +21,7 @@ pub use dvorak::Dvorak;
pub use dvorak_on_qwerty::DvorakOnQwerty;
pub use qgmlwy::Qgmlwy;
pub use qwerty::Qwerty;
pub use workman::Workman;

const MATRIX_SIZE: usize = 63;

Expand Down Expand Up @@ -139,6 +141,7 @@ pub enum AnyKeyboardLayout {
Qgmlwy(Qgmlwy),
ColemakDhAnsi(ColemakDhAnsi),
ColemakDhOrth(ColemakDhOrth),
Workman(Workman),
}

impl AnyKeyboardLayout {
Expand All @@ -160,6 +163,9 @@ impl AnyKeyboardLayout {
pub fn colemak_dh_orth() -> AnyKeyboardLayout {
AnyKeyboardLayout::ColemakDhOrth(ColemakDhOrth)
}
pub fn workman() -> AnyKeyboardLayout {
AnyKeyboardLayout::Workman(Workman)
}
}

impl KeyboardLayout for AnyKeyboardLayout {
Expand All @@ -171,6 +177,7 @@ impl KeyboardLayout for AnyKeyboardLayout {
AnyKeyboardLayout::Qgmlwy(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::ColemakDhAnsi(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::ColemakDhOrth(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::Workman(kb) => kb.map_with_mod(keycode, modifiers),
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/editor/keyboard/workman.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::{
generic_map_keycode,
KeyCode::{self, *},
KeyEvent, KeyboardLayout, Modifiers, MATRIX_SIZE,
};

/// A Workman keyboard.
#[derive(Debug)]
pub struct Workman;

#[rustfmt::skip]
pub(crate) static KEYCODE_INDEX: [KeyCode; MATRIX_SIZE] = [
Unknown,
N1, N2, N3, N4, N5, N6, N7, N8, N9, N0, Minus, Equal, BSlash, Grave,
Q, D, R, W, B, J, F, U, P, SColon, LBracket, RBracket,
A, S, H, T, G, Y, N, E, O, I, Quote,
Z, X, M, C, V, K, L, Comma, Dot, Slash, Space,
Esc, Enter, Del, Backspace, Tab, Left, Right, Up, Down, Home, End,
PageUp, PageDown, NumLock,
];

#[rustfmt::skip]
pub(crate) static UNICODE_MAP: [char; MATRIX_SIZE] = [
'�',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\\', '`',
'q', 'd', 'r', 'w', 'b', 'j', 'f', 'u', 'p', ';', '[', ']',
'a', 's', 'h', 't', 'g', 'y', 'n', 'e', 'o', 'i', '\'',
'z', 'x', 'm', 'c', 'v', 'k', 'l', ',', '.', '/', ' ',
'�', '�', '�', '�', '�', '�', '�', '�', '�', '�',
'�', '�', '�', '�',
];

#[rustfmt::skip]
pub(crate) static SHIFT_MAP: [char; MATRIX_SIZE] = [
'�',
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '|', '~',
'Q', 'D', 'R', 'W', 'B', 'J', 'F', 'U', 'P', ':', '{', '}',
'A', 'S', 'H', 'T', 'G', 'Y', 'N', 'E', 'O', 'I', '"',
'Z', 'X', 'M', 'C', 'V', 'K', 'L', '<', '>', '?', ' ',
'�', '�', '�', '�', '�', '�', '�', '�', '�', '�',
'�', '�', '�', '�',
];

impl KeyboardLayout for Workman {
fn map_with_mod(&self, keycode: KeyCode, modifiers: Modifiers) -> KeyEvent {
generic_map_keycode(&KEYCODE_INDEX, &UNICODE_MAP, &SHIFT_MAP, keycode, modifiers)
}
}
5 changes: 5 additions & 0 deletions src/editor/zhuyin_layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub enum KeyboardLayoutCompat {
ColemakDhAnsi,
/// TODO: docs
ColemakDhOrth,
/// Workman standard layout
Workman,
}

#[derive(Debug)]
Expand Down Expand Up @@ -109,6 +111,7 @@ impl FromStr for KeyboardLayoutCompat {
"KB_CARPALX" => Self::Carpalx,
"KB_COLEMAK_DH_ANSI" => Self::ColemakDhAnsi,
"KB_COLEMAK_DH_ORTH" => Self::ColemakDhOrth,
"KB_WORKMAN" => Self::Workman,
_ => return Err(ParseKeyboardLayoutError),
};
Ok(layout)
Expand All @@ -133,6 +136,7 @@ impl Display for KeyboardLayoutCompat {
KeyboardLayoutCompat::Carpalx => f.write_str("KB_CARPALX"),
KeyboardLayoutCompat::ColemakDhAnsi => f.write_str("KB_COLEMAK_DH_ANSI"),
KeyboardLayoutCompat::ColemakDhOrth => f.write_str("KB_COLEMAK_DH_ORTH"),
KeyboardLayoutCompat::Workman => f.write_str("KB_WORKMAN"),
}
}
}
Expand All @@ -157,6 +161,7 @@ impl TryFrom<u8> for KeyboardLayoutCompat {
12 => Self::Carpalx,
13 => Self::ColemakDhAnsi,
14 => Self::ColemakDhOrth,
15 => Self::Workman,
_ => return Err(()),
})
}
Expand Down
1 change: 1 addition & 0 deletions tests/test-keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static const char *const KEYBOARD_STRING[] = {
"KB_CARPALX",
"KB_COLEMAK_DH_ANSI",
"KB_COLEMAK_DH_ORTH",
"KB_WORKMAN"
};

static const int KEYBOARD_DEFAULT_TYPE = 0;
Expand Down

0 comments on commit e3caee6

Please sign in to comment.