Skip to content

Commit

Permalink
keyboard exclude and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo committed Jun 23, 2024
1 parent 5aca074 commit d1bb3ff
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 56 deletions.
109 changes: 63 additions & 46 deletions parser/src/cfg/defcfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ impl Default for CfgLinuxOptions {
}
}

#[cfg(any(
all(feature = "interception_driver", target_os = "windows"),
target_os = "unknown"
))]
#[derive(Debug, Clone, Default)]
pub struct CfgWinterceptOptions {
pub windows_interception_mouse_hwids: Option<Vec<[u8; HWID_ARR_SZ]>>,
pub windows_interception_mouse_hwids_exclude: Option<Vec<[u8; HWID_ARR_SZ]>>,
pub windows_interception_keyboard_hwids: Option<Vec<[u8; HWID_ARR_SZ]>>,
pub windows_interception_keyboard_hwids_exclude: Option<Vec<[u8; HWID_ARR_SZ]>>,
}

#[cfg(all(any(target_os = "windows", target_os = "unknown"), feature = "gui"))]
#[derive(Debug, Clone)]
pub struct CfgOptionsGui {
Expand Down Expand Up @@ -114,22 +126,7 @@ pub struct CfgOptions {
all(feature = "interception_driver", target_os = "windows"),
target_os = "unknown"
))]
pub windows_interception_mouse_hwids: Option<Vec<[u8; HWID_ARR_SZ]>>,
#[cfg(any(
all(feature = "interception_driver", target_os = "windows"),
target_os = "unknown"
))]
pub windows_interception_mouse_hwids_exclude: Option<Vec<[u8; HWID_ARR_SZ]>>,
#[cfg(any(
all(feature = "interception_driver", target_os = "windows"),
target_os = "unknown"
))]
pub windows_interception_keyboard_hwids: Option<Vec<[u8; HWID_ARR_SZ]>>,
#[cfg(any(
all(feature = "interception_driver", target_os = "windows"),
target_os = "unknown"
))]
pub windows_interception_keyboard_hwids_exclude: Option<Vec<[u8; HWID_ARR_SZ]>>,
pub wintercept_opts: CfgWinterceptOptions,
#[cfg(any(target_os = "macos", target_os = "unknown"))]
pub macos_dev_names_include: Option<Vec<String>>,
#[cfg(all(any(target_os = "windows", target_os = "unknown"), feature = "gui"))]
Expand Down Expand Up @@ -165,22 +162,7 @@ impl Default for CfgOptions {
all(feature = "interception_driver", target_os = "windows"),
target_os = "unknown"
))]
windows_interception_mouse_hwids: None,
#[cfg(any(
all(feature = "interception_driver", target_os = "windows"),
target_os = "unknown"
))]
windows_interception_mouse_hwids_exclude: None,
#[cfg(any(
all(feature = "interception_driver", target_os = "windows"),
target_os = "unknown"
))]
windows_interception_keyboard_hwids: None,
#[cfg(any(
all(feature = "interception_driver", target_os = "windows"),
target_os = "unknown"
))]
windows_interception_keyboard_hwids_exclude: None,
wintercept_opts: Default::default(),
#[cfg(any(target_os = "macos", target_os = "unknown"))]
macos_dev_names_include: None,
#[cfg(all(any(target_os = "windows", target_os = "unknown"), feature = "gui"))]
Expand Down Expand Up @@ -348,7 +330,11 @@ pub fn parse_defcfg(expr: &[SExpr]) -> Result<CfgOptions> {
target_os = "unknown"
))]
{
if cfg.windows_interception_mouse_hwids_exclude.is_some() {
if cfg
.wintercept_opts
.windows_interception_mouse_hwids_exclude
.is_some()
{
bail_expr!(val, "{label} and windows-interception-mouse-hwid-exclude cannot both be included");
}
let v = sexpr_to_str_or_err(val, label)?;
Expand All @@ -371,15 +357,21 @@ pub fn parse_defcfg(expr: &[SExpr]) -> Result<CfgOptions> {
hwid[i] = b;
Ok(hwid)
})?;
match cfg.windows_interception_mouse_hwids.as_mut() {
match cfg
.wintercept_opts
.windows_interception_mouse_hwids
.as_mut()
{
Some(v) => {
v.push(hwid_slice);
}
None => {
cfg.windows_interception_mouse_hwids = Some(vec![hwid_slice]);
cfg.wintercept_opts.windows_interception_mouse_hwids =
Some(vec![hwid_slice]);
}
}
cfg.windows_interception_mouse_hwids
cfg.wintercept_opts
.windows_interception_mouse_hwids
.as_mut()
.unwrap()
.shrink_to_fit();
Expand All @@ -391,23 +383,33 @@ pub fn parse_defcfg(expr: &[SExpr]) -> Result<CfgOptions> {
target_os = "unknown"
))]
{
if cfg.windows_interception_mouse_hwids_exclude.is_some() {
if cfg
.wintercept_opts
.windows_interception_mouse_hwids_exclude
.is_some()
{
bail_expr!(val, "{label} and windows-interception-mouse-hwid-exclude cannot both be included");
}
let parsed_hwids = sexpr_to_hwids_vec(
val,
label,
"entry in windows-interception-mouse-hwids",
)?;
match cfg.windows_interception_mouse_hwids.as_mut() {
match cfg
.wintercept_opts
.windows_interception_mouse_hwids
.as_mut()
{
Some(v) => {
v.extend(parsed_hwids);
}
None => {
cfg.windows_interception_mouse_hwids = Some(parsed_hwids);
cfg.wintercept_opts.windows_interception_mouse_hwids =
Some(parsed_hwids);
}
}
cfg.windows_interception_mouse_hwids
cfg.wintercept_opts
.windows_interception_mouse_hwids
.as_mut()
.unwrap()
.shrink_to_fit();
Expand All @@ -419,15 +421,20 @@ pub fn parse_defcfg(expr: &[SExpr]) -> Result<CfgOptions> {
target_os = "unknown"
))]
{
if cfg.windows_interception_mouse_hwids.is_some() {
if cfg
.wintercept_opts
.windows_interception_mouse_hwids
.is_some()
{
bail_expr!(val, "{label} and windows-interception-mouse-hwid(s) cannot both be used");
}
let parsed_hwids = sexpr_to_hwids_vec(
val,
label,
"entry in windows-interception-mouse-hwids-exclude",
)?;
cfg.windows_interception_mouse_hwids_exclude = Some(parsed_hwids);
cfg.wintercept_opts.windows_interception_mouse_hwids_exclude =
Some(parsed_hwids);
}
}
"windows-interception-keyboard-hwids" => {
Expand All @@ -436,15 +443,20 @@ pub fn parse_defcfg(expr: &[SExpr]) -> Result<CfgOptions> {
target_os = "unknown"
))]
{
if cfg.windows_interception_keyboard_hwids_exclude.is_some() {
if cfg
.wintercept_opts
.windows_interception_keyboard_hwids_exclude
.is_some()
{
bail_expr!(val, "{label} and windows-interception-keyboard-hwid-exclude cannot both be used");
}
let parsed_hwids = sexpr_to_hwids_vec(
val,
label,
"entry in windows-interception-keyboard-hwids",
)?;
cfg.windows_interception_keyboard_hwids = Some(parsed_hwids);
cfg.wintercept_opts.windows_interception_keyboard_hwids =
Some(parsed_hwids);
}
}
"windows-interception-keyboard-hwids-exclude" => {
Expand All @@ -453,15 +465,20 @@ pub fn parse_defcfg(expr: &[SExpr]) -> Result<CfgOptions> {
target_os = "unknown"
))]
{
if cfg.windows_interception_keyboard_hwids.is_some() {
if cfg
.wintercept_opts
.windows_interception_keyboard_hwids
.is_some()
{
bail_expr!(val, "{label} and windows-interception-keyboard-hwid cannot both be used");
}
let parsed_hwids = sexpr_to_hwids_vec(
val,
label,
"entry in windows-interception-keyboard-hwids-exclude",
)?;
cfg.windows_interception_keyboard_hwids_exclude = Some(parsed_hwids);
cfg.wintercept_opts
.windows_interception_keyboard_hwids_exclude = Some(parsed_hwids);
}
}
"macos-dev-names-include" => {
Expand Down
42 changes: 34 additions & 8 deletions src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,15 @@ pub struct Kanata {
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
/// Used to know which mouse input devices to exclude from processing inputs by kanata. This is
/// mutually exclusive from `intercept_mouse_hwids` and kanata will panic if both are included.
intercept_mouse_hwids_excluded: Option<Vec<[u8; HWID_ARR_SZ]>>,
intercept_mouse_hwids_exclude: Option<Vec<[u8; HWID_ARR_SZ]>>,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
/// Used to know which input device to treat as a mouse for intercepting and processing inputs
/// Used to know which input device to treat as a keyboard for intercepting and processing inputs
/// by kanata.
intercept_kb_hwids: Option<Vec<[u8; HWID_ARR_SZ]>>,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
/// Used to know which keyboard input devices to exclude from processing inputs by kanata. This
/// is mutually exclusive from `intercept_kb_hwids` and kanata will panic if both are included.
intercept_kb_hwids_exclude: Option<Vec<[u8; HWID_ARR_SZ]>>,
/// User configuration to do logging of layer changes or not.
log_layer_changes: bool,
/// Tracks the caps-word state. Is Some(...) if caps-word is active and None otherwise.
Expand Down Expand Up @@ -351,11 +355,22 @@ impl Kanata {
#[cfg(target_os = "linux")]
exclude_names: cfg.options.linux_opts.linux_dev_names_exclude,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
intercept_mouse_hwids: cfg.options.windows_interception_mouse_hwids,
intercept_mouse_hwids: cfg.options.wintercept_opts.windows_interception_mouse_hwids,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
intercept_mouse_hwids_excluded: cfg.options.windows_interception_mouse_hwids_exclude,
intercept_mouse_hwids_exclude: cfg
.options
.wintercept_opts
.windows_interception_mouse_hwids_exclude,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
intercept_kb_hwids: cfg.options.windows_interception_keyboard_hwids,
intercept_kb_hwids: cfg
.options
.wintercept_opts
.windows_interception_keyboard_hwids,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
intercept_kb_hwids_exclude: cfg
.options
.wintercept_opts
.windows_interception_keyboard_hwids_exclude,
dynamic_macro_replay_state: None,
dynamic_macro_record_state: None,
dynamic_macros: Default::default(),
Expand Down Expand Up @@ -453,11 +468,22 @@ impl Kanata {
#[cfg(target_os = "linux")]
exclude_names: cfg.options.linux_opts.linux_dev_names_exclude,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
intercept_mouse_hwids: cfg.options.windows_interception_mouse_hwids,
intercept_mouse_hwids: cfg.options.wintercept_opts.windows_interception_mouse_hwids,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
intercept_mouse_hwids_exclude: cfg
.options
.wintercept_opts
.windows_interception_mouse_hwids_exclude,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
intercept_mouse_hwids_excluded: cfg.options.windows_interception_mouse_hwids_exclude,
intercept_kb_hwids: cfg
.options
.wintercept_opts
.windows_interception_keyboard_hwids,
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
intercept_kb_hwids: cfg.options.windows_interception_keyboard_hwids,
intercept_kb_hwids_exclude: cfg
.options
.wintercept_opts
.windows_interception_keyboard_hwids_exclude,
dynamic_macro_replay_state: None,
dynamic_macro_record_state: None,
dynamic_macros: Default::default(),
Expand Down
5 changes: 3 additions & 2 deletions src/kanata/windows/interception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ impl Kanata {
}; 32];

let keyboards_to_intercept_hwids = kanata.lock().intercept_kb_hwids.clone();
let keyboards_to_intercept_hwids_exclude = kanata.lock().intercept_kb_hwids_exclude.clone();
let mouse_to_intercept_hwids: Option<Vec<[u8; HWID_ARR_SZ]>> =
kanata.lock().intercept_mouse_hwids.clone();
let mouse_to_intercept_excluded_hwids: Option<Vec<[u8; HWID_ARR_SZ]>> =
kanata.lock().intercept_mouse_hwids_excluded.clone();
kanata.lock().intercept_mouse_hwids_exclude.clone();
if mouse_to_intercept_hwids.is_some() || mouse_to_intercept_excluded_hwids.is_some() {
intrcptn.set_filter(
ic::is_mouse,
Expand All @@ -42,7 +43,7 @@ impl Kanata {
dev,
&intrcptn,
&keyboards_to_intercept_hwids,
&None,
&keyboards_to_intercept_hwids_exclude,
&mut is_dev_interceptable,
) {
log::debug!("stroke {:?} is from undesired device", strokes[i]);
Expand Down

0 comments on commit d1bb3ff

Please sign in to comment.