Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hints parsing #1

Merged
merged 10 commits into from
Jun 30, 2024
77 changes: 58 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ pub async fn run() -> Result<()> {
let (sender, mut receiver) = unbounded_channel();
let _server = Server::init(sender).await?;

std::hint::spin_loop();

loop {
while let Ok(action) = receiver.try_recv() {
match action {
Action::Show(notification) => {
if let Some(image_data) = &notification.image_data {
if let Some(image_data) = &notification.hints.image_data {
println!("image_data ok");
// utils::save_image(image_data);
};

if let Some(image_path) = &notification.image_path {
if let Some(image_path) = &notification.hints.image_path {
dbg!(image_path);
}

Expand All @@ -40,5 +38,7 @@ pub async fn run() -> Result<()> {
}
}
}

std::hint::spin_loop();
}
}
82 changes: 82 additions & 0 deletions src/data/image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use serde::{Deserialize, Serialize};
use zbus::zvariant::{Array, Structure, Value};

#[derive(Serialize, Deserialize, Clone)]
pub struct ImageData {
// Width of image in pixels
pub width: i32,

// Height of image in pixels
pub height: i32,

// Distance in bytes between row starts
pub rowstride: i32,

// Whether the image has an alpha channel
pub has_alpha: bool,

// Must always be 8
pub bits_per_sample: i32,

// If has_alpha is TRUE, must be 4, otherwise 3
pub channels: i32,

// The image data, in RGB byte order
pub data: Vec<u8>,
}

impl ImageData {
pub fn from_hint(hint: &Value<'_>) -> Option<Self> {
Structure::try_from(hint)
.ok()
.and_then(Self::from_structure)
}

fn from_structure(image_structure: Structure) -> Option<Self> {
let fields = image_structure.fields();
if fields.len() < 7 {
return None;
}

let image_raw = match Array::try_from(&fields[6]) {
Ok(array) => array,
Err(_) => return None,
};

let width = i32::try_from(&fields[0]).ok()?;
let height = i32::try_from(&fields[1]).ok()?;
let rowstride = i32::try_from(&fields[2]).ok()?;
let has_alpha = bool::try_from(&fields[3]).ok()?;
let bits_per_sample = i32::try_from(&fields[4]).ok()?;
let channels = i32::try_from(&fields[5]).ok()?;

let data = image_raw
.iter()
.map(|value| u8::try_from(value).expect("expected u8"))
.collect::<Vec<_>>();

Some(ImageData {
width,
height,
rowstride,
has_alpha,
bits_per_sample,
channels,
data,
})
}
}

impl std::fmt::Debug for ImageData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ImageData")
.field("width", &self.width)
.field("height", &self.height)
.field("rowstride", &self.rowstride)
.field("has_alpha", &self.has_alpha)
.field("bits_per_sample", &self.bits_per_sample)
.field("channels", &self.channels)
.field("data", &"Vec<u8> [...]")
.finish()
}
}
5 changes: 3 additions & 2 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod notification;
pub mod dbus;
pub mod aliases;
pub mod dbus;
pub mod image;
pub mod notification;
Loading