diff --git a/Cargo.lock b/Cargo.lock index be9995b0..ed4777d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,15 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2" +[[package]] +name = "aho-corasick" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada" +dependencies = [ + "memchr", +] + [[package]] name = "bit-set" version = "0.5.1" @@ -125,6 +134,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "memchr" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" + [[package]] name = "numtoa" version = "0.2.3" @@ -182,6 +197,7 @@ dependencies = [ name = "rog-lib" version = "0.4.0" dependencies = [ + "aho-corasick", "gumdrop", "rusb", "serde", diff --git a/rog-lib/Cargo.toml b/rog-lib/Cargo.toml index cf7c59b8..46fb7c98 100644 --- a/rog-lib/Cargo.toml +++ b/rog-lib/Cargo.toml @@ -10,4 +10,5 @@ gumdrop = "0.8" serde = "1.0" serde_derive = "1.0" toml = "0.5" -sysfs-class = "0.1.2" \ No newline at end of file +sysfs-class = "0.1.2" +aho-corasick = "0.7" \ No newline at end of file diff --git a/rog-lib/src/core.rs b/rog-lib/src/core.rs index f10d91da..759295e3 100644 --- a/rog-lib/src/core.rs +++ b/rog-lib/src/core.rs @@ -1,7 +1,9 @@ use crate::{aura::BuiltInModeByte, config::Config, error::AuraError, laptops::*}; +use aho_corasick::AhoCorasick; use gumdrop::Options; use rusb::{DeviceHandle, Error}; use std::cell::{Ref, RefCell}; +use std::process::Command; use std::str::FromStr; use std::time::Duration; use sysfs_class::Brightness; @@ -184,6 +186,27 @@ impl RogCore { } Ok(None) } + + pub fn suspend(&self) { + std::process::Command::new("systemctl") + .arg("suspend") + .spawn() + .expect("failed to suspend"); + } + + pub fn toggle_airplane_mode(&self) { + if let Ok(output) = Command::new("rfkill").arg("list").output() { + if output.status.success() { + let patterns = &["yes"]; + let ac = AhoCorasick::new(patterns); + if ac.earliest_find(output.stdout).is_some() { + Command::new("rfkill").arg("unblock").arg("all").spawn(); + } else { + Command::new("rfkill").arg("block").arg("all").spawn(); + } + } + } + } } pub struct Backlight { diff --git a/rog-lib/src/laptops.rs b/rog-lib/src/laptops.rs index adec8ee6..b4b9928b 100644 --- a/rog-lib/src/laptops.rs +++ b/rog-lib/src/laptops.rs @@ -115,10 +115,10 @@ impl Laptop for LaptopGX502GW { self.backlight.step_down(); } GX502GWKeys::Sleep => { - std::process::Command::new("systemctl") - .arg("suspend") - .spawn() - .expect("failed to suspend"); + rogcore.suspend(); + } + GX502GWKeys::AirplaneMode => { + rogcore.toggle_airplane_mode(); } _ => { if key_byte != 0 {