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(()) }
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::fmt::Write;
|
||||
use chrono::prelude::*;
|
||||
use dim_sdk::{plugin_info, Context, DimPlugin};
|
||||
use dim_sdk::{plugin_info, Context, DimPlugin, Result};
|
||||
|
||||
plugin_info!(
|
||||
Plug, // Your main global structs name that implements `DimPlugin`
|
||||
@@ -11,27 +11,31 @@ plugin_info!(
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Plug {
|
||||
ctx: Context
|
||||
}
|
||||
|
||||
impl Plug {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DimPlugin for Plug {
|
||||
fn init(&mut self, _ctx: Context) {}
|
||||
fn poll(&mut self, f: &mut dim_sdk::CBuffer) -> dim_sdk::Result<()> {
|
||||
let local: DateTime<Local> = Local::now();
|
||||
let formatted_time = local.format("%H:%M (%Y-%m-%d)").to_string();
|
||||
log::info!("Hello from clocky :3");
|
||||
write!(f, "Time: {}", formatted_time)?;
|
||||
fn init(ctx: Context) -> Result<Self> {
|
||||
Ok(Self {
|
||||
ctx
|
||||
})
|
||||
}
|
||||
fn on_message(&mut self, msg: dim_sdk::Message) -> Result<()> {
|
||||
self.ctx.buf.reset();
|
||||
match msg.name.as_str() {
|
||||
"poll" => {
|
||||
let local: DateTime<Local> = Local::now();
|
||||
let formatted_time = local.format("%H:%M (%Y-%m-%d)").to_string();
|
||||
write!(self.ctx, "Time: {}", formatted_time)?;
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
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(()) }
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,3 +11,4 @@ crate-type=["cdylib"]
|
||||
chrono = "0.4.38"
|
||||
dim_sdk = {path="../../sdk/rust/dim_sdk"}
|
||||
lazy_static = "1.4.0"
|
||||
log = "0.4.21"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::fmt::Write;
|
||||
use dim_sdk::{plugin_info, Context, DimPlugin};
|
||||
use dim_sdk::{plugin_info, Context, DimPlugin, Message, Result};
|
||||
use chrono::{NaiveDate, Local};
|
||||
|
||||
plugin_info!(
|
||||
@@ -11,30 +11,32 @@ plugin_info!(
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Plug {
|
||||
}
|
||||
|
||||
impl Plug {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
||||
}
|
||||
}
|
||||
ctx: Context
|
||||
}
|
||||
|
||||
impl DimPlugin for Plug {
|
||||
fn init(&mut self, _ctx: Context) {}
|
||||
fn poll(&mut self, f: &mut dim_sdk::CBuffer) -> dim_sdk::Result<()> {
|
||||
fn init(ctx: Context) -> Result<Self> {
|
||||
Ok(Self {
|
||||
ctx
|
||||
})
|
||||
}
|
||||
fn on_message(&mut self, msg: Message) -> Result<()> {
|
||||
self.ctx.buf.reset();
|
||||
match msg.name.as_str() {
|
||||
"poll" => {
|
||||
let start_date = NaiveDate::from_ymd_opt(2024, 3, 8).expect("Invalid date");
|
||||
let current_date = Local::now().date_naive();
|
||||
let duration = current_date.signed_duration_since(start_date);
|
||||
|
||||
let start_date = NaiveDate::from_ymd_opt(2024, 3, 8).expect("Invalid date");
|
||||
let current_date = Local::now().date_naive();
|
||||
let duration = current_date.signed_duration_since(start_date);
|
||||
|
||||
write!(f, "{} days <3", duration.num_days())?;
|
||||
write!(self.ctx, "{} days <3", duration.num_days())?;
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
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(()) }
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
// Only use this define once, cause it defines funcitons
|
||||
#define PLUG_IMPL
|
||||
#include "dim_sdk.h"
|
||||
|
||||
PLUG_INFO("example_c", "0.0.1", "GPLv3")
|
||||
@@ -9,31 +10,38 @@ PLUG_INFO("example_c", "0.0.1", "GPLv3")
|
||||
typedef struct plug_t {
|
||||
char* some_data;
|
||||
int count;
|
||||
plug_ctx_t ctx;
|
||||
} plug_t;
|
||||
|
||||
plug_t* p = {0};
|
||||
|
||||
void plug_init(plug_ctx_t* ctx) {
|
||||
(void) ctx;
|
||||
// If any function returns 0/NULL the plugin will be disabled
|
||||
int plug_init(plug_ctx_t* ctx) {
|
||||
setup_ctx(ctx);
|
||||
p = malloc(sizeof(plug_t));
|
||||
assert(p != NULL && "Buy more ram KEKW");
|
||||
p->count = 0;
|
||||
p->ctx = *ctx;
|
||||
|
||||
printf("Hello from plugin");
|
||||
// log(INFO, "Hewo fwom C :3");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* plug_pre_reload() {
|
||||
return p;
|
||||
}
|
||||
|
||||
void plug_post_reload(void *state) {
|
||||
int plug_post_reload(void *state) {
|
||||
p = state;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void plug_poll(char *buf, size_t len) {
|
||||
snprintf(buf, len, "Hello from C! (%d)", p->count++);
|
||||
int plug_on_msg(plug_msg_t* msg) {
|
||||
snprintf(p->ctx.buf, BUF_SZ, "Hello from C! (%d)", p->count++);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void plug_free() {
|
||||
int plug_free() {
|
||||
free(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
use dim_sdk::{plugin_info, Context, DimPlugin};
|
||||
use dim_sdk::{plugin_info, Context, DimPlugin, Message, Result};
|
||||
|
||||
plugin_info!(
|
||||
Plug, // Your main global structs name that implements `DimPlugin`
|
||||
@@ -10,37 +10,45 @@ plugin_info!(
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Plug {
|
||||
ctx: Context,
|
||||
counter: usize,
|
||||
}
|
||||
|
||||
impl Plug {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
counter: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DimPlugin for Plug {
|
||||
fn init(&mut self, _ctx: Context) {
|
||||
fn init(ctx: Context) -> dim_sdk::Result<Self> {
|
||||
// Initialise data, this will run once, it will not run again after reload
|
||||
// log::info!("hewo from rust :3");
|
||||
Ok(Self {
|
||||
ctx,
|
||||
counter: 0
|
||||
})
|
||||
}
|
||||
fn poll(&mut self, f: &mut dim_sdk::CBuffer) -> dim_sdk::Result<()> {
|
||||
// Write to buffer the text you want to display, keep this short
|
||||
write!(f, "Hello from rust! ({})", self.counter)?;
|
||||
self.counter += 1;
|
||||
fn on_message(&mut self, msg: Message) -> dim_sdk::Result<()> {
|
||||
self.ctx.buf.reset();
|
||||
match msg.name.as_str() {
|
||||
"poll" => {
|
||||
// Write to buffer the text you want to display, keep this short
|
||||
write!(self.ctx, "Hello from rust! ({})", self.counter)?;
|
||||
self.counter += 1;
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
fn pre_reload(&mut self) {
|
||||
fn pre_reload(&mut self) -> Result<()> {
|
||||
// Do stuff before reload, probably save important things because theres a good chance
|
||||
// (especially on rust) that if you change the data layout it will die
|
||||
Ok(())
|
||||
}
|
||||
fn post_reload(&mut self) {
|
||||
fn post_reload(&mut self) -> Result<()>{
|
||||
// Do stuff after reloading plugin, state a.k.a this struct has the same data, will crash
|
||||
// if the data layout changed
|
||||
Ok(())
|
||||
}
|
||||
fn free(&mut self) {
|
||||
fn free(&mut self) -> Result<()> {
|
||||
// Yout probably dont need this but its for freeing things before the plugin gets unloaded
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user