initial commit. First working basic setup.

This commit is contained in:
Fl1tzi 2023-03-28 23:33:27 +02:00
parent 392b6f31aa
commit 8c32cd611a
10 changed files with 739 additions and 2 deletions

21
Cargo.toml Normal file
View File

@ -0,0 +1,21 @@
[package]
name = "dach-decker"
version = "0.1.0"
edition = "2021"
license = "MIT"
[dependencies]
hidapi = "2.2.0"
elgato-streamdeck = { version = "0.2.4", features = ["async"] }
toml = "0.7.2"
tokio = { version = "1", features = ["full"] }
log = "0.4"
dirs = "4.0.0"
serde = { version = "1.0", features = ["derive"] }
simple_logger = "4.0.0"
image = "0.24.5"
async-trait = "0.1.66"
futures-util = "0.3.27"
lazy_static = "1.4.0"
imageproc = "0.23.0"
rusttype = "0.9.3"

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) <year> <copyright holders>
Fl1tzi<git@tgerber.net>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

@ -1,3 +1,4 @@
# dach-decker
Dach-Decker or Dachdecker (eng. roofer) is a software to configure your Stream Deck with an easy to use configuration file.
Dach-Decker or Dachdecker (eng. roofer) is a software to configure your Stream Deck on Linux with an easy to use configuration file.

5
fonts/README.md Normal file
View File

@ -0,0 +1,5 @@
# Fonts
This folder holds all fonts which may be used by the modules.
Thanks to the creators of these awesome fonts!

View File

@ -0,0 +1,94 @@
Copyright 2020 The Space Grotesk Project Authors (https://github.com/floriankarsten/space-grotesk)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

BIN
fonts/SpaceGrotesk.ttf Normal file

Binary file not shown.

326
src/main.rs Normal file
View File

@ -0,0 +1,326 @@
use hidapi::HidApi;
use log::{debug, error, info, trace, warn};
use serde::Deserialize;
use simple_logger;
use tokio::sync::mpsc::error::TrySendError;
use std::collections::HashMap;
use std::io::ErrorKind;
use std::process::exit;
use std::sync::Arc;
use std::time::Duration;
use std::{env, path::PathBuf};
use std::fs;
use tokio::process::Command;
use tokio::sync::mpsc;
use tokio::sync::Mutex;
mod modules;
use elgato_streamdeck as streamdeck;
use streamdeck::asynchronous::{AsyncStreamDeck, ButtonStateUpdate};
use dirs::config_dir;
use crate::modules::{start_module, HostEvent};
/// The name of the folder which holds the config
pub const CONFIG_FOLDER_NAME: &'static str = "dach-decker";
macro_rules! skip_if_none {
($res:expr) => {
match $res {
Some(v) => v,
None => continue,
}
};
}
/// The config structure
#[derive(Deserialize, Debug)]
pub struct Config {
global: Option<GlobalConfig>,
device: Vec<DeviceConfig>,
}
#[derive(Deserialize, Debug)]
struct GlobalConfig {
default_font: Option<String>
}
fn main() {
simple_logger::init_with_env().unwrap();
let config_file: PathBuf = match env::var_os("DACH_DECKER_CONFIG") {
Some(path) => PathBuf::from(path),
None => {
if let Some(mut path) = config_dir() {
path.push(CONFIG_FOLDER_NAME);
path.push("config.toml");
path
} else {
error!("Please use the \"DACH_DECKER_CONFIG\" environment variable to provide a path to your config");
exit(1);
}
}
};
info!("Loading configuration from \"{}\"", config_file.display());
let config: Config = match fs::read_to_string(config_file) {
Ok(content) => match toml::from_str(&content) {
Ok(c) => c,
Err(e) => {
error!("Error detected in configuration:\n{}", e);
exit(1);
}
},
Err(file_error) => {
if file_error.kind() == ErrorKind::NotFound {
error!("Unable to load configuration because the file does not exist. Please create the configuration file.");
} else {
error!("Cannot open the configuration file: {}", file_error);
}
exit(1);
}
};
debug!("{:#?}", config);
// hidapi
let hid = match streamdeck::new_hidapi() {
Ok(v) => v,
Err(e) => {
error!("HidApi Error:\n{}", e);
exit(1);
}
};
// list devices
// TODO: allow hotplug
let devices = streamdeck::list_devices(&hid);
// lets start some async
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(start(config, hid, devices))
}
pub async fn start(config: Config, hid: HidApi, hw_devices: Vec<(streamdeck::info::Kind, String)>) {
init_devices(config, hid, hw_devices).await;
// TODO: PLEASE IMPROVE THIS!!
// Issue is that tokio sleeps are not kept running while they are sleeping which results in the
// program exiting...
//
// However, this will stay open even if the program is nothing doing anymore.
loop {
tokio::time::sleep(Duration::from_secs(2000)).await;
}
}
/// This is the entry point for the application. This will check all devices for their config,
/// start the bridges and the device button listeners.
async fn init_devices(config: Config, hid: HidApi, devices: Vec<(streamdeck::info::Kind, String)>) {
// check if configuration is correct for device
if devices.len() == 0 {
error!("There are no Decks connected");
exit(1);
}
info!("There are {} Decks connected", devices.len());
'outer: for device in devices {
// no pedals are supported
if !device.0.is_visual() {
continue;
}
// device.1 is the serial number
if let Some(device_conf) = config.device.iter().find(|s| s.serial == device.1) {
// connect to deck or continue to next
let deck = match AsyncStreamDeck::connect(&hid, device.0, &device.1) {
Ok(deck) => {
info!("Successfully connected to {}", device.1);
deck
}
Err(e) => {
error!("Failed to connect to Deck {}:\n{}", device.1, e);
continue 'outer;
}
};
// set brightness
deck.set_brightness(device_conf.brightness).await.unwrap();
// reset
deck.reset().await.unwrap();
// initialize buttons
// let mut bridges: Vec<Bridge> = Vec::new();
let button_count = device.0.key_count();
let mut buttons_key = HashMap::new();
for button in device_conf.buttons.clone().into_iter() {
// if the index of the button is higher than the button count
if button_count < button.index {
warn!(
"The button {} does not exist on Deck {}; skipping",
button.index, device.1
);
continue 'outer;
}
// check if the action has the correct syntax
for key in vec![&button.on_click, &button.on_release] {
if let Some(a) = key {
for action in a {
if !action.starts_with("bash:") && !action.starts_with("sh:") {
error!(
"Unknown action in button {} on Deck {}; skipping",
button.index, device.1
);
continue 'outer;
}
}
}
}
// create a watch channel for the module to receive device events
let (button_sender, button_receiver) = mpsc::channel(4);
buttons_key.insert(
button.index,
(
button_sender,
(button.on_click.clone(), button.on_release.clone()),
),
);
// spawn the module
let b = button.clone();
let rx = Arc::new(Mutex::new(button_receiver));
let dev = deck.clone();
tokio::spawn(async move {
start_module(b, dev, rx).await;
});
}
// start the device key listener
tokio::spawn(async move {
device_key_listener(deck, buttons_key).await;
});
} else {
info!("Deck {} is not configured; skipping", device.1);
}
}
}
/// listener for button press changes on the device. Also executes the scripts.
pub async fn device_key_listener(
device: Arc<AsyncStreamDeck>,
mut keys: HashMap<
u8,
(
mpsc::Sender<HostEvent>,
(Option<Vec<String>>, Option<Vec<String>>),
),
>,
) {
loop {
match device.get_reader().read(7.0).await {
Ok(v) => {
trace!("Received Keypress: {:?}", v);
for update in v {
match update {
ButtonStateUpdate::ButtonDown(i) => {
let options = skip_if_none!(keys.get(&i));
let actions = &options.1 .0;
if send_key_event(options, actions, HostEvent::ButtonPressed).await == false {
debug!("Removed key {} from listeners (receiver dropped)", &i);
keys.remove(&i);
}
}
ButtonStateUpdate::ButtonUp(i) => {
let options = skip_if_none!(keys.get(&i));
let actions = &options.1.1;
/* let sender = &options.0;
let on_release = &options.1 .1;
if let Some(actions) = on_release {
execute_button_action(actions).await;
} else {
if sender.try_send(HostEvent::ButtonReleased).is_err() {
keys.remove(&i);
debug!("Removed key {} from listeners (does not respond)", &i);
}
}*/
if send_key_event(options, actions, HostEvent::ButtonReleased).await == false {
debug!("Removed key {} from listeners (receiver dropped)", &i);
keys.remove(&i);
}
}
}
}
}
Err(e) => {
error!("Error while retrieving key status: {:?}", e);
}
}
}
}
/// manually sends the script event or try to send it to the module.
/// Returns false if the receiver is dead and can therefore be removed.
pub async fn send_key_event(options: &(mpsc::Sender<HostEvent>, (Option<Vec<String>>, Option<Vec<String>>)), actions: &Option<Vec<String>>, event: HostEvent) -> bool {
let sender = &options.0;
if let Some(actions) = actions {
execute_button_action(actions).await;
} else {
if let Err(e) = sender.try_send(event) {
match e {
TrySendError::Full(_) => trace!("Buffer full: {:?}", e),
TrySendError::Closed(_) => {
return false
}
}
}
}
true
}
/// executes a shell script
pub async fn execute_button_action(actions: &Vec<String>) {
for a in actions {
if let Some(v) = a.strip_prefix("bash:") {
execute_bash(v).await;
} else if let Some(v) = a.strip_prefix("sh:") {
execute_sh(v).await;
} else {
unreachable!()
}
}
}
pub async fn execute_bash(command: &str) {
match Command::new("/bin/bash").arg(command).output().await {
Ok(o) => debug!("Command \'{}\' returned: {}", command, o.status),
Err(e) => error!("Command \'{}\' failed: {}", command, e),
}
}
pub async fn execute_sh(command: &str) {
match Command::new("sh").arg(command).output().await {
Ok(o) => debug!("Command \'{}\' returned: {}", command, o.status),
Err(e) => error!("Command \'{}\' failed: {}", command, e),
}
}
#[derive(Deserialize, Debug, Clone)]
pub struct DeviceConfig {
pub serial: String,
#[serde(default = "default_brightness")]
pub brightness: u8,
pub buttons: Vec<Button>,
}
fn default_brightness() -> u8 {
100
}
#[derive(Deserialize, Debug, Clone)]
pub struct Button {
index: u8,
module: String,
/// options which get passed to the module
options: Option<HashMap<String, String>>,
/// allows to overwrite what it will do on a click
/// available options:
/// - \"sh:date\" - executes in sh
/// - \"bash:date\" - executes in bash
pub on_click: Option<Vec<String>>,
/// allows to overwrite what it will do on a release; Same options as [on_click]
pub on_release: Option<Vec<String>>,
}

227
src/modules.rs Normal file
View File

@ -0,0 +1,227 @@
mod blank;
mod counter;
use std::{
collections::HashMap, error::Error, future::Future, hash::Hash, pin::Pin, sync::Arc,
thread::JoinHandle,
};
use crate::{Button, DeviceConfig};
use async_trait::async_trait;
pub use elgato_streamdeck as streamdeck;
use futures_util::TryFutureExt;
use image::DynamicImage;
use log::{debug, error, info, trace, warn};
pub use streamdeck::info::ImageFormat;
use streamdeck::AsyncStreamDeck;
pub use streamdeck::StreamDeckError;
use streamdeck::info::Kind;
use tokio::sync::mpsc;
use tokio::{
sync::Mutex,
time::{sleep, Duration},
};
use self::blank::Blank;
use self::counter::Counter;
use lazy_static::lazy_static;
type ModuleFunction = Box<dyn Fn(DeviceAccess, ChannelReceiver, Button) -> Result<(), ReturnError>>;
/// Events that are coming from the host
#[derive(Clone, Copy, Debug)]
pub enum HostEvent {
/// The button was pressed
ButtonPressed,
/// The button was released
ButtonReleased,
/// The channel was initialized and there were no events yet
Init,
}
/// starts a module
pub async fn start_module(
button: Button,
device: Arc<AsyncStreamDeck>,
br: Arc<Mutex<mpsc::Receiver<HostEvent>>>,
) {
trace!("Starting MODULE {}", button.index);
let b = button.clone();
let da = DeviceAccess::new(device, button.index).await;
let module = match button.module.as_str() {
"counter" => Counter::run(da, br, b),
_ => {
error!("Module \'{}\' does not exist", button.module);
Blank::run(da, br, b)
}
};
match module.await {
Ok(_) => info!("MODULE {} closed", button.index),
Err(e) => error!("MODULE {}: {:?}", button.index, e),
}
/*
match Counter::run(device_access, button_receiver, button.clone()).await {
Ok(_) => info!("MODULE {} closed", button.index),
Err(e) => error!("MODULE {}: {:?}", button.index, e)
}*/
}
/* #[derive(Clone)]
pub struct Bridge;
impl Bridge {
/*pub async fn start(&mut self) {
/*tokio::join!(self.listener(), ColorModule::run(channel, button))
.1
.unwrap();*/
/*tokio::select! {
v = ColorModule::run(self.button_receiver, button) => {
match v {
Ok(_) => info!("MODULE {} closed", self.button.index),
Err(e) => error!("MODULE {}: {:?}", self.button.index, e)
}
},
_ = self.listener() => {}
}*/
/*tokio::spawn(ColorModule::run(channel, button));
loop {
self.listener().await;
sleep(Duration::from_millis(10)).await;
}*/
}*/
/* pub async fn listener(&mut self) {
loop {
if let Ok(event) = self.host_receiver.try_recv() {
trace!("MODULE {}: {:?}", self.button.index, event);
match event {
ModuleEvent::Subscribe(e) => self.events.push(e),
ModuleEvent::Image(i) => {
println!("UPLOADED IMAGE")
}
}
}
sleep(Duration::from_millis(20)).await;
}
}*/
pub async fn start_module<F, Fut>(&self, config: Button, module: F)
where
// Has to be the same as [Module::run]
F: FnOnce(Receiver<HostEvent>, Button) -> Fut,
Fut: Future<Output = Result<(), Box<dyn Error>>>,
{
//let channel = ModuleChannel::new(self.sender.clone(), self.receiver.clone()).await;
//module(channel, config).await;
}
}*/
/*
/// A wrapper around the channel to provide easier communication for the module. It is designed to
/// be not blocking.
pub struct ModuleChannel {
pub sender: Sender<ModuleEvent>,
pub receiver: Receiver<HostEvent>,
}
#[derive(Debug)]
pub enum ModuleChannelError {
/// The host disconnected
HostDisconnected,
/// The message buffer is full. Either the host or the module is not consuming
/// messages.
BufferFull,
/// The message buffer does not include any events for the module
Empty,
}*/
/* impl ModuleChannel {
pub async fn new(
sender: Sender<ModuleEvent>,
receiver: Receiver<HostEvent>,
) -> ModuleChannel {
ModuleChannel { sender, receiver }
}
/// Send a message without blocking - [crossbeam_channel::Sender::try_send]
pub async fn send(&self, message: ModuleEvent) -> Result<(), ModuleChannelError> {
self.sender
.try_send(message)
.map_err(|e| match e {
crossbeam_channel::TrySendError::Full(_) => ModuleChannelError::BufferFull,
crossbeam_channel::TrySendError::Disconnected(_) => {
ModuleChannelError::HostDisconnected
}
})
}
/// Receive messages without blocking - [crossbeam_channel::Receiver::try_recv]
pub async fn receive(&self) -> Result<HostEvent, ModuleChannelError> {
self.receiver.try_recv().map_err(|e| {
match e {
crossbeam_channel::TryRecvError::Empty => ModuleChannelError::Empty,
crossbeam_channel::TryRecvError::Disconnected => {
ModuleChannelError::HostDisconnected
}
}
})
}
pub async fn update_image(&self, img: DynamicImage) {}
}*/
/// Wrapper to provide easier access to the Deck
pub struct DeviceAccess {
streamdeck: Arc<AsyncStreamDeck>,
kind: Kind,
index: u8,
}
impl DeviceAccess {
pub async fn new(streamdeck: Arc<AsyncStreamDeck>, index: u8) -> DeviceAccess {
let kind = streamdeck.kind();
DeviceAccess { streamdeck, kind, index }
}
/// write a raw image to the Deck
pub async fn write_raw_img(&self, img: &[u8]) -> Result<(), StreamDeckError> {
self.streamdeck.write_image(self.index, img).await
}
/// Write an image to the Deck
pub async fn write_img(&self, img: DynamicImage) -> Result<(), StreamDeckError> {
self.streamdeck.set_button_image(self.index, img).await
}
/// reset the image
pub async fn clear_img(&self) -> Result<(), StreamDeckError> {
self.streamdeck.clear_button_image(self.index).await
}
pub fn format(&self) -> ImageFormat {
self.kind.key_image_format()
}
/// The resolution of the image on the Deck
pub fn resolution(&self) -> (usize, usize) {
self.format().size
}
pub fn kind(&self) -> Kind {
self.kind
}
}
pub type ReturnError = Box<dyn Error + Send + Sync>;
pub type ChannelReceiver = Arc<Mutex<mpsc::Receiver<HostEvent>>>;
#[async_trait]
pub trait Module {
async fn run(
device: DeviceAccess,
button_receiver: ChannelReceiver,
config: Button,
) -> Result<(), ReturnError>;
}

19
src/modules/blank.rs Normal file
View File

@ -0,0 +1,19 @@
use super::Button;
use super::ChannelReceiver;
use super::DeviceAccess;
use super::Module;
use super::ReturnError;
use async_trait::async_trait;
pub struct Blank;
#[async_trait]
impl Module for Blank {
async fn run(
_streamdeck: DeviceAccess,
_button_receiver: ChannelReceiver,
_config: Button,
) -> Result<(), ReturnError> {
Ok(())
}
}

44
src/modules/counter.rs Normal file
View File

@ -0,0 +1,44 @@
use crate::Button;
use super::Module;
use super::{ChannelReceiver, DeviceAccess, HostEvent, ReturnError};
use async_trait::async_trait;
use image::{Rgb, RgbImage};
use imageproc::drawing::draw_text_mut;
use rusttype::{Scale, Font};
/// A module which displays a counter
pub struct Counter;
#[async_trait]
impl Module for Counter {
async fn run(
streamdeck: DeviceAccess,
button_receiver: ChannelReceiver,
_config: Button,
) -> Result<(), ReturnError> {
let font_data: &[u8] = include_bytes!("../../fonts/SpaceGrotesk.ttf");
let font: Font<'static> = Font::try_from_bytes(font_data).unwrap();
let (h, w) = streamdeck.resolution();
let mut stream = button_receiver.lock().await;
let mut counter: u32 = 0;
loop {
if let Some(event) = stream.recv().await {
match event {
HostEvent::ButtonPressed => {
counter += 1;
let mut image = RgbImage::new(h as u32, w as u32);
draw_text_mut(&mut image, Rgb([255, 255, 255]), 10, 10, Scale::uniform(20.0), &font, format!("{}", counter).as_str());
streamdeck.write_img(image::DynamicImage::ImageRgb8(image)).await.unwrap();
},
_ => {}
}
}
}
}
}