Too lazy to make propper edit but i changed the
polling system to a message one so we can have more ways we can interract with plugins (reload/ reset/ on click events)
This commit is contained in:
@@ -10,3 +10,4 @@ crate-type=["cdylib"]
|
||||
[dependencies]
|
||||
dim_sdk = {path="../../sdk/rust/dim_sdk"}
|
||||
lazy_static = "1.4.0"
|
||||
log = "0.4.21"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{fmt::Write, path::PathBuf};
|
||||
use dim_sdk::{plugin_info, Context, DimPlugin};
|
||||
use dim_sdk::{plugin_info, Context, DimPlugin, Message, Result};
|
||||
use std::fs::read_to_string;
|
||||
|
||||
plugin_info!(
|
||||
@@ -11,39 +11,38 @@ plugin_info!(
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Plug {
|
||||
}
|
||||
|
||||
impl Plug {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
}
|
||||
}
|
||||
ctx: Context
|
||||
}
|
||||
|
||||
const BATT0: &'static str = "/sys/class/power_supply/BAT0/capacity";
|
||||
const BATT1: &'static str = "/sys/class/power_supply/BAT1/capacity";
|
||||
|
||||
impl DimPlugin for Plug {
|
||||
fn init(&mut self, _ctx: Context) {
|
||||
fn init(ctx: Context) -> Result<Self> {
|
||||
dim_sdk::DimLogger::new(&ctx).init();
|
||||
Ok(Self {
|
||||
ctx
|
||||
})
|
||||
}
|
||||
fn poll(&mut self, f: &mut dim_sdk::CBuffer) -> dim_sdk::Result<()> {
|
||||
//TODO: Quick fix, make this better, more portable
|
||||
let path = if PathBuf::from(BATT0).exists() { BATT0 } else { BATT1 };
|
||||
fn on_message(&mut self, msg: Message) -> Result<()> {
|
||||
self.ctx.buf.reset();
|
||||
match msg.name.as_str() {
|
||||
"poll" => {
|
||||
//TODO: Quick fix, make this better, more portable
|
||||
let path = if PathBuf::from(BATT0).exists() { BATT0 } else { BATT1 };
|
||||
|
||||
let contents = read_to_string(path)?;
|
||||
let cleaned_contents: String = contents.chars().filter(|c| c.is_digit(10)).collect();
|
||||
let contents = read_to_string(path)?;
|
||||
let cleaned_contents: String = contents.chars().filter(|c| c.is_digit(10)).collect();
|
||||
|
||||
write!(f, "Battery: {}%", cleaned_contents)?;
|
||||
write!(self.ctx, "Battery: {}%", cleaned_contents)?;
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn pre_reload(&mut self) {
|
||||
}
|
||||
fn post_reload(&mut self) {
|
||||
|
||||
}
|
||||
fn free(&mut self) {
|
||||
|
||||
}
|
||||
fn pre_reload(&mut self) -> Result<()> { Ok(()) }
|
||||
fn post_reload(&mut self) -> Result<()> { Ok(()) }
|
||||
fn free(&mut self) -> Result<()> { Ok(()) }
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user