sway-launchpad

A small rust script to switch sway workspaces using a Launchpad S
git clone git://archive.git.mtrnord.blog/MTRNord/sway-launchpad.git
Log | Files | Refs | LICENSE

commit 80d0f80b1bc7940b9246d13ca21acb4597d12d68
parent ca9fd52ee3a29fcec3b7396a8750b7374df439a2
Author: MTRNord <mtrnord1@gmail.com>
Date:   Thu, 17 Dec 2020 23:03:57 +0100

refactor: Move layer action keys out of layer

Took 8 minutes

Diffstat:
M.idea/workspace.xml | 30+++++++++++++-----------------
Mlaunchpad_sway_controler/src/main.rs | 50+++++++++++++++++++++++++++++++++++++++++---------
Mlaunchpad_sway_controler/src/mappings/mod.rs | 21---------------------
3 files changed, 54 insertions(+), 47 deletions(-)

diff --git a/.idea/workspace.xml b/.idea/workspace.xml @@ -6,21 +6,9 @@ </component> <component name="ChangeListManager"> <list default="true" id="cfc5721f-cc06-4df2-8555-3745919e4c56" name="Default Changelist" comment=""> - <change afterPath="$PROJECT_DIR$/Requirements.md" afterDir="false" /> - <change afterPath="$PROJECT_DIR$/launchpad_sway_controler/src/mappings/mod.rs" afterDir="false" /> - <change afterPath="$PROJECT_DIR$/launchpad_sway_controler/src/utils/globals.rs" afterDir="false" /> - <change afterPath="$PROJECT_DIR$/launchpad_sway_controler/src/utils/mod.rs" afterDir="false" /> - <change afterPath="$PROJECT_DIR$/launchpad_sway_controler/src/utils/numbers.rs" afterDir="false" /> - <change afterPath="$PROJECT_DIR$/lsc_matrix_presets/Cargo.toml" afterDir="false" /> - <change afterPath="$PROJECT_DIR$/lsc_matrix_presets/src/main.rs" afterDir="false" /> - <change beforePath="$PROJECT_DIR$/.idea/LaunchpadSwayControler.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/LaunchpadSwayControler.iml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> - <change beforePath="$PROJECT_DIR$/Cargo.lock" beforeDir="false" afterPath="$PROJECT_DIR$/Cargo.lock" afterDir="false" /> - <change beforePath="$PROJECT_DIR$/Cargo.toml" beforeDir="false" afterPath="$PROJECT_DIR$/Cargo.toml" afterDir="false" /> - <change beforePath="$PROJECT_DIR$/launchpad_sway_controler/Cargo.toml" beforeDir="false" afterPath="$PROJECT_DIR$/launchpad_sway_controler/Cargo.toml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/launchpad_sway_controler/src/main.rs" beforeDir="false" afterPath="$PROJECT_DIR$/launchpad_sway_controler/src/main.rs" afterDir="false" /> - <change beforePath="$PROJECT_DIR$/launchpad_sway_controler/src/plugin_actions/mod.rs" beforeDir="false" afterPath="$PROJECT_DIR$/launchpad_sway_controler/src/plugin_actions/mod.rs" afterDir="false" /> - <change beforePath="$PROJECT_DIR$/launchpad_sway_controler/src/sway/mod.rs" beforeDir="false" afterPath="$PROJECT_DIR$/launchpad_sway_controler/src/sway/mod.rs" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/launchpad_sway_controler/src/mappings/mod.rs" beforeDir="false" afterPath="$PROJECT_DIR$/launchpad_sway_controler/src/mappings/mod.rs" afterDir="false" /> </list> <option name="SHOW_DIALOG" value="false" /> <option name="HIGHLIGHT_CONFLICTS" value="true" /> @@ -28,7 +16,7 @@ <option name="LAST_RESOLUTION" value="IGNORE" /> </component> <component name="DarkyenusTimeTracker"> - <option name="totalTimeSeconds" value="13297" /> + <option name="totalTimeSeconds" value="13866" /> <option name="idleThresholdMs" value="60000" /> <option name="autoCountIdleSeconds" value="0" /> <option name="stopWhenIdleRatherThanPausing" value="true" /> @@ -135,7 +123,7 @@ <workItem from="1607716945713" duration="4563000" /> <workItem from="1607936162789" duration="15000" /> <workItem from="1608061205255" duration="763000" /> - <workItem from="1608229569199" duration="7540000" /> + <workItem from="1608229569199" duration="8159000" /> </task> <task id="LOCAL-00001" summary="refactor: Use Workspace setup"> <created>1607182699156</created> @@ -165,7 +153,14 @@ <option name="project" value="LOCAL" /> <updated>1607209625887</updated> </task> - <option name="localTasksCounter" value="5" /> + <task id="LOCAL-00005" summary="feat: Prepare Matrix and Layers"> + <created>1608242090649</created> + <option name="number" value="00005" /> + <option name="presentableId" value="LOCAL-00005" /> + <option name="project" value="LOCAL" /> + <updated>1608242090649</updated> + </task> + <option name="localTasksCounter" value="6" /> <servers /> </component> <component name="VcsManagerConfiguration"> @@ -173,7 +168,8 @@ <MESSAGE value="feat: Add ability to have multiple actions beyond workspace managment" /> <MESSAGE value="fix: on reset also disable LEDs again" /> <MESSAGE value="fix: reset colors when moving away from known workspaces" /> - <option name="LAST_COMMIT_MESSAGE" value="fix: reset colors when moving away from known workspaces" /> + <MESSAGE value="feat: Prepare Matrix and Layers" /> + <option name="LAST_COMMIT_MESSAGE" value="feat: Prepare Matrix and Layers" /> </component> <component name="XSLT-Support.FileAssociations.UIState"> <expand /> diff --git a/launchpad_sway_controler/src/main.rs b/launchpad_sway_controler/src/main.rs @@ -1,7 +1,7 @@ use crate::mappings::LaunchpadMapping; use crate::plugin_actions::PluginActions; use crate::sway::{get_workspaces, listen_for_workspace_changes}; -use crate::utils::globals::CURRENT_WORKSPACE; +use crate::utils::globals::{CURRENT_WORKSPACE, PRESELECTED_LAYER_NUMBER}; use color_eyre::Result; use midir::{Ignore, MidiInput, MidiOutputConnection}; use std::convert::{TryFrom, TryInto}; @@ -26,6 +26,9 @@ fn reset_colors(conn_out: &mut MidiOutputConnection) { .send(&[144, (value as i32).try_into().unwrap(), 15]) .unwrap(); } + conn_out.send(&[144, 24, 15]).unwrap(); + conn_out.send(&[144, 8, 15]).unwrap(); + conn_out.send(&[144, 40, 15]).unwrap(); if let Ok(workspace) = sway::WorkspaceLaunchpadMapping::try_from(CURRENT_WORKSPACE.load(Ordering::SeqCst) as i32) { @@ -61,9 +64,6 @@ async fn main() -> Result<()> { info!("Opening connections"); - // Create the runtime - let mut rt = Runtime::new()?; - // One could get the log back here out of the error let conn_in = midi_in .connect( @@ -72,12 +72,36 @@ async fn main() -> Result<()> { move |_, message, _| { if message[2] == 127 { debug!("{:?}", message); + if (message[1] as i32) == 8 { + if PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) < 9 { + PRESELECTED_LAYER_NUMBER.fetch_add(1, Ordering::SeqCst); + } + + run_action( + Runtime::new().unwrap(), + PluginActions::ShowNumber( + PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) as usize, + ), + ); + } + if (message[1] as i32) == 24 { + if PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) > 0 { + PRESELECTED_LAYER_NUMBER.fetch_sub(1, Ordering::SeqCst); + } + + run_action( + Runtime::new().unwrap(), + PluginActions::ShowNumber( + PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) as usize, + ), + ); + } + if (message[1] as i32) == 40 { + run_action(Runtime::new().unwrap(), PluginActions::SelectLayer); + } + if let Ok(v) = LaunchpadMapping::try_from(message[1] as i32) { - rt.block_on(async { - if let Err(e) = Into::<PluginActions>::into(v).run().await { - error!("Error while executing action: {}", e); - } - }); + run_action(Runtime::new().unwrap(), v.into()); } } }, @@ -99,3 +123,11 @@ async fn main() -> Result<()> { } } } + +fn run_action(mut rt: Runtime, action: PluginActions) { + rt.block_on(async { + if let Err(e) = action.run().await { + error!("Error while executing action: {}", e); + } + }); +} diff --git a/launchpad_sway_controler/src/mappings/mod.rs b/launchpad_sway_controler/src/mappings/mod.rs @@ -1,7 +1,5 @@ use crate::plugin_actions::PluginActions; -use crate::utils::globals::PRESELECTED_LAYER_NUMBER; use std::convert::TryFrom; -use std::sync::atomic::Ordering; use strum::EnumIter; // TODO make array for layers @@ -19,9 +17,6 @@ pub enum LaunchpadMapping { Spotify = 112, Test = 0, FirefoxWebdriver = 1, - IncreaseLayer = 8, - DecreaseLayer = 24, - SelectLayer = 40, } impl Into<PluginActions<'_>> for LaunchpadMapping { @@ -38,19 +33,6 @@ impl Into<PluginActions<'_>> for LaunchpadMapping { LaunchpadMapping::Spotify => PluginActions::SwayWorkspace("42:\u{f001}"), LaunchpadMapping::Test => PluginActions::ExamplePlugin, LaunchpadMapping::FirefoxWebdriver => PluginActions::SelectMatrixRoomByID(""), - LaunchpadMapping::IncreaseLayer => { - if PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) < 9 { - PRESELECTED_LAYER_NUMBER.fetch_add(1, Ordering::SeqCst); - } - PluginActions::ShowNumber(PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) as usize) - } - LaunchpadMapping::DecreaseLayer => { - if PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) > 0 { - PRESELECTED_LAYER_NUMBER.fetch_sub(1, Ordering::SeqCst); - } - PluginActions::ShowNumber(PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) as usize) - } - LaunchpadMapping::SelectLayer => PluginActions::SelectLayer, } } } @@ -71,9 +53,6 @@ impl TryFrom<i32> for LaunchpadMapping { 112 => Ok(LaunchpadMapping::Spotify), 0 => Ok(LaunchpadMapping::Test), 1 => Ok(LaunchpadMapping::FirefoxWebdriver), - 8 => Ok(LaunchpadMapping::IncreaseLayer), - 24 => Ok(LaunchpadMapping::DecreaseLayer), - 40 => Ok(LaunchpadMapping::SelectLayer), _ => Err(()), } }