Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Interception builds of the Windows GUI app #1053

Merged
merged 5 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ build_release_windows output_dir:
cargo build --release --features passthru_ahk --package=simulated_passthru; cp target/release/kanata_passthru.dll "{{output_dir}}\kanata_passthru.dll"
cargo build --release --features win_manifest,gui ; cp target/release/kanata.exe "{{output_dir}}\kanata_gui.exe"
cargo build --release --features win_manifest,gui,cmd; cp target/release/kanata.exe "{{output_dir}}\kanata_gui_cmd_allowed.exe"
cargo build --release --features win_manifest,gui ,interception_driver; cp target/release/kanata.exe "{{output_dir}}\kanata_gui_wintercept.exe"
cargo build --release --features win_manifest,gui,cmd,interception_driver; cp target/release/kanata.exe "{{output_dir}}\kanata_gui_wintercept_cmd_allowed.exe"
cp cfg_samples/kanata.kbd "{{output_dir}}"

# Generate the sha256sums for all files in the output directory
Expand Down
1 change: 1 addition & 0 deletions src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pub static GUI_TX: OnceLock<native_windows_gui::NoticeSender> = OnceLock::new();
pub static GUI_CFG_TX: OnceLock<native_windows_gui::NoticeSender> = OnceLock::new();
pub static GUI_ERR_TX: OnceLock<native_windows_gui::NoticeSender> = OnceLock::new();
pub static GUI_ERR_MSG_TX: OnceLock<ASender<(String, String)>> = OnceLock::new();
pub static GUI_EXIT_TX: OnceLock<native_windows_gui::NoticeSender> = OnceLock::new();
15 changes: 14 additions & 1 deletion src/gui/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub struct SystemTray {
pub layer_notice: nwg::Notice,
pub cfg_notice: nwg::Notice,
pub err_notice: nwg::Notice,
pub exit_notice: nwg::Notice,
pub tt_notice: nwg::Notice,
/// Receiver of error message content sent from other threads
/// (e.g., from key event thread via WinDbgLogger that will also notify our GUI
Expand Down Expand Up @@ -130,7 +131,7 @@ const ASSET_FD: [&str; 4] = ["", "icon", "img", "icons"];
const IMG_EXT: [&str; 7] = ["ico", "jpg", "jpeg", "png", "bmp", "dds", "tiff"];
const PRE_LAYER: &str = "\n🗍: "; // : invalid path marker, so should be safe to use as a separator
const TTTIMER_L: u16 = 9; // lifetime delta to duration for a tooltip timer
use crate::gui::{CFG, GUI_CFG_TX, GUI_ERR_MSG_TX, GUI_ERR_TX, GUI_TX};
use crate::gui::{CFG, GUI_CFG_TX, GUI_ERR_MSG_TX, GUI_ERR_TX, GUI_EXIT_TX, GUI_TX};

pub fn send_gui_notice() {
if let Some(gui_tx) = GUI_TX.get() {
Expand All @@ -153,6 +154,13 @@ pub fn send_gui_err_notice() {
error!("no GUI_ERR_TX to notify GUI thread of errors");
}
}
pub fn send_gui_exit_notice() {
if let Some(gui_tx) = GUI_EXIT_TX.get() {
gui_tx.notice();
} else {
error!("no GUI_EXIT_TX to ask GUI thread to exit");
}
}
pub fn show_err_msg_nofail(title: String, msg: String) {
// log gets insalized before gui, so some errors might have no target to log to, ignore them
if let Some(gui_msg_tx) = GUI_ERR_MSG_TX.get() {
Expand Down Expand Up @@ -1318,6 +1326,9 @@ pub mod system_tray_ui {
nwg::Notice::builder()
.parent(&d.window)
.build(&mut d.err_notice)?;
nwg::Notice::builder()
.parent(&d.window)
.build(&mut d.exit_notice)?;
nwg::Menu::builder()
.parent(&d.tray_menu)
.text("&F Load config") //
Expand Down Expand Up @@ -1519,6 +1530,8 @@ pub mod system_tray_ui {
SystemTray::reload_cfg_icon(&evt_ui);
} else if handle == evt_ui.err_notice {
SystemTray::notify_error(&evt_ui);
} else if handle == evt_ui.exit_notice {
SystemTray::exit(&evt_ui);
} else if handle == evt_ui.tt_notice {
SystemTray::update_tooltip_pos(&evt_ui);}
E::OnWindowClose =>
Expand Down
6 changes: 6 additions & 0 deletions src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,13 @@ fn check_for_exit(_event: &KeyEvent) {
log::info!("{EXIT_MSG}");
#[cfg(all(target_os = "windows", feature = "gui"))]
{
#[cfg(not(feature = "interception_driver"))]
native_windows_gui::stop_thread_dispatch();
#[cfg(feature = "interception_driver")]
send_gui_exit_notice(); // interception driver is running in another thread to allow
// GUI take the main one, so it's calling check_for_exit
// from a thread that has no access to the main one, so
// can't stop main thread's dispatch
}
#[cfg(all(
not(target_os = "linux"),
Expand Down
21 changes: 18 additions & 3 deletions src/kanata/windows/interception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::oskbd::KeyValue;
use kanata_parser::keys::OsCode;

impl Kanata {
pub fn event_loop(kanata: Arc<Mutex<Self>>, tx: Sender<KeyEvent>) -> Result<()> {
pub fn event_loop_inner(kanata: Arc<Mutex<Self>>, tx: Sender<KeyEvent>) -> Result<()> {
let intrcptn = ic::Interception::new().ok_or_else(|| anyhow!("interception driver should init: have you completed the interception driver installation?"))?;
intrcptn.set_filter(ic::is_keyboard, ic::Filter::KeyFilter(ic::KeyFilter::all()));
let mut strokes = [ic::Stroke::Keyboard {
Expand All @@ -29,7 +29,6 @@ impl Kanata {
);
}
let mut is_dev_interceptable: HashMap<ic::Device, bool> = HashMap::default();

loop {
let dev = intrcptn.wait();
if dev > 0 {
Expand Down Expand Up @@ -110,6 +109,23 @@ impl Kanata {
}
}
}
pub fn event_loop(
kanata: Arc<Mutex<Self>>,
tx: Sender<KeyEvent>,
#[cfg(feature = "gui")] ui: crate::gui::system_tray_ui::SystemTrayUi,
) -> Result<()> {
#[cfg(not(feature = "gui"))]
{
Self::event_loop_inner(kanata, tx)
}
#[cfg(feature = "gui")]
{
std::thread::spawn(move || -> Result<()> { Self::event_loop_inner(kanata, tx) });
let _ui = ui; // prevents thread from panicking on exiting via a GUI
native_windows_gui::dispatch_thread_events();
Ok(())
}
}
}

fn is_device_interceptable(
Expand All @@ -134,7 +150,6 @@ fn is_device_interceptable(
},
}
}

fn mouse_state_to_event(
input_dev: ic::Device,
allowed_hwids: &Option<Vec<[u8; HWID_ARR_SZ]>>,
Expand Down
4 changes: 4 additions & 0 deletions src/main_lib/win_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ fn main_impl() -> Result<()> {
let gui_tx = ui.layer_notice.sender();
let gui_cfg_tx = ui.cfg_notice.sender(); // allows notifying GUI on config reloads
let gui_err_tx = ui.err_notice.sender(); // allows notifying GUI on erorrs (from logger)
let gui_exit_tx = ui.exit_notice.sender(); // allows notifying GUI on app quit
if GUI_TX.set(gui_tx).is_err() {
warn!("Someone else set our ‘GUI_TX’");
};
Expand All @@ -161,6 +162,9 @@ fn main_impl() -> Result<()> {
if GUI_ERR_TX.set(gui_err_tx).is_err() {
warn!("Someone else set our ‘GUI_ERR_TX’");
};
if GUI_EXIT_TX.set(gui_exit_tx).is_err() {
warn!("Someone else set our ‘GUI_EXIT_TX’");
};
Kanata::start_processing_loop(kanata_arc.clone(), rx, ntx, args.nodelay);

if let (Some(server), Some(nrx)) = (server, nrx) {
Expand Down
Loading