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
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,26 @@
</defaults>
</action>

<action id="org.shadowblip.Input.Source.LEDDevice.GetEffects">
<description>Get the effects supported by the LED device</description>
<message>Authentication is required to get LED device effects</message>
<defaults>
<allow_any>yes</allow_any>
<allow_inactive>yes</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
</action>

<action id="org.shadowblip.Input.Source.LEDDevice.SetState">
<description>Set the LED device effect, color, and brightness</description>
<message>Authentication is required to set the LED device state</message>
<defaults>
<allow_any>no</allow_any>
<allow_inactive>auth_admin</allow_inactive>
<allow_active>auth_admin</allow_active>
</defaults>
</action>

<action id="org.shadowblip.Input.Source.TTYDevice.Id">
<description>Get the TTY device ID</description>
<message>Authentication is required to get TTY device ID</message>
Expand Down
43 changes: 43 additions & 0 deletions src/dbus/interface/source/led.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use crate::dbus::{interface::Unregisterable, polkit::check_polkit};
use crate::input::source::led::multicolor::{LedMcError, LedMultiColor};
use crate::udev::device::UdevDevice;
use zbus::Connection;
use zbus::{fdo, message::Header};
use zbus_macros::interface;

impl From<LedMcError> for fdo::Error {
fn from(value: LedMcError) -> Self {
fdo::Error::Failed(value.to_string())
}
}

/// DBusInterface exposing information about a led device
pub struct SourceLedInterface {
device: UdevDevice,
Expand All @@ -27,6 +34,42 @@ impl SourceLedInterface {
check_polkit(conn, hdr, "org.shadowblip.Input.Source.LEDDevice.Id").await?;
Ok(self.device.sysname())
}

/// Returns the effects the LED supports (e.g. monocolor, breathe, rainbow)
async fn get_effects(
&self,
#[zbus(connection)] conn: &Connection,
#[zbus(header)] hdr: Header<'_>,
) -> fdo::Result<Vec<String>> {
check_polkit(
conn,
Some(hdr),
"org.shadowblip.Input.Source.LEDDevice.GetEffects",
)
.await?;
let led = LedMultiColor::new(self.device.clone())?;
Ok(led.effects()?)
}

/// Sets the LED effect, RGB color, and brightness (0-255)
async fn set_state(
&self,
effect: String,
color: (u8, u8, u8),
brightness: u8,
#[zbus(connection)] conn: &Connection,
#[zbus(header)] hdr: Header<'_>,
) -> fdo::Result<()> {
check_polkit(
conn,
Some(hdr),
"org.shadowblip.Input.Source.LEDDevice.SetState",
)
.await?;
let led = LedMultiColor::new(self.device.clone())?;
led.set_state(&effect, color, brightness)?;
Ok(())
}
}

impl Unregisterable for SourceLedInterface {}
82 changes: 59 additions & 23 deletions src/input/source/led/multicolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum LedMcError {
#[error("Error writing value: {0}")]
Read(std::io::Error),
#[error("Error accessing LED: {0}")]
Io(#[from] std::io::Error),

#[error("Error writing value: {0}")]
Write(std::io::Error),
#[error("Invalid brightness value: {0}")]
Parse(#[from] std::num::ParseIntError),

#[error("Path not found: {0}")]
Path(PathBuf),

#[error("Unsupported index type: {0}")]
UnsupportedColor(String),

#[error("Unsupported effect: {0}")]
UnsupportedEffect(String),
}

#[derive(Eq, Hash, PartialEq, Clone, Copy, Debug)]
Expand Down Expand Up @@ -91,26 +94,26 @@ pub struct LedMultiColor {
impl LedMultiColor {
/// Create a new LedMultiColor source device with the given udev
/// device information
pub fn new(device_info: UdevDevice) -> Result<Self, Box<dyn Error + Send + Sync>> {
pub fn new(device_info: UdevDevice) -> Result<Self, LedMcError> {
let brightness_path = PathBuf::from(device_info.syspath().as_str()).join("brightness");
if !brightness_path.exists() {
return Err(Box::new(LedMcError::Path(brightness_path)));
return Err(LedMcError::Path(brightness_path));
}

let current_brightness: u8 = read_brightness(brightness_path.as_path())?;

let max_brightness_path =
PathBuf::from(device_info.syspath().as_str()).join("max_brightness");
if !max_brightness_path.exists() {
return Err(Box::new(LedMcError::Path(brightness_path)));
return Err(LedMcError::Path(brightness_path));
}

let max_brightness: u8 = read_brightness(max_brightness_path.as_path())?;

let multi_intensity_path =
PathBuf::from(device_info.syspath().as_str()).join("multi_intensity");
if !multi_intensity_path.exists() {
return Err(Box::new(LedMcError::Path(multi_intensity_path)));
return Err(LedMcError::Path(multi_intensity_path));
}

let multi_index_path = PathBuf::from(device_info.syspath().as_str()).join("multi_index");
Expand Down Expand Up @@ -140,9 +143,8 @@ impl LedMultiColor {

/// Writes the given red, green, and blue (only) values to the device. Any other color is set
/// to 0. Used for RGB only implementations we can't adjust (i.e. DualSense)
fn write_color(&self, r: u8, g: u8, b: u8) -> Result<(), Box<dyn Error + Send + Sync>> {
let contents = read_to_string(self.multi_intensity_path.as_path())
.map_err(|err| Box::new(LedMcError::Read(err)))?;
fn write_color(&self, r: u8, g: u8, b: u8) -> Result<(), LedMcError> {
let contents = read_to_string(self.multi_intensity_path.as_path())?;
let contents = (self
.multi_index_map
.iter()
Expand All @@ -158,21 +160,49 @@ impl LedMultiColor {
})
.collect::<Vec<String>>())
.join(" ");
Ok(fs::write(self.multi_intensity_path.as_path(), contents)
.map_err(|err| Box::new(LedMcError::Write(err)))?)
fs::write(self.multi_intensity_path.as_path(), contents)?;
Ok(())
}

/// Writes the given brightness valeu to the brightness path, but scales it to the max
/// brightness value. This assumes userspace will be sending a full u8
fn write_brightness(&self, brightness: u8) -> Result<(), Box<dyn Error + Send + Sync>> {
fn write_brightness(&self, brightness: u8) -> Result<(), LedMcError> {
// Scale brightness to max_brightness, cast avoids overflow
let brightness: u8 = (brightness as u32 * self.max_brightness as u32 / 255) as u8;

Ok(fs::write(
fs::write(
self.brightness_path.as_path(),
brightness.to_string().as_str(),
)
.map_err(|err| Box::new(LedMcError::Write(err)))?)
)?;
Ok(())
}

/// Returns the effects the LED supports (e.g. monocolor, breathe, rainbow).
/// LEDs without effect support return an empty list.
pub fn effects(&self) -> Result<Vec<String>, LedMcError> {
let path = PathBuf::from(self.device_info.syspath().as_str()).join("effect_index");
if !path.exists() {
return Ok(Vec::new());
}
let effects = read_to_string(path)?;
Ok(effects.split_whitespace().map(String::from).collect())
}

/// Sets the LED effect, color, and brightness. Brightness is scaled to the
/// maximum brightness of the device.
pub fn set_state(
&self,
effect: &str,
(red, green, blue): (u8, u8, u8),
brightness: u8,
) -> Result<(), LedMcError> {
if !self.effects()?.iter().any(|e| e == effect) {
return Err(LedMcError::UnsupportedEffect(effect.to_string()));
}
let path = PathBuf::from(self.device_info.syspath().as_str()).join("effect");
fs::write(path, effect)?;
self.write_color(red, green, blue)?;
self.write_brightness(brightness)
}

// Retain below functions for dbus
Expand All @@ -188,7 +218,7 @@ impl LedMultiColor {

/// Reads the brightness path and returns a u8 with the current brightness setting
#[allow(dead_code)]
fn read_brightness(&self) -> Result<u8, Box<dyn Error + Send + Sync>> {
fn read_brightness(&self) -> Result<u8, LedMcError> {
read_brightness(self.brightness_path.as_path())
}

Expand Down Expand Up @@ -216,8 +246,8 @@ impl LedMultiColor {

let contents = raw_values.join(" ");

Ok(fs::write(self.multi_intensity_path.as_path(), contents)
.map_err(|err| Box::new(LedMcError::Write(err)))?)
fs::write(self.multi_intensity_path.as_path(), contents)?;
Ok(())
}

/// Converts a generic dbus provided Vec<u8> to a matching Vec<u32> based on the device's
Expand Down Expand Up @@ -252,6 +282,12 @@ impl LedMultiColor {
}
}

impl From<LedMcError> for OutputError {
fn from(value: LedMcError) -> Self {
OutputError::DeviceError(value.to_string())
}
}

impl Debug for LedMultiColor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MultiColorChassis").finish()
Expand Down Expand Up @@ -292,7 +328,7 @@ impl SourceOutputDevice for LedMultiColor {
fn read_color(path: &Path, index: &[ColorType]) -> Result<Vec<HashMap<ColorType, u8>>, LedMcError> {
let mut current_color: Vec<HashMap<ColorType, u8>> = Vec::new();

let color_contents = read_to_string(path).map_err(LedMcError::Read)?;
let color_contents = read_to_string(path)?;
let color_vals = color_contents
.split_whitespace()
.map(|val| val.to_string().parse().unwrap_or_default())
Expand Down Expand Up @@ -321,7 +357,7 @@ fn read_color(path: &Path, index: &[ColorType]) -> Result<Vec<HashMap<ColorType,
}

/// Reads the brightness path and returns a u8 with the current brightness setting
fn read_brightness(path: &Path) -> Result<u8, Box<dyn Error + Send + Sync>> {
fn read_brightness(path: &Path) -> Result<u8, LedMcError> {
let mut brightstring = read_to_string(path)?;
brightstring = brightstring.trim().to_string();
log::debug!("Got brightness value: {brightstring}");
Expand All @@ -330,7 +366,7 @@ fn read_brightness(path: &Path) -> Result<u8, Box<dyn Error + Send + Sync>> {

/// Reads the multi_index path and returns a Vec<ColorType> of the configured device.
fn read_color_index(path: &Path) -> Result<Vec<ColorType>, LedMcError> {
let mi_contents = read_to_string(path).map_err(LedMcError::Read)?;
let mi_contents = read_to_string(path)?;
let multi_index_strings = mi_contents
.split_whitespace()
.map(String::from)
Expand Down