Skip to content

Commit

Permalink
Move mouse instead of keyboard input
Browse files Browse the repository at this point in the history
  • Loading branch information
haimgel committed Oct 22, 2023
1 parent af0d63b commit 5781322
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push, pull_request]

env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
RUST_BACKTRACE: full

jobs:
build:
Expand Down
22 changes: 14 additions & 8 deletions src/platform/wake_displays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,28 @@ pub fn wake_displays() -> Result<()> {
pub fn wake_displays() -> Result<()> {
use anyhow::Context;
use std::{thread, time};
use uinput::{Device, event::keyboard};
use uinput::event::controller::Controller::Mouse;
use uinput::event::controller::Mouse::Left;
use uinput::event::Event::{Controller, Relative};
use uinput::event::relative::Position::X;
use uinput::event::relative::Relative::Position;

fn make_kbd_device() -> Result<Device> {
Ok(uinput::default()?
let mut device = uinput::default()?
.name("display-switch")?
.event(uinput::event::Keyboard::All)?
.create()?)
}
// It's necessary to enable any mouse button. Otherwise Relative events would not work.
.event(Controller(Mouse(Left)))?
.event(Relative(Position(X)))?
.create()?;

let mut device = make_kbd_device().context("Couldn't wake displays: couldn't configure uinput")?;

// This sleep appears to be necessary based on testing.
// Possibly X does not immediately recognize the new device?
thread::sleep(time::Duration::from_secs(1));

device.click(&keyboard::Key::RightAlt)?;
device.send(X, -1)?;
device.synchronize()?;
thread::sleep(time::Duration::from_millis(50));
device.send(X, 1)?;
device.synchronize()?;
Ok(())
}
Expand Down

0 comments on commit 5781322

Please sign in to comment.