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

feat: add flag to remove delay #410

Merged
merged 1 commit into from
May 20, 2023
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
19 changes: 11 additions & 8 deletions src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,19 +1238,22 @@ impl Kanata {
kanata: Arc<Mutex<Self>>,
rx: Receiver<KeyEvent>,
tx: Option<Sender<ServerMessage>>,
nodelay: bool,
) {
info!("entering the processing loop");
std::thread::spawn(move || {
info!("Init: catching only releases and sending immediately");
for _ in 0..500 {
if let Ok(kev) = rx.try_recv() {
if kev.value == KeyValue::Release {
let mut k = kanata.lock();
info!("Init: releasing {:?}", kev.code);
k.kbd_out.release_key(kev.code).expect("key released");
if !nodelay {
info!("Init: catching only releases and sending immediately");
for _ in 0..500 {
if let Ok(kev) = rx.try_recv() {
if kev.value == KeyValue::Release {
let mut k = kanata.lock();
info!("Init: releasing {:?}", kev.code);
k.kbd_out.release_key(kev.code).expect("key released");
}
}
std::thread::sleep(time::Duration::from_millis(1));
}
std::thread::sleep(time::Duration::from_millis(1));
}

info!("Starting kanata proper");
Expand Down
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct ValidatedArgs {
port: Option<i32>,
#[cfg(target_os = "linux")]
symlink_path: Option<String>,
nodelay: bool,
}

fn default_cfg() -> Vec<PathBuf> {
Expand Down Expand Up @@ -97,6 +98,11 @@ kanata.kbd in the current working directory and
/// Enable trace logging; implies --debug as well.
#[arg(short, long)]
trace: bool,

/// Remove the startup delay on kanata. In some cases, removing the delay may cause keyboard
/// issues on startup.
#[arg(short, long)]
nodelay: bool,
}

/// Parse CLI arguments and initialize logging.
Expand Down Expand Up @@ -144,15 +150,18 @@ fn cli_init() -> Result<ValidatedArgs> {
port: args.port,
#[cfg(target_os = "linux")]
symlink_path: args.symlink_path,
nodelay: args.nodelay,
})
}

fn main_impl() -> Result<()> {
let args = cli_init()?;
let kanata_arc = Kanata::new_arc(&args)?;

info!("Sleeping for 2s. Please release all keys and don't press additional ones.");
std::thread::sleep(std::time::Duration::from_secs(2));
if !args.nodelay {
info!("Sleeping for 2s. Please release all keys and don't press additional ones.");
std::thread::sleep(std::time::Duration::from_secs(2));
}

// Start a processing loop in another thread and run the event loop in this thread.
//
Expand All @@ -170,7 +179,7 @@ fn main_impl() -> Result<()> {
};

let (tx, rx) = std::sync::mpsc::channel();
Kanata::start_processing_loop(kanata_arc.clone(), rx, ntx);
Kanata::start_processing_loop(kanata_arc.clone(), rx, ntx, args.nodelay);

if let (Some(server), Some(nrx)) = (server, nrx) {
Kanata::start_notification_loop(nrx, server.connections);
Expand Down