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 cd23caab1b568e4706af5d1bb0dced4b973f6584
parent 6950a5725a31fc6bf5d2023c5182d57b51069731
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sat,  5 Dec 2020 00:13:04 +0100

fix: Only listen to focus changes

Diffstat:
Msrc/main.rs | 10+---------
Msrc/sway/mod.rs | 20+++++++++++---------
2 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -3,7 +3,7 @@ use crate::sway::{ WorkspaceLaunchpadMapping, }; use color_eyre::Result; -use midir::{Ignore, MidiInput, MidiOutput, MidiOutputConnection}; +use midir::{Ignore, MidiInput, MidiOutputConnection}; use std::convert::{TryFrom, TryInto}; use std::env; use std::io::stdin; @@ -42,7 +42,6 @@ async fn main() -> Result<()> { let mut midi_in = MidiInput::new("midir reading input")?; midi_in.ignore(Ignore::None); - let midi_out = MidiOutput::new("My Test Output")?; let midi_in_ports = midi_in.ports(); /*for (i, p) in midi_in_ports.iter().enumerate() { @@ -52,13 +51,8 @@ async fn main() -> Result<()> { }*/ let in_port = midi_in_ports.get(1).unwrap(); - let midi_out_ports = midi_out.ports(); - let out_port = midi_out_ports.get(1).unwrap(); - info!("Opening connections"); - let mut conn_out = midi_out.connect(out_port, "midir-test").unwrap(); - // Create the runtime let rt = Runtime::new()?; @@ -78,8 +72,6 @@ async fn main() -> Result<()> { .await .unwrap(); }); - reset_colors(&mut conn_out); - conn_out.send(&[144, message[1], 28]).unwrap(); } } }, diff --git a/src/sway/mod.rs b/src/sway/mod.rs @@ -5,7 +5,7 @@ use midir::MidiOutput; use std::convert::{TryFrom, TryInto}; use std::fmt::{Display, Formatter}; use strum::EnumIter; -use swayipc_async::{Connection, Event, EventType}; +use swayipc_async::{Connection, Event, EventType, WorkspaceChange}; use tracing::*; pub async fn get_workspaces(connection: &mut Connection) -> Result<()> { @@ -106,14 +106,16 @@ pub async fn listen_for_workspace_changes() -> Result<()> { while let Some(event) = events.next().await { if let Ok(ref event) = event { if let Event::Workspace(ref w) = event { - 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(); + if w.change == WorkspaceChange::Focus { + 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(); + } } } }