diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..4394696 --- /dev/null +++ b/Cargo.toml @@ -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" diff --git a/LICENSE b/LICENSE index 2071b23..8a30ea5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Fl1tzi 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: diff --git a/README.md b/README.md index 7434c26..e03f9f5 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +Dach-Decker or Dachdecker (eng. roofer) is a software to configure your Stream Deck on Linux with an easy to use configuration file. + diff --git a/fonts/README.md b/fonts/README.md new file mode 100644 index 0000000..cf2bc20 --- /dev/null +++ b/fonts/README.md @@ -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! diff --git a/fonts/SpaceGrotesk-LICENSE b/fonts/SpaceGrotesk-LICENSE new file mode 100644 index 0000000..579cdbf --- /dev/null +++ b/fonts/SpaceGrotesk-LICENSE @@ -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. + diff --git a/fonts/SpaceGrotesk.ttf b/fonts/SpaceGrotesk.ttf new file mode 100644 index 0000000..d0e5eff Binary files /dev/null and b/fonts/SpaceGrotesk.ttf differ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..786aa0f --- /dev/null +++ b/src/main.rs @@ -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, + device: Vec, +} + +#[derive(Deserialize, Debug)] +struct GlobalConfig { + default_font: Option +} + +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 = 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, + mut keys: HashMap< + u8, + ( + mpsc::Sender, + (Option>, Option>), + ), + >, +) { + 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, (Option>, Option>)), actions: &Option>, 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) { + 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