mod.rs (6808B)
1 use crate::reset_colors; 2 use crate::utils::globals::{LAYER_NUMBER, PRESELECTED_LAYER_NUMBER, SHOWING_NUMBER}; 3 use crate::utils::show_number; 4 use color_eyre::Result; 5 use dbus::blocking::Connection as DBUSConnection; 6 use midir::MidiOutput; 7 use notify_rust::{Notification, Timeout}; 8 use std::sync::atomic::Ordering; 9 use std::time::Duration; 10 use swayipc_async::Connection; 11 use tokio::io::AsyncWriteExt; 12 use tokio::net::UnixStream; 13 use tracing::*; 14 use users::get_current_uid; 15 16 pub enum PluginActions<'a> { 17 SwayWorkspace(&'a str), 18 SwayBack, 19 ExamplePlugin, 20 SelectMatrixRoomByID(&'a str), 21 RunMatrixPreset(&'a str), 22 ShowNumber(usize), 23 SelectLayer, 24 MumbleToggleMute, 25 MumbleToggleDeaf, 26 SpotifyNextTrack, 27 SpotifyPrevTrack, 28 SpotifyPause, 29 } 30 31 impl PluginActions<'_> { 32 pub async fn run(&self) -> Result<()> { 33 let midi_out = MidiOutput::new("My Test Output")?; 34 let midi_out_ports = midi_out.ports(); 35 let out_port = midi_out_ports.get(1).unwrap(); 36 let mut conn_out = midi_out.connect(out_port, "midir-test").unwrap(); 37 match self { 38 PluginActions::SwayWorkspace(workspace) => { 39 notification(format!("Changed to workspace: {}", workspace).as_str()); 40 let mut connection = Connection::new().await?; 41 connection 42 .run_command(format!("workspace --no-auto-back-and-forth {}", workspace)) 43 .await 44 .unwrap(); 45 } 46 PluginActions::SwayBack => { 47 let mut connection = Connection::new().await?; 48 connection 49 .run_command("workspace back_and_forth") 50 .await 51 .unwrap(); 52 } 53 PluginActions::ExamplePlugin => { 54 let user_id = get_current_uid(); 55 let path_string = format!("/run/user/{}/lsc_example_plugin.sock", user_id); 56 57 let mut socket = UnixStream::connect(&path_string).await?; 58 59 socket.write(b"test\n").await?; 60 } 61 PluginActions::SelectMatrixRoomByID(room_id) => { 62 let user_id = get_current_uid(); 63 let path_string = format!("/run/user/{}/lsc_matrix_presets.sock", user_id); 64 let mut socket = UnixStream::connect(&path_string).await?; 65 66 socket 67 .write(format!("select {}\n", room_id).as_bytes()) 68 .await?; 69 } 70 PluginActions::RunMatrixPreset(preset_id) => { 71 let user_id = get_current_uid(); 72 let path_string = format!("/run/user/{}/lsc_matrix_presets.sock", user_id); 73 let mut socket = UnixStream::connect(&path_string).await?; 74 75 socket 76 .write(format!("do {}\n", preset_id).as_bytes()) 77 .await?; 78 } 79 PluginActions::ShowNumber(number) => { 80 notification(format!("Number shown: {}", number).as_str()); 81 show_number(&mut conn_out, *number); 82 } 83 PluginActions::SelectLayer => { 84 notification( 85 format!( 86 "Selected layer: {}", 87 PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst) 88 ) 89 .as_str(), 90 ); 91 LAYER_NUMBER.store( 92 PRESELECTED_LAYER_NUMBER.load(Ordering::SeqCst), 93 Ordering::SeqCst, 94 ); 95 SHOWING_NUMBER.store(false, Ordering::SeqCst); 96 reset_colors(&mut conn_out); 97 } 98 PluginActions::MumbleToggleMute => { 99 let conn = DBUSConnection::new_session()?; 100 let proxy = conn.with_proxy( 101 "net.sourceforge.mumble.mumble", 102 "/", 103 Duration::from_millis(5000), 104 ); 105 let (muted,): (bool,) = 106 proxy.method_call("net.sourceforge.mumble.Mumble", "isSelfMuted", ())?; 107 notification(format!("Changed muted to: {}", !muted).as_str()); 108 let (): () = proxy.method_call( 109 "net.sourceforge.mumble.Mumble", 110 "setSelfMuted", 111 (!muted,), 112 )?; 113 } 114 PluginActions::MumbleToggleDeaf => { 115 let conn = DBUSConnection::new_session()?; 116 let proxy = conn.with_proxy( 117 "net.sourceforge.mumble.mumble", 118 "/", 119 Duration::from_millis(5000), 120 ); 121 let (deaf,): (bool,) = 122 proxy.method_call("net.sourceforge.mumble.Mumble", "isSelfDeaf", ())?; 123 notification(format!("Changed Deaf to: {}", !deaf).as_str()); 124 let (): () = 125 proxy.method_call("net.sourceforge.mumble.Mumble", "setSelfDeaf", (!deaf,))?; 126 } 127 PluginActions::SpotifyNextTrack => { 128 let conn = DBUSConnection::new_session()?; 129 let proxy = conn.with_proxy( 130 "org.mpris.MediaPlayer2.spotify", 131 "/org/mpris/MediaPlayer2", 132 Duration::from_millis(5000), 133 ); 134 let (): () = proxy.method_call("org.mpris.MediaPlayer2.Player", "Next", ())?; 135 } 136 PluginActions::SpotifyPrevTrack => { 137 let conn = DBUSConnection::new_session()?; 138 let proxy = conn.with_proxy( 139 "org.mpris.MediaPlayer2.spotify", 140 "/org/mpris/MediaPlayer2", 141 Duration::from_millis(5000), 142 ); 143 // We send it twice as it first just goes back to the track start 144 let (): () = proxy.method_call("org.mpris.MediaPlayer2.Player", "Previous", ())?; 145 let (): () = proxy.method_call("org.mpris.MediaPlayer2.Player", "Previous", ())?; 146 } 147 PluginActions::SpotifyPause => { 148 let conn = DBUSConnection::new_session()?; 149 let proxy = conn.with_proxy( 150 "org.mpris.MediaPlayer2.spotify", 151 "/org/mpris/MediaPlayer2", 152 Duration::from_millis(5000), 153 ); 154 let (): () = proxy.method_call("org.mpris.MediaPlayer2.Player", "PlayPause", ())?; 155 } 156 } 157 Ok(()) 158 } 159 } 160 161 fn notification(text: &str) { 162 Notification::new() 163 .appname("Launchpad Macros") 164 .summary("Launchpad Macros") 165 .body(text) 166 .timeout(Timeout::Milliseconds(700)) //milliseconds 167 .show() 168 .unwrap(); 169 }