Skip to content

Commit

Permalink
introduced unbound channel
Browse files Browse the repository at this point in the history
  • Loading branch information
medzernik committed Feb 3, 2023
1 parent a0cbb54 commit 550dcdc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
19 changes: 9 additions & 10 deletions wooting-macro-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -112,28 +112,27 @@ 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<rdev::EventType>) {
async fn execute(&self, send_channel: UnboundedSender<rdev::EventType>) {
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 => {
send_channel
.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;
Expand All @@ -142,7 +141,7 @@ impl Macro {
.send(rdev::EventType::KeyRelease(
SCANCODE_TO_RDEV[&data.keypress],
))
.await

.unwrap();
}
},
Expand Down Expand Up @@ -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<rdev::EventType>) {
async fn execute_macro(macros: Macro, channel: UnboundedSender<rdev::EventType>) {
match macros.macro_type {
MacroType::Single => {
info!("\nEXECUTING A SINGLE MACRO: {:#?}", macros.name);
Expand All @@ -283,7 +282,7 @@ async fn execute_macro(macros: Macro, channel: Sender<rdev::EventType>) {

/// 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<rdev::EventType>) {
fn keypress_executor_sender(mut rchan_execute: UnboundedReceiver<rdev::EventType>) {
loop {
plugin::util::send(&rchan_execute.blocking_recv().unwrap());

Expand All @@ -307,7 +306,7 @@ fn keypress_executor_sender(mut rchan_execute: Receiver<rdev::EventType>) {
fn check_macro_execution_efficiently(
pressed_events: Vec<u32>,
trigger_overview: Vec<Macro>,
channel_sender: Sender<rdev::EventType>,
channel_sender: UnboundedSender<rdev::EventType>,
) -> bool {
let trigger_overview_print = trigger_overview.clone();

Expand Down Expand Up @@ -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 || {
Expand Down
9 changes: 2 additions & 7 deletions wooting-macro-backend/src/plugin/mouse.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<EventType>) {
pub async fn execute(&self, send_channel: UnboundedSender<EventType>) {
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();
}
},
Expand All @@ -119,7 +115,6 @@ impl MouseAction {
x: *x as f64,
y: *y as f64,
})
.await
.unwrap();
}
}
Expand Down
4 changes: 2 additions & 2 deletions wooting-macro-backend/src/plugin/system_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -42,7 +42,7 @@ pub enum SystemAction {
impl SystemAction {
/// Execute the keys themselves.
pub async fn execute(&self, send_channel: Sender<rdev::EventType>) {
pub async fn execute(&self, send_channel: UnboundedSender<rdev::EventType>) {
match &self {
SystemAction::Open { action } => match action {
DirectoryAction::Directory { data } => {
Expand Down
10 changes: 3 additions & 7 deletions wooting-macro-backend/src/plugin/util.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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<rdev::EventType>, key: Vec<rdev::Key>) {
pub async fn send_key(send_channel: &UnboundedSender<rdev::EventType>, key: Vec<rdev::Key>) {
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<rdev::EventType>, key: Vec<rdev::Key>) {
pub async fn send_hotkey(send_channel: &UnboundedSender<rdev::EventType>, key: Vec<rdev::Key>) {
for press in &key {
send_channel
.send(rdev::EventType::KeyPress(*press))
.await
.unwrap();
}

for press in &key.into_iter().rev().collect::<Vec<rdev::Key>>() {
send_channel
.send(rdev::EventType::KeyRelease(*press))
.await
.unwrap();
}
}

0 comments on commit 550dcdc

Please sign in to comment.