I am VERY new to rust, and really programming in general. I am writing a rust program that will detect idle and print out the time since the last input. I am using the winapi crate. The code below returns between 0ns and 31ms, regardless of how long it's been since I pressed a key or moved the mouse. It never gets higher than 31ms.
use winapi::um::{
winuser::{
LASTINPUTINFO,
PLASTINPUTINFO,
GetLastInputInfo
},
};
fn sleep(milliseconds: u64){
let mills = std::time::Duration::from_millis(milliseconds);
std::thread::sleep(mills);
}
fn main() {
loop {
let now = unsafe { winapi::um::sysinfoapi::GetTickCount() };
let mut last_input_info = LASTINPUTINFO {
cbSize: std::mem::size_of::<LASTINPUTINFO>() as u32,
dwTime: 0
};
let p_last_input_info: PLASTINPUTINFO = &mut last_input_info as *mut LASTINPUTINFO;
let ok = unsafe { GetLastInputInfo(p_last_input_info) } != 0;
let logvar = match ok {
true => {
let millis = now - last_input_info.dwTime;
Ok(std::time::Duration::from_millis(millis as u64))
},
false => Err(format!("GetLastInputInfo failed"))
}.unwrap();
println!("{:?}", logvar);
sleep(1000);
};
}
I considered that it might be some program that is keeping the PC from going idle, so using powercfg -requests, I found some audio streams open (still don't know how to fix that). I just don't know if that could be what's happening here. Community expertise requested!
CodePudding user response:
It might be good for inspecting problem to execute cpp code below. It works well on my PC.
- If it work well --> Your Rust code or Rust winapi problem
- If it does not work well --> Your PC problem
#include <windows.h>
int main() {
LASTINPUTINFO lii;
lii.cbSize = sizeof(LASTINPUTINFO);
for (;;) {
int ret = GetLastInputInfo(&lii);
printf("ret=%d diff=%d\n", ret, GetTickCount() - lii.dwTime);
Sleep(1000);
}
}
output in my PC with mouse move and stop
ret=1 diff=578
ret=1 diff=1578
ret=1 diff=2594
ret=1 diff=0
ret=1 diff=422
ret=1 diff=1422
ret=1 diff=2422
ret=1 diff=3438
ret=1 diff=4438
ret=1 diff=5438
ret=1 diff=79
ret=1 diff=16
CodePudding user response:
I have found the culprit. It was not the rust code at all, but my Game Controller Driver that was registering as input. For some reason, whenever I have a game controller connected, it does that. It took me forever to figure it out, and even so happen to just stumble onto the answer after weeks of googling. Consider this question closed.