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 7c810f815204b930dd6dc1e0512851e74edf2af8
parent 80d0f80b1bc7940b9246d13ca21acb4597d12d68
Author: MTRNord <mtrnord1@gmail.com>
Date:   Fri, 18 Dec 2020 01:21:23 +0100

feat: Implement layers fully

Took 1 hour 6 minutes

Diffstat:
M.idea/workspace.xml | 25+++++++++++++++++++++----
MCargo.lock | 36+++++++-----------------------------
Mlaunchpad_sway_controler/Cargo.toml | 1-
Mlaunchpad_sway_controler/src/main.rs | 33+++++++++++++++++++--------------
Mlaunchpad_sway_controler/src/mappings/mod.rs | 82++++++++++++++++++++++++-------------------------------------------------------
Mlaunchpad_sway_controler/src/sway/mod.rs | 15+++++++++++++++
6 files changed, 87 insertions(+), 105 deletions(-)

diff --git a/.idea/workspace.xml b/.idea/workspace.xml @@ -7,8 +7,12 @@ <component name="ChangeListManager"> <list default="true" id="cfc5721f-cc06-4df2-8555-3745919e4c56" name="Default Changelist" comment=""> <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$/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/mappings/mod.rs" beforeDir="false" afterPath="$PROJECT_DIR$/launchpad_sway_controler/src/mappings/mod.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" /> </list> <option name="SHOW_DIALOG" value="false" /> <option name="HIGHLIGHT_CONFLICTS" value="true" /> @@ -16,7 +20,7 @@ <option name="LAST_RESOLUTION" value="IGNORE" /> </component> <component name="DarkyenusTimeTracker"> - <option name="totalTimeSeconds" value="13866" /> + <option name="totalTimeSeconds" value="17813" /> <option name="idleThresholdMs" value="60000" /> <option name="autoCountIdleSeconds" value="0" /> <option name="stopWhenIdleRatherThanPausing" value="true" /> @@ -34,11 +38,15 @@ <option name="directoryName" value="ho9ja37x" /> </component> <component name="ProjectId" id="1lFAMettmogwp3he1jrYeQkqtz8" /> + <component name="ProjectLevelVcsManager"> + <ConfirmationsSetting value="2" id="Add" /> + </component> <component name="ProjectViewState"> <option name="hideEmptyMiddlePackages" value="true" /> <option name="showLibraryContents" value="true" /> </component> <component name="PropertiesComponent"> + <property name="ASKED_ADD_EXTERNAL_FILES" value="true" /> <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> <property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> <property name="WebServerToolWindowFactoryState" value="false" /> @@ -123,7 +131,7 @@ <workItem from="1607716945713" duration="4563000" /> <workItem from="1607936162789" duration="15000" /> <workItem from="1608061205255" duration="763000" /> - <workItem from="1608229569199" duration="8159000" /> + <workItem from="1608229569199" duration="14716000" /> </task> <task id="LOCAL-00001" summary="refactor: Use Workspace setup"> <created>1607182699156</created> @@ -160,16 +168,25 @@ <option name="project" value="LOCAL" /> <updated>1608242090649</updated> </task> - <option name="localTasksCounter" value="6" /> + <task id="LOCAL-00006" summary="refactor: Move layer action keys out of layer"> + <created>1608242637813</created> + <option name="number" value="00006" /> + <option name="presentableId" value="LOCAL-00006" /> + <option name="project" value="LOCAL" /> + <updated>1608242637813</updated> + </task> + <option name="localTasksCounter" value="7" /> <servers /> </component> <component name="VcsManagerConfiguration"> + <option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" /> <MESSAGE value="refactor: Use Workspace setup" /> <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" /> <MESSAGE value="feat: Prepare Matrix and Layers" /> - <option name="LAST_COMMIT_MESSAGE" value="feat: Prepare Matrix and Layers" /> + <MESSAGE value="refactor: Move layer action keys out of layer" /> + <option name="LAST_COMMIT_MESSAGE" value="refactor: Move layer action keys out of layer" /> </component> <component name="XSLT-Support.FileAssociations.UIState"> <expand /> diff --git a/Cargo.lock b/Cargo.lock @@ -1128,7 +1128,6 @@ dependencies = [ "futures-util", "midir", "once_cell", - "strum 0.20.0", "swayipc-async", "thirtyfour", "tokio 0.2.24", @@ -1987,7 +1986,7 @@ dependencies = [ "ruma-serde", "serde", "serde_json", - "strum 0.19.5", + "strum", "thiserror", ] @@ -2032,7 +2031,7 @@ dependencies = [ "ruma-serde", "serde", "serde_json", - "strum 0.19.5", + "strum", ] [[package]] @@ -2046,7 +2045,7 @@ dependencies = [ "ruma-serde", "serde", "serde_json", - "strum 0.19.5", + "strum", ] [[package]] @@ -2072,7 +2071,7 @@ dependencies = [ "ruma-serde", "serde", "serde_json", - "strum 0.19.5", + "strum", ] [[package]] @@ -2109,7 +2108,7 @@ dependencies = [ "ruma-identifiers-macros", "ruma-identifiers-validation", "serde", - "strum 0.19.5", + "strum", ] [[package]] @@ -2129,7 +2128,7 @@ version = "0.1.1" source = "git+https://github.com/ruma/ruma?rev=50eb700571480d1440e15a387d10f98be8abab59#50eb700571480d1440e15a387d10f98be8abab59" dependencies = [ "serde", - "strum 0.19.5", + "strum", ] [[package]] @@ -2505,16 +2504,7 @@ version = "0.19.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b89a286a7e3b5720b9a477b23253bc50debac207c8d21505f8e70b36792f11b5" dependencies = [ - "strum_macros 0.19.4", -] - -[[package]] -name = "strum" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c" -dependencies = [ - "strum_macros 0.20.1", + "strum_macros", ] [[package]] @@ -2530,18 +2520,6 @@ dependencies = [ ] [[package]] -name = "strum_macros" -version = "0.20.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn", -] - -[[package]] name = "subtle" version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/launchpad_sway_controler/Cargo.toml b/launchpad_sway_controler/Cargo.toml @@ -13,7 +13,6 @@ tokio = { version = "0.2", features = ["full"] } #TODO Update blocked by thirtyf futures-util = "0.3.7" color-eyre = "0.5" alsa = "0.4.3" -strum = { version = "0.20", features = ["derive"] } tracing = "0.1" tracing-subscriber = "0.2" thirtyfour = "0.21" 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::mappings::LAYERS; use crate::plugin_actions::PluginActions; use crate::sway::{get_workspaces, listen_for_workspace_changes}; -use crate::utils::globals::{CURRENT_WORKSPACE, PRESELECTED_LAYER_NUMBER}; +use crate::utils::globals::{CURRENT_WORKSPACE, LAYER_NUMBER, PRESELECTED_LAYER_NUMBER}; use color_eyre::Result; use midir::{Ignore, MidiInput, MidiOutputConnection}; use std::convert::{TryFrom, TryInto}; @@ -9,7 +9,6 @@ use std::env; use std::io::stdin; use std::process::exit; use std::sync::atomic::Ordering; -use strum::IntoEnumIterator; use swayipc_async::Connection; use tokio::runtime::Runtime; use tracing::*; @@ -21,9 +20,9 @@ mod utils; fn reset_colors(conn_out: &mut MidiOutputConnection) { conn_out.send(&[176, 0, 0]).unwrap(); - for value in LaunchpadMapping::iter() { + for (value, _) in LAYERS[LAYER_NUMBER.load(Ordering::SeqCst) as usize].iter() { conn_out - .send(&[144, (value as i32).try_into().unwrap(), 15]) + .send(&[144, (*value as i32).try_into().unwrap(), 15]) .unwrap(); } conn_out.send(&[144, 24, 15]).unwrap(); @@ -32,9 +31,11 @@ fn reset_colors(conn_out: &mut MidiOutputConnection) { if let Ok(workspace) = sway::WorkspaceLaunchpadMapping::try_from(CURRENT_WORKSPACE.load(Ordering::SeqCst) as i32) { - conn_out - .send(&[144, (workspace as i32).try_into().unwrap(), 28]) - .unwrap(); + if LAYER_NUMBER.load(Ordering::SeqCst) == workspace.get_layer() { + conn_out + .send(&[144, (workspace as i32).try_into().unwrap(), 28]) + .unwrap(); + } } } @@ -79,7 +80,7 @@ async fn main() -> Result<()> { run_action( Runtime::new().unwrap(), - PluginActions::ShowNumber( + &PluginActions::ShowNumber( PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) as usize, ), ); @@ -91,17 +92,21 @@ async fn main() -> Result<()> { run_action( Runtime::new().unwrap(), - PluginActions::ShowNumber( + &PluginActions::ShowNumber( PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) as usize, ), ); } if (message[1] as i32) == 40 { - run_action(Runtime::new().unwrap(), PluginActions::SelectLayer); + run_action(Runtime::new().unwrap(), &PluginActions::SelectLayer); } - if let Ok(v) = LaunchpadMapping::try_from(message[1] as i32) { - run_action(Runtime::new().unwrap(), v.into()); + if let Some(action) = LAYERS[LAYER_NUMBER.load(Ordering::SeqCst) as usize] + .iter() + .find(|(x, _)| *x == message[1] as i64) + .map(|(_, y)| y) + { + run_action(Runtime::new().unwrap(), action); } } }, @@ -124,7 +129,7 @@ async fn main() -> Result<()> { } } -fn run_action(mut rt: Runtime, action: PluginActions) { +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,59 +1,27 @@ use crate::plugin_actions::PluginActions; -use std::convert::TryFrom; -use strum::EnumIter; -// TODO make array for layers -#[non_exhaustive] -#[derive(Debug, Copy, Clone, EnumIter)] -pub enum LaunchpadMapping { - Back = 120, - Firefox = 119, - Chats = 118, - Email = 117, - Coding = 116, - Steam = 115, - Games = 114, - Youtube = 113, - Spotify = 112, - Test = 0, - FirefoxWebdriver = 1, -} - -impl Into<PluginActions<'_>> for LaunchpadMapping { - fn into(self) -> PluginActions<'static> { - match self { - LaunchpadMapping::Back => PluginActions::SwayBack, - LaunchpadMapping::Firefox => PluginActions::SwayWorkspace("1:\u{e007}"), - LaunchpadMapping::Chats => PluginActions::SwayWorkspace("2:\u{f086}"), - LaunchpadMapping::Email => PluginActions::SwayWorkspace("3:\u{f0e0}"), - LaunchpadMapping::Coding => PluginActions::SwayWorkspace("5:\u{f1c9}"), - LaunchpadMapping::Steam => PluginActions::SwayWorkspace("11:\u{f1b6}"), - LaunchpadMapping::Games => PluginActions::SwayWorkspace("12:\u{f11b}"), - LaunchpadMapping::Youtube => PluginActions::SwayWorkspace("13:\u{f167}"), - LaunchpadMapping::Spotify => PluginActions::SwayWorkspace("42:\u{f001}"), - LaunchpadMapping::Test => PluginActions::ExamplePlugin, - LaunchpadMapping::FirefoxWebdriver => PluginActions::SelectMatrixRoomByID(""), - } - } -} - -impl TryFrom<i32> for LaunchpadMapping { - type Error = (); - - fn try_from(input: i32) -> Result<LaunchpadMapping, Self::Error> { - match input { - 120 => Ok(LaunchpadMapping::Back), - 119 => Ok(LaunchpadMapping::Firefox), - 118 => Ok(LaunchpadMapping::Chats), - 117 => Ok(LaunchpadMapping::Email), - 116 => Ok(LaunchpadMapping::Coding), - 115 => Ok(LaunchpadMapping::Steam), - 114 => Ok(LaunchpadMapping::Games), - 113 => Ok(LaunchpadMapping::Youtube), - 112 => Ok(LaunchpadMapping::Spotify), - 0 => Ok(LaunchpadMapping::Test), - 1 => Ok(LaunchpadMapping::FirefoxWebdriver), - _ => Err(()), - } - } -} +pub static LAYERS: [&'static [(i64, PluginActions)]; 10] = [ + &[ + (120, PluginActions::SwayBack), + (119, PluginActions::SwayWorkspace("1:\u{e007}")), + (118, PluginActions::SwayWorkspace("2:\u{f086}")), + (117, PluginActions::SwayWorkspace("3:\u{f0e0}")), + (116, PluginActions::SwayWorkspace("5:\u{f1c9}")), + (115, PluginActions::SwayWorkspace("11:\u{f1b6}")), + (114, PluginActions::SwayWorkspace("12:\u{f11b}")), + (113, PluginActions::SwayWorkspace("13:\u{f167}")), + (112, PluginActions::SwayWorkspace("42:\u{f001}")), + ], + &[ + (0, PluginActions::ExamplePlugin), + (1, PluginActions::SelectMatrixRoomByID("")), + ], + &[], + &[], + &[], + &[], + &[], + &[], + &[], + &[], +]; diff --git a/launchpad_sway_controler/src/sway/mod.rs b/launchpad_sway_controler/src/sway/mod.rs @@ -40,6 +40,21 @@ pub enum WorkspaceLaunchpadMapping { Spotify = 112, } +impl WorkspaceLaunchpadMapping { + pub fn get_layer(&self) -> i64 { + match self { + WorkspaceLaunchpadMapping::Firefox => 0, + WorkspaceLaunchpadMapping::Chats => 0, + WorkspaceLaunchpadMapping::Email => 0, + WorkspaceLaunchpadMapping::Coding => 0, + WorkspaceLaunchpadMapping::Steam => 0, + WorkspaceLaunchpadMapping::Games => 0, + WorkspaceLaunchpadMapping::Youtube => 0, + WorkspaceLaunchpadMapping::Spotify => 0, + } + } +} + impl TryFrom<i32> for WorkspaceLaunchpadMapping { type Error = ();