Skip to content

Commit

Permalink
shell: Parse adaptive_sync keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Drakulix committed Nov 20, 2024
1 parent 197b8ed commit c6f3d43
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions shell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub struct Output {
pub transform: Option<Transform>,
pub modes: Vec<ModeKey>,
pub current: Option<ModeKey>,
pub adaptive_sync: Option<AdaptiveSyncState>,
pub adaptive_sync_availability: Option<AdaptiveSyncAvailability>,
}

impl Output {
Expand All @@ -67,6 +69,8 @@ impl Output {
transform: None,
modes: Vec::new(),
current: None,
adaptive_sync: None,
adaptive_sync_availability: None,
}
}
}
Expand Down Expand Up @@ -116,6 +120,66 @@ impl TryFrom<&str> for Transform {
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AdaptiveSyncState {
Always,
Auto,
Disabled,
}

impl Display for AdaptiveSyncState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
AdaptiveSyncState::Always => "true",
AdaptiveSyncState::Auto => "automatic",
AdaptiveSyncState::Disabled => "false",
})
}
}

impl TryFrom<&str> for AdaptiveSyncState {
type Error = &'static str;

fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"true" => AdaptiveSyncState::Always,
"automatic" => AdaptiveSyncState::Auto,
"false" => AdaptiveSyncState::Disabled,
_ => return Err("unknown adaptive_sync state variant"),
})
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AdaptiveSyncAvailability {
Supported,
RequiresModeset,
Unsupported,
}

impl Display for AdaptiveSyncAvailability {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
AdaptiveSyncAvailability::Supported => "true",
AdaptiveSyncAvailability::RequiresModeset => "requires_modeset",
AdaptiveSyncAvailability::Unsupported => "false",
})
}
}

impl TryFrom<&str> for AdaptiveSyncAvailability {
type Error = &'static str;

fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"true" => AdaptiveSyncAvailability::Supported,
"requires_modeset" => AdaptiveSyncAvailability::RequiresModeset,
"false" => AdaptiveSyncAvailability::Unsupported,
_ => return Err("unknown adaptive_sync availability variant"),
})
}
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("`cosmic-randr` KDL format error")]
Expand Down Expand Up @@ -245,6 +309,23 @@ pub async fn list() -> Result<List, Error> {
}
}

"adaptive_sync" => {
if let Some(entry) = node.entries().first() {
if let Some(string) = entry.value().as_string() {
output.adaptive_sync = AdaptiveSyncState::try_from(string).ok();
}
}
}

"adaptive_sync_support" => {
if let Some(entry) = node.entries().first() {
if let Some(string) = entry.value().as_string() {
output.adaptive_sync_availability =
AdaptiveSyncAvailability::try_from(string).ok();
}
}
}

// Switch to parsing output modes.
"modes" => {
let Some(children) = node.children() else {
Expand Down

0 comments on commit c6f3d43

Please sign in to comment.