From 550dcdcd3aec9ca0ffe06756f896c8b427f80e50 Mon Sep 17 00:00:00 2001 From: medzernik <1900179+medzernik@users.noreply.github.com> Date: Tue, 24 Jan 2023 18:45:48 +0100 Subject: [PATCH] introduced unbound channel --- wooting-macro-backend/src/lib.rs | 19 +++++++++---------- wooting-macro-backend/src/plugin/mouse.rs | 9 ++------- .../src/plugin/system_event.rs | 4 ++-- wooting-macro-backend/src/plugin/util.rs | 10 +++------- 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/wooting-macro-backend/src/lib.rs b/wooting-macro-backend/src/lib.rs index e7c6d9b1..2091f6a1 100644 --- a/wooting-macro-backend/src/lib.rs +++ b/wooting-macro-backend/src/lib.rs @@ -12,7 +12,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::{thread, time}; -use tokio::sync::mpsc::{Receiver, Sender}; +use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; use tokio::sync::RwLock; use tokio::task; @@ -112,14 +112,13 @@ impl Macro { /// This function is used to execute a macro. It is called by the macro checker. /// It spawns async tasks to execute said events specifically. /// Make sure to expand this if you implement new action types. - async fn execute(&self, send_channel: Sender) { + async fn execute(&self, send_channel: UnboundedSender) { for action in &self.sequence { match action { ActionEventType::KeyPressEventAction { data } => match data.keytype { key_press::KeyType::Down => { send_channel .send(rdev::EventType::KeyPress(SCANCODE_TO_RDEV[&data.keypress])) - .await .unwrap(); } key_press::KeyType::Up => { @@ -127,13 +126,13 @@ impl Macro { .send(rdev::EventType::KeyRelease( SCANCODE_TO_RDEV[&data.keypress], )) - .await + .unwrap(); } key_press::KeyType::DownUp => { send_channel .send(rdev::EventType::KeyPress(SCANCODE_TO_RDEV[&data.keypress])) - .await + .unwrap(); tokio::time::sleep(time::Duration::from_millis(data.press_duration)).await; @@ -142,7 +141,7 @@ impl Macro { .send(rdev::EventType::KeyRelease( SCANCODE_TO_RDEV[&data.keypress], )) - .await + .unwrap(); } }, @@ -259,7 +258,7 @@ pub struct Collection { /// Executes a given macro (according to its type). /// /// ! **UNIMPLEMENTED** - Only Single macro type is implemented for now. -async fn execute_macro(macros: Macro, channel: Sender) { +async fn execute_macro(macros: Macro, channel: UnboundedSender) { match macros.macro_type { MacroType::Single => { info!("\nEXECUTING A SINGLE MACRO: {:#?}", macros.name); @@ -283,7 +282,7 @@ async fn execute_macro(macros: Macro, channel: Sender) { /// Receives and executes a macro based on the trigger event. /// Puts a mandatory 0-20 ms delay between each macro execution (depending on the platform). -fn keypress_executor_sender(mut rchan_execute: Receiver) { +fn keypress_executor_sender(mut rchan_execute: UnboundedReceiver) { loop { plugin::util::send(&rchan_execute.blocking_recv().unwrap()); @@ -307,7 +306,7 @@ fn keypress_executor_sender(mut rchan_execute: Receiver) { fn check_macro_execution_efficiently( pressed_events: Vec, trigger_overview: Vec, - channel_sender: Sender, + channel_sender: UnboundedSender, ) -> bool { let trigger_overview_print = trigger_overview.clone(); @@ -419,7 +418,7 @@ impl MacroBackend { let inner_is_listening = self.is_listening.clone(); // Spawn the channels - let (schan_execute, rchan_execute) = tokio::sync::mpsc::channel(1); + let (schan_execute, rchan_execute) = tokio::sync::mpsc::unbounded_channel(); //Create the executor thread::spawn(move || { diff --git a/wooting-macro-backend/src/plugin/mouse.rs b/wooting-macro-backend/src/plugin/mouse.rs index 19733956..93e3e8ce 100644 --- a/wooting-macro-backend/src/plugin/mouse.rs +++ b/wooting-macro-backend/src/plugin/mouse.rs @@ -1,7 +1,7 @@ use log::*; use rdev::EventType; use serde_repr; -use tokio::sync::mpsc::Sender; +use tokio::sync::mpsc::UnboundedSender; pub use rdev; @@ -80,32 +80,28 @@ pub enum MousePressAction { impl MouseAction { /// Creates a new MouseAction from a rdev event and sends it to the channel for async execution. - pub async fn execute(&self, send_channel: Sender) { + pub async fn execute(&self, send_channel: UnboundedSender) { match &self { MouseAction::Press { data } => match data { MousePressAction::Down { button } => { send_channel .send(rdev::EventType::ButtonPress(button.into())) - .await .unwrap(); } MousePressAction::Up { button } => { send_channel .send(rdev::EventType::ButtonRelease(button.into())) - .await .unwrap(); } MousePressAction::DownUp { button, duration } => { send_channel .send(rdev::EventType::ButtonPress(button.into())) - .await .unwrap(); tokio::time::sleep(time::Duration::from_millis(*duration as u64)).await; send_channel .send(rdev::EventType::ButtonRelease(button.into())) - .await .unwrap(); } }, @@ -119,7 +115,6 @@ impl MouseAction { x: *x as f64, y: *y as f64, }) - .await .unwrap(); } } diff --git a/wooting-macro-backend/src/plugin/system_event.rs b/wooting-macro-backend/src/plugin/system_event.rs index 4b92aa3f..efec494a 100644 --- a/wooting-macro-backend/src/plugin/system_event.rs +++ b/wooting-macro-backend/src/plugin/system_event.rs @@ -4,7 +4,7 @@ use fastrand; use log::*; use rdev; use std::vec; -use tokio::sync::mpsc::Sender; +use tokio::sync::mpsc::UnboundedSender; use crate::hid_table::SCANCODE_TO_RDEV; @@ -42,7 +42,7 @@ pub enum SystemAction { impl SystemAction { /// Execute the keys themselves. - pub async fn execute(&self, send_channel: Sender) { + pub async fn execute(&self, send_channel: UnboundedSender) { match &self { SystemAction::Open { action } => match action { DirectoryAction::Directory { data } => { diff --git a/wooting-macro-backend/src/plugin/util.rs b/wooting-macro-backend/src/plugin/util.rs index f1d692d4..1123433f 100644 --- a/wooting-macro-backend/src/plugin/util.rs +++ b/wooting-macro-backend/src/plugin/util.rs @@ -1,6 +1,6 @@ use log::*; use rdev; -use tokio::sync::mpsc::Sender; +use tokio::sync::mpsc::UnboundedSender; /// Sends an event to the library to Execute on an OS level. This makes it easier to implement keypresses in custom code. pub fn send(event_type: &rdev::EventType) { @@ -13,32 +13,28 @@ pub fn send(event_type: &rdev::EventType) { } } /// Sends a vector of keys to get processed -pub async fn send_key(send_channel: &Sender, key: Vec) { +pub async fn send_key(send_channel: &UnboundedSender, key: Vec) { for press in key { send_channel .send(rdev::EventType::KeyPress(press)) - .await .unwrap(); send_channel .send(rdev::EventType::KeyRelease(press)) - .await .unwrap(); } } /// Sends a vector of hotkeys to get processed -pub async fn send_hotkey(send_channel: &Sender, key: Vec) { +pub async fn send_hotkey(send_channel: &UnboundedSender, key: Vec) { for press in &key { send_channel .send(rdev::EventType::KeyPress(*press)) - .await .unwrap(); } for press in &key.into_iter().rev().collect::>() { send_channel .send(rdev::EventType::KeyRelease(*press)) - .await .unwrap(); } }