Skip to content

Commit

Permalink
Merge pull request #126 from haimgel/linux-wakeup
Browse files Browse the repository at this point in the history
Display wakeup on Linux
  • Loading branch information
haimgel authored Oct 22, 2023
2 parents 77b63a9 + 77fe3fa commit 2596c6d
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [push, pull_request]

env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: full

jobs:
build:
Expand Down
102 changes: 100 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ddc-macos = "^0.2"
[target.'cfg(target_os = "linux")'.dependencies]
ddc-i2c = "^0.2"
nvapi = "^0.1"
uinput = "^0.1"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "^0.3", features = ["winuser", "libloaderapi"] }
Expand Down
30 changes: 29 additions & 1 deletion src/platform/wake_displays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ pub fn wake_displays() -> Result<()> {

#[cfg(target_os = "linux")]
pub fn wake_displays() -> Result<()> {
use anyhow::Context;
use std::{thread, time};
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;

let mut device = uinput::default()?
.name("display-switch")?
// It's necessary to enable any mouse button. Otherwise Relative events would not work.
.event(Controller(Mouse(Left)))?
.event(Relative(Position(X)))?
.create()?;


// 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.send(X, -1)?;
device.synchronize()?;
thread::sleep(time::Duration::from_millis(50));
device.send(X, 1)?;
device.synchronize()?;
Ok(())
}

Expand All @@ -49,6 +74,9 @@ mod tests {

#[test]
fn test_wake_displays() {
assert!(wake_displays().is_ok());
let waked = wake_displays();
if let Err(err) = &waked {
assert!(err.to_string() == "Permission denied", "Couldn't wake displays: {:?}", err);
}
}
}

0 comments on commit 2596c6d

Please sign in to comment.