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:
2024-06-26 04:00:44 +03:00
parent 9f53240807
commit 026a68364b
33 changed files with 694 additions and 271 deletions

View File

@@ -11,6 +11,7 @@ crate-type=["cdylib"]
anyhow = "1.0.86"
dim_sdk = {path="../../sdk/rust/dim_sdk"}
lazy_static = "1.4.0"
log = "0.4.21"
regex = "1.10.5"
serde = { version = "1.0.203", features = ["derive"] }
toml = "0.8.14"

View File

@@ -1,6 +1,6 @@
use std::{fmt::Write, path::PathBuf, process::Stdio};
use dim_sdk::{plugin_info, Context, DimPlugin};
use std::fs::read_to_string;
use std::{fmt::Write, process::Stdio};
use anyhow::bail;
use dim_sdk::{plugin_info, Context, DimPlugin, Message, Result};
mod config;
@@ -9,57 +9,60 @@ plugin_info!(
"volume", // Plugin name
"1.0.0", // Plugin Version (leave empty for none)
"GPLv3" // Plugin license (leave empty for none)
);
);
#[derive(Debug, Clone)]
struct Plug {
cfg: config::Config,
}
impl Plug {
pub fn new() -> Self {
Self {
cfg: config::Config::default()
}
}
ctx: Context
}
impl DimPlugin for Plug {
fn init(&mut self, ctx: Context) {
match config::Config::parse(&ctx.config_dir.join("config.toml")) {
Ok(c) => self.cfg = c,
fn init(ctx: Context) -> Result<Self> {
let cfg;
match config::Config::parse(&ctx.config_dir.join("config.toml").clone()) {
Ok(c) => cfg = c,
Err(e) => {
eprintln!("ERROR: Failed to open config file: {e}");
log::error!("Failed to open config file: {e}");
// TODO: Add function to disable the plugin
return;
bail!("")
}
}
}
fn poll(&mut self, f: &mut dim_sdk::CBuffer) -> dim_sdk::Result<()> {
let mut proc = {
let mut p = std::process::Command::new("sh");
p.arg("-c");
p.arg(&self.cfg.commands.get_volume);
p.stdout(Stdio::piped());
p
};
let output = String::from_utf8(proc.output()?.stdout)?;
let re = regex::Regex::new(&self.cfg.commands.get_volume_regex)?;
if let Some(caps) = re.captures(&output) {
let volume = &caps["vol"];
write!(f, "Vol: {volume}%")?;
Ok(Self { ctx, cfg })
}
fn on_message(&mut self, msg: Message) -> Result<()> {
self.ctx.buf.reset();
match msg.name.as_str() {
"poll" => {
let mut proc = {
let mut p = std::process::Command::new("sh");
p.arg("-c");
p.arg(&self.cfg.commands.get_volume);
p.stdout(Stdio::piped());
p
};
let output = String::from_utf8(proc.output()?.stdout)?;
let re = regex::Regex::new(&self.cfg.commands.get_volume_regex)?;
if let Some(caps) = re.captures(&output) {
let volume = &caps["vol"];
write!(self.ctx, "Vol: {volume}%")?;
}
}
_ => ()
}
Ok(())
}
fn pre_reload(&mut self) {
fn pre_reload(&mut self) -> Result<()> {
Ok(())
}
fn post_reload(&mut self) {
fn post_reload(&mut self) -> Result<()> {
Ok(())
}
fn free(&mut self) {
fn free(&mut self) -> Result<()> {
Ok(())
}
}