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 de99c0c2a9cf165e7288f55506cf9c75c8e65475
parent 0659769a49f60bd03f4d3b6ea476a3dead9d0f29
Author: MTRNord <mtrnord1@gmail.com>
Date:   Fri,  4 Dec 2020 23:56:21 +0100

feat: Update colors on start and on any change even made from external shortcuts

Diffstat:
MCargo.lock | 8++++----
MCargo.toml | 2+-
Msrc/main.rs | 8+++-----
Msrc/sway/mod.rs | 44+++++++++++++++++++++++++++++++++++---------
4 files changed, 43 insertions(+), 19 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -872,9 +872,9 @@ dependencies = [ [[package]] name = "swayipc-async" -version = "2.0.0-alpha" +version = "2.0.0-alpha.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "425557b41ed8276439d558288dfd9daf41a7689f22724f7e691c5184be26306d" +checksum = "32336e32760a74d88cc4366af2855f8979b39a0449d34a8d4edf84d862363c78" dependencies = [ "async-io", "async-process", @@ -886,9 +886,9 @@ dependencies = [ [[package]] name = "swayipc-types" -version = "1.0.0-alpha" +version = "1.0.0-alpha.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e1995c83609f317032f801e0e37a239c6d537322340a75aa70b0f349ce57995" +checksum = "875548743fc78f29520a26e761c9b0c6e8a59d0e4aede907aa2fd80230390d8e" dependencies = [ "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" [dependencies] midir = "0.7" -swayipc-async = "2.0.0-alpha" +swayipc-async = "2.0.0-alpha.1" tokio = { version = "0.3.2", features = ["full"] } futures-util = "0.3.7" color-eyre = "0.5" diff --git a/src/main.rs b/src/main.rs @@ -49,10 +49,9 @@ async fn main() -> Result<()> { let midi_out_ports = midi_out.ports(); let out_port = midi_out_ports.get(1).unwrap(); - println!("\nOpening connections"); + println!("Opening connections"); let mut conn_out = midi_out.connect(out_port, "midir-test").unwrap(); - reset_colors(&mut conn_out); // Create the runtime let rt = Runtime::new()?; @@ -62,10 +61,9 @@ async fn main() -> Result<()> { .connect( in_port, "midir-test", - move |stamp, message, _| { - // The last of the three callback parameters is the object that we pass in as last parameter of `connect`. - println!("{}: {:?} (len = {})", stamp, message, message.len()); + move |_, message, _| { if message[2] == 127 { + println!("{:?}", message); if let Ok(v) = LaunchpadWorkspaceMapping::try_from(message[1] as i32) { rt.block_on(async { let mut connection = Connection::new().await.unwrap(); diff --git a/src/sway/mod.rs b/src/sway/mod.rs @@ -1,13 +1,30 @@ +use crate::reset_colors; use color_eyre::Result; use futures_util::stream::StreamExt; -use std::convert::TryFrom; +use midir::MidiOutput; +use std::convert::{TryFrom, TryInto}; use std::fmt::{Display, Formatter}; use strum::EnumIter; use swayipc_async::{Connection, Event, EventType}; pub async fn get_workspaces(connection: &mut Connection) -> Result<()> { - // request and print the i3 version - println!("{:#?}", connection.get_workspaces().await?); + let workspaces = connection.get_workspaces().await?; + println!("{:#?}", workspaces); + + for space in workspaces { + if space.focused { + let midi_out = MidiOutput::new("My Test Output")?; + let midi_out_ports = midi_out.ports(); + let out_port = midi_out_ports.get(1).unwrap(); + let mut conn_out = midi_out.connect(out_port, "midir-test").unwrap(); + if let Ok(workspace) = WorkspaceLaunchpadMapping::try_from(space.num) { + reset_colors(&mut conn_out); + conn_out + .send(&[144, (workspace as i32).try_into().unwrap(), 28]) + .unwrap(); + } + } + } Ok(()) } @@ -81,15 +98,24 @@ pub async fn listen_for_workspace_changes() -> Result<()> { // subscribe to a workspace events. let subs = [EventType::Workspace]; let mut events = Connection::new().await?.subscribe(&subs).await?; + let midi_out = MidiOutput::new("My Test Output")?; + let midi_out_ports = midi_out.ports(); + let out_port = midi_out_ports.get(1).unwrap(); + let mut conn_out = midi_out.connect(out_port, "midir-test").unwrap(); while let Some(event) = events.next().await { if let Ok(ref event) = event { if let Event::Workspace(ref w) = event { - println!("{:?}", w.current); - /* - TODO make this somehow work but I really dont know how as the midir crate is horrible - let workspace = WorkspaceLaunchpadMapping::try_from(w.current.unwrap().num)?; - reset_colors(&mut conn_out); - conn_out.send(&[144, (workspace as i32).try_into().unwrap(), 28]).unwrap();*/ + if let Some(v) = &w.current { + if let Some(num) = v.num { + // TODO make ? work + if let Ok(workspace) = WorkspaceLaunchpadMapping::try_from(num) { + reset_colors(&mut conn_out); + conn_out + .send(&[144, (workspace as i32).try_into().unwrap(), 28]) + .unwrap(); + } + } + } } } //println!("{:?}", event)