Skip to content

Commit

Permalink
trying to move away from arc<mutex>
Browse files Browse the repository at this point in the history
  • Loading branch information
ATTron committed Jul 4, 2024
1 parent 2ce6063 commit 7153cf7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 49 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "swaddle"
version = "0.1.2"
version = "0.1.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
62 changes: 19 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
collections::HashMap,
error::Error,
process::{Child, Command},
sync::atomic::AtomicBool,
sync::{Arc, Mutex},
thread::sleep,
time::{Duration, Instant},
Expand All @@ -16,20 +17,17 @@ trait DBusInterface {
fn add_match(&self) -> Result<(), Box<dyn Error>>;
}

trait CommandCaller {
fn execute_command(&self) -> Result<(), Box<dyn Error>>;
}
struct DBusRunner {
connection: Arc<Connection>,
good_to_send: Arc<Mutex<bool>>,
good_to_send: Arc<AtomicBool>,
}

impl DBusRunner {
pub fn new() -> Result<Self, Box<dyn Error>> {
let connection = Connection::new_session()?;
Ok(DBusRunner {
connection: Arc::new(connection),
good_to_send: Arc::new(Mutex::new(false)),
good_to_send: Arc::new(AtomicBool::new(false)),
})
}
}
Expand All @@ -40,21 +38,17 @@ impl DBusInterface for DBusRunner {
.with_interface(INTERFACE_NAME)
.with_namespaced_path(DBUS_NAMESPACE);

let sending_clone = Arc::clone(&self.good_to_send);
let good_to_send = Arc::clone(&self.good_to_send);
self.connection.add_match(rule, move |_: (), _, msg| {
let items: HashMap<String, Variant<Box<dyn RefArg>>> =
msg.read3::<String, HashMap<_, _>, Vec<String>>().unwrap().1;
if let Some(playback_status) = items.get("PlaybackStatus") {
if let Some(status) = playback_status.0.as_str() {
if status == "Paused" {
if let Ok(mut send_it) = sending_clone.lock() {
*send_it = false;
}
good_to_send.store(false, std::sync::atomic::Ordering::SeqCst);
}
if status == "Playing" {
if let Ok(mut send_it) = sending_clone.lock() {
*send_it = true;
}
good_to_send.store(true, std::sync::atomic::Ordering::SeqCst);
}
}
}
Expand Down Expand Up @@ -101,19 +95,21 @@ impl IdleApp {
.process(Duration::from_millis(1000))
{
Ok(_) => {
if let Ok(block) = self.dbus_runner.good_to_send.lock() {
if *block {
match self.run_cmd() {
Ok(child) => inhibit.0 = Some(child),
Err(e) => {
eprintln!("unable to block swayidle :: {:?}", e)
}
}
} else if !*block {
if let Some(mut killing) = inhibit.0.take() {
let _ = killing.kill();
let block = self
.dbus_runner
.good_to_send
.load(std::sync::atomic::Ordering::SeqCst);
if block {
match self.run_cmd() {
Ok(child) => inhibit.0 = Some(child),
Err(e) => {
eprintln!("unable to block swayidle :: {:?}", e)
}
}
} else if !block {
if let Some(mut killing) = inhibit.0.take() {
let _ = killing.kill();
}
}
}
Err(e) => eprintln!("Error handling D-Bus message: {:?}", e),
Expand Down Expand Up @@ -199,23 +195,3 @@ mod idle_app_tests {
assert!(app.is_ok());
}
}

#[cfg(test)]
mod command_caller_tests {
use super::*;

struct MockCommandCaller;

impl CommandCaller for MockCommandCaller {
fn execute_command(&self) -> Result<(), Box<dyn Error>> {
Ok(())
}
}

#[test]
fn test_execute_command() {
let mock_caller = MockCommandCaller;
let result = mock_caller.execute_command();
assert!(result.is_ok());
}
}

0 comments on commit 7153cf7

Please sign in to comment.