Prioritize user input
This commit is contained in:
parent
8583987a67
commit
c1b052c85d
33
src/main.rs
33
src/main.rs
@ -44,15 +44,21 @@ fn main() {
|
|||||||
let (tx2, rx2) = mpsc::sync_channel(1);
|
let (tx2, rx2) = mpsc::sync_channel(1);
|
||||||
let initial_polling_rate: u64 = 50;
|
let initial_polling_rate: u64 = 50;
|
||||||
let initial_scroll_left: bool = true;
|
let initial_scroll_left: bool = true;
|
||||||
|
let mut user_update = false;
|
||||||
// polling thread
|
// polling thread
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let mut polling_rate = initial_polling_rate;
|
let mut polling_rate = initial_polling_rate;
|
||||||
|
let mut new_polling_rate = initial_polling_rate;
|
||||||
let mut scroll_left = initial_scroll_left;
|
let mut scroll_left = initial_scroll_left;
|
||||||
|
let mut new_scroll_left = initial_scroll_left;
|
||||||
let device_state = DeviceState::new();
|
let device_state = DeviceState::new();
|
||||||
loop {
|
loop {
|
||||||
|
// Poll for input
|
||||||
|
let keys = device_state.get_keys(); // poll the keys
|
||||||
|
(user_update, new_polling_rate, new_scroll_left) = detect_keys(keys, polling_rate, scroll_left);
|
||||||
match rx2.try_recv() {
|
match rx2.try_recv() {
|
||||||
Ok(new_scroll) => {
|
Ok(recv_scroll) => {
|
||||||
scroll_left = new_scroll;
|
new_scroll_left = recv_scroll;
|
||||||
}
|
}
|
||||||
Err(mpsc::TryRecvError::Empty) => {
|
Err(mpsc::TryRecvError::Empty) => {
|
||||||
//eprintln!("No new data. ;(");
|
//eprintln!("No new data. ;(");
|
||||||
@ -62,11 +68,10 @@ fn main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Poll for input
|
scroll_left = new_scroll_left;
|
||||||
let keys = device_state.get_keys(); // poll the keys
|
polling_rate = new_polling_rate;
|
||||||
(polling_rate, scroll_left) = detect_keys(keys, polling_rate, scroll_left);
|
let _ = tx.send((user_update, polling_rate, scroll_left));
|
||||||
let _ = tx.send((polling_rate, scroll_left));
|
thread::sleep(Duration::from_millis(100)); // poll every 50 ms
|
||||||
thread::sleep(Duration::from_millis(100)); // poll every 50 ms
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// main thread
|
// main thread
|
||||||
@ -76,9 +81,10 @@ fn main() {
|
|||||||
loop {
|
loop {
|
||||||
// Get data from the polling thread
|
// Get data from the polling thread
|
||||||
match rx.try_recv() {
|
match rx.try_recv() {
|
||||||
Ok((new_rate, new_scroll)) => {
|
Ok((new_user_update, new_rate, new_scroll)) => {
|
||||||
polling_rate = new_rate;
|
polling_rate = new_rate;
|
||||||
scroll_left = new_scroll;
|
scroll_left = new_scroll;
|
||||||
|
user_update = new_user_update;
|
||||||
}
|
}
|
||||||
Err(mpsc::TryRecvError::Empty) => {
|
Err(mpsc::TryRecvError::Empty) => {
|
||||||
//eprintln!("No new data. ;(");
|
//eprintln!("No new data. ;(");
|
||||||
@ -92,7 +98,7 @@ fn main() {
|
|||||||
|
|
||||||
// Autoscroll: Autoscroll automatically toggles ths scrolling direction when the scrolling reaches a nonspace.
|
// Autoscroll: Autoscroll automatically toggles ths scrolling direction when the scrolling reaches a nonspace.
|
||||||
if args.autoscroll {
|
if args.autoscroll {
|
||||||
if !char::is_whitespace(input.chars().nth(0).unwrap()) | !char::is_whitespace(input.chars().nth(input.len() - 1).unwrap()) {
|
if !user_update && (!char::is_whitespace(input.chars().nth(0).unwrap()) | !char::is_whitespace(input.chars().nth(input.len() - 1).unwrap())) { // user update takes priority
|
||||||
scroll_left = !scroll_left;
|
scroll_left = !scroll_left;
|
||||||
// Let the other thread know the scrolling has been changed
|
// Let the other thread know the scrolling has been changed
|
||||||
match tx2.try_send(scroll_left) {
|
match tx2.try_send(scroll_left) {
|
||||||
@ -132,11 +138,12 @@ fn shift_right(mut input: String) -> String {
|
|||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn detect_keys(keys: Vec<Keycode>, mut polling_rate: u64, mut scroll_left: bool) -> (u64, bool) {
|
fn detect_keys(keys: Vec<Keycode>, mut polling_rate: u64, mut scroll_left: bool) -> (bool, u64, bool) {
|
||||||
|
let mut user_update = false;
|
||||||
//detecting keydown events
|
//detecting keydown events
|
||||||
//println!("{} {} ", polling_rate, scroll_left);
|
//println!("{} {} ", polling_rate, scroll_left);
|
||||||
if keys.is_empty() {
|
if keys.is_empty() {
|
||||||
return (polling_rate, scroll_left);
|
return (user_update, polling_rate, scroll_left);
|
||||||
}
|
}
|
||||||
if keys.contains(&Keycode::Up) {
|
if keys.contains(&Keycode::Up) {
|
||||||
polling_rate = increase_poll(polling_rate);
|
polling_rate = increase_poll(polling_rate);
|
||||||
@ -146,11 +153,13 @@ fn detect_keys(keys: Vec<Keycode>, mut polling_rate: u64, mut scroll_left: bool)
|
|||||||
}
|
}
|
||||||
if keys.contains(&Keycode::Left) {
|
if keys.contains(&Keycode::Left) {
|
||||||
scroll_left = true;
|
scroll_left = true;
|
||||||
|
user_update = true;
|
||||||
}
|
}
|
||||||
if keys.contains(&Keycode::Right) {
|
if keys.contains(&Keycode::Right) {
|
||||||
scroll_left = false;
|
scroll_left = false;
|
||||||
|
user_update = true;
|
||||||
}
|
}
|
||||||
return (polling_rate, scroll_left);
|
return (user_update, polling_rate, scroll_left);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn increase_poll(mut polling_rate: u64) -> u64 {
|
fn increase_poll(mut polling_rate: u64) -> u64 {
|
||||||
|
Loading…
Reference in New Issue
Block a user