Skip to content

Commit

Permalink
Add windows InputDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
XX committed Feb 19, 2019
1 parent b2d074e commit 8ef094f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ fn main() {

let num_bytes = log_file.write(text.as_bytes())
.expect("Can't write to log file");
log_file.flush()
.expect("Can't flush to log file");
if num_bytes != text.len() {
panic!("Error while writing to log file");
}

input.sleep();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Key;

impl Key {
// Unknown key string
pub const UK: &'static str = "<UK>";
pub const UNKNOWN: &'static str = "<UK>";

pub const ESC: &'static str = "<ESC>";
pub const BACKSPACE: &'static str = "<Backspace>";
Expand Down
2 changes: 2 additions & 0 deletions src/system/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ impl InputDevice {
self.buf.replace(buf);
result
}

pub fn sleep(&self) {}
}

fn convert_time(secs: isize, micros: isize) -> DateTime<Local> {
Expand Down
18 changes: 9 additions & 9 deletions src/system/unix/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct InputEvent {
}

const KEY_NAMES: [&'static str; MAX_KEYS as usize] = [
Key::UK, Key::ESC,
Key::UNKNOWN, Key::ESC,
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=",
Key::BACKSPACE, Key::TAB,
"q", "w", "e", "r", "t", "y", "u", "i", "o", "p",
Expand All @@ -42,16 +42,16 @@ const KEY_NAMES: [&'static str; MAX_KEYS as usize] = [
Key::KP_ADD,
Key::KP1, Key::KP2, Key::KP3, Key::KP0,
Key::KP_POINT,
Key::UK, Key::UK, Key::UK,
Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN,
Key::F11, Key::F12,
Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, Key::UK,
Key::KP_ENTER, Key::RCTRL, Key::KP_DIV, Key::SYS_RQ, Key::RALT, Key::UK,
Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN,
Key::KP_ENTER, Key::RCTRL, Key::KP_DIV, Key::SYS_RQ, Key::RALT, Key::UNKNOWN,
Key::HOME, Key::UP, Key::PAGE_UP, Key::LEFT, Key::RIGHT, Key::END, Key::DOWN,
Key::PAGE_DOWN, Key::INSERT, Key::DELETE
];

const SHIFT_KEY_NAMES: [&'static str; MAX_KEYS as usize] = [
Key::UK, Key::ESC,
Key::UNKNOWN, Key::ESC,
"!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+",
Key::BACKSPACE, Key::TAB,
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
Expand All @@ -70,10 +70,10 @@ const SHIFT_KEY_NAMES: [&'static str; MAX_KEYS as usize] = [
Key::KP_ADD,
Key::KP1, Key::KP2, Key::KP3, Key::KP0,
Key::KP_POINT,
Key::UK, Key::UK, Key::UK,
Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN,
Key::F11, Key::F12,
Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, Key::UK,
Key::KP_ENTER, Key::RCTRL, Key::KP_DIV, Key::SYS_RQ, Key::RALT, Key::UK,
Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN, Key::UNKNOWN,
Key::KP_ENTER, Key::RCTRL, Key::KP_DIV, Key::SYS_RQ, Key::RALT, Key::UNKNOWN,
Key::HOME, Key::UP, Key::PAGE_UP, Key::LEFT, Key::RIGHT, Key::END, Key::DOWN,
Key::PAGE_DOWN, Key::INSERT, Key::DELETE
];
Expand All @@ -91,7 +91,7 @@ pub fn get_key_text(code: u16, shift_pressed: u8) -> &'static str {
return arr[code as usize];
} else {
debug!("Unknown key: {}", code);
return Key::UK;
return Key::UNKNOWN;
}
}

Expand Down
87 changes: 87 additions & 0 deletions src/system/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::ops::Range;
use std::collections::VecDeque;

use chrono::{NaiveDateTime, DateTime, Local, Utc};
use log::debug;
use user32::{self, winuser};
use kernel32;

use crate::{Config, system::{PressEvent, Key}};

pub fn init() {}

type VkCode = usize;

#[derive(Debug, Copy, Clone)]
struct Vk(VkCode);

impl Vk {
const MAX_KEY_CODE: VkCode = 190;

fn range() -> Range<VkCode> {
8..Self::MAX_KEY_CODE
}

fn key(&self) -> &str {
match self.0 as i32 {
winuser::VK_ESCAPE => Key::ESC,
winuser::VK_BACK => Key::BACKSPACE,
winuser::VK_TAB => Key::TAB,
winuser::VK_RETURN => Key::ENTER,
winuser::VK_LCONTROL => Key::LCTRL,
winuser::VK_RCONTROL => Key::RCTRL,
winuser::VK_LSHIFT => Key::LSHIFT,
winuser::VK_RSHIFT => Key::RSHIFT,
winuser::VK_LMENU => Key::LALT,
winuser::VK_RMENU => Key::RALT,
winuser::VK_SPACE => Key::SPACE,
_ => Key::UNKNOWN,
}
}

fn is_pressed(&self) -> bool {
unsafe { user32::GetAsyncKeyState(self.0 as i32) } == -32767
}
}

pub struct InputDevice {
pressed_keys: [bool; Vk::MAX_KEY_CODE],
events: VecDeque<(PressEvent, Vk, DateTime<Local>)>,
}

impl InputDevice {
pub fn new(config: &Config) -> Self {
Self {
pressed_keys: [false; Vk::MAX_KEY_CODE],
events: VecDeque::new(),
}
}

pub fn check_key_event(&mut self) -> Option<(PressEvent, &str, DateTime<Local>)> {
for i in Vk::range() {
let vk = Vk(i);
if vk.is_pressed() {
if !self.pressed_keys[i] {
self.pressed_keys[i] = true;
self.events.push_back((PressEvent::Press, vk, Local::now()));
}
} else {
if self.pressed_keys[i] {
self.pressed_keys[i] = false;
self.events.push_back((PressEvent::Release, vk, Local::now()));
}
}
}
if let Some((event, vk, datetime)) = self.events.pop_front() {
Some((event, vk.key(), datetime))
} else {
None
}
}

pub fn sleep(&self) {
if self.events.is_empty() {
unsafe { kernel32::SleepEx(1, 1); }
}
}
}

0 comments on commit 8ef094f

Please sign in to comment.