Skip to content

Commit

Permalink
feat: add flag to remove delay (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo authored May 20, 2023
1 parent 3e292fe commit 5254a24
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
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

0 comments on commit 5254a24

Please sign in to comment.