mirror of
https://gitlab.com/asus-linux/asusctl.git
synced 2026-02-06 00:15:04 +01:00
Code cleanup
This commit is contained in:
25
src/core.rs
25
src/core.rs
@@ -12,7 +12,7 @@ use rusb::DeviceHandle;
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::marker::PhantomData;
|
use std::marker::{PhantomData, PhantomPinned};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
@@ -45,6 +45,7 @@ static FAN_TYPE_2_PATH: &str = "/sys/devices/platform/asus-nb-wmi/fan_boost_mode
|
|||||||
pub(crate) struct RogCore {
|
pub(crate) struct RogCore {
|
||||||
handle: DeviceHandle<rusb::GlobalContext>,
|
handle: DeviceHandle<rusb::GlobalContext>,
|
||||||
virt_keys: VirtKeys,
|
virt_keys: VirtKeys,
|
||||||
|
_pin: PhantomPinned,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RogCore {
|
impl RogCore {
|
||||||
@@ -79,6 +80,7 @@ impl RogCore {
|
|||||||
Ok(RogCore {
|
Ok(RogCore {
|
||||||
handle: dev_handle,
|
handle: dev_handle,
|
||||||
virt_keys: VirtKeys::new(),
|
virt_keys: VirtKeys::new(),
|
||||||
|
_pin: PhantomPinned,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,13 +183,6 @@ impl RogCore {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_raw_device_handle(&mut self) -> NonNull<DeviceHandle<rusb::GlobalContext>> {
|
|
||||||
// Breaking every damn lifetime guarantee rust gives us
|
|
||||||
unsafe {
|
|
||||||
NonNull::new_unchecked(&mut self.handle as *mut DeviceHandle<rusb::GlobalContext>)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A direct call to systemd to suspend the PC.
|
/// A direct call to systemd to suspend the PC.
|
||||||
///
|
///
|
||||||
/// This avoids desktop environments being required to handle it
|
/// This avoids desktop environments being required to handle it
|
||||||
@@ -237,13 +232,16 @@ impl RogCore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_raw_device_handle(&mut self) -> NonNull<DeviceHandle<rusb::GlobalContext>> {
|
||||||
|
// Breaking every damn lifetime guarantee rust gives us
|
||||||
|
unsafe {
|
||||||
|
NonNull::new_unchecked(&mut self.handle as *mut DeviceHandle<rusb::GlobalContext>)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// UNSAFE: Must live as long as RogCore
|
/// Lifetime is tied to `DeviceHandle` from `RogCore`
|
||||||
///
|
|
||||||
/// Because we're holding a pointer to something that *may* go out of scope while the
|
|
||||||
/// pointer is held. We're relying on access to struct to be behind a Mutex, and for behaviour
|
|
||||||
/// that may cause invalididated pointer to cause the program to panic rather than continue.
|
|
||||||
pub(crate) struct KeyboardReader<'d, C: 'd>
|
pub(crate) struct KeyboardReader<'d, C: 'd>
|
||||||
where
|
where
|
||||||
C: rusb::UsbContext,
|
C: rusb::UsbContext,
|
||||||
@@ -316,7 +314,6 @@ where
|
|||||||
|
|
||||||
/// UNSAFE
|
/// UNSAFE
|
||||||
unsafe impl<'d, C> Send for LedWriter<'d, C> where C: rusb::UsbContext {}
|
unsafe impl<'d, C> Send for LedWriter<'d, C> where C: rusb::UsbContext {}
|
||||||
unsafe impl<'d, C> Sync for LedWriter<'d, C> where C: rusb::UsbContext {}
|
|
||||||
|
|
||||||
impl<'d, C> LedWriter<'d, C>
|
impl<'d, C> LedWriter<'d, C>
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -31,22 +31,20 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
|
|||||||
let laptop = match_laptop();
|
let laptop = match_laptop();
|
||||||
let mut config = Config::default().read();
|
let mut config = Config::default().read();
|
||||||
|
|
||||||
let mut rogcore = Box::pin(
|
let mut rogcore = RogCore::new(
|
||||||
RogCore::new(
|
laptop.usb_vendor(),
|
||||||
laptop.usb_vendor(),
|
laptop.usb_product(),
|
||||||
laptop.usb_product(),
|
laptop.led_endpoint(),
|
||||||
laptop.led_endpoint(),
|
)
|
||||||
)
|
.map_or_else(
|
||||||
.map_or_else(
|
|err| {
|
||||||
|err| {
|
error!("{}", err);
|
||||||
error!("{}", err);
|
panic!("{}", err);
|
||||||
panic!("{}", err);
|
},
|
||||||
},
|
|daemon| {
|
||||||
|daemon| {
|
info!("RogCore loaded");
|
||||||
info!("RogCore loaded");
|
daemon
|
||||||
daemon
|
},
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
// Reload settings
|
// Reload settings
|
||||||
rogcore.reload(&mut config).await?;
|
rogcore.reload(&mut config).await?;
|
||||||
@@ -76,31 +74,27 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
// Keyboard reader goes in separate task because we want a high interrupt timeout
|
// Keyboard reader goes in separate task because we want a high interrupt timeout
|
||||||
// and don't want that to hold up other tasks, or miss keystrokes
|
// and don't want that to hold up other tasks, or miss keystrokes
|
||||||
{
|
let keyboard_reader = KeyboardReader::new(
|
||||||
let keyboard_reader = KeyboardReader::new(
|
rogcore.get_raw_device_handle(),
|
||||||
rogcore.get_raw_device_handle(),
|
laptop.key_endpoint(),
|
||||||
laptop.key_endpoint(),
|
laptop.key_filter().to_owned(),
|
||||||
laptop.key_filter().to_owned(),
|
);
|
||||||
);
|
|
||||||
// This is *not* safe
|
let led_writer1 = led_writer.clone();
|
||||||
let led_writer = led_writer.clone();
|
let config1 = config.clone();
|
||||||
let config = config.clone();
|
// start the keyboard reader and laptop-action loop
|
||||||
// start the keyboard reader and laptop-action loop
|
tokio::task::spawn(async move {
|
||||||
tokio::spawn(async move {
|
loop {
|
||||||
loop {
|
let data = keyboard_reader.poll_keyboard().await;
|
||||||
let data = unsafe { keyboard_reader.poll_keyboard().await };
|
if let Some(bytes) = data {
|
||||||
if let Some(bytes) = data {
|
laptop
|
||||||
match laptop.run(&mut rogcore, &led_writer, &config, bytes).await {
|
.run(&mut rogcore, &led_writer1, &config1, bytes)
|
||||||
Ok(_) => {}
|
.await
|
||||||
Err(err) => {
|
.map_err(|err| warn!("{:?}", err))
|
||||||
error!("{:?}", err);
|
.unwrap();
|
||||||
panic!("Force crash for systemd to restart service")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// start the LED writer loop
|
// start the LED writer loop
|
||||||
let mut time_mark = Instant::now();
|
let mut time_mark = Instant::now();
|
||||||
@@ -122,6 +116,7 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write a colour block
|
// Write a colour block
|
||||||
|
let led_writer = led_writer.clone();
|
||||||
if let Ok(mut lock) = effect.try_lock() {
|
if let Ok(mut lock) = effect.try_lock() {
|
||||||
// Spawn a writer
|
// Spawn a writer
|
||||||
if let Some(stuff) = lock.take() {
|
if let Some(stuff) = lock.take() {
|
||||||
|
|||||||
Reference in New Issue
Block a user