2023-03-28 02:11:22 +01:00
|
|
|
use std::sync::mpsc::Sender;
|
2022-08-10 01:56:22 +01:00
|
|
|
use std::thread::{self, JoinHandle};
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
2022-11-10 03:09:05 +00:00
|
|
|
use crate::api::handler::ApiMessage;
|
|
|
|
//use crate::utility::unwrap_maybe_fatal;
|
2022-08-10 01:56:22 +01:00
|
|
|
|
2022-09-13 22:09:51 +01:00
|
|
|
const ALLOWED_ERROR: f64 = 100.0; // period of 10ms with 100x means sleep has to be >= 1s to be detected
|
2022-08-10 01:56:22 +01:00
|
|
|
|
2022-11-10 03:09:05 +00:00
|
|
|
pub fn spawn(sender: Sender<ApiMessage>) -> JoinHandle<()> {
|
2022-08-10 01:56:22 +01:00
|
|
|
thread::spawn(move || {
|
2022-09-01 01:18:15 +01:00
|
|
|
log::info!("resume_worker starting...");
|
2022-09-13 22:09:51 +01:00
|
|
|
let duration = Duration::from_millis(10); // very low so it detects before Steam client does
|
2023-03-28 02:11:22 +01:00
|
|
|
// this allows PowerTools to set some values at wakeup and Steam to override them before user notices
|
2022-08-10 01:56:22 +01:00
|
|
|
let mut start = Instant::now();
|
|
|
|
loop {
|
|
|
|
let old_start = start.elapsed();
|
|
|
|
start = Instant::now();
|
|
|
|
if old_start.as_secs_f64() > duration.as_secs_f64() * (1.0 + ALLOWED_ERROR) {
|
|
|
|
// has just resumed from sleep
|
2022-09-11 16:37:26 +01:00
|
|
|
log::info!("Resume detected");
|
2023-03-28 02:11:22 +01:00
|
|
|
sender
|
|
|
|
.send(ApiMessage::OnResume)
|
|
|
|
.expect("resume_worker send failed");
|
2022-09-01 01:18:15 +01:00
|
|
|
log::debug!(
|
2022-08-10 01:56:22 +01:00
|
|
|
"OnResume completed after sleeping for {}s",
|
|
|
|
old_start.as_secs_f32()
|
|
|
|
);
|
2022-09-11 16:37:26 +01:00
|
|
|
} else {
|
|
|
|
log::debug!("OnResume got sleep period of {}s", old_start.as_secs_f32());
|
2022-08-10 01:56:22 +01:00
|
|
|
}
|
2022-09-11 16:37:26 +01:00
|
|
|
thread::sleep(duration);
|
2022-08-10 01:56:22 +01:00
|
|
|
}
|
2022-09-01 01:18:15 +01:00
|
|
|
//log::warn!("resume_worker completed!");
|
2022-08-10 01:56:22 +01:00
|
|
|
})
|
|
|
|
}
|