Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tailor_api/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ pub struct ProfileInfo {
pub fans: Vec<String>,
pub leds: Vec<LedProfile>,
pub performance_profile: Option<String>,
#[serde(default = "default_brightness")]
pub brightness: u8,
}

fn default_brightness() -> u8 {
100
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
Expand Down
17 changes: 17 additions & 0 deletions tailor_client/src/dbus/backlight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use zbus::{dbus_proxy, fdo};

#[dbus_proxy(
interface = "com.tux.Tailor.Backlight",
default_service = "com.tux.Tailor",
default_path = "/com/tux/Tailor"
)]
trait Backlight {
/// Get the current backlight percentage.
async fn get_backlight_percentage(&self) -> fdo::Result<u8>;

/// Get the raw maximum backlight.
async fn get_max_backlight_raw(&self) -> fdo::Result<u8>;

/// Set a new backlight percentage.
async fn set_backlight_percentage(&self, value: u8) -> fdo::Result<()>;
}
2 changes: 2 additions & 0 deletions tailor_client/src/dbus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod backlight;
mod fan;
mod led;
mod performance;
mod profiles;

pub(crate) use backlight::BacklightProxy;
pub(crate) use fan::FanProxy;
pub(crate) use led::LedProxy;
pub(crate) use performance::PerformanceProxy;
Expand Down
20 changes: 20 additions & 0 deletions tailor_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct TailorConnection<'a> {
led: dbus::LedProxy<'a>,
fan: dbus::FanProxy<'a>,
performance: dbus::PerformanceProxy<'a>,
backlight: dbus::BacklightProxy<'a>,
}

impl<'a> TailorConnection<'a> {
Expand All @@ -25,12 +26,14 @@ impl<'a> TailorConnection<'a> {
let keyboard = dbus::LedProxy::new(&connection).await?;
let fan = dbus::FanProxy::new(&connection).await?;
let performance = dbus::PerformanceProxy::new(&connection).await?;
let backlight = dbus::BacklightProxy::new(&connection).await?;

Ok(Self {
profiles,
led: keyboard,
fan,
performance,
backlight,
})
}
}
Expand Down Expand Up @@ -173,3 +176,20 @@ impl<'a> TailorConnection<'a> {
Ok(self.performance.list_profiles().await?)
}
}

impl<'a> TailorConnection<'a> {
/// Get the current backlight percentage.
async fn get_backlight_percentage(&self) -> ClientResult<u8> {
Ok(self.backlight.get_backlight_percentage().await?)
}

/// Get the raw maximum backlight.
async fn get_max_backlight_raw(&self) -> ClientResult<u8> {
Ok(self.backlight.get_max_backlight_raw().await?)
}

/// Set a new backlight percentage.
async fn set_backlight_percentage(&self, value: u8) -> ClientResult<()> {
Ok(self.backlight.set_backlight_percentage(value).await?)
}
}
2 changes: 1 addition & 1 deletion tailord/default_configs/profiles/default.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"fans": ["default"],
"leds": []
}
}
80 changes: 80 additions & 0 deletions tailord/src/backlight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::ops::Deref;

use tokio::sync::mpsc;
use tuxedo_sysfs::backlight::BacklightDriver;

#[derive(Debug)]
pub struct BacklightProfile(u8);

impl Deref for BacklightProfile {
type Target = u8;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl BacklightProfile {
pub fn new(value: u8) -> Self {
Self(value)
}
}

#[allow(unused)]
#[derive(Clone)]
pub struct BacklightRuntimeHandle {
pub backlight_percentage_sender: mpsc::Sender<u8>,
pub actual_backlight_percentage: u8,
pub max_backlight_raw: u8,
}

#[allow(unused)]
pub struct BacklightRuntime {
backlight_percentage_receiver: mpsc::Receiver<u8>,
/// Device i/o interface.
controller: BacklightDriver,
}

impl BacklightRuntime {
#[tracing::instrument(skip(controller))]
pub async fn new(
mut controller: BacklightDriver,
actual_backlight_percentage: u8,
) -> (BacklightRuntimeHandle, BacklightRuntime) {
let (backlight_percentage_sender, backlight_percentage_receiver) = mpsc::channel(1);
let max_backlight_raw = controller.get_maximum_backlight_raw().await.unwrap();
controller
.set_backlight_percentage(actual_backlight_percentage)
.await
.unwrap();
(
BacklightRuntimeHandle {
backlight_percentage_sender,
actual_backlight_percentage,
max_backlight_raw,
},
BacklightRuntime {
backlight_percentage_receiver,
controller,
},
)
}

#[tracing::instrument(skip(self))]
pub async fn run(mut self) {
loop {
if let Some(value) = self.backlight_percentage_receiver.recv().await {
tracing::info!("Setting backlight to {value}%");
self.controller
.set_backlight_percentage(value)
.await
.unwrap();
} else {
tracing::warn!(
"Stopping runtime, the performance profile channel sender has probably dropped"
);
break;
}
}
}
}
36 changes: 36 additions & 0 deletions tailord/src/dbus/backlight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use zbus::{dbus_interface, fdo};

use crate::backlight::BacklightRuntimeHandle;

pub struct BacklightInterface {
pub handler: BacklightRuntimeHandle,
}

#[dbus_interface(name = "com.tux.Tailor.Backlight")]
impl BacklightInterface {
/// Temporarily override the performance profile. Please note that this will not survive a
/// restart as the performance profile is handled by the overall profile configuration.
async fn set_backlight_percentage(&mut self, value: u32) -> fdo::Result<()> {
self.handler
.backlight_percentage_sender
.send(value as u8)
.await
.map_err(|err| {
fdo::Error::IOError(format!(
"Unable to set backlight percentage to {value}%: {err}"
))
})?;
self.handler.actual_backlight_percentage = value as u8;
Ok(())
}

/// Read the current performance profile.
async fn get_backlight_percentage(&self) -> fdo::Result<u8> {
Ok(self.handler.actual_backlight_percentage)
}

/// Read the list of supported performance profiles.
async fn get_max_backlight_raw(&self) -> fdo::Result<u8> {
Ok(self.handler.max_backlight_raw)
}
}
2 changes: 2 additions & 0 deletions tailord/src/dbus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod backlight;
mod fan;
mod led;
mod performance;
mod profiles;

pub use backlight::BacklightInterface;
pub use fan::FanInterface;
pub use led::LedInterface;
pub use performance::PerformanceInterface;
Expand Down
9 changes: 9 additions & 0 deletions tailord/src/dbus/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use tailor_api::{LedDeviceInfo, ProfileInfo};
use zbus::{dbus_interface, fdo};

use crate::{
backlight::BacklightRuntimeHandle,
fancontrol::FanRuntimeHandle,
led::LedRuntimeHandle,
performance::PerformanceProfileRuntimeHandle,
Expand All @@ -13,6 +14,7 @@ pub struct ProfileInterface {
pub fan_handles: Vec<FanRuntimeHandle>,
pub led_handles: Vec<LedRuntimeHandle>,
pub performance_profile_handle: Option<PerformanceProfileRuntimeHandle>,
pub brightness_handle: BacklightRuntimeHandle,
}

#[dbus_interface(name = "com.tux.Tailor.Profiles")]
Expand Down Expand Up @@ -80,6 +82,7 @@ impl ProfileInterface {
fans,
leds,
performance_profile,
brightness,
} = Profile::load();

for (idx, fan_handle) in self.fan_handles.iter().enumerate() {
Expand Down Expand Up @@ -111,6 +114,12 @@ impl ProfileInterface {
}
}

self.brightness_handle
.backlight_percentage_sender
.send(*brightness)
.await
.map_err(|err| fdo::Error::Failed(err.to_string()))?;

Ok(())
}
}
19 changes: 18 additions & 1 deletion tailord/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod backlight;
mod dbus;
mod fancontrol;
pub mod led;
Expand All @@ -9,13 +10,15 @@ pub mod util;

use std::future::pending;

use backlight::BacklightRuntime;
use dbus::{FanInterface, PerformanceInterface, ProfileInterface};
use profiles::Profile;
use tuxedo_ioctl::hal::IoInterface;
use tuxedo_sysfs::backlight::BacklightDriver;
use zbus::ConnectionBuilder;

use crate::{
dbus::LedInterface,
dbus::{BacklightInterface, LedInterface},
fancontrol::FanRuntime,
led::{LedRuntime, LedRuntimeData},
performance::PerformanceProfileRuntime,
Expand Down Expand Up @@ -125,10 +128,15 @@ async fn start_runtime() {
None => (None, None),
};

let backlight_device = BacklightDriver::new().await.unwrap();
let (backlight_handle, backlight_runtime) =
BacklightRuntime::new(backlight_device, *profile.brightness).await;

let profile_interface = ProfileInterface {
led_handles: led_handles.clone(),
fan_handles: fan_handles.clone(),
performance_profile_handle: performance_profile_handle.clone(),
brightness_handle: backlight_handle.clone(),
};

let led_interface = LedInterface {
Expand All @@ -143,6 +151,10 @@ async fn start_runtime() {
handler: performance_profile_handle,
};

let backlight_interface = BacklightInterface {
handler: backlight_handle,
};

tracing::debug!("Connecting to DBUS as {DBUS_NAME}");
let _conn = ConnectionBuilder::system()
.unwrap()
Expand All @@ -156,6 +168,8 @@ async fn start_runtime() {
.unwrap()
.serve_at(DBUS_PATH, performance_profile_interface)
.unwrap()
.serve_at(DBUS_PATH, backlight_interface)
.unwrap()
.build()
.await
.unwrap();
Expand All @@ -178,6 +192,9 @@ async fn start_runtime() {
tokio_uring::spawn(performance_profile_runtime.run());
}

tracing::debug!("Starting backlight runtime");
tokio_uring::spawn(backlight_runtime.run());

tracing::info!("Tailord started");
tokio::select! {
_ = pending() => {
Expand Down
8 changes: 7 additions & 1 deletion tailord/src/profiles.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{collections::HashMap, path::Component};

use crate::{fancontrol::profile::FanProfile, performance::PerformanceProfile};
use crate::{
backlight::BacklightProfile, fancontrol::profile::FanProfile, performance::PerformanceProfile,
};
use tailor_api::{ColorProfile, LedDeviceInfo, LedProfile, ProfileInfo};
use zbus::fdo;

Expand Down Expand Up @@ -43,6 +45,7 @@ pub struct Profile {
pub fans: Vec<FanProfile>,
pub leds: HashMap<LedDeviceInfo, ColorProfile>,
pub performance_profile: Option<PerformanceProfile>,
pub brightness: BacklightProfile,
}

impl Profile {
Expand Down Expand Up @@ -100,10 +103,13 @@ impl Profile {
.performance_profile
.map(PerformanceProfile::new);

let brightness = BacklightProfile::new(profile_info.brightness);

Self {
fans: fan,
leds: led,
performance_profile,
brightness,
}
}

Expand Down
3 changes: 2 additions & 1 deletion tuxedo_sysfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/AaronErhardt/tuxedo-rs"

[dependencies]
futures = "0.3"
glob = "0.3"
tokio = { version = "1", features = ["time", "fs"] }
tokio-uring = "0.4"
tracing = "0.1"
Expand All @@ -17,4 +18,4 @@ tailor_api = { version = "0.2", path = "../tailor_api" }

[dev-dependencies]
sudo = "0.6"
tracing-subscriber = "0.3.16"
tracing-subscriber = "0.3.16"
Loading