main.rs (4668B)
1 use crate::mappings::LAYERS; 2 use crate::plugin_actions::PluginActions; 3 use crate::sway::{get_workspaces, listen_for_workspace_changes}; 4 use crate::utils::globals::{CURRENT_WORKSPACE, LAYER_NUMBER, PRESELECTED_LAYER_NUMBER}; 5 use color_eyre::Result; 6 use midir::{Ignore, MidiInput, MidiOutputConnection}; 7 use std::convert::{TryFrom, TryInto}; 8 use std::env; 9 use std::io::stdin; 10 use std::process::exit; 11 use std::sync::atomic::Ordering; 12 use swayipc_async::Connection; 13 use tokio::runtime::Runtime; 14 use tracing::*; 15 16 mod mappings; 17 mod plugin_actions; 18 mod sway; 19 mod utils; 20 21 fn reset_colors(conn_out: &mut MidiOutputConnection) { 22 conn_out.send(&[176, 0, 0]).unwrap(); 23 for (value, color, _) in LAYERS[LAYER_NUMBER.load(Ordering::SeqCst) as usize].iter() { 24 conn_out 25 .send(&[ 26 144, 27 (*value as i32).try_into().unwrap(), 28 (*color as i32).try_into().unwrap(), 29 ]) 30 .unwrap(); 31 } 32 conn_out.send(&[144, 24, 63]).unwrap(); 33 conn_out.send(&[144, 8, 63]).unwrap(); 34 conn_out.send(&[144, 40, 63]).unwrap(); 35 if let Ok(workspace) = 36 sway::WorkspaceLaunchpadMapping::try_from(CURRENT_WORKSPACE.load(Ordering::SeqCst) as i32) 37 { 38 if LAYER_NUMBER.load(Ordering::SeqCst) == workspace.get_layer() { 39 conn_out 40 .send(&[144, (workspace as i32).try_into().unwrap(), 60]) 41 .unwrap(); 42 } 43 } 44 } 45 46 #[tokio::main] 47 async fn main() -> Result<()> { 48 color_eyre::install()?; 49 if env::var("RUST_LOG").is_err() { 50 env::set_var("RUST_LOG", "INFO"); 51 } 52 tracing_subscriber::fmt::init(); 53 54 // establish a connection to sway over a unix socket 55 let mut connection = Connection::new().await?; 56 57 get_workspaces(&mut connection).await?; 58 tokio::spawn(async { 59 listen_for_workspace_changes().await.unwrap(); 60 }); 61 let mut input = String::new(); 62 63 let mut midi_in = MidiInput::new("midir reading input")?; 64 midi_in.ignore(Ignore::None); 65 66 let midi_in_ports = midi_in.ports(); 67 68 let in_port = midi_in_ports.get(1).unwrap(); 69 70 info!("Opening connections"); 71 72 // One could get the log back here out of the error 73 let conn_in = midi_in 74 .connect( 75 in_port, 76 "midir-test", 77 move |_, message, _| { 78 if message[2] == 127 { 79 debug!("{:?}", message); 80 if (message[1] as i32) == 8 { 81 if PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) < 9 { 82 PRESELECTED_LAYER_NUMBER.fetch_add(1, Ordering::SeqCst); 83 } 84 85 run_action( 86 Runtime::new().unwrap(), 87 &PluginActions::ShowNumber( 88 PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) as usize, 89 ), 90 ); 91 } 92 if (message[1] as i32) == 24 { 93 if PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) > 0 { 94 PRESELECTED_LAYER_NUMBER.fetch_sub(1, Ordering::SeqCst); 95 } 96 97 run_action( 98 Runtime::new().unwrap(), 99 &PluginActions::ShowNumber( 100 PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) as usize, 101 ), 102 ); 103 } 104 if (message[1] as i32) == 40 { 105 run_action(Runtime::new().unwrap(), &PluginActions::SelectLayer); 106 } 107 108 if let Some(action) = LAYERS[LAYER_NUMBER.load(Ordering::SeqCst) as usize] 109 .iter() 110 .find(|(x, _, _)| *x == message[1] as i64) 111 .map(|(_, _, y)| y) 112 { 113 run_action(Runtime::new().unwrap(), action); 114 } 115 } 116 }, 117 (), 118 ) 119 .unwrap(); 120 121 info!("Connections open, enter `q` to exit ..."); 122 123 loop { 124 input.clear(); 125 stdin().read_line(&mut input)?; 126 if input.trim() == "q" { 127 info!("Closing connections"); 128 conn_in.close(); 129 //conn_out.close(); 130 info!("Connections closed"); 131 exit(0); 132 } 133 } 134 } 135 136 fn run_action(mut rt: Runtime, action: &PluginActions) { 137 rt.block_on(async { 138 if let Err(e) = action.run().await { 139 error!("Error while executing action: {}", e); 140 } 141 }); 142 }