Skip to content

Instantly share code, notes, and snippets.

@TheOnlyArtz
Last active July 20, 2021 18:28
Show Gist options
  • Save TheOnlyArtz/32155254bd69235f08f510c5434b6855 to your computer and use it in GitHub Desktop.
Save TheOnlyArtz/32155254bd69235f08f510c5434b6855 to your computer and use it in GitHub Desktop.
A small Rust program which is meant to run via the Task Scheduler in order to unidle your computer, the origin of it was to keep the Discord's Spotify RPC server alive so the `Listening to Spotify` will be available when I'm listening from whatever device while not being home.
[package]
name = "spotify_unidler"
version = "0.1.0"
authors = ["TheOnlyArtz <callcraft456@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
winapi = {version = "*", features = ["winuser"]}
tokio = {version = "*", features = ["time"]}
rand = "*"
chrono = "*"
#![windows_subsystem = "windows"]
use winapi::um::winuser::{
LASTINPUTINFO,
PLASTINPUTINFO,
GetLastInputInfo,
SendInput,
INPUT,
MOUSEINPUT,
INPUT_MOUSE,
INPUT_u,
MOUSEEVENTF_MOVE,
MOUSEEVENTF_ABSOLUTE
};
use tokio::time::{Duration};
use chrono::{self, Timelike};
use rand::Rng;
use std::{thread};
// The whole point of this program
// is literally move the mouse if we are inactive for 10 minutes
// by using SendInput we actually reset the GetLastInputInfo's result
// which is the function probably most programs detect idling with.
fn main() {
let mut last_time_moved = 0;
// To be honest there isn't really a reason
// for it to be that short of a duration
// up to you.
let one_second = Duration::from_millis(1000);
loop {
thread::sleep(one_second);
// Check whether I'm sleeping or not, if I do, do not send inputs !
// Since Israel's timezone right now is GMT+3 and GMT is basically UTC
// I'm adding 3 hours to the calculation
// I'm sleeping between 00:00 to 6:00 (24H format)
let current_time = (chrono::Utc::now().time().hour() + 3) % 24;
if current_time <= 6 {
continue;
}
// Initialize a LASTINPUTINFO struct with the correct fields
let mut last_input_info: LASTINPUTINFO = LASTINPUTINFO {
cbSize: std::mem::size_of::<LASTINPUTINFO>() as u32,
dwTime: 0
};
// Create a new PLASTINPUTINFO and assign it to a mutable version of last_input_info
let plast_input_info: PLASTINPUTINFO = &mut last_input_info;
unsafe {GetLastInputInfo(plast_input_info);}
// If first_time is 0 it means it's uninitialized so we need to assign a value to it
// otherwise continue
if last_time_moved == 0 {
last_time_moved = chrono::Utc::now().timestamp_millis() - last_input_info.dwTime as i64;
continue;
}
// Check the time of the different input given to the computer
// 10 Minutes
if (chrono::Utc::now().timestamp_millis() - last_input_info.dwTime as i64) - last_time_moved <= 600000 {
continue;
}
// Generate random X, Y values to move the mouse to
let num_x = rand::thread_rng().gen_range(0..1500);
let num_y = rand::thread_rng().gen_range(0..3000);
let mut input_u: INPUT_u = unsafe { std::mem::zeroed() };
unsafe {
*input_u.mi_mut() = MOUSEINPUT {
dx: num_x,
dy: num_y,
dwFlags: MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
mouseData: 0,
time: 0,
dwExtraInfo: 0
};
let mut input = INPUT {
type_: INPUT_MOUSE,
u: input_u
};
SendInput(1, &mut input, std::mem::size_of::<INPUT>() as i32);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment