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

mod.rs (3616B)


      1 use crate::reset_colors;
      2 use crate::utils::globals::{CURRENT_WORKSPACE, SHOWING_NUMBER};
      3 use color_eyre::Result;
      4 use futures_util::stream::StreamExt;
      5 use midir::MidiOutput;
      6 use std::convert::TryFrom;
      7 use std::sync::atomic::Ordering;
      8 use swayipc_async::{Connection, Event, EventType, WorkspaceChange};
      9 use tracing::*;
     10 
     11 pub async fn get_workspaces(connection: &mut Connection) -> Result<()> {
     12     let workspaces = connection.get_workspaces().await?;
     13     debug!("{:#?}", workspaces);
     14 
     15     for space in workspaces {
     16         if space.focused {
     17             let midi_out = MidiOutput::new("My Test Output")?;
     18             let midi_out_ports = midi_out.ports();
     19             let out_port = midi_out_ports.get(1).unwrap();
     20             let mut conn_out = midi_out.connect(out_port, "midir-test").unwrap();
     21             CURRENT_WORKSPACE.store(space.num as i64, Ordering::SeqCst);
     22             if !SHOWING_NUMBER.load(Ordering::SeqCst) {
     23                 reset_colors(&mut conn_out);
     24             }
     25         }
     26     }
     27     Ok(())
     28 }
     29 
     30 #[non_exhaustive]
     31 #[derive(Debug, Copy, Clone)]
     32 pub enum WorkspaceLaunchpadMapping {
     33     Firefox = 119,
     34     Chats = 118,
     35     Email = 117,
     36     Coding = 116,
     37     Steam = 115,
     38     Games = 114,
     39     Youtube = 113,
     40     Spotify = 112,
     41 }
     42 
     43 impl WorkspaceLaunchpadMapping {
     44     pub fn get_layer(&self) -> i64 {
     45         match self {
     46             WorkspaceLaunchpadMapping::Firefox => 0,
     47             WorkspaceLaunchpadMapping::Chats => 0,
     48             WorkspaceLaunchpadMapping::Email => 0,
     49             WorkspaceLaunchpadMapping::Coding => 0,
     50             WorkspaceLaunchpadMapping::Steam => 0,
     51             WorkspaceLaunchpadMapping::Games => 0,
     52             WorkspaceLaunchpadMapping::Youtube => 0,
     53             WorkspaceLaunchpadMapping::Spotify => 0,
     54         }
     55     }
     56 }
     57 
     58 impl TryFrom<i32> for WorkspaceLaunchpadMapping {
     59     type Error = ();
     60 
     61     fn try_from(input: i32) -> Result<WorkspaceLaunchpadMapping, Self::Error> {
     62         match input {
     63             1 => Ok(WorkspaceLaunchpadMapping::Firefox),
     64             2 => Ok(WorkspaceLaunchpadMapping::Chats),
     65             3 => Ok(WorkspaceLaunchpadMapping::Email),
     66             5 => Ok(WorkspaceLaunchpadMapping::Coding),
     67             11 => Ok(WorkspaceLaunchpadMapping::Steam),
     68             12 => Ok(WorkspaceLaunchpadMapping::Games),
     69             13 => Ok(WorkspaceLaunchpadMapping::Youtube),
     70             42 => Ok(WorkspaceLaunchpadMapping::Spotify),
     71             _ => Err(()),
     72         }
     73     }
     74 }
     75 
     76 pub async fn listen_for_workspace_changes() -> Result<()> {
     77     // subscribe to a workspace events.
     78     let subs = [EventType::Workspace];
     79     let mut events = Connection::new().await?.subscribe(&subs).await?;
     80     let midi_out = MidiOutput::new("My Test Output").unwrap();
     81     let midi_out_ports = midi_out.ports();
     82     let out_port = midi_out_ports.get(1).unwrap();
     83     let mut conn_out = midi_out.connect(out_port, "midir-test").unwrap();
     84     while let Some(event) = events.next().await {
     85         if let Ok(ref event) = event {
     86             if let Event::Workspace(ref w) = event {
     87                 if w.change == WorkspaceChange::Focus {
     88                     if let Some(v) = &w.current {
     89                         if let Some(num) = v.num {
     90                             CURRENT_WORKSPACE.store(num as i64, Ordering::SeqCst);
     91                             // TODO make ? work
     92                             if !SHOWING_NUMBER.load(Ordering::SeqCst) {
     93                                 reset_colors(&mut conn_out);
     94                             }
     95                         }
     96                     }
     97                 }
     98             }
     99         }
    100         //println!("{:?}", event)
    101     }
    102 
    103     Ok(())
    104 }